3784fa68e6ccbc82927f78b2e5bc8e67e8713e5a.svn-base 5.0 KB

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