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
| Name | Required | Description | Default |
|---|---|---|---|
| maxWidth | No | Maximum width in pixels | |
| minWidth | No | Minimum width in pixels | |
| paddingRatio | No | Padding multiplier for content width |
Implementation Reference
- src/tools/excel-tools.ts:533-561 (registration)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) }; } }
- src/tools/excel-tools.ts:536-543 (schema)Input schema for excel_autofit_all_columns tool defining optional parameters for column width constraintsinputSchema: { 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" } } },
- src/tools/excel-tools.ts:544-560 (handler)Inline handler function executing the tool logic by calling ExcelManager.autoFitAllColumnWidths with user-provided options or defaultshandler: 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) }; }
- src/excel/excel-manager.ts:430-442 (helper)Helper method autoFitAllColumnWidths in ExcelManager that applies column auto-fitting to all worksheets in the workbookasync 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); } }
- src/excel/excel-manager.ts:381-428 (helper)Core helper method autoFitColumnWidths implementing the actual column width calculation based on content length with padding, min/max constraints, used by autoFitAllColumnWidthsasync 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 } }