My goal is to write to an existing Google Sheet via a Ajax/JavaScript call on the client-side.
However, I'm currently getting the following error:
Invalid JSON payload received. Unknown name "{"range":"Sheet1!A8:C8","majorDimension":"ROWS","values":[["Field1","Field2","Field3"]]}": Cannot bind query parameter. Field '{"range":"Sheet1!A8:C8","majorDimension":"ROWS","values":[["Field1","Field2","Field3"]]}' could not be found in request message.
This is my JavaScript code:
const sheetID = "--privateField--";
const key = "--privateField--";
var url = "https://sheets.googleapis.com/v4/spreadsheets/"+sheetID+"/values:batchUpdate/?key="+key;
$.ajax({
url: url,
type: 'PUT',
dataType: 'jsonp',
valueInputOption: 'USER_ENTERED',
data: JSON.stringify({
range: 'Sheet1!A8:C8',
majorDimension: 'ROWS',
values: [
['Field1', 'Field2', 'Field3']
]
}),
headers: {
"Content-Type": "application/json"
},
success: function(data) {
console.log(data);
},
error: function(data) {
console.log(data);
}
Where sheetID & key are replaced with actual production values.
I've tried many different variations of this API call based on different Stack Overflow results I've found, but haven't been able to get anything working. It seems to me like I've specified 'Values' correctly (an array of arrays), but the error message seems to indicate otherwise.
I've been able to read from Google Sheets using Ajax/JavaScript on the client-side before, and am now trying to get an example working for writing to the sheet.
Any advice or tips would be greatly appreciated.