From ed0669fda4b79adeeba3ac89e12a659c64a0df07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Wed, 14 Feb 2024 11:14:22 +0000 Subject: [PATCH] [FIX] import: detect date format at import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 odoo/o-spreadsheet#3696 Task: 3729969 X-original-commit: d40f5fb61e3717c20d55abc0ae405c267d8682ca Signed-off-by: Rémi Rahir (rar) Signed-off-by: Lucas Lefèvre (lul) --- src/plugins/core/cell.ts | 11 +++------ tests/plugins/import_export.test.ts | 35 ++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/plugins/core/cell.ts b/src/plugins/core/cell.ts index 304d0c3b1..6461a9d30 100644 --- a/src/plugins/core/cell.ts +++ b/src/plugins/core/cell.ts @@ -440,11 +440,7 @@ export class CellPlugin extends CorePlugin 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: @@ -501,12 +497,11 @@ export class CellPlugin extends CorePlugin 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, }; } diff --git a/tests/plugins/import_export.test.ts b/tests/plugins/import_export.test.ts index e56d94fd3..7be28c2d2 100644 --- a/tests/plugins/import_export.test.ts +++ b/tests/plugins/import_export.test.ts @@ -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")); @@ -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