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);
}
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With