1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
7
|
|
|
|
8
|
|
|
class ColumnCellIterator extends CellIterator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Current iterator position. |
12
|
|
|
* |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
private $currentRow; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Column index. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $columnIndex; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Start position. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $startRow = 1; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* End position. |
33
|
|
|
* |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $endRow = 1; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Create a new row iterator. |
40
|
|
|
* |
41
|
|
|
* @param Worksheet $subject The worksheet to iterate over |
42
|
|
|
* @param string $columnIndex The column that we want to iterate |
43
|
|
|
* @param int $startRow The row number at which to start iterating |
44
|
|
|
* @param int $endRow Optionally, the row number at which to stop iterating |
45
|
|
|
*/ |
46
|
6 |
|
public function __construct(Worksheet $subject = null, $columnIndex = 'A', $startRow = 1, $endRow = null) |
47
|
|
|
{ |
48
|
|
|
// Set subject |
49
|
6 |
|
$this->worksheet = $subject; |
50
|
6 |
|
$this->columnIndex = Coordinate::columnIndexFromString($columnIndex); |
51
|
6 |
|
$this->resetEnd($endRow); |
52
|
6 |
|
$this->resetStart($startRow); |
53
|
6 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* (Re)Set the start row and the current row pointer. |
57
|
|
|
* |
58
|
|
|
* @param int $startRow The row number at which to start iterating |
59
|
|
|
* |
60
|
|
|
* @throws PhpSpreadsheetException |
61
|
|
|
* |
62
|
|
|
* @return ColumnCellIterator |
63
|
|
|
*/ |
64
|
6 |
|
public function resetStart($startRow = 1) |
65
|
|
|
{ |
66
|
6 |
|
$this->startRow = $startRow; |
67
|
6 |
|
$this->adjustForExistingOnlyRange(); |
68
|
6 |
|
$this->seek($startRow); |
69
|
|
|
|
70
|
6 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* (Re)Set the end row. |
75
|
|
|
* |
76
|
|
|
* @param int $endRow The row number at which to stop iterating |
77
|
|
|
* |
78
|
|
|
* @throws PhpSpreadsheetException |
79
|
|
|
* |
80
|
|
|
* @return ColumnCellIterator |
81
|
|
|
*/ |
82
|
6 |
|
public function resetEnd($endRow = null) |
83
|
|
|
{ |
84
|
6 |
|
$this->endRow = ($endRow) ? $endRow : $this->worksheet->getHighestRow(); |
85
|
6 |
|
$this->adjustForExistingOnlyRange(); |
86
|
|
|
|
87
|
6 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set the row pointer to the selected row. |
92
|
|
|
* |
93
|
|
|
* @param int $row The row number to set the current pointer at |
94
|
|
|
* |
95
|
|
|
* @throws PhpSpreadsheetException |
96
|
|
|
* |
97
|
|
|
* @return ColumnCellIterator |
98
|
|
|
*/ |
99
|
6 |
|
public function seek($row = 1) |
100
|
|
|
{ |
101
|
6 |
|
if (($row < $this->startRow) || ($row > $this->endRow)) { |
102
|
1 |
|
throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})"); |
103
|
6 |
|
} elseif ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $row))) { |
|
|
|
|
104
|
|
|
throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist'); |
105
|
|
|
} |
106
|
6 |
|
$this->currentRow = $row; |
107
|
|
|
|
108
|
6 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Rewind the iterator to the starting row. |
113
|
|
|
*/ |
114
|
2 |
|
public function rewind() |
115
|
|
|
{ |
116
|
2 |
|
$this->currentRow = $this->startRow; |
117
|
2 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return the current cell in this worksheet column. |
121
|
|
|
* |
122
|
|
|
* @return null|\PhpOffice\PhpSpreadsheet\Cell\Cell |
123
|
|
|
*/ |
124
|
2 |
|
public function current() |
125
|
|
|
{ |
126
|
2 |
|
return $this->worksheet->getCellByColumnAndRow($this->columnIndex, $this->currentRow); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Return the current iterator key. |
131
|
|
|
* |
132
|
|
|
* @return int |
133
|
|
|
*/ |
134
|
3 |
|
public function key() |
135
|
|
|
{ |
136
|
3 |
|
return $this->currentRow; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Set the iterator to its next value. |
141
|
|
|
*/ |
142
|
2 |
|
public function next() |
143
|
|
|
{ |
144
|
|
|
do { |
145
|
2 |
|
++$this->currentRow; |
146
|
2 |
|
} while (($this->onlyExistingCells) && |
147
|
2 |
|
(!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) && |
148
|
2 |
|
($this->currentRow <= $this->endRow)); |
149
|
2 |
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set the iterator to its previous value. |
153
|
|
|
*/ |
154
|
2 |
|
public function prev() |
155
|
|
|
{ |
156
|
|
|
do { |
157
|
2 |
|
--$this->currentRow; |
158
|
2 |
|
} while (($this->onlyExistingCells) && |
159
|
2 |
|
(!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->currentRow)) && |
160
|
2 |
|
($this->currentRow >= $this->startRow)); |
161
|
2 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Indicate if more rows exist in the worksheet range of rows that we're iterating. |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
3 |
|
public function valid() |
169
|
|
|
{ |
170
|
3 |
|
return $this->currentRow <= $this->endRow && $this->currentRow >= $this->startRow; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary. |
175
|
|
|
* |
176
|
|
|
* @throws PhpSpreadsheetException |
177
|
|
|
*/ |
178
|
6 |
|
protected function adjustForExistingOnlyRange() |
179
|
|
|
{ |
180
|
6 |
|
if ($this->onlyExistingCells) { |
181
|
|
|
while ((!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->startRow)) && |
|
|
|
|
182
|
|
|
($this->startRow <= $this->endRow)) { |
183
|
|
|
++$this->startRow; |
184
|
|
|
} |
185
|
|
|
if ($this->startRow > $this->endRow) { |
186
|
|
|
throw new PhpSpreadsheetException('No cells exist within the specified range'); |
187
|
|
|
} |
188
|
|
|
while ((!$this->worksheet->cellExistsByColumnAndRow($this->columnIndex, $this->endRow)) && |
189
|
|
|
($this->endRow >= $this->startRow)) { |
190
|
|
|
--$this->endRow; |
191
|
|
|
} |
192
|
|
|
if ($this->endRow < $this->startRow) { |
193
|
|
|
throw new PhpSpreadsheetException('No cells exist within the specified range'); |
194
|
|
|
} |
195
|
|
|
} |
196
|
6 |
|
} |
197
|
|
|
} |
198
|
|
|
|