Respect DataType in insertNewBefore (#2433)

`ReferenceHelper::insertNewBefore` copies data from one cell to another when adding/removing rows or columns.
It now also respects the data type set for that cell and does not use value binder again.
This commit is contained in:
Michael Fürnschuß 2021-12-04 16:31:38 +00:00 committed by GitHub
parent e01a81ec5e
commit aa91abc0d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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

View File

@ -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);
}
}