Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets 使用google sheets API一次修改同一行中的多个单元格_Google Sheets_Google Api_Google Sheets Api_Spreadsheet - Fatal编程技术网

Google sheets 使用google sheets API一次修改同一行中的多个单元格

Google sheets 使用google sheets API一次修改同一行中的多个单元格,google-sheets,google-api,google-sheets-api,spreadsheet,Google Sheets,Google Api,Google Sheets Api,Spreadsheet,我正在使用Google Sheets API BatchUpdate()端点修改给定电子表格中特定单元格的背景色 现在的问题是,我发送的请求只更新一个单元格,而我无法找到一个解决方案来一次性更新整行(我的工作表中每行大约有22个单元格) 我使用的请求主体是: request = [{ "updateCells": { "range": { "sheetId": sheet_id,

我正在使用Google Sheets API BatchUpdate()端点修改给定电子表格中特定单元格的背景色

现在的问题是,我发送的请求只更新一个单元格,而我无法找到一个解决方案来一次性更新整行(我的工作表中每行大约有22个单元格)

我使用的请求主体是:

request = [{
"updateCells": {
    "range":  {
        "sheetId":          sheet_id,
        "startRowIndex":    20,
        "endRowIndex":      21,
        "startColumnIndex": 1,
        "endColumnIndex":   8
    },
    "rows":   [
        {
            "values": [{
                "userEnteredFormat": {
                    "backgroundColor": {
                        "red":   1,
                        "green": 1,
                        "blue":  0,
                        "alpha": 1.0,  # this parameter is not working properly in the API
                    }}}
            ]
        }
    ],
    "fields": 'userEnteredFormat.backgroundColor',
}}]
行和列参数中显示的值有点任意,因为对其进行了一些测试

包含请求的Python代码:

response = service.spreadsheets().batchUpdate(spreadsheetId=SPREADSHEET_ID, body=body).execute()
这里的问题是,Google Sheets API限制了用户能够发出的请求量,因此可能需要大约3分钟的过程需要3小时


关于如何更新请求主体有什么想法吗?也许我错过了什么。

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

  • 要修改行中单元格的背景色。
    • 从脚本中,您希望将“B”列中的单元格修改到工作表列的末尾
  • 您希望通过使用googleapis和python来实现这一点
  • 您已经能够使用Sheets API获取和输入Google电子表格的值
在这种情况下,我建议在batchUpdate方法中使用RepeatCellRequest。当您的脚本被修改时,它将变成如下所示

修改脚本:
  • 在这种情况下,当未使用
    endColumnIndex
    时,背景色将从
    startColumnIndex
    的单元格修改到工作表上的列末尾
  • 当使用
    “endColumnIndex”:8时,列“B”到“H”的背景色将被修改
参考:

这正是我想要的。GoogleSheetsAPI不是最好的,请求wiki也相当混乱。谢谢@朱利安:谢谢你的回复。我很高兴你的问题解决了。也谢谢你。
request = [{
    "repeatCell": {
        "range":  {
            "sheetId": sheet_id,
            "startRowIndex": 20,
            "endRowIndex": 21,
            "startColumnIndex": 1,
            # "endColumnIndex":   8
        },
        "cell": {
            "userEnteredFormat": {
                "backgroundColor": {
                    "red":   1,
                    "green": 1,
                    "blue":  0,
                    "alpha": 1.0,
                }}},
        "fields": 'userEnteredFormat.backgroundColor',
    }}]