ColumnIterator.php 4.8 KB

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