Reputation:
I've a lot of trouble with this sample of code here... I'm sure it is a beginner question but I've no idea what is wrong...
I want to do some calculations with the values in the array I got from a range but I cannot get the elements of the array, only the complete array...
var myarray = sh.getRange("A3:L3").getValues();
for (var i = 0; i < my myarray.length; i++){
var test = myarray[0];
myarray[i] = myarray[i] + 10;
}
First, is it possible to do the second line in the for loop (myarray[i] = myarray[i] + 10) ? I want to replace the value by this value plus 10.
I tried and it does not work for me and returns me NaN.
So I made the var "test" which returns me the full array and not just the first value of the array...
I am stuck and I've no idea what is wrong here...
Thank you for your help!
Upvotes: 2
Views: 2656
Reputation: 46794
The range you are reading has only one row but it still returns a 2D array, ie an array of arrays.
myarray[0]
is the first (and only in this case) array and you should iterate in this one.
You should rewrite your code like this :
for (var i = 0; i < myarray[0].length; i++){
myarray[0][i] = myarray[0][i] + 10;
}
Upvotes: 3
Reputation: 38131
There is an error on for (var i = 0; i < my myarray.length; i++){
. It's very likely that instead of my myarray.length
it should be myarray.length
.
By the other hand, sh.getRange("A3:L3").getValues()
returns a 2D array son instead of myarray[i] = myarray[i] + 10
you should use something like myarray[i][j] = myarray[i][j] + 10
Upvotes: 2