Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(spreadsheet): clamp a whole-row merge instead of rendering 16,384 columns
Elian Doran049cf331c60d
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.tsindex 95bd13d313..0cc7e2e70f 100644--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts@@ -1766,4 +1766,80 @@ describe("renderSpreadsheetToHtml", () => {expect(html).toContain('rowspan="4"');expect(html).toContain('colspan="4"');});++ it("keeps a merge across an entire row from stretching the grid to Excel's column limit", () => {+ // Excel writes a full-row merge's fill onto all 16,384 cells it covers, so the sheet+ // carries styled cells far past its real content (3 columns here).+ const banner: Record<string, unknown> = { "0": { v: "Title" } };+ for (let col = 1; col < 16384; col++) {+ banner[String(col)] = { s: { bg: { rgb: "#FFE699" } } };+ }+ const input = JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: {},+ sheets: {+ s1: {+ id: "s1",+ name: "Sheet1",+ hidden: 0,+ rowCount: 1000,+ columnCount: 16384,+ mergeData: [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 16383 }],+ cellData: {+ "0": banner,+ "1": { "0": { v: "a" }, "1": { v: "b" }, "2": { v: "c" } }+ },+ rowData: {},+ columnData: {}+ }+ }+ }+ });++ const html = renderSpreadsheetToHtml(input);++ // Three content columns, not 16,384: the banner cell plus the three data cells.+ expect((html.match(/<col /g) ?? []).length).toBe(3);+ expect((html.match(/<td/g) ?? []).length).toBe(4);+ // The banner still spans the full width of what is rendered.+ expect(html).toContain('colspan="3"');+ expect(html).toContain("Title");+ expect(html).toContain("<td>c</td>");+ });++ it("renders a full-row merge that is a sheet's only content", () => {+ // Nothing outside the merge, so the clamped grid is the origin cell alone.+ const banner: Record<string, unknown> = { "0": { v: "Title" } };+ for (let col = 1; col < 16384; col++) {+ banner[String(col)] = { s: { bg: { rgb: "#FFE699" } } };+ }+ const input = JSON.stringify({+ version: 1,+ workbook: {+ sheetOrder: ["s1"],+ styles: {},+ sheets: {+ s1: {+ id: "s1",+ name: "Sheet1",+ hidden: 0,+ rowCount: 1000,+ columnCount: 16384,+ mergeData: [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 16383 }],+ cellData: { "0": banner },+ rowData: {},+ columnData: {}+ }+ }+ }+ });++ const html = renderSpreadsheetToHtml(input);++ expect((html.match(/<td/g) ?? []).length).toBe(1);+ expect(html).toContain("Title");+ expect(html).not.toContain("Empty sheet");+ });});diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.tsindex 0d20bba643..c956c2aa58 100644--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts@@ -271,8 +271,26 @@ export interface Bounds {}/**- * Computes the inclusive bounding rectangle of all populated cells, extended to cover- * any merged ranges. Returns `null` when there are no cells and no merges (empty sheet).+ * Excel's hard sheet limits. A merge reaching the last row or column is one made across an+ * entire row or column ("Merge & Center" over a whole row), not a merge sized to its content.+ */+const EXCEL_MAX_ROWS = 1048576;+const EXCEL_MAX_COLUMNS = 16384;++/**+ * Computes the inclusive bounding rectangle a renderer has to emit. Returns `null` when+ * there are no cells and no merges (empty sheet).+ *+ * Two kinds of cell are deliberately kept out of the rectangle, because both let a sheet+ * whose real content is small produce a grid of millions of cells:+ *+ * - Populated cells behind a merge. Excel writes the merge's fill onto every cell it covers,+ * so "Merge & Center" across an entire row leaves 16,384 styled cells in `cellData`. Only+ * the merge origin renders, so the rest must not stretch the rectangle.+ * - The far end of a merge made across an entire row or column, which reaches Excel's sheet+ * limit. Such a merge is clamped back to the populated cells rather than stretching the+ * rectangle to 16,384 columns. Its origin still counts, and callers clamp the resulting span+ * to the rectangle, so the merge still reaches the far edge of what gets rendered.*/export function computeBounds(cellData: CellMatrix, mergeData: IRange[] = []): Bounds | null {let minRow = Infinity;@@ -280,11 +298,14 @@ export function computeBounds(cellData: CellMatrix, mergeData: IRange[] = []): Blet minCol = Infinity;let maxCol = -Infinity;+ const covered = mergeCoverage(mergeData);+for (const rowStr of Object.keys(cellData)) {const row = Number(rowStr);const cols = cellData[row];for (const colStr of Object.keys(cols)) {const col = Number(colStr);+ if (covered(row, col)) continue;if (minRow > row) minRow = row;if (maxRow < row) maxRow = row;if (minCol > col) minCol = col;@@ -294,15 +315,51 @@ export function computeBounds(cellData: CellMatrix, mergeData: IRange[] = []): Bfor (const range of mergeData) {if (minRow > range.startRow) minRow = range.startRow;- if (maxRow < range.endRow) maxRow = range.endRow;if (minCol > range.startColumn) minCol = range.startColumn;- if (maxCol < range.endColumn) maxCol = range.endColumn;+ // The origin always renders, so it extends the rectangle even for a whole-row merge.+ if (maxRow < range.startRow) maxRow = range.startRow;+ if (maxCol < range.startColumn) maxCol = range.startColumn;++ if (maxRow < range.endRow && range.endRow < EXCEL_MAX_ROWS - 1) maxRow = range.endRow;+ if (maxCol < range.endColumn && range.endColumn < EXCEL_MAX_COLUMNS - 1) maxCol = range.endColumn;}if (minRow > maxRow) return null;return { minRow, maxRow, minCol, maxCol };}+/**+ * Builds a predicate telling whether a cell is covered by a merge without being its origin.+ * Merges are usually few, so each cell is tested against the ranges directly; the bounding box+ * of all merges short-circuits the common case of a cell nowhere near one.+ */+function mergeCoverage(mergeData: IRange[]): (row: number, col: number) => boolean {+ if (mergeData.length === 0) return () => false;++ let top = Infinity;+ let bottom = -Infinity;+ let left = Infinity;+ let right = -Infinity;+ for (const range of mergeData) {+ if (top > range.startRow) top = range.startRow;+ if (bottom < range.endRow) bottom = range.endRow;+ if (left > range.startColumn) left = range.startColumn;+ if (right < range.endColumn) right = range.endColumn;+ }++ return (row, col) => {+ if (row < top || row > bottom || col < left || col > right) return false;++ for (const range of mergeData) {+ if (row === range.startRow && col === range.startColumn) continue;+ if (row >= range.startRow && row <= range.endRow && col >= range.startColumn && col <= range.endColumn) {+ return true;+ }+ }+ return false;+ };+}+/** Checks that a value is a finite number (guards against stringified payloads from JSON). */export function isFiniteNumber(v: unknown): v is number {return typeof v === "number" && Number.isFinite(v);