Skip to content

Commit 09442da

Browse files
authored
Merge pull request #4536 from oleibman/issue347
Preserve 0x0a In Strings If Desired
2 parents b442eef + 8cbd7a2 commit 09442da

File tree

7 files changed

+318
-213
lines changed

7 files changed

+318
-213
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3535
- Recognize application/x-empty mimetype. [Issue #4521](https://github.com/PHPOffice/PhpSpreadsheet/issues/4521) [PR #4524](https://github.com/PHPOffice/PhpSpreadsheet/pull/4524)
3636
- Micro-optimization in getSheetByName. [PR #4499](https://github.com/PHPOffice/PhpSpreadsheet/pull/4499)
3737
- Bug in resizeMatricesExtend. [Issue #4451](https://github.com/PHPOffice/PhpSpreadsheet/issues/4451) [PR #4474](https://github.com/PHPOffice/PhpSpreadsheet/pull/4474)
38+
- Preserve 0x0a in Strings if Desired. [Issue #347](https://github.com/PHPOffice/PhpSpreadsheet/issues/347) [PR #4536](https://github.com/PHPOffice/PhpSpreadsheet/pull/4536)
3839

3940
## 2025-06-22 - 4.4.0
4041

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,14 @@ public function setValueExplicit(mixed $value, string $dataType = DataType::TYPE
278278
case DataType::TYPE_INLINE:
279279
// Rich text
280280
$value2 = StringHelper::convertToString($value, true);
281-
$this->value = DataType::checkString(($value instanceof RichText) ? $value : $value2);
281+
// Cells?->Worksheet?->Spreadsheet
282+
$binder = $this->parent?->getParent()?->getParent()?->getValueBinder();
283+
$preserveCr = false;
284+
if ($binder !== null && method_exists($binder, 'getPreserveCr')) {
285+
/** @var bool */
286+
$preserveCr = $binder->getPreserveCr();
287+
}
288+
$this->value = DataType::checkString(($value instanceof RichText) ? $value : $value2, $preserveCr);
282289

283290
break;
284291
case DataType::TYPE_NUMERIC:

src/PhpSpreadsheet/Cell/DataType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function getErrorCodes(): array
5353
*
5454
* @return RichText|string Sanitized value
5555
*/
56-
public static function checkString(null|RichText|string $textValue): RichText|string
56+
public static function checkString(null|RichText|string $textValue, bool $preserveCr = false): RichText|string
5757
{
5858
if ($textValue instanceof RichText) {
5959
// TODO: Sanitize Rich-Text string (max. character count is 32,767)
@@ -64,7 +64,9 @@ public static function checkString(null|RichText|string $textValue): RichText|st
6464
$textValue = StringHelper::substring((string) $textValue, 0, self::MAX_STRING_LENGTH);
6565

6666
// we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
67-
$textValue = str_replace(["\r\n", "\r"], "\n", $textValue);
67+
if (!$preserveCr) {
68+
$textValue = str_replace(["\r\n", "\r"], "\n", $textValue);
69+
}
6870

6971
return $textValue;
7072
}

src/PhpSpreadsheet/Cell/DefaultValueBinder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,18 @@ public static function dataTypeForValue(mixed $value): string
108108

109109
return DataType::TYPE_STRING;
110110
}
111+
112+
protected bool $preserveCr = false;
113+
114+
public function getPreserveCr(): bool
115+
{
116+
return $this->preserveCr;
117+
}
118+
119+
public function setPreserveCr(bool $preserveCr): self
120+
{
121+
$this->preserveCr = $preserveCr;
122+
123+
return $this;
124+
}
111125
}

src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads
2727

2828
// Initialisations
2929
$xls->spreadsheet = $this->newSpreadsheet();
30-
$xls->spreadsheet->setValueBinder($this->valueBinder);
30+
$xls->spreadsheet->setValueBinder($xls->valueBinder);
3131
$xls->spreadsheet->removeSheetByIndex(0); // remove 1st sheet
3232
if (!$xls->readDataOnly) {
3333
$xls->spreadsheet->removeCellStyleXfByIndex(0); // remove the default style

0 commit comments

Comments
 (0)