2

I want to delete a row using rowno in googlesheet using googlesheetv4 api. Can anybody provide me a sample code for this? I have created a method for delete row 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 Answer 1

5

to delete row in google Sheet using google Sheet v4 api we have to set start index and end index and also sheet Id see the example below

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;
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.