I have read some of the basic Google Apps Script documentation/tutorials. I have not found any explanation of the “scope” of code execution.
Here is what I understand so far:
All code consists of one or more statements
All statements must (?) be contained in a "function"
(a slight different from non-google javascript? - is this a false assumption?)
All functions reside in a container called a "file"
Each "file" is part of a "project" container (or library)
Each project container is stored in a "spreadsheet" container.
These are the “physical” (edit access) boundaries.
My question is what is the “logical” boundaries of statements
during execution?
I started by assuming all variables/objects were global in scope,
similar to the way that JavaScript operates in a web-page. I did not think that
“edit access” containers limited the scope of variable/object definitions.
I was wrong.
I thought that a “library” structure is similar to a PHP “include” operation.
By that I thought it would save me from having to copy the same set of
code into every application (spreadsheet container), in which I needed to
use the already “tested” code. I assumed the resources available to the
included project were the same as those available to the including
project common resources). In short, I was wrong. The “properties” were stored in the defining spreadsheet, and are considered to be “owned” by the containing project.
From playing with it, I now understand that the “project key” just adds
a new “namespace” to the spreadsheet container. What happens in each
namespace remains in that namespace. The only (simplest) communication
between the namespaces is via function parameters and return value.
In particular, User, and Project properties are scoped to and remain in
their containing spreadsheet document. Each project within the spreadsheet
document has separate set of User and Project properties.
The same project/library name can appear in multiple spreadsheet files
and are totally independent.
Is this documented somewhere? And are there other things I need to know
about scoping (communication across threads from different applications
sharing the same project/library?
Also, if a function passes back a reference to an object defined in the
library scope, will it persist in the calling’s project? Can I pass/return
a variable that points to a project’s UserProperties “sevice” object and have access
to that data in another project?
This is documented towards the end of the User Guide in Libraries:
https://developers.google.com/apps-script/guide_libraries
When deciding how to scope things, we tried to think really hard about the most common use cases, and try to reduce surprise as much as possible, but we weren’t perfect.
Regarding your question on whether you can use parameter-passing to share ScriptProperties objects between the library and the project, that’s currently not possible. You can always expose getters/setters for particular properties.
If you have an interesting use case in mind which is impossible to achieve without the requested behavior, please file a bug in our issue tracker. Thanks!