我正在尝试使用Google Sheets v4 api更新单元格的背景颜色,但仍在运行。我可以在每次迭代时更新单元格的值,但是当我尝试更改背景色时,
"cell": {
"userEnteredFormat": {
"backgroundColor": {
"red": 1.0,
"green": 0.4,
"blue": 0.4
}
}
},
在下面的代码中,我不断收到错误-GaxiosError: Invalid JSON payload received. Unknown name "cell" at 'data[0]': Cannot find field.
function updateClientFeedbackCells(auth) {
var sheets = google.sheets({ version: 'v4' });
authorize(function () {
var request = {
spreadsheetId: process.env['GOOGLE_SHEET_ID_' + envString], //
valueInputOption: 'RAW',
resource: {
"data": [
{
"range": "N" + ucfSelectedRowIndex,
"majorDimension": "COLUMNS",
"cell": {
"userEnteredFormat": {
"backgroundColor": {
"red": 1.0,
"green": 0.4,
"blue": 0.4
}
}
},
"values": [
[
true
],
]
},
],
},
auth,
};
sheets.spreadsheets.values.batchUpdate(request, function (err, response) {
if (err) {
console.error(err);
return;
}
else {
console.info(response);
console.log("Client Feedback Sent Col Values updated");
};
});
});
function authorize(callback) {
// 'https://www.googleapis.com/auth/drive'
// 'https://www.googleapis.com/auth/drive.file'
// 'https://www.googleapis.com/auth/spreadsheets'
var auth = "https://www.googleapis.com/auth/drive";
if (auth == null) {
console.log('authentication failed');
return;
}
callback(auth);
}
}
我已经阅读了以下文档,但由于有太多不同的信息,因此似乎无法弄清楚我在做什么。请问,我的范围,api类型或语法有问题吗?
ucfSelectedRowIndex
给出。如果我的理解是正确的,那么这个答案呢?
在此修改后的脚本中,我修改了API调用的脚本。使用此脚本之前,请设置电子表格ID和工作表ID。并且请设置ucfSelectedRowIndex
的值。
const sheets = google.sheets({ version: "v4" });
const spreadsheetId = "###";
const sheetId = "###";
const ucfSelectedRowIndex = 1; // Please set more than 0.
const request = {
requests: [
{
updateCells: {
range: {
sheetId: sheetId,
startRowIndex: ucfSelectedRowIndex - 1,
endRowIndex: ucfSelectedRowIndex,
startColumnIndex: 13, // Column "N"
endColumnIndex: 14 // Column "N"
},
rows: [
{
values: [
{
userEnteredFormat: {
backgroundColor: {
red: 1,
green: 0.4,
blue: 0.4
}
},
userEnteredValue: {
boolValue: true
}
}
]
}
],
fields: "userEnteredFormat,userEnteredValue"
}
}
]
};
sheets.spreadsheets.batchUpdate(
{ spreadsheetId: spreadsheetId, requestBody: request, auth },
function(err, response) {
if (err) {
console.error(err);
return;
} else {
console.info(response);
console.log("Client Feedback Sent Col Values updated");
}
}
);
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。