[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Worksheet/ -> RowCellIterator.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Worksheet_RowCellIterator
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Worksheet
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Worksheet_RowCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
  29  {
  30      /**
  31       * Row index
  32       *
  33       * @var int
  34       */
  35      protected $rowIndex;
  36  
  37      /**
  38       * Start position
  39       *
  40       * @var int
  41       */
  42      protected $startColumn = 0;
  43  
  44      /**
  45       * End position
  46       *
  47       * @var int
  48       */
  49      protected $endColumn = 0;
  50  
  51      /**
  52       * Create a new column iterator
  53       *
  54       * @param    PHPExcel_Worksheet    $subject        The worksheet to iterate over
  55       * @param   integer             $rowIndex       The row that we want to iterate
  56       * @param    string                $startColumn    The column address at which to start iterating
  57       * @param    string                $endColumn        Optionally, the column address at which to stop iterating
  58       */
  59      public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
  60      {
  61          // Set subject and row index
  62          $this->subject = $subject;
  63          $this->rowIndex = $rowIndex;
  64          $this->resetEnd($endColumn);
  65          $this->resetStart($startColumn);
  66      }
  67  
  68      /**
  69       * Destructor
  70       */
  71      public function __destruct()
  72      {
  73          unset($this->subject);
  74      }
  75  
  76      /**
  77       * (Re)Set the start column and the current column pointer
  78       *
  79       * @param integer    $startColumn    The column address at which to start iterating
  80       * @return PHPExcel_Worksheet_RowCellIterator
  81       * @throws PHPExcel_Exception
  82       */
  83      public function resetStart($startColumn = 'A')
  84      {
  85          $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
  86          $this->startColumn = $startColumnIndex;
  87          $this->adjustForExistingOnlyRange();
  88          $this->seek(PHPExcel_Cell::stringFromColumnIndex($this->startColumn));
  89  
  90          return $this;
  91      }
  92  
  93      /**
  94       * (Re)Set the end column
  95       *
  96       * @param string    $endColumn    The column address at which to stop iterating
  97       * @return PHPExcel_Worksheet_RowCellIterator
  98       * @throws PHPExcel_Exception
  99       */
 100      public function resetEnd($endColumn = null)
 101      {
 102          $endColumn = ($endColumn) ? $endColumn : $this->subject->getHighestColumn();
 103          $this->endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
 104          $this->adjustForExistingOnlyRange();
 105  
 106          return $this;
 107      }
 108  
 109      /**
 110       * Set the column pointer to the selected column
 111       *
 112       * @param string    $column    The column address to set the current pointer at
 113       * @return PHPExcel_Worksheet_RowCellIterator
 114       * @throws PHPExcel_Exception
 115       */
 116      public function seek($column = 'A')
 117      {
 118          $column = PHPExcel_Cell::columnIndexFromString($column) - 1;
 119          if (($column < $this->startColumn) || ($column > $this->endColumn)) {
 120              throw new PHPExcel_Exception("Column $column is out of range ({$this->startColumn} - {$this->endColumn})");
 121          } elseif ($this->onlyExistingCells && !($this->subject->cellExistsByColumnAndRow($column, $this->rowIndex))) {
 122              throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
 123          }
 124          $this->position = $column;
 125  
 126          return $this;
 127      }
 128  
 129      /**
 130       * Rewind the iterator to the starting column
 131       */
 132      public function rewind()
 133      {
 134          $this->position = $this->startColumn;
 135      }
 136  
 137      /**
 138       * Return the current cell in this worksheet row
 139       *
 140       * @return PHPExcel_Cell
 141       */
 142      public function current()
 143      {
 144          return $this->subject->getCellByColumnAndRow($this->position, $this->rowIndex);
 145      }
 146  
 147      /**
 148       * Return the current iterator key
 149       *
 150       * @return string
 151       */
 152      public function key()
 153      {
 154          return PHPExcel_Cell::stringFromColumnIndex($this->position);
 155      }
 156  
 157      /**
 158       * Set the iterator to its next value
 159       */
 160      public function next()
 161      {
 162          do {
 163              ++$this->position;
 164          } while (($this->onlyExistingCells) &&
 165              (!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) &&
 166              ($this->position <= $this->endColumn));
 167      }
 168  
 169      /**
 170       * Set the iterator to its previous value
 171       *
 172       * @throws PHPExcel_Exception
 173       */
 174      public function prev()
 175      {
 176          if ($this->position <= $this->startColumn) {
 177              throw new PHPExcel_Exception(
 178                  "Column is already at the beginning of range (" .
 179                  PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . " - " .
 180                  PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . ")"
 181              );
 182          }
 183  
 184          do {
 185              --$this->position;
 186          } while (($this->onlyExistingCells) &&
 187              (!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) &&
 188              ($this->position >= $this->startColumn));
 189      }
 190  
 191      /**
 192       * Indicate if more columns exist in the worksheet range of columns that we're iterating
 193       *
 194       * @return boolean
 195       */
 196      public function valid()
 197      {
 198          return $this->position <= $this->endColumn;
 199      }
 200  
 201      /**
 202       * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
 203       *
 204       * @throws PHPExcel_Exception
 205       */
 206      protected function adjustForExistingOnlyRange()
 207      {
 208          if ($this->onlyExistingCells) {
 209              while ((!$this->subject->cellExistsByColumnAndRow($this->startColumn, $this->rowIndex)) &&
 210                  ($this->startColumn <= $this->endColumn)) {
 211                  ++$this->startColumn;
 212              }
 213              if ($this->startColumn > $this->endColumn) {
 214                  throw new PHPExcel_Exception('No cells exist within the specified range');
 215              }
 216              while ((!$this->subject->cellExistsByColumnAndRow($this->endColumn, $this->rowIndex)) &&
 217                  ($this->endColumn >= $this->startColumn)) {
 218                  --$this->endColumn;
 219              }
 220              if ($this->endColumn < $this->startColumn) {
 221                  throw new PHPExcel_Exception('No cells exist within the specified range');
 222              }
 223          }
 224      }
 225  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1