Skip to content

Build your personal knowledge base with Trilium Notes

fix(spreadsheet): preserve empty cells background and row height

Elian Doran9bdc10117ccb
diff --git a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts
index 6386e59e8e..4caa9a3817 100644
--- a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.spec.ts
@@ -149,6 +149,26 @@ describe("parseXlsxToWorkbook", () => {
expect(sheet.rowData?.[0]?.hd).toBe(1);
});
+ it("keeps the background of an empty (value-less) styled cell", async () => {
+ const sheet = await roundTrip((wb) => {
+ const ws = wb.addWorksheet("S");
+ ws.getCell("A1").value = "x";
+ // B1 has only a fill, no value — must still survive the round-trip.
+ ws.getCell("B1").fill = { type: "pattern", pattern: "solid", fgColor: { argb: "FFF9F9F9" } };
+ });
+ expect(sheet.cellData[0][1]?.s).toMatchObject({ bg: { rgb: "#F9F9F9" } });
+ });
+
+ it("reads the sheet default row height and column width", async () => {
+ const sheet = await roundTrip((wb) => {
+ const ws = wb.addWorksheet("S", { properties: { defaultRowHeight: 18, defaultColWidth: 11.857 } });
+ ws.getCell("A1").value = "x";
+ });
+ // 18pt -> 18/0.75 = 24px; 11.857 chars -> 11.857*7+5 ≈ 88px.
+ expect(sheet.defaultRowHeight).toBeCloseTo(24, 0);
+ expect(sheet.defaultColumnWidth).toBeCloseTo(88, 0);
+ });
+
it("preserves sheet order across multiple sheets", async () => {
const wb = new ExcelJS.Workbook();
wb.addWorksheet("First").getCell("A1").value = "1";
diff --git a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts
index d90dc102fd..fd94ddc563 100644
--- a/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts
+++ b/packages/commons/src/lib/spreadsheet/parse_from_xlsx.ts
@@ -108,8 +108,11 @@ function readSheet(ws: ExcelJS.Worksheet, id: string): IWorksheetData {
function readCells(ws: ExcelJS.Worksheet): Record<number, Record<number, ICellData>> {
const cellData: Record<number, Record<number, ICellData>> = {};
- ws.eachRow({ includeEmpty: false }, (row, rowNumber) => {
- row.eachCell({ includeEmpty: false }, (cell, colNumber) => {
+ // `includeEmpty: true` so cells carrying only a style (e.g. a header background with no
+ // text) are visited — `readCell` drops the truly-empty ones. With `false`, exceljs skips
+ // any cell/row without a value, silently losing fills on blank cells.
+ ws.eachRow({ includeEmpty: true }, (row, rowNumber) => {
+ row.eachCell({ includeEmpty: true }, (cell, colNumber) => {
const data = readCell(cell);
if (!data) return;
const r = rowNumber - 1;
@@ -296,11 +299,15 @@ function readRows(ws: ExcelJS.Worksheet): { rowData: Record<number, IRowData>; m
const rowData: Record<number, IRowData> = {};
let maxRow = 0;
- ws.eachRow({ includeEmpty: false }, (row, rowNumber) => {
+ // `includeEmpty: true` keeps custom heights on rows that hold only styled (valueless) cells,
+ // such as a thin spacer row, which `false` would skip.
+ ws.eachRow({ includeEmpty: true }, (row, rowNumber) => {
const index = rowNumber - 1;
if (rowNumber > maxRow) maxRow = rowNumber - 1;
const meta: IRowData = {};
+ // exceljs leaves `row.height` undefined unless the row carries a custom height, so this
+ // never picks up the sheet default (which lives on `ws.properties.defaultRowHeight`).
if (isFiniteNumber(row.height)) meta.h = pointsToPx(row.height);
if (row.hidden) meta.hd = 1;
if (meta.h != null || meta.hd != null) rowData[index] = meta;
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 46ed75868e..5ea9f97e28 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_xlsx.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_xlsx.spec.ts
@@ -170,6 +170,15 @@ describe("renderSpreadsheetToXlsx", () => {
expect(on.views[0].showGridLines).toBe(true);
});
+ it("emits sheet default row height and column width", async () => {
+ const ws = (await roundTrip(
+ singleCellWorkbook({ v: "x" }, { defaultRowHeight: 24, defaultColumnWidth: 88 })
+ )).worksheets[0];
+ // 24px -> 18pt; 88px -> (88-5)/7 ≈ 11.86 chars.
+ expect(ws.properties.defaultRowHeight).toBeCloseTo(18, 1);
+ expect(ws.properties.defaultColWidth).toBeCloseTo(11.86, 1);
+ });
+
it("writes a formula with its cached result", async () => {
const ws = (await roundTrip(singleCellWorkbook({ f: "=SUM(B1:B2)", v: 7, t: 2 }))).worksheets[0];
const value = ws.getCell("A1").value as ExcelJS.CellFormulaValue;
diff --git a/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts b/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
index 66bf946c4a..775fc2d667 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_xlsx.ts
@@ -63,7 +63,13 @@ export async function renderSpreadsheetToXlsx(jsonContent: string): Promise<Exce
function writeSheet(out: ExcelJS.Workbook, sheet: IWorksheetData, styles: Record<string, IStyleData | null>): void {
const ws = out.addWorksheet(sheet.name || "Sheet", {
- views: [{ showGridLines: sheet.showGridlines !== 0 }]
+ views: [{ showGridLines: sheet.showGridlines !== 0 }],
+ // Carry Univer's sheet-wide defaults so rows/columns without an explicit size keep it on
+ // round-trip; otherwise Excel falls back to its own defaults (15pt rows / 8.43-char cols).
+ properties: {
+ ...(isFiniteNumber(sheet.defaultRowHeight) ? { defaultRowHeight: pxToPoints(sheet.defaultRowHeight) } : {}),
+ ...(isFiniteNumber(sheet.defaultColumnWidth) ? { defaultColWidth: pxToExcelWidth(sheet.defaultColumnWidth) } : {})
+ }
});
applyColumns(ws, sheet);