Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet: fix cells not rendering for print/PDF/XLS in certain conditions

Adorian Dorand34d7bfec285
diff --git a/packages/commons/src/lib/spreadsheet/extract_text.spec.ts b/packages/commons/src/lib/spreadsheet/extract_text.spec.ts
index 93e1a42ba4..0c1c951d21 100644
--- a/packages/commons/src/lib/spreadsheet/extract_text.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/extract_text.spec.ts
@@ -505,3 +505,30 @@ describe("extractSpreadsheetText", () => {
expect(text).toContain("This text is available only in the spreadsheet");
});
});
+
+describe("extractSpreadsheetText rich-text cells", () => {
+ it("indexes cells whose text lives only in the rich-text document, lowercased keys included", () => {
+ const sheet = (dataStreamKey: string) => JSON.stringify({
+ version: 1,
+ workbook: {
+ sheetOrder: ["s1"],
+ sheets: {
+ s1: {
+ id: "s1",
+ name: "Sheet1",
+ hidden: 0,
+ cellData: {
+ "0": { "0": { p: { body: { [dataStreamKey]: "Senzor PIR\r\n" } } } },
+ "1": { "0": { v: "plain" } }
+ },
+ rowData: {},
+ columnData: {}
+ }
+ }
+ }
+ });
+
+ expect(extractSpreadsheetText(sheet("dataStream"))).toBe("Senzor PIR plain");
+ expect(extractSpreadsheetText(sheet("datastream"))).toBe("Senzor PIR plain");
+ });
+});
diff --git a/packages/commons/src/lib/spreadsheet/extract_text.ts b/packages/commons/src/lib/spreadsheet/extract_text.ts
index ff38abdb2a..10dd2cb117 100644
--- a/packages/commons/src/lib/spreadsheet/extract_text.ts
+++ b/packages/commons/src/lib/spreadsheet/extract_text.ts
@@ -10,6 +10,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
+import { normalizeDataStream } from "./workbook_model.js";
+
/** Read a property by trying the camelCase name first, then the lowercased form. */
function prop(obj: any, camel: string): any {
if (obj == null) return undefined;
@@ -71,9 +73,15 @@ function extractSheetText(sheet: any, parts: string[]): void {
if (prop(columnData[col], "hd")) continue;
const cell = cols[col];
- if (cell?.v == null) continue;
+ if (!cell) continue;
- if (typeof cell.v === "boolean") {
+ if (cell.v == null || cell.v === "") {
+ // A rich-text cell (a link, mixed formatting) can carry its text only in `p`.
+ const text = cellDocumentText(cell).trim();
+ if (text) {
+ parts.push(text);
+ }
+ } else if (typeof cell.v === "boolean") {
parts.push(cell.v ? "TRUE" : "FALSE");
} else {
const text = String(cell.v).trim();
@@ -84,3 +92,9 @@ function extractSheetText(sheet: any, parts: string[]): void {
}
}
}
+
+/** Reads a rich-text cell's document text, tolerating the lowercased keys `prop` handles. */
+function cellDocumentText(cell: any): string {
+ const body = prop(prop(cell, "p"), "body");
+ return normalizeDataStream(prop(body, "dataStream"));
+}
diff --git a/packages/commons/src/lib/spreadsheet/render_to_csv.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_csv.spec.ts
index 107494e20c..e85f7421bf 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_csv.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_csv.spec.ts
@@ -344,3 +344,12 @@ describe("renderSpreadsheetToCsv (formatDate returns null)", () => {
expect(csv).toBe("46118");
});
});
+
+describe("rich-text cells", () => {
+ it("exports the text of a cell that has no plain value", () => {
+ const csv = renderSpreadsheetToCsv(workbook({
+ 0: { 0: { p: { body: { dataStream: "Telecomand\u0103\r\n" } } }, 1: { v: 135.99 } }
+ }));
+ expect(csv).toBe("Telecomand\u0103,135.99");
+ });
+});
diff --git a/packages/commons/src/lib/spreadsheet/render_to_csv.ts b/packages/commons/src/lib/spreadsheet/render_to_csv.ts
index c8ce2e8d88..253e3d6255 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_csv.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_csv.ts
@@ -23,6 +23,7 @@ import { format as formatNumfmt, getFormatDateInfo, isDateFormat } from "numfmt"
import {
computeBounds,
+ getCellDocumentText,
getVisibleSheets,
type ICellData,
isFiniteNumber,
@@ -148,7 +149,10 @@ function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData |
* serials (a numeric value with a date number format) are rendered as ISO 8601.
*/
function cellText(cell: ICellData | undefined, styles: Record<string, IStyleData | null>): string {
- if (!cell || cell.v == null) return "";
+ if (!cell) return "";
+
+ // A rich-text cell (a link, mixed formatting) can carry its text only in `p`.
+ if (cell.v == null || cell.v === "") return getCellDocumentText(cell);
if (typeof cell.v === "boolean") return cell.v ? "TRUE" : "FALSE";
const pattern = resolveCellStyle(cell.s, styles)?.n?.pattern;
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 95bd13d313..eecc2c3309 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -419,6 +419,11 @@ describe("renderSpreadsheetToHtml", () => {
expect(html).toContain("transparent");
});
+ it("renders a cell whose text lives only in its rich-text document", () => {
+ const html = renderSpreadsheetToHtml(singleCellWorkbook({ p: { body: { dataStream: "Senzor PIR\r\n" } } }));
+ expect(html).toContain("Senzor PIR");
+ });
+
// 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 9534f38907..7d0ab54244 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -15,6 +15,7 @@ import { format as formatNumfmt, formatColor as formatNumfmtColor } from "numfmt
import {
BorderStyle,
computeBounds,
+ getCellDocumentText,
getFloatingDrawings,
getVisibleSheets,
HorizontalAlign,
@@ -520,7 +521,10 @@ function sanitizeCssColor(value: string): string {
// #region Value formatting
function formatCellValue(cell: ICellData | undefined, style: IStyleData | null): string {
- if (!cell || cell.v == null) return "";
+ if (!cell) return "";
+
+ // A rich-text cell (a link, mixed formatting) can carry its text only in `p`.
+ if (cell.v == null || cell.v === "") return escapeHtml(getCellDocumentText(cell));
if (typeof cell.v === "boolean") {
return cell.v ? "TRUE" : "FALSE";
diff --git a/packages/commons/src/lib/spreadsheet/render_to_xlsx.spec.ts b/packages/commons/src/lib/spreadsheet/render_to_xlsx.spec.ts
index 427cd198fb..9663604dc8 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_xlsx.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_xlsx.spec.ts
@@ -837,3 +837,10 @@ function workbookWithSheetNames(names: string[]): string {
});
return JSON.stringify({ version: 1, workbook: { sheetOrder, styles: {}, sheets } });
}
+
+describe("rich-text cells", () => {
+ it("writes the text of a cell that has no plain value", async () => {
+ const wb = await roundTrip(singleCellWorkbook({ p: { body: { dataStream: "Kit\r\n" } } }));
+ expect(wb.worksheets[0].getCell("A1").value).toBe("Kit");
+ });
+});
diff --git a/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts b/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
index 510159fac7..02d40dff7d 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
@@ -18,6 +18,7 @@ import "./exceljs_augmentation.js";
import {
BorderStyle,
type DataValidationRule,
+ getCellDocumentText,
getDataValidations,
getFloatingDrawings,
getVisibleSheets,
@@ -393,7 +394,12 @@ function setCellValue(target: ExcelJS.Cell, cell: ICellData): void {
target.value = { formula: cell.f.replace(/^=/, ""), result: cell.v ?? undefined } as ExcelJS.CellFormulaValue;
return;
}
- if (cell.v == null) return;
+ if (cell.v == null || cell.v === "") {
+ // A rich-text cell (a link, mixed formatting) can carry its text only in `p`.
+ const text = getCellDocumentText(cell);
+ if (text) target.value = text;
+ return;
+ }
target.value = cell.v;
}
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts b/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts
index f870b1c085..684f0b67a9 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.spec.ts
@@ -1,9 +1,11 @@
import { describe, expect, it } from "vitest";
import {
+ getCellDocumentText,
getDataValidations,
getFloatingDrawings,
type IWorkbookData,
+ normalizeDataStream,
SHEET_DATA_VALIDATION_RESOURCE,
SHEET_DRAWING_RESOURCE
} from "./workbook_model.js";
@@ -102,3 +104,19 @@ describe("getDataValidations", () => {
)).toEqual([]);
});
});
+
+describe("getCellDocumentText", () => {
+ it("reads the rich-text document, dropping the terminator and structural characters", () => {
+ expect(getCellDocumentText({ p: { body: { dataStream: "Kit\r\n" } } })).toBe("Kit");
+ expect(getCellDocumentText({ p: { body: { dataStream: "first\rsecond\r\n" } } })).toBe("first\nsecond");
+ expect(getCellDocumentText({ p: { body: { dataStream: "logo\b\r\n" } } })).toBe("logo");
+ });
+
+ it("returns an empty string when the cell carries no document text", () => {
+ expect(getCellDocumentText(undefined)).toBe("");
+ expect(getCellDocumentText({ v: "plain" })).toBe("");
+ expect(getCellDocumentText({ p: { drawings: {} } })).toBe("");
+ expect(getCellDocumentText({ p: { body: { dataStream: "\r\n" } } })).toBe("");
+ expect(normalizeDataStream(42)).toBe("");
+ });
+});
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.ts
index 0d20bba643..134157748b 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts
@@ -72,6 +72,13 @@ export interface ICellData {
export interface ICellDocumentData {
drawings?: Record<string, ISheetDrawing>;
drawingsOrder?: string[];
+ /** Text content, present when the cell holds rich text such as a link or mixed formatting. */
+ body?: ICellDocumentBody;
+}
+
+export interface ICellDocumentBody {
+ /** Univer's flat text buffer, with structure encoded as control characters. */
+ dataStream?: string;
}
export interface ISheetDrawing {
@@ -263,6 +270,29 @@ export function resolveCellStyle(
return s;
}
+/**
+ * Returns the plain text of a cell Univer stored as a rich-text document. Univer writes `p`
+ * without a matching `v` for some cells, such as a link typed into an empty cell, so an emitter
+ * that reads only `v` renders them blank.
+ */
+export function getCellDocumentText(cell: ICellData | undefined): string {
+ return normalizeDataStream(cell?.p?.body?.dataStream);
+}
+
+/**
+ * Converts Univer's `dataStream` to plain text. A carriage return ends a paragraph and a line
+ * feed ends a section, so both become newlines and the trailing terminator every stream carries
+ * is dropped. The other C0 control characters are structural placeholders, the backspace
+ * character standing in for an embedded drawing.
+ */
+export function normalizeDataStream(dataStream: unknown): string {
+ if (typeof dataStream !== "string") return "";
+ return dataStream
+ .replace(/[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g, "")
+ .replace(/\r\n|[\r\n]/g, "\n")
+ .replace(/\n+$/, "");
+}
+
export interface Bounds {
minRow: number;
maxRow: number;