From a95e3f6607c5c1adc330ec493b6366c103681cdd Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 4 Mar 2015 23:36:38 +0000 Subject: [PATCH] Bugfix: Remove cells cleanly when calling RemoveRow() or RemoveColumn() --- .../CachedObjectStorage/CacheBase.php | 31 ++++++++++++++++++- Classes/PHPExcel/Worksheet.php | 14 +++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php index 9f780b2a..84cededf 100644 --- a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php +++ b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php @@ -206,7 +206,7 @@ abstract class PHPExcel_CachedObjectStorage_CacheBase { sscanf($coord,'%[A-Z]%d', $c, $r); $row[$r] = $r; $col[$c] = strlen($c).$c; - } + } if (!empty($row)) { // Determine highest column and row $highestRow = max($row); @@ -333,6 +333,35 @@ abstract class PHPExcel_CachedObjectStorage_CacheBase { } } // function copyCellCollection() + /** + * Remove a row, deleting all cells in that row + * + * @param string $row Row number to remove + * @return void + */ + public function removeRow($row) { + foreach ($this->getCellList() as $coord) { + sscanf($coord,'%[A-Z]%d', $c, $r); + if ($r == $row) { + $this->deleteCacheData($coord); + } + } + } + + /** + * Remove a column, deleting all cells in that column + * + * @param string $column Column ID to remove + * @return void + */ + public function removeColumn($column) { + foreach ($this->getCellList() as $coord) { + sscanf($coord,'%[A-Z]%d', $c, $r); + if ($c == $column) { + $this->deleteCacheData($coord); + } + } + } /** * Identify whether the caching method is currently available diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index 88ffa315..51edf4ab 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -2064,8 +2064,13 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ public function removeRow($pRow = 1, $pNumRows = 1) { if ($pRow >= 1) { + $highestRow = $this->getHighestDataRow(); $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance(); $objReferenceHelper->insertNewBefore('A' . ($pRow + $pNumRows), 0, -$pNumRows, $this); + for($r = 0; $r < $pNumRows; ++$r) { + $this->getCellCacheController()->removeRow($highestRow); + --$highestRow; + } } else { throw new PHPExcel_Exception("Rows to be deleted should at least start from row 1."); } @@ -2075,16 +2080,21 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable /** * Remove a column, updating all possible related data * - * @param int $pColumn Remove starting with this one - * @param int $pNumCols Number of columns to remove + * @param string $pColumn Remove starting with this one + * @param int $pNumCols Number of columns to remove * @throws PHPExcel_Exception * @return PHPExcel_Worksheet */ public function removeColumn($pColumn = 'A', $pNumCols = 1) { if (!is_numeric($pColumn)) { + $highestColumn = $this->getHighestDataColumn(); $pColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($pColumn) - 1 + $pNumCols); $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance(); $objReferenceHelper->insertNewBefore($pColumn . '1', -$pNumCols, 0, $this); + for($c = 0; $c < $pNumCols; ++$c) { + $this->getCellCacheController()->removeColumn($highestColumn); + $highestColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($highestColumn) - 2); + } } else { throw new PHPExcel_Exception("Column references should not be numeric."); }