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