1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\AddressRange; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\AutoFilter; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
13
|
|
|
|
14
|
|
|
class ReferenceHelper |
15
|
|
|
{ |
16
|
|
|
/** Constants */ |
17
|
|
|
/** Regular Expressions */ |
18
|
|
|
private const SHEETNAME_PART = '((\w*|\'[^!]*\')!)'; |
19
|
|
|
private const SHEETNAME_PART_WITH_SLASHES = '/' . self::SHEETNAME_PART . '/'; |
20
|
|
|
const REFHELPER_REGEXP_CELLREF = self::SHEETNAME_PART . '?(?<![:a-z1-9_\.\$])(\$?[a-z]{1,3}\$?\d+)(?=[^:!\d\'])'; |
21
|
|
|
const REFHELPER_REGEXP_CELLRANGE = self::SHEETNAME_PART . '?(\$?[a-z]{1,3}\$?\d+):(\$?[a-z]{1,3}\$?\d+)'; |
22
|
|
|
const REFHELPER_REGEXP_ROWRANGE = self::SHEETNAME_PART . '?(\$?\d+):(\$?\d+)'; |
23
|
|
|
const REFHELPER_REGEXP_COLRANGE = self::SHEETNAME_PART . '?(\$?[a-z]{1,3}):(\$?[a-z]{1,3})'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Instance of this class. |
27
|
|
|
*/ |
28
|
|
|
private static ?ReferenceHelper $instance = null; |
29
|
|
|
|
30
|
|
|
private ?CellReferenceHelper $cellReferenceHelper = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get an instance of this class. |
34
|
|
|
*/ |
35
|
1660 |
|
public static function getInstance(): self |
36
|
|
|
{ |
37
|
1660 |
|
if (self::$instance === null) { |
38
|
82 |
|
self::$instance = new self(); |
39
|
|
|
} |
40
|
|
|
|
41
|
1660 |
|
return self::$instance; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new ReferenceHelper. |
46
|
|
|
*/ |
47
|
82 |
|
protected function __construct() |
48
|
|
|
{ |
49
|
82 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Compare two column addresses |
53
|
|
|
* Intended for use as a Callback function for sorting column addresses by column. |
54
|
|
|
* |
55
|
|
|
* @param string $a First column to test (e.g. 'AA') |
56
|
|
|
* @param string $b Second column to test (e.g. 'Z') |
57
|
|
|
*/ |
58
|
1 |
|
public static function columnSort(string $a, string $b): int |
59
|
|
|
{ |
60
|
1 |
|
return strcasecmp(strlen($a) . $a, strlen($b) . $b); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Compare two column addresses |
65
|
|
|
* Intended for use as a Callback function for reverse sorting column addresses by column. |
66
|
|
|
* |
67
|
|
|
* @param string $a First column to test (e.g. 'AA') |
68
|
|
|
* @param string $b Second column to test (e.g. 'Z') |
69
|
|
|
*/ |
70
|
1 |
|
public static function columnReverseSort(string $a, string $b): int |
71
|
|
|
{ |
72
|
1 |
|
return -strcasecmp(strlen($a) . $a, strlen($b) . $b); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Compare two cell addresses |
77
|
|
|
* Intended for use as a Callback function for sorting cell addresses by column and row. |
78
|
|
|
* |
79
|
|
|
* @param string $a First cell to test (e.g. 'AA1') |
80
|
|
|
* @param string $b Second cell to test (e.g. 'Z1') |
81
|
|
|
*/ |
82
|
20 |
|
public static function cellSort(string $a, string $b): int |
83
|
|
|
{ |
84
|
20 |
|
sscanf($a, '%[A-Z]%d', $ac, $ar); |
85
|
|
|
/** @var int $ar */ |
86
|
|
|
/** @var string $ac */ |
87
|
20 |
|
sscanf($b, '%[A-Z]%d', $bc, $br); |
88
|
|
|
/** @var int $br */ |
89
|
|
|
/** @var string $bc */ |
90
|
20 |
|
if ($ar === $br) { |
91
|
1 |
|
return strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc); |
92
|
|
|
} |
93
|
|
|
|
94
|
20 |
|
return ($ar < $br) ? -1 : 1; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Compare two cell addresses |
99
|
|
|
* Intended for use as a Callback function for sorting cell addresses by column and row. |
100
|
|
|
* |
101
|
|
|
* @param string $a First cell to test (e.g. 'AA1') |
102
|
|
|
* @param string $b Second cell to test (e.g. 'Z1') |
103
|
|
|
*/ |
104
|
23 |
|
public static function cellReverseSort(string $a, string $b): int |
105
|
|
|
{ |
106
|
23 |
|
sscanf($a, '%[A-Z]%d', $ac, $ar); |
107
|
|
|
/** @var int $ar */ |
108
|
|
|
/** @var string $ac */ |
109
|
23 |
|
sscanf($b, '%[A-Z]%d', $bc, $br); |
110
|
|
|
/** @var int $br */ |
111
|
|
|
/** @var string $bc */ |
112
|
23 |
|
if ($ar === $br) { |
113
|
2 |
|
return -strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc); |
114
|
|
|
} |
115
|
|
|
|
116
|
22 |
|
return ($ar < $br) ? 1 : -1; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Update page breaks when inserting/deleting rows/columns. |
121
|
|
|
* |
122
|
|
|
* @param Worksheet $worksheet The worksheet that we're editing |
123
|
|
|
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion) |
124
|
|
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
125
|
|
|
*/ |
126
|
94 |
|
protected function adjustPageBreaks(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void |
127
|
|
|
{ |
128
|
94 |
|
$aBreaks = $worksheet->getBreaks(); |
129
|
94 |
|
($numberOfColumns > 0 || $numberOfRows > 0) |
130
|
60 |
|
? uksort($aBreaks, [self::class, 'cellReverseSort']) |
131
|
55 |
|
: uksort($aBreaks, [self::class, 'cellSort']); |
132
|
|
|
|
133
|
94 |
|
foreach ($aBreaks as $cellAddress => $value) { |
134
|
|
|
/** @var CellReferenceHelper */ |
135
|
4 |
|
$cellReferenceHelper = $this->cellReferenceHelper; |
136
|
4 |
|
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) { |
137
|
|
|
// If we're deleting, then clear any defined breaks that are within the range |
138
|
|
|
// of rows/columns that we're deleting |
139
|
1 |
|
$worksheet->setBreak($cellAddress, Worksheet::BREAK_NONE); |
140
|
|
|
} else { |
141
|
|
|
// Otherwise update any affected breaks by inserting a new break at the appropriate point |
142
|
|
|
// and removing the old affected break |
143
|
4 |
|
$newReference = $this->updateCellReference($cellAddress); |
144
|
4 |
|
if ($cellAddress !== $newReference) { |
145
|
4 |
|
$worksheet->setBreak($newReference, $value) |
146
|
4 |
|
->setBreak($cellAddress, Worksheet::BREAK_NONE); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Update cell comments when inserting/deleting rows/columns. |
154
|
|
|
* |
155
|
|
|
* @param Worksheet $worksheet The worksheet that we're editing |
156
|
|
|
*/ |
157
|
94 |
|
protected function adjustComments(Worksheet $worksheet): void |
158
|
|
|
{ |
159
|
94 |
|
$aComments = $worksheet->getComments(); |
160
|
94 |
|
$aNewComments = []; // the new array of all comments |
161
|
|
|
|
162
|
94 |
|
foreach ($aComments as $cellAddress => &$value) { |
163
|
|
|
// Any comments inside a deleted range will be ignored |
164
|
|
|
/** @var CellReferenceHelper */ |
165
|
21 |
|
$cellReferenceHelper = $this->cellReferenceHelper; |
166
|
21 |
|
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === false) { |
167
|
|
|
// Otherwise build a new array of comments indexed by the adjusted cell reference |
168
|
21 |
|
$newReference = $this->updateCellReference($cellAddress); |
169
|
21 |
|
$aNewComments[$newReference] = $value; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
// Replace the comments array with the new set of comments |
173
|
94 |
|
$worksheet->setComments($aNewComments); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Update hyperlinks when inserting/deleting rows/columns. |
178
|
|
|
* |
179
|
|
|
* @param Worksheet $worksheet The worksheet that we're editing |
180
|
|
|
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion) |
181
|
|
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
182
|
|
|
*/ |
183
|
94 |
|
protected function adjustHyperlinks(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void |
184
|
|
|
{ |
185
|
94 |
|
$aHyperlinkCollection = $worksheet->getHyperlinkCollection(); |
186
|
94 |
|
($numberOfColumns > 0 || $numberOfRows > 0) |
187
|
60 |
|
? uksort($aHyperlinkCollection, [self::class, 'cellReverseSort']) |
188
|
55 |
|
: uksort($aHyperlinkCollection, [self::class, 'cellSort']); |
189
|
|
|
|
190
|
94 |
|
foreach ($aHyperlinkCollection as $cellAddress => $value) { |
191
|
20 |
|
$newReference = $this->updateCellReference($cellAddress); |
192
|
|
|
/** @var CellReferenceHelper */ |
193
|
20 |
|
$cellReferenceHelper = $this->cellReferenceHelper; |
194
|
20 |
|
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) { |
195
|
|
|
$worksheet->setHyperlink($cellAddress, null); |
196
|
20 |
|
} elseif ($cellAddress !== $newReference) { |
197
|
20 |
|
$worksheet->setHyperlink($cellAddress, null); |
198
|
20 |
|
if ($newReference) { |
199
|
|
|
$worksheet->setHyperlink($newReference, $value); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Update conditional formatting styles when inserting/deleting rows/columns. |
207
|
|
|
* |
208
|
|
|
* @param Worksheet $worksheet The worksheet that we're editing |
209
|
|
|
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion) |
210
|
94 |
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
211
|
|
|
*/ |
212
|
94 |
|
protected function adjustConditionalFormatting(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void |
213
|
94 |
|
{ |
214
|
60 |
|
$aStyles = $worksheet->getConditionalStylesCollection(); |
215
|
55 |
|
($numberOfColumns > 0 || $numberOfRows > 0) |
216
|
|
|
? uksort($aStyles, [self::class, 'cellReverseSort']) |
217
|
94 |
|
: uksort($aStyles, [self::class, 'cellSort']); |
218
|
4 |
|
|
219
|
4 |
|
foreach ($aStyles as $cellAddress => $cfRules) { |
220
|
|
|
$worksheet->removeConditionalStyles($cellAddress); |
221
|
4 |
|
$newReference = $this->updateCellReference($cellAddress); |
222
|
|
|
|
223
|
4 |
|
foreach ($cfRules as &$cfRule) { |
224
|
4 |
|
/** @var Conditional $cfRule */ |
225
|
4 |
|
$conditions = $cfRule->getConditions(); |
226
|
|
|
foreach ($conditions as &$condition) { |
227
|
4 |
|
if (is_string($condition)) { |
228
|
4 |
|
/** @var CellReferenceHelper */ |
229
|
4 |
|
$cellReferenceHelper = $this->cellReferenceHelper; |
230
|
4 |
|
$condition = $this->updateFormulaReferences( |
231
|
4 |
|
$condition, |
232
|
4 |
|
$cellReferenceHelper->beforeCellAddress(), |
233
|
4 |
|
$numberOfColumns, |
234
|
4 |
|
$numberOfRows, |
235
|
4 |
|
$worksheet->getTitle(), |
236
|
|
|
true |
237
|
|
|
); |
238
|
4 |
|
} |
239
|
|
|
} |
240
|
4 |
|
$cfRule->setConditions($conditions); |
241
|
|
|
} |
242
|
|
|
$worksheet->setConditionalStyles($newReference, $cfRules); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Update data validations when inserting/deleting rows/columns. |
248
|
|
|
* |
249
|
|
|
* @param Worksheet $worksheet The worksheet that we're editing |
250
|
|
|
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion) |
251
|
94 |
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
252
|
|
|
*/ |
253
|
94 |
|
protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows, string $beforeCellAddress): void |
254
|
94 |
|
{ |
255
|
60 |
|
$aDataValidationCollection = $worksheet->getDataValidationCollection(); |
256
|
55 |
|
($numberOfColumns > 0 || $numberOfRows > 0) |
257
|
|
|
? uksort($aDataValidationCollection, [self::class, 'cellReverseSort']) |
258
|
94 |
|
: uksort($aDataValidationCollection, [self::class, 'cellSort']); |
259
|
7 |
|
|
260
|
7 |
|
foreach ($aDataValidationCollection as $cellAddress => $dataValidation) { |
261
|
7 |
|
$formula = $dataValidation->getFormula1(); |
262
|
7 |
|
if ($formula !== '') { |
263
|
7 |
|
$dataValidation->setFormula1( |
264
|
7 |
|
$this->updateFormulaReferences( |
265
|
7 |
|
$formula, |
266
|
7 |
|
$beforeCellAddress, |
267
|
7 |
|
$numberOfColumns, |
268
|
7 |
|
$numberOfRows, |
269
|
7 |
|
$worksheet->getTitle(), |
270
|
7 |
|
true |
271
|
|
|
) |
272
|
7 |
|
); |
273
|
7 |
|
} |
274
|
1 |
|
$formula = $dataValidation->getFormula2(); |
275
|
1 |
|
if ($formula !== '') { |
276
|
1 |
|
$dataValidation->setFormula2( |
277
|
1 |
|
$this->updateFormulaReferences( |
278
|
1 |
|
$formula, |
279
|
1 |
|
$beforeCellAddress, |
280
|
1 |
|
$numberOfColumns, |
281
|
1 |
|
$numberOfRows, |
282
|
1 |
|
$worksheet->getTitle(), |
283
|
1 |
|
true |
284
|
|
|
) |
285
|
7 |
|
); |
286
|
7 |
|
} |
287
|
7 |
|
$addressParts = explode(' ', $cellAddress); |
288
|
7 |
|
$newReference = ''; |
289
|
7 |
|
$separator = ''; |
290
|
7 |
|
foreach ($addressParts as $addressPart) { |
291
|
|
|
$newReference .= $separator . $this->updateCellReference($addressPart); |
292
|
7 |
|
$separator = ' '; |
293
|
7 |
|
} |
294
|
7 |
|
if ($cellAddress !== $newReference) { |
295
|
|
|
$worksheet->setDataValidation($newReference, $dataValidation); |
296
|
|
|
$worksheet->setDataValidation($cellAddress, null); |
297
|
|
|
if ($newReference) { |
298
|
|
|
$worksheet->setDataValidation($newReference, $dataValidation); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
94 |
|
/** |
305
|
|
|
* Update merged cells when inserting/deleting rows/columns. |
306
|
94 |
|
* |
307
|
94 |
|
* @param Worksheet $worksheet The worksheet that we're editing |
308
|
94 |
|
*/ |
309
|
21 |
|
protected function adjustMergeCells(Worksheet $worksheet): void |
310
|
21 |
|
{ |
311
|
|
|
$aMergeCells = $worksheet->getMergeCells(); |
312
|
94 |
|
$aNewMergeCells = []; // the new array of all merge cells |
313
|
|
|
foreach ($aMergeCells as $cellAddress => &$value) { |
314
|
|
|
$newReference = $this->updateCellReference($cellAddress); |
315
|
|
|
if ($newReference) { |
316
|
|
|
$aNewMergeCells[$newReference] = $newReference; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
$worksheet->setMergeCells($aNewMergeCells); // replace the merge cells array |
320
|
|
|
} |
321
|
|
|
|
322
|
94 |
|
/** |
323
|
|
|
* Update protected cells when inserting/deleting rows/columns. |
324
|
94 |
|
* |
325
|
94 |
|
* @param Worksheet $worksheet The worksheet that we're editing |
326
|
60 |
|
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion) |
327
|
55 |
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
328
|
94 |
|
*/ |
329
|
17 |
|
protected function adjustProtectedCells(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void |
330
|
17 |
|
{ |
331
|
17 |
|
$aProtectedCells = $worksheet->getProtectedCellRanges(); |
332
|
17 |
|
($numberOfColumns > 0 || $numberOfRows > 0) |
333
|
|
|
? uksort($aProtectedCells, [self::class, 'cellReverseSort']) |
334
|
|
|
: uksort($aProtectedCells, [self::class, 'cellSort']); |
335
|
|
|
foreach ($aProtectedCells as $cellAddress => $protectedRange) { |
336
|
|
|
$newReference = $this->updateCellReference($cellAddress); |
337
|
|
|
if ($cellAddress !== $newReference) { |
338
|
|
|
$worksheet->unprotectCells($cellAddress); |
339
|
|
|
if ($newReference) { |
340
|
|
|
$worksheet->protectCells($newReference, $protectedRange->getPassword(), true); |
341
|
|
|
} |
342
|
94 |
|
} |
343
|
|
|
} |
344
|
94 |
|
} |
345
|
94 |
|
|
346
|
25 |
|
/** |
347
|
25 |
|
* Update column dimensions when inserting/deleting rows/columns. |
348
|
25 |
|
* |
349
|
25 |
|
* @param Worksheet $worksheet The worksheet that we're editing |
350
|
19 |
|
*/ |
351
|
|
|
protected function adjustColumnDimensions(Worksheet $worksheet): void |
352
|
|
|
{ |
353
|
|
|
$aColumnDimensions = array_reverse($worksheet->getColumnDimensions(), true); |
354
|
25 |
|
if (!empty($aColumnDimensions)) { |
355
|
|
|
foreach ($aColumnDimensions as $objColumnDimension) { |
356
|
|
|
$newReference = $this->updateCellReference($objColumnDimension->getColumnIndex() . '1'); |
357
|
|
|
[$newReference] = Coordinate::coordinateFromString($newReference); |
358
|
|
|
if ($objColumnDimension->getColumnIndex() !== $newReference) { |
359
|
|
|
$objColumnDimension->setColumnIndex($newReference); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
$worksheet->refreshColumnDimensions(); |
364
|
|
|
} |
365
|
94 |
|
} |
366
|
|
|
|
367
|
94 |
|
/** |
368
|
94 |
|
* Update row dimensions when inserting/deleting rows/columns. |
369
|
7 |
|
* |
370
|
7 |
|
* @param Worksheet $worksheet The worksheet that we're editing |
371
|
7 |
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
372
|
7 |
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
373
|
7 |
|
*/ |
374
|
7 |
|
protected function adjustRowDimensions(Worksheet $worksheet, int $beforeRow, int $numberOfRows): void |
375
|
|
|
{ |
376
|
|
|
$aRowDimensions = array_reverse($worksheet->getRowDimensions(), true); |
377
|
|
|
if (!empty($aRowDimensions)) { |
378
|
7 |
|
foreach ($aRowDimensions as $objRowDimension) { |
379
|
|
|
$newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex()); |
380
|
7 |
|
[, $newReference] = Coordinate::coordinateFromString($newReference); |
381
|
7 |
|
$newRoweference = (int) $newReference; |
382
|
5 |
|
if ($objRowDimension->getRowIndex() !== $newRoweference) { |
383
|
5 |
|
$objRowDimension->setRowIndex($newRoweference); |
384
|
5 |
|
} |
385
|
5 |
|
} |
386
|
5 |
|
|
387
|
|
|
$worksheet->refreshRowDimensions(); |
388
|
|
|
|
389
|
|
|
$copyDimension = $worksheet->getRowDimension($beforeRow - 1); |
390
|
|
|
for ($i = $beforeRow; $i <= $beforeRow - 1 + $numberOfRows; ++$i) { |
391
|
|
|
$newDimension = $worksheet->getRowDimension($i); |
392
|
|
|
$newDimension->setRowHeight($copyDimension->getRowHeight()); |
393
|
|
|
$newDimension->setVisible($copyDimension->getVisible()); |
394
|
|
|
$newDimension->setOutlineLevel($copyDimension->getOutlineLevel()); |
395
|
|
|
$newDimension->setCollapsed($copyDimension->getCollapsed()); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
} |
399
|
94 |
|
|
400
|
|
|
/** |
401
|
|
|
* Insert a new column or row, updating all possible related data. |
402
|
|
|
* |
403
|
|
|
* @param string $beforeCellAddress Insert before this cell address (e.g. 'A1') |
404
|
|
|
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion) |
405
|
94 |
|
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion) |
406
|
|
|
* @param Worksheet $worksheet The worksheet that we're editing |
407
|
|
|
*/ |
408
|
94 |
|
public function insertNewBefore( |
409
|
94 |
|
string $beforeCellAddress, |
410
|
|
|
int $numberOfColumns, |
411
|
82 |
|
int $numberOfRows, |
412
|
|
|
Worksheet $worksheet |
413
|
|
|
): void { |
414
|
|
|
$remove = ($numberOfColumns < 0 || $numberOfRows < 0); |
415
|
94 |
|
|
416
|
|
|
if ( |
417
|
|
|
$this->cellReferenceHelper === null |
418
|
94 |
|
|| $this->cellReferenceHelper->refreshRequired($beforeCellAddress, $numberOfColumns, $numberOfRows) |
419
|
94 |
|
) { |
420
|
94 |
|
$this->cellReferenceHelper = new CellReferenceHelper($beforeCellAddress, $numberOfColumns, $numberOfRows); |
421
|
94 |
|
} |
422
|
|
|
|
423
|
|
|
// Get coordinate of $beforeCellAddress |
424
|
94 |
|
[$beforeColumn, $beforeRow, $beforeColumnString] = Coordinate::indexesFromString($beforeCellAddress); |
425
|
23 |
|
|
426
|
|
|
// Clear cells if we are removing columns or rows |
427
|
|
|
$highestColumn = $worksheet->getHighestColumn(); |
428
|
|
|
$highestDataColumn = $worksheet->getHighestDataColumn(); |
429
|
94 |
|
$highestRow = $worksheet->getHighestRow(); |
430
|
35 |
|
$highestDataRow = $worksheet->getHighestDataRow(); |
431
|
|
|
|
432
|
|
|
// 1. Clear column strips if we are removing columns |
433
|
|
|
if ($numberOfColumns < 0 && $beforeColumn - 2 + $numberOfColumns > 0) { |
434
|
94 |
|
$this->clearColumnStrips($highestRow, $beforeColumn, $numberOfColumns, $worksheet); |
435
|
94 |
|
} |
436
|
94 |
|
|
437
|
58 |
|
// 2. Clear row strips if we are removing rows |
438
|
58 |
|
if ($numberOfRows < 0 && $beforeRow - 1 + $numberOfRows > 0) { |
439
|
57 |
|
$this->clearRowStrips($highestColumn, $beforeColumn, $beforeRow, $numberOfRows, $worksheet); |
440
|
57 |
|
} |
441
|
|
|
|
442
|
94 |
|
// Find missing coordinates. This is important when inserting or deleting column before the last column |
443
|
94 |
|
$startRow = $startCol = 1; |
444
|
84 |
|
$startColString = 'A'; |
445
|
78 |
|
if ($numberOfRows === 0) { |
446
|
|
|
$startCol = $beforeColumn; |
447
|
|
|
$startColString = $beforeColumnString; |
448
|
|
|
} elseif ($numberOfColumns === 0) { |
449
|
94 |
|
$startRow = $beforeRow; |
450
|
94 |
|
} |
451
|
|
|
$highColumn = Coordinate::columnIndexFromString($highestDataColumn); |
452
|
55 |
|
for ($row = $startRow; $row <= $highestDataRow; ++$row) { |
453
|
|
|
for ($col = $startCol, $colString = $startColString; $col <= $highColumn; ++$col, ++$colString) { |
454
|
|
|
$worksheet->getCell("$colString$row"); // create cell if it doesn't exist |
455
|
|
|
} |
456
|
94 |
|
} |
457
|
89 |
|
|
458
|
89 |
|
$allCoordinates = $worksheet->getCoordinates(); |
459
|
|
|
if ($remove) { |
460
|
89 |
|
// It's faster to reverse and pop than to use unshift, especially with large cell collections |
461
|
26 |
|
$allCoordinates = array_reverse($allCoordinates); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// Loop through cells, bottom-up, and change cell coordinate |
465
|
88 |
|
while ($coordinate = array_pop($allCoordinates)) { |
466
|
|
|
$cell = $worksheet->getCell($coordinate); |
467
|
|
|
$cellIndex = Coordinate::columnIndexFromString($cell->getColumn()); |
468
|
88 |
|
|
469
|
|
|
// Don't update cells that are being removed |
470
|
78 |
|
if ($numberOfColumns < 0 && $cellIndex >= $beforeColumn + $numberOfColumns && $cellIndex < $beforeColumn) { |
471
|
|
|
continue; |
472
|
|
|
} |
473
|
78 |
|
|
474
|
|
|
// New coordinate |
475
|
37 |
|
$newCoordinate = Coordinate::stringFromColumnIndex($cellIndex + $numberOfColumns) . ($cell->getRow() + $numberOfRows); |
476
|
37 |
|
|
477
|
|
|
// Should the cell be updated? Move value and cellXf index from one cell to another. |
478
|
|
|
if (($cellIndex >= $beforeColumn) && ($cell->getRow() >= $beforeRow)) { |
479
|
78 |
|
// Update cell styles |
480
|
|
|
$worksheet->getCell($newCoordinate)->setXfIndex($cell->getXfIndex()); |
481
|
|
|
|
482
|
|
|
// Insert this cell at its new location |
483
|
78 |
|
if ($cell->getDataType() === DataType::TYPE_FORMULA) { |
484
|
|
|
// Formula should be adjusted |
485
|
|
|
$worksheet->getCell($newCoordinate) |
486
|
|
|
->setValue($this->updateFormulaReferences($cell->getValueString(), $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle(), true)); |
487
|
59 |
|
} else { |
488
|
|
|
// Cell value should not be adjusted |
489
|
20 |
|
$worksheet->getCell($newCoordinate)->setValueExplicit($cell->getValue(), $cell->getDataType()); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
// Clear the original cell |
493
|
|
|
$worksheet->getCellCollection()->delete($coordinate); |
494
|
|
|
} else { |
495
|
94 |
|
/* We don't need to update styles for rows/columns before our insertion position, |
496
|
94 |
|
but we do still need to adjust any formulae in those cells */ |
497
|
|
|
if ($cell->getDataType() === DataType::TYPE_FORMULA) { |
498
|
94 |
|
// Formula should be adjusted |
499
|
23 |
|
$cell->setValue($this->updateFormulaReferences($cell->getValueString(), $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle(), true)); |
500
|
|
|
} |
501
|
|
|
} |
502
|
94 |
|
} |
503
|
34 |
|
|
504
|
|
|
// Duplicate styles for the newly inserted cells |
505
|
|
|
$highestColumn = $worksheet->getHighestColumn(); |
506
|
|
|
$highestRow = $worksheet->getHighestRow(); |
507
|
94 |
|
|
508
|
|
|
if ($numberOfColumns > 0 && $beforeColumn - 2 > 0) { |
509
|
|
|
$this->duplicateStylesByColumn($worksheet, $beforeColumn, $beforeRow, $highestRow, $numberOfColumns); |
510
|
94 |
|
} |
511
|
|
|
|
512
|
|
|
if ($numberOfRows > 0 && $beforeRow - 1 > 0) { |
513
|
94 |
|
$this->duplicateStylesByRow($worksheet, $beforeColumn, $beforeRow, $highestColumn, $numberOfRows); |
514
|
|
|
} |
515
|
|
|
|
516
|
94 |
|
// Update worksheet: column dimensions |
517
|
|
|
$this->adjustColumnDimensions($worksheet); |
518
|
|
|
|
519
|
94 |
|
// Update worksheet: row dimensions |
520
|
|
|
$this->adjustRowDimensions($worksheet, $beforeRow, $numberOfRows); |
521
|
|
|
|
522
|
94 |
|
// Update worksheet: page breaks |
523
|
|
|
$this->adjustPageBreaks($worksheet, $numberOfColumns, $numberOfRows); |
524
|
|
|
|
525
|
94 |
|
// Update worksheet: comments |
526
|
|
|
$this->adjustComments($worksheet); |
527
|
|
|
|
528
|
94 |
|
// Update worksheet: hyperlinks |
529
|
|
|
$this->adjustHyperlinks($worksheet, $numberOfColumns, $numberOfRows); |
530
|
|
|
|
531
|
94 |
|
// Update worksheet: conditional formatting styles |
532
|
|
|
$this->adjustConditionalFormatting($worksheet, $numberOfColumns, $numberOfRows); |
533
|
|
|
|
534
|
94 |
|
// Update worksheet: data validations |
535
|
|
|
$this->adjustDataValidations($worksheet, $numberOfColumns, $numberOfRows, $beforeCellAddress); |
536
|
|
|
|
537
|
94 |
|
// Update worksheet: merge cells |
538
|
|
|
$this->adjustMergeCells($worksheet); |
539
|
|
|
|
540
|
94 |
|
// Update worksheet: protected cells |
541
|
1 |
|
$this->adjustProtectedCells($worksheet, $numberOfColumns, $numberOfRows); |
542
|
1 |
|
|
543
|
|
|
// Update worksheet: autofilter |
544
|
1 |
|
$this->adjustAutoFilter($worksheet, $beforeCellAddress, $numberOfColumns); |
545
|
1 |
|
|
546
|
|
|
// Update worksheet: table |
547
|
1 |
|
$this->adjustTable($worksheet, $beforeCellAddress, $numberOfColumns); |
548
|
|
|
|
549
|
|
|
// Update worksheet: freeze pane |
550
|
|
|
if ($worksheet->getFreezePane()) { |
551
|
94 |
|
$splitCell = $worksheet->getFreezePane(); |
552
|
6 |
|
$topLeftCell = $worksheet->getTopLeftCell() ?? ''; |
553
|
6 |
|
|
554
|
6 |
|
$splitCell = $this->updateCellReference($splitCell); |
555
|
|
|
$topLeftCell = $this->updateCellReference($topLeftCell); |
556
|
|
|
|
557
|
|
|
$worksheet->freezePane($splitCell, $topLeftCell); |
558
|
94 |
|
} |
559
|
94 |
|
|
560
|
19 |
|
// Page setup |
561
|
19 |
|
if ($worksheet->getPageSetup()->isPrintAreaSet()) { |
562
|
19 |
|
$worksheet->getPageSetup()->setPrintArea( |
563
|
|
|
$this->updateCellReference($worksheet->getPageSetup()->getPrintArea()) |
564
|
19 |
|
); |
565
|
1 |
|
} |
566
|
1 |
|
|
567
|
1 |
|
// Update worksheet: drawings |
568
|
|
|
$aDrawings = $worksheet->getDrawingCollection(); |
569
|
|
|
foreach ($aDrawings as $objDrawing) { |
570
|
|
|
$newReference = $this->updateCellReference($objDrawing->getCoordinates()); |
571
|
|
|
if ($objDrawing->getCoordinates() != $newReference) { |
572
|
|
|
$objDrawing->setCoordinates($newReference); |
573
|
94 |
|
} |
574
|
7 |
|
if ($objDrawing->getCoordinates2() !== '') { |
575
|
|
|
$newReference = $this->updateCellReference($objDrawing->getCoordinates2()); |
576
|
|
|
if ($objDrawing->getCoordinates2() != $newReference) { |
577
|
|
|
$objDrawing->setCoordinates2($newReference); |
578
|
94 |
|
} |
579
|
|
|
} |
580
|
|
|
} |
581
|
270 |
|
|
582
|
|
|
// Update workbook: define names |
583
|
270 |
|
if (count($worksheet->getParentOrThrow()->getDefinedNames()) > 0) { |
584
|
|
|
$this->updateDefinedNames($worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows); |
585
|
|
|
} |
586
|
279 |
|
|
587
|
|
|
// Garbage collect |
588
|
279 |
|
$worksheet->garbageCollect(); |
589
|
|
|
} |
590
|
279 |
|
|
591
|
|
|
private static function matchSheetName(?string $match, string $worksheetName): bool |
592
|
|
|
{ |
593
|
|
|
return $match === null || $match === '' || $match === "'\u{fffc}'" || $match === "'\u{fffb}'" || strcasecmp(trim($match, "'"), $worksheetName) === 0; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
private static function sheetnameBeforeCells(string $match, string $worksheetName, string $cells): string |
597
|
|
|
{ |
598
|
|
|
$toString = ($match > '') ? "$match!" : ''; |
599
|
|
|
|
600
|
|
|
return str_replace(["\u{fffc}", "'\u{fffb}'"], $worksheetName, $toString) . $cells; |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
/** |
604
|
293 |
|
* Update references within formulas. |
605
|
|
|
* |
606
|
|
|
* @param string $formula Formula to update |
607
|
|
|
* @param string $beforeCellAddress Insert before this one |
608
|
|
|
* @param int $numberOfColumns Number of columns to insert |
609
|
|
|
* @param int $numberOfRows Number of rows to insert |
610
|
|
|
* @param string $worksheetName Worksheet name/title |
611
|
|
|
* |
612
|
|
|
* @return string Updated formula |
613
|
293 |
|
*/ |
614
|
|
|
public function updateFormulaReferences( |
615
|
293 |
|
string $formula = '', |
616
|
293 |
|
string $beforeCellAddress = 'A1', |
617
|
|
|
int $numberOfColumns = 0, |
618
|
232 |
|
int $numberOfRows = 0, |
619
|
|
|
string $worksheetName = '', |
620
|
|
|
bool $includeAbsoluteReferences = false, |
621
|
|
|
bool $onlyAbsoluteReferences = false |
622
|
293 |
|
): string { |
623
|
293 |
|
$callback = fn (array $matches): string => (strcasecmp(trim($matches[2], "'"), $worksheetName) === 0) ? (($matches[2][0] === "'") ? "'\u{fffc}'!" : "'\u{fffb}'!") : "'\u{fffd}'!"; |
624
|
293 |
|
if ( |
625
|
|
|
$this->cellReferenceHelper === null |
626
|
293 |
|
|| $this->cellReferenceHelper->refreshRequired($beforeCellAddress, $numberOfColumns, $numberOfRows) |
627
|
293 |
|
) { |
628
|
293 |
|
$this->cellReferenceHelper = new CellReferenceHelper($beforeCellAddress, $numberOfColumns, $numberOfRows); |
629
|
293 |
|
} |
630
|
|
|
|
631
|
293 |
|
// Update cell references in the formula |
632
|
293 |
|
$formulaBlocks = explode('"', $formula); |
633
|
293 |
|
$i = false; |
634
|
3 |
|
foreach ($formulaBlocks as &$formulaBlock) { |
635
|
3 |
|
// Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode) |
636
|
3 |
|
$i = $i === false; |
637
|
3 |
|
if ($i) { |
638
|
|
|
$adjustCount = 0; |
639
|
3 |
|
$newCellTokens = $cellTokens = []; |
640
|
3 |
|
// Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5) |
641
|
3 |
|
$formulaBlockx = ' ' . (preg_replace_callback(self::SHEETNAME_PART_WITH_SLASHES, $callback, $formulaBlock) ?? $formulaBlock) . ' '; |
642
|
|
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_ROWRANGE . '/mui', $formulaBlockx, $matches, PREG_SET_ORDER); |
643
|
3 |
|
if ($matchCount > 0) { |
644
|
3 |
|
foreach ($matches as $match) { |
645
|
3 |
|
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}:{$match[4]}"); |
646
|
|
|
$modified3 = substr($this->updateCellReference('$A' . $match[3], $includeAbsoluteReferences, $onlyAbsoluteReferences, true), 2); |
647
|
3 |
|
$modified4 = substr($this->updateCellReference('$A' . $match[4], $includeAbsoluteReferences, $onlyAbsoluteReferences, false), 2); |
648
|
3 |
|
|
649
|
3 |
|
if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) { |
650
|
|
|
if (self::matchSheetName($match[2], $worksheetName)) { |
651
|
|
|
$toString = self::sheetnameBeforeCells($match[2], $worksheetName, "$modified3:$modified4"); |
652
|
|
|
// Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more |
653
|
|
|
$column = 100000; |
654
|
|
|
$row = 10000000 + (int) trim($match[3], '$'); |
655
|
293 |
|
$cellIndex = "{$column}{$row}"; |
656
|
293 |
|
|
657
|
293 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
658
|
3 |
|
$cellTokens[$cellIndex] = '/(?<!\d\$\!)' . preg_quote($fromString, '/') . '(?!\d)/i'; |
659
|
3 |
|
++$adjustCount; |
660
|
3 |
|
} |
661
|
3 |
|
} |
662
|
|
|
} |
663
|
3 |
|
} |
664
|
3 |
|
// Search for column ranges (e.g. 'Sheet1'!C:E or C:E) with or without $ absolutes (e.g. $C:E) |
665
|
3 |
|
$formulaBlockx = ' ' . (preg_replace_callback(self::SHEETNAME_PART_WITH_SLASHES, $callback, $formulaBlock) ?? $formulaBlock) . ' '; |
666
|
|
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_COLRANGE . '/mui', $formulaBlockx, $matches, PREG_SET_ORDER); |
667
|
3 |
|
if ($matchCount > 0) { |
668
|
3 |
|
foreach ($matches as $match) { |
669
|
3 |
|
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}:{$match[4]}"); |
670
|
|
|
$modified3 = substr($this->updateCellReference($match[3] . '$1', $includeAbsoluteReferences, $onlyAbsoluteReferences, true), 0, -2); |
671
|
3 |
|
$modified4 = substr($this->updateCellReference($match[4] . '$1', $includeAbsoluteReferences, $onlyAbsoluteReferences, false), 0, -2); |
672
|
3 |
|
|
673
|
3 |
|
if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) { |
674
|
|
|
if (self::matchSheetName($match[2], $worksheetName)) { |
675
|
|
|
$toString = self::sheetnameBeforeCells($match[2], $worksheetName, "$modified3:$modified4"); |
676
|
|
|
// Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more |
677
|
|
|
$column = Coordinate::columnIndexFromString(trim($match[3], '$')) + 100000; |
678
|
|
|
$row = 10000000; |
679
|
293 |
|
$cellIndex = "{$column}{$row}"; |
680
|
293 |
|
|
681
|
293 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
682
|
48 |
|
$cellTokens[$cellIndex] = '/(?<![A-Z\$\!])' . preg_quote($fromString, '/') . '(?![A-Z])/i'; |
683
|
48 |
|
++$adjustCount; |
684
|
48 |
|
} |
685
|
48 |
|
} |
686
|
|
|
} |
687
|
48 |
|
} |
688
|
41 |
|
// Search for cell ranges (e.g. 'Sheet1'!A3:C5 or A3:C5) with or without $ absolutes (e.g. $A1:C$5) |
689
|
39 |
|
$formulaBlockx = ' ' . (preg_replace_callback(self::SHEETNAME_PART_WITH_SLASHES, $callback, "$formulaBlock") ?? "$formulaBlock") . ' '; |
690
|
39 |
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_CELLRANGE . '/mui', $formulaBlockx, $matches, PREG_SET_ORDER); |
691
|
|
|
if ($matchCount > 0) { |
692
|
39 |
|
foreach ($matches as $match) { |
693
|
39 |
|
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}:{$match[4]}"); |
694
|
39 |
|
$modified3 = $this->updateCellReference($match[3], $includeAbsoluteReferences, $onlyAbsoluteReferences, true); |
695
|
|
|
$modified4 = $this->updateCellReference($match[4], $includeAbsoluteReferences, $onlyAbsoluteReferences, false); |
696
|
39 |
|
|
697
|
39 |
|
if ($match[3] . $match[4] !== $modified3 . $modified4) { |
698
|
39 |
|
if (self::matchSheetName($match[2], $worksheetName)) { |
699
|
|
|
$toString = self::sheetnameBeforeCells($match[2], $worksheetName, "$modified3:$modified4"); |
700
|
|
|
[$column, $row] = Coordinate::coordinateFromString($match[3]); |
701
|
|
|
// Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more |
702
|
|
|
$column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000; |
703
|
|
|
$row = (int) trim($row, '$') + 10000000; |
704
|
|
|
$cellIndex = "{$column}{$row}"; |
705
|
293 |
|
|
706
|
293 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
707
|
|
|
$cellTokens[$cellIndex] = '/(?<![A-Z]\$\!)' . preg_quote($fromString, '/') . '(?!\d)/i'; |
708
|
293 |
|
++$adjustCount; |
709
|
255 |
|
} |
710
|
255 |
|
} |
711
|
|
|
} |
712
|
255 |
|
} |
713
|
255 |
|
// Search for cell references (e.g. 'Sheet1'!A3 or C5) with or without $ absolutes (e.g. $A1 or C$5) |
714
|
253 |
|
|
715
|
253 |
|
$formulaBlockx = ' ' . (preg_replace_callback(self::SHEETNAME_PART_WITH_SLASHES, $callback, $formulaBlock) ?? $formulaBlock) . ' '; |
716
|
253 |
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_CELLREF . '/mui', $formulaBlockx, $matches, PREG_SET_ORDER); |
717
|
253 |
|
|
718
|
253 |
|
if ($matchCount > 0) { |
719
|
|
|
foreach ($matches as $match) { |
720
|
253 |
|
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}"); |
721
|
253 |
|
|
722
|
253 |
|
$modified3 = $this->updateCellReference($match[3], $includeAbsoluteReferences, $onlyAbsoluteReferences, null); |
723
|
|
|
if ($match[3] !== $modified3) { |
724
|
253 |
|
if (self::matchSheetName($match[2], $worksheetName)) { |
725
|
253 |
|
$toString = self::sheetnameBeforeCells($match[2], $worksheetName, "$modified3"); |
726
|
253 |
|
[$column, $row] = Coordinate::coordinateFromString($match[3]); |
727
|
|
|
$columnAdditionalIndex = $column[0] === '$' ? 1 : 0; |
728
|
|
|
$rowAdditionalIndex = $row[0] === '$' ? 1 : 0; |
729
|
|
|
// Max worksheet size is 1,048,576 rows by 16,384 columns in Excel 2007, so our adjustments need to be at least one digit more |
730
|
|
|
$column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000; |
731
|
293 |
|
$row = (int) trim($row, '$') + 10000000; |
732
|
270 |
|
$cellIndex = $row . $rowAdditionalIndex . $column . $columnAdditionalIndex; |
733
|
263 |
|
|
734
|
263 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
735
|
|
|
$cellTokens[$cellIndex] = '/(?<![A-Z\$\!])' . preg_quote($fromString, '/') . '(?!\d)/i'; |
736
|
28 |
|
++$adjustCount; |
737
|
28 |
|
} |
738
|
|
|
} |
739
|
270 |
|
} |
740
|
|
|
} |
741
|
|
|
if ($adjustCount > 0) { |
742
|
|
|
if ($numberOfColumns > 0 || $numberOfRows > 0) { |
743
|
293 |
|
krsort($cellTokens); |
744
|
|
|
krsort($newCellTokens); |
745
|
|
|
} else { |
746
|
293 |
|
ksort($cellTokens); |
747
|
|
|
ksort($newCellTokens); |
748
|
|
|
} // Update cell references in the formula |
749
|
|
|
$formulaBlock = str_replace('\\', '', (string) preg_replace($cellTokens, $newCellTokens, $formulaBlock)); |
750
|
|
|
} |
751
|
|
|
} |
752
|
143 |
|
} |
753
|
|
|
unset($formulaBlock); |
754
|
143 |
|
|
755
|
|
|
// Then rebuild the formula string |
756
|
143 |
|
return implode('"', $formulaBlocks); |
757
|
134 |
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
143 |
|
* Update all cell references within a formula, irrespective of worksheet. |
761
|
112 |
|
*/ |
762
|
|
|
public function updateFormulaReferencesAnyWorksheet(string $formula = '', int $numberOfColumns = 0, int $numberOfRows = 0): string |
763
|
|
|
{ |
764
|
143 |
|
$formula = $this->updateCellReferencesAllWorksheets($formula, $numberOfColumns, $numberOfRows); |
765
|
|
|
|
766
|
|
|
if ($numberOfColumns !== 0) { |
767
|
143 |
|
$formula = $this->updateColumnRangesAllWorksheets($formula, $numberOfColumns); |
768
|
|
|
} |
769
|
143 |
|
|
770
|
143 |
|
if ($numberOfRows !== 0) { |
771
|
143 |
|
$formula = $this->updateRowRangesAllWorksheets($formula, $numberOfRows); |
772
|
143 |
|
} |
773
|
143 |
|
|
774
|
143 |
|
return $formula; |
775
|
|
|
} |
776
|
143 |
|
|
777
|
143 |
|
private function updateCellReferencesAllWorksheets(string $formula, int $numberOfColumns, int $numberOfRows): string |
778
|
143 |
|
{ |
779
|
143 |
|
$splitCount = preg_match_all( |
780
|
|
|
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', |
781
|
143 |
|
$formula, |
782
|
143 |
|
$splitRanges, |
783
|
|
|
PREG_OFFSET_CAPTURE |
784
|
143 |
|
); |
785
|
137 |
|
|
786
|
137 |
|
$columnLengths = array_map('strlen', array_column($splitRanges[6], 0)); |
787
|
137 |
|
$rowLengths = array_map('strlen', array_column($splitRanges[7], 0)); |
788
|
137 |
|
$columnOffsets = array_column($splitRanges[6], 1); |
789
|
137 |
|
$rowOffsets = array_column($splitRanges[7], 1); |
790
|
137 |
|
|
791
|
137 |
|
$columns = $splitRanges[6]; |
792
|
|
|
$rows = $splitRanges[7]; |
793
|
137 |
|
|
794
|
22 |
|
while ($splitCount > 0) { |
795
|
22 |
|
--$splitCount; |
796
|
22 |
|
$columnLength = $columnLengths[$splitCount]; |
797
|
22 |
|
$rowLength = $rowLengths[$splitCount]; |
798
|
|
|
$columnOffset = $columnOffsets[$splitCount]; |
799
|
137 |
|
$rowOffset = $rowOffsets[$splitCount]; |
800
|
42 |
|
$column = $columns[$splitCount][0]; |
801
|
42 |
|
$row = $rows[$splitCount][0]; |
802
|
|
|
|
803
|
|
|
if ($column[0] !== '$') { |
804
|
|
|
$column = ((Coordinate::columnIndexFromString($column) + $numberOfColumns) % AddressRange::MAX_COLUMN_INT) ?: AddressRange::MAX_COLUMN_INT; |
805
|
143 |
|
$column = Coordinate::stringFromColumnIndex($column); |
806
|
|
|
$rowOffset -= ($columnLength - strlen($column)); |
807
|
|
|
$formula = substr($formula, 0, $columnOffset) . $column . substr($formula, $columnOffset + $columnLength); |
808
|
134 |
|
} |
809
|
|
|
if (!empty($row) && $row[0] !== '$') { |
810
|
134 |
|
$row = (((int) $row + $numberOfRows) % AddressRange::MAX_ROW) ?: AddressRange::MAX_ROW; |
811
|
134 |
|
$formula = substr($formula, 0, $rowOffset) . $row . substr($formula, $rowOffset + $rowLength); |
812
|
134 |
|
} |
813
|
134 |
|
} |
814
|
134 |
|
|
815
|
134 |
|
return $formula; |
816
|
|
|
} |
817
|
134 |
|
|
818
|
134 |
|
private function updateColumnRangesAllWorksheets(string $formula, int $numberOfColumns): string |
819
|
134 |
|
{ |
820
|
134 |
|
$splitCount = preg_match_all( |
821
|
|
|
'/' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '/mui', |
822
|
134 |
|
$formula, |
823
|
134 |
|
$splitRanges, |
824
|
|
|
PREG_OFFSET_CAPTURE |
825
|
134 |
|
); |
826
|
3 |
|
|
827
|
3 |
|
$fromColumnLengths = array_map('strlen', array_column($splitRanges[1], 0)); |
828
|
3 |
|
$fromColumnOffsets = array_column($splitRanges[1], 1); |
829
|
3 |
|
$toColumnLengths = array_map('strlen', array_column($splitRanges[2], 0)); |
830
|
3 |
|
$toColumnOffsets = array_column($splitRanges[2], 1); |
831
|
3 |
|
|
832
|
3 |
|
$fromColumns = $splitRanges[1]; |
833
|
|
|
$toColumns = $splitRanges[2]; |
834
|
3 |
|
|
835
|
2 |
|
while ($splitCount > 0) { |
836
|
2 |
|
--$splitCount; |
837
|
|
|
$fromColumnLength = $fromColumnLengths[$splitCount]; |
838
|
3 |
|
$toColumnLength = $toColumnLengths[$splitCount]; |
839
|
2 |
|
$fromColumnOffset = $fromColumnOffsets[$splitCount]; |
840
|
2 |
|
$toColumnOffset = $toColumnOffsets[$splitCount]; |
841
|
|
|
$fromColumn = $fromColumns[$splitCount][0]; |
842
|
|
|
$toColumn = $toColumns[$splitCount][0]; |
843
|
|
|
|
844
|
134 |
|
if (!empty($fromColumn) && $fromColumn[0] !== '$') { |
845
|
|
|
$fromColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($fromColumn) + $numberOfColumns); |
846
|
|
|
$formula = substr($formula, 0, $fromColumnOffset) . $fromColumn . substr($formula, $fromColumnOffset + $fromColumnLength); |
847
|
112 |
|
} |
848
|
|
|
if (!empty($toColumn) && $toColumn[0] !== '$') { |
849
|
112 |
|
$toColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($toColumn) + $numberOfColumns); |
850
|
112 |
|
$formula = substr($formula, 0, $toColumnOffset) . $toColumn . substr($formula, $toColumnOffset + $toColumnLength); |
851
|
112 |
|
} |
852
|
112 |
|
} |
853
|
112 |
|
|
854
|
112 |
|
return $formula; |
855
|
|
|
} |
856
|
112 |
|
|
857
|
112 |
|
private function updateRowRangesAllWorksheets(string $formula, int $numberOfRows): string |
858
|
112 |
|
{ |
859
|
112 |
|
$splitCount = preg_match_all( |
860
|
|
|
'/' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '/mui', |
861
|
112 |
|
$formula, |
862
|
112 |
|
$splitRanges, |
863
|
|
|
PREG_OFFSET_CAPTURE |
864
|
112 |
|
); |
865
|
3 |
|
|
866
|
3 |
|
$fromRowLengths = array_map('strlen', array_column($splitRanges[1], 0)); |
867
|
3 |
|
$fromRowOffsets = array_column($splitRanges[1], 1); |
868
|
3 |
|
$toRowLengths = array_map('strlen', array_column($splitRanges[2], 0)); |
869
|
3 |
|
$toRowOffsets = array_column($splitRanges[2], 1); |
870
|
3 |
|
|
871
|
3 |
|
$fromRows = $splitRanges[1]; |
872
|
|
|
$toRows = $splitRanges[2]; |
873
|
3 |
|
|
874
|
2 |
|
while ($splitCount > 0) { |
875
|
2 |
|
--$splitCount; |
876
|
|
|
$fromRowLength = $fromRowLengths[$splitCount]; |
877
|
3 |
|
$toRowLength = $toRowLengths[$splitCount]; |
878
|
2 |
|
$fromRowOffset = $fromRowOffsets[$splitCount]; |
879
|
2 |
|
$toRowOffset = $toRowOffsets[$splitCount]; |
880
|
|
|
$fromRow = $fromRows[$splitCount][0]; |
881
|
|
|
$toRow = $toRows[$splitCount][0]; |
882
|
|
|
|
883
|
112 |
|
if (!empty($fromRow) && $fromRow[0] !== '$') { |
884
|
|
|
$fromRow = (int) $fromRow + $numberOfRows; |
885
|
|
|
$formula = substr($formula, 0, $fromRowOffset) . $fromRow . substr($formula, $fromRowOffset + $fromRowLength); |
886
|
|
|
} |
887
|
|
|
if (!empty($toRow) && $toRow[0] !== '$') { |
888
|
|
|
$toRow = (int) $toRow + $numberOfRows; |
889
|
|
|
$formula = substr($formula, 0, $toRowOffset) . $toRow . substr($formula, $toRowOffset + $toRowLength); |
890
|
|
|
} |
891
|
|
|
} |
892
|
|
|
|
893
|
305 |
|
return $formula; |
894
|
|
|
} |
895
|
|
|
|
896
|
305 |
|
/** |
897
|
1 |
|
* Update cell reference. |
898
|
|
|
* |
899
|
|
|
* @param string $cellReference Cell address or range of addresses |
900
|
305 |
|
* |
901
|
|
|
* @return string Updated cell range |
902
|
|
|
*/ |
903
|
293 |
|
private function updateCellReference(string $cellReference = 'A1', bool $includeAbsoluteReferences = false, bool $onlyAbsoluteReferences = false, ?bool $topLeft = null) |
904
|
|
|
{ |
905
|
293 |
|
// Is it in another worksheet? Will not have to update anything. |
906
|
|
|
if (str_contains($cellReference, '!')) { |
907
|
|
|
return $cellReference; |
908
|
|
|
} |
909
|
38 |
|
// Is it a range or a single cell? |
910
|
|
|
if (!Coordinate::coordinateIsRange($cellReference)) { |
911
|
|
|
// Single cell |
912
|
|
|
/** @var CellReferenceHelper */ |
913
|
|
|
$cellReferenceHelper = $this->cellReferenceHelper; |
914
|
|
|
|
915
|
|
|
return $cellReferenceHelper->updateCellReference($cellReference, $includeAbsoluteReferences, $onlyAbsoluteReferences, $topLeft); |
916
|
|
|
} |
917
|
|
|
|
918
|
|
|
// Range |
919
|
799 |
|
return $this->updateCellRange($cellReference, $includeAbsoluteReferences, $onlyAbsoluteReferences); |
920
|
|
|
} |
921
|
799 |
|
|
922
|
1 |
|
/** |
923
|
|
|
* Update named formulae (i.e. containing worksheet references / named ranges). |
924
|
|
|
* |
925
|
799 |
|
* @param Spreadsheet $spreadsheet Object to update |
926
|
799 |
|
* @param string $oldName Old name (name to replace) |
927
|
135 |
|
* @param string $newName New name |
928
|
135 |
|
*/ |
929
|
68 |
|
public function updateNamedFormulae(Spreadsheet $spreadsheet, string $oldName = '', string $newName = ''): void |
930
|
68 |
|
{ |
931
|
1 |
|
if ($oldName == '') { |
932
|
1 |
|
return; |
933
|
1 |
|
} |
934
|
|
|
|
935
|
|
|
foreach ($spreadsheet->getWorksheetIterator() as $sheet) { |
936
|
|
|
foreach ($sheet->getCoordinates(false) as $coordinate) { |
937
|
|
|
$cell = $sheet->getCell($coordinate); |
938
|
|
|
if ($cell->getDataType() === DataType::TYPE_FORMULA) { |
939
|
|
|
$formula = $cell->getValueString(); |
940
|
7 |
|
if (str_contains($formula, $oldName)) { |
941
|
|
|
$formula = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $formula); |
942
|
7 |
|
$formula = str_replace($oldName . '!', $newName . '!', $formula); |
943
|
7 |
|
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA); |
944
|
7 |
|
} |
945
|
|
|
} |
946
|
4 |
|
} |
947
|
|
|
} |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
private function updateDefinedNames(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void |
951
|
7 |
|
{ |
952
|
|
|
foreach ($worksheet->getParentOrThrow()->getDefinedNames() as $definedName) { |
953
|
7 |
|
if ($definedName->isFormula() === false) { |
954
|
7 |
|
$this->updateNamedRange($definedName, $worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows); |
955
|
7 |
|
} else { |
956
|
|
|
$this->updateNamedFormula($definedName, $worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows); |
957
|
|
|
} |
958
|
|
|
} |
959
|
|
|
} |
960
|
|
|
|
961
|
|
|
private function updateNamedRange(DefinedName $definedName, Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void |
962
|
|
|
{ |
963
|
7 |
|
$cellAddress = $definedName->getValue(); |
964
|
5 |
|
$asFormula = ($cellAddress[0] === '='); |
965
|
5 |
|
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) { |
966
|
|
|
/** |
967
|
2 |
|
* If we delete the entire range that is referenced by a Named Range, MS Excel sets the value to #REF! |
968
|
|
|
* PhpSpreadsheet still only does a basic adjustment, so the Named Range will still reference Cells. |
969
|
|
|
* Note that this applies only when deleting columns/rows; subsequent insertion won't fix the #REF! |
970
|
|
|
* TODO Can we work out a method to identify Named Ranges that cease to be valid, so that we can replace |
971
|
|
|
* them with a #REF! |
972
|
4 |
|
*/ |
973
|
|
|
if ($asFormula === true) { |
974
|
4 |
|
$formula = $this->updateFormulaReferences($cellAddress, $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle(), true, true); |
975
|
|
|
$definedName->setValue($formula); |
976
|
|
|
} else { |
977
|
|
|
$definedName->setValue($this->updateCellReference(ltrim($cellAddress, '='), true)); |
978
|
|
|
} |
979
|
|
|
} |
980
|
|
|
} |
981
|
|
|
|
982
|
3 |
|
private function updateNamedFormula(DefinedName $definedName, Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void |
983
|
3 |
|
{ |
984
|
3 |
|
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) { |
985
|
|
|
/** |
986
|
|
|
* If we delete the entire range that is referenced by a Named Formula, MS Excel sets the value to #REF! |
987
|
|
|
* PhpSpreadsheet still only does a basic adjustment, so the Named Formula will still reference Cells. |
988
|
|
|
* Note that this applies only when deleting columns/rows; subsequent insertion won't fix the #REF! |
989
|
|
|
* TODO Can we work out a method to identify Named Ranges that cease to be valid, so that we can replace |
990
|
|
|
* them with a #REF! |
991
|
|
|
*/ |
992
|
|
|
$formula = $definedName->getValue(); |
993
|
|
|
$formula = $this->updateFormulaReferences($formula, $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle(), true); |
994
|
|
|
$definedName->setValue($formula); |
995
|
38 |
|
} |
996
|
|
|
} |
997
|
38 |
|
|
998
|
|
|
/** |
999
|
|
|
* Update cell range. |
1000
|
|
|
* |
1001
|
|
|
* @param string $cellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3') |
1002
|
38 |
|
* |
1003
|
38 |
|
* @return string Updated cell range |
1004
|
38 |
|
*/ |
1005
|
38 |
|
private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsoluteReferences = false, bool $onlyAbsoluteReferences = false): string |
1006
|
38 |
|
{ |
1007
|
|
|
if (!Coordinate::coordinateIsRange($cellRange)) { |
1008
|
38 |
|
throw new Exception('Only cell ranges may be passed to this method.'); |
1009
|
38 |
|
} |
1010
|
|
|
|
1011
|
|
|
// Update range |
1012
|
|
|
$range = Coordinate::splitRange($cellRange); |
1013
|
38 |
|
$ic = count($range); |
1014
|
|
|
for ($i = 0; $i < $ic; ++$i) { |
1015
|
|
|
$jc = count($range[$i]); |
1016
|
|
|
for ($j = 0; $j < $jc; ++$j) { |
1017
|
|
|
/** @var CellReferenceHelper */ |
1018
|
38 |
|
$cellReferenceHelper = $this->cellReferenceHelper; |
1019
|
|
|
if (ctype_alpha($range[$i][$j])) { |
1020
|
|
|
$range[$i][$j] = Coordinate::coordinateFromString( |
1021
|
|
|
$cellReferenceHelper->updateCellReference($range[$i][$j] . '1', $includeAbsoluteReferences, $onlyAbsoluteReferences, null) |
1022
|
|
|
)[0]; |
1023
|
|
|
} elseif (ctype_digit($range[$i][$j])) { |
1024
|
38 |
|
$range[$i][$j] = Coordinate::coordinateFromString( |
1025
|
|
|
$cellReferenceHelper->updateCellReference('A' . $range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences, null) |
1026
|
|
|
)[1]; |
1027
|
23 |
|
} else { |
1028
|
|
|
$range[$i][$j] = $cellReferenceHelper->updateCellReference($range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences, null); |
1029
|
23 |
|
} |
1030
|
23 |
|
} |
1031
|
|
|
} |
1032
|
23 |
|
|
1033
|
22 |
|
// Recreate range string |
1034
|
22 |
|
return Coordinate::buildRange($range); |
1035
|
22 |
|
} |
1036
|
|
|
|
1037
|
|
|
private function clearColumnStrips(int $highestRow, int $beforeColumn, int $numberOfColumns, Worksheet $worksheet): void |
1038
|
|
|
{ |
1039
|
|
|
$startColumnId = Coordinate::stringFromColumnIndex($beforeColumn + $numberOfColumns); |
1040
|
35 |
|
$endColumnId = Coordinate::stringFromColumnIndex($beforeColumn); |
1041
|
|
|
|
1042
|
35 |
|
for ($row = 1; $row <= $highestRow - 1; ++$row) { |
1043
|
35 |
|
for ($column = $startColumnId; $column !== $endColumnId; ++$column) { |
1044
|
|
|
$coordinate = $column . $row; |
1045
|
35 |
|
$this->clearStripCell($worksheet, $coordinate); |
1046
|
35 |
|
} |
1047
|
35 |
|
} |
1048
|
35 |
|
} |
1049
|
|
|
|
1050
|
|
|
private function clearRowStrips(string $highestColumn, int $beforeColumn, int $beforeRow, int $numberOfRows, Worksheet $worksheet): void |
1051
|
|
|
{ |
1052
|
|
|
$startColumnId = Coordinate::stringFromColumnIndex($beforeColumn); |
1053
|
40 |
|
++$highestColumn; |
1054
|
|
|
|
1055
|
40 |
|
for ($column = $startColumnId; $column !== $highestColumn; ++$column) { |
1056
|
40 |
|
for ($row = $beforeRow + $numberOfRows; $row <= $beforeRow - 1; ++$row) { |
1057
|
40 |
|
$coordinate = $column . $row; |
1058
|
40 |
|
$this->clearStripCell($worksheet, $coordinate); |
1059
|
|
|
} |
1060
|
40 |
|
} |
1061
|
19 |
|
} |
1062
|
19 |
|
|
1063
|
|
|
private function clearStripCell(Worksheet $worksheet, string $coordinate): void |
1064
|
|
|
{ |
1065
|
|
|
$worksheet->removeConditionalStyles($coordinate); |
1066
|
94 |
|
$worksheet->setHyperlink($coordinate); |
1067
|
|
|
$worksheet->setDataValidation($coordinate); |
1068
|
94 |
|
$worksheet->removeComment($coordinate); |
1069
|
94 |
|
|
1070
|
94 |
|
if ($worksheet->cellExists($coordinate)) { |
1071
|
4 |
|
$worksheet->getCell($coordinate)->setValueExplicit(null, DataType::TYPE_NULL); |
1072
|
2 |
|
$worksheet->getCell($coordinate)->setXfIndex(0); |
1073
|
2 |
|
} |
1074
|
2 |
|
} |
1075
|
2 |
|
|
1076
|
2 |
|
private function adjustAutoFilter(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns): void |
1077
|
2 |
|
{ |
1078
|
2 |
|
$autoFilter = $worksheet->getAutoFilter(); |
1079
|
2 |
|
$autoFilterRange = $autoFilter->getRange(); |
1080
|
2 |
|
if (!empty($autoFilterRange)) { |
1081
|
1 |
|
if ($numberOfColumns !== 0) { |
1082
|
|
|
$autoFilterColumns = $autoFilter->getColumns(); |
1083
|
2 |
|
if (count($autoFilterColumns) > 0) { |
1084
|
|
|
$column = ''; |
1085
|
|
|
$row = 0; |
1086
|
2 |
|
sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row); |
1087
|
1 |
|
$columnIndex = Coordinate::columnIndexFromString((string) $column); |
1088
|
|
|
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange); |
1089
|
1 |
|
if ($columnIndex <= $rangeEnd[0]) { |
1090
|
|
|
if ($numberOfColumns < 0) { |
1091
|
|
|
$this->adjustAutoFilterDeleteRules($columnIndex, $numberOfColumns, $autoFilterColumns, $autoFilter); |
1092
|
|
|
} |
1093
|
|
|
$startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0]; |
1094
|
|
|
|
1095
|
4 |
|
// Shuffle columns in autofilter range |
1096
|
4 |
|
if ($numberOfColumns > 0) { |
1097
|
4 |
|
$this->adjustAutoFilterInsert($startCol, $numberOfColumns, $rangeEnd[0], $autoFilter); |
1098
|
|
|
} else { |
1099
|
|
|
$this->adjustAutoFilterDelete($startCol, $numberOfColumns, $rangeEnd[0], $autoFilter); |
1100
|
|
|
} |
1101
|
1 |
|
} |
1102
|
|
|
} |
1103
|
|
|
} |
1104
|
|
|
|
1105
|
1 |
|
$worksheet->setAutoFilter( |
1106
|
1 |
|
$this->updateCellReference($autoFilterRange) |
1107
|
|
|
); |
1108
|
1 |
|
} |
1109
|
1 |
|
} |
1110
|
1 |
|
|
1111
|
1 |
|
private function adjustAutoFilterDeleteRules(int $columnIndex, int $numberOfColumns, array $autoFilterColumns, AutoFilter $autoFilter): void |
1112
|
|
|
{ |
1113
|
1 |
|
// If we're actually deleting any columns that fall within the autofilter range, |
1114
|
|
|
// then we delete any rules for those columns |
1115
|
|
|
$deleteColumn = $columnIndex + $numberOfColumns - 1; |
1116
|
|
|
$deleteCount = abs($numberOfColumns); |
1117
|
1 |
|
|
1118
|
|
|
for ($i = 1; $i <= $deleteCount; ++$i) { |
1119
|
1 |
|
$columnName = Coordinate::stringFromColumnIndex($deleteColumn + 1); |
1120
|
1 |
|
if (isset($autoFilterColumns[$columnName])) { |
1121
|
1 |
|
$autoFilter->clearColumn($columnName); |
1122
|
|
|
} |
1123
|
|
|
++$deleteColumn; |
1124
|
1 |
|
} |
1125
|
1 |
|
} |
1126
|
1 |
|
|
1127
|
1 |
|
private function adjustAutoFilterInsert(int $startCol, int $numberOfColumns, int $rangeEnd, AutoFilter $autoFilter): void |
1128
|
|
|
{ |
1129
|
|
|
$startColRef = $startCol; |
1130
|
1 |
|
$endColRef = $rangeEnd; |
1131
|
|
|
$toColRef = $rangeEnd + $numberOfColumns; |
1132
|
|
|
|
1133
|
1 |
|
do { |
1134
|
1 |
|
$autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef)); |
1135
|
1 |
|
--$endColRef; |
1136
|
|
|
--$toColRef; |
1137
|
|
|
} while ($startColRef <= $endColRef); |
1138
|
1 |
|
} |
1139
|
1 |
|
|
1140
|
1 |
|
private function adjustAutoFilterDelete(int $startCol, int $numberOfColumns, int $rangeEnd, AutoFilter $autoFilter): void |
1141
|
1 |
|
{ |
1142
|
|
|
// For delete, we shuffle from beginning to end to avoid overwriting |
1143
|
|
|
$startColID = Coordinate::stringFromColumnIndex($startCol); |
1144
|
94 |
|
$toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns); |
1145
|
|
|
$endColID = Coordinate::stringFromColumnIndex($rangeEnd + 1); |
1146
|
94 |
|
|
1147
|
|
|
do { |
1148
|
94 |
|
$autoFilter->shiftColumn($startColID, $toColID); |
1149
|
4 |
|
++$startColID; |
1150
|
4 |
|
++$toColID; |
1151
|
4 |
|
} while ($startColID !== $endColID); |
1152
|
2 |
|
} |
1153
|
2 |
|
|
1154
|
2 |
|
private function adjustTable(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns): void |
1155
|
2 |
|
{ |
1156
|
2 |
|
$tableCollection = $worksheet->getTableCollection(); |
1157
|
2 |
|
|
1158
|
2 |
|
foreach ($tableCollection as $table) { |
1159
|
2 |
|
$tableRange = $table->getRange(); |
1160
|
2 |
|
if (!empty($tableRange)) { |
1161
|
1 |
|
if ($numberOfColumns !== 0) { |
1162
|
|
|
$tableColumns = $table->getColumns(); |
1163
|
2 |
|
if (count($tableColumns) > 0) { |
1164
|
|
|
$column = ''; |
1165
|
|
|
$row = 0; |
1166
|
2 |
|
sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row); |
1167
|
1 |
|
$columnIndex = Coordinate::columnIndexFromString((string) $column); |
1168
|
|
|
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($tableRange); |
1169
|
1 |
|
if ($columnIndex <= $rangeEnd[0]) { |
1170
|
|
|
if ($numberOfColumns < 0) { |
1171
|
|
|
$this->adjustTableDeleteRules($columnIndex, $numberOfColumns, $tableColumns, $table); |
1172
|
|
|
} |
1173
|
|
|
$startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0]; |
1174
|
|
|
|
1175
|
4 |
|
// Shuffle columns in table range |
1176
|
|
|
if ($numberOfColumns > 0) { |
1177
|
|
|
$this->adjustTableInsert($startCol, $numberOfColumns, $rangeEnd[0], $table); |
1178
|
|
|
} else { |
1179
|
|
|
$this->adjustTableDelete($startCol, $numberOfColumns, $rangeEnd[0], $table); |
1180
|
1 |
|
} |
1181
|
|
|
} |
1182
|
|
|
} |
1183
|
|
|
} |
1184
|
1 |
|
|
1185
|
1 |
|
$table->setRange($this->updateCellReference($tableRange)); |
1186
|
|
|
} |
1187
|
1 |
|
} |
1188
|
1 |
|
} |
1189
|
1 |
|
|
1190
|
1 |
|
private function adjustTableDeleteRules(int $columnIndex, int $numberOfColumns, array $tableColumns, Table $table): void |
1191
|
|
|
{ |
1192
|
1 |
|
// If we're actually deleting any columns that fall within the table range, |
1193
|
|
|
// then we delete any rules for those columns |
1194
|
|
|
$deleteColumn = $columnIndex + $numberOfColumns - 1; |
1195
|
|
|
$deleteCount = abs($numberOfColumns); |
1196
|
1 |
|
|
1197
|
|
|
for ($i = 1; $i <= $deleteCount; ++$i) { |
1198
|
1 |
|
$columnName = Coordinate::stringFromColumnIndex($deleteColumn + 1); |
1199
|
1 |
|
if (isset($tableColumns[$columnName])) { |
1200
|
1 |
|
$table->clearColumn($columnName); |
1201
|
|
|
} |
1202
|
|
|
++$deleteColumn; |
1203
|
1 |
|
} |
1204
|
1 |
|
} |
1205
|
1 |
|
|
1206
|
1 |
|
private function adjustTableInsert(int $startCol, int $numberOfColumns, int $rangeEnd, Table $table): void |
1207
|
|
|
{ |
1208
|
|
|
$startColRef = $startCol; |
1209
|
1 |
|
$endColRef = $rangeEnd; |
1210
|
|
|
$toColRef = $rangeEnd + $numberOfColumns; |
1211
|
|
|
|
1212
|
1 |
|
do { |
1213
|
1 |
|
$table->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef)); |
1214
|
1 |
|
--$endColRef; |
1215
|
|
|
--$toColRef; |
1216
|
|
|
} while ($startColRef <= $endColRef); |
1217
|
1 |
|
} |
1218
|
1 |
|
|
1219
|
1 |
|
private function adjustTableDelete(int $startCol, int $numberOfColumns, int $rangeEnd, Table $table): void |
1220
|
1 |
|
{ |
1221
|
|
|
// For delete, we shuffle from beginning to end to avoid overwriting |
1222
|
|
|
$startColID = Coordinate::stringFromColumnIndex($startCol); |
1223
|
23 |
|
$toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns); |
1224
|
|
|
$endColID = Coordinate::stringFromColumnIndex($rangeEnd + 1); |
1225
|
23 |
|
|
1226
|
23 |
|
do { |
1227
|
|
|
$table->shiftColumn($startColID, $toColID); |
1228
|
22 |
|
++$startColID; |
1229
|
22 |
|
++$toColID; |
1230
|
18 |
|
} while ($startColID !== $endColID); |
1231
|
18 |
|
} |
1232
|
18 |
|
|
1233
|
18 |
|
private function duplicateStylesByColumn(Worksheet $worksheet, int $beforeColumn, int $beforeRow, int $highestRow, int $numberOfColumns): void |
1234
|
|
|
{ |
1235
|
|
|
$beforeColumnName = Coordinate::stringFromColumnIndex($beforeColumn - 1); |
1236
|
|
|
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) { |
1237
|
|
|
// Style |
1238
|
|
|
$coordinate = $beforeColumnName . $i; |
1239
|
|
|
if ($worksheet->cellExists($coordinate)) { |
1240
|
34 |
|
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex(); |
1241
|
|
|
for ($j = $beforeColumn; $j <= $beforeColumn - 1 + $numberOfColumns; ++$j) { |
1242
|
34 |
|
if (!empty($xfIndex) || $worksheet->cellExists([$j, $i])) { |
1243
|
34 |
|
$worksheet->getCell([$j, $i])->setXfIndex($xfIndex); |
1244
|
|
|
} |
1245
|
34 |
|
} |
1246
|
34 |
|
} |
1247
|
32 |
|
} |
1248
|
32 |
|
} |
1249
|
32 |
|
|
1250
|
23 |
|
private function duplicateStylesByRow(Worksheet $worksheet, int $beforeColumn, int $beforeRow, string $highestColumn, int $numberOfRows): void |
1251
|
|
|
{ |
1252
|
|
|
$highestColumnIndex = Coordinate::columnIndexFromString($highestColumn); |
1253
|
|
|
for ($i = $beforeColumn; $i <= $highestColumnIndex; ++$i) { |
1254
|
|
|
// Style |
1255
|
|
|
$coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1); |
1256
|
|
|
if ($worksheet->cellExists($coordinate)) { |
1257
|
|
|
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex(); |
1258
|
|
|
for ($j = $beforeRow; $j <= $beforeRow - 1 + $numberOfRows; ++$j) { |
1259
|
|
|
if (!empty($xfIndex) || $worksheet->cellExists([$i, $j])) { |
1260
|
1 |
|
$worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex); |
1261
|
|
|
} |
1262
|
1 |
|
} |
1263
|
|
|
} |
1264
|
|
|
} |
1265
|
|
|
} |
1266
|
|
|
|
1267
|
|
|
/** |
1268
|
|
|
* __clone implementation. Cloning should not be allowed in a Singleton! |
1269
|
|
|
*/ |
1270
|
|
|
final public function __clone() |
1271
|
|
|
{ |
1272
|
|
|
throw new Exception('Cloning a Singleton is not allowed!'); |
1273
|
|
|
} |
1274
|
|
|
} |
1275
|
|
|
|