首页 > 解决方案 > 如何在 google sheet java api 中 BatchUpdateSpreadsheetRequest 一个特定的工作表选项卡?

问题描述

在 Google 表格 java api 中,我正在执行 BatchUpdateSpreadsheetRequest,它正在更新表格中单元格的文本和颜色,但它似乎忽略了我的范围请求,告诉它更新第二张而不是第一张。

当我使用下面的字符串列表为整个电子表格设置文本时,我让它工作了

service.spreadsheets().values().update(spreadsheetId, "Sheet2", body).execute();

但这并不能处理更改单元格的颜色。因此,我尝试使用 CellData 对象列表进行批量更新,这适用于第一个工作表选项卡,但拒绝遵守告诉每个请求在第二个工作表上工作的范围。它总是只更新 Sheet1 而不是 Sheet2。

//for every row, do an update request updating that row 
List<Request> requests = new ArrayList<>();
List<String> ranges = new ArrayList<>();
int row=0;
while(row < values.size())
{
//add the request that will update the rows text and cell colors
  requests.add(new Request()
  .setUpdateCells(new UpdateCellsRequest()
  .setStart(new GridCoordinate().setRowIndex(row).setColumnIndex(0))
  .setRows(Arrays.asList(new RowData().setValues(values.get(row))))
  .setFields("userEnteredValue,userEnteredFormat.backgroundColor")));

//add the range specifying that this update should be applied to the second sheet
ranges.add("Sheet2");

row++;
}

//Do tha batch update we just defined
BatchUpdateSpreadsheetRequest batchUpdateRequest = new BatchUpdateSpreadsheetRequest()
.setResponseRanges(ranges)  //all ranges are set to "Sheet2" so why are the requests still updating "Sheet1"??
.setRequests(requests);

service.spreadsheets().batchUpdate(spreadsheetId, batchUpdateRequest)
        .execute();

如何将更新应用到 Sheet2?

标签: javagoogle-sheets-api

解决方案


在 .NET API 中有一个GridRange具有SheetId属性的类。

如Google Sheets API v4 文档中所示,您的类看起来GridCoordinate很相似并且有一个setSheetId()方法


推荐阅读