Total Complexity | 526 |
Total Lines | 3777 |
Duplicated Lines | 0 % |
Coverage | 94.14% |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like Worksheet often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Worksheet, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Worksheet |
||
36 | { |
||
37 | // Break types |
||
38 | public const BREAK_NONE = 0; |
||
39 | public const BREAK_ROW = 1; |
||
40 | public const BREAK_COLUMN = 2; |
||
41 | // Maximum column for row break |
||
42 | public const BREAK_ROW_MAX_COLUMN = 16383; |
||
43 | |||
44 | // Sheet state |
||
45 | public const SHEETSTATE_VISIBLE = 'visible'; |
||
46 | public const SHEETSTATE_HIDDEN = 'hidden'; |
||
47 | public const SHEETSTATE_VERYHIDDEN = 'veryHidden'; |
||
48 | |||
49 | public const MERGE_CELL_CONTENT_EMPTY = 'empty'; |
||
50 | public const MERGE_CELL_CONTENT_HIDE = 'hide'; |
||
51 | public const MERGE_CELL_CONTENT_MERGE = 'merge'; |
||
52 | |||
53 | public const FUNCTION_LIKE_GROUPBY = '/\b(groupby|_xleta)\b/i'; // weird new syntax |
||
54 | |||
55 | protected const SHEET_NAME_REQUIRES_NO_QUOTES = '/^[_\p{L}][_\p{L}\p{N}]*$/mui'; |
||
56 | |||
57 | /** |
||
58 | * Maximum 31 characters allowed for sheet title. |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | const SHEET_TITLE_MAXIMUM_LENGTH = 31; |
||
63 | |||
64 | /** |
||
65 | * Invalid characters in sheet title. |
||
66 | */ |
||
67 | private const INVALID_CHARACTERS = ['*', ':', '/', '\\', '?', '[', ']']; |
||
68 | |||
69 | /** |
||
70 | * Parent spreadsheet. |
||
71 | */ |
||
72 | private ?Spreadsheet $parent = null; |
||
73 | |||
74 | /** |
||
75 | * Collection of cells. |
||
76 | */ |
||
77 | private Cells $cellCollection; |
||
78 | |||
79 | private bool $cellCollectionInitialized = true; |
||
80 | |||
81 | /** |
||
82 | * Collection of row dimensions. |
||
83 | * |
||
84 | * @var RowDimension[] |
||
85 | */ |
||
86 | private array $rowDimensions = []; |
||
87 | |||
88 | /** |
||
89 | * Default row dimension. |
||
90 | */ |
||
91 | private RowDimension $defaultRowDimension; |
||
92 | |||
93 | /** |
||
94 | * Collection of column dimensions. |
||
95 | * |
||
96 | * @var ColumnDimension[] |
||
97 | */ |
||
98 | private array $columnDimensions = []; |
||
99 | |||
100 | /** |
||
101 | * Default column dimension. |
||
102 | */ |
||
103 | private ColumnDimension $defaultColumnDimension; |
||
104 | |||
105 | /** |
||
106 | * Collection of drawings. |
||
107 | * |
||
108 | * @var ArrayObject<int, BaseDrawing> |
||
109 | */ |
||
110 | private ArrayObject $drawingCollection; |
||
111 | |||
112 | /** |
||
113 | * Collection of Chart objects. |
||
114 | * |
||
115 | * @var ArrayObject<int, Chart> |
||
116 | */ |
||
117 | private ArrayObject $chartCollection; |
||
118 | |||
119 | /** |
||
120 | * Collection of Table objects. |
||
121 | * |
||
122 | * @var ArrayObject<int, Table> |
||
123 | */ |
||
124 | private ArrayObject $tableCollection; |
||
125 | |||
126 | /** |
||
127 | * Worksheet title. |
||
128 | */ |
||
129 | private string $title = ''; |
||
130 | |||
131 | /** |
||
132 | * Sheet state. |
||
133 | */ |
||
134 | private string $sheetState; |
||
135 | |||
136 | /** |
||
137 | * Page setup. |
||
138 | */ |
||
139 | private PageSetup $pageSetup; |
||
140 | |||
141 | /** |
||
142 | * Page margins. |
||
143 | */ |
||
144 | private PageMargins $pageMargins; |
||
145 | |||
146 | /** |
||
147 | * Page header/footer. |
||
148 | */ |
||
149 | private HeaderFooter $headerFooter; |
||
150 | |||
151 | /** |
||
152 | * Sheet view. |
||
153 | */ |
||
154 | private SheetView $sheetView; |
||
155 | |||
156 | /** |
||
157 | * Protection. |
||
158 | */ |
||
159 | private Protection $protection; |
||
160 | |||
161 | /** |
||
162 | * Conditional styles. Indexed by cell coordinate, e.g. 'A1'. |
||
163 | */ |
||
164 | private array $conditionalStylesCollection = []; |
||
165 | |||
166 | /** |
||
167 | * Collection of row breaks. |
||
168 | * |
||
169 | * @var PageBreak[] |
||
170 | */ |
||
171 | private array $rowBreaks = []; |
||
172 | |||
173 | /** |
||
174 | * Collection of column breaks. |
||
175 | * |
||
176 | * @var PageBreak[] |
||
177 | */ |
||
178 | private array $columnBreaks = []; |
||
179 | |||
180 | /** |
||
181 | * Collection of merged cell ranges. |
||
182 | * |
||
183 | * @var string[] |
||
184 | */ |
||
185 | private array $mergeCells = []; |
||
186 | |||
187 | /** |
||
188 | * Collection of protected cell ranges. |
||
189 | * |
||
190 | * @var ProtectedRange[] |
||
191 | */ |
||
192 | private array $protectedCells = []; |
||
193 | |||
194 | /** |
||
195 | * Autofilter Range and selection. |
||
196 | */ |
||
197 | private AutoFilter $autoFilter; |
||
198 | |||
199 | /** |
||
200 | * Freeze pane. |
||
201 | */ |
||
202 | private ?string $freezePane = null; |
||
203 | |||
204 | /** |
||
205 | * Default position of the right bottom pane. |
||
206 | */ |
||
207 | private ?string $topLeftCell = null; |
||
208 | |||
209 | private string $paneTopLeftCell = ''; |
||
210 | |||
211 | private string $activePane = ''; |
||
212 | |||
213 | private int $xSplit = 0; |
||
214 | |||
215 | private int $ySplit = 0; |
||
216 | |||
217 | private string $paneState = ''; |
||
218 | |||
219 | /** |
||
220 | * Properties of the 4 panes. |
||
221 | * |
||
222 | * @var (null|Pane)[] |
||
223 | */ |
||
224 | private array $panes = [ |
||
225 | 'bottomRight' => null, |
||
226 | 'bottomLeft' => null, |
||
227 | 'topRight' => null, |
||
228 | 'topLeft' => null, |
||
229 | ]; |
||
230 | |||
231 | /** |
||
232 | * Show gridlines? |
||
233 | */ |
||
234 | private bool $showGridlines = true; |
||
235 | |||
236 | /** |
||
237 | * Print gridlines? |
||
238 | */ |
||
239 | private bool $printGridlines = false; |
||
240 | |||
241 | /** |
||
242 | * Show row and column headers? |
||
243 | */ |
||
244 | private bool $showRowColHeaders = true; |
||
245 | |||
246 | /** |
||
247 | * Show summary below? (Row/Column outline). |
||
248 | */ |
||
249 | private bool $showSummaryBelow = true; |
||
250 | |||
251 | /** |
||
252 | * Show summary right? (Row/Column outline). |
||
253 | */ |
||
254 | private bool $showSummaryRight = true; |
||
255 | |||
256 | /** |
||
257 | * Collection of comments. |
||
258 | * |
||
259 | * @var Comment[] |
||
260 | */ |
||
261 | private array $comments = []; |
||
262 | |||
263 | /** |
||
264 | * Active cell. (Only one!). |
||
265 | */ |
||
266 | private string $activeCell = 'A1'; |
||
267 | |||
268 | /** |
||
269 | * Selected cells. |
||
270 | */ |
||
271 | private string $selectedCells = 'A1'; |
||
272 | |||
273 | /** |
||
274 | * Cached highest column. |
||
275 | */ |
||
276 | private int $cachedHighestColumn = 1; |
||
277 | |||
278 | /** |
||
279 | * Cached highest row. |
||
280 | */ |
||
281 | private int $cachedHighestRow = 1; |
||
282 | |||
283 | /** |
||
284 | * Right-to-left? |
||
285 | */ |
||
286 | private bool $rightToLeft = false; |
||
287 | |||
288 | /** |
||
289 | * Hyperlinks. Indexed by cell coordinate, e.g. 'A1'. |
||
290 | */ |
||
291 | private array $hyperlinkCollection = []; |
||
292 | |||
293 | /** |
||
294 | * Data validation objects. Indexed by cell coordinate, e.g. 'A1'. |
||
295 | * Index can include ranges, and multiple cells/ranges. |
||
296 | */ |
||
297 | private array $dataValidationCollection = []; |
||
298 | |||
299 | /** |
||
300 | * Tab color. |
||
301 | */ |
||
302 | private ?Color $tabColor = null; |
||
303 | |||
304 | /** |
||
305 | * Hash. |
||
306 | */ |
||
307 | private int $hash; |
||
308 | |||
309 | /** |
||
310 | * CodeName. |
||
311 | */ |
||
312 | private ?string $codeName = null; |
||
313 | |||
314 | /** |
||
315 | * Create a new worksheet. |
||
316 | 10473 | */ |
|
317 | public function __construct(?Spreadsheet $parent = null, string $title = 'Worksheet') |
||
318 | { |
||
319 | 10473 | // Set parent and title |
|
320 | 10473 | $this->parent = $parent; |
|
321 | 10473 | $this->hash = spl_object_id($this); |
|
322 | $this->setTitle($title, false); |
||
323 | 10473 | // setTitle can change $pTitle |
|
324 | 10473 | $this->setCodeName($this->getTitle()); |
|
325 | $this->setSheetState(self::SHEETSTATE_VISIBLE); |
||
326 | 10473 | ||
327 | $this->cellCollection = CellsFactory::getInstance($this); |
||
328 | 10473 | // Set page setup |
|
329 | $this->pageSetup = new PageSetup(); |
||
330 | 10473 | // Set page margins |
|
331 | $this->pageMargins = new PageMargins(); |
||
332 | 10473 | // Set page header/footer |
|
333 | $this->headerFooter = new HeaderFooter(); |
||
334 | 10473 | // Set sheet view |
|
335 | $this->sheetView = new SheetView(); |
||
336 | 10473 | // Drawing collection |
|
337 | $this->drawingCollection = new ArrayObject(); |
||
338 | 10473 | // Chart collection |
|
339 | $this->chartCollection = new ArrayObject(); |
||
340 | 10473 | // Protection |
|
341 | $this->protection = new Protection(); |
||
342 | 10473 | // Default row dimension |
|
343 | $this->defaultRowDimension = new RowDimension(null); |
||
344 | 10473 | // Default column dimension |
|
345 | $this->defaultColumnDimension = new ColumnDimension(null); |
||
346 | 10473 | // AutoFilter |
|
347 | $this->autoFilter = new AutoFilter('', $this); |
||
348 | 10473 | // Table collection |
|
349 | $this->tableCollection = new ArrayObject(); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Disconnect all cells from this Worksheet object, |
||
354 | * typically so that the worksheet object can be unset. |
||
355 | 9096 | */ |
|
356 | public function disconnectCells(): void |
||
357 | 9096 | { |
|
358 | 9096 | if ($this->cellCollectionInitialized) { |
|
359 | 9096 | $this->cellCollection->unsetWorksheetCells(); |
|
360 | 9096 | unset($this->cellCollection); |
|
361 | $this->cellCollectionInitialized = false; |
||
362 | } |
||
363 | 9096 | // detach ourself from the workbook, so that it can then delete this worksheet successfully |
|
364 | $this->parent = null; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Code to execute when this worksheet is unset(). |
||
369 | 122 | */ |
|
370 | public function __destruct() |
||
371 | 122 | { |
|
372 | Calculation::getInstance($this->parent)->clearCalculationCacheForWorksheet($this->title); |
||
373 | 122 | ||
374 | 122 | $this->disconnectCells(); |
|
375 | unset($this->rowDimensions, $this->columnDimensions, $this->tableCollection, $this->drawingCollection, $this->chartCollection, $this->autoFilter); |
||
376 | } |
||
377 | 5 | ||
378 | public function __wakeup(): void |
||
379 | 5 | { |
|
380 | $this->hash = spl_object_id($this); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Return the cell collection. |
||
385 | 10131 | */ |
|
386 | public function getCellCollection(): Cells |
||
387 | 10131 | { |
|
388 | return $this->cellCollection; |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Get array of invalid characters for sheet title. |
||
393 | 1 | */ |
|
394 | public static function getInvalidCharacters(): array |
||
395 | 1 | { |
|
396 | return self::INVALID_CHARACTERS; |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Check sheet code name for valid Excel syntax. |
||
401 | * |
||
402 | * @param string $sheetCodeName The string to check |
||
403 | * |
||
404 | * @return string The valid string |
||
405 | 10473 | */ |
|
406 | private static function checkSheetCodeName(string $sheetCodeName): string |
||
407 | 10473 | { |
|
408 | 10473 | $charCount = StringHelper::countCharacters($sheetCodeName); |
|
409 | 1 | if ($charCount == 0) { |
|
410 | throw new Exception('Sheet code name cannot be empty.'); |
||
411 | } |
||
412 | // Some of the printable ASCII characters are invalid: * : / \ ? [ ] and first and last characters cannot be a "'" |
||
413 | 10473 | if ( |
|
414 | 10473 | (str_replace(self::INVALID_CHARACTERS, '', $sheetCodeName) !== $sheetCodeName) |
|
415 | 10473 | || (StringHelper::substring($sheetCodeName, -1, 1) == '\'') |
|
416 | || (StringHelper::substring($sheetCodeName, 0, 1) == '\'') |
||
417 | 1 | ) { |
|
418 | throw new Exception('Invalid character found in sheet code name'); |
||
419 | } |
||
420 | |||
421 | 10473 | // Enforce maximum characters allowed for sheet title |
|
422 | 1 | if ($charCount > self::SHEET_TITLE_MAXIMUM_LENGTH) { |
|
423 | throw new Exception('Maximum ' . self::SHEET_TITLE_MAXIMUM_LENGTH . ' characters allowed in sheet code name.'); |
||
424 | } |
||
425 | 10473 | ||
426 | return $sheetCodeName; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Check sheet title for valid Excel syntax. |
||
431 | * |
||
432 | * @param string $sheetTitle The string to check |
||
433 | * |
||
434 | * @return string The valid string |
||
435 | 10473 | */ |
|
436 | private static function checkSheetTitle(string $sheetTitle): string |
||
437 | { |
||
438 | 10473 | // Some of the printable ASCII characters are invalid: * : / \ ? [ ] |
|
439 | 2 | if (str_replace(self::INVALID_CHARACTERS, '', $sheetTitle) !== $sheetTitle) { |
|
440 | throw new Exception('Invalid character found in sheet title'); |
||
441 | } |
||
442 | |||
443 | 10473 | // Enforce maximum characters allowed for sheet title |
|
444 | 3 | if (StringHelper::countCharacters($sheetTitle) > self::SHEET_TITLE_MAXIMUM_LENGTH) { |
|
445 | throw new Exception('Maximum ' . self::SHEET_TITLE_MAXIMUM_LENGTH . ' characters allowed in sheet title.'); |
||
446 | } |
||
447 | 10473 | ||
448 | return $sheetTitle; |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Get a sorted list of all cell coordinates currently held in the collection by row and column. |
||
453 | * |
||
454 | * @param bool $sorted Also sort the cell collection? |
||
455 | * |
||
456 | * @return string[] |
||
457 | 1385 | */ |
|
458 | public function getCoordinates(bool $sorted = true): array |
||
459 | 1385 | { |
|
460 | 1 | if ($this->cellCollectionInitialized === false) { |
|
461 | return []; |
||
462 | } |
||
463 | 1385 | ||
464 | 492 | if ($sorted) { |
|
465 | return $this->cellCollection->getSortedCoordinates(); |
||
466 | } |
||
467 | 1292 | ||
468 | return $this->cellCollection->getCoordinates(); |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Get collection of row dimensions. |
||
473 | * |
||
474 | * @return RowDimension[] |
||
475 | 1110 | */ |
|
476 | public function getRowDimensions(): array |
||
477 | 1110 | { |
|
478 | return $this->rowDimensions; |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * Get default row dimension. |
||
483 | 1059 | */ |
|
484 | public function getDefaultRowDimension(): RowDimension |
||
485 | 1059 | { |
|
486 | return $this->defaultRowDimension; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Get collection of column dimensions. |
||
491 | * |
||
492 | * @return ColumnDimension[] |
||
493 | 1116 | */ |
|
494 | public function getColumnDimensions(): array |
||
495 | { |
||
496 | 1116 | /** @var callable $callable */ |
|
497 | 1116 | $callable = [self::class, 'columnDimensionCompare']; |
|
498 | uasort($this->columnDimensions, $callable); |
||
499 | 1116 | ||
500 | return $this->columnDimensions; |
||
501 | } |
||
502 | 82 | ||
503 | private static function columnDimensionCompare(ColumnDimension $a, ColumnDimension $b): int |
||
504 | 82 | { |
|
505 | return $a->getColumnNumeric() - $b->getColumnNumeric(); |
||
506 | } |
||
507 | |||
508 | /** |
||
509 | * Get default column dimension. |
||
510 | 518 | */ |
|
511 | public function getDefaultColumnDimension(): ColumnDimension |
||
512 | 518 | { |
|
513 | return $this->defaultColumnDimension; |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * Get collection of drawings. |
||
518 | * |
||
519 | * @return ArrayObject<int, BaseDrawing> |
||
520 | 1089 | */ |
|
521 | public function getDrawingCollection(): ArrayObject |
||
522 | 1089 | { |
|
523 | return $this->drawingCollection; |
||
524 | } |
||
525 | |||
526 | /** |
||
527 | * Get collection of charts. |
||
528 | * |
||
529 | * @return ArrayObject<int, Chart> |
||
530 | 99 | */ |
|
531 | public function getChartCollection(): ArrayObject |
||
532 | 99 | { |
|
533 | return $this->chartCollection; |
||
534 | } |
||
535 | 105 | ||
536 | public function addChart(Chart $chart): Chart |
||
537 | 105 | { |
|
538 | 105 | $chart->setWorksheet($this); |
|
539 | $this->chartCollection[] = $chart; |
||
540 | 105 | ||
541 | return $chart; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Return the count of charts on this worksheet. |
||
546 | * |
||
547 | * @return int The number of charts |
||
548 | 82 | */ |
|
549 | public function getChartCount(): int |
||
550 | 82 | { |
|
551 | return count($this->chartCollection); |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Get a chart by its index position. |
||
556 | * |
||
557 | * @param ?string $index Chart index position |
||
558 | * |
||
559 | * @return Chart|false |
||
560 | 77 | */ |
|
561 | public function getChartByIndex(?string $index) |
||
562 | 77 | { |
|
563 | 77 | $chartCount = count($this->chartCollection); |
|
564 | if ($chartCount == 0) { |
||
565 | return false; |
||
566 | 77 | } |
|
567 | if ($index === null) { |
||
568 | $index = --$chartCount; |
||
569 | 77 | } |
|
570 | if (!isset($this->chartCollection[$index])) { |
||
571 | return false; |
||
572 | } |
||
573 | 77 | ||
574 | return $this->chartCollection[$index]; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Return an array of the names of charts on this worksheet. |
||
579 | * |
||
580 | * @return string[] The names of charts |
||
581 | 5 | */ |
|
582 | public function getChartNames(): array |
||
583 | 5 | { |
|
584 | 5 | $chartNames = []; |
|
585 | 5 | foreach ($this->chartCollection as $chart) { |
|
586 | $chartNames[] = $chart->getName(); |
||
587 | } |
||
588 | 5 | ||
589 | return $chartNames; |
||
590 | } |
||
591 | |||
592 | /** |
||
593 | * Get a chart by name. |
||
594 | * |
||
595 | * @param string $chartName Chart name |
||
596 | * |
||
597 | * @return Chart|false |
||
598 | 6 | */ |
|
599 | public function getChartByName(string $chartName) |
||
600 | 6 | { |
|
601 | 6 | foreach ($this->chartCollection as $index => $chart) { |
|
602 | 6 | if ($chart->getName() == $chartName) { |
|
603 | return $chart; |
||
604 | } |
||
605 | } |
||
606 | 1 | ||
607 | return false; |
||
608 | } |
||
609 | 6 | ||
610 | public function getChartByNameOrThrow(string $chartName): Chart |
||
611 | 6 | { |
|
612 | 6 | $chart = $this->getChartByName($chartName); |
|
613 | 6 | if ($chart !== false) { |
|
614 | return $chart; |
||
615 | } |
||
616 | 1 | ||
617 | throw new Exception("Sheet does not have a chart named $chartName."); |
||
618 | } |
||
619 | |||
620 | /** |
||
621 | * Refresh column dimensions. |
||
622 | * |
||
623 | * @return $this |
||
624 | 25 | */ |
|
625 | public function refreshColumnDimensions(): static |
||
626 | 25 | { |
|
627 | 25 | $newColumnDimensions = []; |
|
628 | 25 | foreach ($this->getColumnDimensions() as $objColumnDimension) { |
|
629 | $newColumnDimensions[$objColumnDimension->getColumnIndex()] = $objColumnDimension; |
||
630 | } |
||
631 | 25 | ||
632 | $this->columnDimensions = $newColumnDimensions; |
||
633 | 25 | ||
634 | return $this; |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Refresh row dimensions. |
||
639 | * |
||
640 | * @return $this |
||
641 | 7 | */ |
|
642 | public function refreshRowDimensions(): static |
||
643 | 7 | { |
|
644 | 7 | $newRowDimensions = []; |
|
645 | 7 | foreach ($this->getRowDimensions() as $objRowDimension) { |
|
646 | $newRowDimensions[$objRowDimension->getRowIndex()] = $objRowDimension; |
||
647 | } |
||
648 | 7 | ||
649 | $this->rowDimensions = $newRowDimensions; |
||
650 | 7 | ||
651 | return $this; |
||
652 | } |
||
653 | |||
654 | /** |
||
655 | * Calculate worksheet dimension. |
||
656 | * |
||
657 | * @return string String containing the dimension of this worksheet |
||
658 | 428 | */ |
|
659 | public function calculateWorksheetDimension(): string |
||
660 | { |
||
661 | 428 | // Return |
|
662 | return 'A1:' . $this->getHighestColumn() . $this->getHighestRow(); |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Calculate worksheet data dimension. |
||
667 | * |
||
668 | * @return string String containing the dimension of this worksheet that actually contain data |
||
669 | 539 | */ |
|
670 | public function calculateWorksheetDataDimension(): string |
||
671 | { |
||
672 | 539 | // Return |
|
673 | return 'A1:' . $this->getHighestDataColumn() . $this->getHighestDataRow(); |
||
674 | } |
||
675 | |||
676 | /** |
||
677 | * Calculate widths for auto-size columns. |
||
678 | * |
||
679 | * @return $this |
||
680 | 748 | */ |
|
681 | public function calculateColumnWidths(): static |
||
682 | 748 | { |
|
683 | 748 | $activeSheet = $this->getParent()?->getActiveSheetIndex(); |
|
684 | $selectedCells = $this->selectedCells; |
||
685 | 748 | // initialize $autoSizes array |
|
686 | 748 | $autoSizes = []; |
|
687 | 141 | foreach ($this->getColumnDimensions() as $colDimension) { |
|
688 | 58 | if ($colDimension->getAutoSize()) { |
|
689 | $autoSizes[$colDimension->getColumnIndex()] = -1; |
||
690 | } |
||
691 | } |
||
692 | |||
693 | 748 | // There is only something to do if there are some auto-size columns |
|
694 | 58 | if (!empty($autoSizes)) { |
|
695 | $holdActivePane = $this->activePane; |
||
696 | 58 | // build list of cells references that participate in a merge |
|
697 | 58 | $isMergeCell = []; |
|
698 | 16 | foreach ($this->getMergeCells() as $cells) { |
|
699 | 16 | foreach (Coordinate::extractAllCellReferencesInRange($cells) as $cellReference) { |
|
700 | $isMergeCell[$cellReference] = true; |
||
701 | } |
||
702 | } |
||
703 | 58 | ||
704 | $autoFilterIndentRanges = (new AutoFit($this))->getAutoFilterIndentRanges(); |
||
705 | |||
706 | 58 | // loop through all cells in the worksheet |
|
707 | 58 | foreach ($this->getCoordinates(false) as $coordinate) { |
|
708 | $cell = $this->getCellOrNull($coordinate); |
||
709 | 58 | ||
710 | if ($cell !== null && isset($autoSizes[$this->cellCollection->getCurrentColumn()])) { |
||
711 | 58 | //Determine if cell is in merge range |
|
712 | $isMerged = isset($isMergeCell[$this->cellCollection->getCurrentCoordinate()]); |
||
713 | |||
714 | 58 | //By default merged cells should be ignored |
|
715 | $isMergedButProceed = false; |
||
716 | |||
717 | 58 | //The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide) |
|
718 | if ($isMerged && $cell->isMergeRangeValueCell()) { |
||
719 | $range = (string) $cell->getMergeRange(); |
||
720 | $rangeBoundaries = Coordinate::rangeDimension($range); |
||
721 | if ($rangeBoundaries[0] === 1) { |
||
722 | $isMergedButProceed = true; |
||
723 | } |
||
724 | } |
||
725 | |||
726 | 58 | // Determine width if cell is not part of a merge or does and is a value cell of 1-column wide range |
|
727 | if (!$isMerged || $isMergedButProceed) { |
||
728 | // Determine if we need to make an adjustment for the first row in an AutoFilter range that |
||
729 | 58 | // has a column filter dropdown |
|
730 | 58 | $filterAdjustment = false; |
|
731 | 4 | if (!empty($autoFilterIndentRanges)) { |
|
732 | 4 | foreach ($autoFilterIndentRanges as $autoFilterFirstRowRange) { |
|
733 | 4 | if ($cell->isInRange($autoFilterFirstRowRange)) { |
|
734 | $filterAdjustment = true; |
||
735 | 4 | ||
736 | break; |
||
737 | } |
||
738 | } |
||
739 | } |
||
740 | 58 | ||
741 | 58 | $indentAdjustment = $cell->getStyle()->getAlignment()->getIndent(); |
|
742 | $indentAdjustment += (int) ($cell->getStyle()->getAlignment()->getHorizontal() === Alignment::HORIZONTAL_CENTER); |
||
743 | |||
744 | // Calculated value |
||
745 | 58 | // To formatted string |
|
746 | 58 | $cellValue = NumberFormat::toFormattedString( |
|
747 | 58 | $cell->getCalculatedValueString(), |
|
748 | 58 | (string) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()) |
|
749 | 58 | ->getNumberFormat()->getFormatCode(true) |
|
750 | ); |
||
751 | 58 | ||
752 | 58 | if ($cellValue !== null && $cellValue !== '') { |
|
753 | 58 | $autoSizes[$this->cellCollection->getCurrentColumn()] = max( |
|
754 | 58 | $autoSizes[$this->cellCollection->getCurrentColumn()], |
|
755 | 58 | round( |
|
756 | 58 | Shared\Font::calculateColumnWidth( |
|
757 | 58 | $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getFont(), |
|
758 | 58 | $cellValue, |
|
759 | 58 | (int) $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()) |
|
760 | 58 | ->getAlignment()->getTextRotation(), |
|
761 | 58 | $this->getParentOrThrow()->getDefaultStyle()->getFont(), |
|
762 | 58 | $filterAdjustment, |
|
763 | 58 | $indentAdjustment |
|
764 | 58 | ), |
|
765 | 58 | 3 |
|
766 | 58 | ) |
|
767 | ); |
||
768 | } |
||
769 | } |
||
770 | } |
||
771 | } |
||
772 | |||
773 | 58 | // adjust column widths |
|
774 | 58 | foreach ($autoSizes as $columnIndex => $width) { |
|
775 | if ($width == -1) { |
||
776 | $width = $this->getDefaultColumnDimension()->getWidth(); |
||
777 | 58 | } |
|
778 | $this->getColumnDimension($columnIndex)->setWidth($width); |
||
779 | 58 | } |
|
780 | $this->activePane = $holdActivePane; |
||
781 | 748 | } |
|
782 | 748 | if ($activeSheet !== null && $activeSheet >= 0) { |
|
783 | $this->getParent()?->setActiveSheetIndex($activeSheet); |
||
784 | 748 | } |
|
785 | $this->setSelectedCells($selectedCells); |
||
786 | 748 | ||
787 | return $this; |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * Get parent or null. |
||
792 | 10150 | */ |
|
793 | public function getParent(): ?Spreadsheet |
||
794 | 10150 | { |
|
795 | return $this->parent; |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * Get parent, throw exception if null. |
||
800 | 10182 | */ |
|
801 | public function getParentOrThrow(): Spreadsheet |
||
802 | 10182 | { |
|
803 | 10181 | if ($this->parent !== null) { |
|
804 | return $this->parent; |
||
805 | } |
||
806 | 1 | ||
807 | throw new Exception('Sheet does not have a parent.'); |
||
808 | } |
||
809 | |||
810 | /** |
||
811 | * Re-bind parent. |
||
812 | * |
||
813 | * @return $this |
||
814 | 54 | */ |
|
815 | public function rebindParent(Spreadsheet $parent): static |
||
816 | 54 | { |
|
817 | 4 | if ($this->parent !== null) { |
|
818 | 4 | $definedNames = $this->parent->getDefinedNames(); |
|
819 | foreach ($definedNames as $definedName) { |
||
820 | $parent->addDefinedName($definedName); |
||
821 | } |
||
822 | 4 | ||
823 | 4 | $this->parent->removeSheetByIndex( |
|
824 | 4 | $this->parent->getIndex($this) |
|
825 | ); |
||
826 | 54 | } |
|
827 | $this->parent = $parent; |
||
828 | 54 | ||
829 | return $this; |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * Get title. |
||
834 | 10474 | */ |
|
835 | public function getTitle(): string |
||
836 | 10474 | { |
|
837 | return $this->title; |
||
838 | } |
||
839 | |||
840 | /** |
||
841 | * Set title. |
||
842 | * |
||
843 | * @param string $title String containing the dimension of this worksheet |
||
844 | * @param bool $updateFormulaCellReferences Flag indicating whether cell references in formulae should |
||
845 | * be updated to reflect the new sheet name. |
||
846 | * This should be left as the default true, unless you are |
||
847 | * certain that no formula cells on any worksheet contain |
||
848 | * references to this worksheet |
||
849 | * @param bool $validate False to skip validation of new title. WARNING: This should only be set |
||
850 | * at parse time (by Readers), where titles can be assumed to be valid. |
||
851 | * |
||
852 | * @return $this |
||
853 | 10473 | */ |
|
854 | public function setTitle(string $title, bool $updateFormulaCellReferences = true, bool $validate = true): static |
||
855 | { |
||
856 | 10473 | // Is this a 'rename' or not? |
|
857 | 269 | if ($this->getTitle() == $title) { |
|
858 | return $this; |
||
859 | } |
||
860 | |||
861 | 10473 | // Old title |
|
862 | $oldTitle = $this->getTitle(); |
||
863 | 10473 | ||
864 | if ($validate) { |
||
865 | 10473 | // Syntax check |
|
866 | self::checkSheetTitle($title); |
||
867 | 10473 | ||
868 | if ($this->parent && $this->parent->getIndex($this, true) >= 0) { |
||
869 | 792 | // Is there already such sheet name? |
|
870 | if ($this->parent->sheetNameExists($title)) { |
||
871 | // Use name, but append with lowest possible integer |
||
872 | 2 | ||
873 | if (StringHelper::countCharacters($title) > 29) { |
||
874 | $title = StringHelper::substring($title, 0, 29); |
||
875 | 2 | } |
|
876 | 2 | $i = 1; |
|
877 | 1 | while ($this->parent->sheetNameExists($title . ' ' . $i)) { |
|
878 | 1 | ++$i; |
|
879 | if ($i == 10) { |
||
880 | if (StringHelper::countCharacters($title) > 28) { |
||
881 | $title = StringHelper::substring($title, 0, 28); |
||
882 | 1 | } |
|
883 | } elseif ($i == 100) { |
||
884 | if (StringHelper::countCharacters($title) > 27) { |
||
885 | $title = StringHelper::substring($title, 0, 27); |
||
886 | } |
||
887 | } |
||
888 | } |
||
889 | 2 | ||
890 | $title .= " $i"; |
||
891 | } |
||
892 | } |
||
893 | } |
||
894 | |||
895 | 10473 | // Set title |
|
896 | $this->title = $title; |
||
897 | 10473 | ||
898 | if ($this->parent && $this->parent->getIndex($this, true) >= 0 && $this->parent->getCalculationEngine()) { |
||
899 | 1375 | // New title |
|
900 | 1375 | $newTitle = $this->getTitle(); |
|
901 | 1375 | $this->parent->getCalculationEngine() |
|
902 | 1375 | ->renameCalculationCacheForWorksheet($oldTitle, $newTitle); |
|
903 | 792 | if ($updateFormulaCellReferences) { |
|
904 | ReferenceHelper::getInstance()->updateNamedFormulae($this->parent, $oldTitle, $newTitle); |
||
905 | } |
||
906 | } |
||
907 | 10473 | ||
908 | return $this; |
||
909 | } |
||
910 | |||
911 | /** |
||
912 | * Get sheet state. |
||
913 | * |
||
914 | * @return string Sheet state (visible, hidden, veryHidden) |
||
915 | 474 | */ |
|
916 | public function getSheetState(): string |
||
919 | } |
||
920 | |||
921 | /** |
||
922 | * Set sheet state. |
||
923 | * |
||
924 | * @param string $value Sheet state (visible, hidden, veryHidden) |
||
925 | * |
||
926 | * @return $this |
||
927 | 10473 | */ |
|
928 | public function setSheetState(string $value): static |
||
929 | 10473 | { |
|
930 | $this->sheetState = $value; |
||
931 | 10473 | ||
932 | return $this; |
||
933 | } |
||
934 | |||
935 | /** |
||
936 | * Get page setup. |
||
937 | 1497 | */ |
|
938 | public function getPageSetup(): PageSetup |
||
939 | 1497 | { |
|
940 | return $this->pageSetup; |
||
941 | } |
||
942 | |||
943 | /** |
||
944 | * Set page setup. |
||
945 | * |
||
946 | * @return $this |
||
947 | 1 | */ |
|
948 | public function setPageSetup(PageSetup $pageSetup): static |
||
949 | 1 | { |
|
950 | $this->pageSetup = $pageSetup; |
||
951 | 1 | ||
952 | return $this; |
||
953 | } |
||
954 | |||
955 | /** |
||
956 | * Get page margins. |
||
957 | 1510 | */ |
|
958 | public function getPageMargins(): PageMargins |
||
959 | 1510 | { |
|
960 | return $this->pageMargins; |
||
961 | } |
||
962 | |||
963 | /** |
||
964 | * Set page margins. |
||
965 | * |
||
966 | * @return $this |
||
967 | 1 | */ |
|
968 | public function setPageMargins(PageMargins $pageMargins): static |
||
973 | } |
||
974 | |||
975 | /** |
||
976 | * Get page header/footer. |
||
977 | 525 | */ |
|
978 | public function getHeaderFooter(): HeaderFooter |
||
979 | 525 | { |
|
980 | return $this->headerFooter; |
||
981 | } |
||
982 | |||
983 | /** |
||
984 | * Set page header/footer. |
||
985 | * |
||
986 | * @return $this |
||
987 | 1 | */ |
|
988 | public function setHeaderFooter(HeaderFooter $headerFooter): static |
||
989 | 1 | { |
|
990 | $this->headerFooter = $headerFooter; |
||
991 | 1 | ||
992 | return $this; |
||
993 | } |
||
994 | |||
995 | /** |
||
996 | * Get sheet view. |
||
997 | 551 | */ |
|
998 | public function getSheetView(): SheetView |
||
999 | 551 | { |
|
1000 | return $this->sheetView; |
||
1001 | } |
||
1002 | |||
1003 | /** |
||
1004 | * Set sheet view. |
||
1005 | * |
||
1006 | * @return $this |
||
1007 | 1 | */ |
|
1008 | public function setSheetView(SheetView $sheetView): static |
||
1009 | 1 | { |
|
1010 | $this->sheetView = $sheetView; |
||
1011 | 1 | ||
1012 | return $this; |
||
1013 | } |
||
1014 | |||
1015 | /** |
||
1016 | * Get Protection. |
||
1017 | 576 | */ |
|
1018 | public function getProtection(): Protection |
||
1019 | 576 | { |
|
1020 | return $this->protection; |
||
1021 | } |
||
1022 | |||
1023 | /** |
||
1024 | * Set Protection. |
||
1025 | * |
||
1026 | * @return $this |
||
1027 | 1 | */ |
|
1028 | public function setProtection(Protection $protection): static |
||
1029 | 1 | { |
|
1030 | $this->protection = $protection; |
||
1031 | 1 | ||
1032 | return $this; |
||
1033 | } |
||
1034 | |||
1035 | /** |
||
1036 | * Get highest worksheet column. |
||
1037 | * |
||
1038 | * @param null|int|string $row Return the data highest column for the specified row, |
||
1039 | * or the highest column of any row if no row number is passed |
||
1040 | * |
||
1041 | * @return string Highest column name |
||
1042 | 1401 | */ |
|
1043 | public function getHighestColumn($row = null): string |
||
1044 | 1401 | { |
|
1045 | 1400 | if ($row === null) { |
|
1046 | return Coordinate::stringFromColumnIndex($this->cachedHighestColumn); |
||
1047 | } |
||
1048 | 1 | ||
1049 | return $this->getHighestDataColumn($row); |
||
1050 | } |
||
1051 | |||
1052 | /** |
||
1053 | * Get highest worksheet column that contains data. |
||
1054 | * |
||
1055 | * @param null|int|string $row Return the highest data column for the specified row, |
||
1056 | * or the highest data column of any row if no row number is passed |
||
1057 | * |
||
1058 | * @return string Highest column name that contains data |
||
1059 | 668 | */ |
|
1060 | public function getHighestDataColumn($row = null): string |
||
1061 | 668 | { |
|
1062 | return $this->cellCollection->getHighestColumn($row); |
||
1063 | } |
||
1064 | |||
1065 | /** |
||
1066 | * Get highest worksheet row. |
||
1067 | * |
||
1068 | * @param null|string $column Return the highest data row for the specified column, |
||
1069 | * or the highest row of any column if no column letter is passed |
||
1070 | * |
||
1071 | * @return int Highest row number |
||
1072 | 899 | */ |
|
1073 | public function getHighestRow(?string $column = null): int |
||
1080 | } |
||
1081 | |||
1082 | /** |
||
1083 | * Get highest worksheet row that contains data. |
||
1084 | * |
||
1085 | * @param null|string $column Return the highest data row for the specified column, |
||
1086 | * or the highest data row of any column if no column letter is passed |
||
1087 | * |
||
1088 | * @return int Highest row number that contains data |
||
1089 | 667 | */ |
|
1090 | public function getHighestDataRow(?string $column = null): int |
||
1093 | } |
||
1094 | |||
1095 | /** |
||
1096 | * Get highest worksheet column and highest row that have cell records. |
||
1097 | * |
||
1098 | * @return array Highest column name and highest row number |
||
1099 | 1 | */ |
|
1100 | public function getHighestRowAndColumn(): array |
||
1101 | 1 | { |
|
1102 | return $this->cellCollection->getHighestRowAndColumn(); |
||
1103 | } |
||
1104 | |||
1105 | /** |
||
1106 | * Set a cell value. |
||
1107 | * |
||
1108 | * @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
||
1109 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
1110 | * @param mixed $value Value for the cell |
||
1111 | * @param null|IValueBinder $binder Value Binder to override the currently set Value Binder |
||
1112 | * |
||
1113 | * @return $this |
||
1114 | 4671 | */ |
|
1115 | public function setCellValue(CellAddress|string|array $coordinate, mixed $value, ?IValueBinder $binder = null): static |
||
1116 | 4671 | { |
|
1117 | 4671 | $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1118 | $this->getCell($cellAddress)->setValue($value, $binder); |
||
1119 | 4671 | ||
1120 | return $this; |
||
1121 | } |
||
1122 | |||
1123 | /** |
||
1124 | * Set a cell value. |
||
1125 | * |
||
1126 | * @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
||
1127 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
1128 | * @param mixed $value Value of the cell |
||
1129 | * @param string $dataType Explicit data type, see DataType::TYPE_* |
||
1130 | * Note that PhpSpreadsheet does not validate that the value and datatype are consistent, in using this |
||
1131 | * method, then it is your responsibility as an end-user developer to validate that the value and |
||
1132 | * the datatype match. |
||
1133 | * If you do mismatch value and datatpe, then the value you enter may be changed to match the datatype |
||
1134 | * that you specify. |
||
1135 | * |
||
1136 | * @see DataType |
||
1137 | * |
||
1138 | * @return $this |
||
1139 | 104 | */ |
|
1140 | public function setCellValueExplicit(CellAddress|string|array $coordinate, mixed $value, string $dataType): static |
||
1141 | 104 | { |
|
1142 | 104 | $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1143 | $this->getCell($cellAddress)->setValueExplicit($value, $dataType); |
||
1144 | 104 | ||
1145 | return $this; |
||
1146 | } |
||
1147 | |||
1148 | /** |
||
1149 | * Get cell at a specific coordinate. |
||
1150 | * |
||
1151 | * @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
||
1152 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
1153 | * |
||
1154 | * @return Cell Cell that was found or created |
||
1155 | * WARNING: Because the cell collection can be cached to reduce memory, it only allows one |
||
1156 | * "active" cell at a time in memory. If you assign that cell to a variable, then select |
||
1157 | * another cell using getCell() or any of its variants, the newly selected cell becomes |
||
1158 | * the "active" cell, and any previous assignment becomes a disconnected reference because |
||
1159 | * the active cell has changed. |
||
1160 | 10090 | */ |
|
1161 | public function getCell(CellAddress|string|array $coordinate): Cell |
||
1162 | 10090 | { |
|
1163 | $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
||
1164 | |||
1165 | 10090 | // Shortcut for increased performance for the vast majority of simple cases |
|
1166 | if ($this->cellCollection->has($cellAddress)) { |
||
1167 | 10069 | /** @var Cell $cell */ |
|
1168 | $cell = $this->cellCollection->get($cellAddress); |
||
1169 | 10069 | ||
1170 | return $cell; |
||
1171 | } |
||
1172 | |||
1173 | 10089 | /** @var Worksheet $sheet */ |
|
1174 | 10089 | [$sheet, $finalCoordinate] = $this->getWorksheetAndCoordinate($cellAddress); |
|
1175 | $cell = $sheet->getCellCollection()->get($finalCoordinate); |
||
1176 | 10089 | ||
1177 | return $cell ?? $sheet->createNewCell($finalCoordinate); |
||
1178 | } |
||
1179 | |||
1180 | /** |
||
1181 | * Get the correct Worksheet and coordinate from a coordinate that may |
||
1182 | * contains reference to another sheet or a named range. |
||
1183 | * |
||
1184 | * @return array{0: Worksheet, 1: string} |
||
1185 | 10092 | */ |
|
1186 | private function getWorksheetAndCoordinate(string $coordinate): array |
||
1187 | 10092 | { |
|
1188 | 10092 | $sheet = null; |
|
1189 | $finalCoordinate = null; |
||
1190 | |||
1191 | 10092 | // Worksheet reference? |
|
1192 | if (str_contains($coordinate, '!')) { |
||
1193 | $worksheetReference = self::extractSheetTitle($coordinate, true, true); |
||
1194 | |||
1195 | $sheet = $this->getParentOrThrow()->getSheetByName($worksheetReference[0]); |
||
1196 | $finalCoordinate = strtoupper($worksheetReference[1]); |
||
1197 | |||
1198 | if ($sheet === null) { |
||
1199 | throw new Exception('Sheet not found for name: ' . $worksheetReference[0]); |
||
1200 | } |
||
1201 | 10092 | } elseif ( |
|
1202 | 10092 | !preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $coordinate) |
|
1203 | && preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/iu', $coordinate) |
||
1204 | ) { |
||
1205 | 16 | // Named range? |
|
1206 | 16 | $namedRange = $this->validateNamedRange($coordinate, true); |
|
1207 | 11 | if ($namedRange !== null) { |
|
1208 | 11 | $sheet = $namedRange->getWorksheet(); |
|
1209 | if ($sheet === null) { |
||
1210 | throw new Exception('Sheet not found for named range: ' . $namedRange->getName()); |
||
1211 | } |
||
1212 | |||
1213 | 11 | /** @phpstan-ignore-next-line */ |
|
1214 | 11 | $cellCoordinate = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!'); |
|
1215 | $finalCoordinate = str_replace('$', '', $cellCoordinate); |
||
1216 | } |
||
1217 | } |
||
1218 | 10092 | ||
1219 | 10092 | if ($sheet === null || $finalCoordinate === null) { |
|
1220 | 10092 | $sheet = $this; |
|
1221 | $finalCoordinate = strtoupper($coordinate); |
||
1222 | } |
||
1223 | 10092 | ||
1224 | 2 | if (Coordinate::coordinateIsRange($finalCoordinate)) { |
|
1225 | 10092 | throw new Exception('Cell coordinate string can not be a range of cells.'); |
|
1226 | } |
||
1227 | $finalCoordinate = str_replace('$', '', $finalCoordinate); |
||
1228 | |||
1229 | 10092 | return [$sheet, $finalCoordinate]; |
|
1230 | } |
||
1231 | |||
1232 | /** |
||
1233 | * Get an existing cell at a specific coordinate, or null. |
||
1234 | * |
||
1235 | * @param string $coordinate Coordinate of the cell, eg: 'A1' |
||
1236 | * |
||
1237 | * @return null|Cell Cell that was found or null |
||
1238 | */ |
||
1239 | 58 | private function getCellOrNull(string $coordinate): ?Cell |
|
1240 | { |
||
1241 | // Check cell collection |
||
1242 | 58 | if ($this->cellCollection->has($coordinate)) { |
|
1243 | 58 | return $this->cellCollection->get($coordinate); |
|
1244 | } |
||
1245 | |||
1246 | return null; |
||
1247 | } |
||
1248 | |||
1249 | /** |
||
1250 | * Create a new cell at the specified coordinate. |
||
1251 | * |
||
1252 | * @param string $coordinate Coordinate of the cell |
||
1253 | * |
||
1254 | * @return Cell Cell that was created |
||
1255 | * WARNING: Because the cell collection can be cached to reduce memory, it only allows one |
||
1256 | * "active" cell at a time in memory. If you assign that cell to a variable, then select |
||
1257 | * another cell using getCell() or any of its variants, the newly selected cell becomes |
||
1258 | * the "active" cell, and any previous assignment becomes a disconnected reference because |
||
1259 | * the active cell has changed. |
||
1260 | */ |
||
1261 | 10089 | public function createNewCell(string $coordinate): Cell |
|
1262 | { |
||
1263 | 10089 | [$column, $row, $columnString] = Coordinate::indexesFromString($coordinate); |
|
1264 | 10089 | $cell = new Cell(null, DataType::TYPE_NULL, $this); |
|
1265 | 10089 | $this->cellCollection->add($coordinate, $cell); |
|
1266 | |||
1267 | // Coordinates |
||
1268 | 10089 | if ($column > $this->cachedHighestColumn) { |
|
1269 | 6920 | $this->cachedHighestColumn = $column; |
|
1270 | } |
||
1271 | 10089 | if ($row > $this->cachedHighestRow) { |
|
1272 | 8482 | $this->cachedHighestRow = $row; |
|
1273 | } |
||
1274 | |||
1275 | // Cell needs appropriate xfIndex from dimensions records |
||
1276 | // but don't create dimension records if they don't already exist |
||
1277 | 10089 | $rowDimension = $this->rowDimensions[$row] ?? null; |
|
1278 | 10089 | $columnDimension = $this->columnDimensions[$columnString] ?? null; |
|
1279 | |||
1280 | 10089 | $xfSet = false; |
|
1281 | 10089 | if ($rowDimension !== null) { |
|
1282 | 389 | $rowXf = (int) $rowDimension->getXfIndex(); |
|
1283 | 389 | if ($rowXf > 0) { |
|
1284 | // then there is a row dimension with explicit style, assign it to the cell |
||
1285 | 203 | $cell->setXfIndex($rowXf); |
|
1286 | 203 | $xfSet = true; |
|
1287 | } |
||
1288 | } |
||
1289 | 10089 | if (!$xfSet && $columnDimension !== null) { |
|
1290 | 538 | $colXf = (int) $columnDimension->getXfIndex(); |
|
1291 | 538 | if ($colXf > 0) { |
|
1292 | // then there is a column dimension, assign it to the cell |
||
1293 | 215 | $cell->setXfIndex($colXf); |
|
1294 | } |
||
1295 | } |
||
1296 | |||
1297 | 10089 | return $cell; |
|
1298 | } |
||
1299 | |||
1300 | /** |
||
1301 | * Does the cell at a specific coordinate exist? |
||
1302 | * |
||
1303 | * @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
||
1304 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
1305 | */ |
||
1306 | 10012 | public function cellExists(CellAddress|string|array $coordinate): bool |
|
1307 | { |
||
1308 | 10012 | $cellAddress = Validations::validateCellAddress($coordinate); |
|
1309 | 10012 | [$sheet, $finalCoordinate] = $this->getWorksheetAndCoordinate($cellAddress); |
|
1310 | |||
1311 | 10012 | return $sheet->getCellCollection()->has($finalCoordinate); |
|
1312 | } |
||
1313 | |||
1314 | /** |
||
1315 | * Get row dimension at a specific row. |
||
1316 | * |
||
1317 | * @param int $row Numeric index of the row |
||
1318 | */ |
||
1319 | 546 | public function getRowDimension(int $row): RowDimension |
|
1320 | { |
||
1321 | // Get row dimension |
||
1322 | 546 | if (!isset($this->rowDimensions[$row])) { |
|
1323 | 546 | $this->rowDimensions[$row] = new RowDimension($row); |
|
1324 | |||
1325 | 546 | $this->cachedHighestRow = max($this->cachedHighestRow, $row); |
|
1326 | } |
||
1327 | |||
1328 | 546 | return $this->rowDimensions[$row]; |
|
1329 | } |
||
1330 | |||
1331 | 1 | public function getRowStyle(int $row): ?Style |
|
1332 | { |
||
1333 | 1 | return $this->parent?->getCellXfByIndexOrNull( |
|
1334 | 1 | ($this->rowDimensions[$row] ?? null)?->getXfIndex() |
|
1335 | 1 | ); |
|
1336 | } |
||
1337 | |||
1338 | 566 | public function rowDimensionExists(int $row): bool |
|
1339 | { |
||
1340 | 566 | return isset($this->rowDimensions[$row]); |
|
1341 | } |
||
1342 | |||
1343 | 37 | public function columnDimensionExists(string $column): bool |
|
1344 | { |
||
1345 | 37 | return isset($this->columnDimensions[$column]); |
|
1346 | } |
||
1347 | |||
1348 | /** |
||
1349 | * Get column dimension at a specific column. |
||
1350 | * |
||
1351 | * @param string $column String index of the column eg: 'A' |
||
1352 | */ |
||
1353 | 628 | public function getColumnDimension(string $column): ColumnDimension |
|
1354 | { |
||
1355 | // Uppercase coordinate |
||
1356 | 628 | $column = strtoupper($column); |
|
1357 | |||
1358 | // Fetch dimensions |
||
1359 | 628 | if (!isset($this->columnDimensions[$column])) { |
|
1360 | 628 | $this->columnDimensions[$column] = new ColumnDimension($column); |
|
1361 | |||
1362 | 628 | $columnIndex = Coordinate::columnIndexFromString($column); |
|
1363 | 628 | if ($this->cachedHighestColumn < $columnIndex) { |
|
1364 | 442 | $this->cachedHighestColumn = $columnIndex; |
|
1365 | } |
||
1366 | } |
||
1367 | |||
1368 | 628 | return $this->columnDimensions[$column]; |
|
1369 | } |
||
1370 | |||
1371 | /** |
||
1372 | * Get column dimension at a specific column by using numeric cell coordinates. |
||
1373 | * |
||
1374 | * @param int $columnIndex Numeric column coordinate of the cell |
||
1375 | */ |
||
1376 | 101 | public function getColumnDimensionByColumn(int $columnIndex): ColumnDimension |
|
1377 | { |
||
1378 | 101 | return $this->getColumnDimension(Coordinate::stringFromColumnIndex($columnIndex)); |
|
1379 | } |
||
1380 | |||
1381 | 1 | public function getColumnStyle(string $column): ?Style |
|
1382 | { |
||
1383 | 1 | return $this->parent?->getCellXfByIndexOrNull( |
|
1384 | 1 | ($this->columnDimensions[$column] ?? null)?->getXfIndex() |
|
1385 | 1 | ); |
|
1386 | } |
||
1387 | |||
1388 | /** |
||
1389 | * Get style for cell. |
||
1390 | * |
||
1391 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $cellCoordinate |
||
1392 | * A simple string containing a cell address like 'A1' or a cell range like 'A1:E10' |
||
1393 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
1394 | * or a CellAddress or AddressRange object. |
||
1395 | */ |
||
1396 | 10052 | public function getStyle(AddressRange|CellAddress|int|string|array $cellCoordinate): Style |
|
1397 | { |
||
1398 | 10052 | if (is_string($cellCoordinate)) { |
|
1 ignored issue
–
show
|
|||
1399 | $cellCoordinate = Validations::definedNameToCoordinate($cellCoordinate, $this, true); |
||
1400 | } |
||
1401 | 10052 | $cellCoordinate = Validations::validateCellOrCellRange($cellCoordinate); |
|
1402 | |||
1403 | // set this sheet as active |
||
1404 | 10052 | $this->getParentOrThrow()->setActiveSheetIndex($this->getParentOrThrow()->getIndex($this)); |
|
1405 | |||
1406 | 10052 | // set cell coordinate as active |
|
1407 | $this->setSelectedCells($cellCoordinate); |
||
1408 | |||
1409 | return $this->getParentOrThrow()->getCellXfSupervisor(); |
||
1410 | } |
||
1411 | |||
1412 | /** |
||
1413 | * Get conditional styles for a cell. |
||
1414 | * |
||
1415 | * @param string $coordinate eg: 'A1' or 'A1:A3'. |
||
1416 | * If a single cell is referenced, then the array of conditional styles will be returned if the cell is |
||
1417 | * included in a conditional style range. |
||
1418 | * If a range of cells is specified, then the styles will only be returned if the range matches the entire |
||
1419 | * range of the conditional. |
||
1420 | * @param bool $firstOnly default true, return all matching |
||
1421 | * conditionals ordered by priority if false, first only if true |
||
1422 | 240 | * |
|
1423 | * @return Conditional[] |
||
1424 | 240 | */ |
|
1425 | 240 | public function getConditionalStyles(string $coordinate, bool $firstOnly = true): array |
|
1426 | 48 | { |
|
1427 | $coordinate = strtoupper($coordinate); |
||
1428 | if (preg_match('/[: ,]/', $coordinate) === 1) { |
||
1429 | 217 | return $this->conditionalStylesCollection[$coordinate] ?? []; |
|
1430 | 217 | } |
|
1431 | 209 | ||
1432 | 209 | $conditionalStyles = []; |
|
1433 | 209 | foreach ($this->conditionalStylesCollection as $keyStylesOrig => $conditionalRange) { |
|
1434 | 209 | $keyStyles = Coordinate::resolveUnionAndIntersection($keyStylesOrig); |
|
1435 | 9 | $keyParts = explode(',', $keyStyles); |
|
1436 | 9 | foreach ($keyParts as $keyPart) { |
|
1437 | if ($keyPart === $coordinate) { |
||
1438 | if ($firstOnly) { |
||
1439 | return $conditionalRange; |
||
1440 | } |
||
1441 | 204 | $conditionalStyles[$keyStylesOrig] = $conditionalRange; |
|
1442 | 203 | ||
1443 | 189 | break; |
|
1444 | 188 | } elseif (str_contains($keyPart, ':')) { |
|
1445 | if (Coordinate::coordinateIsInsideRange($keyPart, $coordinate)) { |
||
1446 | 1 | if ($firstOnly) { |
|
1447 | return $conditionalRange; |
||
1448 | 1 | } |
|
1449 | $conditionalStyles[$keyStylesOrig] = $conditionalRange; |
||
1450 | |||
1451 | break; |
||
1452 | } |
||
1453 | 46 | } |
|
1454 | 46 | } |
|
1455 | 1 | } |
|
1456 | 1 | $outArray = []; |
|
1457 | foreach ($conditionalStyles as $conditionalArray) { |
||
1458 | foreach ($conditionalArray as $conditional) { |
||
1459 | 46 | $outArray[] = $conditional; |
|
1460 | } |
||
1461 | 46 | } |
|
1462 | usort($outArray, [self::class, 'comparePriority']); |
||
1463 | |||
1464 | 1 | return $outArray; |
|
1465 | } |
||
1466 | 1 | ||
1467 | 1 | private static function comparePriority(Conditional $condA, Conditional $condB): int |
|
1468 | 1 | { |
|
1469 | $a = $condA->getPriority(); |
||
1470 | $b = $condB->getPriority(); |
||
1471 | 1 | if ($a === $b) { |
|
1472 | return 0; |
||
1473 | } |
||
1474 | 1 | if ($a === 0) { |
|
1475 | return 1; |
||
1476 | } |
||
1477 | if ($b === 0) { |
||
1478 | 1 | return -1; |
|
1479 | } |
||
1480 | |||
1481 | 180 | return ($a < $b) ? -1 : 1; |
|
1482 | } |
||
1483 | 180 | ||
1484 | 180 | public function getConditionalRange(string $coordinate): ?string |
|
1485 | 180 | { |
|
1486 | 180 | $coordinate = strtoupper($coordinate); |
|
1487 | 180 | $cell = $this->getCell($coordinate); |
|
1488 | 180 | foreach (array_keys($this->conditionalStylesCollection) as $conditionalRange) { |
|
1489 | 179 | $cellBlocks = explode(',', Coordinate::resolveUnionAndIntersection($conditionalRange)); |
|
1490 | foreach ($cellBlocks as $cellBlock) { |
||
1491 | if ($cell->isInRange($cellBlock)) { |
||
1492 | return $conditionalRange; |
||
1493 | } |
||
1494 | 3 | } |
|
1495 | } |
||
1496 | |||
1497 | return null; |
||
1498 | } |
||
1499 | |||
1500 | /** |
||
1501 | * Do conditional styles exist for this cell? |
||
1502 | * |
||
1503 | * @param string $coordinate eg: 'A1' or 'A1:A3'. |
||
1504 | * If a single cell is specified, then this method will return true if that cell is included in a |
||
1505 | * conditional style range. |
||
1506 | 22 | * If a range of cells is specified, then true will only be returned if the range matches the entire |
|
1507 | * range of the conditional. |
||
1508 | 22 | */ |
|
1509 | public function conditionalStylesExists(string $coordinate): bool |
||
1510 | { |
||
1511 | return !empty($this->getConditionalStyles($coordinate)); |
||
1512 | } |
||
1513 | |||
1514 | /** |
||
1515 | * Removes conditional styles for a cell. |
||
1516 | * |
||
1517 | * @param string $coordinate eg: 'A1' |
||
1518 | 42 | * |
|
1519 | * @return $this |
||
1520 | 42 | */ |
|
1521 | public function removeConditionalStyles(string $coordinate): static |
||
1522 | 42 | { |
|
1523 | unset($this->conditionalStylesCollection[strtoupper($coordinate)]); |
||
1524 | |||
1525 | return $this; |
||
1526 | } |
||
1527 | |||
1528 | 553 | /** |
|
1529 | * Get collection of conditional styles. |
||
1530 | 553 | */ |
|
1531 | public function getConditionalStylesCollection(): array |
||
1532 | { |
||
1533 | return $this->conditionalStylesCollection; |
||
1534 | } |
||
1535 | |||
1536 | /** |
||
1537 | * Set conditional styles. |
||
1538 | * |
||
1539 | * @param string $coordinate eg: 'A1' |
||
1540 | * @param Conditional[] $styles |
||
1541 | 306 | * |
|
1542 | * @return $this |
||
1543 | 306 | */ |
|
1544 | public function setConditionalStyles(string $coordinate, array $styles): static |
||
1545 | 306 | { |
|
1546 | $this->conditionalStylesCollection[strtoupper($coordinate)] = $styles; |
||
1547 | |||
1548 | return $this; |
||
1549 | } |
||
1550 | |||
1551 | /** |
||
1552 | * Duplicate cell style to a range of cells. |
||
1553 | * |
||
1554 | * Please note that this will overwrite existing cell styles for cells in range! |
||
1555 | * |
||
1556 | * @param Style $style Cell style to duplicate |
||
1557 | * @param string $range Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1") |
||
1558 | 2 | * |
|
1559 | * @return $this |
||
1560 | */ |
||
1561 | 2 | public function duplicateStyle(Style $style, string $range): static |
|
1562 | 2 | { |
|
1563 | // Add the style to the workbook if necessary |
||
1564 | 1 | $workbook = $this->getParentOrThrow(); |
|
1565 | if ($existingStyle = $workbook->getCellXfByHashCode($style->getHashCode())) { |
||
1566 | // there is already such cell Xf in our collection |
||
1567 | 2 | $xfIndex = $existingStyle->getIndex(); |
|
1568 | 2 | } else { |
|
1569 | // we don't have such a cell Xf, need to add |
||
1570 | $workbook->addCellXf($style); |
||
1571 | $xfIndex = $style->getIndex(); |
||
1572 | 2 | } |
|
1573 | |||
1574 | // Calculate range outer borders |
||
1575 | 2 | [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range . ':' . $range); |
|
1576 | |||
1577 | // Make sure we can loop upwards on rows and columns |
||
1578 | if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) { |
||
1579 | $tmp = $rangeStart; |
||
1580 | $rangeStart = $rangeEnd; |
||
1581 | $rangeEnd = $tmp; |
||
1582 | 2 | } |
|
1583 | 2 | ||
1584 | 2 | // Loop through cells and apply styles |
|
1585 | for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
||
1586 | for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
||
1587 | $this->getCell(Coordinate::stringFromColumnIndex($col) . $row)->setXfIndex($xfIndex); |
||
1588 | 2 | } |
|
1589 | } |
||
1590 | |||
1591 | return $this; |
||
1592 | } |
||
1593 | |||
1594 | /** |
||
1595 | * Duplicate conditional style to a range of cells. |
||
1596 | * |
||
1597 | * Please note that this will overwrite existing cell styles for cells in range! |
||
1598 | * |
||
1599 | * @param Conditional[] $styles Cell style to duplicate |
||
1600 | * @param string $range Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1") |
||
1601 | 18 | * |
|
1602 | * @return $this |
||
1603 | 18 | */ |
|
1604 | 18 | public function duplicateConditionalStyle(array $styles, string $range = ''): static |
|
1605 | { |
||
1606 | foreach ($styles as $cellStyle) { |
||
1607 | if (!($cellStyle instanceof Conditional)) { |
||
1608 | throw new Exception('Style is not a conditional style'); |
||
1609 | } |
||
1610 | 18 | } |
|
1611 | |||
1612 | // Calculate range outer borders |
||
1613 | 18 | [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range . ':' . $range); |
|
1614 | |||
1615 | // Make sure we can loop upwards on rows and columns |
||
1616 | if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) { |
||
1617 | $tmp = $rangeStart; |
||
1618 | $rangeStart = $rangeEnd; |
||
1619 | $rangeEnd = $tmp; |
||
1620 | 18 | } |
|
1621 | 18 | ||
1622 | 18 | // Loop through cells and apply styles |
|
1623 | for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { |
||
1624 | for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { |
||
1625 | $this->setConditionalStyles(Coordinate::stringFromColumnIndex($col) . $row, $styles); |
||
1626 | 18 | } |
|
1627 | } |
||
1628 | |||
1629 | return $this; |
||
1630 | } |
||
1631 | |||
1632 | /** |
||
1633 | * Set break on a cell. |
||
1634 | * |
||
1635 | * @param array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
||
1636 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
1637 | * @param int $break Break type (type of Worksheet::BREAK_*) |
||
1638 | 29 | * |
|
1639 | * @return $this |
||
1640 | 29 | */ |
|
1641 | public function setBreak(CellAddress|string|array $coordinate, int $break, int $max = -1): static |
||
1642 | 29 | { |
|
1643 | 7 | $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)); |
|
1644 | 29 | ||
1645 | 21 | if ($break === self::BREAK_NONE) { |
|
1646 | 17 | unset($this->rowBreaks[$cellAddress], $this->columnBreaks[$cellAddress]); |
|
1647 | 17 | } elseif ($break === self::BREAK_ROW) { |
|
1648 | $this->rowBreaks[$cellAddress] = new PageBreak($break, $cellAddress, $max); |
||
1649 | } elseif ($break === self::BREAK_COLUMN) { |
||
1650 | 29 | $this->columnBreaks[$cellAddress] = new PageBreak($break, $cellAddress, $max); |
|
1651 | } |
||
1652 | |||
1653 | return $this; |
||
1654 | } |
||
1655 | |||
1656 | /** |
||
1657 | * Get breaks. |
||
1658 | 623 | * |
|
1659 | * @return int[] |
||
1660 | 623 | */ |
|
1661 | public function getBreaks(): array |
||
1662 | 623 | { |
|
1663 | 623 | $breaks = []; |
|
1664 | 623 | /** @var callable $compareFunction */ |
|
1665 | 10 | $compareFunction = [self::class, 'compareRowBreaks']; |
|
1666 | uksort($this->rowBreaks, $compareFunction); |
||
1667 | foreach ($this->rowBreaks as $break) { |
||
1668 | 623 | $breaks[$break->getCoordinate()] = self::BREAK_ROW; |
|
1669 | 623 | } |
|
1670 | 623 | /** @var callable $compareFunction */ |
|
1671 | 8 | $compareFunction = [self::class, 'compareColumnBreaks']; |
|
1672 | uksort($this->columnBreaks, $compareFunction); |
||
1673 | foreach ($this->columnBreaks as $break) { |
||
1674 | 623 | $breaks[$break->getCoordinate()] = self::BREAK_COLUMN; |
|
1675 | } |
||
1676 | |||
1677 | return $breaks; |
||
1678 | } |
||
1679 | |||
1680 | /** |
||
1681 | * Get row breaks. |
||
1682 | 475 | * |
|
1683 | * @return PageBreak[] |
||
1684 | */ |
||
1685 | 475 | public function getRowBreaks(): array |
|
1686 | 475 | { |
|
1687 | /** @var callable $compareFunction */ |
||
1688 | 475 | $compareFunction = [self::class, 'compareRowBreaks']; |
|
1689 | uksort($this->rowBreaks, $compareFunction); |
||
1690 | |||
1691 | 9 | return $this->rowBreaks; |
|
1692 | } |
||
1693 | 9 | ||
1694 | 9 | protected static function compareRowBreaks(string $coordinate1, string $coordinate2): int |
|
1695 | { |
||
1696 | 9 | $row1 = Coordinate::indexesFromString($coordinate1)[1]; |
|
1697 | $row2 = Coordinate::indexesFromString($coordinate2)[1]; |
||
1698 | |||
1699 | 5 | return $row1 - $row2; |
|
1700 | } |
||
1701 | 5 | ||
1702 | 5 | protected static function compareColumnBreaks(string $coordinate1, string $coordinate2): int |
|
1703 | { |
||
1704 | 5 | $column1 = Coordinate::indexesFromString($coordinate1)[0]; |
|
1705 | $column2 = Coordinate::indexesFromString($coordinate2)[0]; |
||
1706 | |||
1707 | return $column1 - $column2; |
||
1708 | } |
||
1709 | |||
1710 | /** |
||
1711 | * Get column breaks. |
||
1712 | 474 | * |
|
1713 | * @return PageBreak[] |
||
1714 | */ |
||
1715 | 474 | public function getColumnBreaks(): array |
|
1716 | 474 | { |
|
1717 | /** @var callable $compareFunction */ |
||
1718 | 474 | $compareFunction = [self::class, 'compareColumnBreaks']; |
|
1719 | uksort($this->columnBreaks, $compareFunction); |
||
1720 | |||
1721 | return $this->columnBreaks; |
||
1722 | } |
||
1723 | |||
1724 | /** |
||
1725 | * Set merge on a cell range. |
||
1726 | * |
||
1727 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range A simple string containing a Cell range like 'A1:E10' |
||
1728 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
1729 | * or an AddressRange. |
||
1730 | * @param string $behaviour How the merged cells should behave. |
||
1731 | * Possible values are: |
||
1732 | * MERGE_CELL_CONTENT_EMPTY - Empty the content of the hidden cells |
||
1733 | * MERGE_CELL_CONTENT_HIDE - Keep the content of the hidden cells |
||
1734 | * MERGE_CELL_CONTENT_MERGE - Move the content of the hidden cells into the first cell |
||
1735 | 168 | * |
|
1736 | * @return $this |
||
1737 | 168 | */ |
|
1738 | public function mergeCells(AddressRange|string|array $range, string $behaviour = self::MERGE_CELL_CONTENT_EMPTY): static |
||
1739 | 167 | { |
|
1740 | 1 | $range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range)); |
|
1741 | |||
1742 | if (!str_contains($range, ':')) { |
||
1743 | 167 | $range .= ":{$range}"; |
|
1744 | 1 | } |
|
1745 | |||
1746 | if (preg_match('/^([A-Z]+)(\d+):([A-Z]+)(\d+)$/', $range, $matches) !== 1) { |
||
1747 | 166 | throw new Exception('Merge must be on a valid range of cells.'); |
|
1748 | 166 | } |
|
1749 | 166 | ||
1750 | 166 | $this->mergeCells[$range] = $range; |
|
1751 | 166 | $firstRow = (int) $matches[2]; |
|
1752 | 166 | $lastRow = (int) $matches[4]; |
|
1753 | 166 | $firstColumn = $matches[1]; |
|
1754 | 166 | $lastColumn = $matches[3]; |
|
1755 | 166 | $firstColumnIndex = Coordinate::columnIndexFromString($firstColumn); |
|
1756 | $lastColumnIndex = Coordinate::columnIndexFromString($lastColumn); |
||
1757 | 166 | $numberRows = $lastRow - $firstRow; |
|
1758 | 34 | $numberColumns = $lastColumnIndex - $firstColumnIndex; |
|
1759 | |||
1760 | if ($numberRows === 1 && $numberColumns === 1) { |
||
1761 | return $this; |
||
1762 | 159 | } |
|
1763 | 159 | ||
1764 | 36 | // create upper left cell if it does not already exist |
|
1765 | $upperLeft = "{$firstColumn}{$firstRow}"; |
||
1766 | if (!$this->cellExists($upperLeft)) { |
||
1767 | 159 | $this->getCell($upperLeft)->setValueExplicit(null, DataType::TYPE_NULL); |
|
1768 | } |
||
1769 | 56 | ||
1770 | 17 | if ($behaviour !== self::MERGE_CELL_CONTENT_HIDE) { |
|
1771 | // Blank out the rest of the cells in the range (if they exist) |
||
1772 | 39 | if ($numberRows > $numberColumns) { |
|
1773 | $this->clearMergeCellsByColumn($firstColumn, $lastColumn, $firstRow, $lastRow, $upperLeft, $behaviour); |
||
1774 | } else { |
||
1775 | $this->clearMergeCellsByRow($firstColumn, $lastColumnIndex, $firstRow, $lastRow, $upperLeft, $behaviour); |
||
1776 | 159 | } |
|
1777 | } |
||
1778 | |||
1779 | 17 | return $this; |
|
1780 | } |
||
1781 | 17 | ||
1782 | private function clearMergeCellsByColumn(string $firstColumn, string $lastColumn, int $firstRow, int $lastRow, string $upperLeft, string $behaviour): void |
||
1783 | 17 | { |
|
1784 | $leftCellValue = ($behaviour === self::MERGE_CELL_CONTENT_MERGE) |
||
1785 | 17 | ? [$this->getCell($upperLeft)->getFormattedValue()] |
|
1786 | 17 | : []; |
|
1787 | 17 | ||
1788 | 17 | foreach ($this->getColumnIterator($firstColumn, $lastColumn) as $column) { |
|
1789 | 17 | $iterator = $column->getCellIterator($firstRow); |
|
1790 | 17 | $iterator->setIterateOnlyExistingCells(true); |
|
1791 | 17 | foreach ($iterator as $cell) { |
|
1792 | 7 | if ($cell !== null) { |
|
1793 | $row = $cell->getRow(); |
||
1794 | 17 | if ($row > $lastRow) { |
|
1795 | break; |
||
1796 | } |
||
1797 | $leftCellValue = $this->mergeCellBehaviour($cell, $upperLeft, $behaviour, $leftCellValue); |
||
1798 | } |
||
1799 | 17 | } |
|
1800 | } |
||
1801 | |||
1802 | if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) { |
||
1803 | $this->getCell($upperLeft)->setValueExplicit(implode(' ', $leftCellValue), DataType::TYPE_STRING); |
||
1804 | 39 | } |
|
1805 | } |
||
1806 | 39 | ||
1807 | 4 | private function clearMergeCellsByRow(string $firstColumn, int $lastColumnIndex, int $firstRow, int $lastRow, string $upperLeft, string $behaviour): void |
|
1808 | 35 | { |
|
1809 | $leftCellValue = ($behaviour === self::MERGE_CELL_CONTENT_MERGE) |
||
1810 | 39 | ? [$this->getCell($upperLeft)->getFormattedValue()] |
|
1811 | 39 | : []; |
|
1812 | 39 | ||
1813 | 39 | foreach ($this->getRowIterator($firstRow, $lastRow) as $row) { |
|
1814 | 39 | $iterator = $row->getCellIterator($firstColumn); |
|
1815 | 39 | $iterator->setIterateOnlyExistingCells(true); |
|
1816 | 39 | foreach ($iterator as $cell) { |
|
1817 | 39 | if ($cell !== null) { |
|
1818 | 8 | $column = $cell->getColumn(); |
|
1819 | $columnIndex = Coordinate::columnIndexFromString($column); |
||
1820 | 39 | if ($columnIndex > $lastColumnIndex) { |
|
1821 | break; |
||
1822 | } |
||
1823 | $leftCellValue = $this->mergeCellBehaviour($cell, $upperLeft, $behaviour, $leftCellValue); |
||
1824 | } |
||
1825 | 39 | } |
|
1826 | 4 | } |
|
1827 | |||
1828 | if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) { |
||
1829 | $this->getCell($upperLeft)->setValueExplicit(implode(' ', $leftCellValue), DataType::TYPE_STRING); |
||
1830 | 56 | } |
|
1831 | } |
||
1832 | 56 | ||
1833 | 24 | public function mergeCellBehaviour(Cell $cell, string $upperLeft, string $behaviour, array $leftCellValue): array |
|
1834 | 24 | { |
|
1835 | 4 | if ($cell->getCoordinate() !== $upperLeft) { |
|
1836 | 4 | Calculation::getInstance($cell->getWorksheet()->getParentOrThrow())->flushInstance(); |
|
1837 | 4 | if ($behaviour === self::MERGE_CELL_CONTENT_MERGE) { |
|
1838 | $cellValue = $cell->getFormattedValue(); |
||
1839 | if ($cellValue !== '') { |
||
1840 | 24 | $leftCellValue[] = $cellValue; |
|
1841 | } |
||
1842 | } |
||
1843 | 56 | $cell->setValueExplicit(null, DataType::TYPE_NULL); |
|
1844 | } |
||
1845 | |||
1846 | return $leftCellValue; |
||
1847 | } |
||
1848 | |||
1849 | /** |
||
1850 | * Remove merge on a cell range. |
||
1851 | * |
||
1852 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|string $range A simple string containing a Cell range like 'A1:E10' |
||
1853 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
1854 | * or an AddressRange. |
||
1855 | 23 | * |
|
1856 | * @return $this |
||
1857 | 23 | */ |
|
1858 | public function unmergeCells(AddressRange|string|array $range): static |
||
1859 | 23 | { |
|
1860 | 22 | $range = Functions::trimSheetFromCellReference(Validations::validateCellRange($range)); |
|
1861 | 22 | ||
1862 | if (str_contains($range, ':')) { |
||
1863 | if (isset($this->mergeCells[$range])) { |
||
1864 | unset($this->mergeCells[$range]); |
||
1865 | } else { |
||
1866 | 1 | throw new Exception('Cell range ' . $range . ' not known as merged.'); |
|
1867 | } |
||
1868 | } else { |
||
1869 | 22 | throw new Exception('Merge can only be removed from a range of cells.'); |
|
1870 | } |
||
1871 | |||
1872 | return $this; |
||
1873 | } |
||
1874 | |||
1875 | /** |
||
1876 | * Get merge cells array. |
||
1877 | 1121 | * |
|
1878 | * @return string[] |
||
1879 | 1121 | */ |
|
1880 | public function getMergeCells(): array |
||
1881 | { |
||
1882 | return $this->mergeCells; |
||
1883 | } |
||
1884 | |||
1885 | /** |
||
1886 | * Set merge cells array for the entire sheet. Use instead mergeCells() to merge |
||
1887 | * a single cell range. |
||
1888 | * |
||
1889 | * @param string[] $mergeCells |
||
1890 | 94 | * |
|
1891 | * @return $this |
||
1892 | 94 | */ |
|
1893 | public function setMergeCells(array $mergeCells): static |
||
1894 | 94 | { |
|
1895 | $this->mergeCells = $mergeCells; |
||
1896 | |||
1897 | return $this; |
||
1898 | } |
||
1899 | |||
1900 | /** |
||
1901 | * Set protection on a cell or cell range. |
||
1902 | * |
||
1903 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10' |
||
1904 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
1905 | * or a CellAddress or AddressRange object. |
||
1906 | * @param string $password Password to unlock the protection |
||
1907 | * @param bool $alreadyHashed If the password has already been hashed, set this to true |
||
1908 | 24 | * |
|
1909 | * @return $this |
||
1910 | 24 | */ |
|
1911 | public function protectCells(AddressRange|CellAddress|int|string|array $range, string $password = '', bool $alreadyHashed = false, string $name = '', string $securityDescriptor = ''): static |
||
1912 | 24 | { |
|
1913 | 24 | $range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range)); |
|
1914 | |||
1915 | 24 | if (!$alreadyHashed && $password !== '') { |
|
1916 | $password = Shared\PasswordHasher::hashPassword($password); |
||
1917 | 24 | } |
|
1918 | $this->protectedCells[$range] = new ProtectedRange($range, $password, $name, $securityDescriptor); |
||
1919 | |||
1920 | return $this; |
||
1921 | } |
||
1922 | |||
1923 | /** |
||
1924 | * Remove protection on a cell or cell range. |
||
1925 | * |
||
1926 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $range A simple string containing a Cell range like 'A1:E10' |
||
1927 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
1928 | * or a CellAddress or AddressRange object. |
||
1929 | 20 | * |
|
1930 | * @return $this |
||
1931 | 20 | */ |
|
1932 | public function unprotectCells(AddressRange|CellAddress|int|string|array $range): static |
||
1933 | 20 | { |
|
1934 | 19 | $range = Functions::trimSheetFromCellReference(Validations::validateCellOrCellRange($range)); |
|
1935 | |||
1936 | 1 | if (isset($this->protectedCells[$range])) { |
|
1937 | unset($this->protectedCells[$range]); |
||
1938 | } else { |
||
1939 | 19 | throw new Exception('Cell range ' . $range . ' not known as protected.'); |
|
1940 | } |
||
1941 | |||
1942 | return $this; |
||
1943 | } |
||
1944 | |||
1945 | /** |
||
1946 | * Get protected cells. |
||
1947 | 553 | * |
|
1948 | * @return ProtectedRange[] |
||
1949 | 553 | */ |
|
1950 | public function getProtectedCellRanges(): array |
||
1951 | { |
||
1952 | return $this->protectedCells; |
||
1953 | } |
||
1954 | |||
1955 | 743 | /** |
|
1956 | * Get Autofilter. |
||
1957 | 743 | */ |
|
1958 | public function getAutoFilter(): AutoFilter |
||
1959 | { |
||
1960 | return $this->autoFilter; |
||
1961 | } |
||
1962 | |||
1963 | /** |
||
1964 | * Set AutoFilter. |
||
1965 | * |
||
1966 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|AutoFilter|string $autoFilterOrRange |
||
1967 | * A simple string containing a Cell range like 'A1:E10' is permitted for backward compatibility |
||
1968 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
1969 | * or an AddressRange. |
||
1970 | 18 | * |
|
1971 | * @return $this |
||
1972 | 18 | */ |
|
1973 | public function setAutoFilter(AddressRange|string|array|AutoFilter $autoFilterOrRange): static |
||
1974 | { |
||
1975 | 18 | if (is_object($autoFilterOrRange) && ($autoFilterOrRange instanceof AutoFilter)) { |
|
1976 | $this->autoFilter = $autoFilterOrRange; |
||
1977 | 18 | } else { |
|
1978 | $cellRange = Functions::trimSheetFromCellReference(Validations::validateCellRange($autoFilterOrRange)); |
||
1979 | |||
1980 | 18 | $this->autoFilter->setRange($cellRange); |
|
1981 | } |
||
1982 | |||
1983 | return $this; |
||
1984 | } |
||
1985 | |||
1986 | 1 | /** |
|
1987 | * Remove autofilter. |
||
1988 | 1 | */ |
|
1989 | public function removeAutoFilter(): self |
||
1990 | 1 | { |
|
1991 | $this->autoFilter->setRange(''); |
||
1992 | |||
1993 | return $this; |
||
1994 | } |
||
1995 | |||
1996 | /** |
||
1997 | * Get collection of Tables. |
||
1998 | 10060 | * |
|
1999 | * @return ArrayObject<int, Table> |
||
2000 | 10060 | */ |
|
2001 | public function getTableCollection(): ArrayObject |
||
2002 | { |
||
2003 | return $this->tableCollection; |
||
2004 | } |
||
2005 | |||
2006 | /** |
||
2007 | * Add Table. |
||
2008 | 97 | * |
|
2009 | * @return $this |
||
2010 | 97 | */ |
|
2011 | 97 | public function addTable(Table $table): self |
|
2012 | { |
||
2013 | 97 | $table->setWorksheet($this); |
|
2014 | $this->tableCollection[] = $table; |
||
2015 | |||
2016 | return $this; |
||
2017 | } |
||
2018 | |||
2019 | 1 | /** |
|
2020 | * @return string[] array of Table names |
||
2021 | 1 | */ |
|
2022 | public function getTableNames(): array |
||
2023 | 1 | { |
|
2024 | $tableNames = []; |
||
2025 | 1 | ||
2026 | foreach ($this->tableCollection as $table) { |
||
2027 | /** @var Table $table */ |
||
2028 | 1 | $tableNames[] = $table->getName(); |
|
2029 | } |
||
2030 | |||
2031 | return $tableNames; |
||
2032 | } |
||
2033 | |||
2034 | /** |
||
2035 | * @param string $name the table name to search |
||
2036 | 92 | * |
|
2037 | * @return null|Table The table from the tables collection, or null if not found |
||
2038 | 92 | */ |
|
2039 | public function getTableByName(string $name): ?Table |
||
2040 | 92 | { |
|
2041 | $tableIndex = $this->getTableIndexByName($name); |
||
2042 | |||
2043 | return ($tableIndex === null) ? null : $this->tableCollection[$tableIndex]; |
||
2044 | } |
||
2045 | |||
2046 | /** |
||
2047 | * @param string $name the table name to search |
||
2048 | 93 | * |
|
2049 | * @return null|int The index of the located table in the tables collection, or null if not found |
||
2050 | 93 | */ |
|
2051 | 93 | protected function getTableIndexByName(string $name): ?int |
|
2052 | { |
||
2053 | 62 | $name = StringHelper::strToUpper($name); |
|
2054 | 61 | foreach ($this->tableCollection as $index => $table) { |
|
2055 | /** @var Table $table */ |
||
2056 | if (StringHelper::strToUpper($table->getName()) === $name) { |
||
2057 | return $index; |
||
2058 | 37 | } |
|
2059 | } |
||
2060 | |||
2061 | return null; |
||
2062 | } |
||
2063 | |||
2064 | /** |
||
2065 | * Remove Table by name. |
||
2066 | * |
||
2067 | * @param string $name Table name |
||
2068 | 1 | * |
|
2069 | * @return $this |
||
2070 | 1 | */ |
|
2071 | public function removeTableByName(string $name): self |
||
2072 | 1 | { |
|
2073 | 1 | $tableIndex = $this->getTableIndexByName($name); |
|
2074 | |||
2075 | if ($tableIndex !== null) { |
||
2076 | 1 | unset($this->tableCollection[$tableIndex]); |
|
2077 | } |
||
2078 | |||
2079 | return $this; |
||
2080 | } |
||
2081 | |||
2082 | 1 | /** |
|
2083 | * Remove collection of Tables. |
||
2084 | 1 | */ |
|
2085 | public function removeTableCollection(): self |
||
2086 | 1 | { |
|
2087 | $this->tableCollection = new ArrayObject(); |
||
2088 | |||
2089 | return $this; |
||
2090 | } |
||
2091 | |||
2092 | 243 | /** |
|
2093 | * Get Freeze Pane. |
||
2094 | 243 | */ |
|
2095 | public function getFreezePane(): ?string |
||
2096 | { |
||
2097 | return $this->freezePane; |
||
2098 | } |
||
2099 | |||
2100 | /** |
||
2101 | * Freeze Pane. |
||
2102 | * |
||
2103 | * Examples: |
||
2104 | * |
||
2105 | * - A2 will freeze the rows above cell A2 (i.e row 1) |
||
2106 | * - B1 will freeze the columns to the left of cell B1 (i.e column A) |
||
2107 | * - B2 will freeze the rows above and to the left of cell B2 (i.e row 1 and column A) |
||
2108 | * |
||
2109 | * @param null|array{0: int, 1: int}|CellAddress|string $coordinate Coordinate of the cell as a string, eg: 'C5'; |
||
2110 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
2111 | * Passing a null value for this argument will clear any existing freeze pane for this worksheet. |
||
2112 | * @param null|array{0: int, 1: int}|CellAddress|string $topLeftCell default position of the right bottom pane |
||
2113 | * Coordinate of the cell as a string, eg: 'C5'; or as an array of [$columnIndex, $row] (e.g. [3, 5]), |
||
2114 | * or a CellAddress object. |
||
2115 | 49 | * |
|
2116 | * @return $this |
||
2117 | 49 | */ |
|
2118 | 49 | public function freezePane(null|CellAddress|string|array $coordinate, null|CellAddress|string|array $topLeftCell = null, bool $frozenSplit = false): static |
|
2119 | 49 | { |
|
2120 | 49 | $this->panes = [ |
|
2121 | 49 | 'bottomRight' => null, |
|
2122 | 49 | 'bottomLeft' => null, |
|
2123 | 49 | 'topRight' => null, |
|
2124 | 49 | 'topLeft' => null, |
|
2125 | 1 | ]; |
|
2126 | 49 | $cellAddress = ($coordinate !== null) |
|
2127 | 1 | ? Functions::trimSheetFromCellReference(Validations::validateCellAddress($coordinate)) |
|
2128 | : null; |
||
2129 | 48 | if ($cellAddress !== null && Coordinate::coordinateIsRange($cellAddress)) { |
|
2130 | 37 | throw new Exception('Freeze pane can not be set on a range of cells.'); |
|
2131 | 36 | } |
|
2132 | $topLeftCell = ($topLeftCell !== null) |
||
2133 | 48 | ? Functions::trimSheetFromCellReference(Validations::validateCellAddress($topLeftCell)) |
|
2134 | 36 | : null; |
|
2135 | 36 | ||
2136 | if ($cellAddress !== null && $topLeftCell === null) { |
||
2137 | $coordinate = Coordinate::coordinateFromString($cellAddress); |
||
2138 | 48 | $topLeftCell = $coordinate[0] . $coordinate[1]; |
|
2139 | 48 | } |
|
2140 | |||
2141 | 48 | $topLeftCell = "$topLeftCell"; |
|
2142 | 48 | $this->paneTopLeftCell = $topLeftCell; |
|
2143 | 48 | ||
2144 | 1 | $this->freezePane = $cellAddress; |
|
2145 | 1 | $this->topLeftCell = $topLeftCell; |
|
2146 | 1 | if ($cellAddress === null) { |
|
2147 | $this->paneState = ''; |
||
2148 | 48 | $this->xSplit = $this->ySplit = 0; |
|
2149 | 48 | $this->activePane = ''; |
|
2150 | 48 | } else { |
|
2151 | 48 | $coordinates = Coordinate::indexesFromString($cellAddress); |
|
2152 | 47 | $this->xSplit = $coordinates[0] - 1; |
|
2153 | 47 | $this->ySplit = $coordinates[1] - 1; |
|
2154 | if ($this->xSplit > 0 || $this->ySplit > 0) { |
||
2155 | 1 | $this->paneState = $frozenSplit ? self::PANE_FROZENSPLIT : self::PANE_FROZEN; |
|
2156 | 1 | $this->setSelectedCellsActivePane(); |
|
2157 | 1 | } else { |
|
2158 | $this->paneState = ''; |
||
2159 | $this->freezePane = null; |
||
2160 | $this->activePane = ''; |
||
2161 | 48 | } |
|
2162 | } |
||
2163 | |||
2164 | 47 | return $this; |
|
2165 | } |
||
2166 | 47 | ||
2167 | public function setTopLeftCell(string $topLeftCell): self |
||
2168 | 47 | { |
|
2169 | $this->topLeftCell = $topLeftCell; |
||
2170 | |||
2171 | return $this; |
||
2172 | } |
||
2173 | |||
2174 | /** |
||
2175 | * Unfreeze Pane. |
||
2176 | 1 | * |
|
2177 | * @return $this |
||
2178 | 1 | */ |
|
2179 | public function unfreezePane(): static |
||
2180 | { |
||
2181 | return $this->freezePane(null); |
||
2182 | } |
||
2183 | |||
2184 | 422 | /** |
|
2185 | * Get the default position of the right bottom pane. |
||
2186 | 422 | */ |
|
2187 | public function getTopLeftCell(): ?string |
||
2188 | { |
||
2189 | 11 | return $this->topLeftCell; |
|
2190 | } |
||
2191 | 11 | ||
2192 | public function getPaneTopLeftCell(): string |
||
2193 | { |
||
2194 | 26 | return $this->paneTopLeftCell; |
|
2195 | } |
||
2196 | 26 | ||
2197 | public function setPaneTopLeftCell(string $paneTopLeftCell): self |
||
2198 | 26 | { |
|
2199 | $this->paneTopLeftCell = $paneTopLeftCell; |
||
2200 | |||
2201 | 410 | return $this; |
|
2202 | } |
||
2203 | 410 | ||
2204 | public function usesPanes(): bool |
||
2205 | { |
||
2206 | 2 | return $this->xSplit > 0 || $this->ySplit > 0; |
|
2207 | } |
||
2208 | 2 | ||
2209 | public function getPane(string $position): ?Pane |
||
2210 | { |
||
2211 | 35 | return $this->panes[$position] ?? null; |
|
2212 | } |
||
2213 | 35 | ||
2214 | 35 | public function setPane(string $position, ?Pane $pane): self |
|
2215 | { |
||
2216 | if (array_key_exists($position, $this->panes)) { |
||
2217 | 35 | $this->panes[$position] = $pane; |
|
2218 | } |
||
2219 | |||
2220 | return $this; |
||
2221 | 3 | } |
|
2222 | |||
2223 | 3 | /** @return (null|Pane)[] */ |
|
2224 | public function getPanes(): array |
||
2225 | { |
||
2226 | 14 | return $this->panes; |
|
2227 | } |
||
2228 | 14 | ||
2229 | public function getActivePane(): string |
||
2230 | { |
||
2231 | 48 | return $this->activePane; |
|
2232 | } |
||
2233 | 48 | ||
2234 | public function setActivePane(string $activePane): self |
||
2235 | 48 | { |
|
2236 | $this->activePane = array_key_exists($activePane, $this->panes) ? $activePane : ''; |
||
2237 | |||
2238 | 11 | return $this; |
|
2239 | } |
||
2240 | 11 | ||
2241 | public function getXSplit(): int |
||
2242 | { |
||
2243 | 11 | return $this->xSplit; |
|
2244 | } |
||
2245 | 11 | ||
2246 | 11 | public function setXSplit(int $xSplit): self |
|
2247 | 1 | { |
|
2248 | $this->xSplit = $xSplit; |
||
2249 | if (in_array($this->paneState, self::VALIDFROZENSTATE, true)) { |
||
2250 | 11 | $this->freezePane([$this->xSplit + 1, $this->ySplit + 1], $this->topLeftCell, $this->paneState === self::PANE_FROZENSPLIT); |
|
2251 | } |
||
2252 | |||
2253 | 11 | return $this; |
|
2254 | } |
||
2255 | 11 | ||
2256 | public function getYSplit(): int |
||
2257 | { |
||
2258 | 26 | return $this->ySplit; |
|
2259 | } |
||
2260 | 26 | ||
2261 | 26 | public function setYSplit(int $ySplit): self |
|
2262 | 1 | { |
|
2263 | $this->ySplit = $ySplit; |
||
2264 | if (in_array($this->paneState, self::VALIDFROZENSTATE, true)) { |
||
2265 | 26 | $this->freezePane([$this->xSplit + 1, $this->ySplit + 1], $this->topLeftCell, $this->paneState === self::PANE_FROZENSPLIT); |
|
2266 | } |
||
2267 | |||
2268 | 21 | return $this; |
|
2269 | } |
||
2270 | 21 | ||
2271 | public function getPaneState(): string |
||
2272 | { |
||
2273 | return $this->paneState; |
||
2274 | } |
||
2275 | |||
2276 | public const PANE_FROZEN = 'frozen'; |
||
2277 | public const PANE_FROZENSPLIT = 'frozenSplit'; |
||
2278 | public const PANE_SPLIT = 'split'; |
||
2279 | 26 | private const VALIDPANESTATE = [self::PANE_FROZEN, self::PANE_SPLIT, self::PANE_FROZENSPLIT]; |
|
2280 | private const VALIDFROZENSTATE = [self::PANE_FROZEN, self::PANE_FROZENSPLIT]; |
||
2281 | 26 | ||
2282 | 26 | public function setPaneState(string $paneState): self |
|
2283 | 25 | { |
|
2284 | $this->paneState = in_array($paneState, self::VALIDPANESTATE, true) ? $paneState : ''; |
||
2285 | 3 | if (in_array($this->paneState, self::VALIDFROZENSTATE, true)) { |
|
2286 | $this->freezePane([$this->xSplit + 1, $this->ySplit + 1], $this->topLeftCell, $this->paneState === self::PANE_FROZENSPLIT); |
||
2287 | } else { |
||
2288 | 26 | $this->freezePane = null; |
|
2289 | } |
||
2290 | |||
2291 | return $this; |
||
2292 | } |
||
2293 | |||
2294 | /** |
||
2295 | * Insert a new row, updating all possible related data. |
||
2296 | * |
||
2297 | * @param int $before Insert before this row number |
||
2298 | * @param int $numberOfRows Number of new rows to insert |
||
2299 | 38 | * |
|
2300 | * @return $this |
||
2301 | 38 | */ |
|
2302 | 37 | public function insertNewRowBefore(int $before, int $numberOfRows = 1): static |
|
2303 | 37 | { |
|
2304 | if ($before >= 1) { |
||
2305 | 1 | $objReferenceHelper = ReferenceHelper::getInstance(); |
|
2306 | $objReferenceHelper->insertNewBefore('A' . $before, 0, $numberOfRows, $this); |
||
2307 | } else { |
||
2308 | 37 | throw new Exception('Rows can only be inserted before at least row 1.'); |
|
2309 | } |
||
2310 | |||
2311 | return $this; |
||
2312 | } |
||
2313 | |||
2314 | /** |
||
2315 | * Insert a new column, updating all possible related data. |
||
2316 | * |
||
2317 | * @param string $before Insert before this column Name, eg: 'A' |
||
2318 | * @param int $numberOfColumns Number of new columns to insert |
||
2319 | 45 | * |
|
2320 | * @return $this |
||
2321 | 45 | */ |
|
2322 | 44 | public function insertNewColumnBefore(string $before, int $numberOfColumns = 1): static |
|
2323 | 44 | { |
|
2324 | if (!is_numeric($before)) { |
||
2325 | 1 | $objReferenceHelper = ReferenceHelper::getInstance(); |
|
2326 | $objReferenceHelper->insertNewBefore($before . '1', $numberOfColumns, 0, $this); |
||
2327 | } else { |
||
2328 | 44 | throw new Exception('Column references should not be numeric.'); |
|
2329 | } |
||
2330 | |||
2331 | return $this; |
||
2332 | } |
||
2333 | |||
2334 | /** |
||
2335 | * Insert a new column, updating all possible related data. |
||
2336 | * |
||
2337 | * @param int $beforeColumnIndex Insert before this column ID (numeric column coordinate of the cell) |
||
2338 | * @param int $numberOfColumns Number of new columns to insert |
||
2339 | 2 | * |
|
2340 | * @return $this |
||
2341 | 2 | */ |
|
2342 | 1 | public function insertNewColumnBeforeByIndex(int $beforeColumnIndex, int $numberOfColumns = 1): static |
|
2343 | { |
||
2344 | if ($beforeColumnIndex >= 1) { |
||
2345 | 1 | return $this->insertNewColumnBefore(Coordinate::stringFromColumnIndex($beforeColumnIndex), $numberOfColumns); |
|
2346 | } |
||
2347 | |||
2348 | throw new Exception('Columns can only be inserted before at least column A (1).'); |
||
2349 | } |
||
2350 | |||
2351 | /** |
||
2352 | * Delete a row, updating all possible related data. |
||
2353 | * |
||
2354 | * @param int $row Remove rows, starting with this row number |
||
2355 | * @param int $numberOfRows Number of rows to remove |
||
2356 | 41 | * |
|
2357 | * @return $this |
||
2358 | 41 | */ |
|
2359 | 1 | public function removeRow(int $row, int $numberOfRows = 1): static |
|
2360 | { |
||
2361 | if ($row < 1) { |
||
2362 | 40 | throw new Exception('Rows to be deleted should at least start from row 1.'); |
|
2363 | 40 | } |
|
2364 | 40 | ||
2365 | $holdRowDimensions = $this->removeRowDimensions($row, $numberOfRows); |
||
2366 | 40 | $highestRow = $this->getHighestDataRow(); |
|
2367 | 40 | $removedRowsCounter = 0; |
|
2368 | 36 | ||
2369 | 36 | for ($r = 0; $r < $numberOfRows; ++$r) { |
|
2370 | if ($row + $r <= $highestRow) { |
||
2371 | $this->cellCollection->removeRow($row + $r); |
||
2372 | ++$removedRowsCounter; |
||
2373 | 40 | } |
|
2374 | 40 | } |
|
2375 | 40 | ||
2376 | 36 | $objReferenceHelper = ReferenceHelper::getInstance(); |
|
2377 | 36 | $objReferenceHelper->insertNewBefore('A' . ($row + $numberOfRows), 0, -$numberOfRows, $this); |
|
2378 | for ($r = 0; $r < $removedRowsCounter; ++$r) { |
||
2379 | $this->cellCollection->removeRow($highestRow); |
||
2380 | 40 | --$highestRow; |
|
2381 | } |
||
2382 | 40 | ||
2383 | $this->rowDimensions = $holdRowDimensions; |
||
2384 | |||
2385 | 40 | return $this; |
|
2386 | } |
||
2387 | 40 | ||
2388 | 40 | private function removeRowDimensions(int $row, int $numberOfRows): array |
|
2389 | 40 | { |
|
2390 | 4 | $highRow = $row + $numberOfRows - 1; |
|
2391 | 4 | $holdRowDimensions = []; |
|
2392 | 3 | foreach ($this->rowDimensions as $rowDimension) { |
|
2393 | 4 | $num = $rowDimension->getRowIndex(); |
|
2394 | 4 | if ($num < $row) { |
|
2395 | 4 | $holdRowDimensions[$num] = $rowDimension; |
|
2396 | 4 | } elseif ($num > $highRow) { |
|
2397 | 4 | $num -= $numberOfRows; |
|
2398 | $cloneDimension = clone $rowDimension; |
||
2399 | $cloneDimension->setRowIndex($num); |
||
2400 | $holdRowDimensions[$num] = $cloneDimension; |
||
2401 | 40 | } |
|
2402 | } |
||
2403 | |||
2404 | return $holdRowDimensions; |
||
2405 | } |
||
2406 | |||
2407 | /** |
||
2408 | * Remove a column, updating all possible related data. |
||
2409 | * |
||
2410 | * @param string $column Remove columns starting with this column name, eg: 'A' |
||
2411 | * @param int $numberOfColumns Number of columns to remove |
||
2412 | 33 | * |
|
2413 | * @return $this |
||
2414 | 33 | */ |
|
2415 | 1 | public function removeColumn(string $column, int $numberOfColumns = 1): static |
|
2416 | { |
||
2417 | if (is_numeric($column)) { |
||
2418 | 32 | throw new Exception('Column references should not be numeric.'); |
|
2419 | 32 | } |
|
2420 | 32 | ||
2421 | $highestColumn = $this->getHighestDataColumn(); |
||
2422 | 32 | $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn); |
|
2423 | $pColumnIndex = Coordinate::columnIndexFromString($column); |
||
2424 | 32 | ||
2425 | 32 | $holdColumnDimensions = $this->removeColumnDimensions($pColumnIndex, $numberOfColumns); |
|
2426 | 32 | ||
2427 | $column = Coordinate::stringFromColumnIndex($pColumnIndex + $numberOfColumns); |
||
2428 | 32 | $objReferenceHelper = ReferenceHelper::getInstance(); |
|
2429 | $objReferenceHelper->insertNewBefore($column . '1', -$numberOfColumns, 0, $this); |
||
2430 | 32 | ||
2431 | 2 | $this->columnDimensions = $holdColumnDimensions; |
|
2432 | |||
2433 | if ($pColumnIndex > $highestColumnIndex) { |
||
2434 | 30 | return $this; |
|
2435 | } |
||
2436 | 30 | ||
2437 | 30 | $maxPossibleColumnsToBeRemoved = $highestColumnIndex - $pColumnIndex + 1; |
|
2438 | 30 | ||
2439 | for ($c = 0, $n = min($maxPossibleColumnsToBeRemoved, $numberOfColumns); $c < $n; ++$c) { |
||
2440 | $this->cellCollection->removeColumn($highestColumn); |
||
2441 | 30 | $highestColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($highestColumn) - 1); |
|
2442 | } |
||
2443 | 30 | ||
2444 | $this->garbageCollect(); |
||
2445 | |||
2446 | 32 | return $this; |
|
2447 | } |
||
2448 | 32 | ||
2449 | 32 | private function removeColumnDimensions(int $pColumnIndex, int $numberOfColumns): array |
|
2450 | 32 | { |
|
2451 | 18 | $highCol = $pColumnIndex + $numberOfColumns - 1; |
|
2452 | 18 | $holdColumnDimensions = []; |
|
2453 | 18 | foreach ($this->columnDimensions as $columnDimension) { |
|
2454 | 18 | $num = $columnDimension->getColumnNumeric(); |
|
2455 | 18 | if ($num < $pColumnIndex) { |
|
2456 | 18 | $str = $columnDimension->getColumnIndex(); |
|
2457 | 18 | $holdColumnDimensions[$str] = $columnDimension; |
|
2458 | 18 | } elseif ($num > $highCol) { |
|
2459 | 18 | $cloneDimension = clone $columnDimension; |
|
2460 | $cloneDimension->setColumnNumeric($num - $numberOfColumns); |
||
2461 | $str = $cloneDimension->getColumnIndex(); |
||
2462 | $holdColumnDimensions[$str] = $cloneDimension; |
||
2463 | 32 | } |
|
2464 | } |
||
2465 | |||
2466 | return $holdColumnDimensions; |
||
2467 | } |
||
2468 | |||
2469 | /** |
||
2470 | * Remove a column, updating all possible related data. |
||
2471 | * |
||
2472 | * @param int $columnIndex Remove starting with this column Index (numeric column coordinate) |
||
2473 | * @param int $numColumns Number of columns to remove |
||
2474 | 2 | * |
|
2475 | * @return $this |
||
2476 | 2 | */ |
|
2477 | 1 | public function removeColumnByIndex(int $columnIndex, int $numColumns = 1): static |
|
2478 | { |
||
2479 | if ($columnIndex >= 1) { |
||
2480 | 1 | return $this->removeColumn(Coordinate::stringFromColumnIndex($columnIndex), $numColumns); |
|
2481 | } |
||
2482 | |||
2483 | throw new Exception('Columns to be deleted should at least start from column A (1)'); |
||
2484 | } |
||
2485 | |||
2486 | 986 | /** |
|
2487 | * Show gridlines? |
||
2488 | 986 | */ |
|
2489 | public function getShowGridlines(): bool |
||
2490 | { |
||
2491 | return $this->showGridlines; |
||
2492 | } |
||
2493 | |||
2494 | /** |
||
2495 | * Set show gridlines. |
||
2496 | * |
||
2497 | * @param bool $showGridLines Show gridlines (true/false) |
||
2498 | 822 | * |
|
2499 | * @return $this |
||
2500 | 822 | */ |
|
2501 | public function setShowGridlines(bool $showGridLines): self |
||
2502 | 822 | { |
|
2503 | $this->showGridlines = $showGridLines; |
||
2504 | |||
2505 | return $this; |
||
2506 | } |
||
2507 | |||
2508 | 992 | /** |
|
2509 | * Print gridlines? |
||
2510 | 992 | */ |
|
2511 | public function getPrintGridlines(): bool |
||
2512 | { |
||
2513 | return $this->printGridlines; |
||
2514 | } |
||
2515 | |||
2516 | /** |
||
2517 | * Set print gridlines. |
||
2518 | * |
||
2519 | * @param bool $printGridLines Print gridlines (true/false) |
||
2520 | 565 | * |
|
2521 | * @return $this |
||
2522 | 565 | */ |
|
2523 | public function setPrintGridlines(bool $printGridLines): self |
||
2524 | 565 | { |
|
2525 | $this->printGridlines = $printGridLines; |
||
2526 | |||
2527 | return $this; |
||
2528 | } |
||
2529 | |||
2530 | 471 | /** |
|
2531 | * Show row and column headers? |
||
2532 | 471 | */ |
|
2533 | public function getShowRowColHeaders(): bool |
||
2534 | { |
||
2535 | return $this->showRowColHeaders; |
||
2536 | } |
||
2537 | |||
2538 | /** |
||
2539 | * Set show row and column headers. |
||
2540 | * |
||
2541 | * @param bool $showRowColHeaders Show row and column headers (true/false) |
||
2542 | 363 | * |
|
2543 | * @return $this |
||
2544 | 363 | */ |
|
2545 | public function setShowRowColHeaders(bool $showRowColHeaders): self |
||
2546 | 363 | { |
|
2547 | $this->showRowColHeaders = $showRowColHeaders; |
||
2548 | |||
2549 | return $this; |
||
2550 | } |
||
2551 | |||
2552 | 472 | /** |
|
2553 | * Show summary below? (Row/Column outlining). |
||
2554 | 472 | */ |
|
2555 | public function getShowSummaryBelow(): bool |
||
2556 | { |
||
2557 | return $this->showSummaryBelow; |
||
2558 | } |
||
2559 | |||
2560 | /** |
||
2561 | * Set show summary below. |
||
2562 | * |
||
2563 | * @param bool $showSummaryBelow Show summary below (true/false) |
||
2564 | 369 | * |
|
2565 | * @return $this |
||
2566 | 369 | */ |
|
2567 | public function setShowSummaryBelow(bool $showSummaryBelow): self |
||
2568 | 369 | { |
|
2569 | $this->showSummaryBelow = $showSummaryBelow; |
||
2570 | |||
2571 | return $this; |
||
2572 | } |
||
2573 | |||
2574 | 472 | /** |
|
2575 | * Show summary right? (Row/Column outlining). |
||
2576 | 472 | */ |
|
2577 | public function getShowSummaryRight(): bool |
||
2578 | { |
||
2579 | return $this->showSummaryRight; |
||
2580 | } |
||
2581 | |||
2582 | /** |
||
2583 | * Set show summary right. |
||
2584 | * |
||
2585 | * @param bool $showSummaryRight Show summary right (true/false) |
||
2586 | 369 | * |
|
2587 | * @return $this |
||
2588 | 369 | */ |
|
2589 | public function setShowSummaryRight(bool $showSummaryRight): self |
||
2590 | 369 | { |
|
2591 | $this->showSummaryRight = $showSummaryRight; |
||
2592 | |||
2593 | return $this; |
||
2594 | } |
||
2595 | |||
2596 | /** |
||
2597 | * Get comments. |
||
2598 | 1017 | * |
|
2599 | * @return Comment[] |
||
2600 | 1017 | */ |
|
2601 | public function getComments(): array |
||
2602 | { |
||
2603 | return $this->comments; |
||
2604 | } |
||
2605 | |||
2606 | /** |
||
2607 | * Set comments array for the entire sheet. |
||
2608 | * |
||
2609 | * @param Comment[] $comments |
||
2610 | 94 | * |
|
2611 | * @return $this |
||
2612 | 94 | */ |
|
2613 | public function setComments(array $comments): self |
||
2614 | 94 | { |
|
2615 | $this->comments = $comments; |
||
2616 | |||
2617 | return $this; |
||
2618 | } |
||
2619 | |||
2620 | /** |
||
2621 | * Remove comment from cell. |
||
2622 | * |
||
2623 | * @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5'; |
||
2624 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
2625 | 44 | * |
|
2626 | * @return $this |
||
2627 | 44 | */ |
|
2628 | public function removeComment(CellAddress|string|array $cellCoordinate): self |
||
2629 | 44 | { |
|
2630 | 1 | $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); |
|
2631 | 43 | ||
2632 | 1 | if (Coordinate::coordinateIsRange($cellAddress)) { |
|
2633 | 42 | throw new Exception('Cell coordinate string can not be a range of cells.'); |
|
2634 | 1 | } elseif (str_contains($cellAddress, '$')) { |
|
2635 | throw new Exception('Cell coordinate string must not be absolute.'); |
||
2636 | } elseif ($cellAddress == '') { |
||
2637 | 41 | throw new Exception('Cell coordinate can not be zero-length string.'); |
|
2638 | 2 | } |
|
2639 | // Check if we have a comment for this cell and delete it |
||
2640 | if (isset($this->comments[$cellAddress])) { |
||
2641 | 41 | unset($this->comments[$cellAddress]); |
|
2642 | } |
||
2643 | |||
2644 | return $this; |
||
2645 | } |
||
2646 | |||
2647 | /** |
||
2648 | * Get comment for cell. |
||
2649 | * |
||
2650 | 109 | * @param array{0: int, 1: int}|CellAddress|string $cellCoordinate Coordinate of the cell as a string, eg: 'C5'; |
|
2651 | * or as an array of [$columnIndex, $row] (e.g. [3, 5]), or a CellAddress object. |
||
2652 | 109 | */ |
|
2653 | public function getComment(CellAddress|string|array $cellCoordinate, bool $attachNew = true): Comment |
||
2654 | 109 | { |
|
2655 | 1 | $cellAddress = Functions::trimSheetFromCellReference(Validations::validateCellAddress($cellCoordinate)); |
|
2656 | 108 | ||
2657 | 1 | if (Coordinate::coordinateIsRange($cellAddress)) { |
|
2658 | 107 | throw new Exception('Cell coordinate string can not be a range of cells.'); |
|
2659 | 1 | } elseif (str_contains($cellAddress, '$')) { |
|
2660 | throw new Exception('Cell coordinate string must not be absolute.'); |
||
2661 | } elseif ($cellAddress == '') { |
||
2662 | throw new Exception('Cell coordinate can not be zero-length string.'); |
||
2663 | 106 | } |
|
2664 | 73 | ||
2665 | // Check if we already have a comment for this cell. |
||
2666 | if (isset($this->comments[$cellAddress])) { |
||
2667 | return $this->comments[$cellAddress]; |
||
2668 | 106 | } |
|
2669 | 106 | ||
2670 | 106 | // If not, create a new comment. |
|
2671 | $newComment = new Comment(); |
||
2672 | if ($attachNew) { |
||
2673 | 106 | $this->comments[$cellAddress] = $newComment; |
|
2674 | } |
||
2675 | |||
2676 | return $newComment; |
||
2677 | } |
||
2678 | |||
2679 | /** |
||
2680 | * Get active cell. |
||
2681 | 10099 | * |
|
2682 | * @return string Example: 'A1' |
||
2683 | 10099 | */ |
|
2684 | public function getActiveCell(): string |
||
2685 | { |
||
2686 | return $this->activeCell; |
||
2687 | } |
||
2688 | |||
2689 | 10142 | /** |
|
2690 | * Get selected cells. |
||
2691 | 10142 | */ |
|
2692 | public function getSelectedCells(): string |
||
2693 | { |
||
2694 | return $this->selectedCells; |
||
2695 | } |
||
2696 | |||
2697 | /** |
||
2698 | * Selected cell. |
||
2699 | * |
||
2700 | * @param string $coordinate Cell (i.e. A1) |
||
2701 | 36 | * |
|
2702 | * @return $this |
||
2703 | 36 | */ |
|
2704 | public function setSelectedCell(string $coordinate): static |
||
2705 | { |
||
2706 | return $this->setSelectedCells($coordinate); |
||
2707 | } |
||
2708 | |||
2709 | /** |
||
2710 | * Select a range of cells. |
||
2711 | * |
||
2712 | * @param AddressRange<CellAddress>|AddressRange<int>|AddressRange<string>|array{0: int, 1: int, 2: int, 3: int}|array{0: int, 1: int}|CellAddress|int|string $coordinate A simple string containing a Cell range like 'A1:E10' |
||
2713 | * or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]), |
||
2714 | * or a CellAddress or AddressRange object. |
||
2715 | 10122 | * |
|
2716 | * @return $this |
||
2717 | 10122 | */ |
|
2718 | 10122 | public function setSelectedCells(AddressRange|CellAddress|int|string|array $coordinate): static |
|
2719 | { |
||
2720 | 10122 | if (is_string($coordinate)) { |
|
2721 | $coordinate = Validations::definedNameToCoordinate($coordinate, $this); |
||
2722 | 10122 | } |
|
2723 | 484 | $coordinate = Validations::validateCellOrCellRange($coordinate); |
|
2724 | 484 | ||
2725 | if (Coordinate::coordinateIsRange($coordinate)) { |
||
2726 | 10091 | [$first] = Coordinate::splitRange($coordinate); |
|
2727 | $this->activeCell = $first[0]; |
||
2728 | 10122 | } else { |
|
2729 | 10122 | $this->activeCell = $coordinate; |
|
2730 | } |
||
2731 | 10122 | $this->selectedCells = $coordinate; |
|
2732 | $this->setSelectedCellsActivePane(); |
||
2733 | |||
2734 | 10123 | return $this; |
|
2735 | } |
||
2736 | 10123 | ||
2737 | 47 | private function setSelectedCellsActivePane(): void |
|
2738 | 47 | { |
|
2739 | 47 | if (!empty($this->freezePane)) { |
|
2740 | 26 | $coordinateC = Coordinate::indexesFromString($this->freezePane); |
|
2741 | 23 | $coordinateT = Coordinate::indexesFromString($this->activeCell); |
|
2742 | 3 | if ($coordinateC[0] === 1) { |
|
2743 | 21 | $activePane = ($coordinateT[1] <= $coordinateC[1]) ? 'topLeft' : 'bottomLeft'; |
|
2744 | 21 | } elseif ($coordinateC[1] === 1) { |
|
2745 | $activePane = ($coordinateT[0] <= $coordinateC[0]) ? 'topLeft' : 'topRight'; |
||
2746 | 10 | } elseif ($coordinateT[1] <= $coordinateC[1]) { |
|
2747 | $activePane = ($coordinateT[0] <= $coordinateC[0]) ? 'topLeft' : 'topRight'; |
||
2748 | 47 | } else { |
|
2749 | 47 | $activePane = ($coordinateT[0] <= $coordinateC[0]) ? 'bottomLeft' : 'bottomRight'; |
|
2750 | } |
||
2751 | $this->setActivePane($activePane); |
||
2752 | $this->panes[$activePane] = new Pane($activePane, $this->selectedCells, $this->activeCell); |
||
2753 | } |
||
2754 | } |
||
2755 | |||
2756 | 471 | /** |
|
2757 | * Get right-to-left. |
||
2758 | 471 | */ |
|
2759 | public function getRightToLeft(): bool |
||
2760 | { |
||
2761 | return $this->rightToLeft; |
||
2762 | } |
||
2763 | |||
2764 | /** |
||
2765 | * Set right-to-left. |
||
2766 | * |
||
2767 | * @param bool $value Right-to-left true/false |
||
2768 | 130 | * |
|
2769 | * @return $this |
||
2770 | 130 | */ |
|
2771 | public function setRightToLeft(bool $value): static |
||
2772 | 130 | { |
|
2773 | $this->rightToLeft = $value; |
||
2774 | |||
2775 | return $this; |
||
2776 | } |
||
2777 | |||
2778 | /** |
||
2779 | * Fill worksheet from values in array. |
||
2780 | * |
||
2781 | * @param array $source Source array |
||
2782 | * @param mixed $nullValue Value in source array that stands for blank cell |
||
2783 | * @param string $startCell Insert array starting from this cell address as the top left coordinate |
||
2784 | * @param bool $strictNullComparison Apply strict comparison when testing for null values in the array |
||
2785 | 744 | * |
|
2786 | * @return $this |
||
2787 | */ |
||
2788 | 744 | public function fromArray(array $source, mixed $nullValue = null, string $startCell = 'A1', bool $strictNullComparison = false): static |
|
2789 | 42 | { |
|
2790 | // Convert a 1-D array to 2-D (for ease of looping) |
||
2791 | if (!is_array(end($source))) { |
||
2792 | $source = [$source]; |
||
2793 | 744 | } |
|
2794 | |||
2795 | // start coordinate |
||
2796 | 744 | [$startColumn, $startRow] = Coordinate::coordinateFromString($startCell); |
|
2797 | 346 | ||
2798 | 346 | // Loop through $source |
|
2799 | 346 | if ($strictNullComparison) { |
|
2800 | 346 | foreach ($source as $rowData) { |
|
2801 | $currentColumn = $startColumn; |
||
2802 | 346 | foreach ($rowData as $cellValue) { |
|
2803 | if ($cellValue !== $nullValue) { |
||
2804 | 346 | // Set cell value |
|
2805 | $this->getCell($currentColumn . $startRow)->setValue($cellValue); |
||
2806 | 346 | } |
|
2807 | ++$currentColumn; |
||
2808 | } |
||
2809 | 399 | ++$startRow; |
|
2810 | 399 | } |
|
2811 | 399 | } else { |
|
2812 | 398 | foreach ($source as $rowData) { |
|
2813 | $currentColumn = $startColumn; |
||
2814 | 392 | foreach ($rowData as $cellValue) { |
|
2815 | if ($cellValue != $nullValue) { |
||
2816 | 398 | // Set cell value |
|
2817 | $this->getCell($currentColumn . $startRow)->setValue($cellValue); |
||
2818 | 399 | } |
|
2819 | ++$currentColumn; |
||
2820 | } |
||
2821 | ++$startRow; |
||
2822 | 744 | } |
|
2823 | } |
||
2824 | |||
2825 | return $this; |
||
2826 | } |
||
2827 | |||
2828 | /** |
||
2829 | * @param null|bool|float|int|RichText|string $nullValue value to use when null |
||
2830 | * |
||
2831 | 166 | * @throws Exception |
|
2832 | * @throws \PhpOffice\PhpSpreadsheet\Calculation\Exception |
||
2833 | 166 | */ |
|
2834 | protected function cellToArray(Cell $cell, bool $calculateFormulas, bool $formatData, mixed $nullValue): mixed |
||
2835 | 166 | { |
|
2836 | 166 | $returnValue = $nullValue; |
|
2837 | 4 | ||
2838 | if ($cell->getValue() !== null) { |
||
2839 | 166 | if ($cell->getValue() instanceof RichText) { |
|
2840 | $returnValue = $cell->getValue()->getPlainText(); |
||
2841 | } else { |
||
2842 | 166 | $returnValue = ($calculateFormulas) ? $cell->getCalculatedValue() : $cell->getValue(); |
|
2843 | 106 | } |
|
2844 | |||
2845 | 106 | if ($formatData) { |
|
2846 | 106 | $style = $this->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex()); |
|
2847 | 106 | /** @var null|bool|float|int|RichText|string */ |
|
2848 | 106 | $returnValuex = $returnValue; |
|
2849 | 106 | $returnValue = NumberFormat::toFormattedString( |
|
2850 | $returnValuex, |
||
2851 | $style->getNumberFormat()->getFormatCode() ?? NumberFormat::FORMAT_GENERAL |
||
2852 | ); |
||
2853 | 166 | } |
|
2854 | } |
||
2855 | |||
2856 | return $returnValue; |
||
2857 | } |
||
2858 | |||
2859 | /** |
||
2860 | * Create array from a range of cells. |
||
2861 | * |
||
2862 | * @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
||
2863 | * @param bool $calculateFormulas Should formulas be calculated? |
||
2864 | * @param bool $formatData Should formatting be applied to cell values? |
||
2865 | * @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
||
2866 | * True - Return rows and columns indexed by their actual row and column IDs |
||
2867 | 139 | * @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
2868 | * True - Don't return values for rows/columns that are defined as hidden. |
||
2869 | */ |
||
2870 | public function rangeToArray( |
||
2871 | string $range, |
||
2872 | mixed $nullValue = null, |
||
2873 | bool $calculateFormulas = true, |
||
2874 | bool $formatData = true, |
||
2875 | bool $returnCellRef = false, |
||
2876 | 139 | bool $ignoreHidden = false, |
|
2877 | bool $reduceArrays = false |
||
2878 | ): array { |
||
2879 | 139 | $returnValue = []; |
|
2880 | 139 | ||
2881 | // Loop through rows |
||
2882 | foreach ($this->rangeToArrayYieldRows($range, $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays) as $rowRef => $rowArray) { |
||
2883 | $returnValue[$rowRef] = $rowArray; |
||
2884 | 139 | } |
|
2885 | |||
2886 | // Return |
||
2887 | return $returnValue; |
||
2888 | } |
||
2889 | |||
2890 | /** |
||
2891 | * Create array from a range of cells, yielding each row in turn. |
||
2892 | * |
||
2893 | * @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
||
2894 | * @param bool $calculateFormulas Should formulas be calculated? |
||
2895 | * @param bool $formatData Should formatting be applied to cell values? |
||
2896 | * @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
||
2897 | * True - Return rows and columns indexed by their actual row and column IDs |
||
2898 | * @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
||
2899 | * True - Don't return values for rows/columns that are defined as hidden. |
||
2900 | 166 | * |
|
2901 | * @return Generator<array> |
||
2902 | */ |
||
2903 | public function rangeToArrayYieldRows( |
||
2904 | string $range, |
||
2905 | mixed $nullValue = null, |
||
2906 | bool $calculateFormulas = true, |
||
2907 | bool $formatData = true, |
||
2908 | bool $returnCellRef = false, |
||
2909 | 166 | bool $ignoreHidden = false, |
|
2910 | bool $reduceArrays = false |
||
2911 | ) { |
||
2912 | 166 | $range = Validations::validateCellOrCellRange($range); |
|
2913 | 166 | ||
2914 | 166 | // Identify the range that we need to extract from the worksheet |
|
2915 | 166 | [$rangeStart, $rangeEnd] = Coordinate::rangeBoundaries($range); |
|
2916 | 166 | $minCol = Coordinate::stringFromColumnIndex($rangeStart[0]); |
|
2917 | 166 | $minRow = $rangeStart[1]; |
|
2918 | 166 | $maxCol = Coordinate::stringFromColumnIndex($rangeEnd[0]); |
|
2919 | $maxRow = $rangeEnd[1]; |
||
2920 | 166 | $minColInt = $rangeStart[0]; |
|
2921 | $maxColInt = $rangeEnd[0]; |
||
2922 | 166 | ||
2923 | 166 | ++$maxCol; |
|
2924 | 166 | /** @var array<string, bool> */ |
|
2925 | $hiddenColumns = []; |
||
2926 | 166 | $nullRow = $this->buildNullRow($nullValue, $minCol, $maxCol, $returnCellRef, $ignoreHidden, $hiddenColumns); |
|
2927 | 166 | $hideColumns = !empty($hiddenColumns); |
|
2928 | 166 | ||
2929 | $keys = $this->cellCollection->getSortedCoordinatesInt(); |
||
2930 | 166 | $keyIndex = 0; |
|
2931 | 166 | $keysCount = count($keys); |
|
2932 | 4 | // Loop through rows |
|
2933 | for ($row = $minRow; $row <= $maxRow; ++$row) { |
||
2934 | 166 | if (($ignoreHidden === true) && ($this->isRowVisible($row) === false)) { |
|
2935 | 166 | continue; |
|
2936 | } |
||
2937 | 166 | $rowRef = $returnCellRef ? $row : ($row - $minRow); |
|
2938 | 166 | $returnValue = $nullRow; |
|
2939 | 166 | ||
2940 | 49 | $index = ($row - 1) * AddressRange::MAX_COLUMN_INT + 1; |
|
2941 | $indexPlus = $index + AddressRange::MAX_COLUMN_INT - 1; |
||
2942 | 166 | while ($keyIndex < $keysCount && $keys[$keyIndex] < $index) { |
|
2943 | 166 | ++$keyIndex; |
|
2944 | 166 | } |
|
2945 | 166 | while ($keyIndex < $keysCount && $keys[$keyIndex] <= $indexPlus) { |
|
2946 | 166 | $key = $keys[$keyIndex]; |
|
2947 | 166 | $thisRow = intdiv($key - 1, AddressRange::MAX_COLUMN_INT) + 1; |
|
2948 | 166 | $thisCol = ($key % AddressRange::MAX_COLUMN_INT) ?: AddressRange::MAX_COLUMN_INT; |
|
2949 | 166 | if ($thisCol >= $minColInt && $thisCol <= $maxColInt) { |
|
2950 | 166 | $col = Coordinate::stringFromColumnIndex($thisCol); |
|
2951 | 166 | if ($hideColumns === false || !isset($hiddenColumns[$col])) { |
|
2952 | 166 | $columnRef = $returnCellRef ? $col : ($thisCol - $minColInt); |
|
2953 | 166 | $cell = $this->cellCollection->get("{$col}{$thisRow}"); |
|
2954 | 21 | if ($cell !== null) { |
|
2955 | 19 | $value = $this->cellToArray($cell, $calculateFormulas, $formatData, $nullValue); |
|
2956 | if ($reduceArrays) { |
||
2957 | while (is_array($value)) { |
||
2958 | 166 | $value = array_shift($value); |
|
2959 | 166 | } |
|
2960 | } |
||
2961 | if ($value !== $nullValue) { |
||
2962 | $returnValue[$columnRef] = $value; |
||
2963 | } |
||
2964 | 166 | } |
|
2965 | } |
||
2966 | } |
||
2967 | 166 | ++$keyIndex; |
|
2968 | } |
||
2969 | |||
2970 | yield $rowRef => $returnValue; |
||
2971 | } |
||
2972 | } |
||
2973 | |||
2974 | /** |
||
2975 | * Prepare a row data filled with null values to deduplicate the memory areas for empty rows. |
||
2976 | * |
||
2977 | * @param mixed $nullValue Value returned in the array entry if a cell doesn't exist |
||
2978 | * @param string $minCol Start column of the range |
||
2979 | * @param string $maxCol End column of the range |
||
2980 | * @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
||
2981 | * True - Return rows and columns indexed by their actual row and column IDs |
||
2982 | * @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
||
2983 | 166 | * True - Don't return values for rows/columns that are defined as hidden. |
|
2984 | * @param array<string, bool> $hiddenColumns |
||
2985 | */ |
||
2986 | private function buildNullRow( |
||
2987 | mixed $nullValue, |
||
2988 | string $minCol, |
||
2989 | string $maxCol, |
||
2990 | bool $returnCellRef, |
||
2991 | 166 | bool $ignoreHidden, |
|
2992 | 166 | array &$hiddenColumns |
|
2993 | 166 | ): array { |
|
2994 | 166 | $nullRow = []; |
|
2995 | 2 | $c = -1; |
|
2996 | for ($col = $minCol; $col !== $maxCol; ++$col) { |
||
2997 | 166 | if ($ignoreHidden === true && $this->columnDimensionExists($col) && $this->getColumnDimension($col)->getVisible() === false) { |
|
2998 | 166 | $hiddenColumns[$col] = true; |
|
2999 | } else { |
||
3000 | $columnRef = $returnCellRef ? $col : ++$c; |
||
3001 | $nullRow[$columnRef] = $nullValue; |
||
3002 | 166 | } |
|
3003 | } |
||
3004 | |||
3005 | 18 | return $nullRow; |
|
3006 | } |
||
3007 | 18 | ||
3008 | 18 | private function validateNamedRange(string $definedName, bool $returnNullIfInvalid = false): ?DefinedName |
|
3009 | 6 | { |
|
3010 | 5 | $namedRange = DefinedName::resolveName($definedName, $this); |
|
3011 | if ($namedRange === null) { |
||
3012 | if ($returnNullIfInvalid) { |
||
3013 | 1 | return null; |
|
3014 | } |
||
3015 | |||
3016 | 12 | throw new Exception('Named Range ' . $definedName . ' does not exist.'); |
|
3017 | } |
||
3018 | |||
3019 | if ($namedRange->isFormula()) { |
||
3020 | if ($returnNullIfInvalid) { |
||
3021 | return null; |
||
3022 | } |
||
3023 | |||
3024 | 12 | throw new Exception('Defined Named ' . $definedName . ' is a formula, not a range or cell.'); |
|
3025 | 2 | } |
|
3026 | 2 | ||
3027 | if ($namedRange->getLocalOnly()) { |
||
3028 | $worksheet = $namedRange->getWorksheet(); |
||
3029 | if ($worksheet === null || $this->hash !== $worksheet->getHashInt()) { |
||
3030 | if ($returnNullIfInvalid) { |
||
3031 | return null; |
||
3032 | } |
||
3033 | |||
3034 | throw new Exception( |
||
3035 | 'Named range ' . $definedName . ' is not accessible from within sheet ' . $this->getTitle() |
||
3036 | ); |
||
3037 | 12 | } |
|
3038 | } |
||
3039 | |||
3040 | return $namedRange; |
||
3041 | } |
||
3042 | |||
3043 | /** |
||
3044 | * Create array from a range of cells. |
||
3045 | * |
||
3046 | * @param string $definedName The Named Range that should be returned |
||
3047 | * @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
||
3048 | * @param bool $calculateFormulas Should formulas be calculated? |
||
3049 | * @param bool $formatData Should formatting be applied to cell values? |
||
3050 | * @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
||
3051 | * True - Return rows and columns indexed by their actual row and column IDs |
||
3052 | 2 | * @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3053 | * True - Don't return values for rows/columns that are defined as hidden. |
||
3054 | */ |
||
3055 | public function namedRangeToArray( |
||
3056 | string $definedName, |
||
3057 | mixed $nullValue = null, |
||
3058 | bool $calculateFormulas = true, |
||
3059 | bool $formatData = true, |
||
3060 | bool $returnCellRef = false, |
||
3061 | 2 | bool $ignoreHidden = false, |
|
3062 | 2 | bool $reduceArrays = false |
|
3063 | 1 | ): array { |
|
3064 | 1 | $retVal = []; |
|
3065 | 1 | $namedRange = $this->validateNamedRange($definedName); |
|
3066 | 1 | if ($namedRange !== null) { |
|
3067 | 1 | $cellRange = ltrim(substr($namedRange->getValue(), (int) strrpos($namedRange->getValue(), '!')), '!'); |
|
3068 | 1 | $cellRange = str_replace('$', '', $cellRange); |
|
3069 | $workSheet = $namedRange->getWorksheet(); |
||
3070 | if ($workSheet !== null) { |
||
3071 | $retVal = $workSheet->rangeToArray($cellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays); |
||
3072 | 1 | } |
|
3073 | } |
||
3074 | |||
3075 | return $retVal; |
||
3076 | } |
||
3077 | |||
3078 | /** |
||
3079 | * Create array from worksheet. |
||
3080 | * |
||
3081 | * @param null|bool|float|int|RichText|string $nullValue Value returned in the array entry if a cell doesn't exist |
||
3082 | * @param bool $calculateFormulas Should formulas be calculated? |
||
3083 | * @param bool $formatData Should formatting be applied to cell values? |
||
3084 | * @param bool $returnCellRef False - Return a simple array of rows and columns indexed by number counting from zero |
||
3085 | * True - Return rows and columns indexed by their actual row and column IDs |
||
3086 | 72 | * @param bool $ignoreHidden False - Return values for rows/columns even if they are defined as hidden. |
|
3087 | * True - Don't return values for rows/columns that are defined as hidden. |
||
3088 | */ |
||
3089 | public function toArray( |
||
3090 | mixed $nullValue = null, |
||
3091 | bool $calculateFormulas = true, |
||
3092 | bool $formatData = true, |
||
3093 | bool $returnCellRef = false, |
||
3094 | bool $ignoreHidden = false, |
||
3095 | 72 | bool $reduceArrays = false |
|
3096 | 72 | ): array { |
|
3097 | // Garbage collect... |
||
3098 | $this->garbageCollect(); |
||
3099 | 72 | $this->calculateArrays($calculateFormulas); |
|
3100 | 72 | ||
3101 | // Identify the range that we need to extract from the worksheet |
||
3102 | $maxCol = $this->getHighestColumn(); |
||
3103 | 72 | $maxRow = $this->getHighestRow(); |
|
3104 | |||
3105 | // Return |
||
3106 | return $this->rangeToArray("A1:{$maxCol}{$maxRow}", $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays); |
||
3107 | } |
||
3108 | |||
3109 | /** |
||
3110 | * Get row iterator. |
||
3111 | * |
||
3112 | 86 | * @param int $startRow The row number at which to start iterating |
|
3113 | * @param ?int $endRow The row number at which to stop iterating |
||
3114 | 86 | */ |
|
3115 | public function getRowIterator(int $startRow = 1, ?int $endRow = null): RowIterator |
||
3116 | { |
||
3117 | return new RowIterator($this, $startRow, $endRow); |
||
3118 | } |
||
3119 | |||
3120 | /** |
||
3121 | * Get column iterator. |
||
3122 | * |
||
3123 | 25 | * @param string $startColumn The column address at which to start iterating |
|
3124 | * @param ?string $endColumn The column address at which to stop iterating |
||
3125 | 25 | */ |
|
3126 | public function getColumnIterator(string $startColumn = 'A', ?string $endColumn = null): ColumnIterator |
||
3127 | { |
||
3128 | return new ColumnIterator($this, $startColumn, $endColumn); |
||
3129 | } |
||
3130 | |||
3131 | /** |
||
3132 | * Run PhpSpreadsheet garbage collector. |
||
3133 | 1101 | * |
|
3134 | * @return $this |
||
3135 | */ |
||
3136 | 1101 | public function garbageCollect(): static |
|
3166 | } |
||
3167 | 10436 | ||
3168 | public function getHashInt(): int |
||
3169 | { |
||
3170 | return $this->hash; |
||
3171 | } |
||
3172 | |||
3173 | /** |
||
3174 | * Extract worksheet title from range. |
||
3175 | * |
||
3176 | * Example: extractSheetTitle("testSheet!A1") ==> 'A1' |
||
3177 | * Example: extractSheetTitle("testSheet!A1:C3") ==> 'A1:C3' |
||
3178 | * Example: extractSheetTitle("'testSheet 1'!A1", true) ==> ['testSheet 1', 'A1']; |
||
3179 | * Example: extractSheetTitle("'testSheet 1'!A1:C3", true) ==> ['testSheet 1', 'A1:C3']; |
||
3180 | * Example: extractSheetTitle("A1", true) ==> ['', 'A1']; |
||
3181 | * Example: extractSheetTitle("A1:C3", true) ==> ['', 'A1:C3'] |
||
3182 | * |
||
3183 | * @param ?string $range Range to extract title from |
||
3184 | * @param bool $returnRange Return range? (see example) |
||
3185 | 10325 | * |
|
3186 | * @return ($range is non-empty-string ? ($returnRange is true ? array{0: string, 1: string} : string) : ($returnRange is true ? array{0: null, 1: null} : null)) |
||
3187 | 10325 | */ |
|
3188 | 13 | public static function extractSheetTitle(?string $range, bool $returnRange = false, bool $unapostrophize = false): array|null|string |
|
3189 | { |
||
3190 | if (empty($range)) { |
||
3191 | return $returnRange ? [null, null] : null; |
||
3192 | 10323 | } |
|
3193 | 10297 | ||
3194 | // Sheet title included? |
||
3195 | if (($sep = strrpos($range, '!')) === false) { |
||
3196 | 1334 | return $returnRange ? ['', $range] : ''; |
|
3197 | 1334 | } |
|
3198 | |||
3199 | if ($returnRange) { |
||
3200 | 7 | $title = substr($range, 0, $sep); |
|
3201 | if ($unapostrophize) { |
||
3202 | $title = self::unApostrophizeTitle($title); |
||
3203 | } |
||
3204 | |||
3205 | return [$title, substr($range, $sep + 1)]; |
||
3206 | } |
||
3207 | |||
3208 | 90 | return substr($range, $sep + 1); |
|
3209 | } |
||
3210 | |||
3211 | 90 | public static function unApostrophizeTitle(?string $title): string |
|
3212 | 42 | { |
|
3213 | $title ??= ''; |
||
3214 | if ($title[0] === "'" && substr($title, -1) === "'") { |
||
3215 | $title = str_replace("''", "'", substr($title, 1, -1)); |
||
3216 | 90 | } |
|
3217 | |||
3218 | 90 | return $title; |
|
3219 | } |
||
3220 | |||
3221 | /** |
||
3222 | * Get hyperlink. |
||
3223 | * |
||
3224 | * @param string $cellCoordinate Cell coordinate to get hyperlink for, eg: 'A1' |
||
3225 | */ |
||
3226 | public function getHyperlink(string $cellCoordinate): Hyperlink |
||
3227 | { |
||
3228 | 42 | // return hyperlink if we already have one |
|
3229 | if (isset($this->hyperlinkCollection[$cellCoordinate])) { |
||
3230 | 42 | return $this->hyperlinkCollection[$cellCoordinate]; |
|
3231 | 41 | } |
|
3232 | |||
3233 | 21 | // else create hyperlink |
|
3234 | $this->hyperlinkCollection[$cellCoordinate] = new Hyperlink(); |
||
3235 | |||
3236 | 42 | return $this->hyperlinkCollection[$cellCoordinate]; |
|
3237 | } |
||
3238 | |||
3239 | /** |
||
3240 | * Set hyperlink. |
||
3241 | * |
||
3242 | * @param string $cellCoordinate Cell coordinate to insert hyperlink, eg: 'A1' |
||
3243 | * |
||
3244 | 530 | * @return $this |
|
3245 | */ |
||
3246 | 530 | public function setHyperlink(string $cellCoordinate, ?Hyperlink $hyperlink = null): static |
|
3247 | { |
||
3248 | if ($hyperlink === null) { |
||
3249 | unset($this->hyperlinkCollection[$cellCoordinate]); |
||
3250 | } else { |
||
3251 | $this->hyperlinkCollection[$cellCoordinate] = $hyperlink; |
||
3252 | } |
||
3253 | |||
3254 | 549 | return $this; |
|
3255 | } |
||
3256 | 549 | ||
3257 | /** |
||
3258 | * Hyperlink at a specific coordinate exists? |
||
3259 | * |
||
3260 | * @param string $coordinate eg: 'A1' |
||
3261 | */ |
||
3262 | public function hyperlinkExists(string $coordinate): bool |
||
3263 | { |
||
3264 | 35 | return isset($this->hyperlinkCollection[$coordinate]); |
|
3265 | } |
||
3266 | |||
3267 | 35 | /** |
|
3268 | 26 | * Get collection of hyperlinks. |
|
3269 | * |
||
3270 | * @return Hyperlink[] |
||
3271 | */ |
||
3272 | 28 | public function getHyperlinkCollection(): array |
|
3273 | 12 | { |
|
3274 | 12 | return $this->hyperlinkCollection; |
|
3275 | 12 | } |
|
3276 | 1 | ||
3277 | /** |
||
3278 | 12 | * Get data validation. |
|
3279 | 9 | * |
|
3280 | 9 | * @param string $cellCoordinate Cell coordinate to get data validation for, eg: 'A1' |
|
3281 | */ |
||
3282 | public function getDataValidation(string $cellCoordinate): DataValidation |
||
3283 | { |
||
3284 | // return data validation if we already have one |
||
3285 | if (isset($this->dataValidationCollection[$cellCoordinate])) { |
||
3286 | return $this->dataValidationCollection[$cellCoordinate]; |
||
3287 | 20 | } |
|
3288 | 20 | ||
3289 | 20 | // or if cell is part of a data validation range |
|
3290 | foreach ($this->dataValidationCollection as $key => $dataValidation) { |
||
3291 | 20 | $keyParts = explode(' ', $key); |
|
3292 | foreach ($keyParts as $keyPart) { |
||
3293 | if ($keyPart === $cellCoordinate) { |
||
3294 | return $dataValidation; |
||
3295 | } |
||
3296 | if (str_contains($keyPart, ':')) { |
||
3297 | if (Coordinate::coordinateIsInsideRange($keyPart, $cellCoordinate)) { |
||
3298 | return $dataValidation; |
||
3299 | } |
||
3300 | } |
||
3301 | 77 | } |
|
3302 | } |
||
3303 | 77 | ||
3304 | 46 | // else create data validation |
|
3305 | $dataValidation = new DataValidation(); |
||
3306 | 38 | $dataValidation->setSqref($cellCoordinate); |
|
3307 | 38 | $this->dataValidationCollection[$cellCoordinate] = $dataValidation; |
|
3308 | |||
3309 | return $dataValidation; |
||
3310 | 77 | } |
|
3311 | |||
3312 | /** |
||
3313 | * Set data validation. |
||
3314 | * |
||
3315 | * @param string $cellCoordinate Cell coordinate to insert data validation, eg: 'A1' |
||
3316 | * |
||
3317 | * @return $this |
||
3318 | 25 | */ |
|
3319 | public function setDataValidation(string $cellCoordinate, ?DataValidation $dataValidation = null): static |
||
3320 | 25 | { |
|
3321 | 23 | if ($dataValidation === null) { |
|
3322 | unset($this->dataValidationCollection[$cellCoordinate]); |
||
3323 | 8 | } else { |
|
3324 | 7 | $dataValidation->setSqref($cellCoordinate); |
|
3325 | 7 | $this->dataValidationCollection[$cellCoordinate] = $dataValidation; |
|
3326 | 7 | } |
|
3327 | 1 | ||
3328 | return $this; |
||
3329 | 7 | } |
|
3330 | 2 | ||
3331 | 2 | /** |
|
3332 | * Data validation at a specific coordinate exists? |
||
3333 | * |
||
3334 | * @param string $coordinate eg: 'A1' |
||
3335 | */ |
||
3336 | public function dataValidationExists(string $coordinate): bool |
||
3337 | 6 | { |
|
3338 | if (isset($this->dataValidationCollection[$coordinate])) { |
||
3339 | return true; |
||
3340 | } |
||
3341 | foreach ($this->dataValidationCollection as $key => $dataValidation) { |
||
3342 | $keyParts = explode(' ', $key); |
||
3343 | foreach ($keyParts as $keyPart) { |
||
3344 | if ($keyPart === $coordinate) { |
||
3345 | 550 | return true; |
|
3346 | } |
||
3347 | 550 | if (str_contains($keyPart, ':')) { |
|
3348 | 550 | if (Coordinate::coordinateIsInsideRange($keyPart, $coordinate)) { |
|
3349 | 550 | return true; |
|
3350 | 27 | } |
|
3351 | 15 | } |
|
3352 | } |
||
3353 | 22 | } |
|
3354 | |||
3355 | return false; |
||
3356 | } |
||
3357 | 550 | ||
3358 | /** |
||
3359 | * Get collection of data validations. |
||
3360 | * |
||
3361 | * @return DataValidation[] |
||
3362 | */ |
||
3363 | public function getDataValidationCollection(): array |
||
3364 | { |
||
3365 | $collectionCells = []; |
||
3366 | $collectionRanges = []; |
||
3367 | foreach ($this->dataValidationCollection as $key => $dataValidation) { |
||
3368 | if (preg_match('/[: ]/', $key) === 1) { |
||
3369 | $collectionRanges[$key] = $dataValidation; |
||
3370 | } else { |
||
3371 | $collectionCells[$key] = $dataValidation; |
||
3372 | } |
||
3373 | } |
||
3374 | |||
3375 | return array_merge($collectionCells, $collectionRanges); |
||
3376 | } |
||
3377 | |||
3378 | /** |
||
3379 | * Accepts a range, returning it as a range that falls within the current highest row and column of the worksheet. |
||
3380 | * |
||
3381 | * @return string Adjusted range value |
||
3382 | */ |
||
3383 | public function shrinkRangeToFit(string $range): string |
||
3384 | { |
||
3385 | $maxCol = $this->getHighestColumn(); |
||
3386 | $maxRow = $this->getHighestRow(); |
||
3387 | $maxCol = Coordinate::columnIndexFromString($maxCol); |
||
3388 | |||
3389 | $rangeBlocks = explode(' ', $range); |
||
3390 | foreach ($rangeBlocks as &$rangeSet) { |
||
3391 | $rangeBoundaries = Coordinate::getRangeBoundaries($rangeSet); |
||
3392 | |||
3393 | if (Coordinate::columnIndexFromString($rangeBoundaries[0][0]) > $maxCol) { |
||
3394 | $rangeBoundaries[0][0] = Coordinate::stringFromColumnIndex($maxCol); |
||
3395 | } |
||
3396 | if ($rangeBoundaries[0][1] > $maxRow) { |
||
3397 | 23 | $rangeBoundaries[0][1] = $maxRow; |
|
3398 | } |
||
3399 | 23 | if (Coordinate::columnIndexFromString($rangeBoundaries[1][0]) > $maxCol) { |
|
3400 | 23 | $rangeBoundaries[1][0] = Coordinate::stringFromColumnIndex($maxCol); |
|
3401 | } |
||
3402 | if ($rangeBoundaries[1][1] > $maxRow) { |
||
3403 | 23 | $rangeBoundaries[1][1] = $maxRow; |
|
3404 | } |
||
3405 | $rangeSet = $rangeBoundaries[0][0] . $rangeBoundaries[0][1] . ':' . $rangeBoundaries[1][0] . $rangeBoundaries[1][1]; |
||
3406 | } |
||
3407 | unset($rangeSet); |
||
3408 | |||
3409 | return implode(' ', $rangeBlocks); |
||
3410 | } |
||
3411 | 1 | ||
3412 | /** |
||
3413 | 1 | * Get tab color. |
|
3414 | */ |
||
3415 | 1 | public function getTabColor(): Color |
|
3416 | { |
||
3417 | if ($this->tabColor === null) { |
||
3418 | $this->tabColor = new Color(); |
||
3419 | } |
||
3420 | |||
3421 | 473 | return $this->tabColor; |
|
3422 | } |
||
3423 | 473 | ||
3424 | /** |
||
3425 | * Reset tab color. |
||
3426 | * |
||
3427 | * @return $this |
||
3428 | */ |
||
3429 | public function resetTabColor(): static |
||
3430 | { |
||
3431 | $this->tabColor = null; |
||
3432 | |||
3433 | return $this; |
||
3434 | } |
||
3435 | |||
3436 | /** |
||
3437 | * Tab color set? |
||
3438 | */ |
||
3439 | public function isTabColorSet(): bool |
||
3440 | { |
||
3441 | return $this->tabColor !== null; |
||
3442 | } |
||
3443 | |||
3444 | /** |
||
3445 | * Copy worksheet (!= clone!). |
||
3446 | */ |
||
3447 | public function copy(): static |
||
3448 | { |
||
3449 | return clone $this; |
||
3450 | } |
||
3451 | 9 | ||
3452 | /** |
||
3453 | * Returns a boolean true if the specified row contains no cells. By default, this means that no cell records |
||
3454 | 9 | * exist in the collection for this row. false will be returned otherwise. |
|
3455 | 8 | * This rule can be modified by passing a $definitionOfEmptyFlags value: |
|
3456 | 8 | * 1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value |
|
3457 | 1 | * cells, then the row will be considered empty. |
|
3458 | 1 | * 2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty |
|
3459 | * string value cells, then the row will be considered empty. |
||
3460 | * 3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
||
3461 | 8 | * If the only cells in the collection are null value or empty string value cells, then the row |
|
3462 | * will be considered empty. |
||
3463 | * |
||
3464 | * @param int $definitionOfEmptyFlags |
||
3465 | * Possible Flag Values are: |
||
3466 | * CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL |
||
3467 | * CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
||
3468 | */ |
||
3469 | public function isEmptyRow(int $rowId, int $definitionOfEmptyFlags = 0): bool |
||
3470 | { |
||
3471 | try { |
||
3472 | $iterator = new RowIterator($this, $rowId, $rowId); |
||
3473 | $iterator->seek($rowId); |
||
3474 | $row = $iterator->current(); |
||
3475 | } catch (Exception) { |
||
3476 | return true; |
||
3477 | } |
||
3478 | |||
3479 | return $row->isEmpty($definitionOfEmptyFlags); |
||
3480 | } |
||
3481 | 9 | ||
3482 | /** |
||
3483 | * Returns a boolean true if the specified column contains no cells. By default, this means that no cell records |
||
3484 | 9 | * exist in the collection for this column. false will be returned otherwise. |
|
3485 | 8 | * This rule can be modified by passing a $definitionOfEmptyFlags value: |
|
3486 | 8 | * 1 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL If the only cells in the collection are null value |
|
3487 | 1 | * cells, then the column will be considered empty. |
|
3488 | 1 | * 2 - CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL If the only cells in the collection are empty |
|
3489 | * string value cells, then the column will be considered empty. |
||
3490 | * 3 - CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
||
3491 | 8 | * If the only cells in the collection are null value or empty string value cells, then the column |
|
3492 | * will be considered empty. |
||
3493 | * |
||
3494 | * @param int $definitionOfEmptyFlags |
||
3495 | * Possible Flag Values are: |
||
3496 | * CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL |
||
3497 | 15 | * CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL |
|
3498 | */ |
||
3499 | public function isEmptyColumn(string $columnId, int $definitionOfEmptyFlags = 0): bool |
||
3500 | 15 | { |
|
3501 | 15 | try { |
|
3502 | 15 | $iterator = new ColumnIterator($this, $columnId, $columnId); |
|
3503 | $iterator->seek($columnId); |
||
3504 | $column = $iterator->current(); |
||
3505 | 15 | } catch (Exception) { |
|
3506 | 15 | return true; |
|
3507 | 15 | } |
|
3508 | 15 | ||
3509 | 15 | return $column->isEmpty($definitionOfEmptyFlags); |
|
3510 | 15 | } |
|
3511 | 15 | ||
3512 | 15 | /** |
|
3513 | 4 | * Implement PHP __clone to create a deep clone, not just a shallow copy. |
|
3514 | 4 | */ |
|
3515 | public function __clone() |
||
3516 | 15 | { |
|
3517 | 15 | // @phpstan-ignore-next-line |
|
3518 | 15 | foreach ($this as $key => $val) { |
|
3519 | 15 | if ($key == 'parent') { |
|
3520 | 1 | continue; |
|
3521 | 1 | } |
|
3522 | 1 | ||
3523 | if (is_object($val) || (is_array($val))) { |
||
3524 | 15 | if ($key == 'cellCollection') { |
|
3525 | 15 | $newCollection = $this->cellCollection->cloneCellCollection($this); |
|
3526 | 15 | $this->cellCollection = $newCollection; |
|
3527 | 15 | } elseif ($key == 'drawingCollection') { |
|
3528 | 5 | $currentCollection = $this->drawingCollection; |
|
3529 | 5 | $this->drawingCollection = new ArrayObject(); |
|
3530 | foreach ($currentCollection as $item) { |
||
3531 | 15 | $newDrawing = clone $item; |
|
3532 | 15 | $newDrawing->setWorksheet($this); |
|
3533 | 15 | } |
|
3534 | 15 | } elseif ($key == 'tableCollection') { |
|
3535 | $currentCollection = $this->tableCollection; |
||
3536 | 15 | $this->tableCollection = new ArrayObject(); |
|
3537 | foreach ($currentCollection as $item) { |
||
3538 | $newTable = clone $item; |
||
3539 | $newTable->setName($item->getName() . 'clone'); |
||
3540 | 15 | $this->addTable($newTable); |
|
3541 | } |
||
3542 | } elseif ($key == 'chartCollection') { |
||
3543 | $currentCollection = $this->chartCollection; |
||
3544 | $this->chartCollection = new ArrayObject(); |
||
3545 | foreach ($currentCollection as $item) { |
||
3546 | $newChart = clone $item; |
||
3547 | $this->addChart($newChart); |
||
3548 | } |
||
3549 | } elseif (($key == 'autoFilter') && ($this->autoFilter instanceof AutoFilter)) { |
||
3550 | $newAutoFilter = clone $this->autoFilter; |
||
3551 | $this->autoFilter = $newAutoFilter; |
||
3552 | $this->autoFilter->setParent($this); |
||
3553 | 10473 | } else { |
|
3554 | $this->{$key} = unserialize(serialize($val)); |
||
3555 | } |
||
3556 | 10473 | } |
|
3557 | } |
||
3558 | $this->hash = spl_object_id($this); |
||
3559 | } |
||
3560 | 10473 | ||
3561 | 10473 | /** |
|
3562 | * Define the code name of the sheet. |
||
3563 | * |
||
3564 | * @param string $codeName Same rule as Title minus space not allowed (but, like Excel, change |
||
3565 | 10473 | * silently space to underscore) |
|
3566 | * @param bool $validate False to skip validation of new title. WARNING: This should only be set |
||
3567 | * at parse time (by Readers), where titles can be assumed to be valid. |
||
3568 | * |
||
3569 | 10473 | * @return $this |
|
3570 | */ |
||
3571 | 10434 | public function setCodeName(string $codeName, bool $validate = true): static |
|
3572 | { |
||
3573 | // Is this a 'rename' or not? |
||
3574 | 674 | if ($this->getCodeName() == $codeName) { |
|
3575 | return $this; |
||
3576 | } |
||
3577 | 674 | ||
3578 | 674 | if ($validate) { |
|
3579 | 275 | $codeName = str_replace(' ', '_', $codeName); //Excel does this automatically without flinching, we are doing the same |
|
3580 | 275 | ||
3581 | 2 | // Syntax check |
|
3582 | // throw an exception if not valid |
||
3583 | self::checkSheetCodeName($codeName); |
||
3584 | 275 | ||
3585 | // We use the same code that setTitle to find a valid codeName else not using a space (Excel don't like) but a '_' |
||
3586 | |||
3587 | if ($this->parent !== null) { |
||
3588 | // Is there already such sheet name? |
||
3589 | if ($this->parent->sheetCodeNameExists($codeName)) { |
||
3590 | // Use name, but append with lowest possible integer |
||
3591 | 674 | ||
3592 | if (StringHelper::countCharacters($codeName) > 29) { |
||
3593 | $codeName = StringHelper::substring($codeName, 0, 29); |
||
3594 | } |
||
3595 | $i = 1; |
||
3596 | 10473 | while ($this->getParentOrThrow()->sheetCodeNameExists($codeName . '_' . $i)) { |
|
3597 | ++$i; |
||
3598 | 10473 | if ($i == 10) { |
|
3599 | if (StringHelper::countCharacters($codeName) > 28) { |
||
3600 | $codeName = StringHelper::substring($codeName, 0, 28); |
||
3601 | } |
||
3602 | } elseif ($i == 100) { |
||
3603 | if (StringHelper::countCharacters($codeName) > 27) { |
||
3604 | 10473 | $codeName = StringHelper::substring($codeName, 0, 27); |
|
3605 | } |
||
3606 | 10473 | } |
|
3607 | } |
||
3608 | |||
3609 | $codeName .= '_' . $i; // ok, we have a valid name |
||
3610 | } |
||
3611 | } |
||
3612 | 2 | } |
|
3613 | |||
3614 | 2 | $this->codeName = $codeName; |
|
3615 | |||
3616 | return $this; |
||
3617 | 4 | } |
|
3618 | |||
3619 | 4 | /** |
|
3620 | * Return the code name of the sheet. |
||
3621 | */ |
||
3622 | 119 | public function getCodeName(): ?string |
|
3623 | { |
||
3624 | 119 | return $this->codeName; |
|
3625 | } |
||
3626 | |||
3627 | /** |
||
3628 | * Sheet has a code name ? |
||
3629 | */ |
||
3630 | 1 | public function hasCodeName(): bool |
|
3631 | { |
||
3632 | 1 | return $this->codeName !== null; |
|
3633 | 1 | } |
|
3634 | |||
3635 | 1 | public static function nameRequiresQuotes(string $sheetName): bool |
|
3638 | 1 | } |
|
3639 | 1 | ||
3640 | 1 | public function isRowVisible(int $row): bool |
|
3641 | 1 | { |
|
3642 | return !$this->rowDimensionExists($row) || $this->getRowDimension($row)->getVisible(); |
||
3643 | } |
||
3644 | |||
3645 | /** |
||
3646 | * Same as Cell->isLocked, but without creating cell if it doesn't exist. |
||
3647 | */ |
||
3648 | public function isCellLocked(string $coordinate): bool |
||
3649 | { |
||
3650 | 1 | if ($this->getProtection()->getsheet() !== true) { |
|
3651 | return false; |
||
3652 | 1 | } |
|
3653 | 1 | if ($this->cellExists($coordinate)) { |
|
3654 | return $this->getCell($coordinate)->isLocked(); |
||
3655 | } |
||
3656 | $spreadsheet = $this->parent; |
||
3657 | $xfIndex = $this->getXfIndex($coordinate); |
||
3658 | 1 | if ($spreadsheet === null || $xfIndex === null) { |
|
3659 | return true; |
||
3660 | } |
||
3661 | 1 | ||
3662 | return $spreadsheet->getCellXfByIndex($xfIndex)->getProtection()->getLocked() !== StyleProtection::PROTECTION_UNPROTECTED; |
||
3663 | 1 | } |
|
3664 | 1 | ||
3665 | 1 | /** |
|
3666 | 1 | * Same as Cell->isHiddenOnFormulaBar, but without creating cell if it doesn't exist. |
|
3667 | */ |
||
3668 | public function isCellHiddenOnFormulaBar(string $coordinate): bool |
||
3669 | 1 | { |
|
3670 | if ($this->cellExists($coordinate)) { |
||
3671 | return $this->getCell($coordinate)->isHiddenOnFormulaBar(); |
||
3672 | } |
||
3673 | 1 | ||
3674 | // cell doesn't exist, therefore isn't a formula, |
||
3675 | // therefore isn't hidden on formula bar. |
||
3676 | return false; |
||
3677 | } |
||
3678 | |||
3679 | private function getXfIndex(string $coordinate): ?int |
||
3680 | { |
||
3681 | [$column, $row] = Coordinate::coordinateFromString($coordinate); |
||
3682 | 934 | $row = (int) $row; |
|
3683 | $xfIndex = null; |
||
3684 | 934 | if ($this->rowDimensionExists($row)) { |
|
3685 | $xfIndex = $this->getRowDimension($row)->getXfIndex(); |
||
3686 | } |
||
3687 | 373 | if ($xfIndex === null && $this->ColumnDimensionExists($column)) { |
|
3688 | $xfIndex = $this->getColumnDimension($column)->getXfIndex(); |
||
3689 | 373 | } |
|
3690 | |||
3691 | return $xfIndex; |
||
3692 | 373 | } |
|
3693 | |||
3694 | 373 | private string $backgroundImage = ''; |
|
3695 | |||
3696 | private string $backgroundMime = ''; |
||
3697 | |||
3698 | private string $backgroundExtension = ''; |
||
3699 | |||
3700 | public function getBackgroundImage(): string |
||
3701 | { |
||
3702 | return $this->backgroundImage; |
||
3703 | } |
||
3704 | 4 | ||
3705 | public function getBackgroundMime(): string |
||
3706 | 4 | { |
|
3707 | 4 | return $this->backgroundMime; |
|
3708 | 4 | } |
|
3709 | 3 | ||
3710 | 3 | public function getBackgroundExtension(): string |
|
3711 | 3 | { |
|
3712 | 3 | return $this->backgroundExtension; |
|
3713 | 3 | } |
|
3714 | |||
3715 | /** |
||
3716 | 4 | * Set background image. |
|
3717 | * Used on read/write for Xlsx. |
||
3718 | * Used on write for Html. |
||
3719 | * |
||
3720 | * @param string $backgroundImage Image represented as a string, e.g. results of file_get_contents |
||
3721 | */ |
||
3722 | public function setBackgroundImage(string $backgroundImage): self |
||
3723 | { |
||
3724 | $imageArray = getimagesizefromstring($backgroundImage) ?: ['mime' => '']; |
||
3725 | $mime = $imageArray['mime']; |
||
3726 | if ($mime !== '') { |
||
3727 | 1 | $extension = explode('/', $mime); |
|
3728 | $extension = $extension[1]; |
||
3729 | 1 | $this->backgroundImage = $backgroundImage; |
|
3730 | 1 | $this->backgroundMime = $mime; |
|
3731 | 1 | $this->backgroundExtension = $extension; |
|
3732 | 1 | } |
|
3733 | 1 | ||
3734 | 1 | return $this; |
|
3735 | 1 | } |
|
3736 | 1 | ||
3737 | 1 | /** |
|
3738 | 1 | * Copy cells, adjusting relative cell references in formulas. |
|
3739 | 1 | * Acts similarly to Excel "fill handle" feature. |
|
3740 | * |
||
3741 | * @param string $fromCell Single source cell, e.g. C3 |
||
3742 | * @param string $toCells Single cell or cell range, e.g. C4 or C4:C10 |
||
3743 | * @param bool $copyStyle Copy styles as well as values, defaults to true |
||
3744 | */ |
||
3745 | 1081 | public function copyCells(string $fromCell, string $toCells, bool $copyStyle = true): void |
|
3758 | } |
||
3759 | 2 | } |
|
3760 | } |
||
3761 | 2 | } |
|
3762 | 1 | ||
3763 | public function calculateArrays(bool $preCalculateFormulas = true): void |
||
3764 | 1 | { |
|
3765 | 1 | if ($preCalculateFormulas && Calculation::getInstance($this->parent)->getInstanceArrayReturnType() === Calculation::RETURN_ARRAY_AS_ARRAY) { |
|
3766 | 1 | $keys = $this->cellCollection->getCoordinates(); |
|
3767 | 1 | foreach ($keys as $key) { |
|
3768 | 1 | if ($this->getCell($key)->getDataType() === DataType::TYPE_FORMULA) { |
|
3769 | 1 | if (preg_match(self::FUNCTION_LIKE_GROUPBY, $this->getCell($key)->getValue()) !== 1) { |
|
3770 | $this->getCell($key)->getCalculatedValue(); |
||
3771 | 1 | } |
|
3772 | } |
||
3773 | } |
||
3774 | } |
||
3775 | } |
||
3776 | 1 | ||
3777 | public function isCellInSpillRange(string $coordinate): bool |
||
3778 | { |
||
3779 | 2 | if (Calculation::getInstance($this->parent)->getInstanceArrayReturnType() !== Calculation::RETURN_ARRAY_AS_ARRAY) { |
|
3780 | return false; |
||
3781 | 2 | } |
|
3782 | 2 | $this->calculateArrays(); |
|
3783 | 1 | $keys = $this->cellCollection->getCoordinates(); |
|
3784 | foreach ($keys as $key) { |
||
3785 | 1 | $attributes = $this->getCell($key)->getFormulaAttributes(); |
|
3786 | 1 | if (isset($attributes['ref'])) { |
|
3787 | 1 | if (Coordinate::coordinateIsInsideRange($attributes['ref'], $coordinate)) { |
|
3788 | 1 | // false for first cell in range, true otherwise |
|
3789 | 1 | return $coordinate !== $key; |
|
3790 | 1 | } |
|
3791 | } |
||
3792 | } |
||
3793 | 1 | ||
3794 | return false; |
||
3795 | } |
||
3796 | |||
3797 | public function applyStylesFromArray(string $coordinate, array $styleArray): bool |
||
3798 | { |
||
3799 | $spreadsheet = $this->parent; |
||
3800 | if ($spreadsheet === null) { |
||
3801 | return false; |
||
3802 | } |
||
3803 | $activeSheetIndex = $spreadsheet->getActiveSheetIndex(); |
||
3804 | $originalSelected = $this->selectedCells; |
||
3805 | $this->getStyle($coordinate)->applyFromArray($styleArray); |
||
3806 | $this->setSelectedCells($originalSelected); |
||
3807 | if ($activeSheetIndex >= 0) { |
||
3808 | $spreadsheet->setActiveSheetIndex($activeSheetIndex); |
||
3809 | } |
||
3810 | |||
3811 | return true; |
||
3812 | } |
||
3813 | } |
||
3814 |