I run the following in the Chrome console without problem:
// Usage:
// console.log([...scope]); // => ['user','script','document']
// console.log(`${scope.user`}); // => 'user'
// console.log(scope.user.properties); // 'user.properties' (not real code)
class scope {
static user = new scope();
static script = new scope();
static document = new scope();
constructor(propertyーscope) {
if(propertyーscope in scope) return scope[propertyーscope];
}
get properties() {
return `${this.toString()}.properties`; // just for the example
// in reality, I'd return:
// PropertiesService[`get${this.toString().toSentenceCase()}Properties`]();
}
static *[Symbol.iterator]() { for (const key of Object.keys(scope)) yield key; }
toString() { return Object.entries(scope).filter(([key, value])=>value===this)[0][0]; }
};
However, Google Apps script refuses to save this code snippet and complains about the static declarations (= sign after static user
.
- Isn't GAS supposed to support static fields?
- More importantly, how can I achieve the same?
(note: the dash in propertyーscope
is not a dash but a unicode letter looking like it; I use that as a more readable alternative to underscores).