Ignore square-$-brackets prefix in format string (#2886)

* Ignore square-$-brackets prefix in format string

* Test for square-$-brackets prefix in format string issue fixed

* Fix for phpstan compliance

* Additional assert for checking number format of tested source cell
This commit is contained in:
FlameStorm 2022-06-16 00:55:17 +03:00 committed by GitHub
parent 11348a4830
commit 1829dea91e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -112,6 +112,9 @@ class Formatter
return $value;
}
// Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc
$format = (string) preg_replace('/^\[\$-[^\]]*\]/', '', $format);
$format = (string) preg_replace_callback(
'/(["])(?:(?=(\\\\?))\\2.)*?\\1/u',
function ($matches) {

View File

@ -0,0 +1,30 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;
class Issue2885Test extends TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.2885.xlsx';
public function testIssue2885(): void
{
$filename = self::$testbook;
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('[$-809]0%', $sheet->getStyle('A1')->getNumberFormat()->getFormatCode());
$finishColumns = $sheet->getHighestColumn();
$rowsCount = $sheet->getHighestRow();
$rows = $sheet->rangeToArray("A1:{$finishColumns}{$rowsCount}");
self::assertSame('8%', $rows[0][0]);
$spreadsheet->disconnectWorksheets();
}
}

Binary file not shown.