Google Apps Script .setvalues For loop not working - arrays

The goal of the loop is to fill each cell over 797 rows across 5 columns A, B, C, D and E with a formula whose cell reference increments by 1.
E.g. Column A rows 6 onwards will have formula "=indirect("'Data Repository'!A3++")"
Column B rows 6 onwards will have formula "=indirect("'Data Repository'!B3++")"
What happens when I run the function however is it only fills in column A. I've checked the execution transcript and execution succeeded is logged after the first column has been filled up. I've tried various variations to no avail.
Below is the last variation I've tested:
function indirect(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Fleet - Weekly V3");
var formulaArray = [];
var columns = ["A","B","C","D","E"];
var row = 2;
var text = '=indirect(\"\'Data Repository\'!';
var headerRow = 6;
var column;
for(i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(i = 0; i < 797; i++) {
row += 1;
if (formulaArray.length == 797) {
sheet.getRange(headerRow, column).offset(0, 0, formulaArray.length).setValues(formulaArray);
} else {
formulaArray.push([formula + row + '")']);
}
Logger.log(formulaArray.length);
}
Logger.log(i)
formulaArray = [];
}
}

Here is where you might be making an error - you need to create the variable i (var i = 0 instead of just i = 0) and if you're nesting loops, you need to have different variables increasing (first loop use i, then nest with j, then nest in that with k etc as needed)
for(var i = 0; i < 5; i++) {
column = parseInt(i) + 1;
formula = text + columns[i];
for(var j = 0; j < 797; j++) {
Untested but I believe it should work if you just substitute that in.

Your problem is in your loops. You are using the 'i' variable twice. Change the for loop that you have nested to iterate over the variable 'j' or something other than 'i'.

Related

Need help in looping

I've range of data, and I want to loop the range 52 times and after completing one loop it should add 7 with date last used,
Something like this :-
I have demonstrated it 2 times, but I want to do it 52 times or n number of times.
Need help in figuring out how I can do this, either by formula or script!
Wrote a script for this, it should repeat the range n number of times with dates incrementing by 7 days. Please take a look at the nested for loop for the manipulation of the 2D array.
function dateFill() {
var sheet = SpreadsheetApp.getActiveSheet();
// constants
var numOfIteration = 52;
var DAY_IN_MS = 1000*60*60*24;
var nameArray = ['A','B','C','D','E','F'];
var newArray = [];
var startDate = new Date(sheet.getRange("B1").getValue());
for (i = 0; i < numOfIteration; i++) {
var newDate = new Date();
newDate.setTime(startDate.getTime() + 7*i*DAY_IN_MS);
for (j = 0; j < nameArray.length; j++) {
newArray.push([nameArray[j], newDate]);
}
}
var lr = sheet.getLastRow();
var writeRange = sheet.getRange(lr+1,1,nameArray.length * numOfIteration,2);
writeRange.setValues(newArray);
}
Sample:

Google App Script breaking singular element into 1D array with several elements

I've created a custom script in GAS (for Google Sheets) so I could join several data sources into one unique display. I added a .splice in the middle so I could cut out all null elements inside the arrays (which meant removing blank rows in the return array).
Here is it how the code goes:
function COMPILER(){
var i;
var c;
var r;
var display = [];
for (i = 0; i < arguments.length; i++) {
for (c = 0; c < arguments[i].length;c++) {
display.push(arguments[i][c]);
};};
for (r = display.length-1; r >= 0; r--) {
if (display[r][1]==""){
display.splice(r, 1)
};};
return display
};
The code works fine with 2+D arrays and with 1D arrays that have 2+ elements; but when its working with 1D arrays that have only one element, it unintendedly breaks down the strings into several elements.
Instead of returning:
ar1
ar2
It returns
a
r
1
a
r
2
How could I solve this?
I think/assume this is what you're trying to do:
function COMPILER()
{
var display = [];
for(var i = 0, numArguments = arguments.length; i < numArguments; ++i)
{
//console.log([i, arguments[i], Array.isArray(arguments[i])]);
if(Array.isArray(arguments[i]))
{
for(var j = 0, len = arguments[i].length; j < len; ++j)
{
if(arguments[i][j] != "")
{
display.push(arguments[i][j]);
}
}
}
else if(arguments[i] != "")
{
display.push(arguments[i]);
}
}
return display;
}
I can't confirm/test cause I don't know how you're calling COMPILER.

Adding a fixed number of unique items to an array using a for loop and indexOf

I'm using a for loop to get 6 random numbers between 1-20, using indexOf to omit duplicates, and pushing them to an array.
However, I always want 6 items in the array, so I'd like duplicates to be replaced. In my naive code duplicates are simply omitted, which means that sometimes I'll get less than 6 in the array. How do I replace the omissions to fill those 6 array slots?
function rolld(event:MouseEvent) {
for (i = 0; i < 6; i++){
d = (Math.floor(Math.random() * (1 + d_hi - d_lo)) + d_lo);
if (rollArray.indexOf(d) < 0){
rollArray.push(d);
}
}
trace (rollArray);
}
Still very new to this. Thanks for any help!
When you get an element from array with 20 elements, try to remove it from array.
function rolld(event:MouseEvent) {
var elements:Array = [];
for (var i:int = 1; i <= 20; i++)
{
elements.push(i);
}
for (i = 0; i < 6; i++){
d = (Math.floor(Math.random() * elements.length);
rollArray.push(d);
//remove the element
elements.splice(d, 1);
}
trace (rollArray);
}

Two Dimension Array with Custom Type Elements

I'm trying to create on my scene an n x n size matrix, each element should be a Movie Clip, named Table, already prepared in the Library.
var tables:Array.<Table> = new Array.<Table>(tablesDimension, tablesDimension);
for (var i:int = 0; i < tablesDimension; i++) {
for (var j:int = 0; j < tablesDimension; j++) {
var tempTable:Table = new Table();
tempTable.x = i * 150 + 100;
tempTable.y = j * 100 + 100;
stage.addChild(tempTable);
tables.push(tempTable);
trace(tables[0][0].x);
}
}
A Movie Clip (in this case, Table) cannot be put in two dimensional arrays? Should I use some type conversion in my last line, at the trace(tables[0][0].x); to suggest to the compiler: it's about a Table type object?
The error message I receive: "Type parameters with non-parameterized type"
I'm not sure why you're trying to do with your first line but it's incorrect.
To quote the adobe actionscript reference:
The Array() constructor function can be used in three ways.
See this adobe reference link on Array creation. Or you can use Vectors (which are typed, like you seem to want to have).
So basically you want to create an Array, that will itself contain arrays. You need to create the arrays contained in it when you go through the first one. Else you will try to push into non existing elements. Also, you need to add the index as RST said.
var tables:Array.<Table> = new Array();
for (var i:int = 0; i < tablesDimension; i++) {
tables[i] = new Array();
for (var j:int = 0; j < tablesDimension; j++) {
var tempTable:Table = new Table();
tempTable.x = i * 150 + 100;
tempTable.y = j * 100 + 100;
stage.addChild(tempTable);
tables[i].push(tempTable);
}
}
trace(tables[0][0].x);
This should work.
I think you are missing an index. Try this
for (var i:int = 0; i < tablesDimension; i++) {
for (var j:int = 0; j < tablesDimension; j++) {
var tempTable:Table = new Table();
tempTable.x = i * 150 + 100;
tempTable.y = j * 100 + 100;
stage.addChild(tempTable);
tables[i].push(tempTable);
trace(tables[0][0].x);
}
}

input an array output bigger array related to input

suppose I have a arrays from each i want to produce b these are just examples
a=[4]=> b=[0,4]
a=[3,1]=>b=[0,3,3,4]
a=[2,2]=>b=[0,2,2,4]
a=[2,1,1]=>b=[0,2,2,3,3,4]
a=[3,4,2,5]=>b=[0,3,3,7,7,9,9,14]
I mean when getting 4 it should produce from 0 and then add it to it's content for example 4
or in a[2,1,1] first it will produce 0 and then it see that the next one in a is 1 so after again producing it it will compute 2+1 and assign it.so the output always will be twice size of the input.
i want a pseudo code for it my problem is that when it will repeat I can not write it.
I used JavaScript like syntax.
var a = new Array(3,4,2,5);
var b = new Array();
var bArrayIndex = 0;
b[bArrayIndex] = 0;
bArrayIndex++;
for(i = 0; i < a.length; i++) {
b[bArrayIndex] = b[bArrayIndex-1] + a[i];
if(i < a.length - 1) {
b[bArrayIndex+1] = b[bArrayIndex];
}
bArrayIndex+=2;
}
for(i = 0; i < b.length; i++) {
document.write(b[i] + " ");
}

Resources