Overall Goal:
I'm looping through a variety of files, and trying to figure out a way to dynamically list/capture as much information from a GAS File Object as possible.
If I were to take a NONdynamic approach, I'd type out each property I want to capture like this:
var zText = 'File Id:' + aFile.getId();
zText += "\n" + "getName: " + aFile.getName();
zText += "\n" + "getDateCreated: " + aFile.getDateCreated();
zText += "\n" + "getSize: " + aFile.getSize();
etc...
While the above approach would eventually get what I want, I'd like to avoid having to lookup all the property names. I'm aware that many properties might not be able to be converted to string, but I'd like to at least have them isolated by name.
What I've Tried
I thought my question was similar to this question that uses stringify. However, that doesn't work on a Google File object (it just returns a string value of {}
). I'm guessing this is because a lot of the properties are calling functions?
I can get all of the file's property/functions LISTED as string, by using: Object.keys(aFile).forEach((prop)=> Logger.log(prop ));
However, this doesn't return the RESULT of each function, and many of these are functions I wouldn't want to execute (i.e. makeCopy
).
While there may be a better method, I am assuming I'd probably be safe to try to execute all the properties/functions that start with get
or is
. I don't know of way to do this, nor can I find anything matching this type of use case. I did see this post about using executeFunctionByName, but that didn't work on the App Script environment. If it did work, I'd have used something like this..
var allKeys = Object.keys(aFile);
for(var i=0;i<allKeys.length;i++){
var eachProperty = allKeys[i];
if(eachProperty.search(RegExp("^get|^is"))>-1 ){
//??? run the literal name of function on aFile and print result?
Logger.log(eachProperty + ':' + executeFunctionByName(eachProperty,aFile));
}
}
If there's an entirely better approach to this that I'm unaware of, feel free to redirect me.