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
This commit is contained in:
Mark Baker 2011-01-12 15:20:29 +00:00
parent 56d7506cf5
commit 1643bb9613
2 changed files with 64 additions and 9 deletions

View File

@ -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 mixed $nullValue Value returned in the array entry if a cell doesn't exist
* @param boolean $calculateFormulas Should formulas be calculated? * @param boolean $calculateFormulas Should formulas be calculated?
* @param boolean $formatData Should formatting be applied to cell values? * @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 * True - Return rows and columns indexed by their actual row and column IDs
* @return array * @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
$returnValue = array(); $returnValue = array();
// Garbage collect...
$this->garbageCollect();
// Identify the range that we need to extract from the worksheet // Identify the range that we need to extract from the worksheet
$maxCol = $this->getHighestColumn(); list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
$maxRow = $this->getHighestRow(); $minCol = PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] -1);
$minRow = $rangeStart[1];
$maxCol = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] -1);
$maxRow = $rangeEnd[1];
$maxCol++; $maxCol++;
// Loop through rows // Loop through rows
for ($row = 1; $row <= $maxRow; ++$row) { for ($row = $minRow; $row <= $maxRow; ++$row) {
$c = -1; $c = -1;
// Loop through columns in the current row // Loop through columns in the current row
for ($col = 'A'; $col != $maxCol; ++$col) { for ($col = $minCol; $col != $maxCol; ++$col) {
$rRef = ($returnCellRef) ? $row : $row-1; $rRef = ($returnCellRef) ? $row : $row-1;
$cRef = ($returnCellRef) ? $col : ++$c; $cRef = ($returnCellRef) ? $col : ++$c;
// Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen // 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; 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 * Get row iterator
* *

View File

@ -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 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) 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) 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 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 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 - Bugfix: (MBaker) Work item 14901 - VLOOKUP choking on parameters in PHPExcel.1.7.5/PHPExcel_Writer_Excel2007