Skip to content

Build your personal knowledge base with Trilium Notes

fix(spreadsheet): bound the CSV export by the cells that hold a value

Elian Doranc00b6b24df40
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 5d452fd116..b509226518 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_csv.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_csv.spec.ts
@@ -79,6 +79,18 @@ describe("renderSpreadsheetToCsv", () => {
expect(csv).toBe("x,y");
});
+ it("ignores cells that carry only formatting when bounding the grid", () => {
+ // An Excel template borders the rows meant to be filled in, leaving stored but empty cells
+ // past the data; a merge past the data does not widen the grid either.
+ const csv = renderSpreadsheetToCsv(workbook({
+ 0: { 0: { v: "a" }, 1: { v: "b" }, 2: { s: "bordered" } },
+ 1: { 0: { v: "c" }, 1: { v: "d" } },
+ 2: { 0: { s: "bordered" }, 1: { s: "bordered" } },
+ 3: { 0: { s: "bordered" }, 1: { s: "bordered" } }
+ }, { mergeData: [{ startRow: 5, endRow: 5, startColumn: 0, endColumn: 4 }] }));
+ expect(csv).toBe("a,b\r\nc,d");
+ });
+
it("renders numbers and booleans as raw values", () => {
const csv = renderSpreadsheetToCsv(workbook({
0: { 0: { v: 42, t: 2 }, 1: { v: true, t: 3 }, 2: { v: false, t: 3 } }
@@ -148,9 +160,9 @@ describe("renderSpreadsheetToCsv", () => {
it("treats empty and null cell values as empty fields", () => {
const csv = renderSpreadsheetToCsv(workbook({
- 0: { 0: { v: "" }, 1: { v: null }, 2: { v: "z" } }
+ 0: { 0: { v: "a" }, 1: { v: "" }, 2: { v: null }, 3: { v: "z" } }
}));
- expect(csv).toBe(",,z");
+ expect(csv).toBe("a,,,z");
});
it("exports the first visible sheet by default and the requested sheet by id", () => {
diff --git a/packages/commons/src/lib/spreadsheet/render_to_csv.ts b/packages/commons/src/lib/spreadsheet/render_to_csv.ts
index 253e3d6255..75efceefce 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_csv.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_csv.ts
@@ -3,10 +3,10 @@
*
* CSV is a flat, single-sheet, value-only format: it carries no styling, merges, or
* formulas. So this emitter throws away everything the HTML/XLSX emitters preserve and
- * keeps only the cell values, laid out as a dense rectangle over the sheet's populated
- * bounds. Formula cells export their cached result (Univer stores it in `v`); most numbers
- * export their raw underlying value rather than the formatted display string, which is what
- * downstream CSV consumers expect.
+ * keeps only the cell values, laid out as a dense rectangle over the cells that hold one —
+ * a cell carrying nothing but formatting does not widen it. Formula cells export their cached
+ * result (Univer stores it in `v`); most numbers export their raw underlying value rather than
+ * the formatted display string, which is what downstream CSV consumers expect.
*
* Dates are the exception: Univer stores them as serial numbers (e.g. `46118`), which no
* CSV consumer recognizes as a date. So date-formatted cells are emitted as ISO 8601
@@ -25,6 +25,7 @@ import {
computeBounds,
getCellDocumentText,
getVisibleSheets,
+ hasContent,
type ICellData,
isFiniteNumber,
type IStyleData,
@@ -124,7 +125,13 @@ function uniqueFileName(sheetName: string, usedNames: Set<string>): string {
function renderSheet(sheet: IWorksheetData, styles: Record<string, IStyleData | null>): string {
const { cellData } = sheet;
- const bounds = computeBounds(cellData, sheet.mergeData);
+
+ // A sheet usually carries formatting past its data: an Excel template borders or fills the rows
+ // someone is meant to fill in, and each of those empty cells is stored. CSV holds no formatting,
+ // so a rectangle bounded by every stored cell would trail hundreds of comma-only records. Only
+ // the cells that emit a field bound it, and a merge does not widen it: the merged value sits in
+ // the cell the rectangle already reaches.
+ const bounds = computeBounds(cellData, [], hasContent);
if (!bounds) {
return "";
}
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 46a6dd36ce..88f516ece9 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.ts
@@ -24,6 +24,7 @@ import {
getCellDocumentText,
getFloatingDrawings,
getVisibleSheets,
+ hasContent,
HorizontalAlign,
type IBorderData,
type IBorderStyleData,
@@ -1156,13 +1157,6 @@ function holdsSomething(cell: ICellData, styles: Record<string, IStyleData | nul
return Boolean(borders && (borders.t?.s || borders.r?.s || borders.b?.s || borders.l?.s));
}
-/** 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;
- if (cell.v != null && cell.v !== "") return true;
- return getCellDocumentText(cell) !== "";
-}
-
/**
* The alignment Univer falls back to for a cell that sets none: numbers to the right, booleans
* centered, everything else left (`_horizontalHandler` in `@univerjs/engine-render`). Returns
diff --git a/packages/commons/src/lib/spreadsheet/workbook_model.ts b/packages/commons/src/lib/spreadsheet/workbook_model.ts
index abdf5fdeb8..761b79bcae 100644
--- a/packages/commons/src/lib/spreadsheet/workbook_model.ts
+++ b/packages/commons/src/lib/spreadsheet/workbook_model.ts
@@ -378,6 +378,13 @@ export function getCellDocumentText(cell: ICellData | undefined): string {
return getCellDocumentSegments(cell).map((segment) => segment.text).join("");
}
+/** Whether a cell shows anything. A cell carrying only a style is empty, as it is in the editor. */
+export function hasContent(cell: ICellData | undefined): boolean {
+ if (!cell) return false;
+ if (cell.v != null && cell.v !== "") return true;
+ return getCellDocumentText(cell) !== "";
+}
+
/**
* Builds the rich-text document Univer stores for a hyperlinked cell: the text terminated by a
* paragraph and a section break, plus one custom range covering it. `rangeId` identifies the link