Skip to main content
Glama

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
NameRequiredDescriptionDefault
maxWidthNoMaximum width in pixels
minWidthNoMinimum width in pixels
paddingRatioNoPadding multiplier for content width
worksheetNameYesName of worksheet to auto-fit

Implementation Reference

  • 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) }; } } },
  • 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 ExcelJS
    async 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 handlers
    const allTools = [ ...excelTools, ...financialTools, ...rentalTools, ...expenseTools, ...reportingTools, ...cashFlowTools, ...taxTools, ...analyticsTools, ...chartTools, ...complianceTools, ...propertyTools, ];

Other Tools

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/jeremycharlesgillespie/excel-mcp'

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