mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-20 07:09:21 +00:00
f1477b130f
Custom functions were introduced recently, with a restriction that non-custom functions could not be replaced. However, reviewing a recent issue has led me to the conclusion that it might sometimes be impractical to implement an Excel function in PhpSpreadsheet for all users, but it could still be helpful to some users to offer an implementation anyhow. The Excel ASC function is currently unimplemented in PhpSpreadsheet, and is a candidate for replacement using the functionality added in this PR. This PR supersedes PR #4513, which I will now close, and its earlier incarnation PR #4511. For reasons discussed in 4513, I don't see a way forward for implementing the Excel ASC function in a way that would be generally usable. However, this PR would permit a user to implement ASC in a way which would satisfy the user's local requirements. See the new `testReplaceDummyFunction`; I think, or at least hope, that, despite the deficiencies of 4511 and 4513, this might satisfy the author's requirement.
50 lines
2.0 KiB
PHP
50 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class CustomFunctionTest extends TestCase
|
|
{
|
|
public static function testCustomFunction(): void
|
|
{
|
|
$calculation = Calculation::getInstance();
|
|
$key = 'FOURTHPOWER';
|
|
$value = [
|
|
'category' => 'custom',
|
|
'functionCall' => [CustomFunction::class, 'fourthPower'],
|
|
'argumentCount' => '1',
|
|
];
|
|
self::assertTrue(Calculation::addFunction($key, $value));
|
|
self::assertFalse(Calculation::addFunction('sqrt', $value));
|
|
self::assertSame(16, $calculation->calculateFormula('=FOURTHPOWER(2)'));
|
|
self::assertSame('#VALUE!', $calculation->calculateFormula('=FOURTHPOWER("X")'));
|
|
self::assertSame('#NAME?', $calculation->calculateFormula('=FOURTHPOWE("X")'));
|
|
self::assertFalse(Calculation::removeFunction('SQRT'));
|
|
self::assertSame(2.0, $calculation->calculateFormula('=SQRT(4)'));
|
|
self::assertFalse(Calculation::removeFunction('sqrt'));
|
|
self::assertSame(4.0, $calculation->calculateFormula('=sqrt(16)'));
|
|
self::assertTrue(Calculation::removeFunction($key));
|
|
self::assertSame('#NAME?', $calculation->calculateFormula('=FOURTHPOWER(3)'));
|
|
self::assertFalse(Calculation::removeFunction('WHATEVER'));
|
|
}
|
|
|
|
public static function testReplaceDummyFunction(): void
|
|
{
|
|
$functions = Calculation::getFunctions();
|
|
$key = 'ASC';
|
|
$oldValue = $functions[$key] ?? null;
|
|
self::assertIsArray($oldValue);
|
|
$calculation = Calculation::getInstance();
|
|
$value = $oldValue;
|
|
$value['functionCall'] = [CustomFunction::class, 'ASC'];
|
|
self::assertTrue(Calculation::addFunction($key, $value));
|
|
self::assertSame('ABC', $calculation->calculateFormula('=ASC("ABC")'));
|
|
self::assertTrue(Calculation::removeFunction('ASC'));
|
|
self::assertTrue(Calculation::addFunction($key, $oldValue));
|
|
}
|
|
}
|