Google Sheets API v4 dot Net 删除一行
Google Sheets API v4 dot Net Deleting a row
我正在构建一个界面以配合 Google 表格。它的工作方式是将 spreadsheet 中的所有记录拉入我界面上的网格视图中。然后这个界面上有一个按钮,提供编辑、删除等选项
我的删除方法有问题。它的工作方式是将我的所有记录都拉入方法,然后使用 for 循环遍历结果并将结果中的 ID 字段与传递到方法中的 ID 进行比较。当有匹配项时,就是要删除的那个。所有这一切都有效,一切顺利,但由于某种原因,记录没有从我的 google sheet 中删除。下面是我的 Google Sheets API v4.NET 包装器的代码。如果有人可以帮助我,将不胜感激。几个小时以来,我一直在盯着文档并将其他语言的部分拼凑在一起,但我的大脑没有想出任何新东西,哈哈。
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);
}
我刚刚弄清楚了问题所在。而不是:
service.Spreadsheets.BatchUpdate(DeleteRequest, SpreadsheetID);
我需要:
SpreadsheetsResource.BatchUpdateRequest Deletion = new SpreadsheetsResource.BatchUpdateRequest(service, DeleteRequest, SpreadsheetID);
Deletion.Execute();
我一想到这一点,一切就顺利进行了。只是我在浏览文档和对象资源管理器时没有专注于正确的事情。
对 StartIndex 和 EndIndex 使用相同的数字并没有为我删除该行,我不得不向 EndIndex 添加 1:
StartIndex = Convert.ToInt32(i),
EndIndex = Convert.ToInt32(i + 1)
请注意代码在Java所以请转换,这只是为了向你展示我的方法。
我不确定我的方法是否最好,但它达到了目的,所以我会解释我的做法。
在要保存的 sheet 中保留一个额外的列(比如 ROWNUM)
数据.
每次在该列中写入相同的字符串“=ROW()”
您正在发布数据,它将为该行生成行 ID
那就利用下面的方法,希望对你有帮助!
public void deleteRows (String range, String textToBeDeleted) {//textToBeDeleted is cell value which will be find in the column and that row will be deleted
try {
Sheets sheetsService = createSheetsService();
ValueRange response = sheetsService.spreadsheets().values()
.get(spreadsheetId, range)
.execute(); //Getting all the data of the sheet
List<Request> requests = new ArrayList<>();
List<List<Object>> allData = response.getValues();
List<Integer> rowsToBeDeleted = new ArrayList<>();
for (List<Object> row : allData) {
if (row.get(0).toString().equalsIgnoreCase(textToBeDeleted)) { //0 is the column value based on which I'll be deleting the row
rowsToBeDeleted.add(Integer.parseInt(row.get(23).toString())); //23 is the RowNumber column in my case
}
}
BatchUpdateSpreadsheetRequest requestBody = new BatchUpdateSpreadsheetRequest();
for (int i = rowsToBeDeleted.size() - 1; i >= 0; i--) { //Reverse loop while deleting so that wrong index doesn't get deleted
Request request = new Request();
request.setDeleteDimension(new DeleteDimensionRequest()
.setRange(new DimensionRange()
.setSheetId(sheetid)
.setDimension("ROWS")
.setStartIndex(rowsToBeDeleted.get(i) - 1)
.setEndIndex(rowsToBeDeleted.get(i))
)
);
requests.add(request);
}
requestBody.setRequests(requests);
Sheets.Spreadsheets.BatchUpdate deleteRequest =
sheetsService.spreadsheets().batchUpdate(spreadsheetId, requestBody);
BatchUpdateSpreadsheetResponse deleteResponse = deleteRequest.execute();
System.out.println(deleteResponse);
} catch (Exception e) {
System.out.println(e.getStackTrace());
}}
我正在构建一个界面以配合 Google 表格。它的工作方式是将 spreadsheet 中的所有记录拉入我界面上的网格视图中。然后这个界面上有一个按钮,提供编辑、删除等选项
我的删除方法有问题。它的工作方式是将我的所有记录都拉入方法,然后使用 for 循环遍历结果并将结果中的 ID 字段与传递到方法中的 ID 进行比较。当有匹配项时,就是要删除的那个。所有这一切都有效,一切顺利,但由于某种原因,记录没有从我的 google sheet 中删除。下面是我的 Google Sheets API v4.NET 包装器的代码。如果有人可以帮助我,将不胜感激。几个小时以来,我一直在盯着文档并将其他语言的部分拼凑在一起,但我的大脑没有想出任何新东西,哈哈。
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);
}
我刚刚弄清楚了问题所在。而不是:
service.Spreadsheets.BatchUpdate(DeleteRequest, SpreadsheetID);
我需要:
SpreadsheetsResource.BatchUpdateRequest Deletion = new SpreadsheetsResource.BatchUpdateRequest(service, DeleteRequest, SpreadsheetID);
Deletion.Execute();
我一想到这一点,一切就顺利进行了。只是我在浏览文档和对象资源管理器时没有专注于正确的事情。
对 StartIndex 和 EndIndex 使用相同的数字并没有为我删除该行,我不得不向 EndIndex 添加 1:
StartIndex = Convert.ToInt32(i),
EndIndex = Convert.ToInt32(i + 1)
请注意代码在Java所以请转换,这只是为了向你展示我的方法。
我不确定我的方法是否最好,但它达到了目的,所以我会解释我的做法。
在要保存的 sheet 中保留一个额外的列(比如 ROWNUM) 数据.
每次在该列中写入相同的字符串“=ROW()” 您正在发布数据,它将为该行生成行 ID
那就利用下面的方法,希望对你有帮助!
public void deleteRows (String range, String textToBeDeleted) {//textToBeDeleted is cell value which will be find in the column and that row will be deleted try { Sheets sheetsService = createSheetsService(); ValueRange response = sheetsService.spreadsheets().values() .get(spreadsheetId, range) .execute(); //Getting all the data of the sheet List<Request> requests = new ArrayList<>(); List<List<Object>> allData = response.getValues(); List<Integer> rowsToBeDeleted = new ArrayList<>(); for (List<Object> row : allData) { if (row.get(0).toString().equalsIgnoreCase(textToBeDeleted)) { //0 is the column value based on which I'll be deleting the row rowsToBeDeleted.add(Integer.parseInt(row.get(23).toString())); //23 is the RowNumber column in my case } } BatchUpdateSpreadsheetRequest requestBody = new BatchUpdateSpreadsheetRequest(); for (int i = rowsToBeDeleted.size() - 1; i >= 0; i--) { //Reverse loop while deleting so that wrong index doesn't get deleted Request request = new Request(); request.setDeleteDimension(new DeleteDimensionRequest() .setRange(new DimensionRange() .setSheetId(sheetid) .setDimension("ROWS") .setStartIndex(rowsToBeDeleted.get(i) - 1) .setEndIndex(rowsToBeDeleted.get(i)) ) ); requests.add(request); } requestBody.setRequests(requests); Sheets.Spreadsheets.BatchUpdate deleteRequest = sheetsService.spreadsheets().batchUpdate(spreadsheetId, requestBody); BatchUpdateSpreadsheetResponse deleteResponse = deleteRequest.execute(); System.out.println(deleteResponse); } catch (Exception e) { System.out.println(e.getStackTrace()); }}