1

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

ses
  • 13,174
  • 31
  • 123
  • 226

1 Answers1

1

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();

Ma3x
  • 5,761
  • 2
  • 17
  • 22
  • i found that thing happens (it modifies the google sheet file) on `batchUpdateSpreadsheetRequest.setRequests(List.of(request));` even before the `.execute()`... – ses Jan 06 '22 at 18:44
  • Are you sure? How would that be possible, since you do not specify a `sheetId` until you call `sheetService.spreadsheets().batchUpdate(sheetId, batchUpdateSpreadsheetRequest).execute();` – Ma3x Jan 06 '22 at 18:53
  • it did happen for another call (for AddSheetRequest, which is outside of the scope of this question i guess) ok.. – ses Jan 06 '22 at 18:59
  • the call passed successful. but the thing is: I can not see any difference. I still can edit my my fields (by specified column index row index) in google sheet. was expecting to see that the row-columns are "protected" from change. – ses Jan 06 '22 at 19:18
  • @ses I suspect the `gridRange.setSheetId(1)` might be causing that problem. Have you tried with `gridRange.setSheetId(sheetId)`. – Ma3x Jan 06 '22 at 19:40
  • ohh got it: the range should not be: 0,0 - i should be 0,1 - different numbers. Thanks @Ma3x – ses Jan 06 '22 at 20:17