Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define global variable in Google Apps Script

I see most examples from Google is they use only functions in a single giant script.

e.g. https://developers.google.com/apps-script/quickstart/macros

But in our style, we usually write all functions under a single namespace, such as

MyCompany = (MyCompany || {}); MyCompany.init = function () {     Logger.log('init');   };  function onOpen() {     var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();     var menus = [{         name: "Init",         functionName: MyCompany.init     }];     spreadsheet.addMenu("Test", menus); }; 

However, when I run the code above, it return

"MyCompany is not defined." 

How to solve?

like image 287
Ryan Avatar asked Jul 13 '14 09:07

Ryan


People also ask

How do you write a global variable in a script?

A global variable is a variable that is defined in the main script. In the following example, var c=10; in the main script is a global variable.

Where do you declare global variables?

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.


2 Answers

You might be better off using the Properties Service as you can use these as a kind of persistent global variable.

click 'file > project properties > project properties' to set a key value, or you can use

PropertiesService.getScriptProperties().setProperty('mykey', 'myvalue'); 

The data can be retrieved with

var myvalue = PropertiesService.getScriptProperties().getProperty('mykey'); 
like image 146
AshClarke Avatar answered Sep 20 '22 13:09

AshClarke


In GAS global variables are not what they are in other languages. They are not constants nor variables available in all routines.

I thought I could use global variables for consistency amongst functions and efficiency as well. But I was wrong as pointed out by some people here at SO.

Global variable will be evaluated at each execution of a script, so not just once every time you run your application.
Global variables CAN be changed in a script (so they are not constants that cannot be changed by accident), but will be reinitialized when another script will be invoked.
There is also a speed penalty on using global variables. If within a function you use the same global variable two or more times, it will be faster to assign a local variable and use that instead.

If you want to preserve variables between all functions in your application, it might be using a cacheService will be best. I found out that looping through all files and folders on a drive takes a LOT of time. But you can store info about files and folders within cache (or even properties) and speed up at least 100 times.

The only way I use global variables now is for some prefixes and for naming widgets.

like image 33
SoftwareTester Avatar answered Sep 20 '22 13:09

SoftwareTester