我 运行 遇到一些配额问题

I'm running into some quota issue

我有以下代码来清除每个sheet中的所有过滤器:

function clearAllFilter() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var sheetIds = ss.getSheets();
  for (var i in sheetIds) {
    var requests = [{
      "clearBasicFilter": {
        "sheetId": sheetIds[i].getSheetId()
      }
    }];
    Sheets.Spreadsheets.batchUpdate({'requests': requests}, ssId); 
  }
}

代码运行良好,但出现以下错误:

如何消除此错误消息或更好地优化代码以尽快完成其工作?...

编辑:要添加更多信息,我的点差[​​=23=] 有 119 sheets。

您可能已达到当前的配额限制。请注意表格 API 的 Usage Limits

This version of the Google Sheets API has a limit of 500 requests per 100 seconds per project, and 100 requests per 100 seconds per user. Limits for reads and writes are tracked separately. There is no daily usage limit.

To view or change usage limits for your project, or to request an increase to your quota, do the following:

  1. If you don't already have a billing account for your project, then create one.
  2. Visit the Enabled APIs page of the API library in the API Console, and select an API from the list.
  3. To view and change quota-related settings, select Quotas. To view usage statistics, select Usage.

希望对您有所帮助!

@tehhowch 评论是我所需要的,他给了我一个线索,我找到了代码的修复。

错误在于for循环:

for (var i in sheetIds) {
    var requests = [{
      "clearBasicFilter": {
        "sheetId": sheetIds[i].getSheetId()
      }
    }];
    Sheets.Spreadsheets.batchUpdate({'requests': requests}, ssId); 
  }

这里我循环遍历传播sheet中的每个sheet,获取sheet ID,然后调用.batchUpdate()。这里的问题是,我为每个 sheet 调用了 sheets API,这意味着我为所有 119 个 sheets 调用了 119 次。

以上代码效率低下,超出了我的配额限制。

修复:

  1. 将所有 sheet 个 ID 放入一个数组中。
  2. .batchUpdate() 移到 for 循环之外
  3. 然后执行 .batchUpdate({'requests': array} 而不是 .batchUpdate({'requests': requests}

所以现在代码是高效的,而不是调用 sheets API 119 次,现在我只调用它一次,解决了我的配额问题并成功 运行没有任何错误消息的脚本。

完整代码:

function clearAllFilter() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ssId = ss.getId();
  var sheetIds = ss.getSheets();
  var idArray = [];

  //loop through all the sheets, get the sheet ID, then push into the array
  for (var i in sheetIds) {
    var requests = [{
      "clearBasicFilter": {
        "sheetId": sheetIds[i].getSheetId()
      }
    }];
        idArray.push(requests); //now the array stores all the sheet IDs
        //Logger.log(idArray);
  }
      Sheets.Spreadsheets.batchUpdate({'requests': idArray}, ssId); //do .batchUpdate only once by passing in the array
}