我想使用googlesheetv4 api在googlesheet中使用rowno删除一行。 任何人都可以为我提供示例代码吗? 我创建了一个删除行的方法:
public void deleteRow() {
BatchUpdateSpreadsheetRequest content = new BatchUpdateSpreadsheetRequest();
Request request = new Request();
request.setDeleteDimension(new DeleteDimensionRequest().setRange(new DimensionRange().setDimension("D30:D31")));
List<Request> requests = new ArrayList<Request>();
requests.add(request);
content.setRequests(requests);
System.out.println(content.getRequests());
try {
service.spreadsheets().batchUpdate(IConstant.SPREADSHEET_ID, content);
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
正如Migrate to the Google Sheets API中提到的那样,使用Google表格API V4向delete a row提及,使用spreadsheet.batchUpdate
请求通过DeleteDimension
方法调用进行处理。此请求也可用于删除列,开发人员可以选择仅删除行或列的一部分。
示例代码,用于删除具有给定ID的工作表的第6行:
POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
{
"requests": [
{
"deleteDimension": {
"range": {
"sheetId": sheetId,
"dimension": "ROWS",
"startIndex": 5,
"endIndex": 6
}
}
}
],
}
row and column operations中的更多信息可能有所帮助。
答案 1 :(得分:1)
您的代码看起来很近,但您没有正确设置DimensionRange
。请尝试以下方法:
Request request = new Request()
.setDeleteDimension(new DeleteDimensionRequest()
.setRange(new DimensionRange()
.setSheetId(0)
.setDimension("ROWS")
.setStartIndex(30)
.setEndIndex(32)
)
);
答案 2 :(得分:0)
您的代码正确,但是您必须确保执行请求。我的代码基本上将您的两个结果结合在一起。
BatchUpdateSpreadsheetRequest content = new BatchUpdateSpreadsheetRequest();
Request request = new Request()
.setDeleteDimension(new DeleteDimensionRequest()
.setRange(new DimensionRange()
.setSheetId(Integer.parseInt(Defines.GRID_ID))
.setDimension("ROWS")
.setStartIndex(30)
.setEndIndex(30 + numofrows)
)
);
// request.setDeleteDimension(new DeleteDimensionRequest().setRange(new DimensionRange().setDimension("D30:D31")));
List<Request> requests = new ArrayList<Request>();
requests.add(request);
content.setRequests(requests);
try {
sheetsService.spreadsheets().batchUpdate(Defines.GOOGLE_SPREADSHEET_ID, content).execute();
} catch (IOException e) {
e.printStackTrace();
}
答案 3 :(得分:0)
我不确定我的方法是否是最好的方法,但它达到了目的,所以我会解释我做了什么。
在要保存的工作表中多保留一列(比如 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());
}}