【发布时间】: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