From 5fb3ffceb0d27320a3df59012ffe27c7cfb34252 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 4 Dec 2011 11:24:59 +0000 Subject: [PATCH] Performance improvement for readers that reduces overheads when setting titles in multi-worksheet workbooks, by avoiding re-iterating through all worksheet/cells whenever a sheet title is set git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@83603 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Reader/Excel2003XML.php | 5 ++++- Classes/PHPExcel/Reader/Excel2007.php | 6 +++++- Classes/PHPExcel/Reader/Excel5.php | 5 ++++- Classes/PHPExcel/Reader/Gnumeric.php | 5 ++++- Classes/PHPExcel/Reader/OOCalc.php | 5 ++++- Classes/PHPExcel/Worksheet.php | 16 +++++++++++----- 6 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Classes/PHPExcel/Reader/Excel2003XML.php b/Classes/PHPExcel/Reader/Excel2003XML.php index 608bfe23..2a64a13f 100644 --- a/Classes/PHPExcel/Reader/Excel2003XML.php +++ b/Classes/PHPExcel/Reader/Excel2003XML.php @@ -571,7 +571,10 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader $objPHPExcel->setActiveSheetIndex($worksheetID); if (isset($worksheet_ss['Name'])) { $worksheetName = self::_convertStringEncoding((string) $worksheet_ss['Name'],$this->_charSet); - $objPHPExcel->getActiveSheet()->setTitle($worksheetName); + // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in + // formula cells... during the load, all formulae should be correct, and we're simply bringing + // the worksheet name in line with the formula, not the reverse + $objPHPExcel->getActiveSheet()->setTitle($worksheetName,false); } $columnID = 'A'; diff --git a/Classes/PHPExcel/Reader/Excel2007.php b/Classes/PHPExcel/Reader/Excel2007.php index 111e0b6d..27aeedff 100644 --- a/Classes/PHPExcel/Reader/Excel2007.php +++ b/Classes/PHPExcel/Reader/Excel2007.php @@ -622,7 +622,11 @@ class PHPExcel_Reader_Excel2007 implements PHPExcel_Reader_IReader // Load sheet $docSheet = $excel->createSheet(); - $docSheet->setTitle((string) $eleSheet["name"]); + // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet + // references in formula cells... during the load, all formulae should be correct, + // and we're simply bringing the worksheet name in line with the formula, not the + // reverse + $docSheet->setTitle((string) $eleSheet["name"],false); $fileWorksheet = $worksheets[(string) self::array_item($eleSheet->attributes("http://schemas.openxmlformats.org/officeDocument/2006/relationships"), "id")]; $xmlSheet = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$fileWorksheet")); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main"); diff --git a/Classes/PHPExcel/Reader/Excel5.php b/Classes/PHPExcel/Reader/Excel5.php index 741285e5..286fd4c4 100644 --- a/Classes/PHPExcel/Reader/Excel5.php +++ b/Classes/PHPExcel/Reader/Excel5.php @@ -727,7 +727,10 @@ class PHPExcel_Reader_Excel5 implements PHPExcel_Reader_IReader // add sheet to PHPExcel object $this->_phpSheet = $this->_phpExcel->createSheet(); - $this->_phpSheet->setTitle($sheet['name']); + // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula + // cells... during the load, all formulae should be correct, and we're simply bringing the worksheet + // name in line with the formula, not the reverse + $this->_phpSheet->setTitle($sheet['name'],false); $this->_phpSheet->setSheetState($sheet['sheetState']); $this->_pos = $sheet['offset']; diff --git a/Classes/PHPExcel/Reader/Gnumeric.php b/Classes/PHPExcel/Reader/Gnumeric.php index 55061bc0..8496ebb4 100644 --- a/Classes/PHPExcel/Reader/Gnumeric.php +++ b/Classes/PHPExcel/Reader/Gnumeric.php @@ -414,7 +414,10 @@ class PHPExcel_Reader_Gnumeric implements PHPExcel_Reader_IReader // Create new Worksheet $objPHPExcel->createSheet(); $objPHPExcel->setActiveSheetIndex($worksheetID); - $objPHPExcel->getActiveSheet()->setTitle($worksheetName); + // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula + // cells... during the load, all formulae should be correct, and we're simply bringing the worksheet + // name in line with the formula, not the reverse + $objPHPExcel->getActiveSheet()->setTitle($worksheetName,false); if ((!$this->_readDataOnly) && (isset($sheet->PrintInformation))) { if (isset($sheet->PrintInformation->Margins)) { diff --git a/Classes/PHPExcel/Reader/OOCalc.php b/Classes/PHPExcel/Reader/OOCalc.php index 44524cfe..b6e47ded 100644 --- a/Classes/PHPExcel/Reader/OOCalc.php +++ b/Classes/PHPExcel/Reader/OOCalc.php @@ -396,7 +396,10 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader $objPHPExcel->setActiveSheetIndex($worksheetID); if (isset($worksheetDataAttributes['name'])) { $worksheetName = (string) $worksheetDataAttributes['name']; - $objPHPExcel->getActiveSheet()->setTitle($worksheetName); + // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in + // formula cells... during the load, all formulae should be correct, and we're simply + // bringing the worksheet name in line with the formula, not the reverse + $objPHPExcel->getActiveSheet()->setTitle($worksheetName,false); } $rowID = 1; diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index a2e5ebe3..2e0b5905 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -644,9 +644,14 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable * Set title * * @param string $pValue String containing the dimension of this worksheet + * @param string $updateFormulaCellReferences boolean Flag indicating whether cell references in formulae should + * be updated to reflect the new sheet name. + * This should be left as the default true, unless you are + * certain that no formula cells on any worksheet contain + * references to this worksheet * @return PHPExcel_Worksheet */ - public function setTitle($pValue = 'Worksheet') + public function setTitle($pValue = 'Worksheet', $updateFormulaCellReferences = true) { // Is this a 'rename' or not? if ($this->getTitle() == $pValue) { @@ -690,7 +695,8 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // New title $newTitle = $this->getTitle(); - PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle); + if ($updateFormulaCellReferences) + PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle); return $this; } @@ -2260,7 +2266,6 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // Identify the range that we need to extract from the worksheet $maxCol = $this->getHighestColumn(); $maxRow = $this->getHighestRow(); - // Return return $this->rangeToArray( 'A1:'.$maxCol.$maxRow, $nullValue, $calculateFormulas, $formatData, $returnCellRef); @@ -2269,10 +2274,11 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable /** * Get row iterator * + * @param integer $startRow The row number at which to start iterating * @return PHPExcel_Worksheet_RowIterator */ - public function getRowIterator() { - return new PHPExcel_Worksheet_RowIterator($this); + public function getRowIterator($startRow = 1) { + return new PHPExcel_Worksheet_RowIterator($this,$startRow); } /**