ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RowCellIterator.php
Go to the documentation of this file.
1<?php
2
4
7use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
8
10{
17
23 private $rowIndex = 1;
24
30 private $startColumnIndex = 1;
31
37 private $endColumnIndex = 1;
38
47 public function __construct(?Worksheet $worksheet = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
48 {
49 // Set subject and row index
50 $this->worksheet = $worksheet;
51 $this->rowIndex = $rowIndex;
52 $this->resetEnd($endColumn);
53 $this->resetStart($startColumn);
54 }
55
63 public function resetStart(string $startColumn = 'A')
64 {
65 $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn);
67 $this->seek(Coordinate::stringFromColumnIndex($this->startColumnIndex));
68
69 return $this;
70 }
71
79 public function resetEnd($endColumn = null)
80 {
81 $endColumn = $endColumn ?: $this->worksheet->getHighestColumn();
82 $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);
84
85 return $this;
86 }
87
95 public function seek(string $column = 'A')
96 {
97 $columnx = $column;
98 $column = Coordinate::columnIndexFromString($column);
99 if ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($column, $this->rowIndex))) {
100 throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
101 }
102 if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) {
103 throw new PhpSpreadsheetException("Column $columnx is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})");
104 }
105 $this->currentColumnIndex = $column;
106
107 return $this;
108 }
109
113 public function rewind(): void
114 {
115 $this->currentColumnIndex = $this->startColumnIndex;
116 }
117
121 public function current(): ?Cell
122 {
123 return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex);
124 }
125
129 public function key(): string
130 {
131 return Coordinate::stringFromColumnIndex($this->currentColumnIndex);
132 }
133
137 public function next(): void
138 {
139 do {
141 } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex <= $this->endColumnIndex));
142 }
143
147 public function prev(): void
148 {
149 do {
151 } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex >= $this->startColumnIndex));
152 }
153
159 public function valid()
160 {
161 return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex;
162 }
163
167 public function getCurrentColumnIndex(): int
168 {
170 }
171
175 protected function adjustForExistingOnlyRange(): void
176 {
177 if ($this->onlyExistingCells) {
178 while ((!$this->worksheet->cellExistsByColumnAndRow($this->startColumnIndex, $this->rowIndex)) && ($this->startColumnIndex <= $this->endColumnIndex)) {
180 }
181 while ((!$this->worksheet->cellExistsByColumnAndRow($this->endColumnIndex, $this->rowIndex)) && ($this->endColumnIndex >= $this->startColumnIndex)) {
183 }
184 }
185 }
186}
An exception for terminatinating execution or to throw for unit testing.
Helper class to manipulate cell coordinates.
Definition: Coordinate.php:15
static columnIndexFromString($pString)
Column index from string.
Definition: Coordinate.php:265
static stringFromColumnIndex($columnIndex)
String from column index.
Definition: Coordinate.php:313
prev()
Set the iterator to its previous value.
adjustForExistingOnlyRange()
Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
valid()
Indicate if more columns exist in the worksheet range of columns that we're iterating.
next()
Set the iterator to its next value.
resetStart(string $startColumn='A')
(Re)Set the start column and the current column pointer.
__construct(?Worksheet $worksheet=null, $rowIndex=1, $startColumn='A', $endColumn=null)
Create a new column iterator.
getCurrentColumnIndex()
Return the current iterator position.
resetEnd($endColumn=null)
(Re)Set the end column.
seek(string $column='A')
Set the column pointer to the selected column.
current()
Return the current cell in this worksheet row.
rewind()
Rewind the iterator to the starting column.