Failed Conditions
Push — master ( ea97af...216db0 )
by
unknown
15:51 queued 07:40
created

Spreadsheet::getSheetCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet;
4
5
use JsonSerializable;
6
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
7
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
8
use PhpOffice\PhpSpreadsheet\Document\Properties;
9
use PhpOffice\PhpSpreadsheet\Document\Security;
10
use PhpOffice\PhpSpreadsheet\Shared\Date;
11
use PhpOffice\PhpSpreadsheet\Shared\Font as SharedFont;
12
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
13
use PhpOffice\PhpSpreadsheet\Style\Style;
14
use PhpOffice\PhpSpreadsheet\Worksheet\Iterator;
15
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
16
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
17
18
class Spreadsheet implements JsonSerializable
19
{
20
    // Allowable values for workbook window visilbity
21
    const VISIBILITY_VISIBLE = 'visible';
22
    const VISIBILITY_HIDDEN = 'hidden';
23
    const VISIBILITY_VERY_HIDDEN = 'veryHidden';
24
25
    private const DEFINED_NAME_IS_RANGE = false;
26
    private const DEFINED_NAME_IS_FORMULA = true;
27
28
    private const WORKBOOK_VIEW_VISIBILITY_VALUES = [
29
        self::VISIBILITY_VISIBLE,
30
        self::VISIBILITY_HIDDEN,
31
        self::VISIBILITY_VERY_HIDDEN,
32
    ];
33
34
    protected int $excelCalendar = Date::CALENDAR_WINDOWS_1900;
35
36
    /**
37
     * Unique ID.
38
     */
39
    private string $uniqueID;
40
41
    /**
42
     * Document properties.
43
     */
44
    private Properties $properties;
45
46
    /**
47
     * Document security.
48
     */
49
    private Security $security;
50
51
    /**
52
     * Collection of Worksheet objects.
53
     *
54
     * @var Worksheet[]
55
     */
56
    private array $workSheetCollection;
57
58
    /**
59
     * Calculation Engine.
60
     */
61
    private Calculation $calculationEngine;
62
63
    /**
64
     * Active sheet index.
65
     */
66
    private int $activeSheetIndex;
67
68
    /**
69
     * Named ranges.
70
     *
71
     * @var DefinedName[]
72
     */
73
    private array $definedNames;
74
75
    /**
76
     * CellXf supervisor.
77
     */
78
    private Style $cellXfSupervisor;
79
80
    /**
81
     * CellXf collection.
82
     *
83
     * @var Style[]
84
     */
85
    private array $cellXfCollection = [];
86
87
    /**
88
     * CellStyleXf collection.
89
     *
90
     * @var Style[]
91
     */
92
    private array $cellStyleXfCollection = [];
93
94
    /**
95
     * hasMacros : this workbook have macros ?
96
     */
97
    private bool $hasMacros = false;
98
99
    /**
100
     * macrosCode : all macros code as binary data (the vbaProject.bin file, this include form, code,  etc.), null if no macro.
101
     */
102
    private ?string $macrosCode = null;
103
104
    /**
105
     * macrosCertificate : if macros are signed, contains binary data vbaProjectSignature.bin file, null if not signed.
106
     */
107
    private ?string $macrosCertificate = null;
108
109
    /**
110
     * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI.
111
     *
112
     * @var null|array{target: string, data: string}
113
     */
114
    private ?array $ribbonXMLData = null;
115
116
    /**
117
     * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements
118
     * ignored if $ribbonXMLData is null.
119
     *
120
     * @var null|mixed[]
121
     */
122
    private ?array $ribbonBinObjects = null;
123
124
    /**
125
     * List of unparsed loaded data for export to same format with better compatibility.
126
     * It has to be minimized when the library start to support currently unparsed data.
127
     *
128
     * @var array<array<array<array<string>|string>>>
129
     */
130
    private array $unparsedLoadedData = [];
131
132
    /**
133
     * Controls visibility of the horizonal scroll bar in the application.
134
     */
135
    private bool $showHorizontalScroll = true;
136
137
    /**
138
     * Controls visibility of the horizonal scroll bar in the application.
139
     */
140
    private bool $showVerticalScroll = true;
141
142
    /**
143
     * Controls visibility of the sheet tabs in the application.
144
     */
145
    private bool $showSheetTabs = true;
146
147
    /**
148
     * Specifies a boolean value that indicates whether the workbook window
149
     * is minimized.
150
     */
151
    private bool $minimized = false;
152
153
    /**
154
     * Specifies a boolean value that indicates whether to group dates
155
     * when presenting the user with filtering optiomd in the user
156
     * interface.
157
     */
158
    private bool $autoFilterDateGrouping = true;
159
160
    /**
161
     * Specifies the index to the first sheet in the book view.
162
     */
163
    private int $firstSheetIndex = 0;
164
165
    /**
166
     * Specifies the visible status of the workbook.
167
     */
168
    private string $visibility = self::VISIBILITY_VISIBLE;
169
170
    /**
171
     * Specifies the ratio between the workbook tabs bar and the horizontal
172
     * scroll bar.  TabRatio is assumed to be out of 1000 of the horizontal
173
     * window width.
174
     */
175
    private int $tabRatio = 600;
176
177
    private Theme $theme;
178
179
    private ?IValueBinder $valueBinder = null;
180
181
    /** @var array<string, int> */
182
    private array $fontCharsets = [
183
        'B Nazanin' => SharedFont::CHARSET_ANSI_ARABIC,
184
    ];
185
186
    /**
187
     * @param int $charset uses any value from Shared\Font,
188
     *    but defaults to ARABIC because that is the only known
189
     *    charset for which this declaration might be needed
190
     */
191 21
    public function addFontCharset(string $fontName, int $charset = SharedFont::CHARSET_ANSI_ARABIC): void
192
    {
193 21
        $this->fontCharsets[$fontName] = $charset;
194
    }
195
196 394
    public function getFontCharset(string $fontName): int
197
    {
198 394
        return $this->fontCharsets[$fontName] ?? -1;
199
    }
200
201
    /**
202
     * Return all fontCharsets.
203
     *
204
     * @return array<string, int>
205
     */
206 1
    public function getFontCharsets(): array
207
    {
208 1
        return $this->fontCharsets;
209
    }
210
211 804
    public function getTheme(): Theme
212
    {
213 804
        return $this->theme;
214
    }
215
216
    /**
217
     * The workbook has macros ?
218
     */
219 436
    public function hasMacros(): bool
220
    {
221 436
        return $this->hasMacros;
222
    }
223
224
    /**
225
     * Define if a workbook has macros.
226
     *
227
     * @param bool $hasMacros true|false
228
     */
229 3
    public function setHasMacros(bool $hasMacros): void
230
    {
231 3
        $this->hasMacros = (bool) $hasMacros;
232
    }
233
234
    /**
235
     * Set the macros code.
236
     */
237 3
    public function setMacrosCode(?string $macroCode): void
238
    {
239 3
        $this->macrosCode = $macroCode;
240 3
        $this->setHasMacros($macroCode !== null);
241
    }
242
243
    /**
244
     * Return the macros code.
245
     */
246 3
    public function getMacrosCode(): ?string
247
    {
248 3
        return $this->macrosCode;
249
    }
250
251
    /**
252
     * Set the macros certificate.
253
     */
254 3
    public function setMacrosCertificate(?string $certificate): void
255
    {
256 3
        $this->macrosCertificate = $certificate;
257
    }
258
259
    /**
260
     * Is the project signed ?
261
     *
262
     * @return bool true|false
263
     */
264 2
    public function hasMacrosCertificate(): bool
265
    {
266 2
        return $this->macrosCertificate !== null;
267
    }
268
269
    /**
270
     * Return the macros certificate.
271
     */
272 2
    public function getMacrosCertificate(): ?string
273
    {
274 2
        return $this->macrosCertificate;
275
    }
276
277
    /**
278
     * Remove all macros, certificate from spreadsheet.
279
     */
280 1
    public function discardMacros(): void
281
    {
282 1
        $this->hasMacros = false;
283 1
        $this->macrosCode = null;
284 1
        $this->macrosCertificate = null;
285
    }
286
287
    /**
288
     * set ribbon XML data.
289
     */
290 2
    public function setRibbonXMLData(mixed $target, mixed $xmlData): void
291
    {
292 2
        if (is_string($target) && is_string($xmlData)) {
293 2
            $this->ribbonXMLData = ['target' => $target, 'data' => $xmlData];
294
        } else {
295
            $this->ribbonXMLData = null;
296
        }
297
    }
298
299
    /**
300
     * retrieve ribbon XML Data.
301
     *
302
     * @return mixed[]
303
     */
304 392
    public function getRibbonXMLData(string $what = 'all'): null|array|string //we need some constants here...
305
    {
306 392
        $returnData = null;
307 392
        $what = strtolower($what);
308
        switch ($what) {
309 392
            case 'all':
310 2
                $returnData = $this->ribbonXMLData;
311
312 2
                break;
313 392
            case 'target':
314 2
            case 'data':
315 392
                if (is_array($this->ribbonXMLData)) {
316 2
                    $returnData = $this->ribbonXMLData[$what];
317
                }
318
319 392
                break;
320
        }
321
322 392
        return $returnData;
323
    }
324
325
    /**
326
     * store binaries ribbon objects (pictures).
327
     */
328 2
    public function setRibbonBinObjects(mixed $binObjectsNames, mixed $binObjectsData): void
329
    {
330 2
        if ($binObjectsNames !== null && $binObjectsData !== null) {
331
            $this->ribbonBinObjects = ['names' => $binObjectsNames, 'data' => $binObjectsData];
332
        } else {
333 2
            $this->ribbonBinObjects = null;
334
        }
335
    }
336
337
    /**
338
     * List of unparsed loaded data for export to same format with better compatibility.
339
     * It has to be minimized when the library start to support currently unparsed data.
340
     *
341
     * @internal
342
     *
343
     * @return mixed[]
344
     */
345 437
    public function getUnparsedLoadedData(): array
346
    {
347 437
        return $this->unparsedLoadedData;
348
    }
349
350
    /**
351
     * List of unparsed loaded data for export to same format with better compatibility.
352
     * It has to be minimized when the library start to support currently unparsed data.
353
     *
354
     * @internal
355
     *
356
     * @param array<array<array<array<string>|string>>> $unparsedLoadedData
357
     */
358 686
    public function setUnparsedLoadedData(array $unparsedLoadedData): void
359
    {
360 686
        $this->unparsedLoadedData = $unparsedLoadedData;
361
    }
362
363
    /**
364
     * retrieve Binaries Ribbon Objects.
365
     *
366
     * @return mixed[]
367
     */
368 2
    public function getRibbonBinObjects(string $what = 'all'): ?array
369
    {
370 2
        $ReturnData = null;
371 2
        $what = strtolower($what);
372
        switch ($what) {
373 2
            case 'all':
374 2
                return $this->ribbonBinObjects;
375 1
            case 'names':
376 1
            case 'data':
377 1
                if (is_array($this->ribbonBinObjects) && is_array($this->ribbonBinObjects[$what] ?? null)) {
378
                    $ReturnData = $this->ribbonBinObjects[$what];
379
                }
380
381 1
                break;
382 1
            case 'types':
383
                if (
384 1
                    is_array($this->ribbonBinObjects)
385 1
                    && isset($this->ribbonBinObjects['data']) && is_array($this->ribbonBinObjects['data'])
386
                ) {
387
                    $tmpTypes = array_keys($this->ribbonBinObjects['data']);
388
                    $ReturnData = array_unique(array_map(fn (string $path): string => pathinfo($path, PATHINFO_EXTENSION), $tmpTypes));
389
                } else {
390 1
                    $ReturnData = []; // the caller want an array... not null if empty
391
                }
392
393 1
                break;
394
        }
395
396 1
        return $ReturnData;
397
    }
398
399
    /**
400
     * This workbook have a custom UI ?
401
     */
402 392
    public function hasRibbon(): bool
403
    {
404 392
        return $this->ribbonXMLData !== null;
405
    }
406
407
    /**
408
     * This workbook have additionnal object for the ribbon ?
409
     */
410 392
    public function hasRibbonBinObjects(): bool
411
    {
412 392
        return $this->ribbonBinObjects !== null;
413
    }
414
415
    /**
416
     * Check if a sheet with a specified code name already exists.
417
     *
418
     * @param string $codeName Name of the worksheet to check
419
     */
420 10617
    public function sheetCodeNameExists(string $codeName): bool
421
    {
422 10617
        return $this->getSheetByCodeName($codeName) !== null;
423
    }
424
425
    /**
426
     * Get sheet by code name. Warning : sheet don't have always a code name !
427
     *
428
     * @param string $codeName Sheet name
429
     */
430 10617
    public function getSheetByCodeName(string $codeName): ?Worksheet
431
    {
432 10617
        $worksheetCount = count($this->workSheetCollection);
433 10617
        for ($i = 0; $i < $worksheetCount; ++$i) {
434 724
            if ($this->workSheetCollection[$i]->getCodeName() == $codeName) {
435 690
                return $this->workSheetCollection[$i];
436
            }
437
        }
438
439 10617
        return null;
440
    }
441
442
    /**
443
     * Create a new PhpSpreadsheet with one Worksheet.
444
     */
445 10617
    public function __construct()
446
    {
447 10617
        $this->uniqueID = uniqid('', true);
448 10617
        $this->calculationEngine = new Calculation($this);
449 10617
        $this->theme = new Theme();
450
451
        // Initialise worksheet collection and add one worksheet
452 10617
        $this->workSheetCollection = [];
453 10617
        $this->workSheetCollection[] = new Worksheet($this);
454 10617
        $this->activeSheetIndex = 0;
455
456
        // Create document properties
457 10617
        $this->properties = new Properties();
458
459
        // Create document security
460 10617
        $this->security = new Security();
461
462
        // Set defined names
463 10617
        $this->definedNames = [];
464
465
        // Create the cellXf supervisor
466 10617
        $this->cellXfSupervisor = new Style(true);
467 10617
        $this->cellXfSupervisor->bindParent($this);
468
469
        // Create the default style
470 10617
        $this->addCellXf(new Style());
471 10617
        $this->addCellStyleXf(new Style());
472
    }
473
474
    /**
475
     * Code to execute when this worksheet is unset().
476
     */
477 127
    public function __destruct()
478
    {
479 127
        $this->disconnectWorksheets();
480 127
        unset($this->calculationEngine);
481 127
        $this->cellXfCollection = [];
482 127
        $this->cellStyleXfCollection = [];
483 127
        $this->definedNames = [];
484
    }
485
486
    /**
487
     * Disconnect all worksheets from this PhpSpreadsheet workbook object,
488
     * typically so that the PhpSpreadsheet object can be unset.
489
     */
490 9701
    public function disconnectWorksheets(): void
491
    {
492 9701
        foreach ($this->workSheetCollection as $worksheet) {
493 9699
            $worksheet->disconnectCells();
494 9699
            unset($worksheet);
495
        }
496 9701
        $this->workSheetCollection = [];
497
    }
498
499
    /**
500
     * Return the calculation engine for this worksheet.
501
     */
502 9691
    public function getCalculationEngine(): Calculation
503
    {
504 9691
        return $this->calculationEngine;
505
    }
506
507
    /**
508
     * Intended for use only via a destructor.
509
     *
510
     * @internal
511
     */
512 88
    public function getCalculationEngineOrNull(): ?Calculation
513
    {
514 88
        if (!isset($this->calculationEngine)) { //* @phpstan-ignore-line
515 58
            return null;
516
        }
517
518 87
        return $this->calculationEngine;
519
    }
520
521
    /**
522
     * Get properties.
523
     */
524 1647
    public function getProperties(): Properties
525
    {
526 1647
        return $this->properties;
527
    }
528
529
    /**
530
     * Set properties.
531
     */
532 1
    public function setProperties(Properties $documentProperties): void
533
    {
534 1
        $this->properties = $documentProperties;
535
    }
536
537
    /**
538
     * Get security.
539
     */
540 406
    public function getSecurity(): Security
541
    {
542 406
        return $this->security;
543
    }
544
545
    /**
546
     * Set security.
547
     */
548 1
    public function setSecurity(Security $documentSecurity): void
549
    {
550 1
        $this->security = $documentSecurity;
551
    }
552
553
    /**
554
     * Get active sheet.
555
     */
556 10542
    public function getActiveSheet(): Worksheet
557
    {
558 10542
        return $this->getSheet($this->activeSheetIndex);
559
    }
560
561
    /**
562
     * Create sheet and add it to this workbook.
563
     *
564
     * @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
565
     */
566 1264
    public function createSheet(?int $sheetIndex = null): Worksheet
567
    {
568 1264
        $newSheet = new Worksheet($this);
569 1264
        $this->addSheet($newSheet, $sheetIndex, true);
570
571 1264
        return $newSheet;
572
    }
573
574
    /**
575
     * Check if a sheet with a specified name already exists.
576
     *
577
     * @param string $worksheetName Name of the worksheet to check
578
     */
579 1844
    public function sheetNameExists(string $worksheetName): bool
580
    {
581 1844
        return $this->getSheetByName($worksheetName) !== null;
582
    }
583
584 1
    public function duplicateWorksheetByTitle(string $title): Worksheet
585
    {
586 1
        $original = $this->getSheetByNameOrThrow($title);
587 1
        $index = $this->getIndex($original) + 1;
588 1
        $clone = clone $original;
589
590 1
        return $this->addSheet($clone, $index, true);
591
    }
592
593
    /**
594
     * Add sheet.
595
     *
596
     * @param Worksheet $worksheet The worksheet to add
597
     * @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
598
     */
599 1355
    public function addSheet(Worksheet $worksheet, ?int $sheetIndex = null, bool $retitleIfNeeded = false): Worksheet
600
    {
601 1355
        if ($retitleIfNeeded) {
602 1264
            $title = $worksheet->getTitle();
603 1264
            if ($this->sheetNameExists($title)) {
604 169
                $i = 1;
605 169
                $newTitle = "$title $i";
606 169
                while ($this->sheetNameExists($newTitle)) {
607 34
                    ++$i;
608 34
                    $newTitle = "$title $i";
609
                }
610 169
                $worksheet->setTitle($newTitle);
611
            }
612
        }
613 1355
        if ($this->sheetNameExists($worksheet->getTitle())) {
614 2
            throw new Exception(
615 2
                "Workbook already contains a worksheet named '{$worksheet->getTitle()}'. Rename this worksheet first."
616 2
            );
617
        }
618
619 1355
        if ($sheetIndex === null) {
620 1322
            if ($this->activeSheetIndex < 0) {
621 934
                $this->activeSheetIndex = 0;
622
            }
623 1322
            $this->workSheetCollection[] = $worksheet;
624
        } else {
625
            // Insert the sheet at the requested index
626 38
            array_splice(
627 38
                $this->workSheetCollection,
628 38
                $sheetIndex,
629 38
                0,
630 38
                [$worksheet]
631 38
            );
632
633
            // Adjust active sheet index if necessary
634 38
            if ($this->activeSheetIndex >= $sheetIndex) {
635 30
                ++$this->activeSheetIndex;
636
            }
637 38
            if ($this->activeSheetIndex < 0) {
638 3
                $this->activeSheetIndex = 0;
639
            }
640
        }
641
642 1355
        if ($worksheet->getParent() === null) {
643 51
            $worksheet->rebindParent($this);
644
        }
645
646 1355
        return $worksheet;
647
    }
648
649
    /**
650
     * Remove sheet by index.
651
     *
652
     * @param int $sheetIndex Index position of the worksheet to remove
653
     */
654 976
    public function removeSheetByIndex(int $sheetIndex): void
655
    {
656 976
        $numSheets = count($this->workSheetCollection);
657 976
        if ($sheetIndex > $numSheets - 1) {
658 1
            throw new Exception(
659 1
                "You tried to remove a sheet by the out of bounds index: {$sheetIndex}. The actual number of sheets is {$numSheets}."
660 1
            );
661
        }
662 975
        array_splice($this->workSheetCollection, $sheetIndex, 1);
663
664
        // Adjust active sheet index if necessary
665
        if (
666 975
            ($this->activeSheetIndex >= $sheetIndex)
667 975
            && ($this->activeSheetIndex > 0 || $numSheets <= 1)
668
        ) {
669 973
            --$this->activeSheetIndex;
670
        }
671
    }
672
673
    /**
674
     * Get sheet by index.
675
     *
676
     * @param int $sheetIndex Sheet index
677
     */
678 10548
    public function getSheet(int $sheetIndex): Worksheet
679
    {
680 10548
        if (!isset($this->workSheetCollection[$sheetIndex])) {
681 1
            $numSheets = $this->getSheetCount();
682
683 1
            throw new Exception(
684 1
                "Your requested sheet index: {$sheetIndex} is out of bounds. The actual number of sheets is {$numSheets}."
685 1
            );
686
        }
687
688 10548
        return $this->workSheetCollection[$sheetIndex];
689
    }
690
691
    /**
692
     * Get all sheets.
693
     *
694
     * @return Worksheet[]
695
     */
696 173
    public function getAllSheets(): array
697
    {
698 173
        return $this->workSheetCollection;
699
    }
700
701
    /**
702
     * Get sheet by name.
703
     *
704
     * @param string $worksheetName Sheet name
705
     */
706 9526
    public function getSheetByName(string $worksheetName): ?Worksheet
707
    {
708 9526
        $trimWorksheetName = trim($worksheetName, "'");
709 9526
        foreach ($this->workSheetCollection as $worksheet) {
710 9175
            if (strcasecmp($worksheet->getTitle(), $trimWorksheetName) === 0) {
711 8592
                return $worksheet;
712
            }
713
        }
714
715 1860
        return null;
716
    }
717
718
    /**
719
     * Get sheet by name, throwing exception if not found.
720
     */
721 236
    public function getSheetByNameOrThrow(string $worksheetName): Worksheet
722
    {
723 236
        $worksheet = $this->getSheetByName($worksheetName);
724 236
        if ($worksheet === null) {
725 1
            throw new Exception("Sheet $worksheetName does not exist.");
726
        }
727
728 235
        return $worksheet;
729
    }
730
731
    /**
732
     * Get index for sheet.
733
     *
734
     * @return int index
735
     */
736 10618
    public function getIndex(Worksheet $worksheet, bool $noThrow = false): int
737
    {
738 10618
        $wsHash = $worksheet->getHashInt();
739 10618
        foreach ($this->workSheetCollection as $key => $value) {
740 10389
            if ($value->getHashInt() === $wsHash) {
741 10377
                return $key;
742
            }
743
        }
744 10617
        if ($noThrow) {
745 10617
            return -1;
746
        }
747
748 3
        throw new Exception('Sheet does not exist.');
749
    }
750
751
    /**
752
     * Set index for sheet by sheet name.
753
     *
754
     * @param string $worksheetName Sheet name to modify index for
755
     * @param int $newIndexPosition New index for the sheet
756
     *
757
     * @return int New sheet index
758
     */
759 1
    public function setIndexByName(string $worksheetName, int $newIndexPosition): int
760
    {
761 1
        $oldIndex = $this->getIndex($this->getSheetByNameOrThrow($worksheetName));
762 1
        $worksheet = array_splice(
763 1
            $this->workSheetCollection,
764 1
            $oldIndex,
765 1
            1
766 1
        );
767 1
        array_splice(
768 1
            $this->workSheetCollection,
769 1
            $newIndexPosition,
770 1
            0,
771 1
            $worksheet
772 1
        );
773
774 1
        return $newIndexPosition;
775
    }
776
777
    /**
778
     * Get sheet count.
779
     */
780 1626
    public function getSheetCount(): int
781
    {
782 1626
        return count($this->workSheetCollection);
783
    }
784
785
    /**
786
     * Get active sheet index.
787
     *
788
     * @return int Active sheet index
789
     */
790 10261
    public function getActiveSheetIndex(): int
791
    {
792 10261
        return $this->activeSheetIndex;
793
    }
794
795
    /**
796
     * Set active sheet index.
797
     *
798
     * @param int $worksheetIndex Active sheet index
799
     */
800 10344
    public function setActiveSheetIndex(int $worksheetIndex): Worksheet
801
    {
802 10344
        $numSheets = count($this->workSheetCollection);
803
804 10344
        if ($worksheetIndex > $numSheets - 1) {
805 6
            throw new Exception(
806 6
                "You tried to set a sheet active by the out of bounds index: {$worksheetIndex}. The actual number of sheets is {$numSheets}."
807 6
            );
808
        }
809 10338
        $this->activeSheetIndex = $worksheetIndex;
810
811 10338
        return $this->getActiveSheet();
812
    }
813
814
    /**
815
     * Set active sheet index by name.
816
     *
817
     * @param string $worksheetName Sheet title
818
     */
819 106
    public function setActiveSheetIndexByName(string $worksheetName): Worksheet
820
    {
821 106
        if (($worksheet = $this->getSheetByName($worksheetName)) instanceof Worksheet) {
822 104
            $this->setActiveSheetIndex($this->getIndex($worksheet));
823
824 104
            return $worksheet;
825
        }
826
827 2
        throw new Exception('Workbook does not contain sheet:' . $worksheetName);
828
    }
829
830
    /**
831
     * Get sheet names.
832
     *
833
     * @return string[]
834
     */
835 10
    public function getSheetNames(): array
836
    {
837 10
        $returnValue = [];
838 10
        $worksheetCount = $this->getSheetCount();
839 10
        for ($i = 0; $i < $worksheetCount; ++$i) {
840 10
            $returnValue[] = $this->getSheet($i)->getTitle();
841
        }
842
843 10
        return $returnValue;
844
    }
845
846
    /**
847
     * Add external sheet.
848
     *
849
     * @param Worksheet $worksheet External sheet to add
850
     * @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
851
     */
852 5
    public function addExternalSheet(Worksheet $worksheet, ?int $sheetIndex = null): Worksheet
853
    {
854 5
        if ($this->sheetNameExists($worksheet->getTitle())) {
855 1
            throw new Exception("Workbook already contains a worksheet named '{$worksheet->getTitle()}'. Rename the external sheet first.");
856
        }
857
858
        // count how many cellXfs there are in this workbook currently, we will need this below
859 4
        $countCellXfs = count($this->cellXfCollection);
860
861
        // copy all the shared cellXfs from the external workbook and append them to the current
862 4
        foreach ($worksheet->getParentOrThrow()->getCellXfCollection() as $cellXf) {
863 4
            $this->addCellXf(clone $cellXf);
864
        }
865
866
        // move sheet to this workbook
867 4
        $worksheet->rebindParent($this);
868
869
        // update the cellXfs
870 4
        foreach ($worksheet->getCoordinates(false) as $coordinate) {
871 4
            $cell = $worksheet->getCell($coordinate);
872 4
            $cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
873
        }
874
875
        // update the column dimensions Xfs
876 4
        foreach ($worksheet->getColumnDimensions() as $columnDimension) {
877 1
            $columnDimension->setXfIndex($columnDimension->getXfIndex() + $countCellXfs);
878
        }
879
880
        // update the row dimensions Xfs
881 4
        foreach ($worksheet->getRowDimensions() as $rowDimension) {
882 1
            $xfIndex = $rowDimension->getXfIndex();
883 1
            if ($xfIndex !== null) {
884 1
                $rowDimension->setXfIndex($xfIndex + $countCellXfs);
885
            }
886
        }
887
888 4
        return $this->addSheet($worksheet, $sheetIndex);
889
    }
890
891
    /**
892
     * Get an array of all Named Ranges.
893
     *
894
     * @return DefinedName[]
895
     */
896 9
    public function getNamedRanges(): array
897
    {
898 9
        return array_filter(
899 9
            $this->definedNames,
900 9
            fn (DefinedName $definedName): bool => $definedName->isFormula() === self::DEFINED_NAME_IS_RANGE
901 9
        );
902
    }
903
904
    /**
905
     * Get an array of all Named Formulae.
906
     *
907
     * @return DefinedName[]
908
     */
909 15
    public function getNamedFormulae(): array
910
    {
911 15
        return array_filter(
912 15
            $this->definedNames,
913 15
            fn (DefinedName $definedName): bool => $definedName->isFormula() === self::DEFINED_NAME_IS_FORMULA
914 15
        );
915
    }
916
917
    /**
918
     * Get an array of all Defined Names (both named ranges and named formulae).
919
     *
920
     * @return DefinedName[]
921
     */
922 604
    public function getDefinedNames(): array
923
    {
924 604
        return $this->definedNames;
925
    }
926
927
    /**
928
     * Add a named range.
929
     * If a named range with this name already exists, then this will replace the existing value.
930
     */
931 315
    public function addNamedRange(NamedRange $namedRange): void
932
    {
933 315
        $this->addDefinedName($namedRange);
934
    }
935
936
    /**
937
     * Add a named formula.
938
     * If a named formula with this name already exists, then this will replace the existing value.
939
     */
940 12
    public function addNamedFormula(NamedFormula $namedFormula): void
941
    {
942 12
        $this->addDefinedName($namedFormula);
943
    }
944
945
    /**
946
     * Add a defined name (either a named range or a named formula).
947
     * If a defined named with this name already exists, then this will replace the existing value.
948
     */
949 453
    public function addDefinedName(DefinedName $definedName): void
950
    {
951 453
        $upperCaseName = StringHelper::strToUpper($definedName->getName());
952 453
        if ($definedName->getScope() == null) {
953
            // global scope
954 439
            $this->definedNames[$upperCaseName] = $definedName;
955
        } else {
956
            // local scope
957 122
            $this->definedNames[$definedName->getScope()->getTitle() . '!' . $upperCaseName] = $definedName;
958
        }
959
    }
960
961
    /**
962
     * Get named range.
963
     *
964
     * @param null|Worksheet $worksheet Scope. Use null for global scope
965
     */
966 33
    public function getNamedRange(string $namedRange, ?Worksheet $worksheet = null): ?NamedRange
967
    {
968 33
        $returnValue = null;
969
970 33
        if ($namedRange !== '') {
971 32
            $namedRange = StringHelper::strToUpper($namedRange);
972
            // first look for global named range
973 32
            $returnValue = $this->getGlobalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE);
974
            // then look for local named range (has priority over global named range if both names exist)
975 32
            $returnValue = $this->getLocalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE, $worksheet) ?: $returnValue;
976
        }
977
978 33
        return $returnValue instanceof NamedRange ? $returnValue : null;
979
    }
980
981
    /**
982
     * Get named formula.
983
     *
984
     * @param null|Worksheet $worksheet Scope. Use null for global scope
985
     */
986 11
    public function getNamedFormula(string $namedFormula, ?Worksheet $worksheet = null): ?NamedFormula
987
    {
988 11
        $returnValue = null;
989
990 11
        if ($namedFormula !== '') {
991 11
            $namedFormula = StringHelper::strToUpper($namedFormula);
992
            // first look for global named formula
993 11
            $returnValue = $this->getGlobalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA);
994
            // then look for local named formula (has priority over global named formula if both names exist)
995 11
            $returnValue = $this->getLocalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA, $worksheet) ?: $returnValue;
996
        }
997
998 11
        return $returnValue instanceof NamedFormula ? $returnValue : null;
999
    }
1000
1001 43
    private function getGlobalDefinedNameByType(string $name, bool $type): ?DefinedName
1002
    {
1003 43
        if (isset($this->definedNames[$name]) && $this->definedNames[$name]->isFormula() === $type) {
1004 30
            return $this->definedNames[$name];
1005
        }
1006
1007 15
        return null;
1008
    }
1009
1010 43
    private function getLocalDefinedNameByType(string $name, bool $type, ?Worksheet $worksheet = null): ?DefinedName
1011
    {
1012
        if (
1013 43
            ($worksheet !== null) && isset($this->definedNames[$worksheet->getTitle() . '!' . $name])
1014 43
            && $this->definedNames[$worksheet->getTitle() . '!' . $name]->isFormula() === $type
1015
        ) {
1016 8
            return $this->definedNames[$worksheet->getTitle() . '!' . $name];
1017
        }
1018
1019 41
        return null;
1020
    }
1021
1022
    /**
1023
     * Get named range.
1024
     *
1025
     * @param null|Worksheet $worksheet Scope. Use null for global scope
1026
     */
1027 10296
    public function getDefinedName(string $definedName, ?Worksheet $worksheet = null): ?DefinedName
1028
    {
1029 10296
        $returnValue = null;
1030
1031 10296
        if ($definedName !== '') {
1032 10296
            $definedName = StringHelper::strToUpper($definedName);
1033
            // first look for global defined name
1034 10296
            if (isset($this->definedNames[$definedName])) {
1035 137
                $returnValue = $this->definedNames[$definedName];
1036
            }
1037
1038
            // then look for local defined name (has priority over global defined name if both names exist)
1039 10296
            if (($worksheet !== null) && isset($this->definedNames[$worksheet->getTitle() . '!' . $definedName])) {
1040 22
                $returnValue = $this->definedNames[$worksheet->getTitle() . '!' . $definedName];
1041
            }
1042
        }
1043
1044 10296
        return $returnValue;
1045
    }
1046
1047
    /**
1048
     * Remove named range.
1049
     *
1050
     * @param null|Worksheet $worksheet scope: use null for global scope
1051
     *
1052
     * @return $this
1053
     */
1054 5
    public function removeNamedRange(string $namedRange, ?Worksheet $worksheet = null): self
1055
    {
1056 5
        if ($this->getNamedRange($namedRange, $worksheet) === null) {
1057 1
            return $this;
1058
        }
1059
1060 4
        return $this->removeDefinedName($namedRange, $worksheet);
1061
    }
1062
1063
    /**
1064
     * Remove named formula.
1065
     *
1066
     * @param null|Worksheet $worksheet scope: use null for global scope
1067
     *
1068
     * @return $this
1069
     */
1070 4
    public function removeNamedFormula(string $namedFormula, ?Worksheet $worksheet = null): self
1071
    {
1072 4
        if ($this->getNamedFormula($namedFormula, $worksheet) === null) {
1073 1
            return $this;
1074
        }
1075
1076 3
        return $this->removeDefinedName($namedFormula, $worksheet);
1077
    }
1078
1079
    /**
1080
     * Remove defined name.
1081
     *
1082
     * @param null|Worksheet $worksheet scope: use null for global scope
1083
     *
1084
     * @return $this
1085
     */
1086 11
    public function removeDefinedName(string $definedName, ?Worksheet $worksheet = null): self
1087
    {
1088 11
        $definedName = StringHelper::strToUpper($definedName);
1089
1090 11
        if ($worksheet === null) {
1091 1
            if (isset($this->definedNames[$definedName])) {
1092 1
                unset($this->definedNames[$definedName]);
1093
            }
1094
        } else {
1095 10
            if (isset($this->definedNames[$worksheet->getTitle() . '!' . $definedName])) {
1096 3
                unset($this->definedNames[$worksheet->getTitle() . '!' . $definedName]);
1097 7
            } elseif (isset($this->definedNames[$definedName])) {
1098 7
                unset($this->definedNames[$definedName]);
1099
            }
1100
        }
1101
1102 11
        return $this;
1103
    }
1104
1105
    /**
1106
     * Get worksheet iterator.
1107
     */
1108 1421
    public function getWorksheetIterator(): Iterator
1109
    {
1110 1421
        return new Iterator($this);
1111
    }
1112
1113
    /**
1114
     * Copy workbook (!= clone!).
1115
     */
1116 5
    public function copy(): self
1117
    {
1118 5
        return unserialize(serialize($this)); //* @phpstan-ignore-line
1119
    }
1120
1121
    /**
1122
     * Implement PHP __clone to create a deep clone, not just a shallow copy.
1123
     */
1124 5
    public function __clone()
1125
    {
1126 5
        $this->uniqueID = uniqid('', true);
1127
1128 5
        $usedKeys = [];
1129
        // I don't now why new Style rather than clone.
1130 5
        $this->cellXfSupervisor = new Style(true);
1131
        //$this->cellXfSupervisor = clone $this->cellXfSupervisor;
1132 5
        $this->cellXfSupervisor->bindParent($this);
1133 5
        $usedKeys['cellXfSupervisor'] = true;
1134
1135 5
        $oldCalc = $this->calculationEngine;
1136 5
        $this->calculationEngine = new Calculation($this);
1137 5
        $this->calculationEngine
1138 5
            ->setSuppressFormulaErrors(
1139 5
                $oldCalc->getSuppressFormulaErrors()
1140 5
            )
1141 5
            ->setCalculationCacheEnabled(
1142 5
                $oldCalc->getCalculationCacheEnabled()
1143 5
            )
1144 5
            ->setBranchPruningEnabled(
1145 5
                $oldCalc->getBranchPruningEnabled()
1146 5
            )
1147 5
            ->setInstanceArrayReturnType(
1148 5
                $oldCalc->getInstanceArrayReturnType()
1149 5
            );
1150 5
        $usedKeys['calculationEngine'] = true;
1151
1152 5
        $currentCollection = $this->cellStyleXfCollection;
1153 5
        $this->cellStyleXfCollection = [];
1154 5
        foreach ($currentCollection as $item) {
1155 5
            $clone = $item->exportArray();
1156 5
            $style = (new Style())->applyFromArray($clone);
1157 5
            $this->addCellStyleXf($style);
1158
        }
1159 5
        $usedKeys['cellStyleXfCollection'] = true;
1160
1161 5
        $currentCollection = $this->cellXfCollection;
1162 5
        $this->cellXfCollection = [];
1163 5
        foreach ($currentCollection as $item) {
1164 5
            $clone = $item->exportArray();
1165 5
            $style = (new Style())->applyFromArray($clone);
1166 5
            $this->addCellXf($style);
1167
        }
1168 5
        $usedKeys['cellXfCollection'] = true;
1169
1170 5
        $currentCollection = $this->workSheetCollection;
1171 5
        $this->workSheetCollection = [];
1172 5
        foreach ($currentCollection as $item) {
1173 5
            $clone = clone $item;
1174 5
            $clone->setParent($this);
1175 5
            $this->workSheetCollection[] = $clone;
1176
        }
1177 5
        $usedKeys['workSheetCollection'] = true;
1178
1179 5
        foreach (get_object_vars($this) as $key => $val) {
1180 5
            if (isset($usedKeys[$key])) {
1181 5
                continue;
1182
            }
1183
            switch ($key) {
1184
                // arrays of objects not covered above
1185 5
                case 'definedNames':
1186
                    /** @var DefinedName[] */
1187 5
                    $currentCollection = $val;
1188 5
                    $this->$key = [];
1189 5
                    foreach ($currentCollection as $item) {
1190
                        $clone = clone $item;
1191
                        $this->{$key}[] = $clone;
1192
                    }
1193
1194 5
                    break;
1195
                default:
1196 5
                    if (is_object($val)) {
1197 5
                        $this->$key = clone $val;
1198
                    }
1199
            }
1200
        }
1201
    }
1202
1203
    /**
1204
     * Get the workbook collection of cellXfs.
1205
     *
1206
     * @return Style[]
1207
     */
1208 1142
    public function getCellXfCollection(): array
1209
    {
1210 1142
        return $this->cellXfCollection;
1211
    }
1212
1213
    /**
1214
     * Get cellXf by index.
1215
     */
1216 10203
    public function getCellXfByIndex(int $cellStyleIndex): Style
1217
    {
1218 10203
        return $this->cellXfCollection[$cellStyleIndex];
1219
    }
1220
1221 2
    public function getCellXfByIndexOrNull(?int $cellStyleIndex): ?Style
1222
    {
1223 2
        return ($cellStyleIndex === null) ? null : ($this->cellXfCollection[$cellStyleIndex] ?? null);
1224
    }
1225
1226
    /**
1227
     * Get cellXf by hash code.
1228
     *
1229
     * @return false|Style
1230
     */
1231 915
    public function getCellXfByHashCode(string $hashcode): bool|Style
1232
    {
1233 915
        foreach ($this->cellXfCollection as $cellXf) {
1234 915
            if ($cellXf->getHashCode() === $hashcode) {
1235 267
                return $cellXf;
1236
            }
1237
        }
1238
1239 852
        return false;
1240
    }
1241
1242
    /**
1243
     * Check if style exists in style collection.
1244
     */
1245 1
    public function cellXfExists(Style $cellStyleIndex): bool
1246
    {
1247 1
        return in_array($cellStyleIndex, $this->cellXfCollection, true);
1248
    }
1249
1250
    /**
1251
     * Get default style.
1252
     */
1253 1036
    public function getDefaultStyle(): Style
1254
    {
1255 1036
        if (isset($this->cellXfCollection[0])) {
1256 1035
            return $this->cellXfCollection[0];
1257
        }
1258
1259 1
        throw new Exception('No default style found for this workbook');
1260
    }
1261
1262
    /**
1263
     * Add a cellXf to the workbook.
1264
     */
1265 10617
    public function addCellXf(Style $style): void
1266
    {
1267 10617
        $this->cellXfCollection[] = $style;
1268 10617
        $style->setIndex(count($this->cellXfCollection) - 1);
1269
    }
1270
1271
    /**
1272
     * Remove cellXf by index. It is ensured that all cells get their xf index updated.
1273
     *
1274
     * @param int $cellStyleIndex Index to cellXf
1275
     */
1276 813
    public function removeCellXfByIndex(int $cellStyleIndex): void
1277
    {
1278 813
        if ($cellStyleIndex > count($this->cellXfCollection) - 1) {
1279 1
            throw new Exception('CellXf index is out of bounds.');
1280
        }
1281
1282
        // first remove the cellXf
1283 812
        array_splice($this->cellXfCollection, $cellStyleIndex, 1);
1284
1285
        // then update cellXf indexes for cells
1286 812
        foreach ($this->workSheetCollection as $worksheet) {
1287 2
            foreach ($worksheet->getCoordinates(false) as $coordinate) {
1288 1
                $cell = $worksheet->getCell($coordinate);
1289 1
                $xfIndex = $cell->getXfIndex();
1290 1
                if ($xfIndex > $cellStyleIndex) {
1291
                    // decrease xf index by 1
1292 1
                    $cell->setXfIndex($xfIndex - 1);
1293 1
                } elseif ($xfIndex == $cellStyleIndex) {
1294
                    // set to default xf index 0
1295 1
                    $cell->setXfIndex(0);
1296
                }
1297
            }
1298
        }
1299
    }
1300
1301
    /**
1302
     * Get the cellXf supervisor.
1303
     */
1304 10230
    public function getCellXfSupervisor(): Style
1305
    {
1306 10230
        return $this->cellXfSupervisor;
1307
    }
1308
1309
    /**
1310
     * Get the workbook collection of cellStyleXfs.
1311
     *
1312
     * @return Style[]
1313
     */
1314 1
    public function getCellStyleXfCollection(): array
1315
    {
1316 1
        return $this->cellStyleXfCollection;
1317
    }
1318
1319
    /**
1320
     * Get cellStyleXf by index.
1321
     *
1322
     * @param int $cellStyleIndex Index to cellXf
1323
     */
1324 1
    public function getCellStyleXfByIndex(int $cellStyleIndex): Style
1325
    {
1326 1
        return $this->cellStyleXfCollection[$cellStyleIndex];
1327
    }
1328
1329
    /**
1330
     * Get cellStyleXf by hash code.
1331
     *
1332
     * @return false|Style
1333
     */
1334 1
    public function getCellStyleXfByHashCode(string $hashcode): bool|Style
1335
    {
1336 1
        foreach ($this->cellStyleXfCollection as $cellStyleXf) {
1337 1
            if ($cellStyleXf->getHashCode() === $hashcode) {
1338 1
                return $cellStyleXf;
1339
            }
1340
        }
1341
1342 1
        return false;
1343
    }
1344
1345
    /**
1346
     * Add a cellStyleXf to the workbook.
1347
     */
1348 10617
    public function addCellStyleXf(Style $style): void
1349
    {
1350 10617
        $this->cellStyleXfCollection[] = $style;
1351 10617
        $style->setIndex(count($this->cellStyleXfCollection) - 1);
1352
    }
1353
1354
    /**
1355
     * Remove cellStyleXf by index.
1356
     *
1357
     * @param int $cellStyleIndex Index to cellXf
1358
     */
1359 810
    public function removeCellStyleXfByIndex(int $cellStyleIndex): void
1360
    {
1361 810
        if ($cellStyleIndex > count($this->cellStyleXfCollection) - 1) {
1362 1
            throw new Exception('CellStyleXf index is out of bounds.');
1363
        }
1364 809
        array_splice($this->cellStyleXfCollection, $cellStyleIndex, 1);
1365
    }
1366
1367
    /**
1368
     * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
1369
     * and columns in the workbook.
1370
     */
1371 1038
    public function garbageCollect(): void
1372
    {
1373
        // how many references are there to each cellXf ?
1374 1038
        $countReferencesCellXf = [];
1375 1038
        foreach ($this->cellXfCollection as $index => $cellXf) {
1376 1038
            $countReferencesCellXf[$index] = 0;
1377
        }
1378
1379 1038
        foreach ($this->getWorksheetIterator() as $sheet) {
1380
            // from cells
1381 1038
            foreach ($sheet->getCoordinates(false) as $coordinate) {
1382 1007
                $cell = $sheet->getCell($coordinate);
1383 1007
                ++$countReferencesCellXf[$cell->getXfIndex()];
1384
            }
1385
1386
            // from row dimensions
1387 1038
            foreach ($sheet->getRowDimensions() as $rowDimension) {
1388 92
                if ($rowDimension->getXfIndex() !== null) {
1389 11
                    ++$countReferencesCellXf[$rowDimension->getXfIndex()];
1390
                }
1391
            }
1392
1393
            // from column dimensions
1394 1038
            foreach ($sheet->getColumnDimensions() as $columnDimension) {
1395 148
                ++$countReferencesCellXf[$columnDimension->getXfIndex()];
1396
            }
1397
        }
1398
1399
        // remove cellXfs without references and create mapping so we can update xfIndex
1400
        // for all cells and columns
1401 1038
        $countNeededCellXfs = 0;
1402 1038
        $map = [];
1403 1038
        foreach ($this->cellXfCollection as $index => $cellXf) {
1404 1038
            if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
1405 1038
                ++$countNeededCellXfs;
1406
            } else {
1407 37
                unset($this->cellXfCollection[$index]);
1408
            }
1409 1038
            $map[$index] = $countNeededCellXfs - 1;
1410
        }
1411 1038
        $this->cellXfCollection = array_values($this->cellXfCollection);
1412
1413
        // update the index for all cellXfs
1414 1038
        foreach ($this->cellXfCollection as $i => $cellXf) {
1415 1038
            $cellXf->setIndex($i);
1416
        }
1417
1418
        // make sure there is always at least one cellXf (there should be)
1419 1038
        if (empty($this->cellXfCollection)) {
1420
            $this->cellXfCollection[] = new Style();
1421
        }
1422
1423
        // update the xfIndex for all cells, row dimensions, column dimensions
1424 1038
        foreach ($this->getWorksheetIterator() as $sheet) {
1425
            // for all cells
1426 1038
            foreach ($sheet->getCoordinates(false) as $coordinate) {
1427 1007
                $cell = $sheet->getCell($coordinate);
1428 1007
                $cell->setXfIndex($map[$cell->getXfIndex()]);
1429
            }
1430
1431
            // for all row dimensions
1432 1038
            foreach ($sheet->getRowDimensions() as $rowDimension) {
1433 92
                if ($rowDimension->getXfIndex() !== null) {
1434 11
                    $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
1435
                }
1436
            }
1437
1438
            // for all column dimensions
1439 1038
            foreach ($sheet->getColumnDimensions() as $columnDimension) {
1440 148
                $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
1441
            }
1442
1443
            // also do garbage collection for all the sheets
1444 1038
            $sheet->garbageCollect();
1445
        }
1446
    }
1447
1448
    /**
1449
     * Return the unique ID value assigned to this spreadsheet workbook.
1450
     */
1451
    public function getID(): string
1452
    {
1453
        return $this->uniqueID;
1454
    }
1455
1456
    /**
1457
     * Get the visibility of the horizonal scroll bar in the application.
1458
     *
1459
     * @return bool True if horizonal scroll bar is visible
1460
     */
1461 393
    public function getShowHorizontalScroll(): bool
1462
    {
1463 393
        return $this->showHorizontalScroll;
1464
    }
1465
1466
    /**
1467
     * Set the visibility of the horizonal scroll bar in the application.
1468
     *
1469
     * @param bool $showHorizontalScroll True if horizonal scroll bar is visible
1470
     */
1471 274
    public function setShowHorizontalScroll(bool $showHorizontalScroll): void
1472
    {
1473 274
        $this->showHorizontalScroll = (bool) $showHorizontalScroll;
1474
    }
1475
1476
    /**
1477
     * Get the visibility of the vertical scroll bar in the application.
1478
     *
1479
     * @return bool True if vertical scroll bar is visible
1480
     */
1481 393
    public function getShowVerticalScroll(): bool
1482
    {
1483 393
        return $this->showVerticalScroll;
1484
    }
1485
1486
    /**
1487
     * Set the visibility of the vertical scroll bar in the application.
1488
     *
1489
     * @param bool $showVerticalScroll True if vertical scroll bar is visible
1490
     */
1491 274
    public function setShowVerticalScroll(bool $showVerticalScroll): void
1492
    {
1493 274
        $this->showVerticalScroll = (bool) $showVerticalScroll;
1494
    }
1495
1496
    /**
1497
     * Get the visibility of the sheet tabs in the application.
1498
     *
1499
     * @return bool True if the sheet tabs are visible
1500
     */
1501 393
    public function getShowSheetTabs(): bool
1502
    {
1503 393
        return $this->showSheetTabs;
1504
    }
1505
1506
    /**
1507
     * Set the visibility of the sheet tabs  in the application.
1508
     *
1509
     * @param bool $showSheetTabs True if sheet tabs are visible
1510
     */
1511 274
    public function setShowSheetTabs(bool $showSheetTabs): void
1512
    {
1513 274
        $this->showSheetTabs = (bool) $showSheetTabs;
1514
    }
1515
1516
    /**
1517
     * Return whether the workbook window is minimized.
1518
     *
1519
     * @return bool true if workbook window is minimized
1520
     */
1521 393
    public function getMinimized(): bool
1522
    {
1523 393
        return $this->minimized;
1524
    }
1525
1526
    /**
1527
     * Set whether the workbook window is minimized.
1528
     *
1529
     * @param bool $minimized true if workbook window is minimized
1530
     */
1531 268
    public function setMinimized(bool $minimized): void
1532
    {
1533 268
        $this->minimized = (bool) $minimized;
1534
    }
1535
1536
    /**
1537
     * Return whether to group dates when presenting the user with
1538
     * filtering optiomd in the user interface.
1539
     *
1540
     * @return bool true if workbook window is minimized
1541
     */
1542 393
    public function getAutoFilterDateGrouping(): bool
1543
    {
1544 393
        return $this->autoFilterDateGrouping;
1545
    }
1546
1547
    /**
1548
     * Set whether to group dates when presenting the user with
1549
     * filtering optiomd in the user interface.
1550
     *
1551
     * @param bool $autoFilterDateGrouping true if workbook window is minimized
1552
     */
1553 268
    public function setAutoFilterDateGrouping(bool $autoFilterDateGrouping): void
1554
    {
1555 268
        $this->autoFilterDateGrouping = (bool) $autoFilterDateGrouping;
1556
    }
1557
1558
    /**
1559
     * Return the first sheet in the book view.
1560
     *
1561
     * @return int First sheet in book view
1562
     */
1563 393
    public function getFirstSheetIndex(): int
1564
    {
1565 393
        return $this->firstSheetIndex;
1566
    }
1567
1568
    /**
1569
     * Set the first sheet in the book view.
1570
     *
1571
     * @param int $firstSheetIndex First sheet in book view
1572
     */
1573 276
    public function setFirstSheetIndex(int $firstSheetIndex): void
1574
    {
1575 276
        if ($firstSheetIndex >= 0) {
1576 275
            $this->firstSheetIndex = (int) $firstSheetIndex;
1577
        } else {
1578 1
            throw new Exception('First sheet index must be a positive integer.');
1579
        }
1580
    }
1581
1582
    /**
1583
     * Return the visibility status of the workbook.
1584
     *
1585
     * This may be one of the following three values:
1586
     * - visibile
1587
     *
1588
     * @return string Visible status
1589
     */
1590 394
    public function getVisibility(): string
1591
    {
1592 394
        return $this->visibility;
1593
    }
1594
1595
    /**
1596
     * Set the visibility status of the workbook.
1597
     *
1598
     * Valid values are:
1599
     *  - 'visible' (self::VISIBILITY_VISIBLE):
1600
     *       Workbook window is visible
1601
     *  - 'hidden' (self::VISIBILITY_HIDDEN):
1602
     *       Workbook window is hidden, but can be shown by the user
1603
     *       via the user interface
1604
     *  - 'veryHidden' (self::VISIBILITY_VERY_HIDDEN):
1605
     *       Workbook window is hidden and cannot be shown in the
1606
     *       user interface.
1607
     *
1608
     * @param null|string $visibility visibility status of the workbook
1609
     */
1610 269
    public function setVisibility(?string $visibility): void
1611
    {
1612 269
        if ($visibility === null) {
1613 1
            $visibility = self::VISIBILITY_VISIBLE;
1614
        }
1615
1616 269
        if (in_array($visibility, self::WORKBOOK_VIEW_VISIBILITY_VALUES)) {
1617 269
            $this->visibility = $visibility;
1618
        } else {
1619 1
            throw new Exception('Invalid visibility value.');
1620
        }
1621
    }
1622
1623
    /**
1624
     * Get the ratio between the workbook tabs bar and the horizontal scroll bar.
1625
     * TabRatio is assumed to be out of 1000 of the horizontal window width.
1626
     *
1627
     * @return int Ratio between the workbook tabs bar and the horizontal scroll bar
1628
     */
1629 393
    public function getTabRatio(): int
1630
    {
1631 393
        return $this->tabRatio;
1632
    }
1633
1634
    /**
1635
     * Set the ratio between the workbook tabs bar and the horizontal scroll bar
1636
     * TabRatio is assumed to be out of 1000 of the horizontal window width.
1637
     *
1638
     * @param int $tabRatio Ratio between the tabs bar and the horizontal scroll bar
1639
     */
1640 280
    public function setTabRatio(int $tabRatio): void
1641
    {
1642 280
        if ($tabRatio >= 0 && $tabRatio <= 1000) {
1643 279
            $this->tabRatio = (int) $tabRatio;
1644
        } else {
1645 1
            throw new Exception('Tab ratio must be between 0 and 1000.');
1646
        }
1647
    }
1648
1649 2
    public function reevaluateAutoFilters(bool $resetToMax): void
1650
    {
1651 2
        foreach ($this->workSheetCollection as $sheet) {
1652 2
            $filter = $sheet->getAutoFilter();
1653 2
            if (!empty($filter->getRange())) {
1654 2
                if ($resetToMax) {
1655 1
                    $filter->setRangeToMaxRow();
1656
                }
1657 2
                $filter->showHideRows();
1658
            }
1659
        }
1660
    }
1661
1662
    /**
1663
     * @throws Exception
1664
     */
1665 1
    public function jsonSerialize(): mixed
1666
    {
1667 1
        throw new Exception('Spreadsheet objects cannot be json encoded');
1668
    }
1669
1670 1
    public function resetThemeFonts(): void
1671
    {
1672 1
        $majorFontLatin = $this->theme->getMajorFontLatin();
1673 1
        $minorFontLatin = $this->theme->getMinorFontLatin();
1674 1
        foreach ($this->cellXfCollection as $cellStyleXf) {
1675 1
            $scheme = $cellStyleXf->getFont()->getScheme();
1676 1
            if ($scheme === 'major') {
1677 1
                $cellStyleXf->getFont()->setName($majorFontLatin)->setScheme($scheme);
1678 1
            } elseif ($scheme === 'minor') {
1679 1
                $cellStyleXf->getFont()->setName($minorFontLatin)->setScheme($scheme);
1680
            }
1681
        }
1682 1
        foreach ($this->cellStyleXfCollection as $cellStyleXf) {
1683 1
            $scheme = $cellStyleXf->getFont()->getScheme();
1684 1
            if ($scheme === 'major') {
1685
                $cellStyleXf->getFont()->setName($majorFontLatin)->setScheme($scheme);
1686 1
            } elseif ($scheme === 'minor') {
1687 1
                $cellStyleXf->getFont()->setName($minorFontLatin)->setScheme($scheme);
1688
            }
1689
        }
1690
    }
1691
1692 39
    public function getTableByName(string $tableName): ?Table
1693
    {
1694 39
        $table = null;
1695 39
        foreach ($this->workSheetCollection as $sheet) {
1696 39
            $table = $sheet->getTableByName($tableName);
1697 39
            if ($table !== null) {
1698 5
                break;
1699
            }
1700
        }
1701
1702 39
        return $table;
1703
    }
1704
1705
    /**
1706
     * @return bool Success or failure
1707
     */
1708 806
    public function setExcelCalendar(int $baseYear): bool
1709
    {
1710 806
        if (($baseYear === Date::CALENDAR_WINDOWS_1900) || ($baseYear === Date::CALENDAR_MAC_1904)) {
1711 806
            $this->excelCalendar = $baseYear;
1712
1713 806
            return true;
1714
        }
1715
1716 1
        return false;
1717
    }
1718
1719
    /**
1720
     * @return int Excel base date (1900 or 1904)
1721
     */
1722 8493
    public function getExcelCalendar(): int
1723
    {
1724 8493
        return $this->excelCalendar;
1725
    }
1726
1727 2
    public function deleteLegacyDrawing(Worksheet $worksheet): void
1728
    {
1729 2
        unset($this->unparsedLoadedData['sheets'][$worksheet->getCodeName()]['legacyDrawing']);
1730
    }
1731
1732 3
    public function getLegacyDrawing(Worksheet $worksheet): ?string
1733
    {
1734
        /** @var ?string */
1735 3
        $temp = $this->unparsedLoadedData['sheets'][$worksheet->getCodeName()]['legacyDrawing'] ?? null;
1736
1737 3
        return $temp;
1738
    }
1739
1740 10073
    public function getValueBinder(): ?IValueBinder
1741
    {
1742 10073
        return $this->valueBinder;
1743
    }
1744
1745 1593
    public function setValueBinder(?IValueBinder $valueBinder): self
1746
    {
1747 1593
        $this->valueBinder = $valueBinder;
1748
1749 1593
        return $this;
1750
    }
1751
1752
    /**
1753
     * All the PDF writers treat charts as if they occupy a single cell.
1754
     * This will be better most of the time.
1755
     * It is not needed for any other output type.
1756
     * It changes the contents of the spreadsheet, so you might
1757
     * be better off cloning the spreadsheet and then using
1758
     * this method on, and then writing, the clone.
1759
     */
1760 1
    public function mergeChartCellsForPdf(): void
1761
    {
1762 1
        foreach ($this->workSheetCollection as $worksheet) {
1763 1
            foreach ($worksheet->getChartCollection() as $chart) {
1764 1
                $br = $chart->getBottomRightCell();
1765 1
                $tl = $chart->getTopLeftCell();
1766 1
                if ($br !== '' && $br !== $tl) {
1767 1
                    if (!$worksheet->cellExists($br)) {
1768 1
                        $worksheet->getCell($br)->setValue(' ');
1769
                    }
1770 1
                    $worksheet->mergeCells("$tl:$br");
1771
                }
1772
            }
1773
        }
1774
    }
1775
1776
    /**
1777
     * All the PDF writers do better with drawings than charts.
1778
     * This will be better some of the time.
1779
     * It is not needed for any other output type.
1780
     * It changes the contents of the spreadsheet, so you might
1781
     * be better off cloning the spreadsheet and then using
1782
     * this method on, and then writing, the clone.
1783
     */
1784 1
    public function mergeDrawingCellsForPdf(): void
1785
    {
1786 1
        foreach ($this->workSheetCollection as $worksheet) {
1787 1
            foreach ($worksheet->getDrawingCollection() as $drawing) {
1788 1
                $br = $drawing->getCoordinates2();
1789 1
                $tl = $drawing->getCoordinates();
1790 1
                if ($br !== '' && $br !== $tl) {
1791 1
                    if (!$worksheet->cellExists($br)) {
1792 1
                        $worksheet->getCell($br)->setValue(' ');
1793
                    }
1794 1
                    $worksheet->mergeCells("$tl:$br");
1795
                }
1796
            }
1797
        }
1798
    }
1799
1800
    /**
1801
     * Excel will sometimes replace user's formatting choice
1802
     * with a built-in choice that it thinks is equivalent.
1803
     * Its choice is often not equivalent after all.
1804
     * Such treatment is astonishingly user-hostile.
1805
     * This function will undo such changes.
1806
     */
1807 1
    public function replaceBuiltinNumberFormat(int $builtinFormatIndex, string $formatCode): void
1808
    {
1809 1
        foreach ($this->cellXfCollection as $style) {
1810 1
            $numberFormat = $style->getNumberFormat();
1811 1
            if ($numberFormat->getBuiltInFormatCode() === $builtinFormatIndex) {
1812 1
                $numberFormat->setFormatCode($formatCode);
1813
            }
1814
        }
1815
    }
1816
1817 34
    public function returnArrayAsArray(): void
1818
    {
1819 34
        $this->calculationEngine->setInstanceArrayReturnType(
1820 34
            Calculation::RETURN_ARRAY_AS_ARRAY
1821 34
        );
1822
    }
1823
1824 2
    public function returnArrayAsValue(): void
1825
    {
1826 2
        $this->calculationEngine->setInstanceArrayReturnType(
1827 2
            Calculation::RETURN_ARRAY_AS_VALUE
1828 2
        );
1829
    }
1830
}
1831