gpt4 book ai didi

java - 使用 Google Sheets API v4 更改行的背景颜色

转载 作者:行者123 更新时间:2023-12-01 13:21:45 25 4
gpt4 key购买 nike

我正在尝试更改 Google 电子表格中第一行(标题)的背景颜色。基于几个小时的研究和阅读,我想出了以下代码:

Request updateBackgroundColorRequest = new Request();
UpdateCellsRequest updateCellsRequest = new UpdateCellsRequest();

CellFormat cellFormat = new CellFormat();
Color color = new Color();
color.setRed((float) 246);
color.setGreen((float) 178);
color.setBlue((float) 107);

cellFormat.setBackgroundColor(color);

GridRange gridRange = new GridRange();
gridRange.setStartRowIndex(0);
gridRange.setEndRowIndex(1);
gridRange.setStartColumnIndex(0);
gridRange.setEndColumnIndex(14);
updateCellsRequest.setRange(gridRange);

String field = "userEnteredFormat.backgroundColor";
updateCellsRequest.setFields(field);
updateBackgroundColorRequest.setUpdateCells(updateCellsRequest);

requestList.add(updateBackgroundColorRequest);
batchUpdateSpreadsheetRequest.setRequests(requestList);

Spreadsheets.BatchUpdate batchUpdate = service.spreadsheets()
.batchUpdate(spreadsheetId, batchUpdateSpreadsheetRequest);
batchUpdate.execute();

但是,尽管我能够成功执行此代码(API 未返回任何错误),但背景颜色根本没有改变(我希望它变为橙色,但仍为白色)。

我错过了什么?

最佳答案

被同样的问题困住了。谢谢你的代码!它对我有很大帮助!
所以问题就出来了。

updateBackgroundColorRequest.setUpdateCells(updateCellsRequest);
您需要 RepeatCellRequest 类而不是 UpdateCellRequest .
简单的请求集会像
requestList.add(new Request().setRepeatCell(
new RepeatCellRequest().
setCell(cellData).
setRange(gridRange).
setFields("*")));
我所有的代码看起来像这样:
List<Request> requestList = new ArrayList<>();

CellFormat cellFormat = new CellFormat(); //setting cell color
Color color = new Color();
color.setRed(246f);
color.setGreen(178f);
color.setBlue(107f);

cellFormat.setBackgroundColor(color);

CellData cellData = new CellData();
cellData.setUserEnteredFormat(cellFormat);

GridRange gridRange = new GridRange(); //setting grid that we will paint
gridRange.setSheetId(1115615634); //you can find it in your URL - param "gid"
gridRange.setStartRowIndex(0);
gridRange.setEndRowIndex(1);
gridRange.setStartColumnIndex(0);
gridRange.setEndColumnIndex(14);

requestList.add(new Request().setRepeatCell(new RepeatCellRequest().setCell(cellData).setRange(gridRange).setFields("userEnteredFormat.backgroundColor")));

BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
batchUpdateSpreadsheetRequest.setRequests(requestList);

final Sheets.Spreadsheets.BatchUpdate batchUpdate = sheetsService.
spreadsheets().batchUpdate("yourSpreadSheetId - find it in URL", batchUpdateSpreadsheetRequest);

batchUpdate.execute();

关于java - 使用 Google Sheets API v4 更改行的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com