Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet/print & PDF export: fix border for merged cells

Adorian Dorandde528168cb6
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 8d95f3bd11..214a8f9833 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { renderSpreadsheetToHtml } from "./render_to_html.js";
-import { HorizontalAlign } from "./workbook_model.js";
+import { BorderStyle, HorizontalAlign } from "./workbook_model.js";
describe("renderSpreadsheetToHtml", () => {
it("renders a basic spreadsheet with values and styles", () => {
@@ -552,6 +552,84 @@ describe("renderSpreadsheetToHtml", () => {
expect(html).toContain(`Days left</span><img class="spreadsheet-cell-image"`);
});
+ describe("borders of a merged range", () => {
+ const THIN = { s: BorderStyle.THIN };
+
+ /** A sheet of `cellData` (keyed by row, then column) carrying the given merges. */
+ function mergedWorkbook(cellData: Record<number, Record<number, unknown>>, mergeData: unknown[]): string {
+ return JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ styles: {},
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ mergeData,
+ cellData,
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+ }
+
+ /** The border declarations of the first cell in the rendered row. */
+ function bordersOfFirstCell(html: string): string[] {
+ const style = (html.match(/<td[^>]*style="([^"]*)"/) ?? [])[1] ?? "";
+ return style.split(";").filter((part) => part.startsWith("border"));
+ }
+
+ it("takes the right edge from the range's last column and the bottom from its last row", () => {
+ // Excel spreads a range's outline over its member cells, so A1:B1 keeps its right
+ // border on B1 and A1:A2 keeps its bottom on A2.
+ const across = renderSpreadsheetToHtml(mergedWorkbook(
+ { 0: { 0: { v: "x", s: { bd: { l: THIN } } }, 1: { s: { bd: { r: THIN } } } } },
+ [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 1 }]
+ ));
+ expect(bordersOfFirstCell(across)).toEqual(["border-right:1px solid #000", "border-left:1px solid #000"]);
+
+ const down = renderSpreadsheetToHtml(mergedWorkbook(
+ { 0: { 0: { v: "x", s: { bd: { t: THIN } } } }, 1: { 0: { s: { bd: { b: THIN } } } } },
+ [{ startRow: 0, endRow: 1, startColumn: 0, endColumn: 0 }]
+ ));
+ expect(bordersOfFirstCell(down)).toEqual(["border-top:1px solid #000", "border-bottom:1px solid #000"]);
+ });
+
+ it("drops the anchor's own right and bottom when they fall inside the range", () => {
+ // In a 2x2 range the anchor's right and bottom are internal edges the merge hides.
+ const html = renderSpreadsheetToHtml(mergedWorkbook(
+ { 0: { 0: { v: "x", s: { bd: { t: THIN, r: THIN, b: THIN } } }, 1: {} }, 1: { 0: {}, 1: {} } },
+ [{ startRow: 0, endRow: 1, startColumn: 0, endColumn: 1 }]
+ ));
+ expect(bordersOfFirstCell(html)).toEqual(["border-top:1px solid #000"]);
+ });
+
+ it("leaves an unmerged cell's borders alone", () => {
+ const html = renderSpreadsheetToHtml(mergedWorkbook({ 0: { 0: { v: "x", s: { bd: { r: THIN, b: THIN } } } } }, []));
+ expect(bordersOfFirstCell(html)).toEqual(["border-right:1px solid #000", "border-bottom:1px solid #000"]);
+ });
+
+ it("keeps its own edges when the range is one cell wide and tall", () => {
+ const html = renderSpreadsheetToHtml(mergedWorkbook(
+ { 0: { 0: { v: "x", s: { bd: { r: THIN, b: THIN } } } } },
+ [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }]
+ ));
+ expect(bordersOfFirstCell(html)).toEqual(["border-right:1px solid #000", "border-bottom:1px solid #000"]);
+ });
+
+ it("emits no border when neither the anchor nor the edge cells carry one", () => {
+ const html = renderSpreadsheetToHtml(mergedWorkbook(
+ { 0: { 0: { v: "x", s: { bl: 1 } }, 1: {} } },
+ [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 1 }]
+ ));
+ expect(bordersOfFirstCell(html)).toEqual([]);
+ });
+ });
+
// Helper to wrap a single styled cell into a complete workbook payload.
function singleCellWorkbook(cell: unknown, sheetExtra: Record<string, unknown> = {}): string {
return JSON.stringify({
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index c51d82c663..44ef08fa37 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -21,6 +21,8 @@ import {
getFloatingDrawings,
getVisibleSheets,
HorizontalAlign,
+ type CellMatrix,
+ type IBorderData,
type IBorderStyleData,
type ICellData,
isFiniteNumber,
@@ -294,7 +296,7 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
if (mergeInfo === "hidden") continue;
const cell = cellData[row]?.[col];
- const cellStyle = resolveCellStyle(cell?.s, styles);
+ const cellStyle = mergeAwareStyle(resolveCellStyle(cell?.s, styles), mergeInfo, row, col, cellData, styles);
const cssText = buildCssText(cellStyle, cell);
const value = rotate(formatCellValue(cell, cellStyle), cellStyle?.tr)
+ (cell ? renderCellImages(cell) : "");
@@ -348,6 +350,34 @@ function buildTableTag(sheet: IWorksheetData, totalWidth: number): string {
interface MergeOrigin {
rowSpan: number;
colSpan: number;
+ /** Last row and column of the range, clamped to the rendered bounds. */
+ endRow: number;
+ endColumn: number;
+}
+
+/**
+ * Replaces a merged range's border with the one composed from the cells on its edges. Excel keeps
+ * a range's outline on the member cell each edge belongs to: the anchor holds the top and the
+ * left, the cell in the range's last column holds the right, and the one in its last row holds the
+ * bottom. Reading only the anchor drops the two far edges, so the range renders with no line
+ * between it and the range beside it. An unmerged cell is returned untouched.
+ */
+function mergeAwareStyle(
+ style: IStyleData | null,
+ merge: MergeOrigin | undefined,
+ row: number,
+ col: number,
+ cellData: CellMatrix,
+ styles: Record<string, IStyleData | null>
+): IStyleData | null {
+ if (!merge) return style;
+
+ const bd: IBorderData = {
+ ...style?.bd,
+ r: resolveCellStyle(cellData[row]?.[merge.endColumn]?.s, styles)?.bd?.r,
+ b: resolveCellStyle(cellData[merge.endRow]?.[col]?.s, styles)?.bd?.b
+ };
+ return { ...style, bd };
}
type MergeInfo = MergeOrigin | "hidden";
@@ -367,7 +397,9 @@ function buildMergeMap(mergeData: IRange[], minRow: number, maxRow: number, minC
map.set(cellKey(range.startRow, range.startColumn), {
rowSpan: endRow - startRow + 1,
- colSpan: endCol - startCol + 1
+ colSpan: endCol - startCol + 1,
+ endRow,
+ endColumn: endCol
});
for (let r = startRow; r <= endRow; r++) {