search_sheet
Search spreadsheet cells for values matching a regex pattern to find specific data across sheets and return matches with cell references.
Instructions
Search all cells in a sheet for values matching a regex pattern.
Returns a list of matches, each with the cell reference and value, e.g. [{"cell": "B3", "value": "hello"}, ...]. Returns an empty list if no matches are found.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file | Yes | Path to the spreadsheet file | |
| pattern | Yes | Regular expression pattern to search for. Matched against the string representation of each cell value. | |
| sheet | No | Sheet name. Defaults to the first sheet if omitted. |
Implementation Reference
- The implementation of the search_sheet tool, which searches for a regex pattern within a spreadsheet and returns matching cell references and values.
def search_sheet( file: Annotated[str, Field(description="Path to the spreadsheet file")], pattern: Annotated[str, Field(description="Regular expression pattern to search for. Matched against the string representation of each cell value.")], sheet: Annotated[str | None, Field(description="Sheet name. Defaults to the first sheet if omitted.")] = None, ) -> list[dict]: """Search all cells in a sheet for values matching a regex pattern. Returns a list of matches, each with the cell reference and value, e.g. [{"cell": "B3", "value": "hello"}, ...]. Returns an empty list if no matches are found. """ wb = load_workbook(file) ws = _resolve_sheet(wb, sheet) regex = re.compile(pattern) results = [] for r in range(1, ws.max_row + 1): for c in range(1, ws.max_column + 1): val = ws.cell_value(r, c) if val is None: continue if regex.search(str(val)): results.append({ "cell": f"{get_column_letter(c)}{r}", "value": val, }) return results