gpt4 book ai didi

python - 有没有办法更新与我在 python 的 gspread api 中使用 .find 搜索的单元格相邻的单元格

转载 作者:行者123 更新时间:2023-12-01 07:37:40 25 4
gpt4 key购买 nike

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

我有一些使用 gspread api 的 python 代码。 Google 表格具有“名称”和“颜色”列。我可以使用 .find 功能来搜索包含名称“Joe”的单元格,但是我需要使用另一个名为“color”的变量来更新“颜色”列中单元格“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 文档,但是我没有运气。也许还有另一种我不知道的方法可以实现这个结果。

最佳答案

  • 您想要将 color 值放入 name 的下一个单元格。
    • 关于名为“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)

引用文献:

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

关于python - 有没有办法更新与我在 python 的 gspread api 中使用 .find 搜索的单元格相邻的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56908396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com