Skip to content

Commit

Permalink
Changed find in spreadsheet to simply use find on each worksheet.
Browse files Browse the repository at this point in the history
Worksheet will update values if it is linked.
Added search compare for a string when full_match is false.
  • Loading branch information
Kordishal committed Mar 17, 2018
1 parent 956a1c9 commit 57f71a1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 60 deletions.
63 changes: 16 additions & 47 deletions pygsheets/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,58 +228,27 @@ def del_worksheet(self, worksheet):
self.client.sh_batch_update(self.id, request, '', False)
self._sheet_list.remove(worksheet)

def findReplace(self, pattern, replacement=None, regex=True, match_case=False, include_formulas=False, sheet=None):
"""
:param pattern:
:param replacement:
:param regex:
:param match_case:
:param include_formulas:
:param sheet:
:return:
"""
warnings.warn('Not Yet Implemented.')
def find(self, pattern, replacement=None, **kwargs):
"""Searches through all worksheets.
Search all worksheets with the options given. If an option is not given, the default will be used.
Will return a list of cells for each worksheet packed into a list.
def find(self, pattern, replace=None, regex=True, match_case=False, include_formulas=False,
srange=None, sheet=True):
"""Finds and replaces data in cells over a range, sheet, or all sheets.
This does the same if as Worksheet.find if only one worksheet is present.
Creates and executes a findReplaceRequest.
:param pattern: The value to search.
:param replacement: The value to use as the replacement (default None -> no replacement)
:keyword regex: Interpret pattern as regex. (default False)
:keyword include_formulas: Include cells with formulas in search. (default False)
:keyword full_match: Match entire cell content. (default True)
:keyword match_case: Make search case sensitive (default False)
Reference: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#findreplacerequest
:param pattern: The value to search.
:param replace: The value to use as the replacement.
:param regex: Search string is a Regex pattern.
:param match_case: Make search case sensitive.
:param include_formulas: Search cells with formula.
:param srange: Range to search (as label A1:C15)
:param sheet: Search all sheets if True or search a specific sheet by sheet ID.
:returns A list of lists of :class:cells
"""
if not replace:
found_list = []
for wks in self.worksheets():
found_list.extend(wks.find(pattern))
return found_list
body = {
"find": pattern,
"replacement": replace,
"matchCase": match_case,
"matchEntireCell": False,
"searchByRegex": regex,
"includeFormulas": include_formulas,
}
if srange:
body['range'] = srange
elif type(sheet) == bool:
body['allSheets'] = True
elif type(sheet) == int:
body['sheetId'] = sheet
body = {'findReplace': body}
response = self.client.sh_batch_update(self.id, request=body, batch=self.batch_mode)
return response['replies'][0]['findReplace']
found_cells = []
for sheet in self.worksheets():
found_cells.extend(sheet.find(pattern, replacement=replacement, **kwargs))
return found_cells

# @TODO impliment expiration time
def share(self, addr, role='reader', expirationTime=None, is_group=False):
Expand Down
30 changes: 17 additions & 13 deletions pygsheets/worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,27 +862,25 @@ def findReplace(self, pattern, replacement=None, **kwargs):
response = self.client.sh_batch_update(self.id, request=body)
return response['replies'][0]['findReplace']


# TODO: Implement a find method for a range.
def find(self, pattern, regex=True, match_case=False, full_match=True, include_formulas=False,
force_fetch=True):
def find(self, pattern, regex=True, match_case=False, full_match=True, include_formulas=False):
"""Finds all cells matched by the pattern.
Compare each cell within this sheet with pattern and return all matched cells.
Compare each cell within this sheet with pattern and return all matched cells. All cells are compared
as strings.
:param pattern: A string pattern.
:param regex: Compile pattern as regex.
:param match_case: Match case of text.
:param full_match: Only match a cell if the pattern matches the entire value.
:param include_formulas: Match cells with formulas.
:param force_fetch: Update worksheet from remote, even if unmodified.
:param regex: Compile pattern as regex. (default True)
:param match_case: Match case of text. (default False)
:param full_match: Only match a cell if the pattern matches the entire value. (default True)
:param include_formulas: Match cells with formulas. (default False)
:returns A list of :class:`Cells <Cell>`.
"""
self._update_grid(force_fetch)
if self._linked:
self._update_grid(True)

# flatten data grid.
found_cells = [item for sublist in self.data_grid for item in sublist ]
found_cells = [item for sublist in self.data_grid for item in sublist]
if not include_formulas:
found_cells = filter(lambda x: not x.startswith('='), found_cells)
if not match_case:
Expand All @@ -902,7 +900,13 @@ def regex_search(x): return pattern.search(x.value)
matcher = regex_search
else:
def compare(x): return x.value == pattern
matcher = compare

def search(x): return True if x.value.find(pattern) else False

if full_match:
matcher = compare
else:
matcher = search
return filter(matcher, found_cells)

# @TODO optimize with unlink
Expand Down

0 comments on commit 57f71a1

Please sign in to comment.