Skip to content

Commit 2a8345d

Browse files
authored
Merge pull request #3940 from oleibman/issue3923
Unallocated Cells Affected by Column/Row Insert/Delete
2 parents 9b3f205 + fbcd68c commit 2a8345d

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1010
### Added
1111

1212
- Default Style Alignment Property (workaround for bug in non-Excel spreadsheet apps) [Issue #3918](https://github.com/PHPOffice/PhpSpreadsheet/issues/3918) [PR #3924](https://github.com/PHPOffice/PhpSpreadsheet/pull/3924)
13+
- Additional Support for Date/Time Styles [PR #3939](https://github.com/PHPOffice/PhpSpreadsheet/pull/3939)
1314

1415
### Changed
1516

@@ -33,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3334
- Incorrect SUMPRODUCT Calculation [Issue #3909](https://github.com/PHPOffice/PhpSpreadsheet/issues/3909) [PR #3916](https://github.com/PHPOffice/PhpSpreadsheet/pull/3916)
3435
- Formula Misidentifying Text as Cell After Insertion/Deletion [Issue #3907](https://github.com/PHPOffice/PhpSpreadsheet/issues/3907) [PR #3915](https://github.com/PHPOffice/PhpSpreadsheet/pull/3915)
3536
- Unexpected Absolute Address in Xlsx Rels [Issue #3730](https://github.com/PHPOffice/PhpSpreadsheet/issues/3730) [PR #3923](https://github.com/PHPOffice/PhpSpreadsheet/pull/3923)
37+
- Unallocated Cells Affected by Column/Row Insert/Delete [Issue #3933](https://github.com/PHPOffice/PhpSpreadsheet/issues/3933) [PR #3940](https://github.com/PHPOffice/PhpSpreadsheet/pull/3940)
3638

3739
## 2.0.0 - 2024-01-04
3840

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public function insertNewBefore(
383383
}
384384

385385
// Get coordinate of $beforeCellAddress
386-
[$beforeColumn, $beforeRow] = Coordinate::indexesFromString($beforeCellAddress);
386+
[$beforeColumn, $beforeRow, $beforeColumnString] = Coordinate::indexesFromString($beforeCellAddress);
387387

388388
// Clear cells if we are removing columns or rows
389389
$highestColumn = $worksheet->getHighestColumn();
@@ -401,17 +401,19 @@ public function insertNewBefore(
401401
$this->clearRowStrips($highestColumn, $beforeColumn, $beforeRow, $numberOfRows, $worksheet);
402402
}
403403

404-
// Find missing coordinates. This is important when inserting column before the last column
405-
$cellCollection = $worksheet->getCellCollection();
406-
$missingCoordinates = array_filter(
407-
array_map(fn ($row): string => "{$highestDataColumn}{$row}", range(1, $highestDataRow)),
408-
fn ($coordinate): bool => $cellCollection->has($coordinate) === false
409-
);
410-
411-
// Create missing cells with null values
412-
if (!empty($missingCoordinates)) {
413-
foreach ($missingCoordinates as $coordinate) {
414-
$worksheet->createNewCell($coordinate);
404+
// Find missing coordinates. This is important when inserting or deleting column before the last column
405+
$startRow = $startCol = 1;
406+
$startColString = 'A';
407+
if ($numberOfRows === 0) {
408+
$startCol = $beforeColumn;
409+
$startColString = $beforeColumnString;
410+
} elseif ($numberOfColumns === 0) {
411+
$startRow = $beforeRow;
412+
}
413+
$highColumn = Coordinate::columnIndexFromString($highestDataColumn);
414+
for ($row = $startRow; $row <= $highestDataRow; ++$row) {
415+
for ($col = $startCol, $colString = $startColString; $col <= $highColumn; ++$col, ++$colString) {
416+
$worksheet->getCell("$colString$row"); // create cell if it doesn't exist
415417
}
416418
}
417419

tests/PhpSpreadsheetTests/Worksheet/WorksheetTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,27 @@ public static function removeColumnProvider(): array
252252
],
253253
'A',
254254
],
255+
'Data includes nulls' => [
256+
[
257+
['A1', 'B1', 'C1', 'D1', 'E1'],
258+
[null, 'B2', 'C2', 'D2', 'E2'],
259+
['A3', null, 'C3', 'D3', 'E3'],
260+
['A4', 'B4', null, 'D4', 'E4'],
261+
['A5', 'B5', 'C5', null, 'E5'],
262+
['A6', 'B6', 'C6', 'D6', null],
263+
],
264+
'B',
265+
2,
266+
[
267+
['A1', 'D1', 'E1'],
268+
[null, 'D2', 'E2'],
269+
['A3', 'D3', 'E3'],
270+
['A4', 'D4', 'E4'],
271+
['A5', null, 'E5'],
272+
['A6', 'D6', null],
273+
],
274+
'C',
275+
],
255276
];
256277
}
257278

@@ -384,6 +405,25 @@ public static function removeRowsProvider(): array
384405
],
385406
4,
386407
],
408+
'Data includes nulls' => [
409+
[
410+
['A1', 'B1', 'C1', 'D1', 'E1'],
411+
[null, 'B2', 'C2', 'D2', 'E2'],
412+
['A3', null, 'C3', 'D3', 'E3'],
413+
['A4', 'B4', null, 'D4', 'E4'],
414+
['A5', 'B5', 'C5', null, 'E5'],
415+
['A6', 'B6', 'C6', 'D6', null],
416+
],
417+
1,
418+
2,
419+
[
420+
['A3', null, 'C3', 'D3', 'E3'],
421+
['A4', 'B4', null, 'D4', 'E4'],
422+
['A5', 'B5', 'C5', null, 'E5'],
423+
['A6', 'B6', 'C6', 'D6', null],
424+
],
425+
4,
426+
],
387427
];
388428
}
389429

0 commit comments

Comments
 (0)