excel_find_replace
Locate and substitute specific text within Excel worksheets to update financial data, correct errors, or modify content efficiently.
Instructions
Find and replace text in a worksheet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| matchCase | No | ||
| matchEntireCell | No | ||
| replaceText | Yes | ||
| searchText | Yes | ||
| worksheetName | Yes |
Implementation Reference
- src/tools/excel-tools.ts:267-289 (handler)MCP tool handler for 'excel_find_replace' that calls ExcelManager.findAndReplace and returns resulthandler: async (args: any): Promise<ToolResult> => { try { const count = await excelManager.findAndReplace( args.worksheetName, args.searchText, args.replaceText, { matchCase: args.matchCase, matchEntireCell: args.matchEntireCell } ); return { success: true, data: { replacements: count }, message: `Replaced ${count} instances` }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } }
- src/tools/excel-tools.ts:256-266 (schema)Input schema for the excel_find_replace tool defining required and optional parametersinputSchema: { type: "object", properties: { worksheetName: { type: "string" }, searchText: { type: "string" }, replaceText: { type: "string" }, matchCase: { type: "boolean", default: false }, matchEntireCell: { type: "boolean", default: false } }, required: ["worksheetName", "searchText", "replaceText"] },
- src/index.ts:32-44 (registration)Registration of all tools including excelTools (which contains excel_find_replace) into the MCP server's allTools array used for listTools and callToolconst allTools = [ ...excelTools, ...financialTools, ...rentalTools, ...expenseTools, ...reportingTools, ...cashFlowTools, ...taxTools, ...analyticsTools, ...chartTools, ...complianceTools, ...propertyTools, ];
- src/excel/excel-manager.ts:254-308 (helper)Core helper method implementing the find and replace functionality using ExcelJS by iterating over worksheet cells and performing regex-based replacementsasync findAndReplace( worksheetName: string, searchText: string | RegExp, replaceText: string, options?: { matchCase?: boolean; matchEntireCell?: boolean } ): Promise<number> { if (!this.workbook) { throw new Error('No workbook is currently open'); } const worksheet = this.workbook.getWorksheet(worksheetName); if (!worksheet) { throw new Error(`Worksheet "${worksheetName}" not found`); } let replacementCount = 0; worksheet.eachRow((row) => { row.eachCell((cell) => { if (cell.value && typeof cell.value === 'string') { let matches = false; if (searchText instanceof RegExp) { matches = searchText.test(cell.value); if (matches) { cell.value = cell.value.replace(searchText, replaceText); replacementCount++; } } else { if (options?.matchEntireCell) { matches = options?.matchCase ? cell.value === searchText : cell.value.toLowerCase() === searchText.toLowerCase(); if (matches) { cell.value = replaceText; replacementCount++; } } else { const regex = new RegExp( searchText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), options?.matchCase ? 'g' : 'gi' ); const newValue = cell.value.replace(regex, replaceText); if (newValue !== cell.value) { cell.value = newValue; replacementCount++; } } } } }); }); return replacementCount; }