【问题标题】:Change the Color of a Cell through Google Sheet API: Invalid requests通过 Google Sheet API 更改单元格的颜色:无效请求
【发布时间】:2020-09-07 05:40:34
【问题描述】:

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

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

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

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

请注意,backgroundColor 已变为 background_color

我尝试了其他字符串,例如 colorbackgroundcolor...但没有一个有效。我不知道该怎么做。

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

【问题讨论】:

    标签: java google-sheets background-color google-sheets-api


    【解决方案1】:

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

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

    在这种情况下,请进行如下修改。

    发件人:

    .setFields("backgroundColor")
    

    收件人:

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

    参考:

    【讨论】:

    • 太棒了!您还阐明了必须如何保留层次结构。谢谢!
    • @Sandor Mezil 感谢您的回复。很高兴您的问题得到解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多