Google apps script if else statements syntax

In this post, you’re going to learn different types of Google apps script if else statements , multiple if else statements, nested if else statements, syntax and examples.

google apps script if else statements

Google apps script if statement

            Google apps script If statement check/validate the condition(s) present inside the parenthesis ().Google apps script if else statements instruction(s) should be enclosed within the curly braces.

It executes the instructions enclosed within the if statement block only if the condition returns true. Otherwise, Google script compiler starts compiling the code present next to if statement block.

Syntax

if (condition) {

            //block of code to be executed if the condition is true

            Instruction 1;

            Instruction 2;

}          

Example

Go to Google drive -> New -> More -> Google apps script. A script editor window will be opened in a new tab. Enter the below code in the script editor, save and execute it.


function learn_if_statement()
{
  //Declaring two variables
  var my_var1=20,my_var2=30;
 
  //Check whether my_var1 is lesser than my_var2
  if(my_var1 < my_var2)
  {
	Logger.log("my_var1 is lesser than my_var2");
  }
 
}

Output

my_var1 is lesser than my_var2

Note: Go to View -> Logs to view the output.

In the above function, two variables my_var1 and my_var2 are declared and initialized with value 20 and 30 respectively. Google script compiler validate whether the my_var1 is lesser than my_var2. Compiler executes the instructions present inside if blocks only if the condition returns true. In this case, the condition returns true so it prints the statement “ my_var1 is lesser than my_var2” in the console logger.

Google apps script if else statement

            Like if statement, Google apps script If else statement check/validate the condition(s) present inside the parenthesis ().  If the condition(s) is true then instructions in if block gets executed. Otherwise, instruction in the else block gets executed.

Syntax

if (condition)

//Google apps script if else statements

{

            //block of code to be executed if the condition is true

            Instruction 1;

            Instruction 2;

}

else

{

//block of code to be executed if the condition is false

            Instruction 1;

            Instruction 2;

}

           

Example

Go to Google drive -> New -> More -> Google apps script. A script editor window will be opened in a new tab. Enter the below code in the script editor, save and execute it.


function learn_if_else_statement()
{
  //Declaring two variables
  var my_var1=40,my_var2=30;
 
  //Check whether my_var1 is lesser than my_var2
  if(my_var1 < my_var2)
  {
	Logger.log("my_var1 is lesser than my_var2");
  }
  else
  {
	Logger.log("my_var1 is greater than my_var2");
  }
 
}

Output

my_var1 is greater than my_var2

Note: Go to View -> Logs to view the output.

In the function learn_if_else_statement(), two variables my_var1 and my_var2 are declared and initialized with value 40and 30 respectively.

In the if statement condition – Google script compiler validate whether the my_var1 is lesser than my_var2. Compiler executes the instructions present inside if blocks if the condition returns true. In this case, the condition returns false so it prints the statement in the else block “ my_var1 is greater than my_var2” in the console logger.

Google apps script multiple if else statement

            Multiple If else statement is used to check the conditions in the sequential order. If anyone of the condition becomes true then compiler execute it instructions and comes out of the multiple if else statement block.  

Syntax

if (condition 1)

{

            //block of code to be executed if the condition 1 is true

            Instruction 1;

            Instruction 2;

}

else  if(condition 2)

{

//block of code to be executed if the condition 2 is true

            Instruction 1;

            Instruction 2;

}

else

{

//block of code to be executed if none of the condition is true

            Instruction 1;

            Instruction 2;

}

           

Example

Go to Google drive -> New -> More -> Google apps script. A script editor window will be opened in a new tab. Enter the below code in the script editor, save and execute it.


function learn_multi_if_else_statement()
{
  //Declaring two variables
  var my_var1=40,my_var2=40;
 
  //Check whether my_var1 is lesser than my_var2
  if(my_var1 < my_var2)
  {
	Logger.log("my_var1 is lesser than my_var2");
  }
  //Check whether my_var1 is equal to my_var2
  else if(my_var1==my_var2)
  {
	Logger.log("my_var1 is equal to my_var2");
  }
  else
  {
    Logger.log("my_var1 is greater than my_var2");
  }
 
  }

Output

my_var1 is equal to my_var2

Note: Go to View -> Logs to view the output.

In the function learn_multi_if_else_statement, two variables my_var1 and my_var2 are declared and initialized with value 40 each.

In the first if condition, Google script compiler check whether my_var1 is lesser than my_var2. If it returns true then a block of code under the if statement gets executed and comes out of the entire multiple if else statement blocks.

If the first condition fails then google script compiler checks the second condition whether my_var1 is equal to my_var2. If it returns true, then block of code under the else if statement gets executed. In this case, it returns true so it prints the statement “my_var1 is equal to my_var2” and terminate the multiple if else statement.

Google apps script nested if else statement

            Nested if else statement is self-explanatory. It surrounded by Nested blocks of If else statements. This will be helpful if your script needs to satisfy multiple conditions.

Syntax

if (condition 1)

{

            //block of code to be executed if the condition 1 is true

            if(condition 2)

            {

                            Instruction 1;

                            Instruction 2;

            }

}

else  if(condition 2)

{

if(condition 2)

            {

                            Instruction 1;

                            Instruction 2;

            }

}

else

{

//block of code to be executed if none of the conditions is true

            Instruction 1;

            Instruction 2;

}

Example

Go to Google drive -> New -> More -> Google apps script. A script editor window will be opened in a new tab. Enter the below code in the script editor, save and execute it.


function learn_nested_if_else_statement()
{
  //Declaring two variables
 
  var my_var1=40,my_var2=40;
 
  //Check whether my_var1 is lesser than 50
 
  if(my_var1 < 50)
  {
	//Check whether my_var1 is lesser than my_var2
 
	        	if(my_var1 < my_var2)
                            	 {
	                                        	  Logger.log("my_var1 is lesser than my_var2");
	                        	}
	//Check whether my_var1 is equal to my_var2
	        	else if(my_var1==my_var2)
                            	 {
 	                                       	 Logger.log("my_var1 is equal to my_var2");
	                        	}
  }
 
 
  else
  {
	Logger.log("my_var1 is greater than 50");
  }
 
 }

Output

my_var1 is equal to my_var2

In learn_nested_if_else_statement() function, first if  condition check whether my_var1 is lesser than 50. After it returns true, it enters into that block and checks the condition my_var1 is lesser than my_var2. It returns false. So, it checks the else if condition whether my_var1 is equal to my_var2. It returns true. It executes the statement inside the block and comes out of the nested if statement.

 

Are you interested in learning Google apps script from scratch?. Here’s my free video tutorial Google Apps Script Tutorials For Beginners

 

Recommended Post:

Google Apps script – Introduction

Google Apps script- Variable Declaration

Google Apps script- For Next statement

Google Apps script- Switch Case statement

 

If you want to view the below video in Youtube click here

5 Comments

Let me know your thoughts

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.