Next to GAS introduction, you’ll learn what is google apps script variables and its scope, how to declare google apps script variables, how to assign value to google apps script variables, rules in defining google apps script variables and its scope.
What is google apps script variables?
How to declare google apps script variables?
Google apps script variables are declared with or without keyword var followed by variable name and complete the syntax with semicolon.
It is recommended to declare variables at the beginning of the program. You can also declare multiple variables in a single line separating each variable by comma.
Go to drive.google.com -> New-> Google sheets. In the newly opened spreadsheet, go to Tools -> Script Editor. Script editor window will be opened in new tab with default myfunction(). Enter the example codes inside that function and click Run or F5 to execute and see the result.
How to assign value to google apps script variables?
Now you are familiar with declaring variables. After variable declaration, literally no value is assigned to those variables. You can assign value to the declared variables using assignment operator ‘=’. You can assign value either at the time of declaration or later.
//Assigning value 10 to variable ‘a’ using = operator
In the script editor, go to View -> Logs to view the value stored in the variable b.
Rules in defining google apps script variables
The general rules for google apps script variables are:
- Variable names can contain letters, digits, underscores, and dollar signs.
- Variable names can also begin with $ and _
- Variable names are case sensitive (a and A are different variables)
- Variable names must begin with a letter
- Google apps script reserved words cannot be used as variable names
Google apps script variable scope
Variables can be declared as global or local. Global variables can be accessed by any functions in the script. Local variables can be accessed only by the function where it is declared.
Global variable example
To examine the global variable, 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. Go to View -> Logs to view the value stored in the variable.
//'a' is local variable
//'d' is a local variable
In the above code, variable ‘b’ is global variable since it is declared outside the functions. It can be accessed by any functions in the script. In this case, there are two functions global_variable_1() and global_variable_2(). In the script editor, select the function you want to execute from the ‘select function’ drop down and click F5 to execute.
Local variable example
//'a' is local variable
In the above code, two functions are created local_variable_1() and local_variable_2(). In the local_variable_1(), three variables are present a,b and c and store the sum of variable ‘a’ and ‘b’ in variable ‘c’. When you execute the function local_variable_1(), you’ll get the output 40.
In the local_variable_2(), we are trying the display the value stored in the variable ‘c’ but the script considers variable ‘c’ as new variable. So, when you execute the local_variable_2(), you’ll get reference error: ’c’ is not defined. Because the local variable ‘c’ can be accessed only by the function local_variable_1().