diff --git a/CHANGELOG.md b/CHANGELOG.md index 60d0eede..b28a0c99 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Fix handling of named ranges referencing sheets with spaces or "!" in their title - Cover `getSheetByName()` with tests for name with quote and spaces - [#739](https://github.com/PHPOffice/PhpSpreadsheet/issues/739) - Best effort to support invalid colspan values in HTML reader - [878](https://github.com/PHPOffice/PhpSpreadsheet/pull/878) +- Fixes incorrect rows deletion [#868](https://github.com/PHPOffice/PhpSpreadsheet/issues/868) ## [1.8.2] - 2019-07-08 diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 689c547e..a485f405 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -2115,6 +2115,10 @@ class Worksheet implements IComparable public function removeRow($pRow, $pNumRows = 1) { if ($pRow >= 1) { + for ($r = 0; $r < $pNumRows; ++$r) { + $this->getCellCollection()->removeRow($pRow + $r); + } + $highestRow = $this->getHighestDataRow(); $objReferenceHelper = ReferenceHelper::getInstance(); $objReferenceHelper->insertNewBefore('A' . ($pRow + $pNumRows), 0, -$pNumRows, $this); diff --git a/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php b/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php index 5e8bbcb2..eb2aa200 100644 --- a/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php +++ b/tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php @@ -161,4 +161,24 @@ class WorksheetTest extends TestCase self::assertSame($expectTitle, $arRange[0]); self::assertSame($expectCell2, $arRange[1]); } + + /** + * Fix https://github.com/PHPOffice/PhpSpreadsheet/issues/868 when cells are not removed correctly + * on row deletion. + */ + public function testRemoveCellsCorrectlyWhenRemovingRow() + { + $workbook = new Spreadsheet(); + $worksheet = $workbook->getActiveSheet(); + $worksheet->getCell('A2')->setValue('A2'); + $worksheet->getCell('C1')->setValue('C1'); + $worksheet->removeRow(1); + $this->assertEquals( + 'A2', + $worksheet->getCell('A1')->getValue() + ); + $this->assertNull( + $worksheet->getCell('C1')->getValue() + ); + } }