I'm trying to use the Google Sheets API spreadsheets.values.batchUpdate
function in the same way as described here. This example uses Google Apps Script. I would like to do it using Google Cloud Functions instead.
The spreadsheets.values.batchUpdate API documentation is available. Note that it is different from the spreadsheets.batchUpdate , which has a different syntax. I'm interested in using the former.
I would like to do the same using the environment: Google Cloud Functions + nodejs8 + googleapis
module.
This is the example code for Google Apps Script from the link above:
function updateGoogleSheet(spreadsheetId) {
/* Written by Amit Agarwal */
/* Web: ctrlq.org Email: amit@labnol.org */
var data = [
{
range: "Sheet1!A1", // Update single cell
values: [
["A1"]
]
},
{
range: "Sheet1!B1:B3", // Update a column
values: [
["B1"],["B2"],["B3"]
]
},
{
range: "Sheet1!C1:E1", // Update a row
values: [
["C1","D1","E1"]
]
},
{
range: "Sheet1!F1:H2", // Update a 2d range
values: [
["F1", "F2"],
["H1", "H2"]
]
}];
var resource = {
valueInputOption: "USER_ENTERED",
data: data
};
Sheets.Spreadsheets.Values.batchUpdate(resource, spreadsheetId);
}
.
This is the above code in the new environment:
const {google} = require('googleapis');
const sheets = google.sheets({version: 'v4'});
const auth = new google.auth.JWT (credentials.client_email, null, credentials.private_key, ['https://www.googleapis.com/auth/spreadsheets']);
function updateGoogleSheet (spreadsheetId) {
/* Written by Amit Agarwal */
/* Web: ctrlq.org Email: amit@labnol.org */
var data = [
{
range: "Sheet1!A1", // Update single cell
values: [
["A1"]
]
},
{
range: "Sheet1!B1:B3", // Update a column
values: [
["B1"],["B2"],["B3"]
]
},
{
range: "Sheet1!C1:E1", // Update a row
values: [
["C1","D1","E1"]
]
},
{
range: "Sheet1!F1:H2", // Update a 2d range
values: [
["F1", "F2"],
["H1", "H2"]
]
}];
var resource = {
spreadsheetId: spreadsheetId,
auth: auth,
valueInputOption: "USER_ENTERED",
data: data
};
sheets.spreadsheets.values.batchUpdate (resource);
}
However this gives the following error:
Error: Invalid JSON payload received. Unknown name "data[range]": Cannot bind query parameter. Field 'data[range]' could not be found in request message. Invalid JSON payload received. Unknown name "data[values]": Cannot bind query parameter. Field 'data[values]' could not be found in request message. at Gaxios.request (/srv/node_modules/gaxios/build/src/gaxios.js:70:23) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7) response: { config: [Object], data: [Object], headers: [Object], status: 400, statusText: 'Bad Request' }, config: { url: 'https://sheets.googleapis.com/v4/spreadsheets/......spreadsheetId....../values:batchUpdate?valueInputOption=USER_ENTERED&data%5Brange%5D=Sheet1%21A1&data%5Bvalues%5D=A1&data%5Brange%5D=Sheet1%21B1%3AB3&data%5Bvalues%5D=B1&data%5Bvalues%5D=B2&data%5Bvalues%5D=B3&data%5Brange%5D=Sheet1%21C1%3AE1&data%5Bvalues%5D=C1&data%5Bvalues%5D=D1&data%5Bvalues%5D=E1&data%5Brange%5D=Sheet1%21F1%3AH2&data%5Bvalues%5D=F1&data%5Bvalues%5D=F2&data%5Bvalues%5D=H1&data%5Bvalues%5D=H2', method: 'POST', paramsSerializer: [Function], headers: [Object], params: [Object], validateStatus: [Function], retry: true, responseType: 'json', retryConfig: [Object] }, code: 400, errors: [ [Object] ]
My question is, how to format the resource parameter to spreadsheets.values.batchUpdate () to make the request succeed?
I couldn't find any documentation on this, but it seems possible from the original code example above.