是否可以通过App脚本代码查询配额限制的当前状态? [英] Is possible query the current status of quotas limitations from a App Script code?

查看:86
本文介绍了是否可以通过App脚本代码查询配额限制的当前状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Google App脚本中编写了一些代码,由于达到执行配额限制,我经常出现异常.

I have wrote some codes in Google App Script and I get recurrently exceptions because I reach execution quota limits.

我想避免这种例外情况,但是我没有找到用于查询配额当前状态的任何类或方法.

I want to avoid this exceptions but I not found any class or method to query the current status of the quotas.

推荐答案

不幸的是,没有单个函数可以让您直接跳过任何配额限制.

Unfortunately, there's no single function that would allow you to skip any quota limitations outright.

但是,您可以构建一个简单的体系结构,一旦您知道遇到哪种速率限制(例如时间或命中次数等),就可以构建代码的逻辑来适应这种情况.场景.

However, you can build a simple architecture where once you know what kind of rate limits you'd run into (say, either time or the count of hits etc.), you can build the logic of your code to accommodate such scenarios.

示例:以下是您以编程方式绕过脚本执行超时的方法-

用于检测阈值的代码-

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

有意破坏代码的代码(在脚本为您完成之前),因此您可以以编程方式设置触发器,以在破坏代码的地方进行处理-

Code to intentionally break the code (before the script does it for you) so you can programatically setup triggers to pick things up right where it were broken off -

function firstFunction() {
    var today = new Date();
    for (var i = 0; i < something.length; i++) {
        // check if the script has exceeded threshold
        if (isTimeUp(today)) {
            // schedule a trigger for a different function
            ScriptApp.newTrigger("repeatFunction")
                .timeBased()
                .everyMinutes(1)
                .create();
            break;
        }
    }
}

function repeatFunction() {
    for (/* condition */) {
        // do stuff; maybe not everything from firstFunction()
        if (/* test condition that defines the last action */) {
            var triggers = ScriptApp.getProjectTriggers();
            for (var i = 0; i < triggers.length; i++) {
                // delete all triggers
                ScriptApp.deleteTrigger(triggers[i]);
            }
            break;
        }
    }
}

我知道您可能不需要太多的编码部分,但是我很乐意按要求提供帮助:)

I realise you might've not needed too much of the coding part of this but I'd be happy to help as required :)

这篇关于是否可以通过App脚本代码查询配额限制的当前状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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