使用googlesheetapi删除googlesheet中的行

时间:2016-08-26 08:00:13

标签: google-sheets-api

我想使用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();
    }
}

1 个答案:

答案 0 :(得分:1)

使用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;
        }
    }