4

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.

psheth
  • 43
  • 1
  • 1
  • 9
  • Which server side language you use? They have given some language options but php and java script https://developers.google.com/sheets/quickstart/php , https://developers.google.com/sheets/quickstart/js might help? – Jitesh Sojitra Aug 09 '16 at 18:28
  • Im using javascript, however those links have very limited methods and no POST option. – psheth Aug 09 '16 at 19:38
  • Did you find the Solution For that ? @psheth – Beckham_Vinoth Aug 23 '16 at 11:47

1 Answers1

10

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. enter image description here

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.

gazdagergo
  • 3,886
  • 1
  • 20
  • 34
noogui
  • 15,376
  • 3
  • 18
  • 42
  • While Trying your code am Getting error as `ReferenceError: access_token is not defined` How can i solve that @noogui – Beckham_Vinoth Aug 23 '16 at 08:58
  • @Becky that's because access_token is my user-defined variable. var access_token = 'ya29.CabcdabcdefgkMHMeDNxC1koDhrS3fM-7pjzMFAfCgVPxnNM2dAxyzaldlRBvxKLcyA'; ..You have to find a way to generate your own. – noogui Aug 23 '16 at 09:10
  • Thanks....and I don't know how to create my own access_token ? How can i make that ? Any idea's ? – Beckham_Vinoth Aug 23 '16 at 09:20
  • 1
    @Becky follow my other guide [here](http://stackoverflow.com/questions/37315266/google-sheets-api-v4-receives-http-401-responses-for-public-feeds). – noogui Aug 23 '16 at 09:21
  • If Possible can you pls Answer for that --> http://stackoverflow.com/questions/39096279/inserting-datas-into-google-sheets-api-using-javascript – Beckham_Vinoth Aug 23 '16 at 09:42
  • 1
    Remember, that access token from oAuthPlayground is good for only 1 hour. – noogui Aug 23 '16 at 09:42