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
13 changes: 12 additions & 1 deletion packages/o-spreadsheet-engine/src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { parseLiteral } from "../../helpers/cells/cell_evaluation";
import {
detectDateFormat,
detectNumberFormat,
isDateTimeFormat,
isExcelCompatible,
isTextFormat,
} from "../../helpers/format/format";
Expand Down Expand Up @@ -510,8 +511,18 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {

// Compute the new cell properties
const afterContent = hasContent ? replaceNewLines(after?.content) : before?.content || "";
const format = "format" in after ? after.format : before && before.format;

let format: Format | undefined = undefined;
if (after.format !== undefined) {
format = after.format;
} else if (before?.format && (before?.format === "@" || isDateTimeFormat(before?.format))) {
format = before.format;
} else if (after.content) {
format = detectDateFormat(after.content, this.getters.getLocale());
}
if (format === undefined) {
format = before?.format || undefined;
}
/* Read the following IF as:
* we need to remove the cell if it is completely empty, but we can know if it completely empty if:
* - the command says the new content is empty and has no format
Expand Down
20 changes: 20 additions & 0 deletions tests/formats/formatting_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,26 @@ describe("formatting values (when change decimal)", () => {
expect(getCell(model, "A1")!.format).toBe("0%");
});

test.each(["", "0%", "#,##0", "#,##0[$ THUNE ]"])(
"Set Date to a cell formatted with format %s, format should be adapted",
(format: string) => {
const model = new Model();
setFormat(model, "A1", format);
setCellContent(model, "A1", "12/12/2025");
expect(getEvaluatedCell(model, "A1").format).toBe("m/d/yyyy");
}
);

test.each(["@", "d/m/yyyy"])(
"Set date to a cell formatted with format %s, format should not be adapted",
(format: string) => {
const model = new Model();
setFormat(model, "A1", format);
setCellContent(model, "A1", "12/12/2025");
expect(getEvaluatedCell(model, "A1").format).toBe(format);
}
);

test.each([
["0%", "0.0%", "0.00%"],
["#,##0", "#,##0.0", "#,##0.00"],
Expand Down