我正在构建一个与Google表格配合的界面。它的工作方式是将电子表格中的所有记录拉入我界面的网格视图中。此界面上有一个按钮,提供编辑,删除等选项
我的删除方法有问题。它的工作方式是我的所有记录都被引入到方法中,然后使用for循环遍历结果并将结果中的ID字段与传递给方法的ID进行比较。匹配时,即要删除的匹配项。所有这一切都有效,一切顺利,但由于某种原因,记录不会从我的谷歌表中删除。下面是我对Google表格API v4的dot 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);
}
答案 0 :(得分:4)
我刚才发现了问题。而不是:
service.Spreadsheets.BatchUpdate(DeleteRequest, SpreadsheetID);
我需要:
SpreadsheetsResource.BatchUpdateRequest Deletion = new SpreadsheetsResource.BatchUpdateRequest(service, DeleteRequest, SpreadsheetID);
Deletion.Execute();
我一想到这一切,一切都顺利完成。在查看文档和对象资源管理器时,我只是不能专注于正确的事情。
答案 1 :(得分:0)
对于StartIndex和EndIndex使用相同的数字并不能删除我的行,我必须在EndIndex中添加1:
StartIndex = Convert.ToInt32(i),
EndIndex = Convert.ToInt32(i + 1)
答案 2 :(得分:0)
请注意代码是用 Java 编写的,所以请转换,这只是为了向您展示我的方法。
我不确定我的方法是否是最好的方法,但它达到了目的,所以我会解释我做了什么。
在要保存的工作表中多保留一列(比如 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());
}}