Skip to content

Build your personal knowledge base with Trilium Notes

client/spreadsheet/print & PDF export: align values in cells by default according to their type (number, boolean)

Adorian Doranb31ff110e654
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 ab0c5cb1f9..5027e3bbe2 100644
--- a/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
+++ b/packages/commons/src/lib/spreadsheet/render_to_html.spec.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import { renderSpreadsheetToHtml } from "./render_to_html.js";
+import { HorizontalAlign } from "./workbook_model.js";
describe("renderSpreadsheetToHtml", () => {
it("renders a basic spreadsheet with values and styles", () => {
@@ -1044,7 +1045,22 @@ describe("renderSpreadsheetToHtml", () => {
it("renders numeric cell values", () => {
const html = renderSpreadsheetToHtml(singleCellWorkbook({ v: 42, t: 2 }));
- expect(html).toContain("<td>42</td>");
+ expect(html).toContain(">42</td>");
+ });
+
+ it("aligns a cell by its value type when it sets no alignment of its own", () => {
+ // Univer right-aligns numbers and centers booleans; text keeps the table default.
+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ v: 42, t: 2 }))).toContain(`<td style="text-align:right">`);
+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ v: true, t: 3 }))).toContain(`<td style="text-align:center">`);
+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ v: "A", t: 1 }))).toContain("<td>A</td>");
+
+ // A number formatted and styled by the workbook still gets the fallback alignment.
+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ v: 42, t: 2, s: { bl: 1 } })))
+ .toContain(`<td style="text-align:right;font-weight:bold">`);
+
+ // An explicit alignment wins over the fallback.
+ expect(renderSpreadsheetToHtml(singleCellWorkbook({ v: 42, t: 2, s: { ht: HorizontalAlign.LEFT } })))
+ .toContain(`<td style="text-align:left">`);
});
it("emits colspan only for a purely horizontal merge", () => {
@@ -1103,7 +1119,7 @@ describe("renderSpreadsheetToHtml", () => {
const html = renderSpreadsheetToHtml(
singleCellWorkbook({ v: 1234.5, t: 2, s: { n: { pattern: "#,##0.00" } } })
);
- expect(html).toContain("<td>1,234.50</td>");
+ expect(html).toContain(">1,234.50</td>");
expect(html).not.toContain("1234.5<");
});
@@ -1217,7 +1233,7 @@ describe("renderSpreadsheetToHtml", () => {
it("renders an unformatted number when no pattern is set", () => {
const html = renderSpreadsheetToHtml(singleCellWorkbook({ v: 1234.5, t: 2 }));
- expect(html).toContain("<td>1234.5</td>");
+ expect(html).toContain(">1234.5</td>");
});
it("marks the table with show-gridlines when the sheet has gridlines enabled", () => {
diff --git a/packages/commons/src/lib/spreadsheet/render_to_html.ts b/packages/commons/src/lib/spreadsheet/render_to_html.ts
index 2e3484b5cc..9f9766f302 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,
type CellDocumentSegment,
+ CellValueType,
computeBounds,
getCellDocumentSegments,
getFloatingDrawings,
@@ -380,10 +381,16 @@ function buildMergeMap(mergeData: IRange[], minRow: number, maxRow: number, minC
// #region Style resolution
function buildCssText(style: IStyleData | null, cell?: ICellData): string {
- if (!style) return "";
-
const parts: string[] = [];
+ const ht = style?.ht ?? defaultHorizontalAlign(cell);
+ if (ht != null) {
+ const align = horizontalAlignToCss(ht);
+ if (align) parts.push(`text-align:${align}`);
+ }
+
+ if (!style) return parts.join(";");
+
if (style.bl) parts.push("font-weight:bold");
if (style.it) parts.push("font-style:italic");
if (style.ul?.s) parts.push("text-decoration:underline");
@@ -406,10 +413,6 @@ function buildCssText(style: IStyleData | null, cell?: ICellData): string {
const textColor = patternColor ?? style.cl?.rgb;
if (textColor) parts.push(`color:${sanitizeCssColor(textColor)}`);
- if (style.ht != null) {
- const align = horizontalAlignToCss(style.ht);
- if (align) parts.push(`text-align:${align}`);
- }
if (style.vt != null) {
const valign = verticalAlignToCss(style.vt);
if (valign) parts.push(`vertical-align:${valign}`);
@@ -432,6 +435,17 @@ function buildCssText(style: IStyleData | null, cell?: ICellData): string {
return parts.join(";");
}
+/**
+ * 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
+ * `undefined` for the left case so no `text-align` is emitted and the table default stands.
+ */
+function defaultHorizontalAlign(cell: ICellData | undefined): HorizontalAlign | undefined {
+ if (cell?.t === CellValueType.NUMBER) return HorizontalAlign.RIGHT;
+ if (cell?.t === CellValueType.BOOLEAN) return HorizontalAlign.CENTER;
+ return undefined;
+}
+
function horizontalAlignToCss(align: number): string | null {
switch (align) {
case HorizontalAlign.LEFT: return "left";