使用 googlesheets 有条件地将背景颜色设置为黄色 api

Conditionally setting background color to yellow with googlesheets api

我正在使用 http://pygsheets.readthedocs.io/en/latest/index.html google 工作表 api v4 的包装器。我有兴趣使用 google-sheets-api v4 设置条件格式。我正在尝试使用自定义公式根据行中 "Q" 列的值突出显示该行。如果 q 列包含 'TRASH',我想将该行着色为黄色。

当我查看 https://github.com/nithinmurali/pygsheets/blob/master/pygsheets/client.py 中的 pygheets 库和 sh_batch_update 方法时

此外,我有 https://developers.google.com/sheets/api/samples/conditional-formatting#add_a_conditional_formatting_rule_to_a_set_of_ranges 基于此我有:

import pygsheets

def cfr1(sheetId):

    # Apply to range: A1:R
    # Format cells if...: Custom formula is
    # (formula:) =$Q1="TRASH"

    return {
          "addConditionalFormatRule": {
            "rule": {
              "ranges": [
                {
                  "sheetId": sheetId,
                  "startColumnIndex": 'A',
                  "endColumnIndex": 'R',
                  "startRowIndex": 1,
                  "endRowIndex": 8
                }
              ],
              "booleanRule": {
                "condition": {
                  "type": "CUSTOM_FORMULA",
                  "values": [
                    {
                      "userEnteredValue": '=$Q1="TRASH"'
                    }
                  ]
                },
                "format": {
                    "backgroundColor": {
                          "yellow": 1.0
                          # "green": 0.0,
                          # "blue": 0.0
                        }


                }
              }
            },
            "index": 0
          }
        }

当我 运行:

    gc.sh_batch_update(ssheet.id,cfr1(ws.id))

我得到:

"Invalid JSON payload received. Unknown name "yellow" at 'requests[0].add_conditional_format_rule.rule.boolean_rule.format.background_color': Cannot find field.">

这对原色绿色、红色和蓝色有效。如何格式化为黄色背景。

你不能将黄色作为颜色,它应该是 RGB 格式,参见 this。所以,对于黄色,你需要 red=1, green=1, blue=0