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

huangapple go评论80阅读模式
英文:

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

问题

我想通过Google Sheet API设置单元格(或一组单元格)的背景颜色。

我编写了以下请求,当我写入`.setFields(" * ")`时,它能够完美运行,但我不能这样做,因为这会覆盖我在该单元格上执行的所有先前请求。

所以我根据[此文档][1]中所示的字段名称指定了`.setFields("backgroundColor")`。
但是我收到了一个错误:

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

请注意,*backgroundColor* 已变为 *background_color*。

我尝试了其他字符串,如 *color*,*backgroundcolor*,但都不起作用。我不知道该怎么办。

      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")
                );

  [1]: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells
英文:

I want to set the background color of a cell (or group of cell) through Google Sheet API.

I wrote this request, it perfectly works when I write .setFields("*"), but I can't do that because this overrides all the previous requests I performed on that cell.

So I specify .setFields("backgroundColor") according to the field name as seen in this document.
But I get an error:

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

Please note that backgroundColor has become background_color.

I tried other strings such as color, backgroundcolor... but none works. I don't know how to do.

  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")
            );

答案1

得分: 2

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

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

在这种情况下,请按照以下方式进行修改。

从:

.setFields("backgroundColor")

到:

.setFields("userEnteredFormat.backgroundColor")
  • 通过这样做,backgroundColor 将会被更新。

参考:

英文:

I believe your situation and goal as follows.

  • In your script, when .setFields("*") is used, the script works.
  • You want to update only backgroundColor.

In this case, please modify as follows.

From:

.setFields("backgroundColor")

To:

.setFields("userEnteredFormat.backgroundColor")
  • By this, backgroundColor is updated.

Reference:

huangapple
  • 本文由 发表于 2020年9月7日 13:40:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63771956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定