微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Google Apps脚本PropertiesService-被不可靠的执行日志记录和编辑器调试所迷惑

如何解决Google Apps脚本PropertiesService-被不可靠的执行日志记录和编辑器调试所迷惑

我发现PropertiesService不可靠,但可能我只是不熟悉Google Apps脚本或Stackdriver,并在这里犯了错误或假定有可能导致问题的原因。

这是脚本:

sp = PropertiesService.getScriptProperties()
sp.setProperties({
  'somekey': 'value'
})
props = sp.getProperties()

console.log(props.toString())

这是我写这个SO问题之前的日志:

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,11:11:43 PM   0 s       UnkNown    Debug [object Object]
Editor   Oct 9,11:08:09 PM   0 s       UnkNown    Debug [object Object],ScriptProperties
Editor   Oct 9,11:05:16 PM   0 s       UnkNown    Debug [object Object],ScriptProperties   

标记Editor类型的是从应用脚本Web IDE手动调试运行,我每15分钟设置onTrigger,然后再添加这些PropertiesServices行。每当我在每次执行中检查Execution日志页面时,都需要花费几分钟来获取日志结果,而现在,未来半个多小时,我会重新检查并查看那些UnkNown状态日志被标记Completed,并且都在0.5秒之内。

这只是故障吗?如果这不正常或我做出了错误/错误的假设,我应该怎么做才能确保我不会遇到这种不可预测的结果?

为什么我没有从props键值对中获得字符串?

解决方法

  • 要获得相对更好和可靠的日志记录,请直接使用Stackdriver(即View> Stackdriver logging),而不是从仪表板的“执行”页面中使用。为此,您需要在资源> Cloud Platform项目>更改项目中设置自定义项目编号,以switch Google cloud project from default to standard

  • 记录对象时,必须始终JSON.stringify对象,然后再将其提供给consoleprops.toString()只会返回[object Object],因为它是内部结构。

/*<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>

,

这似乎很好:

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");
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。