【问题标题】:Google Sheets API update formatting with batch request带有批处理请求的 Google Sheets API 更新格式
【发布时间】:2021-02-13 17:13:21
【问题描述】:

我正在尝试使用 https://developers.google.com/sheets/api/samples/formatting 上的代码来更新 Google 表格的格式。

到目前为止,我尝试的是创建一个resource 对象并将其传递给sheets.spreadsheets.values.batchUpdate 函数。我不确定这是否是正确的做事方式,但如果我传递一个空的requests,它确实可以正常运行。

function doUpdate(auth) {
  const sheets = google.sheets({version: 'v4', auth});

  var resource = {
    spreadsheetId: SHEET_ID,
    auth: auth,
    valueInputOption: "USER_ENTERED",
    "requests": [
      {
        "repeatCell": {
          "range": {
            "sheetId": 0,
            "startRowIndex": 0,
            "endRowIndex": 10,
            "startColumnIndex": 1,
            "endColumnIndex": 2
          },
          "cell": {
            "userEnteredFormat": {
              "numberFormat": {
                "type": "NUMBER",
                "pattern": "#,##0.0000"
              }
            }
          },
          "fields": "userEnteredFormat.numberFormat"
        }
      }
    ]
  };

  sheets.spreadsheets.values.batchUpdate(resource, SHEET_ID);
}

运行此函数时,我收到以下错误:

(node:10496) UnhandledPromiseRejectionWarning: Error: Invalid JSON payload received. Unknown name "requests[repeatCell][range][startRowIndex]": Cannot bind query parameter. Field 'requests[repeatCell][range][startRowIndex]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][range][sheetId]": Cannot bind query parameter. Field 'requests[repeatCell][range][sheetId]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][cell][userEnteredFormat][numberFormat][type]": Cannot bind query parameter. Field 'requests[repeatCell][cell][userEnteredFormat][numberFormat][type]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][cell][userEnteredFormat][numberFormat][pattern]": Cannot bind query parameter. Field 'requests[repeatCell][cell][userEnteredFormat][numberFormat][pattern]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][range][endColumnIndex]": Cannot bind query parameter. Field 'requests[repeatCell][range][endColumnIndex]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][range][endRowIndex]": Cannot bind query parameter. Field 'requests[repeatCell][range][endRowIndex]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][fields]": Cannot bind query parameter. Field 'requests[repeatCell][fields]' could not be found in request message.
Invalid JSON payload received. Unknown name "requests[repeatCell][range][startColumnIndex]": Cannot bind query parameter. Field 'requests[repeatCell][range][startColumnIndex]' could not be found in request message.
    at Gaxios.<anonymous> (/Users/jimmymaslen/Documents/webscraping/node_modules/gaxios/build/src/gaxios.js:73:27)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/jimmymaslen/Documents/webscraping/node_modules/gaxios/build/src/gaxios.js:16:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:10496) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10496) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我对此感到困惑,因为我发送的请求对象是从 Google 的 API 文档中复制和粘贴的。

有谁知道向 Google Sheets API 发送此类请求的正确方法是什么?

【问题讨论】:

    标签: node.js json google-sheets google-sheets-api


    【解决方案1】:

    修改点:

    • 在您的脚本中,auth 包含在 const sheets = google.sheets({version: 'v4', auth}); 中。所以在这种情况下,auth 不需要在resource 中使用。
    • repeatCell 请求用于 Sheets API 的 batchUpdate 方法。
    • 使用batchUpdate方法时,需要修改你的resource

    这些点反映到你的脚本中,变成如下。

    修改脚本:

    在运行脚本之前,请确认SHEET_ID是否已经被声明。

    function doUpdate(auth) {
      const sheets = google.sheets({ version: "v4", auth });
    
      var resource = {
        spreadsheetId: SHEET_ID,
        resource: {
          requests: [
            {
              repeatCell: {
                range: {
                  sheetId: 0,
                  startRowIndex: 0,
                  endRowIndex: 10,
                  startColumnIndex: 1,
                  endColumnIndex: 2,
                },
                cell: {
                  userEnteredFormat: {
                    numberFormat: {
                      type: "NUMBER",
                      pattern: "#,##0.0000",
                    },
                  },
                },
                fields: "userEnteredFormat.numberFormat",
              },
            },
          ],
        },
      };
    
      sheets.spreadsheets.batchUpdate(resource);
    }
    
    • 上述脚本运行时,工作表ID0的工作表中“B1:B10”单元格的数字格式被修改。

    注意:

    • 在这个修改后的脚本中,假设您的auth 可以用于SHEET_ID 的Google 电子表格的Sheets API 的batchUpdate 方法。请注意这一点。

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 2013-03-25
      • 2015-03-09
      相关资源
      最近更新 更多