【问题标题】:Preventing GoogleJsonResponseException: API call to sheets.spreadsheets.batchUpdate failed with error: Must specify at least one request防止 GoogleJsonResponseException:对 sheet.spreadsheets.batchUpdate 的 API 调用失败并出现错误:必须指定至少一个请求
【发布时间】:2021-10-26 11:36:04
【问题描述】:

我有以下 Google 电子表格功能:

function deleteRows() {
  const sheetName = "Sheet"; // Please set the sheet name.
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const sheetId = sheet.getSheetId();
  const requests = sheet
    .getRange(`S1:S${sheet.getLastRow()}`)
    .createTextFinder("Found")
    .findAll()
    .map(r => r.getRow())
    .reverse()
    .map(r => ({deleteDimension:{range:{sheetId:sheetId,startIndex:r - 1,endIndex:r,dimension:"ROWS"}}}));
  Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
}

脚本使用 API 的 batchUpdate 删除重复的行,如果在 S 列中有文本“找到”,但有时没有重复的行,因此我希望函数不要返回错误:

GoogleJsonResponseException:对 sheet.spreadsheets.batchUpdate 的 API 调用失败并出现错误:必须指定至少一个请求。

然后继续执行上述 deleteRows 函数后运行的下一个函数。

如果没有请求,如何防止返回错误?

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-sheets-api


    【解决方案1】:

    你的情况,下面的修改怎么样?

    发件人:

    Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
    

    收件人:

    if (requests.length > 0) {
      Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
    }
    
    • 通过此修改,当requests 有值时,运行Sheets.Spreadsheets.batchUpdate

    【讨论】:

    • 非常感谢,简单有效:D
    • @mishelles 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多