1

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

2
  • 1
    getValues returns a "2D" array, in which the outer elements are rows and the inner elements are values of that row. So for an N-row getvalues call, you get [ [r1c1, r1c2, ..., r1cN], [r2c1, r2c2, ...], ..., [rNc1, ...] ]. To me it looks like you could just do finalsArray = finalsSheet.getRange(numHeaders + 1, 7, finalsSheet.getLastRow() - numHeaders, 3).getValues() where you have set the numHeaders appropriately (e.g. 3) Commented Nov 8, 2018 at 19:34
  • got it, thank you very much! Commented Nov 8, 2018 at 20:03

1 Answer 1

2

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++;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Awesome, thank you very much, that did the trick! However, as someone who just switched over to appscripts from VBA a few weeks back, I am not quite sure I understand WHY that does trick. Can you shed any light on that?
The returned value from finalsSheet.getRange(...).getValues() is a double array. However the outer array has just one element: the inner array. So all the [0] is doing is referencing that one element, inner array.
Not that this only works properly as intended due to the '1' in the getRange (which is the max amount of rows the method will return parameter)
@PetervanderWal I think you mean "Note that..." :)
@ControlAltDel / PetervanderWal Thank you both - that makes perfect sense and helps a lot. Really appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.