简体   繁体   中英

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.

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.

The argument of the method is a list of Cell objects and it doesn't matter where this list comes from. You can get it from range method or create the list yourself:

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. 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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