繁体   English   中英

Google Apps Script PropertiesService - 被不可靠的执行日志记录和编辑器调试搞糊涂了

[英]Google Apps Script PropertiesService - Confused by unreliable Executions logging & editor debugging

我发现PropertiesService不可靠,但可能我只是不熟悉 Google Apps Script 或 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, 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   

标记为Editor类型的是从应用程序脚本 web IDE 手动调试运行,我在添加这些PropertiesServices行之前每 15 分钟设置一次onTrigger 每当我每次执行查看Execution log页面的时候,需要几分钟才能得到日志结果,而刚才,半个多小时后,我重新查看,那些Unknown status logs都标记为Completed ,而且都在0.5s以下.

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

为什么我不能从props键值对中获取字符串?

  • 对于相对更好和可靠的日志记录,请直接使用 Stackdriver(即,查看 > Stackdriver 日志记录),而不是从仪表板中的“执行”页面。 为此,您需要通过在 Resources > Cloud Platform project > Change project 中设置自定义项目编号, 将 Google 云项目从默认切换为标准

  • 记录对象时,您必须始终JSON.stringify object,然后再将其提供给console props.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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM