RowCellIterator.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Worksheet_RowCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
  8. {
  9. /**
  10. * Row index
  11. *
  12. * @var int
  13. */
  14. protected $rowIndex;
  15. /**
  16. * Start position
  17. *
  18. * @var int
  19. */
  20. protected $startColumn = 0;
  21. /**
  22. * End position
  23. *
  24. * @var int
  25. */
  26. protected $endColumn = 0;
  27. /**
  28. * Create a new column iterator
  29. *
  30. * @param PHPExcel_Worksheet $subject The worksheet to iterate over
  31. * @param integer $rowIndex The row that we want to iterate
  32. * @param string $startColumn The column address at which to start iterating
  33. * @param string $endColumn Optionally, the column address at which to stop iterating
  34. */
  35. public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
  36. {
  37. // Set subject and row index
  38. $this->subject = $subject;
  39. $this->rowIndex = $rowIndex;
  40. $this->resetEnd($endColumn);
  41. $this->resetStart($startColumn);
  42. }
  43. /**
  44. * Destructor
  45. */
  46. public function __destruct()
  47. {
  48. unset($this->subject);
  49. }
  50. /**
  51. * (Re)Set the start column and the current column pointer
  52. *
  53. * @param integer $startColumn The column address at which to start iterating
  54. * @return PHPExcel_Worksheet_RowCellIterator
  55. * @throws PHPExcel_Exception
  56. */
  57. public function resetStart($startColumn = 'A')
  58. {
  59. $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
  60. $this->startColumn = $startColumnIndex;
  61. $this->adjustForExistingOnlyRange();
  62. $this->seek(PHPExcel_Cell::stringFromColumnIndex($this->startColumn));
  63. return $this;
  64. }
  65. /**
  66. * (Re)Set the end column
  67. *
  68. * @param string $endColumn The column address at which to stop iterating
  69. * @return PHPExcel_Worksheet_RowCellIterator
  70. * @throws PHPExcel_Exception
  71. */
  72. public function resetEnd($endColumn = null)
  73. {
  74. $endColumn = ($endColumn) ? $endColumn : $this->subject->getHighestColumn();
  75. $this->endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
  76. $this->adjustForExistingOnlyRange();
  77. return $this;
  78. }
  79. /**
  80. * Set the column pointer to the selected column
  81. *
  82. * @param string $column The column address to set the current pointer at
  83. * @return PHPExcel_Worksheet_RowCellIterator
  84. * @throws PHPExcel_Exception
  85. */
  86. public function seek($column = 'A')
  87. {
  88. $column = PHPExcel_Cell::columnIndexFromString($column) - 1;
  89. if (($column < $this->startColumn) || ($column > $this->endColumn)) {
  90. throw new PHPExcel_Exception("Column $column is out of range ({$this->startColumn} - {$this->endColumn})");
  91. } elseif ($this->onlyExistingCells && !($this->subject->cellExistsByColumnAndRow($column, $this->rowIndex))) {
  92. throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
  93. }
  94. $this->position = $column;
  95. return $this;
  96. }
  97. /**
  98. * Rewind the iterator to the starting column
  99. */
  100. public function rewind()
  101. {
  102. $this->position = $this->startColumn;
  103. }
  104. /**
  105. * Return the current cell in this worksheet row
  106. *
  107. * @return PHPExcel_Cell
  108. */
  109. public function current()
  110. {
  111. return $this->subject->getCellByColumnAndRow($this->position, $this->rowIndex);
  112. }
  113. /**
  114. * Return the current iterator key
  115. *
  116. * @return string
  117. */
  118. public function key()
  119. {
  120. return PHPExcel_Cell::stringFromColumnIndex($this->position);
  121. }
  122. /**
  123. * Set the iterator to its next value
  124. */
  125. public function next()
  126. {
  127. do {
  128. ++$this->position;
  129. } while (($this->onlyExistingCells) &&
  130. (!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) &&
  131. ($this->position <= $this->endColumn));
  132. }
  133. /**
  134. * Set the iterator to its previous value
  135. *
  136. * @throws PHPExcel_Exception
  137. */
  138. public function prev()
  139. {
  140. if ($this->position <= $this->startColumn) {
  141. throw new PHPExcel_Exception(
  142. "Column is already at the beginning of range (" .
  143. PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . " - " .
  144. PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . ")"
  145. );
  146. }
  147. do {
  148. --$this->position;
  149. } while (($this->onlyExistingCells) &&
  150. (!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) &&
  151. ($this->position >= $this->startColumn));
  152. }
  153. /**
  154. * Indicate if more columns exist in the worksheet range of columns that we're iterating
  155. *
  156. * @return boolean
  157. */
  158. public function valid()
  159. {
  160. return $this->position <= $this->endColumn;
  161. }
  162. /**
  163. * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
  164. *
  165. * @throws PHPExcel_Exception
  166. */
  167. protected function adjustForExistingOnlyRange()
  168. {
  169. if ($this->onlyExistingCells) {
  170. while ((!$this->subject->cellExistsByColumnAndRow($this->startColumn, $this->rowIndex)) &&
  171. ($this->startColumn <= $this->endColumn)) {
  172. ++$this->startColumn;
  173. }
  174. if ($this->startColumn > $this->endColumn) {
  175. throw new PHPExcel_Exception('No cells exist within the specified range');
  176. }
  177. while ((!$this->subject->cellExistsByColumnAndRow($this->endColumn, $this->rowIndex)) &&
  178. ($this->endColumn >= $this->startColumn)) {
  179. --$this->endColumn;
  180. }
  181. if ($this->endColumn < $this->startColumn) {
  182. throw new PHPExcel_Exception('No cells exist within the specified range');
  183. }
  184. }
  185. }
  186. }