For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.0 KiB

  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. }