简体   繁体   English

Gspread - 如何在不超过配额限制的情况下一次格式化多个单元格?

[英]Gspread - How can I format many cells at once without exceeding my quota limit?

Essentially, I am attempting to print an image onto a worksheet by changing the color of every cell.本质上,我试图通过更改每个单元格的颜色将图像打印到工作表上。 When it comes to small images, it can do it just fine.当涉及到小图像时,它可以做得很好。 However most images I will be importing are more than 100x100.但是,我要导入的大多数图像都超过 100x100。 Not only is this incredibly slow, but I cant do it without exceeding my quota limit.这不仅速度慢得令人难以置信,而且如果不超过我的配额限制,我就无法做到这一点。

I don't think using format() 1000+ times is very optimal, is there a way to set up my formats and then "push" it onto a worksheet without calling the API too many times?我不认为使用format() 1000+ 次是非常理想的,有没有办法设置我的格式,然后将其“推送”到工作表上而不调用 API 太多次?

for row in rows:
    for column in columns:
        color = (image.get_at((column, row)))
        address = utils.rowcol_to_a1(row+1, column+1)
        #the part where it formats the cell
        sheet2.format(str(address), {"backgroundColor": {'red': color[0]/255, 'green': color[1]/255, 'blue': color[2]/255}})

image.get_at() is from another module, it just gets the color of a pixel. image.get_at()来自另一个模块,它只是获取像素的颜色。

sheet2.format() is called for every single pixel in the image. sheet2.format()为图像中的每个像素调用。

I understand I can use time.sleep() to wait for the quota to reset, but at that rate it will take forever to import any decent sized image.我知道我可以使用time.sleep()来等待配额重置,但按照这个速度,导入任何大小合适的图像都需要很长时间。

I believe your goal is as follows.我相信你的目标如下。

  • You want to reduce the process cost of your script.您想降低脚本的处理成本。
  • You want to achieve this using gspread for python.您想使用 gspread for python 来实现这一点。
    • From your showing script, I thought that you might use gspread.从您的演示脚本中,我认为您可能会使用 gspread。

In your situation, how about using batch_update ?在您的情况下,使用batch_update怎么样? When batch_update is used, the request can be achieved by one API call.当使用batch_update时,可以通过一次 API 调用来实现请求。 When your script is modified, how about the following modification?当你的脚本被修改时,下面的修改呢?

Modified script:修改后的脚本:

image = ### # Please declare image.

spreadsheetId = "###" # Please set your Spreadsheet ID.
spreadsheet = client.open_by_key(spreadsheetId)
sheet2 = spreadsheet.worksheet("Sheet1") # Please set your sheet name.
rows = [0, 1, 2, 3,,,] # Please set values of rows.
columns = [0, 1, 2, 3,,,] # Please set values of columns.


rowValues = []
for row in rows:
    colValues = []
    for column in columns:
        color = (image.get_at((column, row))) # I used your script here.
        colValues.append({"userEnteredFormat": {"backgroundColorStyle": {"rgbColor": {"red": color[0] / 255,"green": color[1] / 255,"blue": color[2] / 255}}}})
    rowValues.append({"values": colValues})

requests = {"requests": [{"updateCells": {"rows": rowValues,"range": {"sheetId": sheet2.id,"startRowIndex": rows[0],"endRowIndex": rows[-1] + 1,"startColumnIndex": columns[0],"endColumnIndex": columns[-1] + 1},"fields": "userEnteredFormat.backgroundColorStyle"}}]}
spreadsheet.batch_update(requests)

  • When this script is run, one request of UpdateCellsRequest is created, and request it using batchUpdate method of Sheets API.运行此脚本时,会创建一个 UpdateCellsRequest 请求,并使用 Sheets API 的 batchUpdate 方法请求它。 I thought that by this, your issue might be able to be resolved.我认为通过这个,您的问题可能会得到解决。

Note:笔记:

  • In this sample script, it supposes that the values of rows and columns are an array like [0, 1, 2, 3,,,] created by the continuous numbers.在此示例脚本中,它假设rowscolumns的值是由连续数字创建的类似[0, 1, 2, 3,,,]的数组。

  • Unfortunately, I cannot know your actual situation.不幸的是,我无法知道您的实际情况。 So, I cannot know the values of rows and columns .所以,我不知道rowscolumns的值。 So, when the above script didn't work, can you provide more information?那么,当上述脚本不起作用时,您能否提供更多信息? For example, can you provide the sample values?例如,您能提供样本值吗? By this, I would like to modify the script.通过这个,我想修改脚本。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM