From 25e3e45eb67eea4c1cbe25654ccb73037c98bdc4 Mon Sep 17 00:00:00 2001 From: paulkned Date: Tue, 11 Feb 2020 22:59:19 +0100 Subject: [PATCH] Added support for the ARABIC excel function (#1343) Updated changelog Updated docprops Fixed stylci --- CHANGELOG.md | 1 + .../Calculation/Calculation.php | 5 ++ src/PhpSpreadsheet/Calculation/MathTrig.php | 58 +++++++++++++++++++ .../Calculation/functionlist.txt | 1 + .../Functions/MathTrig/ArabicTest.php | 32 ++++++++++ tests/data/Calculation/MathTrig/ARABIC.php | 36 ++++++++++++ 6 files changed, 133 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ArabicTest.php create mode 100644 tests/data/Calculation/MathTrig/ARABIC.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 864479f6..cbf1bdde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). ## [Unreleased] +- Added support for the ARABIC function - Conditionals - Extend Support for (NOT)CONTAINSBLANKS [#1278](https://github.com/PHPOffice/PhpSpreadsheet/pull/1278) - Handle Error in Formula Processing Better for Xls [#1267](https://github.com/PHPOffice/PhpSpreadsheet/pull/1267) - Handle ConditionalStyle NumberFormat When Reading Xlsx File [#1296](https://github.com/PHPOffice/PhpSpreadsheet/pull/1296) diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index cc8a8bd8..1901e5d3 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -263,6 +263,11 @@ class Calculation 'functionCall' => [Logical::class, 'logicalAnd'], 'argumentCount' => '1+', ], + 'ARABIC' => [ + 'category' => Category::CATEGORY_MATH_AND_TRIG, + 'functionCall' => [MathTrig::class, 'ARABIC'], + 'argumentCount' => '1', + ], 'AREAS' => [ 'category' => Category::CATEGORY_LOOKUP_AND_REFERENCE, 'functionCall' => [Functions::class, 'DUMMY'], diff --git a/src/PhpSpreadsheet/Calculation/MathTrig.php b/src/PhpSpreadsheet/Calculation/MathTrig.php index a73e14e1..7ad78524 100644 --- a/src/PhpSpreadsheet/Calculation/MathTrig.php +++ b/src/PhpSpreadsheet/Calculation/MathTrig.php @@ -38,6 +38,64 @@ class MathTrig return ($num - ($num % $n)) / $n; } + /** + * ARABIC. + * + * Converts a Roman numeral to an Arabic numeral. + * + * Excel Function: + * ARABIC(text) + * + * @category Mathematical and Trigonometric Functions + * + * @param string $roman + * + * @return int|string the arabic numberal contrived from the roman numeral + */ + public static function ARABIC($roman) + { + // An empty string should return 0 + $roman = substr(trim(strtoupper((string) Functions::flattenSingleValue($roman))), 0, 255); + if ($roman === '') { + return 0; + } + + // Convert the roman numeral to an arabic number + $lookup = [ + 'M' => 1000, 'CM' => 900, + 'D' => 500, 'CD' => 400, + 'C' => 100, 'XC' => 90, + 'L' => 50, 'XL' => 40, + 'X' => 10, 'IX' => 9, + 'V' => 5, 'IV' => 4, 'I' => 1, + ]; + + $negativeNumber = $roman[0] === '-'; + if ($negativeNumber) { + $roman = substr($roman, 1); + } + + $arabic = 0; + for ($i = 0; $i < strlen($roman); ++$i) { + if (!isset($lookup[$roman[$i]])) { + return Functions::VALUE(); // Invalid character detected + } + + if ($i < (strlen($roman) - 1) && isset($lookup[substr($roman, $i, 2)])) { + $arabic += $lookup[substr($roman, $i, 2)]; // Detected a match on the next 2 characters + ++$i; + } else { + $arabic += $lookup[$roman[$i]]; // Detected a match on one character only + } + } + + if ($negativeNumber) { + $arabic *= -1; // The number should be negative + } + + return $arabic; + } + /** * ATAN2. * diff --git a/src/PhpSpreadsheet/Calculation/functionlist.txt b/src/PhpSpreadsheet/Calculation/functionlist.txt index 97a0ceeb..66523f4f 100644 --- a/src/PhpSpreadsheet/Calculation/functionlist.txt +++ b/src/PhpSpreadsheet/Calculation/functionlist.txt @@ -9,6 +9,7 @@ ADDRESS AMORDEGRC AMORLINC AND +ARABIC AREAS ASC ASIN diff --git a/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ArabicTest.php b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ArabicTest.php new file mode 100644 index 00000000..f3f5c965 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/Functions/MathTrig/ArabicTest.php @@ -0,0 +1,32 @@ +assertEquals($expectedResult, $result); + } + + public function providerARABIC() + { + return require 'data/Calculation/MathTrig/ARABIC.php'; + } +} diff --git a/tests/data/Calculation/MathTrig/ARABIC.php b/tests/data/Calculation/MathTrig/ARABIC.php new file mode 100644 index 00000000..3c397dc9 --- /dev/null +++ b/tests/data/Calculation/MathTrig/ARABIC.php @@ -0,0 +1,36 @@ +