Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ export const DEFAULT_STYLE = {
textColor: "",
} satisfies Required<Style>;

export const DEFAULT_NUMBER_STYLE = { ...DEFAULT_STYLE, align: "right" } satisfies Required<Style>;

export const DEFAULT_VERTICAL_ALIGN = DEFAULT_STYLE.verticalAlign;
export const DEFAULT_WRAPPING_MODE = DEFAULT_STYLE.wrapping;

Expand Down
18 changes: 11 additions & 7 deletions src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DEFAULT_STYLE } from "../../constants";
import { DEFAULT_NUMBER_STYLE, DEFAULT_STYLE } from "../../constants";
import { Token, compile, tokenize } from "../../formulas";
import { deepEquals } from "../../helpers";
import { deepEquals, isNumber } from "../../helpers";
import { parseLiteral } from "../../helpers/cells";
import {
concat,
Expand Down Expand Up @@ -266,7 +266,7 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
for (const position of positions) {
const cell = this.getters.getCell(position)!;
const xc = toXC(position.col, position.row);
const style = this.removeDefaultStyleValues(cell.style);
const style = this.extractCustomStyle(cell);
cells[xc] = {
style: Object.keys(style).length ? getItemId<Style>(style, styles) : undefined,
format: cell.format ? getItemId<Format>(cell.format, formats) : undefined,
Expand Down Expand Up @@ -295,10 +295,14 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
this.export(data);
}

private removeDefaultStyleValues(style: Style | undefined): Style {
const cleanedStyle = { ...style };
for (const property in DEFAULT_STYLE) {
if (cleanedStyle[property] === DEFAULT_STYLE[property]) {
private extractCustomStyle(cell: Cell): Style {
const cleanedStyle = { ...cell.style };
const defaultStyle = isNumber(cell.content, this.getters.getLocale())
? DEFAULT_NUMBER_STYLE
: DEFAULT_STYLE;
for (const property in defaultStyle) {
if (property === "align" && cell.isFormula) continue;
if (cleanedStyle[property] === defaultStyle[property]) {
delete cleanedStyle[property];
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/cells/style_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ describe("styles", () => {
});
});

test("align left is exported for number and formula but not text", () => {
const model = new Model();
setStyle(model, "A1:A3", { align: "left" });
setStyle(model, "B1:B3", { align: "right" });
setCellContent(model, "A1", "1");
setCellContent(model, "B1", "1");
setCellContent(model, "A2", "TEXT");
setCellContent(model, "B2", "TEXT");
setCellContent(model, "A3", "=1");
setCellContent(model, "B3", "=1");

const data = model.exportData();
expect(data.sheets[0].cells.A1?.style).toBe(1);
expect(data.sheets[0].cells.A2?.style).toBe(undefined);
expect(data.sheets[0].cells.B1?.style).toBe(undefined);
expect(data.sheets[0].cells.B2?.style).toBe(2);
expect(data.sheets[0].cells.A3?.style).toBe(1);
expect(data.sheets[0].cells.B3?.style).toBe(2);

expect(data.styles).toEqual({ 1: { align: "left" }, 2: { align: "right" } });
});

test("clearing format on a cell with no content actually remove it", () => {
const model = new Model();
selectCell(model, "B1");
Expand Down