I am trying to post some data from the client side to the google spreadsheet. After looking hard through their documentation for v4 I haven't found a way.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
How to Write to to a Cell Using Spreadsheet API in JS
In essence, you have to know XHR. I'll give you an example. Let's say you want to write to A1:B1 range of your spreadsheet, say [A1]Hello [B1]World.
Your URI request would look like this:
PUT https://sheets.googleapis.com/v4/spreadsheets/{SPREADSHEET_ID}/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED
request body:
{
"range":"Sheet1!A1:B1",
"majorDimension": "ROWS",
"values": [
["Hello"," World"]
],
}
Did this in oauthplayground using Google Sheetsv4 and it worked.
How to apply this in JS?
After setting up the JS Quickstart find a way to call this inside a function:
var params = {
"range":"Sheet1!A1:B1",
"majorDimension": "ROWS",
"values": [
["Hello","World"]
],
}
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://sheets.googleapis.com/v4/spreadsheets/{SPREADSHEET_ID}/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED');
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
xhr.send(JSON.stringify(params));
Also read Reading & Writing cells in Sheets v4. Hope this crash course helps.