Hi I've a small tool to edit a Google Sheet using the API v4. I can read the data from said sheet but I can't append anything.
$.ajax({
url: 'https://sheets.googleapis.com/v4/spreadsheets/' +
'SPREADSHEETID' +
'/values/' + 'TAB' +
'?alt=json&key=KEY',
type: 'POST',
data: {
majorDimension: 'ROWS',
range: 'A1:A2',
values: '32'
},
dataType: 'jsonp',
success: function (data) {
console.log(data);
}
});
This is the code I'm using, but I keep getting the following error: Invalid JSON payload received. Unknown name \"values\": Cannot bind query parameter. Field 'values' could not be found in request message."
I believe your goal is as follows.
application/json.When these points are reflected in your script, it becomes as follows.
$.ajax({
url: 'https://sheets.googleapis.com/v4/spreadsheets/###spreadsheetId###/values/A1?valueInputOption=USER_ENTERED',
method: 'PUT',
data: JSON.stringify({majorDimension: 'ROWS', values: [['32']]}),
headers: {
"Authorization": "Bearer ###your access token###",
"Content-Type": "application/json"
},
success: function (data) {
console.log(data);
},
error: function (res) {
console.log(res.responseText);
}
});
When you use A1 as the range, the value is put to the 1st tab. When you want to put the value to the specific sheet, for example, please use Sheet2!A1. Please be careful about this.
By the way, from appending to Google Sheets of Invalid JSON payload received when appending to Google Sheets API V4, if you want to append the value of "32" to the Spreadsheet, please modify the above script as follows. The reference of the method of spreadsheets.values.append is here.
From
url: 'https://sheets.googleapis.com/v4/spreadsheets/###spreadsheetId###/values/A1?valueInputOption=USER_ENTERED',
method: 'PUT',
To
url: 'https://sheets.googleapis.com/v4/spreadsheets/###spreadsheetId###/values/###SheetName###:append?valueInputOption=USER_ENTERED',
method: 'POST',