1

I am doing my first loop in Google Script and it isn't working properly. It's kind of only half working so I have no idea what is the issue.

The code is supposed to loop down each row and in column 1 and enter the next ID number (and once it has done that it should increase the LastID number by 1 and then put this on the next row).

The problem is that it enters the LastID in each of the cells and then at the end it increased the LastID by 1 (So lets say the LastID was 1, it puts 1 in all the cells instead of 2,3,4 etc..)

(Note I have left out a bit of the code declaring the sheet variables etc..)

function myFunction() {
    var IDCount = 1;
    var LastID = 1;
    var RowCount = 4;

    {
        for (var x = IDCount; x < RowCount; x++)
            Browser.Buttons.YES_NO);

    sheet.getRange((x + 2), 1).setValue(LastID) //set next row with missing ID number

    sheet.getRange(2, 7).setValue(LastID) //set the new ID
    LastID++ //Increase LastID by 1

}
}

Hope someone can help .Thanks

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63
John Cassell
  • 107
  • 2
  • 3
  • 9

1 Answers1

1

You have not defined the body of loop. If we don't provide braces, by default it's assumed that only one line is there in body. So in your case this is happening. It's assuming only

sheet.getRange((x+2), 1).setValue(LastID) //set next row with missing ID number

is within loop, so it's setting same value to all rows.

Try this code :

function myFunction() {
    var startRow = 1; // You can set your start row number here
    var startId = 1; // You can set your start id number here
    var lastRow = 4; // You can set your last row number here, till where you want to set id;
    //if you want this to be last row of sheet then = sheet.getLastRow();
    var tempId = startId;
    for (var x = startRow; x <= lastRow; x++) {
        sheet.getRange(x, 1).setValue(tempId) //set the ID number
        tempId++; // Increment the ID by 1
    }
}
Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
  • Perfect (and well explained). works brilliantly now. Thanks so much for your help. John – John Cassell Feb 01 '18 at 11:46
  • Is there a thumbs up or way to mark this as solved please? – John Cassell Feb 01 '18 at 12:03
  • Accept as solution/Upvote You can/[should-as per stack overflow etiquette, I think] accept it as accepted answer &/ upvote. So that people who will come to this screen afterwards can distinguish this answer and know that its working solution. https://stackoverflow.com/help/someone-answers – Umair Mohammad Feb 01 '18 at 12:36
  • If you'll accept as solution a green tick will come, which definitely helps other – Umair Mohammad Feb 01 '18 at 12:36
  • Thank you, I've marked that as the answer but as my reputation is not good enough, unfortunately my upvote is publically ignored :-) – John Cassell Feb 01 '18 at 14:52
  • I have answered your one more question , did that worked ?https://stackoverflow.com/questions/48547317/google-sheets-script-count-formula – Umair Mohammad Feb 01 '18 at 15:15