Python gspread CellNotFound 异常错误

Python gspread CellNotFound exception error

我想检查我的 sheet 中是否存在单元格值。如果单元格存在,它将 return 是,如果不存在,它将 return 否。

cell = sheet.find('test')
if cell.value =='test':
    print('Yes')
else:
    print('No')

以上代码适用于 true 段,但如果为 false 则不起作用,并且 return 是一个错误 gspread.exceptions.CellNotFound: test。我尝试添加一个例外,except gspread.exceptions.CellNotFound: 但它仍然不起作用。

关于这段代码有什么问题的线索吗?

  • 您想使用文本在 Spreadsheet 中搜索 sheet 上的单元格。
  • 您想通过 python.
  • 使用 gspread 来实现此目的
  • 您已经能够使用 Sheets API.
  • 获取和放置 Spreadsheet 的值

如果我的理解是正确的,这个答案怎么样?请将此视为几个可能的答案之一。

模式一:

使用cell = sheet.find('test')时,当test在作品sheet中找不到时,会出现gspread.exceptions.CellNotFound: test的错误。虽然我无法理解I tried adding an exception, except gspread.exceptions.CellNotFound: but it still does not work.,但我认为你的做法是正确的。那么你可以测试下面的脚本吗?

示例脚本:

try:
    cell = sheet.find('test')
    print('Yes')
except gspread.exceptions.CellNotFound:  # or except gspread.CellNotFound:
    print('No')

模式二:

使用cells = sheet.findall('test')时,当test在工作sheet中找不到时,返回一个空数组。在这个模式中,使用了这个。

示例脚本:

cells = sheet.findall('test')
if len(cells) > 0:  # or if cells != []:
    print('Yes')
else:
    print('No')

注:

  • 如果以上脚本不起作用,你能post你的整个脚本吗?借此,我想确认一下。当然,请删除您的个人信息。

参考文献:

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