excel_autofit_columns
Automatically adjust Excel column widths to fit content while maintaining specified minimum and maximum constraints for optimal spreadsheet readability.
Instructions
Auto-fit column widths based on content with min/max constraints
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| maxWidth | No | Maximum width in pixels | |
| minWidth | No | Minimum width in pixels | |
| paddingRatio | No | Padding multiplier for content width | |
| worksheetName | Yes | Name of worksheet to auto-fit |
Implementation Reference
- src/tools/excel-tools.ts:500-531 (registration)Tool registration object defining the 'excel_autofit_columns' tool, including input schema and thin wrapper handler that delegates to ExcelManager.autoFitColumnWidths{ name: "excel_autofit_columns", description: "Auto-fit column widths based on content with min/max constraints", inputSchema: { type: "object", properties: { worksheetName: { type: "string", description: "Name of worksheet to auto-fit" }, minWidth: { type: "number", default: 30, description: "Minimum width in pixels" }, maxWidth: { type: "number", default: 300, description: "Maximum width in pixels" }, paddingRatio: { type: "number", default: 1.2, description: "Padding multiplier for content width" } }, required: ["worksheetName"] }, handler: async (args: any): Promise<ToolResult> => { try { await excelManager.autoFitColumnWidths(args.worksheetName, { minWidth: args.minWidth || 30, maxWidth: args.maxWidth || 300, paddingRatio: args.paddingRatio || 1.2 }); return { success: true, message: `Auto-fitted columns in ${args.worksheetName}` }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } },
- src/excel/excel-manager.ts:381-428 (handler)Core handler implementation in ExcelManager.autoFitColumnWidths that scans all cells in the worksheet, estimates optimal column widths based on content length with padding, applies min/max constraints, and sets column widths using ExcelJSasync autoFitColumnWidths(worksheetName: string, options?: { minWidth?: number; maxWidth?: number; paddingRatio?: number; }): Promise<void> { 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`); } const minWidth = options?.minWidth || 30; const maxWidth = options?.maxWidth || 300; const paddingRatio = options?.paddingRatio || 1.2; // Calculate column widths based on content const columnWidths: { [col: number]: number } = {}; worksheet.eachRow((row) => { row.eachCell((cell, colNumber) => { let cellText = ''; if (cell.value !== null && cell.value !== undefined) { if (typeof cell.value === 'object' && 'text' in cell.value) { cellText = String(cell.value.text); } else { cellText = String(cell.value); } } // Estimate character width (approximate) const estimatedWidth = cellText.length * 7 * paddingRatio; // ~7 pixels per character if (!columnWidths[colNumber] || estimatedWidth > columnWidths[colNumber]) { columnWidths[colNumber] = Math.min(Math.max(estimatedWidth, minWidth), maxWidth); } }); }); // Apply calculated widths for (const [colNumber, width] of Object.entries(columnWidths)) { const column = worksheet.getColumn(parseInt(colNumber)); column.width = width / 7; // ExcelJS uses character units, not pixels } }
- src/index.ts:32-44 (registration)Main MCP server tool registration where excelTools (containing excel_autofit_columns) is spread into the complete allTools list used for ListTools and CallTool handlersconst allTools = [ ...excelTools, ...financialTools, ...rentalTools, ...expenseTools, ...reportingTools, ...cashFlowTools, ...taxTools, ...analyticsTools, ...chartTools, ...complianceTools, ...propertyTools, ];