Skip to content

Commit 7893942

Browse files
committed
Unallocated Cells Affected by Column/Row Insert/Delete
Fix #3923. Cells which have not yet been allocated cause problems when they need to be moved due to a column/row insert/delete. Code had been added in ReferenceHelper earlier to create missing cells, but only in the last data column. It needs to change to create the missing cells: - for columns, in rows 1 to "highest data row" for columns "before column" to "highest data column". - for rows, in columns A to "highest data column" for rows "before row" to "highest data row".
1 parent d620497 commit 7893942

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

src/PhpSpreadsheet/ReferenceHelper.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ public function insertNewBefore(
381381
}
382382

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

386386
// Clear cells if we are removing columns or rows
387387
$highestColumn = $worksheet->getHighestColumn();
@@ -399,17 +399,19 @@ public function insertNewBefore(
399399
$this->clearRowStrips($highestColumn, $beforeColumn, $beforeRow, $numberOfRows, $worksheet);
400400
}
401401

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

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)