1
            var params1 = {
               spreadsheetId: 'Id', 
                range: "sheet 1!A2",
            };
        
            var clearValuesRequestBody = {
            };
        
            var request = await gapi.client.sheets.spreadsheets.values.clear(params1, clearValuesRequestBody);

I am trying to delete a row in google sheets using google sheets API in javascript. But I am able to delete only the first cell of the row. I believe there has to be something in clear values request body but I don't know what. Also if you can suggest me how to remove that blank row that has been created by deleting this row, that would be great.

1
  • I'm trying to understand the documentation, but I am knew to APIs and stuffs. So, I am not able to follow what to do specifically. Can you please suggest an example code where it has been implemented? I can remove the row from the sheet but I don't know how to remove that blank row from the google sheet. Please help. Commented Jun 9, 2021 at 16:46

1 Answer 1

4

You can basically copy the request listed in the doc into the example for performing batch updates in JS.

var batchUpdateRequest = {
  "requests": [
    {
      "deleteDimension": {
        "range": {
          "sheetId": sheetId,
          "dimension": "ROWS",
          "startIndex": 0,
          "endIndex": 1
        }
      }
    }
  ]
}
gapi.client.sheets.spreadsheets.batchUpdate({
  spreadsheetId: spreadsheetId,
  resource: batchUpdateRequest
})

You can check here to see how to find the spreadsheetId and the sheetId. You could put those values in variables or plop them directly into the code where they are referenced.

The startIndex and endIndex tell it to delete row one. Since it is zero-indexed, "startIndex":0 refers to the first row, and "endIndex":1 says to stop before the second row. The endIndex is exclusive, meaning the actions will apply to the part starting with the row indicated by startIndex and ending just before endIndex.

Note: I have not actually used the Google Sheets API. The above information is based solely on what I could find in the docs. batch update in JS, deletion API request.

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

Comments

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.