Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip _xlfn. and _xlws. In Formula Translations #3828

Merged
merged 5 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Deprecated

- Nothing
- Functions `_translateFormulaToLocale` and `_translateFormulaEnglish` are replaced by versions without leading underscore. [PR #3828](https://github.com/PHPOffice/PhpSpreadsheet/pull/3828)

### Removed

Expand Down Expand Up @@ -78,6 +78,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Html omitting some charts. [Issue #3767](https://github.com/PHPOffice/PhpSpreadsheet/issues/3767) [PR #3771](https://github.com/PHPOffice/PhpSpreadsheet/pull/3771)
- Case Insensitive Comparison for Sheet Names [PR #3791](https://github.com/PHPOffice/PhpSpreadsheet/pull/3791)
- Performance improvement for Xlsx Reader. [Issue #3683](https://github.com/PHPOffice/PhpSpreadsheet/issues/3683) [PR #3810](https://github.com/PHPOffice/PhpSpreadsheet/pull/3810)
- Strip `xlfn.` and `xlws.` from Formula Translations. [Issue #3819](https://github.com/PHPOffice/PhpSpreadsheet/issues/3819) [PR #3828](https://github.com/PHPOffice/PhpSpreadsheet/pull/3828)
- Prevent loop in Shared/File. [Issue #3807](https://github.com/PHPOffice/PhpSpreadsheet/issues/3807) [PR #3809](https://github.com/PHPOffice/PhpSpreadsheet/pull/3809)
- Consistent handling of decimal/thousands separators between StringHelper and Php setlocale. [Issue #3811](https://github.com/PHPOffice/PhpSpreadsheet/issues/3811) [PR #3815](https://github.com/PHPOffice/PhpSpreadsheet/pull/3815)
- Clone worksheet with tables or charts. [Issue #3820](https://github.com/PHPOffice/PhpSpreadsheet/issues/3820) [PR #3821](https://github.com/PHPOffice/PhpSpreadsheet/pull/3821)
Expand Down
23 changes: 23 additions & 0 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Calculation
const CALCULATION_REGEXP_OPENBRACE = '\(';
// Function (allow for the old @ symbol that could be used to prefix a function, but we'll ignore it)
const CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?(?:_xlws\.)?([\p{L}][\p{L}\p{N}\.]*)[\s]*\(';
// Strip xlfn and xlws prefixes from function name
const CALCULATION_REGEXP_STRIP_XLFN_XLWS = '/(_xlfn[.])?(_xlws[.])?(?=[\p{L}][\p{L}\p{N}\.]*[\s]*[(])/';
// Cell reference (cell or range of cells, with or without a sheet reference)
const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=:`-]*)|(\'(?:[^\']|\'[^!])+?\')|(\"(?:[^\"]|\"[^!])+?\"))!)?\$?\b([a-z]{1,3})\$?(\d{1,7})(?![\w.])';
// Cell reference (with or without a sheet reference) ensuring absolute/relative
Expand Down Expand Up @@ -3326,8 +3328,19 @@ private static function translateFormula(array $from, array $to, string $formula
/** @var ?array */
private static $functionReplaceToLocale;

/**
* @deprecated 1.30.0 use translateFormulaToLocale() instead
*
* @codeCoverageIgnore
*/
public function _translateFormulaToLocale(string $formula): string
{
return $this->translateFormulaToLocale($formula);
}

public function translateFormulaToLocale(string $formula): string
{
$formula = preg_replace(self::CALCULATION_REGEXP_STRIP_XLFN_XLWS, '', $formula) ?? '';
// Build list of function names and constants for translation
if (self::$functionReplaceFromExcel === null) {
self::$functionReplaceFromExcel = [];
Expand Down Expand Up @@ -3364,7 +3377,17 @@ public function _translateFormulaToLocale(string $formula): string
/** @var ?array */
private static $functionReplaceToExcel;

/**
* @deprecated 1.30.0 use translateFormulaToEnglish() instead
*
* @codeCoverageIgnore
*/
public function _translateFormulaToEnglish(string $formula): string
{
return $this->translateFormulaToEnglish($formula);
}

public function translateFormulaToEnglish(string $formula): string
{
if (self::$functionReplaceFromLocale === null) {
self::$functionReplaceFromLocale = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpSpreadsheetTests/Calculation/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testTranslation(string $expectedResult, string $locale, string $
self::assertSame($expectedResult, $translatedFormula);

$restoredFormula = Calculation::getInstance()->_translateFormulaToEnglish($translatedFormula);
self::assertSame($formula, $restoredFormula);
self::assertSame(preg_replace(Calculation::CALCULATION_REGEXP_STRIP_XLFN_XLWS, '', $formula), $restoredFormula);
}

public static function providerTranslations(): array
Expand Down
20 changes: 20 additions & 0 deletions tests/data/Calculation/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,24 @@
'fr',
'=3*ROW(B1)',
],
'handle _xlfn' => [
'=MAXWENNS(C5:C10; C5:C10; "<30")',
'de',
'=_xlfn.MAXIFS(C5:C10, C5:C10, "<30")',
],
'handle _xlfn and _xlws' => [
'=ФИЛЬТР(A5:D20;C5:C20=H2;"")',
'ru',
'=_xlfn._xlws.FILTER(A5:D20,C5:C20=H2,"")',
],
'implicit intersection' => [
'=@INDEKS(A1:A10;B1)',
'nb',
'=@INDEX(A1:A10,B1)',
],
'preserve literal _xlfn.' => [
'=@INDEKS(A1:A10;"_xlfn.")',
'nb',
'=@INDEX(A1:A10,"_xlfn.")',
],
];