Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 172 additions & 115 deletions src/PhpSpreadsheet/ReferenceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class ReferenceHelper
Expand Down Expand Up @@ -358,8 +359,12 @@ protected function adjustRowDimensions(Worksheet $worksheet, $beforeCellAddress,
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
* @param Worksheet $worksheet The worksheet that we're editing
*/
public function insertNewBefore($beforeCellAddress, $numberOfColumns, $numberOfRows, Worksheet $worksheet): void
{
public function insertNewBefore(
string $beforeCellAddress,
int $numberOfColumns,
int $numberOfRows,
Worksheet $worksheet
): void {
$remove = ($numberOfColumns < 0 || $numberOfRows < 0);
$allCoordinates = $worksheet->getCoordinates();

Expand All @@ -372,30 +377,12 @@ public function insertNewBefore($beforeCellAddress, $numberOfColumns, $numberOfR

// 1. Clear column strips if we are removing columns
if ($numberOfColumns < 0 && $beforeColumn - 2 + $numberOfColumns > 0) {
for ($i = 1; $i <= $highestRow - 1; ++$i) {
for ($j = $beforeColumn - 1 + $numberOfColumns; $j <= $beforeColumn - 2; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i;
$worksheet->removeConditionalStyles($coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
}
}
$this->clearColumnStrips($highestRow, $beforeColumn, $numberOfColumns, $worksheet);
}

// 2. Clear row strips if we are removing rows
if ($numberOfRows < 0 && $beforeRow - 1 + $numberOfRows > 0) {
for ($i = $beforeColumn - 1; $i <= Coordinate::columnIndexFromString($highestColumn) - 1; ++$i) {
for ($j = $beforeRow + $numberOfRows; $j <= $beforeRow - 1; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j;
$worksheet->removeConditionalStyles($coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
}
}
$this->clearRowStrips($highestColumn, $beforeColumn, $beforeRow, $numberOfRows, $worksheet);
}

// Find missing coordinates. This is important when inserting column before the last column
Expand Down Expand Up @@ -466,47 +453,11 @@ function ($coordinate) use ($allCoordinates) {
$highestRow = $worksheet->getHighestRow();

if ($numberOfColumns > 0 && $beforeColumn - 2 > 0) {
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
// Style
$coordinate = Coordinate::stringFromColumnIndex($beforeColumn - 1) . $i;
if ($worksheet->cellExists($coordinate)) {
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
$conditionalStyles = $worksheet->conditionalStylesExists($coordinate) ?
$worksheet->getConditionalStyles($coordinate) : false;
for ($j = $beforeColumn; $j <= $beforeColumn - 1 + $numberOfColumns; ++$j) {
$worksheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
if ($conditionalStyles) {
$cloned = [];
foreach ($conditionalStyles as $conditionalStyle) {
$cloned[] = clone $conditionalStyle;
}
$worksheet->setConditionalStyles(Coordinate::stringFromColumnIndex($j) . $i, $cloned);
}
}
}
}
$this->duplicateStylesByColumn($worksheet, $beforeColumn, $beforeRow, $highestRow, $numberOfColumns);
}

if ($numberOfRows > 0 && $beforeRow - 1 > 0) {
for ($i = $beforeColumn; $i <= Coordinate::columnIndexFromString($highestColumn); ++$i) {
// Style
$coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1);
if ($worksheet->cellExists($coordinate)) {
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
$conditionalStyles = $worksheet->conditionalStylesExists($coordinate) ?
$worksheet->getConditionalStyles($coordinate) : false;
for ($j = $beforeRow; $j <= $beforeRow - 1 + $numberOfRows; ++$j) {
$worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
if ($conditionalStyles) {
$cloned = [];
foreach ($conditionalStyles as $conditionalStyle) {
$cloned[] = clone $conditionalStyle;
}
$worksheet->setConditionalStyles(Coordinate::stringFromColumnIndex($i) . $j, $cloned);
}
}
}
}
$this->duplicateStylesByRow($worksheet, $beforeColumn, $beforeRow, $highestColumn, $numberOfRows);
}

// Update worksheet: column dimensions
Expand Down Expand Up @@ -534,59 +485,7 @@ function ($coordinate) use ($allCoordinates) {
$this->adjustProtectedCells($worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows);

// Update worksheet: autofilter
$autoFilter = $worksheet->getAutoFilter();
$autoFilterRange = $autoFilter->getRange();
if (!empty($autoFilterRange)) {
if ($numberOfColumns != 0) {
$autoFilterColumns = $autoFilter->getColumns();
if (count($autoFilterColumns) > 0) {
$column = '';
$row = 0;
sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row);
$columnIndex = Coordinate::columnIndexFromString($column);
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange);
if ($columnIndex <= $rangeEnd[0]) {
if ($numberOfColumns < 0) {
// If we're actually deleting any columns that fall within the autofilter range,
// then we delete any rules for those columns
$deleteColumn = $columnIndex + $numberOfColumns - 1;
$deleteCount = abs($numberOfColumns);
for ($i = 1; $i <= $deleteCount; ++$i) {
if (isset($autoFilterColumns[Coordinate::stringFromColumnIndex($deleteColumn + 1)])) {
$autoFilter->clearColumn(Coordinate::stringFromColumnIndex($deleteColumn + 1));
}
++$deleteColumn;
}
}
$startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0];

// Shuffle columns in autofilter range
if ($numberOfColumns > 0) {
$startColRef = $startCol;
$endColRef = $rangeEnd[0];
$toColRef = $rangeEnd[0] + $numberOfColumns;

do {
$autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef));
--$endColRef;
--$toColRef;
} while ($startColRef <= $endColRef);
} else {
// For delete, we shuffle from beginning to end to avoid overwriting
$startColID = Coordinate::stringFromColumnIndex($startCol);
$toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns);
$endColID = Coordinate::stringFromColumnIndex($rangeEnd[0] + 1);
do {
$autoFilter->shiftColumn($startColID, $toColID);
++$startColID;
++$toColID;
} while ($startColID != $endColID);
}
}
}
}
$worksheet->setAutoFilter($this->updateCellReference($autoFilterRange, $beforeCellAddress, $numberOfColumns, $numberOfRows));
}
$this->adjustAutoFilter($worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows);

// Update worksheet: freeze pane
if ($worksheet->getFreezePane()) {
Expand All @@ -601,7 +500,9 @@ function ($coordinate) use ($allCoordinates) {

// Page setup
if ($worksheet->getPageSetup()->isPrintAreaSet()) {
$worksheet->getPageSetup()->setPrintArea($this->updateCellReference($worksheet->getPageSetup()->getPrintArea(), $beforeCellAddress, $numberOfColumns, $numberOfRows));
$worksheet->getPageSetup()->setPrintArea(
$this->updateCellReference($worksheet->getPageSetup()->getPrintArea(), $beforeCellAddress, $numberOfColumns, $numberOfRows)
);
}

// Update worksheet: drawings
Expand Down Expand Up @@ -948,7 +849,7 @@ public function updateNamedFormulas(Spreadsheet $spreadsheet, $oldName = '', $ne
foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
foreach ($sheet->getCoordinates(false) as $coordinate) {
$cell = $sheet->getCell($coordinate);
if (($cell !== null) && ($cell->getDataType() == DataType::TYPE_FORMULA)) {
if (($cell !== null) && ($cell->getDataType() === DataType::TYPE_FORMULA)) {
$formula = $cell->getValue();
if (strpos($formula, $oldName) !== false) {
$formula = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $formula);
Expand Down Expand Up @@ -1038,6 +939,162 @@ private function updateSingleCellReference($cellReference = 'A1', $beforeCellAdd
return $newColumn . $newRow;
}

private function clearColumnStrips(int $highestRow, int $beforeColumn, int $numberOfColumns, Worksheet $worksheet): void
{
for ($i = 1; $i <= $highestRow - 1; ++$i) {
for ($j = $beforeColumn - 1 + $numberOfColumns; $j <= $beforeColumn - 2; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i;
$worksheet->removeConditionalStyles($coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
}
}
}

private function clearRowStrips(string $highestColumn, int $beforeColumn, int $beforeRow, int $numberOfRows, Worksheet $worksheet): void
{
$lastColumnIndex = Coordinate::columnIndexFromString($highestColumn) - 1;

for ($i = $beforeColumn - 1; $i <= $lastColumnIndex; ++$i) {
for ($j = $beforeRow + $numberOfRows; $j <= $beforeRow - 1; ++$j) {
$coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j;
$worksheet->removeConditionalStyles($coordinate);
if ($worksheet->cellExists($coordinate)) {
$worksheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL);
$worksheet->getCell($coordinate)->setXfIndex(0);
}
}
}
}

private function adjustAutoFilter(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void
{
$autoFilter = $worksheet->getAutoFilter();
$autoFilterRange = $autoFilter->getRange();
if (!empty($autoFilterRange)) {
if ($numberOfColumns !== 0) {
$autoFilterColumns = $autoFilter->getColumns();
if (count($autoFilterColumns) > 0) {
$column = '';
$row = 0;
sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row);
$columnIndex = Coordinate::columnIndexFromString($column);
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange);
if ($columnIndex <= $rangeEnd[0]) {
if ($numberOfColumns < 0) {
$this->adjustAutoFilterDeleteRules($columnIndex, $numberOfColumns, $autoFilterColumns, $autoFilter);
}
$startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0];

// Shuffle columns in autofilter range
if ($numberOfColumns > 0) {
$this->adjustAutoFilterInsert($startCol, $numberOfColumns, $rangeEnd[0], $autoFilter);
} else {
$this->adjustAutoFilterDelete($startCol, $numberOfColumns, $rangeEnd[0], $autoFilter);
}
}
}
}

$worksheet->setAutoFilter(
$this->updateCellReference($autoFilterRange, $beforeCellAddress, $numberOfColumns, $numberOfRows)
);
}
}

private function adjustAutoFilterDeleteRules(int $columnIndex, int $numberOfColumns, array $autoFilterColumns, AutoFilter $autoFilter): void
{
// If we're actually deleting any columns that fall within the autofilter range,
// then we delete any rules for those columns
$deleteColumn = $columnIndex + $numberOfColumns - 1;
$deleteCount = abs($numberOfColumns);

for ($i = 1; $i <= $deleteCount; ++$i) {
$columnName = Coordinate::stringFromColumnIndex($deleteColumn + 1);
if (isset($autoFilterColumns[$columnName])) {
$autoFilter->clearColumn($columnName);
}
++$deleteColumn;
}
}

private function adjustAutoFilterInsert(int $startCol, int $numberOfColumns, int $rangeEnd, AutoFilter $autoFilter): void
{
$startColRef = $startCol;
$endColRef = $rangeEnd;
$toColRef = $rangeEnd + $numberOfColumns;

do {
$autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef));
--$endColRef;
--$toColRef;
} while ($startColRef <= $endColRef);
}

private function adjustAutoFilterDelete(int $startCol, int $numberOfColumns, int $rangeEnd, AutoFilter $autoFilter): void
{
// For delete, we shuffle from beginning to end to avoid overwriting
$startColID = Coordinate::stringFromColumnIndex($startCol);
$toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns);
$endColID = Coordinate::stringFromColumnIndex($rangeEnd + 1);

do {
$autoFilter->shiftColumn($startColID, $toColID);
++$startColID;
++$toColID;
} while ($startColID !== $endColID);
}

private function duplicateStylesByColumn(Worksheet $worksheet, int $beforeColumn, int $beforeRow, int $highestRow, int $numberOfColumns): void
{
$beforeColumnName = Coordinate::stringFromColumnIndex($beforeColumn - 1);
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) {
// Style
$coordinate = $beforeColumnName . $i;
if ($worksheet->cellExists($coordinate)) {
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
$conditionalStyles = $worksheet->conditionalStylesExists($coordinate) ?
$worksheet->getConditionalStyles($coordinate) : false;
for ($j = $beforeColumn; $j <= $beforeColumn - 1 + $numberOfColumns; ++$j) {
$worksheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex);
if ($conditionalStyles) {
$cloned = [];
foreach ($conditionalStyles as $conditionalStyle) {
$cloned[] = clone $conditionalStyle;
}
$worksheet->setConditionalStyles(Coordinate::stringFromColumnIndex($j) . $i, $cloned);
}
}
}
}
}

private function duplicateStylesByRow(Worksheet $worksheet, int $beforeColumn, int $beforeRow, string $highestColumn, int $numberOfRows): void
{
$highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
for ($i = $beforeColumn; $i <= $highestColumnIndex; ++$i) {
// Style
$coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1);
if ($worksheet->cellExists($coordinate)) {
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
$conditionalStyles = $worksheet->conditionalStylesExists($coordinate) ?
$worksheet->getConditionalStyles($coordinate) : false;
for ($j = $beforeRow; $j <= $beforeRow - 1 + $numberOfRows; ++$j) {
$worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
if ($conditionalStyles) {
$cloned = [];
foreach ($conditionalStyles as $conditionalStyle) {
$cloned[] = clone $conditionalStyle;
}
$worksheet->setConditionalStyles(Coordinate::stringFromColumnIndex($i) . $j, $cloned);
}
}
}
}
}

/**
* __clone implementation. Cloning should not be allowed in a Singleton!
*/
Expand Down
12 changes: 6 additions & 6 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2219,25 +2219,25 @@ public function removeColumn($column, $numberOfColumns = 1)
$highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
$pColumnIndex = Coordinate::columnIndexFromString($column);

if ($pColumnIndex > $highestColumnIndex) {
return $this;
}

$holdColumnDimensions = $this->removeColumnDimensions($pColumnIndex, $numberOfColumns);

$column = Coordinate::stringFromColumnIndex($pColumnIndex + $numberOfColumns);
$objReferenceHelper = ReferenceHelper::getInstance();
$objReferenceHelper->insertNewBefore($column . '1', -$numberOfColumns, 0, $this);

$this->columnDimensions = $holdColumnDimensions;

if ($pColumnIndex > $highestColumnIndex) {
return $this;
}

$maxPossibleColumnsToBeRemoved = $highestColumnIndex - $pColumnIndex + 1;

for ($c = 0, $n = min($maxPossibleColumnsToBeRemoved, $numberOfColumns); $c < $n; ++$c) {
$this->getCellCollection()->removeColumn($highestColumn);
$highestColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($highestColumn) - 1);
}

$this->columnDimensions = $holdColumnDimensions;

$this->garbageCollect();

return $this;
Expand Down
Loading