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