0

I'm trying to delete a single row with the Google Sheets API in C#, and I can't get it to work. Fetching and updating rows work as intended.

Here's my code (inspired from C# Google Sheets API - Delete Row and the Java documentation):

var request = new Request
{
    DeleteDimension = new DeleteDimensionRequest
    {
        Range = new DimensionRange
        {
            SheetId = 0,
            Dimension = "ROWS",
            StartIndex = 21,
            EndIndex = 21
        }
    }
};

var deleteRequest = new BatchUpdateSpreadsheetRequest {Requests = new List<Request> {request}};

// First way: create a batch update request from scratch then execute it
var responseFirstWay = new SpreadsheetsResource.BatchUpdateRequest(MY_SHEETS_SERVICE, deleteRequest, MY_SPREADSHEET_ID).Execute();

// Second way: create a batch update request from the existing SheetsService then execute it
var responseSecondWay = MY_SHEETS_SERVICE.Spreadsheets.BatchUpdate(deleteRequest, MY_SPREADSHEET_ID).Execute();

I've tried with various indexes, but it doesn't seem to change anything (in my example above I've put 21, with all rows up to 30 filled with data). I find a bit weird to have a SheetId set to zero, but that's the gid parameter I have when visiting Google Sheets.

No matter how I create my BatchUpdateRequest, the response is empty after I execute it.

3
  • What type of database are you using? The answer may be due to the database. For example SQL Server does not store data in order. It is a Multi-User Threaded database so every time you do a query the data can come back in random order. So to always get same order you need to use an OrderBy. Then if you need to delete you can use TOP (or Bottom) to remove only one results. Commented Jun 3, 2020 at 13:11
  • @jdweng ? It's the Google Sheets API, not a database Commented Jun 3, 2020 at 13:41
  • Note sure. Google sheets is the Front End Application and not sure what the Back End database it is using. The following webpage only say SQL Database : cloud.google.com/blog/products/application-development/… Commented Jun 3, 2020 at 15:38

1 Answer 1

1

According to the documentation for DimensionRange the indexes are half-open meaning the start-index is inclusive, and the end-index is exclusive.

You should change your EndIndex to 22 to delete the 21st row.

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

1 Comment

I'd read this part of the documentation multiple times but I didn't understand it like that. Works perfectly, thanks!

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.