简体   繁体   English

gspread 更新不在范围内的多个单元格

[英]gspread update multiple cells not in range

In my application I have a bunch of cells not in range.在我的应用程序中,我有一堆不在范围内的单元格。 Currently I am updating them one by one but it takes a lot of time.目前我正在一一更新它们,但这需要很多时间。 I would like to update them in batch by making just one call.我想通过一个电话来批量更新它们。

I looked at a few other SO threads such as this , but in my case the cells are not in range.我查看了其他一些 SO 线程,例如this ,但在我的情况下,单元格不在范围内。

To simplify here is an example of what I am trying to achieve:为了简化这里是我试图实现的一个例子:

worksheet.update_acell("A1", "test1")
worksheet.update_acell("C5", "test2")

Is it possible to update cells not in range in one call?是否可以在一次调用中更新不在范围内的单元格?

Yes, it's possible.是的,这是可能的。 You can use Worksheet.update_cells method for this.您可以为此使用Worksheet.update_cells方法。

The argument of the method is a list of Cell objects and it doesn't matter where this list comes from.该方法的参数是一个Cell对象列表,这个列表来自哪里并不重要。 You can get it from range method or create the list yourself:您可以从range方法中获取它或自己创建列表:

a1 = worksheet.acell('A1')
c5 = worksheet.acell('C5')
a1.value = 'Hello'
c5.value = 'World'
wk.update_cells([a1, c5])

This updates multiple cells in one call.这会在一次调用中更新多个单元格。

Burnhash is correct in that there is no way to get a Cell without requesting it. Burnhash 是正确的,因为没有请求就无法获得 Cell。 However, I was able to acheive desired behaviour with a dummy class:但是,我能够使用虚拟类实现所需的行为:

    class Cell:
        def __init__(self, c, r, v):
            self.col = c
            self.row = r
            self.value = v

    cell1 = Cell(1, 1, 'value1')  # A1
    cell2 = Cell(2, 1, 'value2')  # B1
    wk.update_cells([cell1, cell2])

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

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