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

2024-10-01 04:46:11 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here

我正在使用http://pygsheets.readthedocs.io/en/latest/index.html一个围绕googlesheetsapiv4的包装器。我对使用googlesheetsapiv4设置条件格式感兴趣。我尝试使用一个自定义公式根据行中“Q”列的值高亮显示行。如果q列包含“垃圾”,我想把这行涂成黄色。在

当我浏览https://github.com/nithinmurali/pygsheets/blob/master/pygsheets/client.py中的pygheets库和sh_批处理更新方法时

此外,我有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
          }
        }

当我跑步时:

^{pr2}$

我得到了:

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

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


Tags: tohttpscomaddformatindexruleconditional
1条回答
网友
1楼 · 发布于 2024-10-01 04:46:11

不能用黄色作为颜色,它应该是RGB格式,请参见this。所以,对于黄色,你需要红色=1,绿色=1,蓝色=0

相关问题 更多 >