如何加快使用pygsheets更新单元格颜色?

huangapple go评论90阅读模式
英文:

How to speed up updating cell colors with pygsheets?

问题

I'm using this code to update the background color of a range of cells in a Google sheet:

from pygsheets import Workbook, Color

gc = pygsheets.authorize(service_file='path/to/credentials.json')
workbook = gc.open('spreadsheet_name')
worksheet = workbook.worksheet_by_title('Sheet1')

cell_range = worksheet.range('E2:J37')
for row in cell_range:
    for cell in row:
        cell.color = (0.8, 0.8, 0.8)

But the program is extremely slow. After it does a chunk of cells, it will hang for several minutes before continuing, and as a result for a range this size it takes like 20 minutes, somewhat undermining the point of automating this. Is there a way to speed this up? From what I can tell there isn't a way to set the formatting for a range of cells directly, necessitating this iterative approach.

英文:

I'm using this code to update the background color of a range of cells in a google sheet:

from pygsheets import Workbook, Color

gc = pygsheets.authorize(service_file='path/to/credentials.json')
workbook = gc.open('spreadsheet_name')
worksheet = workbook.worksheet_by_title('Sheet1')

cell_range = worksheet.range('E2:J37')
for row in cell_range:
    for cell in row:
        cell.color = (0.8, 0.8, 0.8)

But the program is extremely slow. After it does a chunk of cells, it will hang for several minutes before continuing, and as a result for a range this size it takes like 20 minutes, somewhat undermining the point of automating this. Is there a way to speed this up? From what I can tell there isn't a way to set the formatting for a range of cells directly, necessitating this iterative approach.

答案1

得分: 1

我相信你的目标如下。

  • 你想要减少脚本的处理成本。

在你的脚本中,每个单元格都在循环中更改背景颜色。我认为这是你当前问题的原因。而且,在这种情况下,循环的次数使用了API。在你的情况下,我认为可以使用batchUpdate方法。当这在你的脚本中反映出来时,变成了如下形式。

从:

cell_range = worksheet.range('E2:J37')
for row in cell_range:
    for cell in row:
        cell.color = (0.8, 0.8, 0.8)

到:

requests = [
    {
        "repeatCell": {
            "range": worksheet.get_gridrange("E2", "J37"),
            "cell": {
                "userEnteredFormat": {
                    "backgroundColor": {"red": 0.8, "green": 0.8, "blue": 0.8}
                }
            },
            "fields": "userEnteredFormat.backgroundColor",
        }
    }
]
gc.sheet.batch_update(workbook.id, requests)

通过这种修改,单元格“E2:J37”的背景颜色通过一个API调用更改为{"red": 0.8, "green": 0.8, "blue": 0.8}

参考:

英文:

I believe your goal is as follows.

  • You want to reduce the process cost of your script.

In your script, the background color is changed every cell in a loop. I think that this is the reason for your current issue. And, in this case, the APIs of the number of loops are used. In your situation, I thought that the batchUpdate method can be used. When this is reflected in your script, it becomes as follows.

From:

cell_range = worksheet.range('E2:J37')
for row in cell_range:
    for cell in row:
        cell.color = (0.8, 0.8, 0.8)

To:

requests = [
    {
        "repeatCell": {
            "range": worksheet.get_gridrange("E2", "J37"),
            "cell": {
                "userEnteredFormat": {
                    "backgroundColor": {"red": 0.8, "green": 0.8, "blue": 0.8}
                }
            },
            "fields": "userEnteredFormat.backgroundColor",
        }
    }
]
gc.sheet.batch_update(workbook.id, requests)
  • By this modification, the background color of cells "E2:J37" is changed to {"red": 0.8, "green": 0.8, "blue": 0.8} by one API call.

References:

huangapple
  • 本文由 发表于 2023年2月24日 03:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定