I'm aware that global variables in google apps script have some issues (or, rather, different ways of working than "normal" code):
Issues with global variables in Google App Script
How to define global variable in Google Apps Script
Global variables in Google Script (spreadsheet)
and many more examples, but the question is, in the Google Apps script documentation, they mention that in the onOpen function, there is passed the event object, which contains a source property, which is linked to the current document:
source A Document object, representing the Google Docs file to which the script is bound.
Document
Now, what would be the point of giving us access to this source if you would have to call DocumentApp.getActiveDocument()
anyway? I tried making a global variable at the top of the code stating:
var doc;
then in the onOpen(e)
function, setting it to: doc = e.source
, which works fine in that function, but when I want to use it in another function called by the client, the "global" variable "doc" is undefined or null, so how can I set it up so I don't have to call DocumentApp.getActiveDocument()
every time the client clicks something? Because it takes about 50-70ms, which even though its pretty fast, but it could be faster... any workarounds for this?
basically: how do I store DocumentApp.getActiveDocument()
in a reusable variable? And if I can't, why is the source property provided in the event object in onOpen
? (the fact that it is implies that its possible to store it somewhere perhaps)