In this post, you’re going to learn Google apps script switch case statement, syntax and examples.
Table of Contents
Google apps script switch case statement
Google apps script switch statement is used to evaluate the given expressions with multiple cases. Apps script compiler evaluate the expression with cases from top to bottom.
Each case has a set of instructions. So, the first matched case gets executed when compiler goes from top to bottom. Once that is executed then it moves out of the switch statement block. And it never checks the remaining cases after that.
Each case should end with “break” statement. Default case will be executed if none of the cases matches.
Break statement
- When apps script compiler reaches the break statement, it breaks out the apps script switch statement block
- This break statement stops validating the cases present after that.
- It acts as a terminating point for cases and switch statement block.
- No need to use a break statement in the last case.
Default statement
- It contains the set of instruction(s) to be executed when there is no match.
Difference between Google apps script switch case and multiple if else statements
In the Google apps script, If else statement, we learned about the multiple if else statements. Apps script switch case statement is the advanced form of multiple if else statement. Because the speed of execution in switch case is faster than multiple if else statement. In multiple if else statement, we can check different conditions. But in the switch statement, we can check the given expression with multiple values.
Syntax of apps script switch case statement
switch(expression) {
case case1:
Instruction 1;
Instruction 2;
break;
case case2,case2:
Instruction 1;
Instruction 2;
break;
default:
Instruction 1;
Instruction 2;
}
Example of apps script switch case statement
In this example, we’ll learn about the Google apps switch case statement by prompting the user to enter any number from 1 to 7. We are going to display the day of the week for the entered number. Go to Google drive -> New -> Google sheets. A new Google sheets will be opened. Enter a sheet name and save it. Go to Tools-> Script Editor. A script editor window will be opened in a new tab. Enter the below code in the script editor, save and execute it.
function switch_Case_Statement()
{
//Declaring variables
var ui = SpreadsheetApp.getUi();
// Get the value from the user
var get_text= ui.prompt("Enter any number from 1 to 7").getResponseText();
//Convert the data type to integer
var get_num=parseInt(get_text);
//Comparing the entered number with days in a week
switch(get_num)
{
case 1:
ui.alert("Today is sunday");
break;
case 2:
ui.alert("Today is monday");
break;
case 3:
ui.alert("Today is tuesday");
break;
case 4:
ui.alert("Today is Wednesday")
break;
case 5:
ui.alert("Today is Thursday");
break;
case 6:
ui.alert("Today is Friday");
break;
case 7:
ui.alert("Today is Saturday");
break;
default:
ui.alert("Entered number is not fall between 1 to 7")
}
}
Explanation
var ui = SpreadsheetApp.getUi(); – SpreadsheetApp is a class which allow user to open, create, modify the Google sheets. getUi() is a method of SpreadsheetApp allows the script to add features like menus, dialogs, and sidebars. Here ui is a variable object with which we can access all the getUi features.
var get_text= ui.prompt(“Enter any number from 1 to 7”).getResponseText(); – Prompt display a message to the user in the form of message box with prompt message, text input field and action buttons.
In the text input field, the user can enter a text but in this case, user should enter a number from 1 to 7. Getresponsetext method is used to get the text entered by the user and save it in the string format.
var get_num=parseInt(get_text);- parseInt method accept an argument and convert the argument type to integer. Even user enter a number in the prompt message, initially, it gets stored as a string. We convert the data type from string to int with the parseInt method and stored the value in get_num variable.
Next, come the google apps switch case statement block. In the switch expression switch(get_num), we passed the text entered by the user. We have created cases from 1 to 7 to show the day of the week. If the user enters any value other than 1 to 7 default statement gets executed.
If user enters 6 in the prompt message box then he’ll get the below output.
Execute same code blocks for multiple switch cases
Sometimes we might need to execute the same set of instruction for a different case statement. We’ll see how to do that without copy pasting the same instruction for multiple times.
In the below example, we prompt the user to enter a number and determine whether it’s odd/even number.
function switch_Case_Statement_001()
{
//Declaring variables
var ui = SpreadsheetApp.getUi();
// Get the value from the user
var get_text= ui.prompt("Enter any number from 1 to 10").getResponseText();
//Convert the data type to integer
var get_num=parseInt(get_text);
//Comparing the entered number with days in a week
switch(get_num)
{
case 1:
case 3:
case 5:
case 7:
case 9:
ui.alert("Entered number is odd");
break;
case 2:
case 4:
case 6:
case 8:
case 10:
ui.alert("Entered number is even");
break;
default:
ui.alert("Entered number is not fall between 1 to 10")
}
}
In the above example, case 1,3,5,7 and 9 shares the same instructions and 2, 4, 6, 8 and 10 share the other instructions.
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