PHPExcel_Worksheet
[ class tree: PHPExcel_Worksheet ] [ index: PHPExcel_Worksheet ] [ all elements ]

Source for file CellIterator.php

Documentation is available at CellIterator.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Worksheet
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.7, 2009-04-22
  26.  */
  27.  
  28.  
  29. /** PHPExcel */
  30. require_once 'PHPExcel.php';
  31.  
  32. /** PHPExcel_Worksheet */
  33. require_once 'PHPExcel/Worksheet.php';
  34.  
  35. /** PHPExcel_Cell */
  36. require_once 'PHPExcel/Cell.php';
  37.  
  38.  
  39. /**
  40.  * PHPExcel_Worksheet_CellIterator
  41.  * 
  42.  * Used to iterate rows in a PHPExcel_Worksheet
  43.  *
  44.  * @category   PHPExcel
  45.  * @package    PHPExcel_Worksheet
  46.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  47.  */
  48. class PHPExcel_Worksheet_CellIterator extends IteratorIterator
  49. {
  50.     /**
  51.      * PHPExcel_Worksheet to iterate
  52.      *
  53.      * @var PHPExcel_Worksheet 
  54.      */
  55.     private $_subject;
  56.     
  57.     /**
  58.      * Row index
  59.      *
  60.      * @var int 
  61.      */
  62.     private $_rowIndex;
  63.     
  64.     /**
  65.      * Current iterator position
  66.      *
  67.      * @var int 
  68.      */
  69.     private $_position = 0;
  70.     
  71.     /**
  72.      * Loop only existing cells
  73.      *
  74.      * @var boolean 
  75.      */
  76.     private $_onlyExistingCells = true;
  77.  
  78.     /**
  79.      * Create a new cell iterator
  80.      *
  81.      * @param PHPExcel_Worksheet         $subject 
  82.      * @param int                        $rowIndex 
  83.      */
  84.     public function __construct(PHPExcel_Worksheet $subject null$rowIndex 1{
  85.         // Set subject and row index
  86.         $this->_subject     = $subject;
  87.         $this->_rowIndex     = $rowIndex;
  88.     }
  89.     
  90.     /**
  91.      * Destructor
  92.      */
  93.     public function __destruct({
  94.         unset($this->_subject);
  95.     }
  96.     
  97.     /**
  98.      * Rewind iterator
  99.      */
  100.     public function rewind({
  101.         $this->_position = 0;
  102.     }
  103.  
  104.     /**
  105.      * Current PHPExcel_Cell
  106.      *
  107.      * @return PHPExcel_Cell 
  108.      */
  109.     public function current({
  110.         $cellExists $this->_subject->cellExistsByColumnAndRow($this->_position$this->_rowIndex);
  111.         if ( ($this->_onlyExistingCells && $cellExists|| (!$this->_onlyExistingCells) ) {
  112.             return $this->_subject->getCellByColumnAndRow($this->_position$this->_rowIndex);
  113.         else if ($this->_onlyExistingCells && !$cellExists{
  114.             // Loop untill we find one
  115.             while ($this->valid()) {
  116.                 $this->next();
  117.                 if ($this->_subject->cellExistsByColumnAndRow($this->_position$this->_rowIndex)) {
  118.                     return $this->_subject->getCellByColumnAndRow($this->_position$this->_rowIndex);
  119.                 }
  120.             }
  121.         }
  122.         
  123.         return null;
  124.     }
  125.  
  126.     /**
  127.      * Current key
  128.      *
  129.      * @return int 
  130.      */
  131.     public function key({
  132.         return $this->_position;
  133.     }
  134.  
  135.     /**
  136.      * Next value
  137.      */
  138.     public function next({
  139.         ++$this->_position;
  140.     }
  141.  
  142.     /**
  143.      * More PHPExcel_Cell instances available?
  144.      *
  145.      * @return boolean 
  146.      */
  147.     public function valid({
  148.         return $this->_position < PHPExcel_Cell::columnIndexFromString$this->_subject->getHighestColumn() );
  149.     }
  150.     
  151.     /**
  152.      * Get loop only existing cells
  153.      *
  154.      * @return boolean 
  155.      */
  156.     public function getIterateOnlyExistingCells({
  157.         return $this->_onlyExistingCells;
  158.     }
  159.     
  160.     /**
  161.      * Set loop only existing cells
  162.      *
  163.      * @return boolean 
  164.      */
  165.     public function setIterateOnlyExistingCells($value true{
  166.         $this->_onlyExistingCells = $value;
  167.     }
  168. }

Documentation generated on Wed, 22 Apr 2009 08:56:34 +0200 by phpDocumentor 1.4.1