Reputation: 431
I am trying to update the background color of cells using google sheets v4 api but keep running in circles. I am able to update the cells value on each iteration but when I try changing the background color,
"cell": {
"userEnteredFormat": {
"backgroundColor": {
"red": 1.0,
"green": 0.4,
"blue": 0.4
}
}
},
in the below code I keep getting the error - 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);
}
}
I've been going through the following documentation but cant seem to figure out what I'm doing incorrectly as there is so much different info. Please, is my problem with my scope or with the api type or in my syntax?
Upvotes: 2
Views: 2051
Reputation: 201388
ucfSelectedRowIndex
.If my understanding is correct, how about this answer?
In this modified script, I modified the script for the API call. Before you use this script, please set the Spreadsheet ID and sheet ID. And also please set the value of 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");
}
}
);
If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 4