Skip to main content
Glama

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
NameRequiredDescriptionDefault
filePathYesPath to the CSV or Excel file
columnYesColumn name or index (0-based)
conditionYesCondition: equals, contains, greater_than, less_than
valueYesValue to compare against
sheetNoSheet name for Excel files (optional)

Implementation Reference

  • 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), }, ], }; } }
  • 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';

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ishayoyo/excel-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server