From 1643bb9613019abb081be1430e74903bc7750147 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Wed, 12 Jan 2011 15:20:29 +0000 Subject: [PATCH] Feature: Added new rangeToArray() and namedRangeToArray() methods to the PHPExcel_Worksheet object. Functionally, these are identical to the toArray() method, except that they take an additional first parameter of a Range (e.g. 'B2:C3') or a Named Range name. Modified the toArray() method so that it actually uses rangeToArray(). git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@66799 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Worksheet.php | 70 +++++++++++++++++++++++++++++----- changelog.txt | 3 ++ 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index 64096f91..7e61c7f4 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -2134,8 +2134,9 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable } /** - * Create array from worksheet + * Create array from a range of cells * + * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1") * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist * @param boolean $calculateFormulas Should formulas be calculated? * @param boolean $formatData Should formatting be applied to cell values? @@ -2143,23 +2144,24 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable * True - Return rows and columns indexed by their actual row and column IDs * @return array */ - public function toArray($nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) { + public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) { // Returnvalue $returnValue = array(); - // Garbage collect... - $this->garbageCollect(); - // Identify the range that we need to extract from the worksheet - $maxCol = $this->getHighestColumn(); - $maxRow = $this->getHighestRow(); + list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange); + $minCol = PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] -1); + $minRow = $rangeStart[1]; + $maxCol = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] -1); + $maxRow = $rangeEnd[1]; + $maxCol++; // Loop through rows - for ($row = 1; $row <= $maxRow; ++$row) { + for ($row = $minRow; $row <= $maxRow; ++$row) { $c = -1; // Loop through columns in the current row - for ($col = 'A'; $col != $maxCol; ++$col) { + for ($col = $minCol; $col != $maxCol; ++$col) { $rRef = ($returnCellRef) ? $row : $row-1; $cRef = ($returnCellRef) ? $col : ++$c; // Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen @@ -2194,6 +2196,56 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable return $returnValue; } + + /** + * Create array from a range of cells + * + * @param string $pNamedRange Name of the Named Range + * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist + * @param boolean $calculateFormulas Should formulas be calculated? + * @param boolean $formatData Should formatting be applied to cell values? + * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero + * True - Return rows and columns indexed by their actual row and column IDs + * @return array + * @throws Exception + */ + public function namedRangeToArray($pNamedRange = '', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) { + $namedRange = PHPExcel_NamedRange::resolveRange($pNamedRange, $this); + if (!is_null($namedRange)) { + $pWorkSheet = $namedRange->getWorksheet(); + $pCellRange = $namedRange->getRange(); + + return $pWorkSheet->rangeToArray( $pCellRange, + $nullValue, $calculateFormulas, $formatData, $returnCellRef); + } + + throw new Exception('Named Range '.$pNamedRange.' does not exist.'); + } + + + /** + * Create array from worksheet + * + * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist + * @param boolean $calculateFormulas Should formulas be calculated? + * @param boolean $formatData Should formatting be applied to cell values? + * @param boolean $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero + * True - Return rows and columns indexed by their actual row and column IDs + * @return array + */ + public function toArray($nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) { + // Garbage collect... + $this->garbageCollect(); + + // 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); + } + /** * Get row iterator * diff --git a/changelog.txt b/changelog.txt index 21b68ba4..82fcafb6 100644 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,9 @@ Fixed in SVN: This allows the writing of Excel2007 files, even without ZipArchive enabled (it does require zlib), or when php_zip is one of the buggy PHP 5.2.6 or 5.2.8 versions - Feature: (MBaker) Work item 14979 - Added listWorksheetNames() method to Readers that support multiple worksheets in a workbook, allowing a user to extract a list of all the worksheet names from a file without parsing/loading the whole file. - Feature: (MBaker) Speed boost and memory reduction in the Worksheet toArray() method. +- Feature: (MBaker) Added new rangeToArray() and namedRangeToArray() methods to the PHPExcel_Worksheet object. + Functionally, these are identical to the toArray() method, except that they take an additional first parameter of a Range (e.g. 'B2:C3') or a Named Range name. + Modified the toArray() method so that it actually uses rangeToArray(). - Bugfix: (MBaker) Work item 14888 - Simple =IF() formula disappears - Bugfix: (MBaker) Work item 14898 - PHP Warning: preg_match(): Compilation failed: PCRE does not support \\L, \\l, \\N, \\P, \\p, \\U, \\u, or \\X - Bugfix: (MBaker) Work item 14901 - VLOOKUP choking on parameters in PHPExcel.1.7.5/PHPExcel_Writer_Excel2007