Skip to content

Commit

Permalink
[IMP] functions: add VALUE function
Browse files Browse the repository at this point in the history
Add the VALUE function which converts a text string
that represents a number to a number.
Text can be in any of the constant number, date, or time formats,
If text is not in one of these formats,
VALUE returns the #ERROR error value.

closes #5294

Task: 4373019
X-original-commit: b837929
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Florian Damhaut (flda) <[email protected]>
  • Loading branch information
fdamhaut committed Dec 2, 2024
1 parent fb1cbd8 commit 70cb0f5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/functions/module_text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,16 @@ export const TEXT = {
},
isExported: true,
} satisfies AddFunctionDescription;

// -----------------------------------------------------------------------------
// VALUE
// -----------------------------------------------------------------------------
export const VALUE = {
description: _t("Converts a string to a numeric value."),
args: [arg("value (number)", _t("the string to be converted"))],
returns: ["NUMBER"],
compute: function (value: Maybe<FPayload>): number {
return toNumber(value, this.locale);
},
isExported: true,
} satisfies AddFunctionDescription;
12 changes: 12 additions & 0 deletions tests/functions/module_text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,3 +785,15 @@ test("TEXT formula", () => {
expect(evaluateCell("A1", { A1: '=TEXT(.05, "000%")' })).toBe("005%");
expect(evaluateCell("A1", { A1: "=TEXT(5, 0)" })).toBe("5");
});

test("VALUE formula", () => {
expect(evaluateCell("A1", { A1: "=VALUE(5)" })).toBe(5);
expect(evaluateCell("A1", { A1: '=VALUE("")' })).toBe(0);
expect(evaluateCell("A1", { A1: '=VALUE("$10")' })).toBe(10);
expect(evaluateCell("A1", { A1: '=VALUE("12:00")' })).toBe(0.5);
expect(evaluateCell("A1", { A1: '=VALUE("01/19/1900")' })).toBe(20);
expect(evaluateCell("A1", { A1: '=VALUE("ABC")' })).toBe("#ERROR");
expect(evaluateCell("A1", { A1: "=VALUE(1/0)" })).toBe("#ERROR");
expect(evaluateCell("A1", { A1: "=VALUE(A2)", A2: "12.5" })).toBe(12.5);
expect(evaluateCell("A1", { A1: "=VALUE(A2)" })).toBe(0);
});

0 comments on commit 70cb0f5

Please sign in to comment.