Google Apps Script Loops
Loops can execute a block of code a number of times.
Loops are handy, If in Google Apps Script you want to run the same code over and over again,
use loops.
Often this is the case when working with arrays and Google Sheet:
Loops are handy, If in Google Apps Script you want to run the same code over and over again,
use loops.
Often this is the case when working with arrays and Google Sheet:
Different Kinds of Loops
Google Apps Script supports different kinds of loops:
- for - loops through a block of code a number of times.
- for/in - loops through the properties of an object.
- while - loops through a block of code while a specified condition is true.
- do/while - also loops through a block of code while a specified condition is true.
For Loop
The for loop is often the tool you will use when you want to create a loop.
Syntax
for (statement 1; statement 2; statement 3) {
code block to be executed
}
Note:
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (the code block).
Statement 3 is executed each time after the loop (the code block) has been executed.
Example
function forLoop() {
try{
var text = "";
var i;
for (i = 0; i < 5; i++) {
text += "The number is " + i +"\n" ;
}
Logger.log(text)
}catch(ex){
Logger.log(ex)
}
}
Logs: [18-03-11 23:39:09:851 IST] The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
From the example above, you can read:
Statement 1 sets a variable before the loop starts (var i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5).
Statement 3 increases a value (i++) each time the code block in the loop has been executed.
For/In Loop
The Google Apps Script for/in statement loops through the properties of an object:
Syntax
for (x in object) {
object[x];
}
Example
function forIn(){
try{
var txt = "";
var person = {firstName:"John", lastName:"Doe", age:29};
var x;
for (x in person) {
txt += person[x] + " ";
}
Logger.log(txt)
}catch(ex){
Logger.log(ex)
}
}
Logs [18-03-11 23:46:39:576 IST] John Doe 29
While Loop
Loops can execute a block of code as long as a specified condition is true. The while loop loops through a block of code as long as a specified condition is true.
Syntax
while (condition) {
code block to be executed
}
Example
In the following example, the code in the loop will run, over and over again, as long as a variable (i) is less than equal 5:
function whileLoop(){
try{
var text="",i=0;
while (i <= 5) {
text += "The number is " + i +"\n";
i++;
}
Logger.log(text)
}catch(ex){
Logger.log(ex)
}
}
Logs [18-03-11 23:52:49:492 IST] The number is 0
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax
do {
code block to be executed;
}
while (condition);
Example
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
function doWhileLoop(){
try{
var text="",i=11;
do {
text += "The number is " + i +"\n";
i++;
}
while (i < 10);
Logger.log(text);
}catch(ex){
Logger.log(ex);
}
}
Logs [18-03-11 23:55:26:810 IST] The number is 11
Note: Do not forget to increase the variable used in the condition, otherwise the loop will never end is called infinite loop;
Happy Coding.....
Comments
Post a Comment