From 2f4620c3f95062c830d7dd41065d6f35ab980db2 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 29 Aug 2012 00:24:13 +0100 Subject: [PATCH] Improve cloning of worksheet to handle autoFilter area cleanly --- Classes/PHPExcel/Worksheet.php | 4 ++++ Classes/PHPExcel/Worksheet/AutoFilter.php | 20 +++++++++---------- .../PHPExcel/Worksheet/AutoFilter/Column.php | 2 ++ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index 64256dc1..e3bcd06f 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -2777,6 +2777,10 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable } elseif ($key == '_drawingCollection') { $newCollection = clone $this->_drawingCollection; $this->_drawingCollection = $newCollection; + } elseif (($key == '_autoFilter') && (is_a($this->_autoFilter,'PHPExcel_Worksheet_AutoFilter'))) { + $newAutoFilter = clone $this->_autoFilter; + $this->_autoFilter = $newAutoFilter; + $this->_autoFilter->setParent($this); } else { $this->{$key} = unserialize(serialize($val)); } diff --git a/Classes/PHPExcel/Worksheet/AutoFilter.php b/Classes/PHPExcel/Worksheet/AutoFilter.php index e84ff212..08f5df2b 100644 --- a/Classes/PHPExcel/Worksheet/AutoFilter.php +++ b/Classes/PHPExcel/Worksheet/AutoFilter.php @@ -587,7 +587,7 @@ class PHPExcel_Worksheet_AutoFilter list($rangeStart,$rangeEnd) = PHPExcel_Cell::rangeBoundaries($this->_range); // The heading row should always be visible - echo 'AutoFilter Heading Row ',$rangeStart[1],' is always SHOWN',PHP_EOL; +// echo 'AutoFilter Heading Row ',$rangeStart[1],' is always SHOWN',PHP_EOL; $this->_workSheet->getRowDimension($rangeStart[1])->setVisible(TRUE); $columnFilterTests = array(); @@ -763,17 +763,17 @@ class PHPExcel_Worksheet_AutoFilter } } - echo 'Column Filter Test CRITERIA',PHP_EOL; - var_dump($columnFilterTests); - +// echo 'Column Filter Test CRITERIA',PHP_EOL; +// var_dump($columnFilterTests); +// // Execute the column tests for each row in the autoFilter range to determine show/hide, for ($row = $rangeStart[1]+1; $row <= $rangeEnd[1]; ++$row) { - echo 'Testing Row = ',$row,PHP_EOL; +// echo 'Testing Row = ',$row,PHP_EOL; $result = TRUE; foreach($columnFilterTests as $columnID => $columnFilterTest) { - echo 'Testing cell ',$columnID.$row,PHP_EOL; +// echo 'Testing cell ',$columnID.$row,PHP_EOL; $cellValue = $this->_workSheet->getCell($columnID.$row)->getCalculatedValue(); - echo 'Value is ',$cellValue,PHP_EOL; +// echo 'Value is ',$cellValue,PHP_EOL; // Execute the filter test $result = $result && call_user_func_array( @@ -783,13 +783,13 @@ class PHPExcel_Worksheet_AutoFilter $columnFilterTest['arguments'] ) ); - echo (($result) ? 'VALID' : 'INVALID'),PHP_EOL; +// echo (($result) ? 'VALID' : 'INVALID'),PHP_EOL; // If filter test has resulted in FALSE, exit the loop straightaway rather than running any more tests if (!$result) break; } // Set show/hide for the row based on the result of the autoFilter result - echo (($result) ? 'SHOW' : 'HIDE'),PHP_EOL; +// echo (($result) ? 'SHOW' : 'HIDE'),PHP_EOL; $this->_workSheet->getRowDimension($row)->setVisible($result); } @@ -815,7 +815,7 @@ class PHPExcel_Worksheet_AutoFilter $this->$key = array(); foreach ($value as $k => $v) { $this->$key[$k] = clone $v; - // attache the new cloned Column to this new cloned Autofilter object + // attach the new cloned Column to this new cloned Autofilter object $this->$key[$k]->setParent($this); } } else { diff --git a/Classes/PHPExcel/Worksheet/AutoFilter/Column.php b/Classes/PHPExcel/Worksheet/AutoFilter/Column.php index d7b866b2..d41c3ed5 100644 --- a/Classes/PHPExcel/Worksheet/AutoFilter/Column.php +++ b/Classes/PHPExcel/Worksheet/AutoFilter/Column.php @@ -369,6 +369,8 @@ class PHPExcel_Worksheet_AutoFilter_Column $this->$key = array(); foreach ($value as $k => $v) { $this->$key[$k] = clone $v; + // attach the new cloned Rule to this new cloned Autofilter Cloned object + $this->$key[$k]->setParent($this); } } else { $this->$key = $value;