RowCellIterator.php 7.0 KB

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