Skip to content

Commit 250394d

Browse files
committed
Preserve 0x0a In Strings If Desired
Fix #347, which had gone stale but is now reopened. PhpSpreadsheet is automatically replacing CR-LF and CR with LF. The reason for this is unknown, although I suggest a possibility in the issue. I am not willing to make this a breaking change. This PR adds a new property `preserveCr` with setter and getter to DefaultValueBinder (and, by extension, to StringValueBinder and AdvancedValueBinder). The property defaults to false, but users who wish to preserve CR-LF and CR can set it to true for a Spreadsheet or a Reader.
1 parent 747ccd1 commit 250394d

File tree

6 files changed

+317
-213
lines changed

6 files changed

+317
-213
lines changed

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)