ses
ses

Reputation: 13342

Google sheet API, executing AddProtectedRangeRequest

based on: https://developers.google.com/sheets/api/samples/ranges

How to execute Google sheet API, executing AddProtectedRangeRequest ?

 void addRangeRestriction(final String sheetId, final Sheets sheetService) throws IOException {

    AddProtectedRangeRequest addProtectedRangeRequest = new AddProtectedRangeRequest();


    var gridRange = new GridRange();
        gridRange.setSheetId(1);
        gridRange.setStartColumnIndex(1);
        gridRange.setEndColumnIndex(1);
        gridRange.setStartRowIndex(2);
        gridRange.setEndRowIndex(2);

    var protectedRange = new ProtectedRange();
        protectedRange.setRange(gridRange);
        protectedRange.setWarningOnly(true);

    var request = new Request()
            .setAddProtectedRange(new AddProtectedRangeRequest()
                    .setProtectedRange(new ProtectedRange()
                            .setRange(gridRange)
                            .setRequestingUserCanEdit(false)));


    addProtectedRangeRequest.setProtectedRange(protectedRange);

    // TODO: ... ??
}

--

I can do it for

   var batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();

like this: return sheetService.spreadsheets().batchUpdate(sheetId, batchUpdateSpreadsheetRequest).execute();

but not for AddProtectedRangeRequest or Request.. one

Upvotes: 1

Views: 195

Answers (1)

Ma3x
Ma3x

Reputation: 6559

BatchUpdateSpreadsheetRequest accepts a list of requests to execute. You can add your Request to an ArrayList and then assign that to BatchUpdateSpreadsheetRequest.

You can add all the requests that you would like to execute in order in a single BatchUpdateSpreadsheetRequest in this way. Requests will be applied in the order they are specified in the list. If any request is not valid, no requests will be applied.

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

var batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
batchUpdateSpreadsheetRequest.setRequests(requests);

return sheetService.spreadsheets()
    .batchUpdate(sheetId, batchUpdateSpreadsheetRequest)
    .execute();

Upvotes: 1

Related Questions