diff --git a/src/helpers/format.ts b/src/helpers/format.ts index e433ae7ae8..d70e8540da 100644 --- a/src/helpers/format.ts +++ b/src/helpers/format.ts @@ -113,6 +113,9 @@ function parseFormat(formatString: Format): InternalFormat { export function formatValue(value: CellValue, format?: Format): FormattedValue { switch (typeof value) { case "string": + if (value.includes('\\"')) { + return value.replace(/\\"/g, '"'); + } return value; case "boolean": return value ? "TRUE" : "FALSE"; diff --git a/tests/helpers/format.test.ts b/tests/helpers/format.test.ts index 20aec59d92..f7b8e983c6 100644 --- a/tests/helpers/format.test.ts +++ b/tests/helpers/format.test.ts @@ -1,6 +1,18 @@ import { parseDateTime } from "../../src/helpers"; import { formatValue, isDateTimeFormat } from "../../src/helpers/format"; +describe("formatValue on string", () => { + test("apply on regular strings", () => { + expect(formatValue("")).toBe(""); + expect(formatValue("test")).toBe("test"); + expect(formatValue("test", "#,###.0")).toBe("test"); + }); + + test("apply on strings with escape characters", () => { + expect(formatValue('Hello \\"world\\"')).toBe('Hello "world"'); + }); +}); + describe("formatValue on number", () => { test("apply default format ", () => { expect(formatValue(1)).toBe("1"); diff --git a/tests/plugins/cell.test.ts b/tests/plugins/cell.test.ts index 300800755f..2bf092f3c0 100644 --- a/tests/plugins/cell.test.ts +++ b/tests/plugins/cell.test.ts @@ -106,6 +106,12 @@ describe("getCellText", () => { const result = clearCell(model, "A1"); expect(result).toBeCancelledBecause(CommandResult.NoChanges); }); + + test("escape character is not display when formatting string", () => { + const model = new Model(); + setCellContent(model, "A1", '="hello \\"world\\""'); + expect(getEvaluatedCell(model, "A1")?.formattedValue).toBe('hello "world"'); + }); }); describe("link cell", () => {