Added support for the base function (#1344)
This commit is contained in:
parent
25e3e45eb6
commit
0c52f173aa
|
@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
- Added support for the BASE function
|
||||
- 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)
|
||||
|
|
|
@ -333,6 +333,11 @@ class Calculation
|
|||
'functionCall' => [Functions::class, 'DUMMY'],
|
||||
'argumentCount' => '1',
|
||||
],
|
||||
'BASE' => [
|
||||
'category' => Category::CATEGORY_MATH_AND_TRIG,
|
||||
'functionCall' => [MathTrig::class, 'BASE'],
|
||||
'argumentCount' => '2,3',
|
||||
],
|
||||
'BESSELI' => [
|
||||
'category' => Category::CATEGORY_ENGINEERING,
|
||||
'functionCall' => [Engineering::class, 'BESSELI'],
|
||||
|
|
|
@ -142,6 +142,49 @@ class MathTrig
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -23,6 +23,7 @@ AVERAGEA
|
|||
AVERAGEIF
|
||||
AVERAGEIFS
|
||||
BAHTTEXT
|
||||
BASE
|
||||
BESSELI
|
||||
BESSELJ
|
||||
BESSELK
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
],
|
||||
];
|
Loading…
Reference in New Issue