1

i am using javascript only to read and write values in google spreadsheet (private sheet using oAuth). I can successfully read the values but i am getting issue on modifying the sheet. below is my code for updating the cell-

 $.ajax({
          type: 'post',
            headers: {
                Authorization: auth,
                'content-type': 'application/json',
                "access_token": auth,
            },
            data: JSON.stringify({ // generate from oAuthPlayground
                "access_token": auth,
                
                "range": "A1",
                "values": [
                    [
                        "32",
                        
                    ]
                ]
            
            
            }),              
            url: 'https://sheets.googleapis.com/v4/spreadsheets/' + sheetId + '/values/A2:batchUpdate?insertDataOption=INSERT_ROWS&valueInputOption=RAW',               
            success: function (r) {
                console.log(r)
            }, error: function (r) {
                console.log(r)
            }
        });

anybody can suggest what is the mistake. Again i am using only ajax not node.js or google script.

3
  • i am getting issue on modifying the sheet: which issue exactly? Commented Mar 30, 2022 at 16:58
  • it's not updating the cell. Commented Mar 30, 2022 at 17:21
  • Could you include a minimal example? The question is not clear enough to give a useful answer. Commented Mar 30, 2022 at 19:50

1 Answer 1

4

From your showing script, I couldn't understand the following.

  • What method in Sheets API do you want to use?
  • Which do you want to put the value to the cell "A1" or "A2".
  • I couldn't understand whether your access token can be used for updating the Spreadsheet.

From this situation, I guessed your current issue and your goal as follows.

  • You want to put a value of "32" to a cell "A1" of the 1st sheet of the Spreadsheet.
    • In this case, "Method: spreadsheets.values.update" is used.
  • Your access token can be used for updating the Spreadsheet.

In this case, how about the following sample script?

Sample script:

const auth = "Bearer ###"; // Please replace "###" with your access token.
const sheetId = "###"; // Please set your Spreadsheet ID.

$.ajax({
  type: 'put',
  headers: { Authorization: auth, 'content-type': 'application/json' },
  data: JSON.stringify({
    "values": [["32"]]
  }),
  url: 'https://sheets.googleapis.com/v4/spreadsheets/' + sheetId + '/values/A1?valueInputOption=RAW',
  success: function (r) {
    console.log(r)
  }, error: function (r) {
    console.log(r)
  }
});
  • When this script is run, "32" is put to a cell "A1" of the 1st sheet of the Spreadsheet.

Reference:

Sign up to request clarification or add additional context in comments.

2 Comments

how i can update multiple cells value at one time? suppose i want to update cells A5,C5 and F5 all together. i have done something lie this. but it doesn't work.******* type: 'put', headers: { Authorization: "Bearer " +auth, 'content-type': 'application/json' }, data: JSON.stringify({ "range": "Users!A5", "values": [["ABC"]] },{ "range": "Users!C5", "values": [["XYZ"]] }), url: 'sheets.googleapis.com/v4/spreadsheets/'+ sheetId + '?valueInputOption=RAW',
@user3369120 About your new question, I found your posted new question. So I posted an answer to it. Please confirm it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.