SpreadsheetView: auto-fit column widths to longest text
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Failing after 22s
CI / test-go-edu-search (push) Failing after 23s
CI / test-python-klausur (push) Failing after 11s
CI / test-python-agent-core (push) Failing after 8s
CI / test-nodejs-website (push) Failing after 24s

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) <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-04-15 09:43:50 +02:00
parent e131aa719e
commit 674c9e949e

View File

@@ -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<string, number> = {}
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