【发布时间】:2019-08-19 12:49:22
【问题描述】:
我正在使用 Google 表格客户端库在电子表格中插入新行,然后在顶行(但在标题行下方)写入一些数据。
batchUpdate 方法应该允许那些使用它的人以一种方式组合操作,如果一个操作失败,其他操作也会失败。这对我来说是最好的,因为我不想在第 2 行插入数据,除非我先在顶部成功插入新行。
但是,我不太了解如何正确使用 API。结果是在顶部插入了一个新行,但我的数据点中只有一个正在写入第一个单元格,而不是该行中的所有 5 个单元格。这是 API 调用:
const sheets = google.sheets({ version: 'v4', auth });
sheets.spreadsheets.batchUpdate({
spreadsheetId: SHEET_ID,
"resource": {
"requests": [
{
"insertDimension": {
"range": {
"sheetId": 0,
"dimension": "ROWS",
"startIndex": 1,
"endIndex": 2
},
"inheritFromBefore": false
}
},
{
"updateCells": {
"rows":
{
values: [{
"userEnteredValue": {
stringValue: "Dec 14-Sep 20"
},
"userEnteredValue": {
numberValue: 10
},
"userEnteredValue": {
numberValue: 5
},
"userEnteredValue": {
numberValue: 1
},
"userEnteredValue": {
numberValue: 0.2
},
}]
}
,
"fields": "*",
// "start": {
// "sheetId": 0,
// "rowIndex": 1,
// "columnIndex": 4
// },
"range": {
"sheetId": 0,
"startRowIndex": 1, // I want to write to second row only
"endRowIndex": 2,
"startColumnIndex": 0, // start at first column
"endColumnIndex": 5 // end at 5th column
}
}
}
]
}
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
} else {
console.log('No data found.');
}
});
如您所见,我指定了一个范围,但数据;具体来说,数据数组中的最后一个值 0.2 正在写入工作表的第一列。我也尝试使用“开始”GridCoordinate 而不是“范围”GridRange,但结果是一样的。
我需要修改哪些内容才能将该数据数组中的所有 5 个值一次写入工作表中的一行、一列?
【问题讨论】:
标签: node.js google-sheets google-sheets-api