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\Table;
4
 
5
use PhpOffice\PhpSpreadsheet\Cell\DataType;
6
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
7
use PhpOffice\PhpSpreadsheet\Spreadsheet;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
9
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
10
 
11
class Column
12
{
13
    /**
14
     * Table Column Index.
15
     */
16
    private string $columnIndex;
17
 
18
    /**
19
     * Show Filter Button.
20
     */
21
    private bool $showFilterButton = true;
22
 
23
    /**
24
     * Total Row Label.
25
     */
26
    private ?string $totalsRowLabel = null;
27
 
28
    /**
29
     * Total Row Function.
30
     */
31
    private ?string $totalsRowFunction = null;
32
 
33
    /**
34
     * Total Row Formula.
35
     */
36
    private ?string $totalsRowFormula = null;
37
 
38
    /**
39
     * Column Formula.
40
     */
41
    private ?string $columnFormula = null;
42
 
43
    /**
44
     * Table.
45
     */
46
    private ?Table $table;
47
 
48
    /**
49
     * Create a new Column.
50
     *
51
     * @param string $column Column (e.g. A)
52
     * @param ?Table $table Table for this column
53
     */
54
    public function __construct(string $column, ?Table $table = null)
55
    {
56
        $this->columnIndex = $column;
57
        $this->table = $table;
58
    }
59
 
60
    /**
61
     * Get Table column index as string eg: 'A'.
62
     */
63
    public function getColumnIndex(): string
64
    {
65
        return $this->columnIndex;
66
    }
67
 
68
    /**
69
     * Set Table column index as string eg: 'A'.
70
     *
71
     * @param string $column Column (e.g. A)
72
     */
73
    public function setColumnIndex(string $column): self
74
    {
75
        // Uppercase coordinate
76
        $column = strtoupper($column);
77
        if ($this->table !== null) {
78
            $this->table->isColumnInRange($column);
79
        }
80
 
81
        $this->columnIndex = $column;
82
 
83
        return $this;
84
    }
85
 
86
    /**
87
     * Get show Filter Button.
88
     */
89
    public function getShowFilterButton(): bool
90
    {
91
        return $this->showFilterButton;
92
    }
93
 
94
    /**
95
     * Set show Filter Button.
96
     */
97
    public function setShowFilterButton(bool $showFilterButton): self
98
    {
99
        $this->showFilterButton = $showFilterButton;
100
 
101
        return $this;
102
    }
103
 
104
    /**
105
     * Get total Row Label.
106
     */
107
    public function getTotalsRowLabel(): ?string
108
    {
109
        return $this->totalsRowLabel;
110
    }
111
 
112
    /**
113
     * Set total Row Label.
114
     */
115
    public function setTotalsRowLabel(string $totalsRowLabel): self
116
    {
117
        $this->totalsRowLabel = $totalsRowLabel;
118
 
119
        return $this;
120
    }
121
 
122
    /**
123
     * Get total Row Function.
124
     */
125
    public function getTotalsRowFunction(): ?string
126
    {
127
        return $this->totalsRowFunction;
128
    }
129
 
130
    /**
131
     * Set total Row Function.
132
     */
133
    public function setTotalsRowFunction(string $totalsRowFunction): self
134
    {
135
        $this->totalsRowFunction = $totalsRowFunction;
136
 
137
        return $this;
138
    }
139
 
140
    /**
141
     * Get total Row Formula.
142
     */
143
    public function getTotalsRowFormula(): ?string
144
    {
145
        return $this->totalsRowFormula;
146
    }
147
 
148
    /**
149
     * Set total Row Formula.
150
     */
151
    public function setTotalsRowFormula(string $totalsRowFormula): self
152
    {
153
        $this->totalsRowFormula = $totalsRowFormula;
154
 
155
        return $this;
156
    }
157
 
158
    /**
159
     * Get column Formula.
160
     */
161
    public function getColumnFormula(): ?string
162
    {
163
        return $this->columnFormula;
164
    }
165
 
166
    /**
167
     * Set column Formula.
168
     */
169
    public function setColumnFormula(string $columnFormula): self
170
    {
171
        $this->columnFormula = $columnFormula;
172
 
173
        return $this;
174
    }
175
 
176
    /**
177
     * Get this Column's Table.
178
     */
179
    public function getTable(): ?Table
180
    {
181
        return $this->table;
182
    }
183
 
184
    /**
185
     * Set this Column's Table.
186
     */
187
    public function setTable(?Table $table = null): self
188
    {
189
        $this->table = $table;
190
 
191
        return $this;
192
    }
193
 
194
    public static function updateStructuredReferences(?Worksheet $workSheet, ?string $oldTitle, ?string $newTitle): void
195
    {
196
        if ($workSheet === null || $oldTitle === null || $oldTitle === '' || $newTitle === null) {
197
            return;
198
        }
199
 
200
        // Remember that table headings are case-insensitive
201
        if (StringHelper::strToLower($oldTitle) !== StringHelper::strToLower($newTitle)) {
202
            // We need to check all formula cells that might contain Structured References that refer
203
            //    to this column, and update those formulae to reference the new column text
204
            $spreadsheet = $workSheet->getParentOrThrow();
205
            foreach ($spreadsheet->getWorksheetIterator() as $sheet) {
206
                self::updateStructuredReferencesInCells($sheet, $oldTitle, $newTitle);
207
            }
208
            self::updateStructuredReferencesInNamedFormulae($spreadsheet, $oldTitle, $newTitle);
209
        }
210
    }
211
 
212
    private static function updateStructuredReferencesInCells(Worksheet $worksheet, string $oldTitle, string $newTitle): void
213
    {
214
        $pattern = '/\[(@?)' . preg_quote($oldTitle, '/') . '\]/mui';
215
 
216
        foreach ($worksheet->getCoordinates(false) as $coordinate) {
217
            $cell = $worksheet->getCell($coordinate);
218
            if ($cell->getDataType() === DataType::TYPE_FORMULA) {
219
                $formula = $cell->getValueString();
220
                if (preg_match($pattern, $formula) === 1) {
221
                    $formula = preg_replace($pattern, "[$1{$newTitle}]", $formula);
222
                    $cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
223
                }
224
            }
225
        }
226
    }
227
 
228
    private static function updateStructuredReferencesInNamedFormulae(Spreadsheet $spreadsheet, string $oldTitle, string $newTitle): void
229
    {
230
        $pattern = '/\[(@?)' . preg_quote($oldTitle, '/') . '\]/mui';
231
 
232
        foreach ($spreadsheet->getNamedFormulae() as $namedFormula) {
233
            $formula = $namedFormula->getValue();
234
            if (preg_match($pattern, $formula) === 1) {
235
                $formula = preg_replace($pattern, "[$1{$newTitle}]", $formula) ?? '';
236
                $namedFormula->setValue($formula);
237
            }
238
        }
239
    }
240
}