Added support for the ARABIC excel function (#1343)

Updated changelog

Updated docprops

Fixed stylci
This commit is contained in:
paulkned 2020-02-11 22:59:19 +01:00 committed by GitHub
parent 14d807c2c4
commit 25e3e45eb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 133 additions and 0 deletions

View File

@ -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)

View File

@ -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'],

View File

@ -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.
*

View File

@ -9,6 +9,7 @@ ADDRESS
AMORDEGRC
AMORLINC
AND
ARABIC
AREAS
ASC
ASIN

View File

@ -0,0 +1,32 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class ArabicTest extends TestCase
{
public function setUp()
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerARABIC
*
* @param mixed $expectedResult
* @param string $romanNumeral
*/
public function testARABIC($expectedResult, $romanNumeral)
{
$result = MathTrig::ARABIC($romanNumeral);
$this->assertEquals($expectedResult, $result);
}
public function providerARABIC()
{
return require 'data/Calculation/MathTrig/ARABIC.php';
}
}

View File

@ -0,0 +1,36 @@
<?php
return [
[
0,
' ',
],
[
49,
'XLIX',
],
[
50,
'L',
],
[
2012,
'MMXII',
],
[
999,
'CMXCIX',
],
[
499,
'CDXCIX',
],
[
2018,
'MMXVIII',
],
[
-2018,
'-MMXVIII',
],
];