filter_rows
Filter rows in Excel or CSV files by applying conditions to column values, enabling targeted data extraction and analysis.
Instructions
Filter rows based on column values
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Path to the CSV or Excel file | |
| column | Yes | Column name or index (0-based) | |
| condition | Yes | Condition: equals, contains, greater_than, less_than | |
| value | Yes | Value to compare against | |
| sheet | No | Sheet name for Excel files (optional) |
Implementation Reference
- src/handlers/data-operations.ts:320-410 (handler)Implements the core logic for filtering rows in a spreadsheet file based on a column, condition, and value. Supports conditions: equals, contains, greater_than, less_than.async filterRows(args: ToolArgs): Promise<ToolResponse> { try { const { filePath, column, condition, value, sheet } = args; const data = await readFileContent(filePath, sheet); if (data.length === 0) { return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: 'File is empty', }, null, 2), }, ], }; } const colIndex = isNaN(Number(column)) ? data[0].indexOf(column) : Number(column); if (colIndex === -1 || colIndex >= (data[0]?.length || 0)) { return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: `Column "${column}" not found`, }, null, 2), }, ], }; } const headers = data[0]; const filteredRows = []; for (let i = 1; i < data.length; i++) { const cellValue = String(data[i][colIndex]); let matches = false; switch (condition) { case 'equals': matches = cellValue === value; break; case 'contains': matches = cellValue.toLowerCase().includes(value.toLowerCase()); break; case 'greater_than': matches = Number(cellValue) > Number(value); break; case 'less_than': matches = Number(cellValue) < Number(value); break; } if (matches) { filteredRows.push(data[i]); } } return { content: [ { type: 'text', text: JSON.stringify({ success: true, totalRows: data.length - 1, filteredRows: filteredRows.length, filteredData: filteredRows, }, null, 2), }, ], }; } catch (error) { return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: error instanceof Error ? error.message : 'Unknown error occurred', }, null, 2), }, ], }; } }
- src/index.ts:189-219 (schema)Defines the input schema and metadata for the filter_rows tool in the tools list response.{ name: 'filter_rows', description: 'Filter rows based on column values', inputSchema: { type: 'object', properties: { filePath: { type: 'string', description: 'Path to the CSV or Excel file', }, column: { type: 'string', description: 'Column name or index (0-based)', }, condition: { type: 'string', description: 'Condition: equals, contains, greater_than, less_than', enum: ['equals', 'contains', 'greater_than', 'less_than'], }, value: { type: 'string', description: 'Value to compare against', }, sheet: { type: 'string', description: 'Sheet name for Excel files (optional)', }, }, required: ['filePath', 'column', 'condition', 'value'], }, },
- src/index.ts:1209-1210 (registration)Registers the tool call dispatch to the DataOperationsHandler.filterRows method.case 'filter_rows': return await this.dataOpsHandler.filterRows(toolArgs);
- src/index.ts:13-13 (registration)Imports the DataOperationsHandler class containing the filterRows method.import { DataOperationsHandler } from './handlers/data-operations';