ColumnCellIterator.php 5.7 KB

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