I use an assignment operator in a statement without a "var" keyword.
function define_My_Global_Vars_() {
/*
Sets global var names in the global space -
Not using a var keyword puts the variable into the global scope -
The statements are not in the global scope but the variables get put into the global scope
*/
MY_ADD_ON_NAME = "Super Good Add-on";//No service call needed - assign a value now
MY_GLOBAL_ONE = undefined;
MY_GLOBAL_DOCUMENT = undefined;//Needs a service call to be assigned to an Apps Script Class
}
function set_My_Global_Doc_() {
MY_GLOBAL_DOCUMENT = SpreadsheetApp.getActiveSpreadsheet;//Call a Google Service - Done inside of a fnk not in global space
}
define_My_Global_Vars_();//Call the fnk to define global var names in the global space - This statement is in the global scope
function getActiveDoc_() {
try{
if (MY_GLOBAL_DOCUMENT) {//If already assigned then this avoids additional service calls
return MY_GLOBAL_DOCUMENT;
};
}catch(e){
MY_GLOBAL_DOCUMENT = null;//If not already assigned then there must be an initial assignment
}
return set_My_Global_Doc_();//Assigns and returns the active document
};
Whenever your code needs to access the active spreadsheet, then simply use:
getActiveDoc_()
You can either assign a var in the function or use it directly:
function myFunk_() {
var ssID = getActiveDoc_().getId();
}
function my2_Fnk_() {
var ss = getActiveDoc_();//Get the active spreadsheet