Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gspread findall() only within 1 column

Tags:

python

gspread

I am trying to find the cell locations of specific IDs within the first column of a google spreadsheet using gspread.

Is there a way to search only within the first column, and not the entire spreadsheet?

I have been using: gspread.Worksheet(example).findall(query)

but searching through each cell is time-consuming.

like image 277
klarow Avatar asked Feb 29 '16 22:02

klarow


2 Answers

You can use the Worksheet.range() function to help you do this:

query = "findme"
worksheet = worksheet_object
first_column = worksheet.range("A1:A{}".format(worksheet.row_count)
found_cell_list = [found for found in first_column if found.value == query]

I'm making the assumption that your first column is column "A" but if not just build your own range string with the appropriate column letter or use Worksheet.get_addr_int().

This gives you a list of cells which you can use to do what you need to. Though I'm not sure if the performance will be much better on very large sheets.

like image 112
Keliomer Castillo Avatar answered Nov 14 '22 05:11

Keliomer Castillo


You can use the in_column argument:

import gspread

auth = gspread.oauth()

gsheet = auth.open("Example spreadsheet").get_worksheet(0)
matching_cells = gsheet.findall(query='query', in_column=1)
like image 1
jxck Avatar answered Nov 14 '22 04:11

jxck