f9d72863e58bdf9c0eeebeffc2739e2a683c9183.svn-base 6.8 KB

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