通过 Google Sheet API 更改单元格的颜色:无效请求

Change the Color of a Cell through Google Sheet API: Invalid requests

我想通过Google Sheet API.

设置一个单元格(或一组单元格)的背景颜色

我写了这个请求,当我写 .setFields("*") 时它完美地工作,但我不能这样做,因为这会覆盖我之前在该单元格上执行的所有请求。

所以我根据this document中看到的字段名指定.setFields("backgroundColor")。 但是我得到一个错误:

"message" : "Invalid requests[1].repeatCell: Invalid field: background_color",

请注意 backgroundColor 已变为 background_color.

我尝试了其他字符串,例如 colorbackgroundcolor... 但 none 有效。我不知道怎么办。

  Color XgoogleColor = new Color().setRed(1f).setGreen(0f).setBlue(0f); // Color.RED

    return new Request()
            .setRepeatCell(new RepeatCellRequest()
                    .setCell(new CellData()
                            .setUserEnteredFormat(new CellFormat()
                                    .setBackgroundColor(XgoogleColor)
                            )
                    )
                    .setRange(new GridRange()
                            .setSheetId(sheetId)
                            .setStartRowIndex(startRow)
                            .setEndRowIndex(endRow)
                            .setStartColumnIndex(startColumn)
                            .setEndColumnIndex(endColumn)
                    )
                    .setFields("backgroundColor")
            );

我相信你的情况和目标如下。

  • 在您的脚本中,当使用 .setFields("*") 时,脚本有效。
  • 您只想更新 backgroundColor

这种情况请修改如下。

发件人:

.setFields("backgroundColor")

收件人:

.setFields("userEnteredFormat.backgroundColor")
  • 至此,backgroundColor 更新。

参考: