2

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!

2
  • If test is referring the complete array, then try using for loop to iterate through it. Commented Mar 4, 2018 at 18:08
  • Well I'll be in the same situation, won't I? I try to do that with myarray but every time I put myarray[0] or myarray[i] then I have the complete array... Commented Mar 4, 2018 at 18:38

2 Answers 2

3

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

Comments

2

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

Comments

Your Answer

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