Skip to content

Commit 1829dea

Browse files
authored
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
1 parent 11348a4 commit 1829dea

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public static function toFormattedString($value, $format, $callBack = null)
112112
return $value;
113113
}
114114

115+
// Ignore square-$-brackets prefix in format string, like "[$-411]ge.m.d", "[$-010419]0%", etc
116+
$format = (string) preg_replace('/^\[\$-[^\]]*\]/', '', $format);
117+
115118
$format = (string) preg_replace_callback(
116119
'/(["])(?:(?=(\\\\?))\\2.)*?\\1/u',
117120
function ($matches) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class Issue2885Test extends TestCase
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private static $testbook = 'tests/data/Reader/XLSX/issue.2885.xlsx';
14+
15+
public function testIssue2885(): void
16+
{
17+
$filename = self::$testbook;
18+
$reader = new Xlsx();
19+
$spreadsheet = $reader->load($filename);
20+
$sheet = $spreadsheet->getActiveSheet();
21+
self::assertSame('[$-809]0%', $sheet->getStyle('A1')->getNumberFormat()->getFormatCode());
22+
23+
$finishColumns = $sheet->getHighestColumn();
24+
$rowsCount = $sheet->getHighestRow();
25+
$rows = $sheet->rangeToArray("A1:{$finishColumns}{$rowsCount}");
26+
self::assertSame('8%', $rows[0][0]);
27+
28+
$spreadsheet->disconnectWorksheets();
29+
}
30+
}
5.31 KB
Binary file not shown.

0 commit comments

Comments
 (0)