UpdateCellsRequest - 范围未按预期运行

UpdateCellsRequest - Range not behaving as expected

正在尝试使用 UpdateCellsRequest 更新一系列单元格的格式,但它仅更新该范围内的第一个单元格。

let fixFormats = () => {
    return new Promise((resolve, reject) => {
        var sheets = google.sheets('v4');

        var options = {
            auth: auth,
            spreadsheetId: spreadsheetId,
            resource: {
                requests: [{
                    updateCells: {
                        range: {
                            sheetId: 0,
                            startColumnIndex: 0,
                            endColumnIndex: 2,
                        },
                        rows: [{
                            values: {
                                userEnteredFormat: {
                                    numberFormat: {
                                        type: "CURRENCY",
                                        pattern: "$#,##0.00"
                                    }
                                }
                            }
                        }],
                        fields: "userEnteredFormat.numberFormat",
                    }
                }]
            }
        }

        sheets.spreadsheets.batchUpdate(options, (err, res) => {
            if (err) {
                reject(err);
            } else {
                resolve(res);
                console.log(res);
            }
        });
    });
}

根据this post, it should be possible. I've gone through the reference docs and verified that my request is formatted properly. With special attention given to how range定义。

编辑:来自 startColumnIndex(包含)和 endColumnIndex(不包含)的更令人困惑的行为..

虽然 sheetId: 0, startColumnIndex: 0, endColumnIndex: 2 例如只更新单元格 A1... 而 sheetId: 0, startColumnIndex: 0, endColumnIndex: 3 也一样。

但是...如果 sheetId:0,startColumnIndex:1,endColumnIndex:2 然后只更新列 B (colIndex: 1)...

这是一个错误还是我遗漏了什么?

编辑:按要求输出...虽然不确定这是否显示正确的响应。

{ spreadsheetId: '<mysheet>',
  replies: [ {} ],
  updatedSpreadsheet: 
   { spreadsheetId: '<mysheet>',
     properties: 
      { title: 'OLR',
        locale: 'en_US',
        autoRecalc: 'ON_CHANGE',
        timeZone: 'America/Los_Angeles',
        defaultFormat: [Object] },
     sheets: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
     namedRanges: [ [Object] ],
     spreadsheetUrl: 'https://docs.google.com/a/company.com/spreadsheets/d/<mysheet>/edit' } }

如果我 JSON.stringify 响应,它会为它看起来范围内的每个单元格提供格式,这是很多单元格。尽管如此,在选定的 3 列中,第一列中的第一个单元格已更新,但其余单元格保持不变。

想通了!虽然我能够通过使用 'repeatCells' 函数而不是 'updateCells' 来实现它,但我不确定为什么它不起作用,因为它似乎是该函数的预期目的。我将不得不再次阅读这篇文章,但为了后代更新的代码有效:

var options = {
            auth: auth,
            spreadsheetId: spreadsheetId,
            resource: {
                requests: [{
                    repeatCell: {
                        fields: "userEnteredFormat.numberFormat",
                        range: {
                            sheetId: 1564685276,
                            startColumnIndex: 1, // inclusive
                            endColumnIndex: 3, // exclusive
                        },
                        cell: {
                            userEnteredFormat: {
                                numberFormat: {
                                    type: "CURRENCY",
                                    pattern: "$#,##0.00"
                                }
                            }
                        }
                    }
                }],
            }
        }

在您传递给 sheets.spreadsheets.batchUpdate()options 负载中,resources.requests[].updateCells.rows[].values[] - 更具体地说,values 字段实际上应该是一个数组。在您的示例中,您将其作为散列。

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/sheets#RowData