getNamedRange('NAME')->getWorksheet() Returns Worksheet that points to entire worksheet

Topics: Developer Forum
Jul 29, 2012 at 10:33 AM

Excuse me for my English.

My problem:

In 'my.xls' (file type excel 97-2003) i'm set named range 'TEST'. This range points to 'A1:E12'

My code is

$reader = new PHPExcel_IOFactory::createReader('Ecxel5');


$range =  $reader->getNamedRange('TEST');

$sheet = $range->getWorksheet();

Now in $sheet i have entire worksheet, i.e. $sheet->title='List1', _rowDimension points to all rows in worksheet instead of single row in range and so on

Tell me please, how i'm can get cells only for named range 'TEST'

Jul 29, 2012 at 11:25 AM
Edited Jul 29, 2012 at 11:39 AM

The $range object is a PHPExcel_NamedRange object

Check the methods for the NamedRange object

getWorksheet() returns the worksheet object for the named range. This is not a subset of the worksheet object, but the full worksheet object.

getRange() returns the referenced cells in that worksheet as a string (e.g. C3:D4)

You can then use cell methods like extractAllCellReferencesInRange() to get an array of individual cell references in the range, or getRangeBoundaries() to get the min/max row and min/max columns, or rangeBoundaries() which you can loop through to get the cell values.

There is also a namedRangeToArray() method in the worksheet object that will return an array of cell values

Jul 29, 2012 at 11:46 AM

Oh, thank you! All works!!!