| 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_ColumnIterator |
| 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_ColumnIterator implements Iterator |
| 39 |
{ |
| 40 |
/** |
| 41 |
* PHPExcel_Worksheet to iterate |
| 42 |
* |
| 43 |
* @var PHPExcel_Worksheet |
| 44 |
*/ |
| 45 |
private $_subject; |
| 46 |
|
| 47 |
/** |
| 48 |
* Current iterator position |
| 49 |
* |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
private $_position = 0; |
| 53 |
|
| 54 |
/** |
| 55 |
* Start position |
| 56 |
* |
| 57 |
* @var int |
| 58 |
*/ |
| 59 |
private $_startColumn = 0; |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* End position |
| 64 |
* |
| 65 |
* @var int |
| 66 |
*/ |
| 67 |
private $_endColumn = 0; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Create a new column iterator |
| 72 |
* |
| 73 |
* @param PHPExcel_Worksheet $subject The worksheet to iterate over |
| 74 |
* @param string $startColumn The column address at which to start iterating |
| 75 |
* @param string $endColumn Optionally, the column address at which to stop iterating |
| 76 |
*/ |
| 77 |
public function __construct(PHPExcel_Worksheet $subject = null, $startColumn = 'A', $endColumn = null) { |
| 78 |
// Set subject |
| 79 |
$this->_subject = $subject; |
| 80 |
$this->resetEnd($endColumn); |
| 81 |
$this->resetStart($startColumn); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Destructor |
| 86 |
*/ |
| 87 |
public function __destruct() { |
| 88 |
unset($this->_subject); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* (Re)Set the start column and the current column pointer |
| 93 |
* |
| 94 |
* @param integer $startColumn The column address at which to start iterating |
| 95 |
* @return PHPExcel_Worksheet_ColumnIterator |
| 96 |
*/ |
| 97 |
public function resetStart($startColumn = 'A') { |
| 98 |
$startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1; |
| 99 |
$this->_startColumn = $startColumnIndex; |
| 100 |
$this->seek($startColumn); |
| 101 |
|
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* (Re)Set the end column |
| 107 |
* |
| 108 |
* @param string $endColumn The column address at which to stop iterating |
| 109 |
* @return PHPExcel_Worksheet_ColumnIterator |
| 110 |
*/ |
| 111 |
public function resetEnd($endColumn = null) { |
| 112 |
$endColumn = ($endColumn) ? $endColumn : $this->_subject->getHighestColumn(); |
| 113 |
$this->_endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1; |
| 114 |
|
| 115 |
return $this; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Set the column pointer to the selected column |
| 120 |
* |
| 121 |
* @param string $column The column address to set the current pointer at |
| 122 |
* @return PHPExcel_Worksheet_ColumnIterator |
| 123 |
* @throws PHPExcel_Exception |
| 124 |
*/ |
| 125 |
public function seek($column = 'A') { |
| 126 |
$column = PHPExcel_Cell::columnIndexFromString($column) - 1; |
| 127 |
if (($column < $this->_startColumn) || ($column > $this->_endColumn)) { |
| 128 |
throw new PHPExcel_Exception("Column $column is out of range ({$this->_startColumn} - {$this->_endColumn})"); |
| 129 |
} |
| 130 |
$this->_position = $column; |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Rewind the iterator to the starting column |
| 137 |
*/ |
| 138 |
public function rewind() { |
| 139 |
$this->_position = $this->_startColumn; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Return the current column in this worksheet |
| 144 |
* |
| 145 |
* @return PHPExcel_Worksheet_Column |
| 146 |
*/ |
| 147 |
public function current() { |
| 148 |
return new PHPExcel_Worksheet_Column($this->_subject, PHPExcel_Cell::stringFromColumnIndex($this->_position)); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Return the current iterator key |
| 153 |
* |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
public function key() { |
| 157 |
return PHPExcel_Cell::stringFromColumnIndex($this->_position); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Set the iterator to its next value |
| 162 |
*/ |
| 163 |
public function next() { |
| 164 |
++$this->_position; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Set the iterator to its previous value |
| 169 |
* |
| 170 |
* @throws PHPExcel_Exception |
| 171 |
*/ |
| 172 |
public function prev() { |
| 173 |
if ($this->_position <= $this->_startColumn) { |
| 174 |
throw new PHPExcel_Exception( |
| 175 |
"Column is already at the beginning of range (" . |
| 176 |
PHPExcel_Cell::stringFromColumnIndex($this->_endColumn) . " - " . |
| 177 |
PHPExcel_Cell::stringFromColumnIndex($this->_endColumn) . ")" |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
--$this->_position; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Indicate if more columns exist in the worksheet range of columns that we're iterating |
| 186 |
* |
| 187 |
* @return boolean |
| 188 |
*/ |
| 189 |
public function valid() { |
| 190 |
return $this->_position <= $this->_endColumn; |
| 191 |
} |
| 192 |
} |
| 193 |
|