如何修复 "Service invoked too many times in a short time: exec qps."

How to fix "Service invoked too many times in a short time: exec qps."

我有一个相当大的学校设置来记录会议时间和学生的信息,links 到单独的表格。

使一切变得 link 的大部分是一个自定义函数,它从 hyperlink 函数中提取 URL。我找到了代码 here and here。此函数在学校电子表格中用作每个学生的唯一标识符。

我尝试输入 Utilities.sleep 各种数字,包括 3000,但错误最终是 return(再次是零星的)。看来这可能是服务受到每日配额限制的问题,但我没有看到具体解决的 exec qps。

/** 
 * Returns the URL of a hyperlinked cell, if it's entered with hyperlink command. 
 * Supports ranges
 * @param {A1}  reference Cell reference
 * @customfunction
 */
function linkURL(reference) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var formula = SpreadsheetApp.getActiveRange().getFormula();
  var args = formula.match(/=\w+\((.*)\)/i);
  try {
    var range = sheet.getRange(args[1]);
  }
  catch(e) {
    throw new Error(args[1] + ' is not a valid range');
  }
  var formulas = range.getFormulas();
  var output = [];
  for (var i = 0; i < formulas.length; i++) {
    var row = [];
    for (var j = 0; j < formulas[0].length; j++) {
      var url = formulas[i][j].match(/=hyperlink\("([^"]+)"/i);
      row.push(url ? url[1] : '');
    }
    output.push(row);
    Utilities.sleep(2000);
  }
  return output
}

我遇到的一个错误是 'TypeError: Cannot read property "1" from null. (line 16, file "Code")',它似乎不一定会影响函数执行。我很想知道为什么会发生此错误。

但是,我确实偶尔会在不同学校的电子表格中遇到错误,这些错误显示为 "Service invoked too many times in a short time: exec qps. Try Utilities.sleep(1000) between calls. (line 0)." 这会在整个电子表格中显示 #Error 并导致信息无法显示。

看来这可能是服务受到每日配额限制的问题,但我没有看到具体解决的 exec qps。在这么大的项目中有解决这个问题的方法吗?学校系统是否可以升级到可以使错误消失的东西?

sleep 方法放入 for 循环中不会阻止配额错误,因为该循环未使用任何服务。

服务是:

sheet = SpreadsheetApp.getActiveSheet();
formula = SpreadsheetApp.getActiveRange().getFormula();

服务应放入 for 循环中,如下所示:

/** 
 * Returns the URL of a hyperlinked cell, if it's entered with hyperlink command. 
 * Supports ranges
 * @param {A1}  reference Cell reference
 * @customfunction
 */
function linkURL(reference) {
  var formula,i,sheet;

  for (i=1;i<4;i++) {//Retry up to 3 times - must begin at 1 for sleep calculation
    try{
      sheet = SpreadsheetApp.getActiveSheet();
      formula = SpreadsheetApp.getActiveRange().getFormula();
      break;
    }catch(e){
      if (i!==3){Utilities.sleep(i*1500);}//Wait an increasingly longer time on each iteration
      if (i>=3) {
        errorHandling_(e);//Call central error handling
      }
    };

  }

  if (!sheet) {//If there is no sheet then there is no point in running more code
    //An error has already been handled above
    return;
  }

  var args = formula.match(/=\w+\((.*)\)/i);

  try {
    var range = sheet.getRange(args[1]);
  }catch(e) {
    errorHandling_(e);
  }

  if (!range) {//If there is no range then there is no point in running the code below -
    //An error message has already been handled above
    return;
  }

  var formulas = range.getFormulas();
  var output = [];

  for (var i = 0; i < formulas.length; i++) {
    var row = [];
    for (var j = 0; j < formulas[0].length; j++) {
      var url = formulas[i][j].match(/=hyperlink\("([^"]+)"/i);
      row.push(url ? url[1] : '');
    }
    output.push(row);

  }
  return output
}

function errorHandling_(e) {
  var errorMessage,stack;
  //Handle all errors in one central place

  errorMessage = e.message;
  stack = e.stack;

  //Email the user?


  //Email the developer?

}