Added support for the base function (#1344)

This commit is contained in:
paulkned 2020-02-19 20:12:30 +01:00 committed by GitHub
parent 25e3e45eb6
commit 0c52f173aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 140 additions and 0 deletions

View File

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased] ## [Unreleased]
- Added support for the BASE function
- Added support for the ARABIC function - Added support for the ARABIC function
- Conditionals - Extend Support for (NOT)CONTAINSBLANKS [#1278](https://github.com/PHPOffice/PhpSpreadsheet/pull/1278) - 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 Error in Formula Processing Better for Xls [#1267](https://github.com/PHPOffice/PhpSpreadsheet/pull/1267)

View File

@ -333,6 +333,11 @@ class Calculation
'functionCall' => [Functions::class, 'DUMMY'], 'functionCall' => [Functions::class, 'DUMMY'],
'argumentCount' => '1', 'argumentCount' => '1',
], ],
'BASE' => [
'category' => Category::CATEGORY_MATH_AND_TRIG,
'functionCall' => [MathTrig::class, 'BASE'],
'argumentCount' => '2,3',
],
'BESSELI' => [ 'BESSELI' => [
'category' => Category::CATEGORY_ENGINEERING, 'category' => Category::CATEGORY_ENGINEERING,
'functionCall' => [Engineering::class, 'BESSELI'], 'functionCall' => [Engineering::class, 'BESSELI'],

View File

@ -142,6 +142,49 @@ class MathTrig
return Functions::VALUE(); return Functions::VALUE();
} }
/**
* BASE.
*
* Converts a number into a text representation with the given radix (base).
*
* Excel Function:
* BASE(Number, Radix [Min_length])
*
* @category Mathematical and Trigonometric Functions
*
* @param float $number
* @param float $radix
* @param int $minLength
*
* @return string the text representation with the given radix (base)
*/
public static function BASE($number, $radix, $minLength = null)
{
$number = Functions::flattenSingleValue($number);
$radix = Functions::flattenSingleValue($radix);
$minLength = Functions::flattenSingleValue($minLength);
if (is_numeric($number) && is_numeric($radix) && ($minLength === null || is_numeric($minLength))) {
// Truncate to an integer
$number = (int) $number;
$radix = (int) $radix;
$minLength = (int) $minLength;
if ($number < 0 || $number >= 2 ** 53 || $radix < 2 || $radix > 36) {
return Functions::NAN(); // Numeric range constraints
}
$outcome = strtoupper((string) base_convert($number, 10, $radix));
if ($minLength !== null) {
$outcome = str_pad($outcome, $minLength, '0', STR_PAD_LEFT); // String padding
}
return $outcome;
}
return Functions::VALUE();
}
/** /**
* CEILING. * CEILING.
* *

View File

@ -23,6 +23,7 @@ AVERAGEA
AVERAGEIF AVERAGEIF
AVERAGEIFS AVERAGEIFS
BAHTTEXT BAHTTEXT
BASE
BESSELI BESSELI
BESSELJ BESSELJ
BESSELK BESSELK

View File

@ -0,0 +1,31 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\MathTrig;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
use PHPUnit\Framework\TestCase;
class BaseTest extends TestCase
{
public function setUp()
{
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}
/**
* @dataProvider providerBASE
*
* @param mixed $expectedResult
*/
public function testBASE($expectedResult, ...$args)
{
$result = MathTrig::BASE(...$args);
$this->assertEquals($expectedResult, $result);
}
public function providerBASE()
{
return require 'data/Calculation/MathTrig/BASE.php';
}
}

View File

@ -0,0 +1,59 @@
<?php
return [
[
'111',
7,
2,
],
[
'64',
100,
16,
],
[
'CA',
202,
16,
],
[
'0000001111',
15,
2,
10,
],
[
'0000001111',
15.3,
2.6,
10,
],
[
'#VALUE!',
'ABC',
2,
10,
],
[
'#VALUE!',
15,
'ABC',
10,
],
[
'#VALUE!',
15,
2,
'ABC',
],
[
'#NUM!',
-1,
2,
],
[
'#NUM!',
15,
-1,
],
];