捕获Google App脚本配额和限制错误 [英] Catch Google App Script Quotas and Limitations errors

查看:67
本文介绍了捕获Google App脚本配额和限制错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于Google表格的google应用脚本.最近,当我的函数在6分钟内将数据插入电子表格中时,遇到了脚本运行时"限制.我的模态窗口刚刚挂起,在开发控制台中,我得到了超出最大执行时间".

I have an google app script for Google Sheets. Recently I came across "Script runtime" limitations when my function was inserting data into a spreadsheet over 6 min. My modal window just hung and in the Dev Console I got "Exceeded maximum execution time".

经过一些研究,我弄清楚了如何重新实现我的功能,以便它可以成批执行,并且不太可能达到极限.

After some researching I figured how to re-implement my function so it would execute in batches and less likely hit the limit.

但是,在这一点上,我想在脚本中添加一些逻辑,这些逻辑可以集中捕获此限制和所有其他可能的问题,并仅向用户显示一些错误消息,而只是挂起而无所事事.

However, at this point I would like add some logic to my script which could centralized catch this limitation and all other possible and simply show some error message to a user in comparison just hanging and doing nothing.

有没有什么方法可以做到这一点?

Are there any approaches to accomplish that ?

推荐答案

我建议使用 ScriptApp.newTrigger 创建触发器,该触发器可以通过引入另一个函数来触发,该函数检查是否脚本已超过预定的执行时间(如果最大为6,则可以将其设置为5,以确保安全).

I'd recommend creating a trigger using ScriptApp.newTrigger, which could be triggered by introducing another function which checks whether or not the script has exceeded the predetermined execution time (if the max is 6, you could set it to be 5 to be safe).

引用类似的内容-

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

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

I've written more on this topic here where i've used a .timeBased() trigger; however, you may use a different kind of trigger altogether.

这篇关于捕获Google App脚本配额和限制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆