I successfully created a chrome extension that uses oAuth2 to write to a google spreadsheet.
Now i have the problem that my code just adds my content to the first row and updates that row, whenever i call the function to add new content.
How do i have to change my code to just add a new row whenever i call it but do not update just the first row over and over again??
This is my code:
function addRowToSpreadsheet(params){
chrome.identity.getAuthToken({ 'interactive': true }, getToken);
function getToken(token) {
let init = {
method: 'PUT',
async: true,
body: JSON.stringify(params),
headers: {
Authorization: 'Bearer ' + token,
},
contentType: 'json',
};
fetch('https://sheets.googleapis.com/v4/spreadsheets/KEY/values/A1:K100?valueInputOption=USER_ENTERED&key=KEY', init)
.then((response) => response.json())
.then(function(data) {
});
}}
function paramCreater(){
var params = {
'values': [
['Row 1 Col A','Row 1 Col B'],
]
};
addRowToSpreadSheet(params);
}
I know that i should use the append function from https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append, but i don't know how to add it to my code.
Should be added somewhere here right?
fetch('https://sheets.googleapis.com/v4/spreadsheets/KEY/values/A1:K100?valueInputOption=USER_ENTERED&key=KEY', init)
POST
request to the following endpoint. At least in the code you provided, you are not adding:append
:https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{range}:append
So try changing this line:
To this one:
POST
, notPUT
. So you would have to change this line:To this one:
Because you were not adding
append
and were making aPUT
request, you were basically calling spreadsheets.values.update, which overwrites the values in the range you specify.Then, if you change the
PUT
toPOST
but don't addappend
, you are not making any valid request (there is noPOST
method with that endpoint), so it does not insert anything at all. If you change both things, it should append the data successfully.I hope this is of any help.