ColumnCellIterator.php 6.4 KB

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