Google Sheet API,执行AddProtectedRangeRequest

2022-04-12 00:00:00 google-sheets-api java

基于:https://developers.google.com/sheets/api/samples/ranges

如何执行Google Sheet API,执行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: ... ??
}

--

我可以为

   var batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();

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

但不适用于AddProtectedRangeRequest..一个


解决方案

BatchUpdateSpreadsheetRequest接受要执行的请求列表。您可以将请求添加到ArrayList,然后将其分配给BatchUpdateSpreadsheetRequest

您可以通过这种方式将您希望按顺序执行的所有请求添加到单个BatchUpdateSpreadsheetRequest中。将按照列表中指定的顺序应用请求。如果任何请求无效,则不会应用任何请求。

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

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

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

相关文章