Google Apps Script Conditional Statements


Conditional statements are used to perform different actions based on different conditions.


Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.

Google Apps Script will have the following conditional statements:

  1. Use if to specify a block of code to be executed, if a specified condition is true
  2. Use else to specify a block of code to be executed, if the same condition is false
  3. Use else if to specify a new condition to test, if the first condition is false
  4. Use switch to specify many alternative blocks of code to be executed

if Statement

Use the if statement to specify a block of Google Apps Script code to be executed if a condition is true.
Syntax


if (condition) {
block of code to be executed if the condition is true;
}


Example
“Check voting age in India”
function ifCond() {
 try{
   var age=18;
   if (age >= 18) {
     Logger.log("You are eligible for voting in India");
   }
 }catch(ex){
   Logger.log(ex);
 }
}


Logs:  [18-03-11 23:17:16:958 IST] You are eligible for voting in India

else Statement

Use the else statement to specify a block of code to be executed if the condition is false.
Syntax
if (condition) {
block of code to be executed if the condition is true;
} else {
block of code to be executed if the condition is false;
}


Example
function ifElseCond(){
 try{
   var age=17;
   if (age >= 18) {
     Logger.log("You are eligible for voting in India");
   }else{
   Logger.log("You are not eligible for voting in India");
   }
 }catch(ex){
   Logger.log(ex)
 }
}


Logs : [18-03-11 23:18:51:898 IST] You are not eligible for voting in India


else if Statement
Use the else if statement to specify a new condition if the first condition is false.


Syntax
if (condition1) {
block of code to be executed if condition1 is true;
} else if (condition2) {
block of code to be executed if the condition1 is false and condition2 is true;
} else {
block of code to be executed if the condition1 is false and condition2 is false;
}
Example
function ifElseIfCond(){
 try{
 var greeting="Shiv have a ";
   var time = new Date().getHours();
   if (time < 10) {
       greeting += "Good morning..!";
   } else if (time < 20) {
       greeting += "Good day..!";
   } else {
       greeting += "Good evening..!";
   }
   Logger.log(greeting)
 }catch(ex){
 Logger.log(ex)
 }
}
Logs: [18-03-11 23:27:32:491 IST] Shiv have a Good day..!

 

Switch Statement

The switch statement is used to perform different actions based on different conditions.
Use the switch statement to select one of many blocks of code to be executed.


Syntax
switch(expression) {
case n:
    code block
    break;
case n:
    code block
    break;
default:
       code block
}
This is how it works:
  • The switch expression is evaluated once.
  • The value of the expression is compared with the values of each case.
  • If there is a match, the associated block of code is executed.


Example
The getDay() method returns the weekday as a number between 0 and 6.
(Sunday=0, Monday=1, Tuesday=2 ..)
This example uses the weekday number to calculate the weekday name:


function switchStatement(){
 try{
   var day="";
   switch (new Date().getDay()) {
     case 0:
       day = "Sunday";
       break;
     case 1:
       day = "Monday";
       break;
     case 2:
       day = "Tuesday";
       break;
     case 3:
       day = "Wednesday";
       break;
     case 4:
       day = "Thursday";
       break;
     case 5:
       day = "Friday";
       break;
     case 6:
       day = "Saturday";
   }
   Logger.log("Today is "+day)
 }catch(ex){
   Logger.log(ex)
 }
}

Logs : [18-03-11 23:31:35:643 IST] Today is Sunday


Happy Coding......

Comments

Popular posts from this blog

Google Apps Script Exception Handling

Google Apps Script Regular Expressions

Responsive Web Apps using Google Apps Script