Skip to content

Commit 3e0fb7f

Browse files
committed
[IMP] export: export align left when the cell content is a number;
Task: 5368130
1 parent ab9f15e commit 3e0fb7f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ export const DEFAULT_STYLE = {
165165
textColor: "",
166166
} satisfies Required<Style>;
167167

168+
export const DEFAULT_NUMBER_STYLE = { ...DEFAULT_STYLE, align: "right" } satisfies Required<Style>;
169+
168170
export const DEFAULT_VERTICAL_ALIGN = DEFAULT_STYLE.verticalAlign;
169171
export const DEFAULT_WRAPPING_MODE = DEFAULT_STYLE.wrapping;
170172

src/plugins/core/cell.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { DEFAULT_STYLE } from "../../constants";
1+
import { DEFAULT_NUMBER_STYLE, DEFAULT_STYLE } from "../../constants";
22
import { Token, compile, tokenize } from "../../formulas";
3-
import { deepEquals } from "../../helpers";
3+
import { deepEquals, isNumber } from "../../helpers";
44
import { parseLiteral } from "../../helpers/cells";
55
import {
66
concat,
@@ -266,7 +266,7 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
266266
for (const position of positions) {
267267
const cell = this.getters.getCell(position)!;
268268
const xc = toXC(position.col, position.row);
269-
const style = this.removeDefaultStyleValues(cell.style);
269+
const style = this.extractCustomStyle(cell);
270270
cells[xc] = {
271271
style: Object.keys(style).length ? getItemId<Style>(style, styles) : undefined,
272272
format: cell.format ? getItemId<Format>(cell.format, formats) : undefined,
@@ -295,10 +295,14 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
295295
this.export(data);
296296
}
297297

298-
private removeDefaultStyleValues(style: Style | undefined): Style {
299-
const cleanedStyle = { ...style };
300-
for (const property in DEFAULT_STYLE) {
301-
if (cleanedStyle[property] === DEFAULT_STYLE[property]) {
298+
private extractCustomStyle(cell: Cell): Style {
299+
const cleanedStyle = { ...cell.style };
300+
const defaultStyle = isNumber(cell.content, this.getters.getLocale())
301+
? DEFAULT_NUMBER_STYLE
302+
: DEFAULT_STYLE;
303+
for (const property in defaultStyle) {
304+
if (property === "align" && cell.isFormula) continue;
305+
if (cleanedStyle[property] === defaultStyle[property]) {
302306
delete cleanedStyle[property];
303307
}
304308
}

tests/cells/style_plugin.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ describe("styles", () => {
9595
});
9696
});
9797

98+
test("align left is exported for number and formula but not text", () => {
99+
const model = new Model();
100+
setStyle(model, "A1:A3", { align: "left" });
101+
setStyle(model, "B1:B3", { align: "right" });
102+
setCellContent(model, "A1", "1");
103+
setCellContent(model, "B1", "1");
104+
setCellContent(model, "A2", "TEXT");
105+
setCellContent(model, "B2", "TEXT");
106+
setCellContent(model, "A3", "=1");
107+
setCellContent(model, "B3", "=1");
108+
109+
const data = model.exportData();
110+
expect(data.sheets[0].cells.A1?.style).toBe(1);
111+
expect(data.sheets[0].cells.A2?.style).toBe(undefined);
112+
expect(data.sheets[0].cells.B1?.style).toBe(undefined);
113+
expect(data.sheets[0].cells.B2?.style).toBe(2);
114+
expect(data.sheets[0].cells.A3?.style).toBe(1);
115+
expect(data.sheets[0].cells.B3?.style).toBe(2);
116+
117+
expect(data.styles).toEqual({ 1: { align: "left" }, 2: { align: "right" } });
118+
});
119+
98120
test("clearing format on a cell with no content actually remove it", () => {
99121
const model = new Model();
100122
selectCell(model, "B1");

0 commit comments

Comments
 (0)