Skip to content

Commit

Permalink
[FIX] formatting: do not show escape character
Browse files Browse the repository at this point in the history
If you want to use double quotes in a string which is part of a formula, you
have to escape it using a backslash (="hello \"world\"")
However, the backslash is shown when displaying the cell content.

closes #3565

Task: 3698283
X-original-commit: 050b3ac
Signed-off-by: Pierre Rousseau (pro) <[email protected]>
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
LucasLefevre committed Feb 5, 2024
1 parent 7c23b79 commit b157972
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/helpers/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ function parseFormat(formatString: Format): InternalFormat {
export function formatValue(value: CellValue, { format, locale }: LocaleFormat): FormattedValue {
switch (typeof value) {
case "string":
if (value.includes('\\"')) {
return value.replace(/\\"/g, '"');
}
return value;
case "boolean":
return value ? "TRUE" : "FALSE";
Expand Down
12 changes: 12 additions & 0 deletions tests/helpers/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import { FR_LOCALE } from "./../test_helpers/constants";

const locale = DEFAULT_LOCALE;

describe("formatValue on string", () => {
test("apply on regular strings", () => {
expect(formatValue("", { locale })).toBe("");
expect(formatValue("test", { locale })).toBe("test");
expect(formatValue("test", { locale, format: "#,###.0" })).toBe("test");
});

test("apply on strings with escape characters", () => {
expect(formatValue('Hello \\"world\\"', { locale })).toBe('Hello "world"');
});
});

describe("formatValue on number", () => {
test("apply default format ", () => {
expect(formatValue(1, { locale })).toBe("1");
Expand Down
6 changes: 6 additions & 0 deletions tests/plugins/cell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit b157972

Please sign in to comment.