ColumnIterator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * PHPExcel_Worksheet_ColumnIterator
  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_ColumnIterator implements Iterator
  28. {
  29. /**
  30. * PHPExcel_Worksheet to iterate
  31. *
  32. * @var PHPExcel_Worksheet
  33. */
  34. private $subject;
  35. /**
  36. * Current iterator position
  37. *
  38. * @var int
  39. */
  40. private $position = 0;
  41. /**
  42. * Start position
  43. *
  44. * @var int
  45. */
  46. private $startColumn = 0;
  47. /**
  48. * End position
  49. *
  50. * @var int
  51. */
  52. private $endColumn = 0;
  53. /**
  54. * Create a new column iterator
  55. *
  56. * @param PHPExcel_Worksheet $subject The worksheet to iterate over
  57. * @param string $startColumn The column address at which to start iterating
  58. * @param string $endColumn Optionally, the column address at which to stop iterating
  59. */
  60. public function __construct(PHPExcel_Worksheet $subject = null, $startColumn = 'A', $endColumn = null)
  61. {
  62. // Set subject
  63. $this->subject = $subject;
  64. $this->resetEnd($endColumn);
  65. $this->resetStart($startColumn);
  66. }
  67. /**
  68. * Destructor
  69. */
  70. public function __destruct()
  71. {
  72. unset($this->subject);
  73. }
  74. /**
  75. * (Re)Set the start column and the current column pointer
  76. *
  77. * @param integer $startColumn The column address at which to start iterating
  78. * @return PHPExcel_Worksheet_ColumnIterator
  79. * @throws PHPExcel_Exception
  80. */
  81. public function resetStart($startColumn = 'A')
  82. {
  83. $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
  84. if ($startColumnIndex > PHPExcel_Cell::columnIndexFromString($this->subject->getHighestColumn()) - 1) {
  85. throw new PHPExcel_Exception("Start column ({$startColumn}) is beyond highest column ({$this->subject->getHighestColumn()})");
  86. }
  87. $this->startColumn = $startColumnIndex;
  88. if ($this->endColumn < $this->startColumn) {
  89. $this->endColumn = $this->startColumn;
  90. }
  91. $this->seek($startColumn);
  92. return $this;
  93. }
  94. /**
  95. * (Re)Set the end column
  96. *
  97. * @param string $endColumn The column address at which to stop iterating
  98. * @return PHPExcel_Worksheet_ColumnIterator
  99. */
  100. public function resetEnd($endColumn = null)
  101. {
  102. $endColumn = ($endColumn) ? $endColumn : $this->subject->getHighestColumn();
  103. $this->endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
  104. return $this;
  105. }
  106. /**
  107. * Set the column pointer to the selected column
  108. *
  109. * @param string $column The column address to set the current pointer at
  110. * @return PHPExcel_Worksheet_ColumnIterator
  111. * @throws PHPExcel_Exception
  112. */
  113. public function seek($column = 'A')
  114. {
  115. $column = PHPExcel_Cell::columnIndexFromString($column) - 1;
  116. if (($column < $this->startColumn) || ($column > $this->endColumn)) {
  117. throw new PHPExcel_Exception("Column $column is out of range ({$this->startColumn} - {$this->endColumn})");
  118. }
  119. $this->position = $column;
  120. return $this;
  121. }
  122. /**
  123. * Rewind the iterator to the starting column
  124. */
  125. public function rewind()
  126. {
  127. $this->position = $this->startColumn;
  128. }
  129. /**
  130. * Return the current column in this worksheet
  131. *
  132. * @return PHPExcel_Worksheet_Column
  133. */
  134. public function current()
  135. {
  136. return new PHPExcel_Worksheet_Column($this->subject, PHPExcel_Cell::stringFromColumnIndex($this->position));
  137. }
  138. /**
  139. * Return the current iterator key
  140. *
  141. * @return string
  142. */
  143. public function key()
  144. {
  145. return PHPExcel_Cell::stringFromColumnIndex($this->position);
  146. }
  147. /**
  148. * Set the iterator to its next value
  149. */
  150. public function next()
  151. {
  152. ++$this->position;
  153. }
  154. /**
  155. * Set the iterator to its previous value
  156. *
  157. * @throws PHPExcel_Exception
  158. */
  159. public function prev()
  160. {
  161. if ($this->position <= $this->startColumn) {
  162. throw new PHPExcel_Exception(
  163. "Column is already at the beginning of range (" .
  164. PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . " - " .
  165. PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . ")"
  166. );
  167. }
  168. --$this->position;
  169. }
  170. /**
  171. * Indicate if more columns exist in the worksheet range of columns that we're iterating
  172. *
  173. * @return boolean
  174. */
  175. public function valid()
  176. {
  177. return $this->position <= $this->endColumn;
  178. }
  179. }