我想使用 google sheet api v4 删除 google sheet 中的行
I want to delete row in googlesheet using googlesheet api v4
我想使用 googlesheets4 api 在 google 表格中使用 rownum 删除一行。
有人可以为此提供示例代码吗?
我创建了一个删除行的方法:
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();
}
}
如 Migrate to the Google Sheets API, to delete a row 中所述,使用 Google Sheets API V4,由 spreadsheet.batchUpdate
方法调用处理,使用 DeleteDimension
请求。该请求也可以用于移除列,开发者可以选择只移除部分行或列。
删除具有给定 ID 的 sheet 的第 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 中的更多信息可能会有所帮助。
您的代码看起来很接近,但您没有正确设置 DimensionRange
。尝试以下操作:
Request request = new Request()
.setDeleteDimension(new DeleteDimensionRequest()
.setRange(new DimensionRange()
.setSheetId(0)
.setDimension("ROWS")
.setStartIndex(30)
.setEndIndex(32)
)
);
你的代码是正确的,但你必须确保你执行了请求。我的代码基本上将您的两个结果组合在一起。
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();
}
我不确定我的方法是否最好,但它达到了目的,所以我会解释我的做法。
在要保存的 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());
}}
我想使用 googlesheets4 api 在 google 表格中使用 rownum 删除一行。 有人可以为此提供示例代码吗? 我创建了一个删除行的方法:
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();
}
}
如 Migrate to the Google Sheets API, to delete a row 中所述,使用 Google Sheets API V4,由 spreadsheet.batchUpdate
方法调用处理,使用 DeleteDimension
请求。该请求也可以用于移除列,开发者可以选择只移除部分行或列。
删除具有给定 ID 的 sheet 的第 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 中的更多信息可能会有所帮助。
您的代码看起来很接近,但您没有正确设置 DimensionRange
。尝试以下操作:
Request request = new Request()
.setDeleteDimension(new DeleteDimensionRequest()
.setRange(new DimensionRange()
.setSheetId(0)
.setDimension("ROWS")
.setStartIndex(30)
.setEndIndex(32)
)
);
你的代码是正确的,但你必须确保你执行了请求。我的代码基本上将您的两个结果组合在一起。
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();
}
我不确定我的方法是否最好,但它达到了目的,所以我会解释我的做法。
在要保存的 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()); }}