Skip to content

Commit

Permalink
added rows and column filter in find, fixes #359
Browse files Browse the repository at this point in the history
  • Loading branch information
nithinmurali committed Jul 26, 2019
1 parent 27f7052 commit fc6c740
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pygsheets/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,8 @@ def replace(self, pattern, replacement=None, **kwargs):
else:
cell.value = re.sub(pattern, replacement, cell.value)

def find(self, pattern, searchByRegex=False, matchCase=False, matchEntireCell=False, includeFormulas=False):
def find(self, pattern, searchByRegex=False, matchCase=False, matchEntireCell=False, includeFormulas=False,
cols=None, rows=None):
"""Finds all cells matched by the pattern.
Compare each cell within this sheet with pattern and return all matched cells. All cells are compared
Expand All @@ -1049,14 +1050,23 @@ def find(self, pattern, searchByRegex=False, matchCase=False, matchEntireCell=Fa
:param matchCase: Comparison is case sensitive. (default False)
:param matchEntireCell: Only match a cell if the pattern matches the entire value. (default False)
:param includeFormulas: Match cells with formulas. (default False)
:param rows: Range of rows to search in as tuple, example (2, 10)
:param cols: Range of columns to search in as tuple, example (3, 10)
:returns: A list of :class:`Cells <Cell>`.
"""
if self._linked:
self._update_grid(True)

# flatten data grid.
found_cells = [item for sublist in self.data_grid for item in sublist]
# flatten and filter data grid.
cells = self.data_grid
if rows: cells = self.data_grid[rows[0]-1: rows[1]]
found_cells = []
for cells_row in cells:
if cols: cells_row = cells_row[cols[0]-1: cols[1]]
found_cells.extend(cells_row)

print(found_cells)

if not includeFormulas:
found_cells = filter(lambda x: x.formula == '', found_cells)
Expand Down

0 comments on commit fc6c740

Please sign in to comment.