Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet/preview: adjust to bound of the table to match the actual content

Adorian Doran26af591165f3
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
index c4eb302bbe..e2dd21418e 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -659,10 +659,14 @@ describe("renderSpreadsheetToHtml", () => {
it("holds text to its own edge when the neighbour beside it holds a value", () => {
expect(room(row({ 0: { v: "a long label", t: 1 }, 1: { v: "next", t: 1 } }), 0)).toEqual([0, 0]);
- // Nothing in the way, so the stylesheets are left to spill the cell as the editor does.
+ // A cell that carries only formatting does not extend the grid, so there is no column
+ // beside these to run into.
expect(room(row({ 0: { v: "a long label", t: 1 } }), 0)).toEqual([0, 0]);
- expect(room(row({ 0: { v: "a long label", t: 1 }, 1: { s: { bl: 1 } } }), 0)).toEqual([0, 88]);
- expect(room(row({ 0: { v: "a long label", t: 1 }, 1: { v: "", t: 1 } }), 0)).toEqual([0, 88]);
+ expect(room(row({ 0: { v: "a long label", t: 1 }, 1: { s: { bl: 1 } } }), 0)).toEqual([0, 0]);
+ expect(room(row({ 0: { v: "a long label", t: 1 }, 1: { v: "", t: 1 } }), 0)).toEqual([0, 0]);
+
+ // With a column of content beyond it, the empty one between is room to run across.
+ expect(room(row({ 0: { v: "a long label", t: 1 }, 2: { v: "next", t: 1 } }), 0)).toEqual([0, 88]);
});
it("gives the text the width of the empty cells it may run across", () => {
@@ -1932,6 +1936,59 @@ describe("renderSpreadsheetToHtml", () => {
expect(html).not.toContain(`<span class="spreadsheet-floating-image"`);
});
+ it("bounds the grid by the cells that hold something, not the ones that only carry formatting", () => {
+ // A fill applied across whole rows leaves formatting far past the data. Rendering out to it
+ // would cost a cell per column for a band drawn as one rectangle.
+ const html = renderSpreadsheetToHtml(JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ styles: {},
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ mergeData: [],
+ cellData: {
+ "0": { "0": { v: "a", t: 1 }, "1": { v: "b", t: 1 }, "500": { s: { bg: { rgb: "#FFE699" } } } },
+ "1": { "0": { v: "c", t: 1 }, "500": { s: { bg: { rgb: "#FFE699" } } } }
+ },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ }));
+
+ expect((html.match(/<td/g) ?? []).length).toBe(4);
+ expect(html).toContain(`<col span="2" style="width:88px">`);
+ });
+
+ it("falls back to every cell for a sheet that is nothing but formatting", () => {
+ const html = renderSpreadsheetToHtml(JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ styles: {},
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ mergeData: [],
+ cellData: { "0": { "0": { s: { bg: { rgb: "#FFE699" } } }, "1": { s: { bg: { rgb: "#FFE699" } } } } },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ }));
+
+ expect((html.match(/<td/g) ?? []).length).toBe(2);
+ expect(html).toContain("background-color:#FFE699");
+ });
+
it("renders a floating image absolutely positioned in a per-sheet wrapper", () => {
const html = renderSpreadsheetToHtml(
workbookWithFloatingDrawings([
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index b940eb6b1f..109d4eb10f 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -294,9 +294,15 @@ function trackIndexAtPx(targetPx: number, count: number | undefined, size: (inde
function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData | null>, images: PlacedImage[]): string {
const { cellData, mergeData = [], columnData = {}, rowData = {} } = sheet;
- // Determine the actual bounds (only cells with data), then extend them to cover any floating
- // images that reach past the data so the grid encloses them like the editor does.
- const bounds = computeBounds(cellData, mergeData);
+ // A sheet often carries formatting far past its data: a fill applied to whole rows leaves tens
+ // of thousands of empty cells, each of which would become a `td` the browser has to lay out for
+ // a band it draws as one rectangle. The grid is bounded by the cells that hold something, and
+ // falls back to every cell for a sheet that is nothing but formatting. Then it is extended to
+ // cover any floating image that reaches past the data, so the grid encloses it as the editor does.
+ // A merge widens it as before, except one applied to entire rows or columns, which would
+ // pull the sheet back out to the far edge of the grid.
+ const bounds = computeBounds(cellData, boundingMerges(mergeData, sheet), holdsSomething)
+ ?? computeBounds(cellData, mergeData);
const { maxRow, maxCol } = extendBoundsForImages(sheet, bounds?.maxRow ?? -1, bounds?.maxCol ?? -1, images);
if (maxRow < 0 || maxCol < 0) {
return "<p>Empty sheet.</p>";
@@ -733,6 +739,29 @@ function cellPadding(style: IStyleData | null): Required<IPaddingData> {
};
}
+/**
+ * The merges that say something about how far a sheet reaches. One spanning the whole width or
+ * height of the grid was applied to entire rows or columns, which is a formatting gesture rather
+ * than a statement that the sheet runs to the last column Excel has, so it is left out and clamped
+ * into the content instead.
+ */
+function boundingMerges(mergeData: IRange[], sheet: IWorksheetData): IRange[] {
+ const lastColumn = (sheet.columnCount ?? 0) - 1;
+ const lastRow = (sheet.rowCount ?? 0) - 1;
+
+ return mergeData.filter((range) =>
+ !(lastColumn > 0 && range.startColumn === 0 && range.endColumn >= lastColumn)
+ && !(lastRow > 0 && range.startRow === 0 && range.endRow >= lastRow));
+}
+
+/**
+ * Whether a cell holds something of its own, which is what the rendered grid is bounded by. A
+ * formula counts even when its result is blank, since the cell is part of the sheet's data.
+ */
+function holdsSomething(cell: ICellData): boolean {
+ return Boolean(cell.f) || hasContent(cell);
+}
+
/** Whether a cell shows anything. A cell carrying only a style is empty, as it is in the editor. */
function hasContent(cell: ICellData | undefined): boolean {
if (!cell) return false;
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.ts
index 7e09301c5b..2265ee3b23 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts
@@ -474,10 +474,15 @@ 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).
+ * 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). Pass `counts` to
+ * bound the rectangle by a subset of the cells, such as only the ones holding something.
*/
-export function computeBounds(cellData: CellMatrix, mergeData: IRange[] = []): Bounds | null {
+export function computeBounds(
+ cellData: CellMatrix,
+ mergeData: IRange[] = [],
+ counts?: (cell: ICellData) => boolean
+): Bounds | null {
let minRow = Infinity;
let maxRow = -Infinity;
let minCol = Infinity;
@@ -488,6 +493,8 @@ export function computeBounds(cellData: CellMatrix, mergeData: IRange[] = []): B
const cols = cellData[row];
for (const colStr of Object.keys(cols)) {
const col = Number(colStr);
+ if (counts && !counts(cols[col])) continue;
+
if (minRow > row) minRow = row;
if (maxRow < row) maxRow = row;
if (minCol > col) minCol = col;