Skip to main content
Glama

excel_autofit_all_columns

Automatically adjust column widths across all worksheets to fit content within specified minimum and maximum pixel limits, improving spreadsheet readability.

Instructions

Auto-fit column widths for all worksheets in the workbook

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
maxWidthNoMaximum width in pixels
minWidthNoMinimum width in pixels
paddingRatioNoPadding multiplier for content width

Implementation Reference

  • Registration of the 'excel_autofit_all_columns' tool including name, description, input schema, and handler function that wraps ExcelManager.autoFitAllColumnWidths call
    { name: "excel_autofit_all_columns", description: "Auto-fit column widths for all worksheets in the workbook", inputSchema: { type: "object", properties: { 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" } } }, handler: async (args: any): Promise<ToolResult> => { try { await excelManager.autoFitAllColumnWidths({ minWidth: args.minWidth || 30, maxWidth: args.maxWidth || 300, paddingRatio: args.paddingRatio || 1.2 }); return { success: true, message: "Auto-fitted columns in all worksheets" }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } }
  • Input schema for excel_autofit_all_columns tool defining optional parameters for column width constraints
    inputSchema: { type: "object", properties: { 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" } } },
  • Inline handler function executing the tool logic by calling ExcelManager.autoFitAllColumnWidths with user-provided options or defaults
    handler: async (args: any): Promise<ToolResult> => { try { await excelManager.autoFitAllColumnWidths({ minWidth: args.minWidth || 30, maxWidth: args.maxWidth || 300, paddingRatio: args.paddingRatio || 1.2 }); return { success: true, message: "Auto-fitted columns in all worksheets" }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; }
  • Helper method autoFitAllColumnWidths in ExcelManager that applies column auto-fitting to all worksheets in the workbook
    async autoFitAllColumnWidths(options?: { minWidth?: number; maxWidth?: number; paddingRatio?: number; }): Promise<void> { if (!this.workbook) { throw new Error('No workbook is currently open'); } for (const worksheet of this.workbook.worksheets) { await this.autoFitColumnWidths(worksheet.name, options); } }
  • Core helper method autoFitColumnWidths implementing the actual column width calculation based on content length with padding, min/max constraints, used by autoFitAllColumnWidths
    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 } }

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