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 #3563

Task: 3698283
X-original-commit: 050b3ac
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Pierre Rousseau (pro) <[email protected]>
  • Loading branch information
LucasLefevre committed Feb 6, 2024
1 parent 0bf5f45 commit 743ac9b
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 @@ -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";
Expand Down
12 changes: 12 additions & 0 deletions tests/helpers/format.test.ts
Original file line number Diff line number Diff line change
@@ -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");
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 743ac9b

Please sign in to comment.