首页 > 解决方案 > 有没有办法更新与我在 gspread api for python 中使用 .find 搜索的单元格相邻的单元格

问题描述

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

我有一些使用 gspread api 的 python 代码。Google 表格有列名称和颜色。我可以使用 .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 文档,但是我没有运气。也许还有另一种方法可以实现我不知道的结果。

标签: pythongspread

解决方案


  • 您想将 的值放入color的下一个单元格中name
    • 关于the cell next to the location of the cell named 'Joe',什么时候Joe是“B2”,你想把 的值color放到“C2”。
  • 您已经能够使用 gspread 更新单元格。

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

修改后的脚本:

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)

参考:

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


推荐阅读