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(import/spreadsheet): preserve default font
Elian Doran4cdc3cb01501
diff --git a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.tsindex 4caa9a3817..607f997118 100644--- a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts+++ b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts@@ -7,6 +7,7 @@ import {CellValueType,HorizontalAlign,type ICellData,+ type IStyleData,type IWorksheetData,VerticalAlign,WrapStrategy@@ -27,6 +28,12 @@ function cellA1(sheet: IWorksheetData): ICellData | undefined {return sheet.cellData[0]?.[0];}+/** The inline style object on a cell — the importer never emits string style references. */+function styleOf(cell: ICellData | undefined): IStyleData {+ const s = cell?.s;+ return s && typeof s === "object" ? s : {};+}+describe("parseXlsxToWorkbook", () => {it("reads string, number and boolean values with their cell types", async () => {const sheet = await roundTrip((wb) => {@@ -62,6 +69,36 @@ describe("parseXlsxToWorkbook", () => {});});+ it("does not capture the workbook-default font as an explicit font", async () => {+ // A cell with other styling (number format) but no explicit font: exceljs reports the+ // theme-default Calibri 11 — which must NOT become an explicit font, so the cell keeps+ // inheriting Univer's default font.+ const sheet = await roundTrip((wb) => {+ const ws = wb.addWorksheet("S");+ const cell = ws.getCell("A1");+ cell.value = 5;+ cell.numFmt = "0.00";+ });+ const style = styleOf(cellA1(sheet));+ expect(style.ff).toBeUndefined();+ expect(style.fs).toBeUndefined();+ expect(style.cl).toBeUndefined();+ expect(style.n).toMatchObject({ pattern: "0.00" });+ });++ it("keeps an explicit font size/color on a default-family font", async () => {+ // No explicit family, but an explicit size and color — both must survive even though the+ // family is left to the theme default.+ const sheet = await roundTrip((wb) => {+ const ws = wb.addWorksheet("S");+ const cell = ws.getCell("A1");+ cell.value = "x";+ cell.font = { size: 19, color: { argb: "FF036672" } };+ });+ expect(cellA1(sheet)?.s).toMatchObject({ fs: 19, cl: { rgb: "#036672" } });+ expect(styleOf(cellA1(sheet)).ff).toBeUndefined();+ });+it("reads a solid fill into the background color", async () => {const sheet = await roundTrip((wb) => {const ws = wb.addWorksheet("S");diff --git a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.tsindex fd94ddc563..3c26585693 100644--- a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts+++ b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts@@ -39,6 +39,9 @@ import {const DEFAULT_ROW_COUNT = 1000;const DEFAULT_COLUMN_COUNT = 20;+/** Excel's default body font size (points); exceljs reports it on every theme-default font. */+const EXCEL_DEFAULT_FONT_SIZE = 11;+/*** Reads an `.xlsx` binary and produces a UniversJS workbook. Hidden sheets/rows/columns are* preserved and flagged hidden. Styles are emitted inline on each cell (Univer accepts either@@ -225,15 +228,25 @@ function readFont(font: Partial<ExcelJS.Font> | undefined): Partial<IStyleData>const out: Partial<IStyleData> = {};let any = false;- if (font.name) { out.ff = font.name; any = true; }- if (isFiniteNumber(font.size)) { out.fs = font.size; any = true; }+ // exceljs injects the workbook-default font (Calibri 11, theme-1 text color, `scheme` set)+ // onto any cell that has *other* styling but no explicit font. Carrying that back as an+ // explicit font would override Univer's own default font — so skip the theme-derived name,+ // the default size, and the default text color, while keeping bold/italic/underline/strike+ // and any genuinely-explicit size/color (which exceljs reports without `scheme`).+ const isThemeFont = Boolean(font.scheme);++ if (font.name && !isThemeFont) { out.ff = font.name; any = true; }+ if (isFiniteNumber(font.size) && !(isThemeFont && font.size === EXCEL_DEFAULT_FONT_SIZE)) { out.fs = font.size; any = true; }if (font.bold) { out.bl = 1; any = true; }if (font.italic) { out.it = 1; any = true; }if (font.underline) { out.ul = { s: 1 }; any = true; }if (font.strike) { out.st = { s: 1 }; any = true; }- const color = excelColorToRgb(font.color);- if (color) { out.cl = { rgb: color }; any = true; }+ // Skip the default text color (theme 1) so the cell inherits Univer's default text color.+ if (font.color?.theme !== 1) {+ const color = excelColorToRgb(font.color);+ if (color) { out.cl = { rgb: color }; any = true; }+ }return any ? out : null;}