简体   繁体   中英

Google App Scritpt: How to do a while loop so that the iteration keeps going until the last number is reached?

Good Day,

I have a while loop that iterates 5 times and loops through 200 rows every iteration. Is there a way to have the while loop keep going until it reaches its last row? This is for a row count.

Here is the current while loop I have for the row count so far:

function testMe(a) {
  

  if(a == 5) {
    return JSON.parse('{"info":{"count":"3"}}');
  }
  else {
    return JSON.parse('{"info":{"count":"200"}}');
  }
}
function rowcount ()
{ 
  var rows = 0;
  var go = true;
  var i = 1;
  var data;
  while (go) {
    data = testMe(i);
    if (Number(data.info.count) < 200) {
      go = false;
    };
    rows = Number(rows) + Number(data.info.count);
    i++;
  }
  Logger.log(rows);
}

Use nested loops, such that the inner loop runs while the outer loop holds, and then breaks out of the outer loop when the last time row has been completed. Your current loop should be your inner loop, the outer loop should be like

while (rows <= 200){
     while (go) {
    data = testMe(i);
    if (Number(data.info.count) < 200) {
      go = false;
    };
    rows = Number(rows) + Number(data.info.count);
    i++;
  }
}

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