4
* PHPExcel_Worksheet_RowCellIterator
6
* Copyright (c) 2006 - 2015 PHPExcel
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
* @package PHPExcel_Worksheet
24
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
25
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
26
* @version ##VERSION##, ##DATE##
28
class PHPExcel_Worksheet_RowCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
42
protected $startColumn = 0;
49
protected $endColumn = 0;
52
* Create a new column iterator
54
* @param PHPExcel_Worksheet $subject The worksheet to iterate over
55
* @param integer $rowIndex The row that we want to iterate
56
* @param string $startColumn The column address at which to start iterating
57
* @param string $endColumn Optionally, the column address at which to stop iterating
59
public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
61
// Set subject and row index
62
$this->subject = $subject;
63
$this->rowIndex = $rowIndex;
64
$this->resetEnd($endColumn);
65
$this->resetStart($startColumn);
71
public function __destruct()
73
unset($this->subject);
77
* (Re)Set the start column and the current column pointer
79
* @param integer $startColumn The column address at which to start iterating
80
* @return PHPExcel_Worksheet_RowCellIterator
81
* @throws PHPExcel_Exception
83
public function resetStart($startColumn = 'A')
85
$startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
86
$this->startColumn = $startColumnIndex;
87
$this->adjustForExistingOnlyRange();
88
$this->seek(PHPExcel_Cell::stringFromColumnIndex($this->startColumn));
94
* (Re)Set the end column
96
* @param string $endColumn The column address at which to stop iterating
97
* @return PHPExcel_Worksheet_RowCellIterator
98
* @throws PHPExcel_Exception
100
public function resetEnd($endColumn = null)
102
$endColumn = ($endColumn) ? $endColumn : $this->subject->getHighestColumn();
103
$this->endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
104
$this->adjustForExistingOnlyRange();
110
* Set the column pointer to the selected column
112
* @param string $column The column address to set the current pointer at
113
* @return PHPExcel_Worksheet_RowCellIterator
114
* @throws PHPExcel_Exception
116
public function seek($column = 'A')
118
$column = PHPExcel_Cell::columnIndexFromString($column) - 1;
119
if (($column < $this->startColumn) || ($column > $this->endColumn)) {
120
throw new PHPExcel_Exception("Column $column is out of range ({$this->startColumn} - {$this->endColumn})");
121
} elseif ($this->onlyExistingCells && !($this->subject->cellExistsByColumnAndRow($column, $this->rowIndex))) {
122
throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
124
$this->position = $column;
130
* Rewind the iterator to the starting column
132
public function rewind()
134
$this->position = $this->startColumn;
138
* Return the current cell in this worksheet row
140
* @return PHPExcel_Cell
142
public function current()
144
return $this->subject->getCellByColumnAndRow($this->position, $this->rowIndex);
148
* Return the current iterator key
152
public function key()
154
return PHPExcel_Cell::stringFromColumnIndex($this->position);
158
* Set the iterator to its next value
160
public function next()
164
} while (($this->onlyExistingCells) &&
165
(!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) &&
166
($this->position <= $this->endColumn));
170
* Set the iterator to its previous value
172
* @throws PHPExcel_Exception
174
public function prev()
176
if ($this->position <= $this->startColumn) {
177
throw new PHPExcel_Exception(
178
"Column is already at the beginning of range (" .
179
PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . " - " .
180
PHPExcel_Cell::stringFromColumnIndex($this->endColumn) . ")"
186
} while (($this->onlyExistingCells) &&
187
(!$this->subject->cellExistsByColumnAndRow($this->position, $this->rowIndex)) &&
188
($this->position >= $this->startColumn));
192
* Indicate if more columns exist in the worksheet range of columns that we're iterating
196
public function valid()
198
return $this->position <= $this->endColumn;
202
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
204
* @throws PHPExcel_Exception
206
protected function adjustForExistingOnlyRange()
208
if ($this->onlyExistingCells) {
209
while ((!$this->subject->cellExistsByColumnAndRow($this->startColumn, $this->rowIndex)) &&
210
($this->startColumn <= $this->endColumn)) {
211
++$this->startColumn;
213
if ($this->startColumn > $this->endColumn) {
214
throw new PHPExcel_Exception('No cells exist within the specified range');
216
while ((!$this->subject->cellExistsByColumnAndRow($this->endColumn, $this->rowIndex)) &&
217
($this->endColumn >= $this->startColumn)) {
220
if ($this->endColumn < $this->startColumn) {
221
throw new PHPExcel_Exception('No cells exist within the specified range');