08:07
````
function doSomething1() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
````
function doSomething1() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
/*needs ss*/ const ss = SpreadsheetApp.openById(/*SPREADSHEET_ID*/);
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
````
`
const cache = (callback) => {
const c = (...args) => {
return (c.memo = c.memo !== void 0 ? c.memo : callback(...args));
};
return c;
};
function doSomething1() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
const cache = (callback) => {
const c = (...args) => {
return (c.memo = c.memo !== void 0 ? c.memo : callback(...args));
};
return c;
};
function doSomething1() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
/*needs sheet1*/ const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
/*needs ss*/ const ss = cache(() => SpreadsheetApp.openById(id));
if you use
const ss = cache(() => SpreadsheetApp.openById(id));
in global scope. You aren't doing anything different from ` /*needs sconst ss = SpreadsheetApp.openById(id);`
1 hour later…
09:30
const getSpreadsheet = cache(() => SpreadsheetApp.openById(id));
function doSomething1() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething1() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
const ss = getSpreadsheet();
const sh1 = ss.getSheetByName('Sheet1');
}
if one needs to cache multiple spreadsheets,
cache
can be extended by using an instance of Map
and a second parameter - hashing function that determines the unique key of the result after getting it the first time 10:50
It was a mistake on my part. I understood how the cache function works, but failed to accurately reproduce it in my post. But, still here, you're repeating
const ss= getSpreadsheet()
thrice. That's the issue. For say 5 different data type variables, This will be much complicated and those lines needs to be repeated 5x3 times per function = 15 times total. 11:10
const ss = SpreadsheetApp.openById(id);
function doSomething1() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething1() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething2() {
const sh1 = ss.getSheetByName('Sheet1');
}
function doSomething3() {
const sh1 = ss.getSheetByName('Sheet1');
}
@OlegValter Although I never polluted global scope in production environments, It's a great advantage to have, IMO. Could you show the problems with this method?
As long as you're careful to not over write variable names, you should generally be good. Other than permissions issue, I can't find any cons. Weighing pros against cons, especially when you need say 5 or more shared variables across 5 or more functions, I can't say globals is a bad idea. It significantly improves readability as well as workflow. Without a
cache
r 11:48
@TheMaster 1. Depends on the parsing order of files until GAS gets modules (ever?) - which either means we have to keep the globals in a special file that is always parsed first, or use build tools to ensure that. 2. Potential for name clashes. 3. An unnecessary long (and potentially unused) lifespan of a resource (with the only ability to clear if declared with
let
). 4. Contract violation - functions depend on more from the global scope than they should (aka scope bleed). function doSomething1(ss) { const sh1 = ss.getSheetByName('Sheet1'); } function doSomething2(ss) { const sh1 = ss.getSheetByName('Sheet1'); } function doSomething3(ss) { const sh1 = ss.getSheetByName('Sheet1'); } const main = () => { const ss = SpreadsheetApp.getActiveSpreadsheet(); doSomething1(ss); doSomething2(ss); doSomething3(ss); };
3 hours later…
15:08
@OlegValter The cons are seemingly minor compared to workflow and actual code size burden. I usually pass around variables as you've shown. I've passed around 5 variables around and it gets messy real fast. That's when I actually started to wonder whether globals are better/ easier.
1 hour later…
2 hours later…
19:16
@OlegValter FWIW, We barely use any libraries in apps script. So, global scope pollution is a very minor issue. First the four issues you listed also apply for two global variables you introduced:
getSpreadsheet
and cache
. Right? There's is potential for name clashes and the cache stores the reference longer than needed. Parsing order would also matter here. And the function is dependant on cache. « first day (501 days earlier) ← previous day next day → last day (1112 days later) »