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

前端 未结 4 2163
广开言路
广开言路 2021-01-01 00:23

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 =          


        
4条回答
  •  一个人的身影
    2021-01-01 01:06

    Assuming a table with a header row, as follows:

    Name  | Weight
    ------+-------
    Apple | 56
    Pear  | 23
    Leaf  | 88
    

    Then, the following should be self explanatory

    cell_list = []
    
    # get the headers from row #1
    headers = worksheet.row_values(1)
    # find the column "Weight", we will remember this column #
    colToUpdate = headers.index('Weight')
    
    # task 1 of 2
    cellLookup = worksheet.find('Leaf')
    # get the cell to be updated
    cellToUpdate = worksheet.cell(cellLookup.row, colToUpdate)
    # update the cell's value
    cellToUpdate.value = 77
    # put it in the queue
    cell_list.append(cellToUpdate)
    
    # task 2 of 2
    cellLookup = worksheet.find('Pear')
    # get the cell to be updated
    cellToUpdate = worksheet.cell(cellLookup.row, colToUpdate)
    # update the cell's value
    cellToUpdate.value = 28
    # put it in the queue
    cell_list.append(cellToUpdate)
    
    # now, do it
    worksheet.update_cells(cell_list)
    

提交回复
热议问题