java - 如何在 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?
解决方案
在 .NET API 中有一个GridRange
具有SheetId
属性的类。
如Google Sheets API v4 文档中所示,您的类看起来GridCoordinate
很相似并且有一个setSheetId()
方法
推荐阅读
- javascript - 如何将数据库数据放入数组Vue.js
- angular - Visual Studio CODE for Angular CLI 版本 9.1.1 中的 angular.json 错误中的“属性不允许”
- flutter - 我在颤动中显示网格视图有问题
- django - 将熊猫上传到 django 视图时读取文件时出现问题
- c# - 如何在构建 XDocument 时执行 LINQ 查询?
- c - 什么时候为内核空间中的打开文件创建`struct file*`?
- c# - 检查对象的壁橱继承到声明的类型
- c++ - 如何从前序遍历中列出二叉搜索树中每个节点的子节点?
- python - 在 python 中编写一个不和谐的机器人
- flutter - ListTile 上 onTap 上的警报对话框