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".
This commit is contained in:
oleibman
2024-03-09 11:05:50 -08:00
parent d620497511
commit 7893942a72
2 changed files with 54 additions and 12 deletions
+14 -12
View File
@@ -381,7 +381,7 @@ class ReferenceHelper
}
// Get coordinate of $beforeCellAddress
[$beforeColumn, $beforeRow] = Coordinate::indexesFromString($beforeCellAddress);
[$beforeColumn, $beforeRow, $beforeColumnString] = Coordinate::indexesFromString($beforeCellAddress);
// Clear cells if we are removing columns or rows
$highestColumn = $worksheet->getHighestColumn();
@@ -399,17 +399,19 @@ class ReferenceHelper
$this->clearRowStrips($highestColumn, $beforeColumn, $beforeRow, $numberOfRows, $worksheet);
}
// Find missing coordinates. This is important when inserting column before the last column
$cellCollection = $worksheet->getCellCollection();
$missingCoordinates = array_filter(
array_map(fn ($row): string => "{$highestDataColumn}{$row}", range(1, $highestDataRow)),
fn ($coordinate): bool => $cellCollection->has($coordinate) === false
);
// Create missing cells with null values
if (!empty($missingCoordinates)) {
foreach ($missingCoordinates as $coordinate) {
$worksheet->createNewCell($coordinate);
// Find missing coordinates. This is important when inserting or deleting column before the last column
$startRow = $startCol = 1;
$startColString = 'A';
if ($numberOfRows === 0) {
$startCol = $beforeColumn;
$startColString = $beforeColumnString;
} elseif ($numberOfColumns === 0) {
$startRow = $beforeRow;
}
$highColumn = Coordinate::columnIndexFromString($highestDataColumn);
for ($row = $startRow; $row <= $highestDataRow; ++$row) {
for ($col = $startCol, $colString = $startColString; $col <= $highColumn; ++$col, ++$colString) {
$worksheet->getCell("$colString$row"); // create cell if it doesn't exist
}
}
@@ -252,6 +252,27 @@ class WorksheetTest extends TestCase
],
'A',
],
'Data includes nulls' => [
[
['A1', 'B1', 'C1', 'D1', 'E1'],
[null, 'B2', 'C2', 'D2', 'E2'],
['A3', null, 'C3', 'D3', 'E3'],
['A4', 'B4', null, 'D4', 'E4'],
['A5', 'B5', 'C5', null, 'E5'],
['A6', 'B6', 'C6', 'D6', null],
],
'B',
2,
[
['A1', 'D1', 'E1'],
[null, 'D2', 'E2'],
['A3', 'D3', 'E3'],
['A4', 'D4', 'E4'],
['A5', null, 'E5'],
['A6', 'D6', null],
],
'C',
],
];
}
@@ -384,6 +405,25 @@ class WorksheetTest extends TestCase
],
4,
],
'Data includes nulls' => [
[
['A1', 'B1', 'C1', 'D1', 'E1'],
[null, 'B2', 'C2', 'D2', 'E2'],
['A3', null, 'C3', 'D3', 'E3'],
['A4', 'B4', null, 'D4', 'E4'],
['A5', 'B5', 'C5', null, 'E5'],
['A6', 'B6', 'C6', 'D6', null],
],
1,
2,
[
['A3', null, 'C3', 'D3', 'E3'],
['A4', 'B4', null, 'D4', 'E4'],
['A5', 'B5', 'C5', null, 'E5'],
['A6', 'B6', 'C6', 'D6', null],
],
4,
],
];
}