Python/gspread - 如何一次更新具有不同值的多个单元格? [英] Python/gspread - how can I update multiple cells with DIFFERENT VALUES at once?

查看:37
本文介绍了Python/gspread - 如何一次更新具有不同值的多个单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要更新一系列单元格,请使用以下命令.

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?

推荐答案

您可以在单独的列表上使用 enumerate 包含您想要在单元格中的不同值,并使用元组的索引部分来匹配 cell_list 中的相应单元格.

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)

这篇关于Python/gspread - 如何一次更新具有不同值的多个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆