gpt4 book ai didi

google-sheets-api - 使用 Google Sheet API 删除 Google Sheet 中的行

转载 作者:行者123 更新时间:2023-12-02 20:59:16 26 4
gpt4 key购买 nike

我想使用 googlesheetv4 api 在 googlesheet 中使用 rowno 删除一行。有人可以给我提供一个示例代码吗?我创建了一个删除行的方法公共(public)无效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();
}
}

最佳答案

要使用 google Sheet v4 api 删除 google Sheet 中的行,我们必须设置开始索引和结束索引以及工作表 ID请参阅下面的示例

public void deleteRow(Integer StartIndex, Integer EndIndex) {
Spreadsheet spreadsheet = null;
try {
spreadsheet = service.spreadsheets().get(GoogleExcelFileUtil.SPREASHEET_ID).execute();
} catch (IOException e1) {
e1.printStackTrace();
}
BatchUpdateSpreadsheetRequest content = new BatchUpdateSpreadsheetRequest();
Request request = new Request();
DeleteDimensionRequest deleteDimensionRequest = new DeleteDimensionRequest();
DimensionRange dimensionRange = new DimensionRange();
dimensionRange.setDimension("ROWS");
dimensionRange.setStartIndex(StartIndex);
dimensionRange.setEndIndex(EndIndex);

dimensionRange.setSheetId(spreadsheet.getSheets().get(0).getProperties().getSheetId());
deleteDimensionRequest.setRange(dimensionRange);

request.setDeleteDimension(deleteDimensionRequest);

List<Request> requests = new ArrayList<Request>();
requests.add(request);
content.setRequests(requests);

try {
service.spreadsheets().batchUpdate(GoogleExcelFileUtil.SPREASHEET_ID, content).execute();
} catch (IOException e) {
e.printStackTrace();
} finally {
dimensionRange = null;
deleteDimensionRequest = null;
request = null;
requests = null;
content = null;
}
}

关于google-sheets-api - 使用 Google Sheet API 删除 Google Sheet 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39161266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com