Reputation: 4808
I'm trying to append data to a spreadsheet.
I use UrlFetchApp
to GET
values but is it possible to POST
values, for example, to append rows to a spreadsheet?
Here is my code so far
var token = ScriptApp.getOAuthToken();
var options = {
"headers": {
'Authorization': 'Bearer ' + token
},
"method": "POST",
"Content-type" : "application/json",
"payload": JSON.stringify(values) //stringify a 2D array of data
}
var resp = UrlFetchApp.fetch("https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/RangeName:append?insertDataOption=INSERT_ROWS", options);
Upvotes: 1
Views: 615
Reputation: 201428
It is possible to use Sheets API post URLs with UrlFetchApp. So how about this modification?
valueInputOption
to the query parameter.
valueInputOption' is required but not specified
occurs.Content-type
, please add it to the headers.
contentType: "application/json"
.// sample values
var values = {"values":[["a1","b1","c1"],["a2","b2","c2"]]};
var spreadsheetId = "### spreadsheetId ###";
var RangeName = "### RangeName ###";
var token = ScriptApp.getOAuthToken();
var options = {
"headers": {
'Authorization': 'Bearer ' + token,
"Content-type": "application/json", // Modified
},
"method": "POST",
"payload": JSON.stringify(values) //stringify a 2D array of data
}
var url = "https://sheets.googleapis.com/v4/spreadsheets/" + spreadsheetId + "/values/" + RangeName + ":append?insertDataOption=INSERT_ROWS&valueInputOption=USER_ENTERED"; // Modified
var resp = UrlFetchApp.fetch(url, options);
If this was not what you want, please tell me. I would like to modify it.
Upvotes: 1