可以从App Script代码查询配额限制的当前状态吗?

时间:2018-04-30 14:39:26

标签: google-apps-script google-drive-api

我已经在Google App Script中编写了一些代码,因为达到执行配额限制,我会反复出现例外情况。

我想避免这种例外,但我没有找到任何类或方法来查询配额的当前状态。

1 个答案:

答案 0 :(得分:5)

不幸的是,没有一个功能可以让您直接跳过任何配额限制。

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

示例:如何bypass script execution timeout, programmatically-

检测阈值的代码-

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

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

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

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