Ryan Richards
Ryan Richards

Reputation: 75

Populating multi-dimensional array using getValues

I'd like to populate a multi-dimensional array with the values of 3 contiguous cells using getValues, but when I do this, I seem to be getting an extra set of brackets that prevents me from using the array as intended later in the code.

var finalsArray = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]];

var x = 0;

for(var i = 4; i <= finalsLastRow; i++) {

 finalsArray[x] = finalsSheet.getRange(i, 7, 1, 3).getValues();

 x++;

}

Using this method, finalsArray[x] returns [[88.0, 95.0, 43412HOUOKC]].

But I want finalsArray[x] to return [88.0, 95.0, 43412HOUOKC].

I am able to achieve this and get rid of the extraneous bracket if I populate the array individually using getValue separately for each cell (see below), but I would love to optimize my code and understand why getValues is not working.

var finalsArray = [[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]];

var x = 0;

for(var i = 4; i <= finalsLastRow; i++) {

 finalsArray[x][0] = finalsSheet.getRange(i, 7).getValue();
 finalsArray[x][1] = finalsSheet.getRange(i, 8).getValue();
 finalsArray[x][2] = finalsSheet.getRange(i, 9).getValue();

 x++;

}

Thank you very much for any help you guys can provide.

Ryan

Upvotes: 1

Views: 115

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35106

Just reference the first element of the returned value in place

for(var i = 4; i <= finalsLastRow; i++) {
 finalsArray[x] = finalsSheet.getRange(i, 7, 1, 3).getValues()[0];
 x++;
}

Upvotes: 2

Related Questions