| 1441 |
ariadna |
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 |
public static function getInstance(): self
|
|
|
36 |
{
|
|
|
37 |
if (self::$instance === null) {
|
|
|
38 |
self::$instance = new self();
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
return self::$instance;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Create a new ReferenceHelper.
|
|
|
46 |
*/
|
|
|
47 |
protected function __construct()
|
|
|
48 |
{
|
|
|
49 |
}
|
|
|
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 |
public static function columnSort(string $a, string $b): int
|
|
|
59 |
{
|
|
|
60 |
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 |
public static function columnReverseSort(string $a, string $b): int
|
|
|
71 |
{
|
|
|
72 |
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 |
public static function cellSort(string $a, string $b): int
|
|
|
83 |
{
|
|
|
84 |
sscanf($a, '%[A-Z]%d', $ac, $ar);
|
|
|
85 |
/** @var int $ar */
|
|
|
86 |
/** @var string $ac */
|
|
|
87 |
sscanf($b, '%[A-Z]%d', $bc, $br);
|
|
|
88 |
/** @var int $br */
|
|
|
89 |
/** @var string $bc */
|
|
|
90 |
if ($ar === $br) {
|
|
|
91 |
return strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
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 |
public static function cellReverseSort(string $a, string $b): int
|
|
|
105 |
{
|
|
|
106 |
sscanf($a, '%[A-Z]%d', $ac, $ar);
|
|
|
107 |
/** @var int $ar */
|
|
|
108 |
/** @var string $ac */
|
|
|
109 |
sscanf($b, '%[A-Z]%d', $bc, $br);
|
|
|
110 |
/** @var int $br */
|
|
|
111 |
/** @var string $bc */
|
|
|
112 |
if ($ar === $br) {
|
|
|
113 |
return -strcasecmp(strlen($ac) . $ac, strlen($bc) . $bc);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
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 |
protected function adjustPageBreaks(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void
|
|
|
127 |
{
|
|
|
128 |
$aBreaks = $worksheet->getBreaks();
|
|
|
129 |
($numberOfColumns > 0 || $numberOfRows > 0)
|
|
|
130 |
? uksort($aBreaks, [self::class, 'cellReverseSort'])
|
|
|
131 |
: uksort($aBreaks, [self::class, 'cellSort']);
|
|
|
132 |
|
|
|
133 |
foreach ($aBreaks as $cellAddress => $value) {
|
|
|
134 |
/** @var CellReferenceHelper */
|
|
|
135 |
$cellReferenceHelper = $this->cellReferenceHelper;
|
|
|
136 |
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 |
$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 |
$newReference = $this->updateCellReference($cellAddress);
|
|
|
144 |
if ($cellAddress !== $newReference) {
|
|
|
145 |
$worksheet->setBreak($newReference, $value)
|
|
|
146 |
->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 |
protected function adjustComments(Worksheet $worksheet): void
|
|
|
158 |
{
|
|
|
159 |
$aComments = $worksheet->getComments();
|
|
|
160 |
$aNewComments = []; // the new array of all comments
|
|
|
161 |
|
|
|
162 |
foreach ($aComments as $cellAddress => &$value) {
|
|
|
163 |
// Any comments inside a deleted range will be ignored
|
|
|
164 |
/** @var CellReferenceHelper */
|
|
|
165 |
$cellReferenceHelper = $this->cellReferenceHelper;
|
|
|
166 |
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === false) {
|
|
|
167 |
// Otherwise build a new array of comments indexed by the adjusted cell reference
|
|
|
168 |
$newReference = $this->updateCellReference($cellAddress);
|
|
|
169 |
$aNewComments[$newReference] = $value;
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
// Replace the comments array with the new set of comments
|
|
|
173 |
$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 |
protected function adjustHyperlinks(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void
|
|
|
184 |
{
|
|
|
185 |
$aHyperlinkCollection = $worksheet->getHyperlinkCollection();
|
|
|
186 |
($numberOfColumns > 0 || $numberOfRows > 0)
|
|
|
187 |
? uksort($aHyperlinkCollection, [self::class, 'cellReverseSort'])
|
|
|
188 |
: uksort($aHyperlinkCollection, [self::class, 'cellSort']);
|
|
|
189 |
|
|
|
190 |
foreach ($aHyperlinkCollection as $cellAddress => $value) {
|
|
|
191 |
$newReference = $this->updateCellReference($cellAddress);
|
|
|
192 |
/** @var CellReferenceHelper */
|
|
|
193 |
$cellReferenceHelper = $this->cellReferenceHelper;
|
|
|
194 |
if ($cellReferenceHelper->cellAddressInDeleteRange($cellAddress) === true) {
|
|
|
195 |
$worksheet->setHyperlink($cellAddress, null);
|
|
|
196 |
} elseif ($cellAddress !== $newReference) {
|
|
|
197 |
$worksheet->setHyperlink($cellAddress, null);
|
|
|
198 |
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 |
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
|
|
|
211 |
*/
|
|
|
212 |
protected function adjustConditionalFormatting(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void
|
|
|
213 |
{
|
|
|
214 |
$aStyles = $worksheet->getConditionalStylesCollection();
|
|
|
215 |
($numberOfColumns > 0 || $numberOfRows > 0)
|
|
|
216 |
? uksort($aStyles, [self::class, 'cellReverseSort'])
|
|
|
217 |
: uksort($aStyles, [self::class, 'cellSort']);
|
|
|
218 |
|
|
|
219 |
foreach ($aStyles as $cellAddress => $cfRules) {
|
|
|
220 |
$worksheet->removeConditionalStyles($cellAddress);
|
|
|
221 |
$newReference = $this->updateCellReference($cellAddress);
|
|
|
222 |
|
|
|
223 |
foreach ($cfRules as &$cfRule) {
|
|
|
224 |
/** @var Conditional $cfRule */
|
|
|
225 |
$conditions = $cfRule->getConditions();
|
|
|
226 |
foreach ($conditions as &$condition) {
|
|
|
227 |
if (is_string($condition)) {
|
|
|
228 |
/** @var CellReferenceHelper */
|
|
|
229 |
$cellReferenceHelper = $this->cellReferenceHelper;
|
|
|
230 |
$condition = $this->updateFormulaReferences(
|
|
|
231 |
$condition,
|
|
|
232 |
$cellReferenceHelper->beforeCellAddress(),
|
|
|
233 |
$numberOfColumns,
|
|
|
234 |
$numberOfRows,
|
|
|
235 |
$worksheet->getTitle(),
|
|
|
236 |
true
|
|
|
237 |
);
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
$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 |
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
|
|
|
252 |
*/
|
|
|
253 |
protected function adjustDataValidations(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows, string $beforeCellAddress): void
|
|
|
254 |
{
|
|
|
255 |
$aDataValidationCollection = $worksheet->getDataValidationCollection();
|
|
|
256 |
($numberOfColumns > 0 || $numberOfRows > 0)
|
|
|
257 |
? uksort($aDataValidationCollection, [self::class, 'cellReverseSort'])
|
|
|
258 |
: uksort($aDataValidationCollection, [self::class, 'cellSort']);
|
|
|
259 |
|
|
|
260 |
foreach ($aDataValidationCollection as $cellAddress => $dataValidation) {
|
|
|
261 |
$formula = $dataValidation->getFormula1();
|
|
|
262 |
if ($formula !== '') {
|
|
|
263 |
$dataValidation->setFormula1(
|
|
|
264 |
$this->updateFormulaReferences(
|
|
|
265 |
$formula,
|
|
|
266 |
$beforeCellAddress,
|
|
|
267 |
$numberOfColumns,
|
|
|
268 |
$numberOfRows,
|
|
|
269 |
$worksheet->getTitle(),
|
|
|
270 |
true
|
|
|
271 |
)
|
|
|
272 |
);
|
|
|
273 |
}
|
|
|
274 |
$formula = $dataValidation->getFormula2();
|
|
|
275 |
if ($formula !== '') {
|
|
|
276 |
$dataValidation->setFormula2(
|
|
|
277 |
$this->updateFormulaReferences(
|
|
|
278 |
$formula,
|
|
|
279 |
$beforeCellAddress,
|
|
|
280 |
$numberOfColumns,
|
|
|
281 |
$numberOfRows,
|
|
|
282 |
$worksheet->getTitle(),
|
|
|
283 |
true
|
|
|
284 |
)
|
|
|
285 |
);
|
|
|
286 |
}
|
|
|
287 |
$addressParts = explode(' ', $cellAddress);
|
|
|
288 |
$newReference = '';
|
|
|
289 |
$separator = '';
|
|
|
290 |
foreach ($addressParts as $addressPart) {
|
|
|
291 |
$newReference .= $separator . $this->updateCellReference($addressPart);
|
|
|
292 |
$separator = ' ';
|
|
|
293 |
}
|
|
|
294 |
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 |
/**
|
|
|
305 |
* Update merged cells when inserting/deleting rows/columns.
|
|
|
306 |
*
|
|
|
307 |
* @param Worksheet $worksheet The worksheet that we're editing
|
|
|
308 |
*/
|
|
|
309 |
protected function adjustMergeCells(Worksheet $worksheet): void
|
|
|
310 |
{
|
|
|
311 |
$aMergeCells = $worksheet->getMergeCells();
|
|
|
312 |
$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 |
/**
|
|
|
323 |
* Update protected cells when inserting/deleting rows/columns.
|
|
|
324 |
*
|
|
|
325 |
* @param Worksheet $worksheet The worksheet that we're editing
|
|
|
326 |
* @param int $numberOfColumns Number of columns to insert/delete (negative values indicate deletion)
|
|
|
327 |
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
|
|
|
328 |
*/
|
|
|
329 |
protected function adjustProtectedCells(Worksheet $worksheet, int $numberOfColumns, int $numberOfRows): void
|
|
|
330 |
{
|
|
|
331 |
$aProtectedCells = $worksheet->getProtectedCellRanges();
|
|
|
332 |
($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 |
}
|
|
|
343 |
}
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
/**
|
|
|
347 |
* Update column dimensions when inserting/deleting rows/columns.
|
|
|
348 |
*
|
|
|
349 |
* @param Worksheet $worksheet The worksheet that we're editing
|
|
|
350 |
*/
|
|
|
351 |
protected function adjustColumnDimensions(Worksheet $worksheet): void
|
|
|
352 |
{
|
|
|
353 |
$aColumnDimensions = array_reverse($worksheet->getColumnDimensions(), true);
|
|
|
354 |
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 |
}
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* Update row dimensions when inserting/deleting rows/columns.
|
|
|
369 |
*
|
|
|
370 |
* @param Worksheet $worksheet The worksheet that we're editing
|
|
|
371 |
* @param int $beforeRow Number of the row we're inserting/deleting before
|
|
|
372 |
* @param int $numberOfRows Number of rows to insert/delete (negative values indicate deletion)
|
|
|
373 |
*/
|
|
|
374 |
protected function adjustRowDimensions(Worksheet $worksheet, int $beforeRow, int $numberOfRows): void
|
|
|
375 |
{
|
|
|
376 |
$aRowDimensions = array_reverse($worksheet->getRowDimensions(), true);
|
|
|
377 |
if (!empty($aRowDimensions)) {
|
|
|
378 |
foreach ($aRowDimensions as $objRowDimension) {
|
|
|
379 |
$newReference = $this->updateCellReference('A' . $objRowDimension->getRowIndex());
|
|
|
380 |
[, $newReference] = Coordinate::coordinateFromString($newReference);
|
|
|
381 |
$newRoweference = (int) $newReference;
|
|
|
382 |
if ($objRowDimension->getRowIndex() !== $newRoweference) {
|
|
|
383 |
$objRowDimension->setRowIndex($newRoweference);
|
|
|
384 |
}
|
|
|
385 |
}
|
|
|
386 |
|
|
|
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 |
|
|
|
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 |
* @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 |
public function insertNewBefore(
|
|
|
409 |
string $beforeCellAddress,
|
|
|
410 |
int $numberOfColumns,
|
|
|
411 |
int $numberOfRows,
|
|
|
412 |
Worksheet $worksheet
|
|
|
413 |
): void {
|
|
|
414 |
$remove = ($numberOfColumns < 0 || $numberOfRows < 0);
|
|
|
415 |
|
|
|
416 |
if (
|
|
|
417 |
$this->cellReferenceHelper === null
|
|
|
418 |
|| $this->cellReferenceHelper->refreshRequired($beforeCellAddress, $numberOfColumns, $numberOfRows)
|
|
|
419 |
) {
|
|
|
420 |
$this->cellReferenceHelper = new CellReferenceHelper($beforeCellAddress, $numberOfColumns, $numberOfRows);
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
// Get coordinate of $beforeCellAddress
|
|
|
424 |
[$beforeColumn, $beforeRow, $beforeColumnString] = Coordinate::indexesFromString($beforeCellAddress);
|
|
|
425 |
|
|
|
426 |
// Clear cells if we are removing columns or rows
|
|
|
427 |
$highestColumn = $worksheet->getHighestColumn();
|
|
|
428 |
$highestDataColumn = $worksheet->getHighestDataColumn();
|
|
|
429 |
$highestRow = $worksheet->getHighestRow();
|
|
|
430 |
$highestDataRow = $worksheet->getHighestDataRow();
|
|
|
431 |
|
|
|
432 |
// 1. Clear column strips if we are removing columns
|
|
|
433 |
if ($numberOfColumns < 0 && $beforeColumn - 2 + $numberOfColumns > 0) {
|
|
|
434 |
$this->clearColumnStrips($highestRow, $beforeColumn, $numberOfColumns, $worksheet);
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
// 2. Clear row strips if we are removing rows
|
|
|
438 |
if ($numberOfRows < 0 && $beforeRow - 1 + $numberOfRows > 0) {
|
|
|
439 |
$this->clearRowStrips($highestColumn, $beforeColumn, $beforeRow, $numberOfRows, $worksheet);
|
|
|
440 |
}
|
|
|
441 |
|
|
|
442 |
// Find missing coordinates. This is important when inserting or deleting column before the last column
|
|
|
443 |
$startRow = $startCol = 1;
|
|
|
444 |
$startColString = 'A';
|
|
|
445 |
if ($numberOfRows === 0) {
|
|
|
446 |
$startCol = $beforeColumn;
|
|
|
447 |
$startColString = $beforeColumnString;
|
|
|
448 |
} elseif ($numberOfColumns === 0) {
|
|
|
449 |
$startRow = $beforeRow;
|
|
|
450 |
}
|
|
|
451 |
$highColumn = Coordinate::columnIndexFromString($highestDataColumn);
|
|
|
452 |
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 |
}
|
|
|
457 |
|
|
|
458 |
$allCoordinates = $worksheet->getCoordinates();
|
|
|
459 |
if ($remove) {
|
|
|
460 |
// It's faster to reverse and pop than to use unshift, especially with large cell collections
|
|
|
461 |
$allCoordinates = array_reverse($allCoordinates);
|
|
|
462 |
}
|
|
|
463 |
|
|
|
464 |
// Loop through cells, bottom-up, and change cell coordinate
|
|
|
465 |
while ($coordinate = array_pop($allCoordinates)) {
|
|
|
466 |
$cell = $worksheet->getCell($coordinate);
|
|
|
467 |
$cellIndex = Coordinate::columnIndexFromString($cell->getColumn());
|
|
|
468 |
|
|
|
469 |
// Don't update cells that are being removed
|
|
|
470 |
if ($numberOfColumns < 0 && $cellIndex >= $beforeColumn + $numberOfColumns && $cellIndex < $beforeColumn) {
|
|
|
471 |
continue;
|
|
|
472 |
}
|
|
|
473 |
|
|
|
474 |
// New coordinate
|
|
|
475 |
$newCoordinate = Coordinate::stringFromColumnIndex($cellIndex + $numberOfColumns) . ($cell->getRow() + $numberOfRows);
|
|
|
476 |
|
|
|
477 |
// Should the cell be updated? Move value and cellXf index from one cell to another.
|
|
|
478 |
if (($cellIndex >= $beforeColumn) && ($cell->getRow() >= $beforeRow)) {
|
|
|
479 |
// Update cell styles
|
|
|
480 |
$worksheet->getCell($newCoordinate)->setXfIndex($cell->getXfIndex());
|
|
|
481 |
|
|
|
482 |
// Insert this cell at its new location
|
|
|
483 |
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 |
} else {
|
|
|
488 |
// Cell value should not be adjusted
|
|
|
489 |
$worksheet->getCell($newCoordinate)->setValueExplicit($cell->getValue(), $cell->getDataType());
|
|
|
490 |
}
|
|
|
491 |
|
|
|
492 |
// Clear the original cell
|
|
|
493 |
$worksheet->getCellCollection()->delete($coordinate);
|
|
|
494 |
} else {
|
|
|
495 |
/* We don't need to update styles for rows/columns before our insertion position,
|
|
|
496 |
but we do still need to adjust any formulae in those cells */
|
|
|
497 |
if ($cell->getDataType() === DataType::TYPE_FORMULA) {
|
|
|
498 |
// Formula should be adjusted
|
|
|
499 |
$cell->setValue($this->updateFormulaReferences($cell->getValueString(), $beforeCellAddress, $numberOfColumns, $numberOfRows, $worksheet->getTitle(), true));
|
|
|
500 |
}
|
|
|
501 |
}
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
// Duplicate styles for the newly inserted cells
|
|
|
505 |
$highestColumn = $worksheet->getHighestColumn();
|
|
|
506 |
$highestRow = $worksheet->getHighestRow();
|
|
|
507 |
|
|
|
508 |
if ($numberOfColumns > 0 && $beforeColumn - 2 > 0) {
|
|
|
509 |
$this->duplicateStylesByColumn($worksheet, $beforeColumn, $beforeRow, $highestRow, $numberOfColumns);
|
|
|
510 |
}
|
|
|
511 |
|
|
|
512 |
if ($numberOfRows > 0 && $beforeRow - 1 > 0) {
|
|
|
513 |
$this->duplicateStylesByRow($worksheet, $beforeColumn, $beforeRow, $highestColumn, $numberOfRows);
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
// Update worksheet: column dimensions
|
|
|
517 |
$this->adjustColumnDimensions($worksheet);
|
|
|
518 |
|
|
|
519 |
// Update worksheet: row dimensions
|
|
|
520 |
$this->adjustRowDimensions($worksheet, $beforeRow, $numberOfRows);
|
|
|
521 |
|
|
|
522 |
// Update worksheet: page breaks
|
|
|
523 |
$this->adjustPageBreaks($worksheet, $numberOfColumns, $numberOfRows);
|
|
|
524 |
|
|
|
525 |
// Update worksheet: comments
|
|
|
526 |
$this->adjustComments($worksheet);
|
|
|
527 |
|
|
|
528 |
// Update worksheet: hyperlinks
|
|
|
529 |
$this->adjustHyperlinks($worksheet, $numberOfColumns, $numberOfRows);
|
|
|
530 |
|
|
|
531 |
// Update worksheet: conditional formatting styles
|
|
|
532 |
$this->adjustConditionalFormatting($worksheet, $numberOfColumns, $numberOfRows);
|
|
|
533 |
|
|
|
534 |
// Update worksheet: data validations
|
|
|
535 |
$this->adjustDataValidations($worksheet, $numberOfColumns, $numberOfRows, $beforeCellAddress);
|
|
|
536 |
|
|
|
537 |
// Update worksheet: merge cells
|
|
|
538 |
$this->adjustMergeCells($worksheet);
|
|
|
539 |
|
|
|
540 |
// Update worksheet: protected cells
|
|
|
541 |
$this->adjustProtectedCells($worksheet, $numberOfColumns, $numberOfRows);
|
|
|
542 |
|
|
|
543 |
// Update worksheet: autofilter
|
|
|
544 |
$this->adjustAutoFilter($worksheet, $beforeCellAddress, $numberOfColumns);
|
|
|
545 |
|
|
|
546 |
// Update worksheet: table
|
|
|
547 |
$this->adjustTable($worksheet, $beforeCellAddress, $numberOfColumns);
|
|
|
548 |
|
|
|
549 |
// Update worksheet: freeze pane
|
|
|
550 |
if ($worksheet->getFreezePane()) {
|
|
|
551 |
$splitCell = $worksheet->getFreezePane();
|
|
|
552 |
$topLeftCell = $worksheet->getTopLeftCell() ?? '';
|
|
|
553 |
|
|
|
554 |
$splitCell = $this->updateCellReference($splitCell);
|
|
|
555 |
$topLeftCell = $this->updateCellReference($topLeftCell);
|
|
|
556 |
|
|
|
557 |
$worksheet->freezePane($splitCell, $topLeftCell);
|
|
|
558 |
}
|
|
|
559 |
|
|
|
560 |
// Page setup
|
|
|
561 |
if ($worksheet->getPageSetup()->isPrintAreaSet()) {
|
|
|
562 |
$worksheet->getPageSetup()->setPrintArea(
|
|
|
563 |
$this->updateCellReference($worksheet->getPageSetup()->getPrintArea())
|
|
|
564 |
);
|
|
|
565 |
}
|
|
|
566 |
|
|
|
567 |
// 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 |
}
|
|
|
574 |
if ($objDrawing->getCoordinates2() !== '') {
|
|
|
575 |
$newReference = $this->updateCellReference($objDrawing->getCoordinates2());
|
|
|
576 |
if ($objDrawing->getCoordinates2() != $newReference) {
|
|
|
577 |
$objDrawing->setCoordinates2($newReference);
|
|
|
578 |
}
|
|
|
579 |
}
|
|
|
580 |
}
|
|
|
581 |
|
|
|
582 |
// Update workbook: define names
|
|
|
583 |
if (count($worksheet->getParentOrThrow()->getDefinedNames()) > 0) {
|
|
|
584 |
$this->updateDefinedNames($worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows);
|
|
|
585 |
}
|
|
|
586 |
|
|
|
587 |
// Garbage collect
|
|
|
588 |
$worksheet->garbageCollect();
|
|
|
589 |
}
|
|
|
590 |
|
|
|
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 |
* 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 |
*/
|
|
|
614 |
public function updateFormulaReferences(
|
|
|
615 |
string $formula = '',
|
|
|
616 |
string $beforeCellAddress = 'A1',
|
|
|
617 |
int $numberOfColumns = 0,
|
|
|
618 |
int $numberOfRows = 0,
|
|
|
619 |
string $worksheetName = '',
|
|
|
620 |
bool $includeAbsoluteReferences = false,
|
|
|
621 |
bool $onlyAbsoluteReferences = false
|
|
|
622 |
): string {
|
|
|
623 |
$callback = fn (array $matches): string => (strcasecmp(trim($matches[2], "'"), $worksheetName) === 0) ? (($matches[2][0] === "'") ? "'\u{fffc}'!" : "'\u{fffb}'!") : "'\u{fffd}'!";
|
|
|
624 |
if (
|
|
|
625 |
$this->cellReferenceHelper === null
|
|
|
626 |
|| $this->cellReferenceHelper->refreshRequired($beforeCellAddress, $numberOfColumns, $numberOfRows)
|
|
|
627 |
) {
|
|
|
628 |
$this->cellReferenceHelper = new CellReferenceHelper($beforeCellAddress, $numberOfColumns, $numberOfRows);
|
|
|
629 |
}
|
|
|
630 |
|
|
|
631 |
// Update cell references in the formula
|
|
|
632 |
$formulaBlocks = explode('"', $formula);
|
|
|
633 |
$i = false;
|
|
|
634 |
foreach ($formulaBlocks as &$formulaBlock) {
|
|
|
635 |
// Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode)
|
|
|
636 |
$i = $i === false;
|
|
|
637 |
if ($i) {
|
|
|
638 |
$adjustCount = 0;
|
|
|
639 |
$newCellTokens = $cellTokens = [];
|
|
|
640 |
// Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5)
|
|
|
641 |
$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 |
if ($matchCount > 0) {
|
|
|
644 |
foreach ($matches as $match) {
|
|
|
645 |
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}:{$match[4]}");
|
|
|
646 |
$modified3 = substr($this->updateCellReference('$A' . $match[3], $includeAbsoluteReferences, $onlyAbsoluteReferences, true), 2);
|
|
|
647 |
$modified4 = substr($this->updateCellReference('$A' . $match[4], $includeAbsoluteReferences, $onlyAbsoluteReferences, false), 2);
|
|
|
648 |
|
|
|
649 |
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 |
$cellIndex = "{$column}{$row}";
|
|
|
656 |
|
|
|
657 |
$newCellTokens[$cellIndex] = preg_quote($toString, '/');
|
|
|
658 |
$cellTokens[$cellIndex] = '/(?<!\d\$\!)' . preg_quote($fromString, '/') . '(?!\d)/i';
|
|
|
659 |
++$adjustCount;
|
|
|
660 |
}
|
|
|
661 |
}
|
|
|
662 |
}
|
|
|
663 |
}
|
|
|
664 |
// Search for column ranges (e.g. 'Sheet1'!C:E or C:E) with or without $ absolutes (e.g. $C:E)
|
|
|
665 |
$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 |
if ($matchCount > 0) {
|
|
|
668 |
foreach ($matches as $match) {
|
|
|
669 |
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}:{$match[4]}");
|
|
|
670 |
$modified3 = substr($this->updateCellReference($match[3] . '$1', $includeAbsoluteReferences, $onlyAbsoluteReferences, true), 0, -2);
|
|
|
671 |
$modified4 = substr($this->updateCellReference($match[4] . '$1', $includeAbsoluteReferences, $onlyAbsoluteReferences, false), 0, -2);
|
|
|
672 |
|
|
|
673 |
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 |
$cellIndex = "{$column}{$row}";
|
|
|
680 |
|
|
|
681 |
$newCellTokens[$cellIndex] = preg_quote($toString, '/');
|
|
|
682 |
$cellTokens[$cellIndex] = '/(?<![A-Z\$\!])' . preg_quote($fromString, '/') . '(?![A-Z])/i';
|
|
|
683 |
++$adjustCount;
|
|
|
684 |
}
|
|
|
685 |
}
|
|
|
686 |
}
|
|
|
687 |
}
|
|
|
688 |
// Search for cell ranges (e.g. 'Sheet1'!A3:C5 or A3:C5) with or without $ absolutes (e.g. $A1:C$5)
|
|
|
689 |
$formulaBlockx = ' ' . (preg_replace_callback(self::SHEETNAME_PART_WITH_SLASHES, $callback, "$formulaBlock") ?? "$formulaBlock") . ' ';
|
|
|
690 |
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_CELLRANGE . '/mui', $formulaBlockx, $matches, PREG_SET_ORDER);
|
|
|
691 |
if ($matchCount > 0) {
|
|
|
692 |
foreach ($matches as $match) {
|
|
|
693 |
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}:{$match[4]}");
|
|
|
694 |
$modified3 = $this->updateCellReference($match[3], $includeAbsoluteReferences, $onlyAbsoluteReferences, true);
|
|
|
695 |
$modified4 = $this->updateCellReference($match[4], $includeAbsoluteReferences, $onlyAbsoluteReferences, false);
|
|
|
696 |
|
|
|
697 |
if ($match[3] . $match[4] !== $modified3 . $modified4) {
|
|
|
698 |
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 |
|
|
|
706 |
$newCellTokens[$cellIndex] = preg_quote($toString, '/');
|
|
|
707 |
$cellTokens[$cellIndex] = '/(?<![A-Z]\$\!)' . preg_quote($fromString, '/') . '(?!\d)/i';
|
|
|
708 |
++$adjustCount;
|
|
|
709 |
}
|
|
|
710 |
}
|
|
|
711 |
}
|
|
|
712 |
}
|
|
|
713 |
// Search for cell references (e.g. 'Sheet1'!A3 or C5) with or without $ absolutes (e.g. $A1 or C$5)
|
|
|
714 |
|
|
|
715 |
$formulaBlockx = ' ' . (preg_replace_callback(self::SHEETNAME_PART_WITH_SLASHES, $callback, $formulaBlock) ?? $formulaBlock) . ' ';
|
|
|
716 |
$matchCount = preg_match_all('/' . self::REFHELPER_REGEXP_CELLREF . '/mui', $formulaBlockx, $matches, PREG_SET_ORDER);
|
|
|
717 |
|
|
|
718 |
if ($matchCount > 0) {
|
|
|
719 |
foreach ($matches as $match) {
|
|
|
720 |
$fromString = self::sheetnameBeforeCells($match[2], $worksheetName, "{$match[3]}");
|
|
|
721 |
|
|
|
722 |
$modified3 = $this->updateCellReference($match[3], $includeAbsoluteReferences, $onlyAbsoluteReferences, null);
|
|
|
723 |
if ($match[3] !== $modified3) {
|
|
|
724 |
if (self::matchSheetName($match[2], $worksheetName)) {
|
|
|
725 |
$toString = self::sheetnameBeforeCells($match[2], $worksheetName, "$modified3");
|
|
|
726 |
[$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 |
$row = (int) trim($row, '$') + 10000000;
|
|
|
732 |
$cellIndex = $row . $rowAdditionalIndex . $column . $columnAdditionalIndex;
|
|
|
733 |
|
|
|
734 |
$newCellTokens[$cellIndex] = preg_quote($toString, '/');
|
|
|
735 |
$cellTokens[$cellIndex] = '/(?<![A-Z\$\!])' . preg_quote($fromString, '/') . '(?!\d)/i';
|
|
|
736 |
++$adjustCount;
|
|
|
737 |
}
|
|
|
738 |
}
|
|
|
739 |
}
|
|
|
740 |
}
|
|
|
741 |
if ($adjustCount > 0) {
|
|
|
742 |
if ($numberOfColumns > 0 || $numberOfRows > 0) {
|
|
|
743 |
krsort($cellTokens);
|
|
|
744 |
krsort($newCellTokens);
|
|
|
745 |
} else {
|
|
|
746 |
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 |
}
|
|
|
753 |
unset($formulaBlock);
|
|
|
754 |
|
|
|
755 |
// Then rebuild the formula string
|
|
|
756 |
return implode('"', $formulaBlocks);
|
|
|
757 |
}
|
|
|
758 |
|
|
|
759 |
/**
|
|
|
760 |
* Update all cell references within a formula, irrespective of worksheet.
|
|
|
761 |
*/
|
|
|
762 |
public function updateFormulaReferencesAnyWorksheet(string $formula = '', int $numberOfColumns = 0, int $numberOfRows = 0): string
|
|
|
763 |
{
|
|
|
764 |
$formula = $this->updateCellReferencesAllWorksheets($formula, $numberOfColumns, $numberOfRows);
|
|
|
765 |
|
|
|
766 |
if ($numberOfColumns !== 0) {
|
|
|
767 |
$formula = $this->updateColumnRangesAllWorksheets($formula, $numberOfColumns);
|
|
|
768 |
}
|
|
|
769 |
|
|
|
770 |
if ($numberOfRows !== 0) {
|
|
|
771 |
$formula = $this->updateRowRangesAllWorksheets($formula, $numberOfRows);
|
|
|
772 |
}
|
|
|
773 |
|
|
|
774 |
return $formula;
|
|
|
775 |
}
|
|
|
776 |
|
|
|
777 |
private function updateCellReferencesAllWorksheets(string $formula, int $numberOfColumns, int $numberOfRows): string
|
|
|
778 |
{
|
|
|
779 |
$splitCount = preg_match_all(
|
|
|
780 |
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui',
|
|
|
781 |
$formula,
|
|
|
782 |
$splitRanges,
|
|
|
783 |
PREG_OFFSET_CAPTURE
|
|
|
784 |
);
|
|
|
785 |
|
|
|
786 |
$columnLengths = array_map('strlen', array_column($splitRanges[6], 0));
|
|
|
787 |
$rowLengths = array_map('strlen', array_column($splitRanges[7], 0));
|
|
|
788 |
$columnOffsets = array_column($splitRanges[6], 1);
|
|
|
789 |
$rowOffsets = array_column($splitRanges[7], 1);
|
|
|
790 |
|
|
|
791 |
$columns = $splitRanges[6];
|
|
|
792 |
$rows = $splitRanges[7];
|
|
|
793 |
|
|
|
794 |
while ($splitCount > 0) {
|
|
|
795 |
--$splitCount;
|
|
|
796 |
$columnLength = $columnLengths[$splitCount];
|
|
|
797 |
$rowLength = $rowLengths[$splitCount];
|
|
|
798 |
$columnOffset = $columnOffsets[$splitCount];
|
|
|
799 |
$rowOffset = $rowOffsets[$splitCount];
|
|
|
800 |
$column = $columns[$splitCount][0];
|
|
|
801 |
$row = $rows[$splitCount][0];
|
|
|
802 |
|
|
|
803 |
if ($column[0] !== '$') {
|
|
|
804 |
$column = ((Coordinate::columnIndexFromString($column) + $numberOfColumns) % AddressRange::MAX_COLUMN_INT) ?: AddressRange::MAX_COLUMN_INT;
|
|
|
805 |
$column = Coordinate::stringFromColumnIndex($column);
|
|
|
806 |
$rowOffset -= ($columnLength - strlen($column));
|
|
|
807 |
$formula = substr($formula, 0, $columnOffset) . $column . substr($formula, $columnOffset + $columnLength);
|
|
|
808 |
}
|
|
|
809 |
if (!empty($row) && $row[0] !== '$') {
|
|
|
810 |
$row = (((int) $row + $numberOfRows) % AddressRange::MAX_ROW) ?: AddressRange::MAX_ROW;
|
|
|
811 |
$formula = substr($formula, 0, $rowOffset) . $row . substr($formula, $rowOffset + $rowLength);
|
|
|
812 |
}
|
|
|
813 |
}
|
|
|
814 |
|
|
|
815 |
return $formula;
|
|
|
816 |
}
|
|
|
817 |
|
|
|
818 |
private function updateColumnRangesAllWorksheets(string $formula, int $numberOfColumns): string
|
|
|
819 |
{
|
|
|
820 |
$splitCount = preg_match_all(
|
|
|
821 |
'/' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '/mui',
|
|
|
822 |
$formula,
|
|
|
823 |
$splitRanges,
|
|
|
824 |
PREG_OFFSET_CAPTURE
|
|
|
825 |
);
|
|
|
826 |
|
|
|
827 |
$fromColumnLengths = array_map('strlen', array_column($splitRanges[1], 0));
|
|
|
828 |
$fromColumnOffsets = array_column($splitRanges[1], 1);
|
|
|
829 |
$toColumnLengths = array_map('strlen', array_column($splitRanges[2], 0));
|
|
|
830 |
$toColumnOffsets = array_column($splitRanges[2], 1);
|
|
|
831 |
|
|
|
832 |
$fromColumns = $splitRanges[1];
|
|
|
833 |
$toColumns = $splitRanges[2];
|
|
|
834 |
|
|
|
835 |
while ($splitCount > 0) {
|
|
|
836 |
--$splitCount;
|
|
|
837 |
$fromColumnLength = $fromColumnLengths[$splitCount];
|
|
|
838 |
$toColumnLength = $toColumnLengths[$splitCount];
|
|
|
839 |
$fromColumnOffset = $fromColumnOffsets[$splitCount];
|
|
|
840 |
$toColumnOffset = $toColumnOffsets[$splitCount];
|
|
|
841 |
$fromColumn = $fromColumns[$splitCount][0];
|
|
|
842 |
$toColumn = $toColumns[$splitCount][0];
|
|
|
843 |
|
|
|
844 |
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 |
}
|
|
|
848 |
if (!empty($toColumn) && $toColumn[0] !== '$') {
|
|
|
849 |
$toColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($toColumn) + $numberOfColumns);
|
|
|
850 |
$formula = substr($formula, 0, $toColumnOffset) . $toColumn . substr($formula, $toColumnOffset + $toColumnLength);
|
|
|
851 |
}
|
|
|
852 |
}
|
|
|
853 |
|
|
|
854 |
return $formula;
|
|
|
855 |
}
|
|
|
856 |
|
|
|
857 |
private function updateRowRangesAllWorksheets(string $formula, int $numberOfRows): string
|
|
|
858 |
{
|
|
|
859 |
$splitCount = preg_match_all(
|
|
|
860 |
'/' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '/mui',
|
|
|
861 |
$formula,
|
|
|
862 |
$splitRanges,
|
|
|
863 |
PREG_OFFSET_CAPTURE
|
|
|
864 |
);
|
|
|
865 |
|
|
|
866 |
$fromRowLengths = array_map('strlen', array_column($splitRanges[1], 0));
|
|
|
867 |
$fromRowOffsets = array_column($splitRanges[1], 1);
|
|
|
868 |
$toRowLengths = array_map('strlen', array_column($splitRanges[2], 0));
|
|
|
869 |
$toRowOffsets = array_column($splitRanges[2], 1);
|
|
|
870 |
|
|
|
871 |
$fromRows = $splitRanges[1];
|
|
|
872 |
$toRows = $splitRanges[2];
|
|
|
873 |
|
|
|
874 |
while ($splitCount > 0) {
|
|
|
875 |
--$splitCount;
|
|
|
876 |
$fromRowLength = $fromRowLengths[$splitCount];
|
|
|
877 |
$toRowLength = $toRowLengths[$splitCount];
|
|
|
878 |
$fromRowOffset = $fromRowOffsets[$splitCount];
|
|
|
879 |
$toRowOffset = $toRowOffsets[$splitCount];
|
|
|
880 |
$fromRow = $fromRows[$splitCount][0];
|
|
|
881 |
$toRow = $toRows[$splitCount][0];
|
|
|
882 |
|
|
|
883 |
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 |
return $formula;
|
|
|
894 |
}
|
|
|
895 |
|
|
|
896 |
/**
|
|
|
897 |
* Update cell reference.
|
|
|
898 |
*
|
|
|
899 |
* @param string $cellReference Cell address or range of addresses
|
|
|
900 |
*
|
|
|
901 |
* @return string Updated cell range
|
|
|
902 |
*/
|
|
|
903 |
private function updateCellReference(string $cellReference = 'A1', bool $includeAbsoluteReferences = false, bool $onlyAbsoluteReferences = false, ?bool $topLeft = null)
|
|
|
904 |
{
|
|
|
905 |
// Is it in another worksheet? Will not have to update anything.
|
|
|
906 |
if (str_contains($cellReference, '!')) {
|
|
|
907 |
return $cellReference;
|
|
|
908 |
}
|
|
|
909 |
// 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 |
return $this->updateCellRange($cellReference, $includeAbsoluteReferences, $onlyAbsoluteReferences);
|
|
|
920 |
}
|
|
|
921 |
|
|
|
922 |
/**
|
|
|
923 |
* Update named formulae (i.e. containing worksheet references / named ranges).
|
|
|
924 |
*
|
|
|
925 |
* @param Spreadsheet $spreadsheet Object to update
|
|
|
926 |
* @param string $oldName Old name (name to replace)
|
|
|
927 |
* @param string $newName New name
|
|
|
928 |
*/
|
|
|
929 |
public function updateNamedFormulae(Spreadsheet $spreadsheet, string $oldName = '', string $newName = ''): void
|
|
|
930 |
{
|
|
|
931 |
if ($oldName == '') {
|
|
|
932 |
return;
|
|
|
933 |
}
|
|
|
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 |
if (str_contains($formula, $oldName)) {
|
|
|
941 |
$formula = str_replace("'" . $oldName . "'!", "'" . $newName . "'!", $formula);
|
|
|
942 |
$formula = str_replace($oldName . '!', $newName . '!', $formula);
|
|
|
943 |
$cell->setValueExplicit($formula, DataType::TYPE_FORMULA);
|
|
|
944 |
}
|
|
|
945 |
}
|
|
|
946 |
}
|
|
|
947 |
}
|
|
|
948 |
}
|
|
|
949 |
|
|
|
950 |
private function updateDefinedNames(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void
|
|
|
951 |
{
|
|
|
952 |
foreach ($worksheet->getParentOrThrow()->getDefinedNames() as $definedName) {
|
|
|
953 |
if ($definedName->isFormula() === false) {
|
|
|
954 |
$this->updateNamedRange($definedName, $worksheet, $beforeCellAddress, $numberOfColumns, $numberOfRows);
|
|
|
955 |
} 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 |
$cellAddress = $definedName->getValue();
|
|
|
964 |
$asFormula = ($cellAddress[0] === '=');
|
|
|
965 |
if ($definedName->getWorksheet() !== null && $definedName->getWorksheet()->getHashInt() === $worksheet->getHashInt()) {
|
|
|
966 |
/**
|
|
|
967 |
* 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 |
*/
|
|
|
973 |
if ($asFormula === true) {
|
|
|
974 |
$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 |
private function updateNamedFormula(DefinedName $definedName, Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns, int $numberOfRows): void
|
|
|
983 |
{
|
|
|
984 |
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 |
}
|
|
|
996 |
}
|
|
|
997 |
|
|
|
998 |
/**
|
|
|
999 |
* Update cell range.
|
|
|
1000 |
*
|
|
|
1001 |
* @param string $cellRange Cell range (e.g. 'B2:D4', 'B:C' or '2:3')
|
|
|
1002 |
*
|
|
|
1003 |
* @return string Updated cell range
|
|
|
1004 |
*/
|
|
|
1005 |
private function updateCellRange(string $cellRange = 'A1:A1', bool $includeAbsoluteReferences = false, bool $onlyAbsoluteReferences = false): string
|
|
|
1006 |
{
|
|
|
1007 |
if (!Coordinate::coordinateIsRange($cellRange)) {
|
|
|
1008 |
throw new Exception('Only cell ranges may be passed to this method.');
|
|
|
1009 |
}
|
|
|
1010 |
|
|
|
1011 |
// Update range
|
|
|
1012 |
$range = Coordinate::splitRange($cellRange);
|
|
|
1013 |
$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 |
$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 |
$range[$i][$j] = Coordinate::coordinateFromString(
|
|
|
1025 |
$cellReferenceHelper->updateCellReference('A' . $range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences, null)
|
|
|
1026 |
)[1];
|
|
|
1027 |
} else {
|
|
|
1028 |
$range[$i][$j] = $cellReferenceHelper->updateCellReference($range[$i][$j], $includeAbsoluteReferences, $onlyAbsoluteReferences, null);
|
|
|
1029 |
}
|
|
|
1030 |
}
|
|
|
1031 |
}
|
|
|
1032 |
|
|
|
1033 |
// Recreate range string
|
|
|
1034 |
return Coordinate::buildRange($range);
|
|
|
1035 |
}
|
|
|
1036 |
|
|
|
1037 |
private function clearColumnStrips(int $highestRow, int $beforeColumn, int $numberOfColumns, Worksheet $worksheet): void
|
|
|
1038 |
{
|
|
|
1039 |
$startColumnId = Coordinate::stringFromColumnIndex($beforeColumn + $numberOfColumns);
|
|
|
1040 |
$endColumnId = Coordinate::stringFromColumnIndex($beforeColumn);
|
|
|
1041 |
|
|
|
1042 |
for ($row = 1; $row <= $highestRow - 1; ++$row) {
|
|
|
1043 |
for ($column = $startColumnId; $column !== $endColumnId; ++$column) {
|
|
|
1044 |
$coordinate = $column . $row;
|
|
|
1045 |
$this->clearStripCell($worksheet, $coordinate);
|
|
|
1046 |
}
|
|
|
1047 |
}
|
|
|
1048 |
}
|
|
|
1049 |
|
|
|
1050 |
private function clearRowStrips(string $highestColumn, int $beforeColumn, int $beforeRow, int $numberOfRows, Worksheet $worksheet): void
|
|
|
1051 |
{
|
|
|
1052 |
$startColumnId = Coordinate::stringFromColumnIndex($beforeColumn);
|
|
|
1053 |
++$highestColumn;
|
|
|
1054 |
|
|
|
1055 |
for ($column = $startColumnId; $column !== $highestColumn; ++$column) {
|
|
|
1056 |
for ($row = $beforeRow + $numberOfRows; $row <= $beforeRow - 1; ++$row) {
|
|
|
1057 |
$coordinate = $column . $row;
|
|
|
1058 |
$this->clearStripCell($worksheet, $coordinate);
|
|
|
1059 |
}
|
|
|
1060 |
}
|
|
|
1061 |
}
|
|
|
1062 |
|
|
|
1063 |
private function clearStripCell(Worksheet $worksheet, string $coordinate): void
|
|
|
1064 |
{
|
|
|
1065 |
$worksheet->removeConditionalStyles($coordinate);
|
|
|
1066 |
$worksheet->setHyperlink($coordinate);
|
|
|
1067 |
$worksheet->setDataValidation($coordinate);
|
|
|
1068 |
$worksheet->removeComment($coordinate);
|
|
|
1069 |
|
|
|
1070 |
if ($worksheet->cellExists($coordinate)) {
|
|
|
1071 |
$worksheet->getCell($coordinate)->setValueExplicit(null, DataType::TYPE_NULL);
|
|
|
1072 |
$worksheet->getCell($coordinate)->setXfIndex(0);
|
|
|
1073 |
}
|
|
|
1074 |
}
|
|
|
1075 |
|
|
|
1076 |
private function adjustAutoFilter(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns): void
|
|
|
1077 |
{
|
|
|
1078 |
$autoFilter = $worksheet->getAutoFilter();
|
|
|
1079 |
$autoFilterRange = $autoFilter->getRange();
|
|
|
1080 |
if (!empty($autoFilterRange)) {
|
|
|
1081 |
if ($numberOfColumns !== 0) {
|
|
|
1082 |
$autoFilterColumns = $autoFilter->getColumns();
|
|
|
1083 |
if (count($autoFilterColumns) > 0) {
|
|
|
1084 |
$column = '';
|
|
|
1085 |
$row = 0;
|
|
|
1086 |
sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row);
|
|
|
1087 |
$columnIndex = Coordinate::columnIndexFromString((string) $column);
|
|
|
1088 |
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($autoFilterRange);
|
|
|
1089 |
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 |
// Shuffle columns in autofilter range
|
|
|
1096 |
if ($numberOfColumns > 0) {
|
|
|
1097 |
$this->adjustAutoFilterInsert($startCol, $numberOfColumns, $rangeEnd[0], $autoFilter);
|
|
|
1098 |
} else {
|
|
|
1099 |
$this->adjustAutoFilterDelete($startCol, $numberOfColumns, $rangeEnd[0], $autoFilter);
|
|
|
1100 |
}
|
|
|
1101 |
}
|
|
|
1102 |
}
|
|
|
1103 |
}
|
|
|
1104 |
|
|
|
1105 |
$worksheet->setAutoFilter(
|
|
|
1106 |
$this->updateCellReference($autoFilterRange)
|
|
|
1107 |
);
|
|
|
1108 |
}
|
|
|
1109 |
}
|
|
|
1110 |
|
|
|
1111 |
private function adjustAutoFilterDeleteRules(int $columnIndex, int $numberOfColumns, array $autoFilterColumns, AutoFilter $autoFilter): void
|
|
|
1112 |
{
|
|
|
1113 |
// 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 |
|
|
|
1118 |
for ($i = 1; $i <= $deleteCount; ++$i) {
|
|
|
1119 |
$columnName = Coordinate::stringFromColumnIndex($deleteColumn + 1);
|
|
|
1120 |
if (isset($autoFilterColumns[$columnName])) {
|
|
|
1121 |
$autoFilter->clearColumn($columnName);
|
|
|
1122 |
}
|
|
|
1123 |
++$deleteColumn;
|
|
|
1124 |
}
|
|
|
1125 |
}
|
|
|
1126 |
|
|
|
1127 |
private function adjustAutoFilterInsert(int $startCol, int $numberOfColumns, int $rangeEnd, AutoFilter $autoFilter): void
|
|
|
1128 |
{
|
|
|
1129 |
$startColRef = $startCol;
|
|
|
1130 |
$endColRef = $rangeEnd;
|
|
|
1131 |
$toColRef = $rangeEnd + $numberOfColumns;
|
|
|
1132 |
|
|
|
1133 |
do {
|
|
|
1134 |
$autoFilter->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef));
|
|
|
1135 |
--$endColRef;
|
|
|
1136 |
--$toColRef;
|
|
|
1137 |
} while ($startColRef <= $endColRef);
|
|
|
1138 |
}
|
|
|
1139 |
|
|
|
1140 |
private function adjustAutoFilterDelete(int $startCol, int $numberOfColumns, int $rangeEnd, AutoFilter $autoFilter): void
|
|
|
1141 |
{
|
|
|
1142 |
// For delete, we shuffle from beginning to end to avoid overwriting
|
|
|
1143 |
$startColID = Coordinate::stringFromColumnIndex($startCol);
|
|
|
1144 |
$toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns);
|
|
|
1145 |
$endColID = Coordinate::stringFromColumnIndex($rangeEnd + 1);
|
|
|
1146 |
|
|
|
1147 |
do {
|
|
|
1148 |
$autoFilter->shiftColumn($startColID, $toColID);
|
|
|
1149 |
++$toColID;
|
|
|
1150 |
++$startColID; // this confuses phpstan into thinking startColID is int/float
|
|
|
1151 |
} while ($startColID !== $endColID); // @phpstan-ignore-line
|
|
|
1152 |
}
|
|
|
1153 |
|
|
|
1154 |
private function adjustTable(Worksheet $worksheet, string $beforeCellAddress, int $numberOfColumns): void
|
|
|
1155 |
{
|
|
|
1156 |
$tableCollection = $worksheet->getTableCollection();
|
|
|
1157 |
|
|
|
1158 |
foreach ($tableCollection as $table) {
|
|
|
1159 |
$tableRange = $table->getRange();
|
|
|
1160 |
if (!empty($tableRange)) {
|
|
|
1161 |
if ($numberOfColumns !== 0) {
|
|
|
1162 |
$tableColumns = $table->getColumns();
|
|
|
1163 |
if (count($tableColumns) > 0) {
|
|
|
1164 |
$column = '';
|
|
|
1165 |
$row = 0;
|
|
|
1166 |
sscanf($beforeCellAddress, '%[A-Z]%d', $column, $row);
|
|
|
1167 |
$columnIndex = Coordinate::columnIndexFromString((string) $column);
|
|
|
1168 |
[$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($tableRange);
|
|
|
1169 |
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 |
// 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 |
}
|
|
|
1181 |
}
|
|
|
1182 |
}
|
|
|
1183 |
}
|
|
|
1184 |
|
|
|
1185 |
$table->setRange($this->updateCellReference($tableRange));
|
|
|
1186 |
}
|
|
|
1187 |
}
|
|
|
1188 |
}
|
|
|
1189 |
|
|
|
1190 |
private function adjustTableDeleteRules(int $columnIndex, int $numberOfColumns, array $tableColumns, Table $table): void
|
|
|
1191 |
{
|
|
|
1192 |
// 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 |
|
|
|
1197 |
for ($i = 1; $i <= $deleteCount; ++$i) {
|
|
|
1198 |
$columnName = Coordinate::stringFromColumnIndex($deleteColumn + 1);
|
|
|
1199 |
if (isset($tableColumns[$columnName])) {
|
|
|
1200 |
$table->clearColumn($columnName);
|
|
|
1201 |
}
|
|
|
1202 |
++$deleteColumn;
|
|
|
1203 |
}
|
|
|
1204 |
}
|
|
|
1205 |
|
|
|
1206 |
private function adjustTableInsert(int $startCol, int $numberOfColumns, int $rangeEnd, Table $table): void
|
|
|
1207 |
{
|
|
|
1208 |
$startColRef = $startCol;
|
|
|
1209 |
$endColRef = $rangeEnd;
|
|
|
1210 |
$toColRef = $rangeEnd + $numberOfColumns;
|
|
|
1211 |
|
|
|
1212 |
do {
|
|
|
1213 |
$table->shiftColumn(Coordinate::stringFromColumnIndex($endColRef), Coordinate::stringFromColumnIndex($toColRef));
|
|
|
1214 |
--$endColRef;
|
|
|
1215 |
--$toColRef;
|
|
|
1216 |
} while ($startColRef <= $endColRef);
|
|
|
1217 |
}
|
|
|
1218 |
|
|
|
1219 |
private function adjustTableDelete(int $startCol, int $numberOfColumns, int $rangeEnd, Table $table): void
|
|
|
1220 |
{
|
|
|
1221 |
// For delete, we shuffle from beginning to end to avoid overwriting
|
|
|
1222 |
$startColID = Coordinate::stringFromColumnIndex($startCol);
|
|
|
1223 |
$toColID = Coordinate::stringFromColumnIndex($startCol + $numberOfColumns);
|
|
|
1224 |
$endColID = Coordinate::stringFromColumnIndex($rangeEnd + 1);
|
|
|
1225 |
|
|
|
1226 |
do {
|
|
|
1227 |
$table->shiftColumn($startColID, $toColID);
|
|
|
1228 |
++$toColID;
|
|
|
1229 |
++$startColID; // this confuses phpstan into thinking startColID is int/float
|
|
|
1230 |
} while ($startColID !== $endColID); // @phpstan-ignore-line
|
|
|
1231 |
}
|
|
|
1232 |
|
|
|
1233 |
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 |
$xfIndex = $worksheet->getCell($coordinate)->getXfIndex();
|
|
|
1241 |
for ($j = $beforeColumn; $j <= $beforeColumn - 1 + $numberOfColumns; ++$j) {
|
|
|
1242 |
if (!empty($xfIndex) || $worksheet->cellExists([$j, $i])) {
|
|
|
1243 |
$worksheet->getCell([$j, $i])->setXfIndex($xfIndex);
|
|
|
1244 |
}
|
|
|
1245 |
}
|
|
|
1246 |
}
|
|
|
1247 |
}
|
|
|
1248 |
}
|
|
|
1249 |
|
|
|
1250 |
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 |
$worksheet->getCell(Coordinate::stringFromColumnIndex($i) . $j)->setXfIndex($xfIndex);
|
|
|
1261 |
}
|
|
|
1262 |
}
|
|
|
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 |
}
|