在Google Apps脚本中,是否可以通过try / catch捕获超时,或者超时发生在更高级别?

bug*_*net 2 google-apps-script google-sheets-api

因此,我有一些代码可以通过单击按钮运行。有很多功能,一个接一个。其中一些调用了REST服务。有时,整个shebang会运行6分钟以上,因此会遇到超时屏障,与声音屏障不同,该屏障无法打破。

超时是否可以捕获?我是否可以将最可能的违规者包裹在try / catch中,并在适当的时候对catch块进行评估?还是我坚持尝试使事情表现得如此出色以至于我从来没有碰壁?

Sou*_*ria 5

假设您要调用多个函数,建议您使用创建一个触发器,而不要使用ScriptApp.newTrigger,可以通过引入另一个函数来触发该函数,该函数检查脚本是否超过了预定的执行时间(如果最大值为6,则可以设置安全起见是5。

提到这样的东西-

// var today = new Date();
// 'today' is declard in the original script

function isTimeUp(today) {
  var now = new Date();
  return now.getTime() - today.getTime() > 30000;
  // 30000 = 30 seconds; this is the threshold limit
  // you are free to setup your own threshold limit
}

Run Code Online (Sandbox Code Playgroud)

这里,我已经使用.timeBased()触发器编写更多有关此主题的文章。但是,您可能会完全使用其他类型的触发器。