From 674c9e949e8122d85e0e5dff029d72e9c58611a0 Mon Sep 17 00:00:00 2001 From: Benjamin Admin Date: Wed, 15 Apr 2026 09:43:50 +0200 Subject: [PATCH] SpreadsheetView: auto-fit column widths to longest text Column widths now calculated from the longest text in each column (~7.5px per character + padding). Takes the maximum of auto-fit width and scaled original pixel width. Multi-line cells: uses the longest line for width calculation. Spanning header cells excluded from width calculation (they span multiple columns and would inflate single-column widths). Minimum column width: 60px. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../components/ocr-kombi/SpreadsheetView.tsx | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/admin-lehrer/components/ocr-kombi/SpreadsheetView.tsx b/admin-lehrer/components/ocr-kombi/SpreadsheetView.tsx index 430c338..abd791a 100644 --- a/admin-lehrer/components/ocr-kombi/SpreadsheetView.tsx +++ b/admin-lehrer/components/ocr-kombi/SpreadsheetView.tsx @@ -113,12 +113,28 @@ function zoneToSheet(zone: GridZone, sheetIndex: number, isFirst: boolean): any } } - // Column widths — optimized per zone + // Column widths — auto-fit based on longest text in each column const columnlen: Record = {} for (const col of (zone.columns || [])) { + // Find the longest text in this column (considering only non-spanning cells) + const colCells = (zone.cells || []).filter( + (c: any) => c.col_index === col.index && c.col_type !== 'spanning_header' + ) + let maxTextLen = 0 + for (const c of colCells) { + const text = c.text ?? '' + // For multi-line cells, use the longest line + const lines = text.split('\n') + const longestLine = Math.max(0, ...lines.map((l: string) => l.length)) + if (longestLine > maxTextLen) maxTextLen = longestLine + } + // ~7px per character as a rough estimate, minimum 60px + const autoWidth = Math.max(60, maxTextLen * 7.5 + 16) + // Also consider original pixel width (scaled down) const pxW = (col.x_max_px ?? 0) - (col.x_min_px ?? 0) - const scaleFactor = numCols <= 2 ? 0.6 : 0.4 - columnlen[String(col.index)] = Math.max(80, Math.round(pxW * scaleFactor)) + const scaledPxW = Math.max(60, Math.round(pxW * (numCols <= 2 ? 0.6 : 0.4))) + // Use the larger of auto-fit or scaled pixel width + columnlen[String(col.index)] = Math.round(Math.max(autoWidth, scaledPxW)) } // Row heights — taller for multi-line cells