Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
 
5
use PhpOffice\PhpSpreadsheet\Cell\Cell;
6
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
7
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
8
 
9
/**
10
 * @extends CellIterator<string>
11
 */
12
class RowCellIterator extends CellIterator
13
{
14
    /**
15
     * Current iterator position.
16
     */
17
    private int $currentColumnIndex;
18
 
19
    /**
20
     * Row index.
21
     */
22
    private int $rowIndex;
23
 
24
    /**
25
     * Start position.
26
     */
27
    private int $startColumnIndex = 1;
28
 
29
    /**
30
     * End position.
31
     */
32
    private int $endColumnIndex = 1;
33
 
34
    /**
35
     * Create a new column iterator.
36
     *
37
     * @param Worksheet $worksheet The worksheet to iterate over
38
     * @param int $rowIndex The row that we want to iterate
39
     * @param string $startColumn The column address at which to start iterating
40
     * @param ?string $endColumn Optionally, the column address at which to stop iterating
41
     */
42
    public function __construct(Worksheet $worksheet, int $rowIndex = 1, string $startColumn = 'A', ?string $endColumn = null, bool $iterateOnlyExistingCells = false)
43
    {
44
        // Set subject and row index
45
        $this->worksheet = $worksheet;
46
        $this->cellCollection = $worksheet->getCellCollection();
47
        $this->rowIndex = $rowIndex;
48
        $this->resetEnd($endColumn);
49
        $this->resetStart($startColumn);
50
        $this->setIterateOnlyExistingCells($iterateOnlyExistingCells);
51
    }
52
 
53
    /**
54
     * (Re)Set the start column and the current column pointer.
55
     *
56
     * @param string $startColumn The column address at which to start iterating
57
     *
58
     * @return $this
59
     */
60
    public function resetStart(string $startColumn = 'A'): static
61
    {
62
        $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn);
63
        $this->adjustForExistingOnlyRange();
64
        $this->seek(Coordinate::stringFromColumnIndex($this->startColumnIndex));
65
 
66
        return $this;
67
    }
68
 
69
    /**
70
     * (Re)Set the end column.
71
     *
72
     * @param ?string $endColumn The column address at which to stop iterating
73
     *
74
     * @return $this
75
     */
76
    public function resetEnd(?string $endColumn = null): static
77
    {
78
        $endColumn = $endColumn ?: $this->worksheet->getHighestColumn();
79
        $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);
80
        $this->adjustForExistingOnlyRange();
81
 
82
        return $this;
83
    }
84
 
85
    /**
86
     * Set the column pointer to the selected column.
87
     *
88
     * @param string $column The column address to set the current pointer at
89
     *
90
     * @return $this
91
     */
92
    public function seek(string $column = 'A'): static
93
    {
94
        $columnId = Coordinate::columnIndexFromString($column);
95
        if ($this->onlyExistingCells && !($this->cellCollection->has($column . $this->rowIndex))) {
96
            throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
97
        }
98
        if (($columnId < $this->startColumnIndex) || ($columnId > $this->endColumnIndex)) {
99
            throw new PhpSpreadsheetException("Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})");
100
        }
101
        $this->currentColumnIndex = $columnId;
102
 
103
        return $this;
104
    }
105
 
106
    /**
107
     * Rewind the iterator to the starting column.
108
     */
109
    public function rewind(): void
110
    {
111
        $this->currentColumnIndex = $this->startColumnIndex;
112
    }
113
 
114
    /**
115
     * Return the current cell in this worksheet row.
116
     */
117
    public function current(): ?Cell
118
    {
119
        $cellAddress = Coordinate::stringFromColumnIndex($this->currentColumnIndex) . $this->rowIndex;
120
 
121
        return $this->cellCollection->has($cellAddress)
122
            ? $this->cellCollection->get($cellAddress)
123
            : (
124
                $this->ifNotExists === self::IF_NOT_EXISTS_CREATE_NEW
125
                ? $this->worksheet->createNewCell($cellAddress)
126
                : null
127
            );
128
    }
129
 
130
    /**
131
     * Return the current iterator key.
132
     */
133
    public function key(): string
134
    {
135
        return Coordinate::stringFromColumnIndex($this->currentColumnIndex);
136
    }
137
 
138
    /**
139
     * Set the iterator to its next value.
140
     */
141
    public function next(): void
142
    {
143
        do {
144
            ++$this->currentColumnIndex;
145
        } while (($this->onlyExistingCells) && (!$this->cellCollection->has(Coordinate::stringFromColumnIndex($this->currentColumnIndex) . $this->rowIndex)) && ($this->currentColumnIndex <= $this->endColumnIndex));
146
    }
147
 
148
    /**
149
     * Set the iterator to its previous value.
150
     */
151
    public function prev(): void
152
    {
153
        do {
154
            --$this->currentColumnIndex;
155
        } while (($this->onlyExistingCells) && (!$this->cellCollection->has(Coordinate::stringFromColumnIndex($this->currentColumnIndex) . $this->rowIndex)) && ($this->currentColumnIndex >= $this->startColumnIndex));
156
    }
157
 
158
    /**
159
     * Indicate if more columns exist in the worksheet range of columns that we're iterating.
160
     */
161
    public function valid(): bool
162
    {
163
        return $this->currentColumnIndex <= $this->endColumnIndex && $this->currentColumnIndex >= $this->startColumnIndex;
164
    }
165
 
166
    /**
167
     * Return the current iterator position.
168
     */
169
    public function getCurrentColumnIndex(): int
170
    {
171
        return $this->currentColumnIndex;
172
    }
173
 
174
    /**
175
     * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
176
     */
177
    protected function adjustForExistingOnlyRange(): void
178
    {
179
        if ($this->onlyExistingCells) {
180
            while ((!$this->cellCollection->has(Coordinate::stringFromColumnIndex($this->startColumnIndex) . $this->rowIndex)) && ($this->startColumnIndex <= $this->endColumnIndex)) {
181
                ++$this->startColumnIndex;
182
            }
183
            while ((!$this->cellCollection->has(Coordinate::stringFromColumnIndex($this->endColumnIndex) . $this->rowIndex)) && ($this->endColumnIndex >= $this->startColumnIndex)) {
184
                --$this->endColumnIndex;
185
            }
186
        }
187
    }
188
}