使用电子表格发送多个请求。batchUpdate会导致错误

2024-10-03 15:33:25 发布

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

batch_update_spreadsheet_request_body = {
    'requests': [
        {
            "updateCells": {
                "rows": [
                    {
                        "values": [
                            {
                                "userEnteredValue": {
                                    "boolValue": False
                                }
                            }
                        ]
                    }
                ],
                "fields": "userEnteredValue",
                "start": {
                    "sheetId": "target_sheet_id",
                    "rowIndex": 0,
                    "columnIndex": 0
                }
            },
            "updateSheetProperties": {
                    "properties": {
                        "sheetId": "target_sheet_id",
                        "title": "Some title"
                    },
                    "fields": "title"
            }
        }
    ]
}

request = service.spreadsheets().batchUpdate(
    spreadsheetId=spreadsheet_id,
    body=batch_update_spreadsheet_request_body)
request.execute()

此代码导致以下错误:“请求[0]处的值无效(其中一个),已设置字段“种类”中的一个。无法设置“updateSheetProperties”。详细信息:“[{@type':'type.googleapis.com/google.rpc.BadRequest”,“fieldViolations':[{'field':'requests[0]','description':“requests[0]'(其中一个)处的值无效,已经设置了字段'kind'中的一个。无法设置'updateSheetProperties'}]”

但每个请求机构都在单独工作:

batch_update_spreadsheet_request_body = {
    'requests': [
        {
            "updateCells": {
                "rows": [
                    {
                        "values": [
                            {
                                "userEnteredValue": {
                                    "boolValue": False
                                }
                            }
                        ]
                    }
                ],
                "fields": "userEnteredValue",
                "start": {
                    "sheetId": "target_sheet_id",
                    "rowIndex": 0,
                    "columnIndex": 0
                }
            }
        }
    ]
}

以及“updateSheetProperties”请求主体。阅读documentation没有帮助,我也试图在其他地方找到答案,但没有效果


Tags: idtargetfieldstitlerequestbatchupdatebody
1条回答
网友
1楼 · 发布于 2024-10-03 15:33:25

这个答案怎么样

修改点:

  • 我认为您出现问题的原因是UpdateCellsRequestUpdateSheetPropertiesRequest包含在请求数组的一个元素中。在这种情况下,请为请求中的每个元素将它们分开

当这一点反映到脚本中时,它将变成如下所示。在此修改中,batch_update_spreadsheet_request_body被修改

修改的脚本:

batch_update_spreadsheet_request_body = {
    'requests': [
        {
            "updateCells": {
                "rows": [
                    {
                        "values": [
                            {
                                "userEnteredValue": {
                                    "boolValue": False
                                }
                            }
                        ]
                    }
                ],
                "fields": "userEnteredValue",
                "start": {
                    "sheetId": "target_sheet_id",
                    "rowIndex": 0,
                    "columnIndex": 0
                }
            }
        },
        {
            "updateSheetProperties": {
                "properties": {
                    "sheetId": "target_sheet_id",
                    "title": "Some title"
                },
                "fields": "title"
            }
        }
    ]
}

参考文献:

相关问题 更多 >