Google apps script variables declaration and its scope

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.

Google apps script variables declaration

What is google apps script variables?

        Google apps script variables are used to store data or information and can be retrieved simply by referring the variable names. Google apps script variable is same as javascript 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.

Syntax:

var <variable_name>;

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.

Example

var a;
var a,b,c;

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.

Example

var a;
//Assigning value 10 to variable ‘a’ using = operator
a=10;
var b=55;
Logger.log(b);

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.

//Global variable
var b=15;
 
function global_variable_1()
 
{
  //'a' is local variable
  var a=10;
  c=a+b;
  Logger.log(c);
 
}
 
 
function global_variable_2()
 
{
  //'d' is a local variable
  var d=20;
  e=d+b;
  Logger.log(e);
 
}

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

function local_variable_1()
 
{
  //'a' is local variable
  var a=10;
  var b=30;
  var c;
  c=a+b;
  Logger.log(c);
 
}
 
function local_variable_2()
 
{
  Logger.log(c)
 
}

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().

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- If else statement

Google Apps script- For Next statement

Google Apps script- Switch Case statement

6 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.