From 6ae1a501c5cf0a821f155c382118340e3a6e2c35 Mon Sep 17 00:00:00 2001 From: Matthias Laug Date: Thu, 21 Feb 2013 14:35:59 +0100 Subject: [PATCH 1/4] Fatal Error on non object in Worksheet.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the disconnectCells method is not always fully initialized with the attributeĀ  _cellCollection, so a check needs to be done. Otherwise a fatal error occurs. happens if multiple xsl are created from csv file --- Classes/PHPExcel/Worksheet.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index c8b70fe1..c9830fc0 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -378,9 +378,10 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ public function disconnectCells() { - $this->_cellCollection->unsetWorksheetCells(); - $this->_cellCollection = NULL; - + if ( $this->_cellCollection != null ){ + $this->_cellCollection->unsetWorksheetCells(); + $this->_cellCollection = NULL; + } // detach ourself from the workbook, so that it can then delete this worksheet successfully $this->_parent = null; } From 7b922bbf77c7d4d9e5c39ea1ee2e3de502e10d6c Mon Sep 17 00:00:00 2001 From: Dominik Bonsch Date: Thu, 21 Feb 2013 19:34:41 +0100 Subject: [PATCH 2/4] changed NULL to null --- Classes/PHPExcel/Worksheet.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index c9830fc0..738d1674 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -204,7 +204,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable * * @var PHPExcel_Worksheet_AutoFilter */ - private $_autoFilter = NULL; + private $_autoFilter = null; /** * Freeze pane @@ -362,12 +362,12 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable $this->_protection = new PHPExcel_Worksheet_Protection(); // Default row dimension - $this->_defaultRowDimension = new PHPExcel_Worksheet_RowDimension(NULL); + $this->_defaultRowDimension = new PHPExcel_Worksheet_RowDimension(null); // Default column dimension - $this->_defaultColumnDimension = new PHPExcel_Worksheet_ColumnDimension(NULL); + $this->_defaultColumnDimension = new PHPExcel_Worksheet_ColumnDimension(null); - $this->_autoFilter = new PHPExcel_Worksheet_AutoFilter(NULL, $this); + $this->_autoFilter = new PHPExcel_Worksheet_AutoFilter(null, $this); } @@ -380,7 +380,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable { if ( $this->_cellCollection != null ){ $this->_cellCollection->unsetWorksheetCells(); - $this->_cellCollection = NULL; + $this->_cellCollection = null; } // detach ourself from the workbook, so that it can then delete this worksheet successfully $this->_parent = null; @@ -391,7 +391,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable * */ function __destruct() { - if ($this->_cellCollection !== NULL) { + if ($this->_cellCollection !== null) { $this->disconnectCells(); } } @@ -451,7 +451,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // Re-order cell collection return $this->sortCellCollection(); } - if ($this->_cellCollection !== NULL) { + if ($this->_cellCollection !== null) { return $this->_cellCollection->getCellList(); } return array(); @@ -464,7 +464,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ public function sortCellCollection() { - if ($this->_cellCollection !== NULL) { + if ($this->_cellCollection !== null) { return $this->_cellCollection->getSortedCellList(); } return array(); @@ -1117,7 +1117,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $pCoordinate, $matches)) && (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $pCoordinate, $matches))) { $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this); - if ($namedRange !== NULL) { + if ($namedRange !== null) { $pCoordinate = $namedRange->getRange(); return $namedRange->getWorksheet()->getCell($pCoordinate); } @@ -1136,7 +1136,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // Coordinates $aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate); - $cell = $this->_cellCollection->addCacheData($pCoordinate,new PHPExcel_Cell($pCoordinate, NULL, PHPExcel_Cell_DataType::TYPE_NULL, $this)); + $cell = $this->_cellCollection->addCacheData($pCoordinate,new PHPExcel_Cell($pCoordinate, null, PHPExcel_Cell_DataType::TYPE_NULL, $this)); $this->_cellCollectionIsSorted = false; if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < PHPExcel_Cell::columnIndexFromString($aCoordinates[0])) @@ -1176,7 +1176,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable $coordinate = $columnLetter . $pRow; if (!$this->_cellCollection->isDataSet($coordinate)) { - $cell = $this->_cellCollection->addCacheData($coordinate, new PHPExcel_Cell($coordinate, NULL, PHPExcel_Cell_DataType::TYPE_NULL, $this)); + $cell = $this->_cellCollection->addCacheData($coordinate, new PHPExcel_Cell($coordinate, null, PHPExcel_Cell_DataType::TYPE_NULL, $this)); $this->_cellCollectionIsSorted = false; if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < $pColumn) @@ -1209,7 +1209,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $pCoordinate, $matches)) && (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $pCoordinate, $matches))) { $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this); - if ($namedRange !== NULL) { + if ($namedRange !== null) { $pCoordinate = $namedRange->getRange(); if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) { if (!$namedRange->getLocalOnly()) { @@ -1899,7 +1899,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ public function removeAutoFilter() { - $this->_autoFilter->setRange(NULL); + $this->_autoFilter->setRange(null); return $this; } @@ -2442,7 +2442,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable ); } } else { - // Cell holds a NULL + // Cell holds a null $returnValue[$rRef][$cRef] = $nullValue; } } else { @@ -2475,7 +2475,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable ) { $namedRange = PHPExcel_NamedRange::resolveRange($pNamedRange, $this); - if ($namedRange !== NULL) { + if ($namedRange !== null) { $pWorkSheet = $namedRange->getWorksheet(); $pCellRange = $namedRange->getRange(); @@ -2759,7 +2759,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ public function getTabColor() { - if ($this->_tabColor === NULL) + if ($this->_tabColor === null) $this->_tabColor = new PHPExcel_Style_Color(); return $this->_tabColor; @@ -2785,7 +2785,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ public function isTabColorSet() { - return ($this->_tabColor !== NULL); + return ($this->_tabColor !== null); } /** From e47b543c895ce7f45ce4a8d424835bec6157bce9 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 27 Feb 2013 12:18:34 +0000 Subject: [PATCH 3/4] Bugfix: Work item GH-80 - "Sheet index is out of bounds." Exception --- Classes/PHPExcel/Reader/Excel2007.php | 19 ++++++++++--------- changelog.txt | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Classes/PHPExcel/Reader/Excel2007.php b/Classes/PHPExcel/Reader/Excel2007.php index bd91592d..e2a677de 100644 --- a/Classes/PHPExcel/Reader/Excel2007.php +++ b/Classes/PHPExcel/Reader/Excel2007.php @@ -1608,15 +1608,16 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE break; default: - $range = explode('!', (string)$definedName); - if (count($range) == 2) { - $range[0] = str_replace("''", "'", $range[0]); - $range[0] = str_replace("'", "", $range[0]); - if ($worksheet = $docSheet->getParent()->getSheetByName($range[0])) { - $extractedRange = str_replace('$', '', $range[1]); - $scope = $docSheet->getParent()->getSheet((string)$definedName['localSheetId']); - - $excel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $worksheet, $extractedRange, true, $scope) ); + if ($mapSheetId[(integer) $definedName['localSheetId']] !== null) { + $range = explode('!', (string)$definedName); + if (count($range) == 2) { + $range[0] = str_replace("''", "'", $range[0]); + $range[0] = str_replace("'", "", $range[0]); + if ($worksheet = $docSheet->getParent()->getSheetByName($range[0])) { + $extractedRange = str_replace('$', '', $range[1]); + $scope = $docSheet->getParent()->getSheet($mapSheetId[(integer) $definedName['localSheetId']]); + $excel->addNamedRange( new PHPExcel_NamedRange((string)$definedName['name'], $worksheet, $extractedRange, true, $scope) ); + } } } break; diff --git a/changelog.txt b/changelog.txt index 552cb9e4..e02c3a4a 100644 --- a/changelog.txt +++ b/changelog.txt @@ -50,7 +50,7 @@ Fixed in develop branch: - Bugfix: (MBaker) Work item GH-104 - echo statements in HTML.php - Feature: (Progi1984) Work item GH-8/CP11704 : Conditional formatting in Excel 5 Writer - Bugfix: (MBaker) Work item GH-113 - canRead() Error for GoogleDocs ODS files: in ODS files from Google Docs there is no mimetype file - +- Bugfix: (MBaker) Work item GH-80 - "Sheet index is out of bounds." Exception -------------------------------------------------------------------------------- BREAKING CHANGE! As part of the planned changes for handling array formulae in From a49b3c48341f30bd5630cd79a8f38ee71875fadc Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 27 Feb 2013 12:52:57 +0000 Subject: [PATCH 4/4] Example of cell-level protection in a worksheet --- Examples/12cellProtection.php | 107 ++++++++++++++++++++++++++++++++++ Examples/runall.php | 1 + 2 files changed, 108 insertions(+) create mode 100644 Examples/12cellProtection.php diff --git a/Examples/12cellProtection.php b/Examples/12cellProtection.php new file mode 100644 index 00000000..caa38767 --- /dev/null +++ b/Examples/12cellProtection.php @@ -0,0 +1,107 @@ +'); + +date_default_timezone_set('Europe/London'); + +/** Include PHPExcel */ +require_once '../Classes/PHPExcel.php'; + + +// Create new PHPExcel object +echo date('H:i:s') , " Create new PHPExcel object" , EOL; +$objPHPExcel = new PHPExcel(); + +// Set document properties +echo date('H:i:s') , " Set document properties" , EOL; +$objPHPExcel->getProperties()->setCreator("Mark Baker") + ->setLastModifiedBy("Mark Baker") + ->setTitle("Office 2007 XLSX Test Document") + ->setSubject("Office 2007 XLSX Test Document") + ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") + ->setKeywords("office 2007 openxml php") + ->setCategory("Test result file"); + + +// Add some data +echo date('H:i:s') , " Add some data" , EOL; +$objPHPExcel->setActiveSheetIndex(0); +$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Crouching'); +$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Tiger'); +$objPHPExcel->getActiveSheet()->setCellValue('A2', 'Hidden'); +$objPHPExcel->getActiveSheet()->setCellValue('B2', 'Dragon'); + +// Rename worksheet +echo date('H:i:s') , " Rename worksheet" , EOL; +$objPHPExcel->getActiveSheet()->setTitle('Simple'); + + +// Set document security +echo date('H:i:s') , " Set cell protection" , EOL; + + +// Set sheet security +echo date('H:i:s') , " Set sheet security" , EOL; +$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); +$objPHPExcel->getActiveSheet() + ->getStyle('A2:B2') + ->getProtection()->setLocked( + PHPExcel_Style_Protection::PROTECTION_UNPROTECTED + ); + + +// Set active sheet index to the first sheet, so Excel opens this as the first sheet +$objPHPExcel->setActiveSheetIndex(0); + + +// Save Excel 2007 file +echo date('H:i:s') , " Write to Excel2007 format" , EOL; +$callStartTime = microtime(true); + +$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); +$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); +$callEndTime = microtime(true); +$callTime = $callEndTime - $callStartTime; + +echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; +echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL; +// Echo memory usage +echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL; + + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; + +// Echo done +echo date('H:i:s') , " Done writing file" , EOL; +echo 'File has been created in ' , getcwd() , EOL; diff --git a/Examples/runall.php b/Examples/runall.php index ad37f339..7e97ee82 100644 --- a/Examples/runall.php +++ b/Examples/runall.php @@ -52,6 +52,7 @@ $aTests = array( , '10autofilter-selection-2.php' , '11documentsecurity.php' , '11documentsecurity-xls.php' + , '12cellProtection.php' , '13calculation.php' , '14excel5.php' , '15datavalidation.php'