问题
I find PropertiesService
to be unreliable, but probably I'm just not familiar with Google Apps Script or Stackdriver and made a mistake or assumed something here that may caused the problem.
Here's the script:
sp = PropertiesService.getScriptProperties()
sp.setProperties({
'somekey': 'value'
})
props = sp.getProperties()
console.log(props.toString())
And here's the logs before I wrote this SO question:
Type Start Time Duration Status Stackdriver Log
Trigger Oct 9, 2020, 11:19:07 PM 0.541 s Completed Debug [object Object]
Editor Oct 9, 2020, 11:11:43 PM 0 s Unknown Debug [object Object]
Editor Oct 9, 2020, 11:08:09 PM 0 s Unknown Debug [object Object], ScriptProperties
Editor Oct 9, 2020, 11:05:16 PM 0 s Unknown Debug [object Object], ScriptProperties
The one marked as Editor
type is manual debug runs from apps script web IDE, I set onTrigger
every 15mins before I added those PropertiesServices
lines. Whenever I check the Execution
log page in each execution, it takes minutes to get the log result, and just now, more than half an hour in the future, I re-checked and those Unknown
status logs are marked Completed
and all under 0.5s.
Is this just a glitch? If it's not normal or I made a mistake/wrong assumption, what am I supposed to do to ensure I don't experience this kind of unpredictable results?
Why don't I get strings from the props
key value pair?
回答1:
For relatively better and reliable logging, Use Stackdriver directly(i.e., View> Stackdriver logging) rather than from the "executions" page in the dashboard. To do this, you need to switch Google cloud project from default to standard by setting a custom project number in Resources > Cloud Platform project > Change project.
When logging objects, You must always
JSON.stringify
the object before feeding it toconsole
.props.toString()
will only return[object Object]
as that is it's internal structure.
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const props = {a:1};
console.log(props.toString());//[object Object]
console.log(JSON.stringify(props));//{"a":1}
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
回答2:
This seems to work just fine:
function testprops() {
let sp = PropertiesService.getScriptProperties();
sp.setProperties({'somekey': Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy:MM:dd HH:mm:ss")});
let props = sp.getProperties();
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(props.somekey), "View Properties");
}
来源:https://stackoverflow.com/questions/64284233/google-apps-script-propertiesservice-confused-by-unreliable-executions-logging