如何同时追加和格式化行 Google Sheet API

jan*_*nny 6 javascript api spreadsheet google-sheets google-sheets-api

我想附加到谷歌表中。我写了一段代码,它在 JavaScript 中成功运行。

function appendMajor() {
  var responseJson ='{values : [["3/2/2017 13:38:32","3/2/2017","12:33:00 PM","ABC","xyz","pqr","bca"]] }';
  gapi.client.sheets.spreadsheets.values.append({
    spreadsheetId: 'spreadSheetId',
    range: 'A1:B',  
    resource: responseJson,
    valueInputOption: 'USER_ENTERED',
  }).then(function (response) {
    appendPre(response);
  }, function (response) {
    appendPre('Error: ' + response.result.error.message);
  });
}
Run Code Online (Sandbox Code Playgroud)

我想更改附加行的背景颜色。前三个单元格为蓝色,另外四个单元格为灰色。

我也尝试使用 BatchUpdate 来执行此操作,但它会覆盖给定范围的行,而不是附加到工作表文件中。如果有人知道如何使用 BatchUpdate 附加行,请回答我。

Jrd*_*Jrd 6

要一次性完成此操作,您必须将 batchUpdate 与 AppendCellsRequest 结合使用。不幸的是,以这种方式附加值比电子表格.values.append 有点冗长,但它会得到你想要的。此外,您不需要指定范围(您的“A1:B”),而是需要sheetId(0 表示默认工作表)。以下是将所需样式应用到默认工作表的示例:

const values = ['3/2/2017 13:38:32','3/2/2017','12:33:00 PM','ABC'];
const colors = [
    [0.0, 0.0, 1.0],    //Blue
    [0.0, 0.0, 1.0],    //Blue
    [0.0, 0.0, 1.0],    //Blue
    [0.5, 0.5, 0.5]     //Grey
];
gapi.client.spreadsheets.batchUpdate({
    spreadsheetId: 'spreadsheetId',
    resource: {
        requests: [{
            appendCells: {
                sheetId: 0,
                rows: [{
                    values: values.map((v, i) => ({
                        userEnteredValue: {
                            stringValue: v
                        },
                        userEnteredFormat: {
                            backgroundColor: {
                                red: colors[i][0],
                                green: colors[i][1],
                                blue: colors[i][2]
                            }
                        }
                    }))
                }],
                fields: '*'
            }
        }]
    }
}, (err, resp) => {
    if (err) throw err;
    console.log(resp.data);
});
Run Code Online (Sandbox Code Playgroud)

使用 Google API 始终是一次冒险 ^_^ 希望这会有所帮助。


Rey*_*cia -1

API 中唯一可用的附加内置方法是试算表.values.append

将值附加到电子表格。输入范围用于搜索现有数据并查找该范围内的“表”。值将从表的第一列开始附加到表的下一行。

HTTP请求

POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}:append
Run Code Online (Sandbox Code Playgroud)

如果您需要示例代码,请选中附加值示例