【问题标题】:How do I use batchUpdate to both insert a new row at the top and then insert 5 cells of data?如何使用 batchUpdate 在顶部插入新行,然后插入 5 个数据单元格?
【发布时间】: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


    【解决方案1】:

    问题不在于 start 或 range 属性;相反,问题在于传递到行数组的对象格式不正确。

    现在,Google 确实使使用 batchUpdate 修改单元格的 API 变得相当复杂。首先,在UpdateCellsRequest 的文档中,我们看到 rows[] 数组采用RowData 对象。 RowData 是一个具有键“values”的对象,其中包含一组 CellData 对象,但每个 CellData 对象都必须包装在一组 {} 中。

    CellData 对象由一个或多个属性组成,在本例中我们选择“userEnteredValue”。这包含一个ExtendedValue 对象,它可以是数字、字符串、布尔值、公式或错误,它是这个复杂的嵌套对象系列中的最后一个。

    上面代码的问题是rows属性在文档中被标记为一个数组,但它实际上是一个包含数组的对象;因此,您在“行”之后缺少花括号。

    这是你应该拥有的:

               {
                    "updateCells": {
                        "rows":
                        {
                            values: [
                                {
                                    "userEnteredValue": {
                                        stringValue: "Dec 14-Sep 20"
                                    }
                                },
                                {
                                    "userEnteredValue": {
                                        numberValue: 10
                                    }
                                },
                                {
                                    "userEnteredValue": {
                                        numberValue: 5
                                    }
                                },
                                {
                                    "userEnteredValue": {
                                        numberValue: 1
                                    }
                                },
                                {
                                    "userEnteredValue": {
                                        numberValue: 0.2
                                    }
                                }
                            ]
                        }
                        ,
                        "fields": "*",
                        "range": {
                            "sheetId": 0,
                            "startRowIndex": 1,
                            "endRowIndex": 2,
                            "startColumnIndex": 0,
                            "endColumnIndex": 5
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2011-04-08
      • 1970-01-01
      • 2019-12-05
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 2019-08-07
      相关资源
      最近更新 更多