【问题标题】:Is there a way to update the cell adjacent to the cell I searched for using .find in gspread api for python有没有办法更新与我在 gspread api for python 中使用 .find 搜索的单元格相邻的单元格
【发布时间】:2023-03-30 16:23:01
【问题描述】:

我正在尝试使用 gspread 中的 .find 功能来定位一个单元格并更新它旁边的单元格。

我有一些使用 gspread api 的 python 代码。谷歌表格有列名称和颜色。我可以使用 .find 功能来搜索名称为“Joe”的单元格,但是我需要使用另一个名为“color”的变量来更新 Colors 列中“Joe”单元格旁边的单元格。

#Define the variables
name = 'Joe'
color = 'Blue'

# Search the google sheet to find the location of the cell named 'Joe'
sheet.find(name)
# Update the cell next to the location of the cell named 'Joe' with the color variable.
sheet.update(name, color)

我意识到 sheet.update(name,color) 不正确,但不确定如何表达我想要做的事情。我已经检查了 gspread 文档,但是我没有运气。也许还有另一种方法可以实现我不知道的结果。

【问题讨论】:

    标签: python gspread


    【解决方案1】:
    • 您想将值color 放入name 的下一个单元格。
      • 关于the cell next to the location of the cell named 'Joe',当Joe为“B2”时,要将color的值置为“C2”。
    • 您已经能够使用 gspread 更新单元格。

    如果我的理解是正确的,那么这个修改呢?在此修改中,从sheet.find(name) 的返回值中检索坐标。然后,使用update_cell() 放置color

    修改脚本:

    name = 'Joe'
    color = 'Blue'
    
    cell = sheet.find(name)  # Modified
    sheet.update_cell(cell.row, cell.col + 1, color)  # Modified
    

    注意:

    • Joe 为“B2”时,如果要将color 的值放入“B1”,请使用以下脚本。
      • sheet.update_cell(cell.row - 1, cell.col, color)
    • Joe 为“B2”时,如果要将color 的值放入“B3”,请使用以下脚本。
      • sheet.update_cell(cell.row + 1, cell.col, color)
    • Joe 为“B2”时,如果要将color 的值放入“A2”,请使用以下脚本。
      • sheet.update_cell(cell.row, cell.col - 1, color)

    参考资料:

    如果我误解了您的问题并且这不是您想要的结果,我深表歉意。

    【讨论】:

    • 谢谢@Tanaike,当我回到我的电脑并更新你时,我会试试这个。非常感谢你,是的,你正确地理解了我。
    • 我只是想说谢谢你的回答,它完美地解决了我的问题。我试图将 .find 的结果分解为一个字符串,并将最后一位数字加一。你的方式更干净。谢谢。
    • @Steve 感谢您的回复。我很高兴你的问题得到了解决。也谢谢你。
    最近更新 更多