Google apps script provides a library feature, where if you include the project key, a library is added as global object. I'm looking to iterate all functions of a added library. This used to be possible in rhino engine with a for...in
loop. But I'm unable to iterate through any of the properties of to a library in v8 engine.
The documentation says
In the V8 runtime, a project and its libraries are run in different execution contexts and hence have different globals and prototype chains.
Can anyone explain how this object is created or how to access all it's properties?
Project A:
function testLib(prop = 'main') {
const isEnumerable = MyLibrary.propertyIsEnumerable(prop);
const isOwnProperty = MyLibrary.hasOwnProperty(prop);
console.info({ isEnumerable, isOwnProperty }); // { isEnumerable: false, isOwnProperty: true }
console.info(prop in MyLibrary);//true
for (const property in MyLibrary) {
//loop doesn't start
console.info(property);
}
console.info(Object.getOwnPropertyDescriptor(MyLibrary, prop)); //logs expected data:
/*{ value: [Function: main],
writable: true,
enumerable: false,
configurable: true }*/
console.log(Object.getOwnPropertyDescriptors(MyLibrary)); //actual: {} Expected: array of all functions including `main`
MyLibrary.a = 1;
console.log(Object.getOwnPropertyDescriptors(MyLibrary)); //actual: {a:1} Expected: array of all functions including `main`
}
function testPropDescriptors() {
const obj = { prop1: 1, b: 2 };
console.log(Object.getOwnPropertyDescriptors(obj)); //logs expected data
/*{prop1: { value: 1, writable: true, enumerable: true, configurable: true },
b: { value: 2, writable: true, enumerable: true, configurable: true } }*/
}
MyLibrary(Project B):
function main(){}
function onEdit(){}
To reproduce,
- Create a new project by clicking here - say, Project A
- Create a another script project(say Project B):
- add a function named
main
in Project B and - Deploy it by clicking deploy in top right.
- add it's key in project A and name it
MyLibrary
.
- add a function named
- Copy paste the above script in Project A, select
testLib
function and click run