Skip to content

Commit

Permalink
Strip _xlfn. and _xlfs. In Formula Translations
Browse files Browse the repository at this point in the history
Fix PHPOffice#3819. Excel can add these prefixes (basically invisible to end-user). Formula translation in PhpSpreadsheet fails when dealing with these unexpected prefixes, and, even if it handled it correctly, the unexpected prefixes confuse the users. I have changed to strip those prefixes when translating to a locale. This is probably not perfect, but is almost certainly good enough. I could easily add the same change when translating from a locale to English, but I don't think there's a good use case for that, so am opting not to do so for now.

The documentation mentions `translateFormulaToLocale` and `translateFormulaToEnglish`. Neither of these exist; both names are preceded by an underscore. I have changed the code to match the documentation rather than vice versa, retaining deprecated versions of the underscored routines which merely invoke the non-underscored routines.
  • Loading branch information
oleibman committed Dec 14, 2023
1 parent 3f20b23 commit 5b7fce9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
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 #3829](https://github.com/PHPOffice/PhpSpreadsheet/pull/3829)

### 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 #3829](https://github.com/PHPOffice/PhpSpreadsheet/pull/3829)
- Prevent loop in Shared/File. [Issue #3807](https://github.com/PHPOffice/PhpSpreadsheet/issues/3807) [PR #3809](https://github.com/PHPOffice/PhpSpreadsheet/pull/3809)

## 1.29.0 - 2023-06-15
Expand Down
17 changes: 17 additions & 0 deletions src/PhpSpreadsheet/Calculation/Calculation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3326,8 +3326,17 @@ private static function translateFormula(array $from, array $to, string $formula
/** @var ?array */
private static $functionReplaceToLocale;

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

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

/**
* @deprecated 1.30.0 use translateFormulaToEnglish() instead
*/
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('/_(xlfn|xlws)[.]/', '', $formula), $restoredFormula);
}

public static function providerTranslations(): array
Expand Down
10 changes: 10 additions & 0 deletions tests/data/Calculation/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,14 @@
'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,"")',
],
];

0 comments on commit 5b7fce9

Please sign in to comment.