试图通过Google Sheets API添加我自己的自定义规则时出现“Invalid ConditionValue”错误

2024-10-01 00:34:54 发布

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

我通过Python脚本自动生成包含大量信息的新Google工作表。通过GoogleSheetsAPI添加信息不是问题,我还可以添加公式、更改格式等

但是,当我尝试实现自己的自定义格式规则时,会出现Invalid ConditionValue错误。当值大于我在另一张表(称为Main)中存储的值时,我希望将字段设置为粗体和斜体

我尝试使用的公式,=GT(A14; 'Main'!B1)在直接输入谷歌表单时效果很好。我尝试了{}、{}和{},但都没有成功

我得到的错误是:

 raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/SheetsID:batchUpdate?alt=json returned "Invalid requests[5].addConditionalFormatRule: Invalid ConditionValue.userEnteredValue: =GT(A14; 'Main'!B1)">

请求中有问题的部分:

"addConditionalFormatRule": {
    "rule": {
        "ranges": [{
            "sheetId": sheetId,
            "startRowIndex": int(rows + 2),
            "endRowIndex": int(rows + 3),
            "startColumnIndex": 0,
            "endColumnIndex": cols
        }],
        "booleanRule": {
            "condition": {
                "type": "CUSTOM_FORMULA",
                "values": [{
                    "userEnteredValue": "=GT(A" + str(int(rows + 3)) + "; 'Main'!B1)"
                }]
            },
            "format": {
                "textFormat": {
                    "bold": True,
                    "italic": True
                }
            }
        }
    },
    "index": 0
}

我发送的整个请求:(注意,在添加上述部分之前,这确实有效

"requests": [{
        "repeatCell": {
            "range": {
                "sheetId": sheetId,
                "startRowIndex": 0,
                "endRowIndex": 1
            },
            "cell": {
                "userEnteredFormat": {
                    "horizontalAlignment": "CENTER",
                    "textFormat": {
                        "fontSize": 8,
                        "bold": False
                    }
                }
            },
            "fields": "userEnteredFormat(textFormat,horizontalAlignment)"
        }
    },
    {
        "repeatCell": {
            "range": {
                "sheetId": sheetId,
                "startRowIndex": int(rows + 2),
                "endRowIndex": int(rows + 3),
                "startColumnIndex": 0,
                "endColumnIndex": cols
            },
            "cell": {
                "userEnteredFormat": {
                    "horizontalAlignment": "RIGHT",
                    "textFormat": {
                        "fontSize": 12,
                        "bold": True
                    }
                }
            },
            "fields": "userEnteredFormat(textFormat,horizontalAlignment)"
        }
    },
    {
        "repeatCell": {
            "range": {
                "sheetId": sheetId,
                "startRowIndex": int(rows + 3),
                "endRowIndex": 1000,
                "startColumnIndex": 0,
                "endColumnIndex": 1000
            },
            "cell": {
                "userEnteredFormat": {
                    "horizontalAlignment": "LEFT",
                }
            },
            "fields": "userEnteredFormat(textFormat,horizontalAlignment)"
        }
    },
    {
        "updateSheetProperties": {
            "properties": {
                "sheetId": sheetId,
                "gridProperties": {
                    "frozenRowCount": 1
                }
            },
            "fields": "gridProperties.frozenRowCount"
        }
    },
    {
        "repeatCell": {
            "range": {
                "sheetId": sheetId,
                "startRowIndex": int(rows + 2),
                "endRowIndex": int(rows + 3),
                "startColumnIndex": 0,
                "endColumnIndex": cols
            },
            "cell": {
                "userEnteredValue": {
                    "formulaValue": "=SUM(A2:A" + str(rows + 2) + ")"
                }
            },
            "fields": "userEnteredValue"
        }
    },
    {
        "addConditionalFormatRule": {
            "rule": {
                "ranges": [{
                    "sheetId": sheetId,
                    "startRowIndex": int(rows + 2),
                    "endRowIndex": int(rows + 3),
                    "startColumnIndex": 0,
                    "endColumnIndex": cols
                }],
                "booleanRule": {
                    "condition": {
                        "type": "CUSTOM_FORMULA",
                        "values": [{
                            "userEnteredValue": "=GT(A" + str(int(rows + 3)) + "; 'Main'!B1)"
                        }]
                    },
                    "format": {
                        "textFormat": {
                            "bold": True,
                            "italic": True
                        }
                    }
                }
            },
            "index": 0
        }
    }
]

更新 结果表明,在引用其他图纸时不支持条件格式。但是,应该可以使用INDIRECT()。我试过以下方法,但还是不走运

"condition": {
    "type": "NUMBER_GREATER_THAN_EQ",
    "values": [
        {
             "userEnteredValue": "=INDIRECT('Main'!B2)"
        }
    ]
}

Tags: gttruefieldsmainrowsinttextformathorizontalalignment