From 70cb0f5fac1992e143e44e9955c226878bd314a0 Mon Sep 17 00:00:00 2001 From: Florian Damhaut Date: Mon, 2 Dec 2024 10:32:08 +0000 Subject: [PATCH] [IMP] functions: add VALUE function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 odoo/o-spreadsheet#5294 Task: 4373019 X-original-commit: b83792911ce481ba208065134e14ff49faa6156f Signed-off-by: Lucas Lefèvre (lul) Signed-off-by: Florian Damhaut (flda) --- src/functions/module_text.ts | 13 +++++++++++++ tests/functions/module_text.test.ts | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/functions/module_text.ts b/src/functions/module_text.ts index 5be61b6ddf..7745c04560 100644 --- a/src/functions/module_text.ts +++ b/src/functions/module_text.ts @@ -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): number { + return toNumber(value, this.locale); + }, + isExported: true, +} satisfies AddFunctionDescription; diff --git a/tests/functions/module_text.test.ts b/tests/functions/module_text.test.ts index aa97f53f92..aa5dfb7de2 100644 --- a/tests/functions/module_text.test.ts +++ b/tests/functions/module_text.test.ts @@ -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); +});