我想在谷歌表中附加。我写了一个代码,它成功地在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);
});
}
我想更改附加行的背景颜色。前三个单元格为蓝色,另外四个单元格为灰色。
我也尝试使用BatchUpdate但它覆盖给定范围的行,而不是附加在工作表文件中。如果有人知道如何使用BatchUpdate追加行,那么请回答我。
要一次性完成此操作,您必须将batchUpdate与AppendCellsRequest一起使用。不幸的是,以这种方式追加值比使用spreadsheets.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);
});
使用Google API始终是一次冒险^ _ ^希望这会有所帮助。
唯一可用于在API中附加的内置方法是spreadsheets.values.append
将值附加到电子表格。输入范围用于搜索现有数据并查找该范围内的“表”。值将从表的第一列开始附加到表的下一行。
HTTP请求
POST https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}:append
如果您需要示例代码,请查看Append Values sample。