1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
9
|
|
|
|
10
|
|
|
class ReferenceHelper |
11
|
|
|
{ |
12
|
|
|
/** Constants */ |
13
|
|
|
/** Regular Expressions */ |
14
|
|
|
const REFHELPER_REGEXP_CELLREF = '((\w*|\'[^!]*\')!)?(?<![:a-z\$])(\$?[a-z]{1,3}\$?\d+)(?=[^:!\d\'])'; |
15
|
|
|
const REFHELPER_REGEXP_CELLRANGE = '((\w*|\'[^!]*\')!)?(\$?[a-z]{1,3}\$?\d+):(\$?[a-z]{1,3}\$?\d+)'; |
16
|
|
|
const REFHELPER_REGEXP_ROWRANGE = '((\w*|\'[^!]*\')!)?(\$?\d+):(\$?\d+)'; |
17
|
|
|
const REFHELPER_REGEXP_COLRANGE = '((\w*|\'[^!]*\')!)?(\$?[a-z]{1,3}):(\$?[a-z]{1,3})'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Instance of this class. |
21
|
|
|
* |
22
|
|
|
* @var ReferenceHelper |
23
|
|
|
*/ |
24
|
|
|
private static $instance; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get an instance of this class. |
28
|
|
|
* |
29
|
|
|
* @return ReferenceHelper |
30
|
|
|
*/ |
31
|
3907 |
|
public static function getInstance() |
32
|
|
|
{ |
33
|
3907 |
|
if (!isset(self::$instance) || (self::$instance === null)) { |
34
|
124 |
|
self::$instance = new self(); |
35
|
|
|
} |
36
|
|
|
|
37
|
3907 |
|
return self::$instance; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new ReferenceHelper. |
42
|
|
|
*/ |
43
|
124 |
|
protected function __construct() |
44
|
|
|
{ |
45
|
124 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Compare two column addresses |
49
|
|
|
* Intended for use as a Callback function for sorting column addresses by column. |
50
|
|
|
* |
51
|
|
|
* @param string $a First column to test (e.g. 'AA') |
52
|
|
|
* @param string $b Second column to test (e.g. 'Z') |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
1 |
|
public static function columnSort($a, $b) |
57
|
|
|
{ |
58
|
1 |
|
return strcasecmp(strlen($a) . $a, strlen($b) . $b); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Compare two column addresses |
63
|
|
|
* Intended for use as a Callback function for reverse sorting column addresses by column. |
64
|
|
|
* |
65
|
|
|
* @param string $a First column to test (e.g. 'AA') |
66
|
|
|
* @param string $b Second column to test (e.g. 'Z') |
67
|
|
|
* |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
1 |
|
public static function columnReverseSort($a, $b) |
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
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
18 |
|
public static function cellSort($a, $b) |
85
|
|
|
{ |
86
|
18 |
|
[$ac, $ar] = sscanf($a, '%[A-Z]%d'); |
87
|
18 |
|
[$bc, $br] = sscanf($b, '%[A-Z]%d'); |
88
|
|
|
|
89
|
18 |
|
if ($ar === $br) { |
90
|
1 |
|
return strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc); |
91
|
|
|
} |
92
|
|
|
|
93
|
18 |
|
return ($ar < $br) ? -1 : 1; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Compare two cell addresses |
98
|
|
|
* Intended for use as a Callback function for sorting cell addresses by column and row. |
99
|
|
|
* |
100
|
|
|
* @param string $a First cell to test (e.g. 'AA1') |
101
|
|
|
* @param string $b Second cell to test (e.g. 'Z1') |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
18 |
|
public static function cellReverseSort($a, $b) |
106
|
|
|
{ |
107
|
18 |
|
[$ac, $ar] = sscanf($a, '%[A-Z]%d'); |
108
|
18 |
|
[$bc, $br] = sscanf($b, '%[A-Z]%d'); |
109
|
|
|
|
110
|
18 |
|
if ($ar === $br) { |
111
|
1 |
|
return -strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc); |
112
|
|
|
} |
113
|
|
|
|
114
|
18 |
|
return ($ar < $br) ? 1 : -1; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Test whether a cell address falls within a defined range of cells. |
119
|
|
|
* |
120
|
|
|
* @param string $cellAddress Address of the cell we're testing |
121
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
122
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
123
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
124
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
18 |
|
private static function cellAddressInDeleteRange($cellAddress, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols) |
129
|
|
|
{ |
130
|
18 |
|
[$cellColumn, $cellRow] = Coordinate::coordinateFromString($cellAddress); |
131
|
18 |
|
$cellColumnIndex = Coordinate::columnIndexFromString($cellColumn); |
132
|
|
|
// Is cell within the range of rows/columns if we're deleting |
133
|
|
|
if ( |
134
|
18 |
|
$pNumRows < 0 && |
135
|
18 |
|
($cellRow >= ($beforeRow + $pNumRows)) && |
136
|
18 |
|
($cellRow < $beforeRow) |
137
|
|
|
) { |
138
|
|
|
return true; |
139
|
|
|
} elseif ( |
140
|
18 |
|
$pNumCols < 0 && |
141
|
18 |
|
($cellColumnIndex >= ($beforeColumnIndex + $pNumCols)) && |
142
|
18 |
|
($cellColumnIndex < $beforeColumnIndex) |
143
|
|
|
) { |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
18 |
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Update page breaks when inserting/deleting rows/columns. |
152
|
|
|
* |
153
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
154
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
155
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
156
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
157
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
158
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
159
|
|
|
*/ |
160
|
32 |
|
protected function adjustPageBreaks(Worksheet $pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
161
|
|
|
{ |
162
|
32 |
|
$aBreaks = $pSheet->getBreaks(); |
163
|
32 |
|
($pNumCols > 0 || $pNumRows > 0) ? |
164
|
32 |
|
uksort($aBreaks, ['self', 'cellReverseSort']) : uksort($aBreaks, ['self', 'cellSort']); |
165
|
|
|
|
166
|
32 |
|
foreach ($aBreaks as $key => $value) { |
167
|
|
|
if (self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) { |
168
|
|
|
// If we're deleting, then clear any defined breaks that are within the range |
169
|
|
|
// of rows/columns that we're deleting |
170
|
|
|
$pSheet->setBreak($key, Worksheet::BREAK_NONE); |
171
|
|
|
} else { |
172
|
|
|
// Otherwise update any affected breaks by inserting a new break at the appropriate point |
173
|
|
|
// and removing the old affected break |
174
|
|
|
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); |
175
|
|
|
if ($key != $newReference) { |
176
|
|
|
$pSheet->setBreak($newReference, $value) |
177
|
|
|
->setBreak($key, Worksheet::BREAK_NONE); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
32 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Update cell comments when inserting/deleting rows/columns. |
185
|
|
|
* |
186
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
187
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
188
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
189
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
190
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
191
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
192
|
|
|
*/ |
193
|
32 |
|
protected function adjustComments($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
194
|
|
|
{ |
195
|
32 |
|
$aComments = $pSheet->getComments(); |
196
|
32 |
|
$aNewComments = []; // the new array of all comments |
197
|
|
|
|
198
|
32 |
|
foreach ($aComments as $key => &$value) { |
199
|
|
|
// Any comments inside a deleted range will be ignored |
200
|
18 |
|
if (!self::cellAddressInDeleteRange($key, $beforeRow, $pNumRows, $beforeColumnIndex, $pNumCols)) { |
201
|
|
|
// Otherwise build a new array of comments indexed by the adjusted cell reference |
202
|
18 |
|
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); |
203
|
18 |
|
$aNewComments[$newReference] = $value; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
// Replace the comments array with the new set of comments |
207
|
32 |
|
$pSheet->setComments($aNewComments); |
208
|
32 |
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Update hyperlinks when inserting/deleting rows/columns. |
212
|
|
|
* |
213
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
214
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
215
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
216
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
217
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
218
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
219
|
|
|
*/ |
220
|
32 |
|
protected function adjustHyperlinks($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
|
|
|
|
221
|
|
|
{ |
222
|
32 |
|
$aHyperlinkCollection = $pSheet->getHyperlinkCollection(); |
223
|
32 |
|
($pNumCols > 0 || $pNumRows > 0) ? |
224
|
32 |
|
uksort($aHyperlinkCollection, ['self', 'cellReverseSort']) : uksort($aHyperlinkCollection, ['self', 'cellSort']); |
225
|
|
|
|
226
|
32 |
|
foreach ($aHyperlinkCollection as $key => $value) { |
227
|
17 |
|
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); |
228
|
17 |
|
if ($key != $newReference) { |
229
|
17 |
|
$pSheet->setHyperlink($newReference, $value); |
230
|
17 |
|
$pSheet->setHyperlink($key, null); |
231
|
|
|
} |
232
|
|
|
} |
233
|
32 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Update data validations when inserting/deleting rows/columns. |
237
|
|
|
* |
238
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
239
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
240
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
241
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
242
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
243
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
244
|
|
|
*/ |
245
|
32 |
|
protected function adjustDataValidations($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
|
|
|
|
246
|
|
|
{ |
247
|
32 |
|
$aDataValidationCollection = $pSheet->getDataValidationCollection(); |
248
|
32 |
|
($pNumCols > 0 || $pNumRows > 0) ? |
249
|
32 |
|
uksort($aDataValidationCollection, ['self', 'cellReverseSort']) : uksort($aDataValidationCollection, ['self', 'cellSort']); |
250
|
|
|
|
251
|
32 |
|
foreach ($aDataValidationCollection as $key => $value) { |
252
|
|
|
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); |
253
|
|
|
if ($key != $newReference) { |
254
|
|
|
$pSheet->setDataValidation($newReference, $value); |
255
|
|
|
$pSheet->setDataValidation($key, null); |
256
|
|
|
} |
257
|
|
|
} |
258
|
32 |
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Update merged cells when inserting/deleting rows/columns. |
262
|
|
|
* |
263
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
264
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
265
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
266
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
267
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
268
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
269
|
|
|
*/ |
270
|
32 |
|
protected function adjustMergeCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
|
|
|
|
271
|
|
|
{ |
272
|
32 |
|
$aMergeCells = $pSheet->getMergeCells(); |
273
|
32 |
|
$aNewMergeCells = []; // the new array of all merge cells |
274
|
32 |
|
foreach ($aMergeCells as $key => &$value) { |
275
|
18 |
|
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); |
276
|
18 |
|
$aNewMergeCells[$newReference] = $newReference; |
277
|
|
|
} |
278
|
32 |
|
$pSheet->setMergeCells($aNewMergeCells); // replace the merge cells array |
279
|
32 |
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Update protected cells when inserting/deleting rows/columns. |
283
|
|
|
* |
284
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
285
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
286
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
287
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
288
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
289
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
290
|
|
|
*/ |
291
|
32 |
|
protected function adjustProtectedCells($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
|
|
|
|
292
|
|
|
{ |
293
|
32 |
|
$aProtectedCells = $pSheet->getProtectedCells(); |
294
|
32 |
|
($pNumCols > 0 || $pNumRows > 0) ? |
295
|
32 |
|
uksort($aProtectedCells, ['self', 'cellReverseSort']) : uksort($aProtectedCells, ['self', 'cellSort']); |
296
|
32 |
|
foreach ($aProtectedCells as $key => $value) { |
297
|
16 |
|
$newReference = $this->updateCellReference($key, $pBefore, $pNumCols, $pNumRows); |
298
|
16 |
|
if ($key != $newReference) { |
299
|
16 |
|
$pSheet->protectCells($newReference, $value, true); |
300
|
16 |
|
$pSheet->unprotectCells($key); |
301
|
|
|
} |
302
|
|
|
} |
303
|
32 |
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Update column dimensions when inserting/deleting rows/columns. |
307
|
|
|
* |
308
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
309
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
310
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
311
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
312
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
313
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
314
|
|
|
*/ |
315
|
32 |
|
protected function adjustColumnDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
|
|
|
|
316
|
|
|
{ |
317
|
32 |
|
$aColumnDimensions = array_reverse($pSheet->getColumnDimensions(), true); |
318
|
32 |
|
if (!empty($aColumnDimensions)) { |
319
|
20 |
|
foreach ($aColumnDimensions as $objColumnDimension) { |
320
|
20 |
|
$newReference = $this->updateCellReference($objColumnDimension->getColumnIndex() . '1', $pBefore, $pNumCols, $pNumRows); |
321
|
20 |
|
[$newReference] = Coordinate::coordinateFromString($newReference); |
322
|
20 |
|
if ($objColumnDimension->getColumnIndex() != $newReference) { |
323
|
16 |
|
$objColumnDimension->setColumnIndex($newReference); |
324
|
|
|
} |
325
|
|
|
} |
326
|
20 |
|
$pSheet->refreshColumnDimensions(); |
327
|
|
|
} |
328
|
32 |
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Update row dimensions when inserting/deleting rows/columns. |
332
|
|
|
* |
333
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
334
|
|
|
* @param string $pBefore Insert/Delete before this cell address (e.g. 'A1') |
335
|
|
|
* @param int $beforeColumnIndex Index number of the column we're inserting/deleting before |
336
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
337
|
|
|
* @param int $beforeRow Number of the row we're inserting/deleting before |
338
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
339
|
|
|
*/ |
340
|
32 |
|
protected function adjustRowDimensions($pSheet, $pBefore, $beforeColumnIndex, $pNumCols, $beforeRow, $pNumRows): void |
|
|
|
|
341
|
|
|
{ |
342
|
32 |
|
$aRowDimensions = array_reverse($pSheet->getRowDimensions(), true); |
343
|
32 |
|
if (!empty($aRowDimensions)) { |
344
|
3 |
|
foreach ($aRowDimensions as $objRowDimension) { |
345
|
3 |
|
$newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex(), $pBefore, $pNumCols, $pNumRows); |
346
|
3 |
|
[, $newReference] = Coordinate::coordinateFromString($newReference); |
347
|
3 |
|
if ($objRowDimension->getRowIndex() != $newReference) { |
348
|
3 |
|
$objRowDimension->setRowIndex($newReference); |
349
|
|
|
} |
350
|
|
|
} |
351
|
3 |
|
$pSheet->refreshRowDimensions(); |
352
|
|
|
|
353
|
3 |
|
$copyDimension = $pSheet->getRowDimension($beforeRow - 1); |
354
|
3 |
|
for ($i = $beforeRow; $i <= $beforeRow - 1 + $pNumRows; ++$i) { |
355
|
2 |
|
$newDimension = $pSheet->getRowDimension($i); |
356
|
2 |
|
$newDimension->setRowHeight($copyDimension->getRowHeight()); |
357
|
2 |
|
$newDimension->setVisible($copyDimension->getVisible()); |
358
|
2 |
|
$newDimension->setOutlineLevel($copyDimension->getOutlineLevel()); |
359
|
2 |
|
$newDimension->setCollapsed($copyDimension->getCollapsed()); |
360
|
|
|
} |
361
|
|
|
} |
362
|
32 |
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Insert a new column or row, updating all possible related data. |
366
|
|
|
* |
367
|
|
|
* @param string $pBefore Insert before this cell address (e.g. 'A1') |
368
|
|
|
* @param int $pNumCols Number of columns to insert/delete (negative values indicate deletion) |
369
|
|
|
* @param int $pNumRows Number of rows to insert/delete (negative values indicate deletion) |
370
|
|
|
* @param Worksheet $pSheet The worksheet that we're editing |
371
|
|
|
*/ |
372
|
32 |
|
public function insertNewBefore($pBefore, $pNumCols, $pNumRows, Worksheet $pSheet): void |
373
|
|
|
{ |
374
|
32 |
|
$remove = ($pNumCols < 0 || $pNumRows < 0); |
375
|
32 |
|
$allCoordinates = $pSheet->getCoordinates(); |
376
|
|
|
|
377
|
|
|
// Get coordinate of $pBefore |
378
|
32 |
|
[$beforeColumn, $beforeRow] = Coordinate::indexesFromString($pBefore); |
379
|
|
|
|
380
|
|
|
// Clear cells if we are removing columns or rows |
381
|
32 |
|
$highestColumn = $pSheet->getHighestColumn(); |
382
|
32 |
|
$highestRow = $pSheet->getHighestRow(); |
383
|
|
|
|
384
|
|
|
// 1. Clear column strips if we are removing columns |
385
|
32 |
|
if ($pNumCols < 0 && $beforeColumn - 2 + $pNumCols > 0) { |
386
|
17 |
|
for ($i = 1; $i <= $highestRow - 1; ++$i) { |
387
|
17 |
|
for ($j = $beforeColumn - 1 + $pNumCols; $j <= $beforeColumn - 2; ++$j) { |
388
|
17 |
|
$coordinate = Coordinate::stringFromColumnIndex($j + 1) . $i; |
389
|
17 |
|
$pSheet->removeConditionalStyles($coordinate); |
390
|
17 |
|
if ($pSheet->cellExists($coordinate)) { |
391
|
17 |
|
$pSheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL); |
392
|
17 |
|
$pSheet->getCell($coordinate)->setXfIndex(0); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
// 2. Clear row strips if we are removing rows |
399
|
32 |
|
if ($pNumRows < 0 && $beforeRow - 1 + $pNumRows > 0) { |
400
|
24 |
|
for ($i = $beforeColumn - 1; $i <= Coordinate::columnIndexFromString($highestColumn) - 1; ++$i) { |
401
|
24 |
|
for ($j = $beforeRow + $pNumRows; $j <= $beforeRow - 1; ++$j) { |
402
|
24 |
|
$coordinate = Coordinate::stringFromColumnIndex($i + 1) . $j; |
403
|
24 |
|
$pSheet->removeConditionalStyles($coordinate); |
404
|
24 |
|
if ($pSheet->cellExists($coordinate)) { |
405
|
|
|
$pSheet->getCell($coordinate)->setValueExplicit('', DataType::TYPE_NULL); |
406
|
|
|
$pSheet->getCell($coordinate)->setXfIndex(0); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
// Loop through cells, bottom-up, and change cell coordinate |
413
|
32 |
|
if ($remove) { |
414
|
|
|
// It's faster to reverse and pop than to use unshift, especially with large cell collections |
415
|
32 |
|
$allCoordinates = array_reverse($allCoordinates); |
416
|
|
|
} |
417
|
32 |
|
while ($coordinate = array_pop($allCoordinates)) { |
418
|
32 |
|
$cell = $pSheet->getCell($coordinate); |
419
|
32 |
|
$cellIndex = Coordinate::columnIndexFromString($cell->getColumn()); |
420
|
|
|
|
421
|
32 |
|
if ($cellIndex - 1 + $pNumCols < 0) { |
422
|
20 |
|
continue; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
// New coordinate |
426
|
31 |
|
$newCoordinate = Coordinate::stringFromColumnIndex($cellIndex + $pNumCols) . ($cell->getRow() + $pNumRows); |
427
|
|
|
|
428
|
|
|
// Should the cell be updated? Move value and cellXf index from one cell to another. |
429
|
31 |
|
if (($cellIndex >= $beforeColumn) && ($cell->getRow() >= $beforeRow)) { |
430
|
|
|
// Update cell styles |
431
|
26 |
|
$pSheet->getCell($newCoordinate)->setXfIndex($cell->getXfIndex()); |
432
|
|
|
|
433
|
|
|
// Insert this cell at its new location |
434
|
26 |
|
if ($cell->getDataType() == DataType::TYPE_FORMULA) { |
435
|
|
|
// Formula should be adjusted |
436
|
18 |
|
$pSheet->getCell($newCoordinate) |
437
|
18 |
|
->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle())); |
438
|
|
|
} else { |
439
|
|
|
// Formula should not be adjusted |
440
|
26 |
|
$pSheet->getCell($newCoordinate)->setValue($cell->getValue()); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
// Clear the original cell |
444
|
26 |
|
$pSheet->getCellCollection()->delete($coordinate); |
445
|
|
|
} else { |
446
|
|
|
/* We don't need to update styles for rows/columns before our insertion position, |
447
|
|
|
but we do still need to adjust any formulae in those cells */ |
448
|
26 |
|
if ($cell->getDataType() == DataType::TYPE_FORMULA) { |
449
|
|
|
// Formula should be adjusted |
450
|
18 |
|
$cell->setValue($this->updateFormulaReferences($cell->getValue(), $pBefore, $pNumCols, $pNumRows, $pSheet->getTitle())); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
// Duplicate styles for the newly inserted cells |
456
|
32 |
|
$highestColumn = $pSheet->getHighestColumn(); |
457
|
32 |
|
$highestRow = $pSheet->getHighestRow(); |
458
|
|
|
|
459
|
32 |
|
if ($pNumCols > 0 && $beforeColumn - 2 > 0) { |
460
|
16 |
|
for ($i = $beforeRow; $i <= $highestRow - 1; ++$i) { |
461
|
|
|
// Style |
462
|
16 |
|
$coordinate = Coordinate::stringFromColumnIndex($beforeColumn - 1) . $i; |
463
|
16 |
|
if ($pSheet->cellExists($coordinate)) { |
464
|
16 |
|
$xfIndex = $pSheet->getCell($coordinate)->getXfIndex(); |
465
|
16 |
|
$conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ? |
466
|
16 |
|
$pSheet->getConditionalStyles($coordinate) : false; |
467
|
16 |
|
for ($j = $beforeColumn; $j <= $beforeColumn - 1 + $pNumCols; ++$j) { |
468
|
16 |
|
$pSheet->getCellByColumnAndRow($j, $i)->setXfIndex($xfIndex); |
469
|
16 |
|
if ($conditionalStyles) { |
470
|
|
|
$cloned = []; |
471
|
|
|
foreach ($conditionalStyles as $conditionalStyle) { |
472
|
|
|
$cloned[] = clone $conditionalStyle; |
473
|
|
|
} |
474
|
|
|
$pSheet->setConditionalStyles(Coordinate::stringFromColumnIndex($j) . $i, $cloned); |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
} |
479
|
|
|
} |
480
|
|
|
|
481
|
32 |
|
if ($pNumRows > 0 && $beforeRow - 1 > 0) { |
482
|
18 |
|
for ($i = $beforeColumn; $i <= Coordinate::columnIndexFromString($highestColumn); ++$i) { |
483
|
|
|
// Style |
484
|
18 |
|
$coordinate = Coordinate::stringFromColumnIndex($i) . ($beforeRow - 1); |
485
|
18 |
|
if ($pSheet->cellExists($coordinate)) { |
486
|
18 |
|
$xfIndex = $pSheet->getCell($coordinate)->getXfIndex(); |
487
|
18 |
|
$conditionalStyles = $pSheet->conditionalStylesExists($coordinate) ? |
488
|
18 |
|
$pSheet->getConditionalStyles($coordinate) : false; |
489
|
18 |
|
for ($j = $beforeRow; $j <= $beforeRow - 1 + $pNumRows; ++$j) { |
490
|
18 |
|
$pSheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex); |
491
|
18 |
|
if ($conditionalStyles) { |
492
|
|
|
$cloned = []; |
493
|
|
|
foreach ($conditionalStyles as $conditionalStyle) { |
494
|
|
|
$cloned[] = clone $conditionalStyle; |
495
|
|
|
} |
496
|
|
|
$pSheet->setConditionalStyles(Coordinate::stringFromColumnIndex($i) . $j, $cloned); |
497
|
|
|
} |
498
|
|
|
} |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
// Update worksheet: column dimensions |
504
|
32 |
|
$this->adjustColumnDimensions($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
505
|
|
|
|
506
|
|
|
// Update worksheet: row dimensions |
507
|
32 |
|
$this->adjustRowDimensions($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
508
|
|
|
|
509
|
|
|
// Update worksheet: page breaks |
510
|
32 |
|
$this->adjustPageBreaks($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
511
|
|
|
|
512
|
|
|
// Update worksheet: comments |
513
|
32 |
|
$this->adjustComments($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
514
|
|
|
|
515
|
|
|
// Update worksheet: hyperlinks |
516
|
32 |
|
$this->adjustHyperlinks($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
517
|
|
|
|
518
|
|
|
// Update worksheet: data validations |
519
|
32 |
|
$this->adjustDataValidations($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
520
|
|
|
|
521
|
|
|
// Update worksheet: merge cells |
522
|
32 |
|
$this->adjustMergeCells($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
523
|
|
|
|
524
|
|
|
// Update worksheet: protected cells |
525
|
32 |
|
$this->adjustProtectedCells($pSheet, $pBefore, $beforeColumn, $pNumCols, $beforeRow, $pNumRows); |
526
|
|
|
|
527
|
|
|
// Update worksheet: autofilter |
528
|
32 |
|
$autoFilter = $pSheet->getAutoFilter(); |
529
|
32 |
|
$autoFilterRange = $autoFilter->getRange(); |
530
|
32 |
|
if (!empty($autoFilterRange)) { |
531
|
|
|
if ($pNumCols != 0) { |
532
|
|
|
$autoFilterColumns = $autoFilter->getColumns(); |
533
|
|
|
if (count($autoFilterColumns) > 0) { |
534
|
|
|
$column = ''; |
535
|
|
|
$row = 0; |
536
|
|
|
sscanf($pBefore, '%[A-Z]%d', $column, $row); |
537
|
|
|
$columnIndex = Coordinate::columnIndexFromString($column); |
538
|
|
|
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange); |
539
|
|
|
if ($columnIndex <= $rangeEnd[0]) { |
540
|
|
|
if ($pNumCols < 0) { |
541
|
|
|
// If we're actually deleting any columns that fall within the autofilter range, |
542
|
|
|
// then we delete any rules for those columns |
543
|
|
|
$deleteColumn = $columnIndex + $pNumCols - 1; |
544
|
|
|
$deleteCount = abs($pNumCols); |
545
|
|
|
for ($i = 1; $i <= $deleteCount; ++$i) { |
546
|
|
|
if (isset($autoFilterColumns[Coordinate::stringFromColumnIndex($deleteColumn + 1)])) { |
547
|
|
|
$autoFilter->clearColumn(Coordinate::stringFromColumnIndex($deleteColumn + 1)); |
548
|
|
|
} |
549
|
|
|
++$deleteColumn; |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
$startCol = ($columnIndex > $rangeStart[0]) ? $columnIndex : $rangeStart[0]; |
553
|
|
|
|
554
|
|
|
// Shuffle columns in autofilter range |
555
|
|
|
if ($pNumCols > 0) { |
556
|
|
|
$startColRef = $startCol; |
557
|
|
|
$endColRef = $rangeEnd[0]; |
558
|
|
|
$toColRef = $rangeEnd[0] + $pNumCols; |
559
|
|
|
|
560
|
|
|
do { |
561
|
|
|
$autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef)); |
562
|
|
|
--$endColRef; |
563
|
|
|
--$toColRef; |
564
|
|
|
} while ($startColRef <= $endColRef); |
565
|
|
|
} else { |
566
|
|
|
// For delete, we shuffle from beginning to end to avoid overwriting |
567
|
|
|
$startColID = Coordinate::stringFromColumnIndex($startCol); |
568
|
|
|
$toColID = Coordinate::stringFromColumnIndex($startCol + $pNumCols); |
569
|
|
|
$endColID = Coordinate::stringFromColumnIndex($rangeEnd[0] + 1); |
570
|
|
|
do { |
571
|
|
|
$autoFilter->shiftColumn($startColID, $toColID); |
572
|
|
|
++$startColID; |
573
|
|
|
++$toColID; |
574
|
|
|
} while ($startColID != $endColID); |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
$pSheet->setAutoFilter($this->updateCellReference($autoFilterRange, $pBefore, $pNumCols, $pNumRows)); |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
// Update worksheet: freeze pane |
583
|
32 |
|
if ($pSheet->getFreezePane()) { |
584
|
1 |
|
$splitCell = $pSheet->getFreezePane(); |
585
|
1 |
|
$topLeftCell = $pSheet->getTopLeftCell(); |
586
|
|
|
|
587
|
1 |
|
$splitCell = $this->updateCellReference($splitCell, $pBefore, $pNumCols, $pNumRows); |
588
|
1 |
|
$topLeftCell = $this->updateCellReference($topLeftCell, $pBefore, $pNumCols, $pNumRows); |
589
|
|
|
|
590
|
1 |
|
$pSheet->freezePane($splitCell, $topLeftCell); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
// Page setup |
594
|
32 |
|
if ($pSheet->getPageSetup()->isPrintAreaSet()) { |
595
|
|
|
$pSheet->getPageSetup()->setPrintArea($this->updateCellReference($pSheet->getPageSetup()->getPrintArea(), $pBefore, $pNumCols, $pNumRows)); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
// Update worksheet: drawings |
599
|
32 |
|
$aDrawings = $pSheet->getDrawingCollection(); |
600
|
32 |
|
foreach ($aDrawings as $objDrawing) { |
601
|
17 |
|
$newReference = $this->updateCellReference($objDrawing->getCoordinates(), $pBefore, $pNumCols, $pNumRows); |
602
|
17 |
|
if ($objDrawing->getCoordinates() != $newReference) { |
603
|
17 |
|
$objDrawing->setCoordinates($newReference); |
604
|
|
|
} |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
// Update workbook: define names |
608
|
32 |
|
if (count($pSheet->getParent()->getDefinedNames()) > 0) { |
609
|
1 |
|
foreach ($pSheet->getParent()->getDefinedNames() as $definedName) { |
610
|
1 |
|
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashCode() === $pSheet->getHashCode()) { |
611
|
1 |
|
$definedName->setValue($this->updateCellReference($definedName->getValue(), $pBefore, $pNumCols, $pNumRows)); |
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
// Garbage collect |
617
|
32 |
|
$pSheet->garbageCollect(); |
618
|
32 |
|
} |
619
|
|
|
|
620
|
|
|
/** |
621
|
|
|
* Update references within formulas. |
622
|
|
|
* |
623
|
|
|
* @param string $pFormula Formula to update |
624
|
|
|
* @param string $pBefore Insert before this one |
625
|
|
|
* @param int $pNumCols Number of columns to insert |
626
|
|
|
* @param int $pNumRows Number of rows to insert |
627
|
|
|
* @param string $sheetName Worksheet name/title |
628
|
|
|
* |
629
|
|
|
* @return string Updated formula |
630
|
|
|
*/ |
631
|
39 |
|
public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') |
632
|
|
|
{ |
633
|
|
|
// Update cell references in the formula |
634
|
39 |
|
$formulaBlocks = explode('"', $pFormula); |
635
|
39 |
|
$i = false; |
636
|
39 |
|
foreach ($formulaBlocks as &$formulaBlock) { |
637
|
|
|
// Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode) |
638
|
39 |
|
if ($i = !$i) { |
|
|
|
|
639
|
39 |
|
$adjustCount = 0; |
640
|
39 |
|
$newCellTokens = $cellTokens = []; |
641
|
|
|
// Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5) |
642
|
39 |
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_ROWRANGE . '/i', ' ' . $formulaBlock . ' ', $matches, PREG_SET_ORDER); |
643
|
39 |
|
if ($matchCount > 0) { |
644
|
1 |
|
foreach ($matches as $match) { |
645
|
1 |
|
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; |
646
|
1 |
|
$fromString .= $match[3] . ':' . $match[4]; |
647
|
1 |
|
$modified3 = substr($this->updateCellReference('$A' . $match[3], $pBefore, $pNumCols, $pNumRows), 2); |
648
|
1 |
|
$modified4 = substr($this->updateCellReference('$A' . $match[4], $pBefore, $pNumCols, $pNumRows), 2); |
649
|
|
|
|
650
|
1 |
|
if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) { |
651
|
1 |
|
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { |
652
|
1 |
|
$toString = ($match[2] > '') ? $match[2] . '!' : ''; |
653
|
1 |
|
$toString .= $modified3 . ':' . $modified4; |
654
|
|
|
// 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 |
655
|
1 |
|
$column = 100000; |
656
|
1 |
|
$row = 10000000 + (int) trim($match[3], '$'); |
657
|
1 |
|
$cellIndex = $column . $row; |
658
|
|
|
|
659
|
1 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
660
|
1 |
|
$cellTokens[$cellIndex] = '/(?<!\d\$\!)' . preg_quote($fromString, '/') . '(?!\d)/i'; |
661
|
1 |
|
++$adjustCount; |
662
|
|
|
} |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
} |
666
|
|
|
// Search for column ranges (e.g. 'Sheet1'!C:E or C:E) with or without $ absolutes (e.g. $C:E) |
667
|
39 |
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_COLRANGE . '/i', ' ' . $formulaBlock . ' ', $matches, PREG_SET_ORDER); |
668
|
39 |
|
if ($matchCount > 0) { |
669
|
1 |
|
foreach ($matches as $match) { |
670
|
1 |
|
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; |
671
|
1 |
|
$fromString .= $match[3] . ':' . $match[4]; |
672
|
1 |
|
$modified3 = substr($this->updateCellReference($match[3] . '$1', $pBefore, $pNumCols, $pNumRows), 0, -2); |
673
|
1 |
|
$modified4 = substr($this->updateCellReference($match[4] . '$1', $pBefore, $pNumCols, $pNumRows), 0, -2); |
674
|
|
|
|
675
|
1 |
|
if ($match[3] . ':' . $match[4] !== $modified3 . ':' . $modified4) { |
676
|
1 |
|
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { |
677
|
1 |
|
$toString = ($match[2] > '') ? $match[2] . '!' : ''; |
678
|
1 |
|
$toString .= $modified3 . ':' . $modified4; |
679
|
|
|
// 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 |
680
|
1 |
|
$column = Coordinate::columnIndexFromString(trim($match[3], '$')) + 100000; |
681
|
1 |
|
$row = 10000000; |
682
|
1 |
|
$cellIndex = $column . $row; |
683
|
|
|
|
684
|
1 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
685
|
1 |
|
$cellTokens[$cellIndex] = '/(?<![A-Z\$\!])' . preg_quote($fromString, '/') . '(?![A-Z])/i'; |
686
|
1 |
|
++$adjustCount; |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
} |
691
|
|
|
// Search for cell ranges (e.g. 'Sheet1'!A3:C5 or A3:C5) with or without $ absolutes (e.g. $A1:C$5) |
692
|
39 |
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_CELLRANGE . '/i', ' ' . $formulaBlock . ' ', $matches, PREG_SET_ORDER); |
693
|
39 |
|
if ($matchCount > 0) { |
694
|
25 |
|
foreach ($matches as $match) { |
695
|
25 |
|
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; |
696
|
25 |
|
$fromString .= $match[3] . ':' . $match[4]; |
697
|
25 |
|
$modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows); |
698
|
25 |
|
$modified4 = $this->updateCellReference($match[4], $pBefore, $pNumCols, $pNumRows); |
699
|
|
|
|
700
|
25 |
|
if ($match[3] . $match[4] !== $modified3 . $modified4) { |
701
|
23 |
|
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { |
702
|
23 |
|
$toString = ($match[2] > '') ? $match[2] . '!' : ''; |
703
|
23 |
|
$toString .= $modified3 . ':' . $modified4; |
704
|
23 |
|
[$column, $row] = Coordinate::coordinateFromString($match[3]); |
705
|
|
|
// 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 |
706
|
23 |
|
$column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000; |
707
|
23 |
|
$row = (int) trim($row, '$') + 10000000; |
708
|
23 |
|
$cellIndex = $column . $row; |
709
|
|
|
|
710
|
23 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
711
|
23 |
|
$cellTokens[$cellIndex] = '/(?<![A-Z]\$\!)' . preg_quote($fromString, '/') . '(?!\d)/i'; |
712
|
23 |
|
++$adjustCount; |
713
|
|
|
} |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
// Search for cell references (e.g. 'Sheet1'!A3 or C5) with or without $ absolutes (e.g. $A1 or C$5) |
718
|
39 |
|
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_CELLREF . '/i', ' ' . $formulaBlock . ' ', $matches, PREG_SET_ORDER); |
719
|
|
|
|
720
|
39 |
|
if ($matchCount > 0) { |
721
|
33 |
|
foreach ($matches as $match) { |
722
|
33 |
|
$fromString = ($match[2] > '') ? $match[2] . '!' : ''; |
723
|
33 |
|
$fromString .= $match[3]; |
724
|
|
|
|
725
|
33 |
|
$modified3 = $this->updateCellReference($match[3], $pBefore, $pNumCols, $pNumRows); |
726
|
33 |
|
if ($match[3] !== $modified3) { |
727
|
33 |
|
if (($match[2] == '') || (trim($match[2], "'") == $sheetName)) { |
728
|
33 |
|
$toString = ($match[2] > '') ? $match[2] . '!' : ''; |
729
|
33 |
|
$toString .= $modified3; |
730
|
33 |
|
[$column, $row] = Coordinate::coordinateFromString($match[3]); |
731
|
|
|
// 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 |
732
|
33 |
|
$column = Coordinate::columnIndexFromString(trim($column, '$')) + 100000; |
733
|
33 |
|
$row = (int) trim($row, '$') + 10000000; |
734
|
33 |
|
$cellIndex = $row . $column; |
735
|
|
|
|
736
|
33 |
|
$newCellTokens[$cellIndex] = preg_quote($toString, '/'); |
737
|
33 |
|
$cellTokens[$cellIndex] = '/(?<![A-Z\$\!])' . preg_quote($fromString, '/') . '(?!\d)/i'; |
738
|
33 |
|
++$adjustCount; |
739
|
|
|
} |
740
|
|
|
} |
741
|
|
|
} |
742
|
|
|
} |
743
|
39 |
|
if ($adjustCount > 0) { |
744
|
37 |
|
if ($pNumCols > 0 || $pNumRows > 0) { |
745
|
32 |
|
krsort($cellTokens); |
746
|
32 |
|
krsort($newCellTokens); |
747
|
|
|
} else { |
748
|
22 |
|
ksort($cellTokens); |
749
|
22 |
|
ksort($newCellTokens); |
750
|
|
|
} // Update cell references in the formula |
751
|
37 |
|
$formulaBlock = str_replace('\\', '', preg_replace($cellTokens, $newCellTokens, $formulaBlock)); |
752
|
|
|
} |
753
|
|
|
} |
754
|
|
|
} |
755
|
39 |
|
unset($formulaBlock); |
756
|
|
|
|
757
|
|
|
// Then rebuild the formula string |
758
|
39 |
|
return implode('"', $formulaBlocks); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* Update all cell references within a formula, irrespective of worksheet. |
763
|
|
|
*/ |
764
|
78 |
|
public function updateFormulaReferencesAnyWorksheet(string $formula = '', int $insertColumns = 0, int $insertRows = 0): string |
765
|
|
|
{ |
766
|
78 |
|
$formula = $this->updateCellReferencesAllWorksheets($formula, $insertColumns, $insertRows); |
767
|
|
|
|
768
|
78 |
|
if ($insertColumns !== 0) { |
769
|
74 |
|
$formula = $this->updateColumnRangesAllWorksheets($formula, $insertColumns); |
770
|
|
|
} |
771
|
|
|
|
772
|
78 |
|
if ($insertRows !== 0) { |
773
|
72 |
|
$formula = $this->updateRowRangesAllWorksheets($formula, $insertRows); |
774
|
|
|
} |
775
|
|
|
|
776
|
78 |
|
return $formula; |
777
|
|
|
} |
778
|
|
|
|
779
|
78 |
|
private function updateCellReferencesAllWorksheets(string $formula, int $insertColumns, int $insertRows): string |
780
|
|
|
{ |
781
|
78 |
|
$splitCount = preg_match_all( |
782
|
78 |
|
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', |
783
|
|
|
$formula, |
784
|
|
|
$splitRanges, |
785
|
78 |
|
PREG_OFFSET_CAPTURE |
786
|
|
|
); |
787
|
|
|
|
788
|
78 |
|
$columnLengths = array_map('strlen', array_column($splitRanges[6], 0)); |
789
|
78 |
|
$rowLengths = array_map('strlen', array_column($splitRanges[7], 0)); |
790
|
78 |
|
$columnOffsets = array_column($splitRanges[6], 1); |
791
|
78 |
|
$rowOffsets = array_column($splitRanges[7], 1); |
792
|
|
|
|
793
|
78 |
|
$columns = $splitRanges[6]; |
794
|
78 |
|
$rows = $splitRanges[7]; |
795
|
|
|
|
796
|
78 |
|
while ($splitCount > 0) { |
797
|
72 |
|
--$splitCount; |
798
|
72 |
|
$columnLength = $columnLengths[$splitCount]; |
799
|
72 |
|
$rowLength = $rowLengths[$splitCount]; |
800
|
72 |
|
$columnOffset = $columnOffsets[$splitCount]; |
801
|
72 |
|
$rowOffset = $rowOffsets[$splitCount]; |
802
|
72 |
|
$column = $columns[$splitCount][0]; |
803
|
72 |
|
$row = $rows[$splitCount][0]; |
804
|
|
|
|
805
|
72 |
|
if (!empty($column) && $column[0] !== '$') { |
806
|
20 |
|
$column = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($column) + $insertColumns); |
807
|
20 |
|
$formula = substr($formula, 0, $columnOffset) . $column . substr($formula, $columnOffset + $columnLength); |
808
|
|
|
} |
809
|
72 |
|
if (!empty($row) && $row[0] !== '$') { |
810
|
39 |
|
$row += $insertRows; |
811
|
39 |
|
$formula = substr($formula, 0, $rowOffset) . $row . substr($formula, $rowOffset + $rowLength); |
812
|
|
|
} |
813
|
|
|
} |
814
|
|
|
|
815
|
78 |
|
return $formula; |
816
|
|
|
} |
817
|
|
|
|
818
|
74 |
|
private function updateColumnRangesAllWorksheets(string $formula, int $insertColumns): string |
819
|
|
|
{ |
820
|
74 |
|
$splitCount = preg_match_all( |
821
|
74 |
|
'/' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '/mui', |
822
|
|
|
$formula, |
823
|
|
|
$splitRanges, |
824
|
74 |
|
PREG_OFFSET_CAPTURE |
825
|
|
|
); |
826
|
|
|
|
827
|
74 |
|
$fromColumnLengths = array_map('strlen', array_column($splitRanges[1], 0)); |
828
|
74 |
|
$fromColumnOffsets = array_column($splitRanges[1], 1); |
829
|
74 |
|
$toColumnLengths = array_map('strlen', array_column($splitRanges[2], 0)); |
830
|
74 |
|
$toColumnOffsets = array_column($splitRanges[2], 1); |
831
|
|
|
|
832
|
74 |
|
$fromColumns = $splitRanges[1]; |
833
|
74 |
|
$toColumns = $splitRanges[2]; |
834
|
|
|
|
835
|
74 |
|
while ($splitCount > 0) { |
836
|
3 |
|
--$splitCount; |
837
|
3 |
|
$fromColumnLength = $fromColumnLengths[$splitCount]; |
838
|
3 |
|
$toColumnLength = $toColumnLengths[$splitCount]; |
839
|
3 |
|
$fromColumnOffset = $fromColumnOffsets[$splitCount]; |
840
|
3 |
|
$toColumnOffset = $toColumnOffsets[$splitCount]; |
841
|
3 |
|
$fromColumn = $fromColumns[$splitCount][0]; |
842
|
3 |
|
$toColumn = $toColumns[$splitCount][0]; |
843
|
|
|
|
844
|
3 |
|
if (!empty($fromColumn) && $fromColumn[0] !== '$') { |
845
|
2 |
|
$fromColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($fromColumn) + $insertColumns); |
846
|
2 |
|
$formula = substr($formula, 0, $fromColumnOffset) . $fromColumn . substr($formula, $fromColumnOffset + $fromColumnLength); |
847
|
|
|
} |
848
|
3 |
|
if (!empty($toColumn) && $toColumn[0] !== '$') { |
849
|
2 |
|
$toColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($toColumn) + $insertColumns); |
850
|
2 |
|
$formula = substr($formula, 0, $toColumnOffset) . $toColumn . substr($formula, $toColumnOffset + $toColumnLength); |
851
|
|
|
} |
852
|
|
|
} |
853
|
|
|
|
854
|
74 |
|
return $formula; |
855
|
|
|
} |
856
|
|
|
|
857
|
72 |
|
private function updateRowRangesAllWorksheets(string $formula, int $insertRows): string |
858
|
|
|
{ |
859
|
72 |
|
$splitCount = preg_match_all( |
860
|
72 |
|
'/' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '/mui', |
861
|
|
|
$formula, |
862
|
|
|
$splitRanges, |
863
|
72 |
|
PREG_OFFSET_CAPTURE |
864
|
|
|
); |
865
|
|
|
|
866
|
72 |
|
$fromRowLengths = array_map('strlen', array_column($splitRanges[1], 0)); |
867
|
72 |
|
$fromRowOffsets = array_column($splitRanges[1], 1); |
868
|
72 |
|
$toRowLengths = array_map('strlen', array_column($splitRanges[2], 0)); |
869
|
72 |
|
$toRowOffsets = array_column($splitRanges[2], 1); |
870
|
|
|
|
871
|
72 |
|
$fromRows = $splitRanges[1]; |
872
|
72 |
|
$toRows = $splitRanges[2]; |
873
|
|
|
|
874
|
72 |
|
while ($splitCount > 0) { |
875
|
3 |
|
--$splitCount; |
876
|
3 |
|
$fromRowLength = $fromRowLengths[$splitCount]; |
877
|
3 |
|
$toRowLength = $toRowLengths[$splitCount]; |
878
|
3 |
|
$fromRowOffset = $fromRowOffsets[$splitCount]; |
879
|
3 |
|
$toRowOffset = $toRowOffsets[$splitCount]; |
880
|
3 |
|
$fromRow = $fromRows[$splitCount][0]; |
881
|
3 |
|
$toRow = $toRows[$splitCount][0]; |
882
|
|
|
|
883
|
3 |
|
if (!empty($fromRow) && $fromRow[0] !== '$') { |
884
|
2 |
|
$fromRow += $insertRows; |
885
|
2 |
|
$formula = substr($formula, 0, $fromRowOffset) . $fromRow . substr($formula, $fromRowOffset + $fromRowLength); |
886
|
|
|
} |
887
|
3 |
|
if (!empty($toRow) && $toRow[0] !== '$') { |
888
|
2 |
|
$toRow += $insertRows; |
889
|
2 |
|
$formula = substr($formula, 0, $toRowOffset) . $toRow . substr($formula, $toRowOffset + $toRowLength); |
890
|
|
|
} |
891
|
|
|
} |
892
|
|
|
|
893
|
72 |
|
return $formula; |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
/** |
897
|
|
|
* Update cell reference. |
898
|
|
|
* |
899
|
|
|
* @param string $pCellRange Cell range |
900
|
|
|
* @param string $pBefore Insert before this one |
901
|
|
|
* @param int $pNumCols Number of columns to increment |
902
|
|
|
* @param int $pNumRows Number of rows to increment |
903
|
|
|
* |
904
|
|
|
* @return string Updated cell range |
905
|
|
|
*/ |
906
|
41 |
|
public function updateCellReference($pCellRange = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0) |
907
|
|
|
{ |
908
|
|
|
// Is it in another worksheet? Will not have to update anything. |
909
|
41 |
|
if (strpos($pCellRange, '!') !== false) { |
910
|
|
|
return $pCellRange; |
911
|
|
|
// Is it a range or a single cell? |
912
|
41 |
|
} elseif (!Coordinate::coordinateIsRange($pCellRange)) { |
913
|
|
|
// Single cell |
914
|
41 |
|
return $this->updateSingleCellReference($pCellRange, $pBefore, $pNumCols, $pNumRows); |
915
|
18 |
|
} elseif (Coordinate::coordinateIsRange($pCellRange)) { |
916
|
|
|
// Range |
917
|
18 |
|
return $this->updateCellRange($pCellRange, $pBefore, $pNumCols, $pNumRows); |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
// Return original |
921
|
|
|
return $pCellRange; |
922
|
|
|
} |
923
|
|
|
|
924
|
|
|
/** |
925
|
|
|
* Update named formulas (i.e. containing worksheet references / named ranges). |
926
|
|
|
* |
927
|
|
|
* @param Spreadsheet $spreadsheet Object to update |
928
|
|
|
* @param string $oldName Old name (name to replace) |
929
|
|
|
* @param string $newName New name |
930
|
|
|
*/ |
931
|
251 |
|
public function updateNamedFormulas(Spreadsheet $spreadsheet, $oldName = '', $newName = ''): void |
932
|
|
|
{ |
933
|
251 |
|
if ($oldName == '') { |
934
|
|
|
return; |
935
|
|
|
} |
936
|
|
|
|
937
|
251 |
|
foreach ($spreadsheet->getWorksheetIterator() as $sheet) { |
938
|
251 |
|
foreach ($sheet->getCoordinates(false) as $coordinate) { |
939
|
60 |
|
$cell = $sheet->getCell($coordinate); |
940
|
60 |
|
if (($cell !== null) && ($cell->getDataType() == DataType::TYPE_FORMULA)) { |
941
|
45 |
|
$formula = $cell->getValue(); |
942
|
45 |
|
if (strpos($formula, $oldName) !== false) { |
943
|
|
|
$formula = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $formula); |
944
|
|
|
$formula = str_replace($oldName . '!', $newName . '!', $formula); |
945
|
|
|
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA); |
946
|
|
|
} |
947
|
|
|
} |
948
|
|
|
} |
949
|
|
|
} |
950
|
251 |
|
} |
951
|
|
|
|
952
|
|
|
/** |
953
|
|
|
* Update cell range. |
954
|
|
|
* |
955
|
|
|
* @param string $pCellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3') |
956
|
|
|
* @param string $pBefore Insert before this one |
957
|
|
|
* @param int $pNumCols Number of columns to increment |
958
|
|
|
* @param int $pNumRows Number of rows to increment |
959
|
|
|
* |
960
|
|
|
* @return string Updated cell range |
961
|
|
|
*/ |
962
|
18 |
|
private function updateCellRange($pCellRange = 'A1:A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0) |
963
|
|
|
{ |
964
|
18 |
|
if (!Coordinate::coordinateIsRange($pCellRange)) { |
965
|
|
|
throw new Exception('Only cell ranges may be passed to this method.'); |
966
|
|
|
} |
967
|
|
|
|
968
|
|
|
// Update range |
969
|
18 |
|
$range = Coordinate::splitRange($pCellRange); |
970
|
18 |
|
$ic = count($range); |
971
|
18 |
|
for ($i = 0; $i < $ic; ++$i) { |
972
|
18 |
|
$jc = count($range[$i]); |
973
|
18 |
|
for ($j = 0; $j < $jc; ++$j) { |
974
|
18 |
|
if (ctype_alpha($range[$i][$j])) { |
975
|
|
|
$r = Coordinate::coordinateFromString($this->updateSingleCellReference($range[$i][$j] . '1', $pBefore, $pNumCols, $pNumRows)); |
976
|
|
|
$range[$i][$j] = $r[0]; |
977
|
18 |
|
} elseif (ctype_digit($range[$i][$j])) { |
978
|
|
|
$r = Coordinate::coordinateFromString($this->updateSingleCellReference('A' . $range[$i][$j], $pBefore, $pNumCols, $pNumRows)); |
979
|
|
|
$range[$i][$j] = $r[1]; |
980
|
|
|
} else { |
981
|
18 |
|
$range[$i][$j] = $this->updateSingleCellReference($range[$i][$j], $pBefore, $pNumCols, $pNumRows); |
982
|
|
|
} |
983
|
|
|
} |
984
|
|
|
} |
985
|
|
|
|
986
|
|
|
// Recreate range string |
987
|
18 |
|
return Coordinate::buildRange($range); |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
/** |
991
|
|
|
* Update single cell reference. |
992
|
|
|
* |
993
|
|
|
* @param string $pCellReference Single cell reference |
994
|
|
|
* @param string $pBefore Insert before this one |
995
|
|
|
* @param int $pNumCols Number of columns to increment |
996
|
|
|
* @param int $pNumRows Number of rows to increment |
997
|
|
|
* |
998
|
|
|
* @return string Updated cell reference |
999
|
|
|
*/ |
1000
|
41 |
|
private function updateSingleCellReference($pCellReference = 'A1', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0) |
1001
|
|
|
{ |
1002
|
41 |
|
if (Coordinate::coordinateIsRange($pCellReference)) { |
1003
|
|
|
throw new Exception('Only single cell references may be passed to this method.'); |
1004
|
|
|
} |
1005
|
|
|
|
1006
|
|
|
// Get coordinate of $pBefore |
1007
|
41 |
|
[$beforeColumn, $beforeRow] = Coordinate::coordinateFromString($pBefore); |
1008
|
|
|
|
1009
|
|
|
// Get coordinate of $pCellReference |
1010
|
41 |
|
[$newColumn, $newRow] = Coordinate::coordinateFromString($pCellReference); |
1011
|
|
|
|
1012
|
|
|
// Verify which parts should be updated |
1013
|
41 |
|
$updateColumn = (($newColumn[0] != '$') && ($beforeColumn[0] != '$') && (Coordinate::columnIndexFromString($newColumn) >= Coordinate::columnIndexFromString($beforeColumn))); |
1014
|
41 |
|
$updateRow = (($newRow[0] != '$') && ($beforeRow[0] != '$') && $newRow >= $beforeRow); |
1015
|
|
|
|
1016
|
|
|
// Create new column reference |
1017
|
41 |
|
if ($updateColumn) { |
1018
|
36 |
|
$newColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($newColumn) + $pNumCols); |
1019
|
|
|
} |
1020
|
|
|
|
1021
|
|
|
// Create new row reference |
1022
|
41 |
|
if ($updateRow) { |
1023
|
33 |
|
$newRow = (int) $newRow + $pNumRows; |
1024
|
|
|
} |
1025
|
|
|
|
1026
|
|
|
// Return new reference |
1027
|
41 |
|
return $newColumn . $newRow; |
1028
|
|
|
} |
1029
|
|
|
|
1030
|
|
|
/** |
1031
|
|
|
* __clone implementation. Cloning should not be allowed in a Singleton! |
1032
|
|
|
*/ |
1033
|
|
|
final public function __clone() |
1034
|
|
|
{ |
1035
|
|
|
throw new Exception('Cloning a Singleton is not allowed!'); |
1036
|
|
|
} |
1037
|
|
|
} |
1038
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.