Skip to content
Advertisement

setValue function skipping rows instead of choosing current in for loop

I have this code in Google Apps Script that is linked to a Google Sheet:

JavaScript

The script is supposed to send reminder emails. Everything works well, except this one line:

JavaScript

This line should add the value of 1 to a specific cell in the Google Sheet, in the current row the for loop is at.

However, instead of adding the value to the current row, it skips rows and adds the value to some other row. In some cases, there’s a difference of 18 rows, and in others, 198 rows.

Here is the Google Sheet link for reference.

Advertisement

Answer

I noticed some issues in your sheet and script.

  1. In your sample sheet, you have values on M22, M27 and M210. The function sheet.getLastRow() will include those values and your last row will be 210.

  2. If you print the value of data, there is an extra empty array.

  3. If you try to print startRow + i, the values are 22, 23, 24...etc. It just appends the value of startRow and i and not doing the arithmetic operation sum.

Solution:

  1. Remove the unnecessary data or clean your sheet so that the value of numRows will become 12.

  2. Since you skip 1 row, you should also deduct 1 row to numRow. Simple add -1 to numRows in your getRange().

  3. Use the parseInt() method to variable i in sheet.getRange((startRow + i), 13).setValue(1);.

Your code should look like this:

JavaScript

Sample:

Note: I adjusted the Reminder Date values to my current date so that the script will work.

enter image description here

Output:

enter image description here

Reference:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement