Python/gspread - how can I update multiple cells with DIFFERENT VALUES at once?

0 views
Skip to first unread message

ecollis6 via StackOverflow

unread,
May 21, 2013, 12:47:44 PM5/21/13
to google-appengin...@googlegroups.com

To update a range of cells, you use the following command.

"""Select a range"""

cell_list = worksheet.range('A1:A7')

for cell in cell_list: cell.value = 'O_o'

"""Update in batch"""

worksheet.update_cells(cell_list)

For my application, I would like it to update an entire range, but I am trying to set a different value for each individual cell. The problem with this example is that every cell ends up with the same value. Updating each cell individually is inefficient and takes way too long. How can I do this efficiently?



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/16675258/python-gspread-how-can-i-update-multiple-cells-with-different-values-at-once

ecline6 via StackOverflow

unread,
May 21, 2013, 1:07:43 PM5/21/13
to google-appengin...@googlegroups.com

You can use enumerate on a separate list containing the different values you want in the cells and use the index part of the tuple to match to the appropriate cells in cell_list.

cell_list = worksheet.range('A1:A7')
cell_values = [1,2,3,4,5,6,7]

for i, val in enumerate(cell_values):  #gives us a tuple of an index and value
    cell_list[i].value = val    #use the index on cell_list and the val from cell_values

worksheet.update_cells(cell_list)


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/16675258/python-gspread-how-can-i-update-multiple-cells-with-different-values-at-once/16675639#16675639
Reply all
Reply to author
Forward
0 new messages