| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHPExcel |
| 4 |
* |
| 5 |
* Copyright (c) 2006 - 2014 PHPExcel |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
* |
| 21 |
* @category PHPExcel |
| 22 |
* @package PHPExcel_Worksheet |
| 23 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 24 |
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
| 25 |
* @version ##VERSION##, ##DATE## |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* PHPExcel_Worksheet_RowCellIterator |
| 31 |
* |
| 32 |
* Used to iterate columns in a PHPExcel_Worksheet |
| 33 |
* |
| 34 |
* @category PHPExcel |
| 35 |
* @package PHPExcel_Worksheet |
| 36 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 37 |
*/ |
| 38 |
class PHPExcel_Worksheet_RowCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator |
| 39 |
{ |
| 40 |
/** |
| 41 |
* Row index |
| 42 |
* |
| 43 |
* @var int |
| 44 |
*/ |
| 45 |
protected $_rowIndex; |
| 46 |
|
| 47 |
/** |
| 48 |
* Start position |
| 49 |
* |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
protected $_startColumn = 0; |
| 53 |
|
| 54 |
/** |
| 55 |
* End position |
| 56 |
* |
| 57 |
* @var int |
| 58 |
*/ |
| 59 |
protected $_endColumn = 0; |
| 60 |
|
| 61 |
/** |
| 62 |
* Create a new column iterator |
| 63 |
* |
| 64 |
* @param PHPExcel_Worksheet $subject The worksheet to iterate over |
| 65 |
* @param integer $rowIndex The row that we want to iterate |
| 66 |
* @param string $startColumn The column address at which to start iterating |
| 67 |
* @param string $endColumn Optionally, the column address at which to stop iterating |
| 68 |
*/ |
| 69 |
public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null) { |
| 70 |
// Set subject and row index |
| 71 |
$this->_subject = $subject; |
| 72 |
$this->_rowIndex = $rowIndex; |
| 73 |
$this->resetEnd($endColumn); |
| 74 |
$this->resetStart($startColumn); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Destructor |
| 79 |
*/ |
| 80 |
public function __destruct() { |
| 81 |
unset($this->_subject); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* (Re)Set the start column and the current column pointer |
| 86 |
* |
| 87 |
* @param integer $startColumn The column address at which to start iterating |
| 88 |
* @return PHPExcel_Worksheet_RowCellIterator |
| 89 |
* @throws PHPExcel_Exception |
| 90 |
*/ |
| 91 |
public function resetStart($startColumn = 'A') { |
| 92 |
$startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1; |
| 93 |
$this->_startColumn = $startColumnIndex; |
| 94 |
$this->adjustForExistingOnlyRange(); |
| 95 |
$this->seek(PHPExcel_Cell::stringFromColumnIndex($this->_startColumn)); |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* (Re)Set the end column |
| 102 |
* |
| 103 |
* @param string $endColumn The column address at which to stop iterating |
| 104 |
* @return PHPExcel_Worksheet_RowCellIterator |
| 105 |
* @throws PHPExcel_Exception |
| 106 |
*/ |
| 107 |
public function resetEnd($endColumn = null) { |
| 108 |
$endColumn = ($endColumn) ? $endColumn : $this->_subject->getHighestColumn(); |
| 109 |
$this->_endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1; |
| 110 |
$this->adjustForExistingOnlyRange(); |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Set the column pointer to the selected column |
| 117 |
* |
| 118 |
* @param string $column The column address to set the current pointer at |
| 119 |
* @return PHPExcel_Worksheet_RowCellIterator |
| 120 |
* @throws PHPExcel_Exception |
| 121 |
*/ |
| 122 |
public function seek($column = 'A') { |
| 123 |
$column = PHPExcel_Cell::columnIndexFromString($column) - 1; |
| 124 |
if (($column < $this->_startColumn) || ($column > $this->_endColumn)) { |
| 125 |
throw new PHPExcel_Exception("Column $column is out of range ({$this->_startColumn} - {$this->_endColumn})"); |
| 126 |
} elseif ($this->_onlyExistingCells && !($this->_subject->cellExistsByColumnAndRow($column, $this->_rowIndex))) { |
| 127 |
throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist'); |
| 128 |
} |
| 129 |
$this->_position = $column; |
| 130 |
|
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Rewind the iterator to the starting column |
| 136 |
*/ |
| 137 |
public function rewind() { |
| 138 |
$this->_position = $this->_startColumn; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Return the current cell in this worksheet row |
| 143 |
* |
| 144 |
* @return PHPExcel_Cell |
| 145 |
*/ |
| 146 |
public function current() { |
| 147 |
return $this->_subject->getCellByColumnAndRow($this->_position, $this->_rowIndex); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Return the current iterator key |
| 152 |
* |
| 153 |
* @return string |
| 154 |
*/ |
| 155 |
public function key() { |
| 156 |
return PHPExcel_Cell::stringFromColumnIndex($this->_position); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Set the iterator to its next value |
| 161 |
*/ |
| 162 |
public function next() { |
| 163 |
do { |
| 164 |
++$this->_position; |
| 165 |
} while (($this->_onlyExistingCells) && |
| 166 |
(!$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) && |
| 167 |
($this->_position <= $this->_endColumn)); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Set the iterator to its previous value |
| 172 |
* |
| 173 |
* @throws PHPExcel_Exception |
| 174 |
*/ |
| 175 |
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) . ")" |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
do { |
| 185 |
--$this->_position; |
| 186 |
} while (($this->_onlyExistingCells) && |
| 187 |
(!$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) && |
| 188 |
($this->_position >= $this->_startColumn)); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Indicate if more columns exist in the worksheet range of columns that we're iterating |
| 193 |
* |
| 194 |
* @return boolean |
| 195 |
*/ |
| 196 |
public function valid() { |
| 197 |
return $this->_position <= $this->_endColumn; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary |
| 202 |
* |
| 203 |
* @throws PHPExcel_Exception |
| 204 |
*/ |
| 205 |
protected function adjustForExistingOnlyRange() { |
| 206 |
if ($this->_onlyExistingCells) { |
| 207 |
while ((!$this->_subject->cellExistsByColumnAndRow($this->_startColumn, $this->_rowIndex)) && |
| 208 |
($this->_startColumn <= $this->_endColumn)) { |
| 209 |
++$this->_startColumn; |
| 210 |
} |
| 211 |
if ($this->_startColumn > $this->_endColumn) { |
| 212 |
throw new PHPExcel_Exception('No cells exist within the specified range'); |
| 213 |
} |
| 214 |
while ((!$this->_subject->cellExistsByColumnAndRow($this->_endColumn, $this->_rowIndex)) && |
| 215 |
($this->_endColumn >= $this->_startColumn)) { |
| 216 |
--$this->_endColumn; |
| 217 |
} |
| 218 |
if ($this->_endColumn < $this->_startColumn) { |
| 219 |
throw new PHPExcel_Exception('No cells exist within the specified range'); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|