[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Worksheet_ColumnCellIterator
   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_ColumnCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
  29  {
  30      /**
  31       * Column index
  32       *
  33       * @var string
  34       */
  35      protected $columnIndex;
  36  
  37      /**
  38       * Start position
  39       *
  40       * @var int
  41       */
  42      protected $startRow = 1;
  43  
  44      /**
  45       * End position
  46       *
  47       * @var int
  48       */
  49      protected $endRow = 1;
  50  
  51      /**
  52       * Create a new row iterator
  53       *
  54       * @param    PHPExcel_Worksheet    $subject        The worksheet to iterate over
  55       * @param   string              $columnIndex    The column that we want to iterate
  56       * @param    integer                $startRow        The row number at which to start iterating
  57       * @param    integer                $endRow            Optionally, the row number at which to stop iterating
  58       */
  59      public function __construct(PHPExcel_Worksheet $subject = null, $columnIndex = 'A', $startRow = 1, $endRow = null)
  60      {
  61          // Set subject
  62          $this->subject = $subject;
  63          $this->columnIndex = PHPExcel_Cell::columnIndexFromString($columnIndex) - 1;
  64          $this->resetEnd($endRow);
  65          $this->resetStart($startRow);
  66      }
  67  
  68      /**
  69       * Destructor
  70       */
  71      public function __destruct()
  72      {
  73          unset($this->subject);
  74      }
  75  
  76      /**
  77       * (Re)Set the start row and the current row pointer
  78       *
  79       * @param integer    $startRow    The row number at which to start iterating
  80       * @return PHPExcel_Worksheet_ColumnCellIterator
  81       * @throws PHPExcel_Exception
  82       */
  83      public function resetStart($startRow = 1)
  84      {
  85          $this->startRow = $startRow;
  86          $this->adjustForExistingOnlyRange();
  87          $this->seek($startRow);
  88  
  89          return $this;
  90      }
  91  
  92      /**
  93       * (Re)Set the end row
  94       *
  95       * @param integer    $endRow    The row number at which to stop iterating
  96       * @return PHPExcel_Worksheet_ColumnCellIterator
  97       * @throws PHPExcel_Exception
  98       */
  99      public function resetEnd($endRow = null)
 100      {
 101          $this->endRow = ($endRow) ? $endRow : $this->subject->getHighestRow();
 102          $this->adjustForExistingOnlyRange();
 103  
 104          return $this;
 105      }
 106  
 107      /**
 108       * Set the row pointer to the selected row
 109       *
 110       * @param integer    $row    The row number to set the current pointer at
 111       * @return PHPExcel_Worksheet_ColumnCellIterator
 112       * @throws PHPExcel_Exception
 113       */
 114      public function seek($row = 1)
 115      {
 116          if (($row < $this->startRow) || ($row > $this->endRow)) {
 117              throw new PHPExcel_Exception("Row $row is out of range ({$this->startRow} - {$this->endRow})");
 118          } elseif ($this->onlyExistingCells && !($this->subject->cellExistsByColumnAndRow($this->columnIndex, $row))) {
 119              throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
 120          }
 121          $this->position = $row;
 122  
 123          return $this;
 124      }
 125  
 126      /**
 127       * Rewind the iterator to the starting row
 128       */
 129      public function rewind()
 130      {
 131          $this->position = $this->startRow;
 132      }
 133  
 134      /**
 135       * Return the current cell in this worksheet column
 136       *
 137       * @return PHPExcel_Worksheet_Row
 138       */
 139      public function current()
 140      {
 141          return $this->subject->getCellByColumnAndRow($this->columnIndex, $this->position);
 142      }
 143  
 144      /**
 145       * Return the current iterator key
 146       *
 147       * @return int
 148       */
 149      public function key()
 150      {
 151          return $this->position;
 152      }
 153  
 154      /**
 155       * Set the iterator to its next value
 156       */
 157      public function next()
 158      {
 159          do {
 160              ++$this->position;
 161          } while (($this->onlyExistingCells) &&
 162              (!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->position)) &&
 163              ($this->position <= $this->endRow));
 164      }
 165  
 166      /**
 167       * Set the iterator to its previous value
 168       */
 169      public function prev()
 170      {
 171          if ($this->position <= $this->startRow) {
 172              throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->startRow} - {$this->endRow})");
 173          }
 174  
 175          do {
 176              --$this->position;
 177          } while (($this->onlyExistingCells) &&
 178              (!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->position)) &&
 179              ($this->position >= $this->startRow));
 180      }
 181  
 182      /**
 183       * Indicate if more rows exist in the worksheet range of rows that we're iterating
 184       *
 185       * @return boolean
 186       */
 187      public function valid()
 188      {
 189          return $this->position <= $this->endRow;
 190      }
 191  
 192      /**
 193       * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
 194       *
 195       * @throws PHPExcel_Exception
 196       */
 197      protected function adjustForExistingOnlyRange()
 198      {
 199          if ($this->onlyExistingCells) {
 200              while ((!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->startRow)) &&
 201                  ($this->startRow <= $this->endRow)) {
 202                  ++$this->startRow;
 203              }
 204              if ($this->startRow > $this->endRow) {
 205                  throw new PHPExcel_Exception('No cells exist within the specified range');
 206              }
 207              while ((!$this->subject->cellExistsByColumnAndRow($this->columnIndex, $this->endRow)) &&
 208                  ($this->endRow >= $this->startRow)) {
 209                  --$this->endRow;
 210              }
 211              if ($this->endRow < $this->startRow) {
 212                  throw new PHPExcel_Exception('No cells exist within the specified range');
 213              }
 214          }
 215      }
 216  }


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