Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Recognize application/x-empty mimetype. [Issue #4521](https://github.com/PHPOffice/PhpSpreadsheet/issues/4521) [PR #4524](https://github.com/PHPOffice/PhpSpreadsheet/pull/4524)
- Micro-optimization in getSheetByName. [PR #4499](https://github.com/PHPOffice/PhpSpreadsheet/pull/4499)
- Bug in resizeMatricesExtend. [Issue #4451](https://github.com/PHPOffice/PhpSpreadsheet/issues/4451) [PR #4474](https://github.com/PHPOffice/PhpSpreadsheet/pull/4474)
- Preserve 0x0a in Strings if Desired. [Issue #347](https://github.com/PHPOffice/PhpSpreadsheet/issues/347) [PR #4536](https://github.com/PHPOffice/PhpSpreadsheet/pull/4536)

## 2025-06-22 - 4.4.0

Expand Down
9 changes: 8 additions & 1 deletion src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,14 @@ public function setValueExplicit(mixed $value, string $dataType = DataType::TYPE
case DataType::TYPE_INLINE:
// Rich text
$value2 = StringHelper::convertToString($value, true);
$this->value = DataType::checkString(($value instanceof RichText) ? $value : $value2);
// Cells?->Worksheet?->Spreadsheet
$binder = $this->parent?->getParent()?->getParent()?->getValueBinder();
$preserveCr = false;
if ($binder !== null && method_exists($binder, 'getPreserveCr')) {
/** @var bool */
$preserveCr = $binder->getPreserveCr();
}
$this->value = DataType::checkString(($value instanceof RichText) ? $value : $value2, $preserveCr);

break;
case DataType::TYPE_NUMERIC:
Expand Down
6 changes: 4 additions & 2 deletions src/PhpSpreadsheet/Cell/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function getErrorCodes(): array
*
* @return RichText|string Sanitized value
*/
public static function checkString(null|RichText|string $textValue): RichText|string
public static function checkString(null|RichText|string $textValue, bool $preserveCr = false): RichText|string
{
if ($textValue instanceof RichText) {
// TODO: Sanitize Rich-Text string (max. character count is 32,767)
Expand All @@ -64,7 +64,9 @@ public static function checkString(null|RichText|string $textValue): RichText|st
$textValue = StringHelper::substring((string) $textValue, 0, self::MAX_STRING_LENGTH);

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

return $textValue;
}
Expand Down
14 changes: 14 additions & 0 deletions src/PhpSpreadsheet/Cell/DefaultValueBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,18 @@ public static function dataTypeForValue(mixed $value): string

return DataType::TYPE_STRING;
}

protected bool $preserveCr = false;

public function getPreserveCr(): bool
{
return $this->preserveCr;
}

public function setPreserveCr(bool $preserveCr): self
{
$this->preserveCr = $preserveCr;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Xls/LoadSpreadsheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function loadSpreadsheetFromFile2(string $filename, Xls $xls): Spreads

// Initialisations
$xls->spreadsheet = $this->newSpreadsheet();
$xls->spreadsheet->setValueBinder($this->valueBinder);
$xls->spreadsheet->setValueBinder($xls->valueBinder);
$xls->spreadsheet->removeSheetByIndex(0); // remove 1st sheet
if (!$xls->readDataOnly) {
$xls->spreadsheet->removeCellStyleXfByIndex(0); // remove the default style
Expand Down
Loading