Files
oleibman f1477b130f Allow Replace of Dummy Function with Custom Function
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.
2025-07-18 22:01:59 -07:00

45 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcException;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Helpers;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
class CustomFunction
{
public static function fourthPower(mixed $number): float|int|string
{
try {
$number = Helpers::validateNumericNullBool($number);
} catch (CalcException $e) {
return $e->getMessage();
}
return $number ** 4;
}
/**
* ASC.
* Converts full-width (double-byte) characters to half-width (single-byte) characters.
* There are many difficulties with implementing this into PhpSpreadsheet.
*/
public static function ASC(mixed $stringValue): string
{
/*if (is_array($stringValue)) {
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $stringValue);
}*/
$stringValue = StringHelper::convertToString($stringValue, convertBool: true);
if (function_exists('mb_convert_kana')) {
return mb_convert_kana($stringValue, 'a', 'UTF-8');
}
// Fallback if mb_convert_kana is not available.
// PhpSpreadsheet heavily relies on mbstring, so this is more of a theoretical fallback.
// A comprehensive manual conversion is extensive.
return $stringValue;
}
}