3

Google documentation says that the PropertiesServices.getScriptProperties() is scoped to a Script.

However, I have a working example that seems to contradict this.

I have one project, with three scripts (tabs) in it. Each tab sets a value of "last run time" in the ScriptProperties. If the scope of these ScriptProperties was truly "per script" then I would expect to be able to store different values for each script (tab).

But what I am finding is that each time one script (tab) executes, it overwrites the value of "last run time" that was saved by the other scripts (tabs).

This seems to indicate that the ScriptProperties are actually scoped per Project, not per script.

Can anyone corroborate this, or tell me what I am misunderstanding?

(I left feedback on the Google documentation page already)

knwpsk
  • 41
  • 3

2 Answers2

4

Yes, the ScriptProperties are common to the entire script project. The concept of files within the projects is purely for organisational convenience, all of the .gs "files" within a single Google Apps Script "Project" represent one "Script".

Generally, if I need to track multiple last execution time values, I'll set a unique property for each:

Action1_LastExecuteTime
Action2_LastExecuteTime

etc..

Another example of this is Script authorization, when a user authorizes a scope, it's at the project level, and that authorization applies to any code within the project.

Cameron Roberts
  • 7,127
  • 1
  • 20
  • 32
  • Thank you Cameron. Hope Google will take my feedback and clarify this in their documentation! – knwpsk Jun 25 '20 at 18:10
  • Cameron, maybe you can help with this part, then? My three scripts use a common function to set the script properties. So, can I parameterize the name of the Key? I tried setProperties( 'LastExecuteTime' + constThisScriptName : currentTime ) This didn't work. GAS throws an error saying the plus sign is unexpected. I also tried setProperties (constThisScriptName : currentTime ) This also didn't work. GAS treats the constant name as a literal string. – knwpsk Jun 25 '20 at 18:29
  • Nevermind I figured it out, thanks to this: https://stackoverflow.com/questions/13833204/how-to-set-a-js-object-property-name-from-a-variable – knwpsk Jun 25 '20 at 19:02
  • 2
    I want to add a comment that if they were really separate scripts that would be a huge pain, you couldn't share functions and variables across the pages so you'd have to lump everything in one. – J. G. Jun 25 '20 at 19:20
2

Preface

There is an important difference between scope and a script (or project).

Properties

PropertiesService accesses resources scoped to a particular access level, each determining what will and will not be shared in different execution contexts (standalone, bound to document or Web App). There are 3 access levels that have distinct scopes:

  1. Script - available to all users regardless, a global scope so to speak
  2. Document - available to all users, but limited to current (container) document
  3. User - limited to the user under which authority the script runs

Projects

Apps Script Projects have a well-defined and consistent meaning, to quote the documentation:

A script project represents a collection of files and resources in Google Apps Script

Note that a script project is more than a mere sum of its files, it has a notion of resources. There are currently 3 groups of resources that can be associated with an Apps Script project:

  1. Libraries - acts as a stand-in for modules
  2. Cloud Platform project - controls quotas, permissions and access
  3. Advanced Google Services - thin wrappers around corresponding REST APIs

How does this answer the question?

Although getScriptProperties() method deals with the "script" scope it does so insomuch the resources represented (properties storage) will be accessible anywhere in the execution context (provided being reachable, ofc) of the script, not the project as it represents more than that.

The difference becomes more apparent in the complexity Libraries bring in as multiple projects share library's ScriptProperties (if it exposes them), but each have their own instance as well.

It is true that the glossary states that script projects are "sometimes referred to simply as "the script"" but it does not say that whenever you encounter the term "script" you can substitute "project", and vice versa.

Whole structure visualized:

+----------------------------------+
| Project                          |
| +------------------------------+ |
| | Script                       | |
| | +--------------------------+ | |
| | | .gs files                | | |
| | +--------------------------+ | |
| | +--------------------------+ | |
| | | .html files              | | |
| | +--------------------------+ | |
| | +--------------------------+ | |
| | | appsscript.json manifest | | |
| | +--------------------------+ | |
| +------------------------------+ |
| +------------------------------+ |
| | Resources                    | |
| | +--------------------------+ | |
| | | Libraries                | | |
| | +--------------------------+ | |
| | +--------------------------+ | |
| | | Cloud Platform Project   | | |
| | +--------------------------+ | |
| | +--------------------------+ | |
| | | Advanced Google Services | | |
| | +--------------------------+ | |
| +------------------------------+ |
| +------------------------------+ |
| | Properties                   | |
| | +--------------------------+ | |
| | | ScriptProperties         | | |
| | +--------------------------+ | |
| | +--------------------------+ | |
| | | DocumentProperties       | | |
| | +--------------------------+ | |
| | | UserProperties           | | |
| | +--------------------------+ | |
| +------------------------------+ |
+----------------------------------+