1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
8
|
|
|
|
9
|
|
|
class ColumnIterator implements \Iterator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Worksheet to iterate. |
13
|
|
|
* |
14
|
|
|
* @var Worksheet |
15
|
|
|
*/ |
16
|
|
|
private $worksheet; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Current iterator position. |
20
|
|
|
* |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $currentColumnIndex = 1; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Start position. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $startColumnIndex = 1; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* End position. |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $endColumnIndex = 1; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new column iterator. |
41
|
|
|
* |
42
|
|
|
* @param Worksheet $worksheet The worksheet to iterate over |
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
|
5 |
|
public function __construct(Worksheet $worksheet, $startColumn = 'A', $endColumn = null) |
47
|
|
|
{ |
48
|
|
|
// Set subject |
49
|
5 |
|
$this->worksheet = $worksheet; |
50
|
5 |
|
$this->resetEnd($endColumn); |
51
|
5 |
|
$this->resetStart($startColumn); |
52
|
5 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Destructor. |
56
|
|
|
*/ |
57
|
5 |
|
public function __destruct() |
58
|
|
|
{ |
59
|
5 |
|
unset($this->worksheet); |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* (Re)Set the start column and the current column pointer. |
64
|
|
|
* |
65
|
|
|
* @param string $startColumn The column address at which to start iterating |
66
|
|
|
* |
67
|
|
|
* @throws Exception |
68
|
|
|
* |
69
|
|
|
* @return ColumnIterator |
70
|
|
|
*/ |
71
|
5 |
|
public function resetStart($startColumn = 'A') |
72
|
|
|
{ |
73
|
5 |
|
$startColumnIndex = Coordinate::columnIndexFromString($startColumn); |
74
|
5 |
|
if ($startColumnIndex > Coordinate::columnIndexFromString($this->worksheet->getHighestColumn())) { |
75
|
|
|
throw new Exception("Start column ({$startColumn}) is beyond highest column ({$this->worksheet->getHighestColumn()})"); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
$this->startColumnIndex = $startColumnIndex; |
79
|
5 |
|
if ($this->endColumnIndex < $this->startColumnIndex) { |
80
|
|
|
$this->endColumnIndex = $this->startColumnIndex; |
81
|
|
|
} |
82
|
5 |
|
$this->seek($startColumn); |
83
|
|
|
|
84
|
5 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* (Re)Set the end column. |
89
|
|
|
* |
90
|
|
|
* @param string $endColumn The column address at which to stop iterating |
91
|
|
|
* |
92
|
|
|
* @return ColumnIterator |
93
|
|
|
*/ |
94
|
5 |
|
public function resetEnd($endColumn = null) |
95
|
|
|
{ |
96
|
5 |
|
$endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn(); |
97
|
5 |
|
$this->endColumnIndex = Coordinate::columnIndexFromString($endColumn); |
98
|
|
|
|
99
|
5 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set the column pointer to the selected column. |
104
|
|
|
* |
105
|
|
|
* @param string $column The column address to set the current pointer at |
106
|
|
|
* |
107
|
|
|
* @throws PhpSpreadsheetException |
108
|
|
|
* |
109
|
|
|
* @return ColumnIterator |
110
|
|
|
*/ |
111
|
5 |
|
public function seek($column = 'A') |
112
|
|
|
{ |
113
|
5 |
|
$column = Coordinate::columnIndexFromString($column); |
114
|
5 |
|
if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) { |
115
|
1 |
|
throw new PhpSpreadsheetException("Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})"); |
116
|
|
|
} |
117
|
5 |
|
$this->currentColumnIndex = $column; |
118
|
|
|
|
119
|
5 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Rewind the iterator to the starting column. |
124
|
|
|
*/ |
125
|
2 |
|
public function rewind() |
126
|
|
|
{ |
127
|
2 |
|
$this->currentColumnIndex = $this->startColumnIndex; |
128
|
2 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return the current column in this worksheet. |
132
|
|
|
* |
133
|
|
|
* @return Column |
134
|
|
|
*/ |
135
|
2 |
|
public function current() |
136
|
|
|
{ |
137
|
2 |
|
return new Column($this->worksheet, Coordinate::stringFromColumnIndex($this->currentColumnIndex)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Return the current iterator key. |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
3 |
|
public function key() |
146
|
|
|
{ |
147
|
3 |
|
return Coordinate::stringFromColumnIndex($this->currentColumnIndex); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set the iterator to its next value. |
152
|
|
|
*/ |
153
|
2 |
|
public function next() |
154
|
|
|
{ |
155
|
2 |
|
++$this->currentColumnIndex; |
156
|
2 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the iterator to its previous value. |
160
|
|
|
*/ |
161
|
2 |
|
public function prev() |
162
|
|
|
{ |
163
|
2 |
|
--$this->currentColumnIndex; |
164
|
2 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Indicate if more columns exist in the worksheet range of columns that we're iterating. |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
3 |
|
public function valid() |
172
|
|
|
{ |
173
|
3 |
|
return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|