Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sheets API v4 dot Net Deleting a row

I am building an interface to tie in with Google Sheets. The way it works is all of the records from a spreadsheet are pulled into a grid view on my interface. There is then a button on this interface offering options such as Edit, Delete, etc.

I am having trouble with my delete method. The way it works is all of my records are pulled in to the method which then uses a for loop to go through the results and compare the ID field from the result to the ID that is passed into the method. When there is a match, that is the one to be deleted. All of this works and everything goes off smoothly, but for some reason, the record is not deleted from my google sheet. Below is my code for the dot NET wrapper of Google Sheets API v4. If anyone can help me it would be greatly appreciated. I've been staring at documentation and piecing together parts of other languages for a couple of hours now and my brain isn't coming up with anything new, lol.

if (ID.ToString() == WHVACRMembers[i][0].ToString()) // IF INCOMING ID MATCHES ID OF CURRENT ROW BEING LOOPED OVER
{
    //DELETE THIS ROW
    Request RequestBody = new Request() { 
        DeleteDimension = new DeleteDimensionRequest() { 
            Range = new DimensionRange() { 
                SheetId = 0, 
                Dimension = "ROWS", 
                StartIndex = Convert.ToInt32(i), 
                EndIndex = Convert.ToInt32(i) 
            } 
        } 
    };

    List<Request> RequestContainer = new List<Request>();
    RequestContainer.Add(RequestBody);

    BatchUpdateSpreadsheetRequest DeleteRequest = new BatchUpdateSpreadsheetRequest();
    DeleteRequest.Requests = RequestContainer;

    service.Spreadsheets.BatchUpdate(DeleteRequest, SpreadsheetID);

}
like image 973
David Webb Avatar asked Mar 10 '23 16:03

David Webb


2 Answers

I just figured out the problem. Instead of:

service.Spreadsheets.BatchUpdate(DeleteRequest, SpreadsheetID);

I needed:

SpreadsheetsResource.BatchUpdateRequest Deletion = new SpreadsheetsResource.BatchUpdateRequest(service, DeleteRequest, SpreadsheetID);
Deletion.Execute();

As soon as I figured that out, everything went through without a hitch. Was just a matter of me not being focused on the right thing while looking through the documentation and object explorer.

like image 81
David Webb Avatar answered Mar 13 '23 05:03

David Webb


Using the same number for StartIndex and EndIndex was not deleting the row for me, I had to add 1 to EndIndex:

            StartIndex = Convert.ToInt32(i), 
            EndIndex = Convert.ToInt32(i + 1) 
like image 30
Jordan Miller Avatar answered Mar 13 '23 05:03

Jordan Miller