05c438619c79ed94cc29be936ae976d0f3c173d1.svn-base 6.2 KB

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