Global variables in Google Script (spreadsheet)
You won't like this: global variables in GAS are static - you can't update them and expect them to retain their values. I, too, googled this for hours.
You can use CacheService
or ScriptDB
as possible storage for this sort of problem. CacheService
is quick and easy to use, but limited because the cache will expire eventually. I haven't tried ScriptDB
PropertiesService -> Properties
Currently (2015), I think the way to go is using the class PropertiesService and the return type Properties.
About the global scope
As I understand it, each new invokation of a script function (time triggers, user clicked a menu item, pressed a button, etc), will lead to a new full parse of the script without memory of earlier executions unless they were persisted somehow (in a spreadsheet range or using Properties
for example).