Reputation: 89
I want to check whether if a cell value exists in my sheet. If the cell exists, it will return yes and if it doesn't, it will return no.
cell = sheet.find('test')
if cell.value =='test':
print('Yes')
else:
print('No')
The above code works for the true segment but does not work if false and it returns an error gspread.exceptions.CellNotFound: test
. I tried adding an exception, except gspread.exceptions.CellNotFound:
but it still does not work.
Any clue in whats wrong with this code?
Upvotes: 3
Views: 1588
Reputation: 201388
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
When cell = sheet.find('test')
is used, when test
cannot find in the work sheet, an error of gspread.exceptions.CellNotFound: test
occurs. Although I cannot understand about I tried adding an exception, except gspread.exceptions.CellNotFound: but it still does not work.
, I think that your approach is correct. So can you test the following script?
try:
cell = sheet.find('test')
print('Yes')
except gspread.exceptions.CellNotFound: # or except gspread.CellNotFound:
print('No')
When cells = sheet.findall('test')
is used, when test
cannot find in the work sheet, an empty array is returned. In this pattern, this is used.
cells = sheet.findall('test')
if len(cells) > 0: # or if cells != []:
print('Yes')
else:
print('No')
If I misunderstood your question and this was not the direction you want, I apologize.
Upvotes: 2