How to define global variable in Google Apps Script
global-variables, google-apps-script, google-docs, google-sheets
Solution
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');
Problem
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?