Skip to content

Commit

Permalink
Ignore cell formatting when the format is a single @
Browse files Browse the repository at this point in the history
This commit fix two issues that happened when the a cell was formatted as text
* When the cell contains a number prefixed with dollar sign, this number is getting replaced with 0. The replacement happens due to the preg_replace function.
* When the cell contains quotes, the quote would be removed.
  • Loading branch information
sirbaconjr committed Nov 25, 2024
1 parent fd562af commit a726ff9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Fixed

- Nothing yet.
- Ignore cell formatting when the format is a single @. [Issue #4242](https://github.com/PHPOffice/PhpSpreadsheet/issues/4242) [PR #4243](https://github.com/PHPOffice/PhpSpreadsheet/pull/4243)

## 2024-11-22 - 3.5.0

Expand Down
10 changes: 7 additions & 3 deletions src/PhpSpreadsheet/Style/NumberFormat/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,13 @@ public static function toFormattedString($value, string $format, ?array $callBac
return $value ? Calculation::getTRUE() : Calculation::getFALSE();
}
// For now we do not treat strings in sections, although section 4 of a format code affects strings
// Process a single block format code containing @ for text substitution
if (preg_match(self::SECTION_SPLIT, $format) === 0 && preg_match(self::SYMBOL_AT, $format) === 1) {
return str_replace('"', '', preg_replace(self::SYMBOL_AT, (string) $value, $format) ?? '');
// Process a single block format code containing @ for text substitution, but ignore if the whole format is @
if (
$format != '@'
&& preg_match(self::SECTION_SPLIT, $format) === 0
&& preg_match(self::SYMBOL_AT, $format) === 1
) {
return str_replace('"', '', preg_replace(self::SYMBOL_AT, $value, $format) ?? '');
}

// If we have a text value, return it "as is"
Expand Down
43 changes: 43 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/ToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PHPUnit\Framework\TestCase;

class ToArrayTest extends TestCase
Expand Down Expand Up @@ -76,4 +77,46 @@ public static function testMaxCol(): void
self::assertSame('start', $array[0][0]);
self::assertSame('end', $array[0][16383]);
}

public static function testDollarSign(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$inputArray = [
['General', '$0', 'Value is $100', '200 number - $200 value'],
['Text', '$0', 'Value is $100', '200 number - $200 value'],
];

$sheet->fromArray($inputArray, null, 'A1', true);

$sheet->getStyle('A2:D2')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);

$expected = [
['General', '$0', 'Value is $100', '200 number - $200 value'],
['Text', '$0', 'Value is $100', '200 number - $200 value'],
];
$result = $sheet->rangeToArray('A1:D2');
self::assertSame($expected, $result);
}

public static function testQuotes(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$inputArray = [
['General', '"'],
['Text', '"'],
];

$sheet->fromArray($inputArray, null, 'A1', true);

$sheet->getStyle('A2:B2')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_TEXT);

$expected = [
['General', '"'],
['Text', '"'],
];
$result = $sheet->rangeToArray('A1:B2');
self::assertSame($expected, $result);
}
}

0 comments on commit a726ff9

Please sign in to comment.