Skip to content

Commit

Permalink
[FIX] import: detect date format at import
Browse files Browse the repository at this point in the history
If a cell content is a date "12/31/2020", the format
is not detected.

Steps to reproduce (in odoo):
- go to Accounting > Reporting > Aged Receivable
- click on the "XLSX" button to download the file
- go to Documents and upload the xlsx file
- open the file with odoo spreadsheet
=> dates have been transformed to numbers (the format is gone)

opw-3729969

closes #3696

Task: 3729969
X-original-commit: d40f5fb
Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
LucasLefevre committed Feb 14, 2024
1 parent ee8f182 commit ed0669f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
11 changes: 3 additions & 8 deletions src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,7 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
} else {
style = before ? before.style : undefined;
}
const locale = this.getters.getLocale();
let format =
("format" in after ? after.format : before && before.format) ||
detectDateFormat(afterContent, locale) ||
detectNumberFormat(afterContent);
const format = "format" in after ? after.format : before && before.format;

/* 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:
Expand Down Expand Up @@ -501,12 +497,11 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
style: Style | undefined
): LiteralCell {
const locale = this.getters.getLocale();
content = parseLiteral(content, locale).toString();
return {
id,
content,
content: parseLiteral(content, locale).toString(),
style,
format,
format: format || detectDateFormat(content, locale) || detectNumberFormat(content),
isFormula: false,
};
}
Expand Down
35 changes: 34 additions & 1 deletion tests/plugins/import_export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ import {
setCellContent,
setStyle,
} from "../test_helpers/commands_helpers";
import { getCell, getCellContent, getMerges } from "../test_helpers/getters_helpers";
import { FR_LOCALE } from "../test_helpers/constants";
import {
getCell,
getCellContent,
getEvaluatedCell,
getMerges,
} from "../test_helpers/getters_helpers";
import "../test_helpers/helpers";

jest.mock("../../src/helpers/uuid", () => require("../__mocks__/uuid"));
Expand Down Expand Up @@ -695,6 +701,33 @@ test("import then export (figures)", () => {
expect(model).toExport(modelData);
});

test("import date as string and detect the format", () => {
const model = new Model({
sheets: [
{
cells: { A1: { content: "12/31/2020" } },
},
],
});
expect(getCell(model, "A1")?.format).toBe("m/d/yyyy");
expect(getCell(model, "A1")?.content).toBe("44196");
expect(getEvaluatedCell(model, "A1")?.formattedValue).toBe("12/31/2020");
});

test("import localized date as string and detect the format", () => {
const model = new Model({
sheets: [
{
cells: { A1: { content: "31/12/2020" } },
},
],
settings: { locale: FR_LOCALE },
});
expect(getCell(model, "A1")?.format).toBe("d/m/yyyy");
expect(getCell(model, "A1")?.content).toBe("44196");
expect(getEvaluatedCell(model, "A1")?.formattedValue).toBe("31/12/2020");
});

test("Can import spreadsheet with only version", () => {
new Model({ version: 1 });
// We expect the model to be loaded without traceback
Expand Down

0 comments on commit ed0669f

Please sign in to comment.