简体   繁体   中英

2.0 is recognized as 20 on google apps script

I'm trying to run a script on a Google spreadsheet. The below is the code snippet:

var startRow = 2;
var data = dataRange.getValues();
for (i in data) {
   sheet.getRange( startRow + i, 4).setValue("EMAIL_SENT");
}

I expect that "EMAIL_SENT" appears from the 2nd row. But, instead, it appears from the 20th row. When I write out startRow using Logger, it appears as 2.0, instead of 2. I am guessing that 2.0 is recognized as 20 since I live in Finland, where the comma, rather than dot, is used as a radix point. Having said that, my language setting is set with Japanese where dot is used for a radix point. So, this interpretation isn't logical, but this is the only explanation I could think of. When I used

Math.round( startRow )

This didn't help, either. Could anybody suggest solution? Thank you!

I don't know why, but for some reason that type of loop does not create numbered index( i ). So, your script reads startRow + i is like saying 2 and i , not 2 plus i .

Your not seeing the number 20 you are seeing the number 2 with the index 0.

To change this, you can either use this type of loop.

for(var i=0;i<data.length;i++){
    sheet.getRange( startRow + i, 4).setValue("EMAIL_SENT");
  }

Or you can just add Number() to the i

sheet.getRange( startRow + Number(i), 4).setValue("EMAIL_SENT");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM