klarow
klarow

Reputation: 81

gspread findall() only within 1 column

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.

Upvotes: 8

Views: 3232

Answers (2)

jxck
jxck

Reputation: 36

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)

Upvotes: 1

Keliomer Castillo
Keliomer Castillo

Reputation: 123

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.

Upvotes: 1

Related Questions