diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index ba4eefb0..0d72b305 100644 --- a/src/PhpSpreadsheet/ReferenceHelper.php +++ b/src/PhpSpreadsheet/ReferenceHelper.php @@ -426,7 +426,7 @@ class ReferenceHelper ->setValue($this->updateFormulaReferences($cell->getValue(), $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle())); } else { // Formula should not be adjusted - $worksheet->getCell($newCoordinate)->setValue($cell->getValue()); + $worksheet->getCell($newCoordinate)->setValueExplicit($cell->getValue(), $cell->getDataType()); } // Clear the original cell diff --git a/tests/PhpSpreadsheetTests/ReferenceHelperTest.php b/tests/PhpSpreadsheetTests/ReferenceHelperTest.php index bf32f746..874dbfb5 100644 --- a/tests/PhpSpreadsheetTests/ReferenceHelperTest.php +++ b/tests/PhpSpreadsheetTests/ReferenceHelperTest.php @@ -2,7 +2,9 @@ namespace PhpOffice\PhpSpreadsheetTests; +use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\ReferenceHelper; +use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; class ReferenceHelperTest extends TestCase @@ -129,4 +131,22 @@ class ReferenceHelperTest extends TestCase { return require 'tests/data/ReferenceHelperFormulaUpdatesMultipleSheet.php'; } + + public function testInsertNewBeforeRetainDataType(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell = $sheet->getCell('A1'); + $cell->setValueExplicit('+1', DataType::TYPE_STRING); + $oldDataType = $cell->getDataType(); + $oldValue = $cell->getValue(); + + $sheet->insertNewRowBefore(1); + $newCell = $sheet->getCell('A2'); + $newDataType = $newCell->getDataType(); + $newValue = $newCell->getValue(); + + self::assertSame($oldValue, $newValue); + self::assertSame($oldDataType, $newDataType); + } }