Gnumeric   F
last analyzed

Complexity

Total Complexity 112

Size/Duplication

Total Lines 599
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 112
eloc 308
dl 0
loc 599
ccs 317
cts 317
cp 1
rs 2
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A processMergedCells() 0 7 5
B processColumnWidths() 0 16 7
A processRowHeights() 0 13 6
A parseRichText() 0 6 1
A gnumericMappings() 0 3 1
A processComments() 0 10 6
A __construct() 0 5 1
A matchXml() 0 5 3
A canRead() 0 11 3
B loadIntoExisting() 0 86 10
A testSimpleXml() 0 3 2
B loadCell() 0 55 10
A loadSpreadsheetFromFile() 0 8 1
A setRowHeight() 0 6 1
A setSelectedSheet() 0 6 2
A listWorksheetNames() 0 24 5
A processRowLoop() 0 21 6
A setColumnInvisible() 0 7 1
A setSelectedCells() 0 18 5
A setRowInvisible() 0 6 1
A getArrayFormulaRange() 0 11 1
A gzfileGetContents() 0 22 6
B listWorksheetInfo() 0 49 9
A setColumnWidth() 0 7 1
A processColumnLoop() 0 21 6
B processDefinedNames() 0 17 7
A processAutofilter() 0 7 5

How to fix   Complexity   

Complex Class

Complex classes like Gnumeric 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 Gnumeric, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Reader;
4
5
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Cell\DataType;
7
use PhpOffice\PhpSpreadsheet\DefinedName;
8
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\PageSetup;
9
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\Properties;
10
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\Styles;
11
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
12
use PhpOffice\PhpSpreadsheet\ReferenceHelper;
13
use PhpOffice\PhpSpreadsheet\RichText\RichText;
14
use PhpOffice\PhpSpreadsheet\Shared\File;
15
use PhpOffice\PhpSpreadsheet\Spreadsheet;
16
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
17
use SimpleXMLElement;
18
use XMLReader;
19
20
class Gnumeric extends BaseReader
21
{
22
    const NAMESPACE_GNM = 'http://www.gnumeric.org/v10.dtd'; // gmr in old sheets
23
24
    const NAMESPACE_XSI = 'http://www.w3.org/2001/XMLSchema-instance';
25
26
    const NAMESPACE_OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
27
28
    const NAMESPACE_XLINK = 'http://www.w3.org/1999/xlink';
29
30
    const NAMESPACE_DC = 'http://purl.org/dc/elements/1.1/';
31
32
    const NAMESPACE_META = 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0';
33
34
    const NAMESPACE_OOO = 'http://openoffice.org/2004/office';
35
36
    const GNM_SHEET_VISIBILITY_VISIBLE = 'GNM_SHEET_VISIBILITY_VISIBLE';
37
    const GNM_SHEET_VISIBILITY_HIDDEN = 'GNM_SHEET_VISIBILITY_HIDDEN';
38
39
    /**
40
     * Shared Expressions.
41
     *
42
     * @var array<array{column: int, row: int, formula:string}>
43
     */
44
    private array $expressions = [];
45
46
    /**
47
     * Spreadsheet shared across all functions.
48
     */
49
    private Spreadsheet $spreadsheet;
50
51
    private ReferenceHelper $referenceHelper;
52
53
    /** @var array{'dataType': string[]} */
54
    public static array $mappings = [
55
        'dataType' => [
56
            '10' => DataType::TYPE_NULL,
57
            '20' => DataType::TYPE_BOOL,
58
            '30' => DataType::TYPE_NUMERIC, // Integer doesn't exist in Excel
59
            '40' => DataType::TYPE_NUMERIC, // Float
60
            '50' => DataType::TYPE_ERROR,
61
            '60' => DataType::TYPE_STRING,
62
            //'70':        //    Cell Range
63
            //'80':        //    Array
64
        ],
65
    ];
66
67
    /**
68
     * Create a new Gnumeric.
69
     */
70 50
    public function __construct()
71
    {
72 50
        parent::__construct();
73 50
        $this->referenceHelper = ReferenceHelper::getInstance();
74 50
        $this->securityScanner = XmlScanner::getInstance($this);
75
    }
76
77
    /**
78
     * Can the current IReader read the file?
79
     */
80 49
    public function canRead(string $filename): bool
81
    {
82 49
        $data = null;
83 49
        if (File::testFileNoThrow($filename)) {
84 49
            $data = $this->gzfileGetContents($filename);
85 48
            if (!str_contains($data, self::NAMESPACE_GNM)) {
86 18
                $data = '';
87
            }
88
        }
89
90 48
        return !empty($data);
91
    }
92
93 3
    private static function matchXml(XMLReader $xml, string $expectedLocalName): bool
94
    {
95 3
        return $xml->namespaceURI === self::NAMESPACE_GNM
96 3
            && $xml->localName === $expectedLocalName
97 3
            && $xml->nodeType === XMLReader::ELEMENT;
98
    }
99
100
    /**
101
     * Reads names of the worksheets from a file, without parsing the whole file to a Spreadsheet object.
102
     *
103
     * @return string[]
104
     */
105 2
    public function listWorksheetNames(string $filename): array
106
    {
107 2
        File::assertFile($filename);
108 2
        if (!$this->canRead($filename)) {
109 1
            throw new Exception($filename . ' is an invalid Gnumeric file.');
110
        }
111
112 1
        $xml = new XMLReader();
113 1
        $contents = $this->gzfileGetContents($filename);
114 1
        $xml->xml($contents);
115 1
        $xml->setParserProperty(2, true);
116
117 1
        $worksheetNames = [];
118 1
        while ($xml->read()) {
119 1
            if (self::matchXml($xml, 'SheetName')) {
120 1
                $xml->read(); //    Move onto the value node
121 1
                $worksheetNames[] = (string) $xml->value;
122 1
            } elseif (self::matchXml($xml, 'Sheets')) {
123
                //    break out of the loop once we've got our sheet names rather than parse the entire file
124 1
                break;
125
            }
126
        }
127
128 1
        return $worksheetNames;
129
    }
130
131
    /**
132
     * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
133
     *
134
     * @return array<int, array{worksheetName: string, lastColumnLetter: string, lastColumnIndex: int, totalRows: int, totalColumns: int, sheetState: string}>
135
     */
136 3
    public function listWorksheetInfo(string $filename): array
137
    {
138 3
        File::assertFile($filename);
139 3
        if (!$this->canRead($filename)) {
140 1
            throw new Exception($filename . ' is an invalid Gnumeric file.');
141
        }
142
143 2
        $xml = new XMLReader();
144 2
        $contents = $this->gzfileGetContents($filename);
145 2
        $xml->xml($contents);
146 2
        $xml->setParserProperty(2, true);
147
148 2
        $worksheetInfo = [];
149 2
        while ($xml->read()) {
150 2
            if (self::matchXml($xml, 'Sheet')) {
151 2
                $tmpInfo = [
152 2
                    'worksheetName' => '',
153 2
                    'lastColumnLetter' => 'A',
154 2
                    'lastColumnIndex' => 0,
155 2
                    'totalRows' => 0,
156 2
                    'totalColumns' => 0,
157 2
                    'sheetState' => Worksheet::SHEETSTATE_VISIBLE,
158 2
                ];
159 2
                $visibility = $xml->getAttribute('Visibility');
160 2
                if ((string) $visibility === self::GNM_SHEET_VISIBILITY_HIDDEN) {
161 1
                    $tmpInfo['sheetState'] = Worksheet::SHEETSTATE_HIDDEN;
162
                }
163
164 2
                while ($xml->read()) {
165 2
                    if (self::matchXml($xml, 'Name')) {
166 2
                        $xml->read(); //    Move onto the value node
167 2
                        $tmpInfo['worksheetName'] = (string) $xml->value;
168 2
                    } elseif (self::matchXml($xml, 'MaxCol')) {
169 2
                        $xml->read(); //    Move onto the value node
170 2
                        $tmpInfo['lastColumnIndex'] = (int) $xml->value;
171 2
                        $tmpInfo['totalColumns'] = (int) $xml->value + 1;
172 2
                    } elseif (self::matchXml($xml, 'MaxRow')) {
173 2
                        $xml->read(); //    Move onto the value node
174 2
                        $tmpInfo['totalRows'] = (int) $xml->value + 1;
175
176 2
                        break;
177
                    }
178
                }
179 2
                $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
180 2
                $worksheetInfo[] = $tmpInfo;
181
            }
182
        }
183
184 2
        return $worksheetInfo;
185
    }
186
187 49
    private function gzfileGetContents(string $filename): string
188
    {
189 49
        $data = '';
190 49
        $contents = @file_get_contents($filename);
191 49
        if ($contents !== false) {
192 49
            if (str_starts_with($contents, "\x1f\x8b")) {
193
                // Check if gzlib functions are available
194 28
                if (function_exists('gzdecode')) {
195 28
                    $contents = @gzdecode($contents);
196 28
                    if ($contents !== false) {
197 28
                        $data = $contents;
198
                    }
199
                }
200
            } else {
201 21
                $data = $contents;
202
            }
203
        }
204 49
        if ($data !== '') {
205 48
            $data = $this->getSecurityScannerOrThrow()->scan($data);
206
        }
207
208 48
        return $data;
209
    }
210
211
    /** @return mixed[] */
212 59
    public static function gnumericMappings(): array
213
    {
214 59
        return array_merge(self::$mappings, Styles::$mappings);
215
    }
216
217 26
    private function processComments(SimpleXMLElement $sheet): void
218
    {
219 26
        if ((!$this->readDataOnly) && (isset($sheet->Objects))) {
220 5
            foreach ($sheet->Objects->children(self::NAMESPACE_GNM) as $key => $comment) {
221 5
                $commentAttributes = $comment->attributes();
222
                //    Only comment objects are handled at the moment
223 5
                if ($commentAttributes && $commentAttributes->Text) {
224 4
                    $this->spreadsheet->getActiveSheet()->getComment((string) $commentAttributes->ObjectBound)
225 4
                        ->setAuthor((string) $commentAttributes->Author)
226 4
                        ->setText($this->parseRichText((string) $commentAttributes->Text));
227
                }
228
            }
229
        }
230
    }
231
232 27
    private static function testSimpleXml(mixed $value): SimpleXMLElement
233
    {
234 27
        return ($value instanceof SimpleXMLElement) ? $value : new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>');
235
    }
236
237
    /**
238
     * Loads Spreadsheet from file.
239
     */
240 30
    protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
241
    {
242 30
        $spreadsheet = $this->newSpreadsheet();
243 30
        $spreadsheet->setValueBinder($this->valueBinder);
244 30
        $spreadsheet->removeSheetByIndex(0);
245
246
        // Load into this instance
247 30
        return $this->loadIntoExisting($filename, $spreadsheet);
248
    }
249
250
    /**
251
     * Loads from file into Spreadsheet instance.
252
     */
253 30
    public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
254
    {
255 30
        $this->spreadsheet = $spreadsheet;
256 30
        File::assertFile($filename);
257 30
        if (!$this->canRead($filename)) {
258 2
            throw new Exception($filename . ' is an invalid Gnumeric file.');
259
        }
260
261 27
        $gFileData = $this->gzfileGetContents($filename);
262
263
        /** @var XmlScanner */
264 27
        $securityScanner = $this->securityScanner;
265 27
        $xml2 = simplexml_load_string($securityScanner->scan($gFileData));
266 27
        $xml = self::testSimpleXml($xml2);
267
268 27
        $gnmXML = $xml->children(self::NAMESPACE_GNM);
269 27
        (new Properties($this->spreadsheet))->readProperties($xml, $gnmXML);
270
271 27
        $worksheetID = 0;
272 27
        foreach ($gnmXML->Sheets->Sheet as $sheetOrNull) {
273 27
            $sheet = self::testSimpleXml($sheetOrNull);
274 27
            $worksheetName = (string) $sheet->Name;
275 27
            if (is_array($this->loadSheetsOnly) && !in_array($worksheetName, $this->loadSheetsOnly, true)) {
276 2
                continue;
277
            }
278
279 26
            $maxRow = $maxCol = 0;
280
281
            // Create new Worksheet
282 26
            $this->spreadsheet->createSheet();
283 26
            $this->spreadsheet->setActiveSheetIndex($worksheetID);
284
            //    Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
285
            //        cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
286
            //        name in line with the formula, not the reverse
287 26
            $this->spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false);
288
289 26
            $visibility = $sheet->attributes()['Visibility'] ?? self::GNM_SHEET_VISIBILITY_VISIBLE;
290 26
            if ((string) $visibility !== self::GNM_SHEET_VISIBILITY_VISIBLE) {
291 1
                $this->spreadsheet->getActiveSheet()->setSheetState(Worksheet::SHEETSTATE_HIDDEN);
292
            }
293
294 26
            if (!$this->readDataOnly) {
295 26
                (new PageSetup($this->spreadsheet))
296 26
                    ->printInformation($sheet)
297 26
                    ->sheetMargins($sheet);
298
            }
299
300 26
            foreach ($sheet->Cells->Cell as $cellOrNull) {
301 23
                $cell = self::testSimpleXml($cellOrNull);
302 23
                $cellAttributes = self::testSimpleXml($cell->attributes());
303 23
                $row = (int) $cellAttributes->Row + 1;
304 23
                $column = (int) $cellAttributes->Col;
305
306 23
                $maxRow = max($maxRow, $row);
307 23
                $maxCol = max($maxCol, $column);
308
309 23
                $column = Coordinate::stringFromColumnIndex($column + 1);
310
311
                // Read cell?
312 23
                if (!$this->getReadFilter()->readCell($column, $row, $worksheetName)) {
313 1
                    continue;
314
                }
315
316 23
                $this->loadCell($cell, $worksheetName, $cellAttributes, $column, $row);
317
            }
318
319 26
            if ($sheet->Styles !== null) {
320 26
                (new Styles($this->spreadsheet, $this->readDataOnly))->read($sheet, $maxRow, $maxCol);
321
            }
322
323 26
            $this->processComments($sheet);
324 26
            $this->processColumnWidths($sheet, $maxCol);
325 26
            $this->processRowHeights($sheet, $maxRow);
326 26
            $this->processMergedCells($sheet);
327 26
            $this->processAutofilter($sheet);
328
329 26
            $this->setSelectedCells($sheet);
330 26
            ++$worksheetID;
331
        }
332
333 27
        $this->processDefinedNames($gnmXML);
334
335 27
        $this->setSelectedSheet($gnmXML);
336
337
        // Return
338 26
        return $this->spreadsheet;
339
    }
340
341 27
    private function setSelectedSheet(SimpleXMLElement $gnmXML): void
342
    {
343 27
        if (isset($gnmXML->UIData)) {
344 27
            $attributes = self::testSimpleXml($gnmXML->UIData->attributes());
345 27
            $selectedSheet = (int) $attributes['SelectedTab'];
346 27
            $this->spreadsheet->setActiveSheetIndex($selectedSheet);
347
        }
348
    }
349
350 26
    private function setSelectedCells(?SimpleXMLElement $sheet): void
351
    {
352 26
        if ($sheet !== null && isset($sheet->Selections)) {
353 26
            foreach ($sheet->Selections as $selection) {
354 26
                $startCol = (int) ($selection->StartCol ?? 0);
355 26
                $startRow = (int) ($selection->StartRow ?? 0) + 1;
356 26
                $endCol = (int) ($selection->EndCol ?? $startCol);
357 26
                $endRow = (int) ($selection->endRow ?? 0) + 1;
358
359 26
                $startColumn = Coordinate::stringFromColumnIndex($startCol + 1);
360 26
                $endColumn = Coordinate::stringFromColumnIndex($endCol + 1);
361
362 26
                $startCell = "{$startColumn}{$startRow}";
363 26
                $endCell = "{$endColumn}{$endRow}";
364 26
                $selectedRange = $startCell . (($endCell !== $startCell) ? ':' . $endCell : '');
365 26
                $this->spreadsheet->getActiveSheet()->setSelectedCell($selectedRange);
366
367 26
                break;
368
            }
369
        }
370
    }
371
372 26
    private function processMergedCells(?SimpleXMLElement $sheet): void
373
    {
374
        //    Handle Merged Cells in this worksheet
375 26
        if ($sheet !== null && isset($sheet->MergedRegions)) {
376 4
            foreach ($sheet->MergedRegions->Merge as $mergeCells) {
377 4
                if (str_contains((string) $mergeCells, ':')) {
378 4
                    $this->spreadsheet->getActiveSheet()->mergeCells($mergeCells, Worksheet::MERGE_CELL_CONTENT_HIDE);
379
                }
380
            }
381
        }
382
    }
383
384 26
    private function processAutofilter(?SimpleXMLElement $sheet): void
385
    {
386 26
        if ($sheet !== null && isset($sheet->Filters)) {
387 1
            foreach ($sheet->Filters->Filter as $autofilter) {
388 1
                $attributes = $autofilter->attributes();
389 1
                if (isset($attributes['Area'])) {
390 1
                    $this->spreadsheet->getActiveSheet()->setAutoFilter((string) $attributes['Area']);
391
                }
392
            }
393
        }
394
    }
395
396 26
    private function setColumnWidth(int $whichColumn, float $defaultWidth): void
397
    {
398 26
        $this->spreadsheet->getActiveSheet()
399 26
            ->getColumnDimension(
400 26
                Coordinate::stringFromColumnIndex($whichColumn + 1)
401 26
            )
402 26
            ->setWidth($defaultWidth);
403
    }
404
405 5
    private function setColumnInvisible(int $whichColumn): void
406
    {
407 5
        $this->spreadsheet->getActiveSheet()
408 5
            ->getColumnDimension(
409 5
                Coordinate::stringFromColumnIndex($whichColumn + 1)
410 5
            )
411 5
            ->setVisible(false);
412
    }
413
414 16
    private function processColumnLoop(int $whichColumn, int $maxCol, ?SimpleXMLElement $columnOverride, float $defaultWidth): int
415
    {
416 16
        $columnOverride = self::testSimpleXml($columnOverride);
417 16
        $columnAttributes = self::testSimpleXml($columnOverride->attributes());
418 16
        $column = $columnAttributes['No'];
419 16
        $columnWidth = ((float) $columnAttributes['Unit']) / 5.4;
420 16
        $hidden = (isset($columnAttributes['Hidden'])) && ((string) $columnAttributes['Hidden'] == '1');
421 16
        $columnCount = (int) ($columnAttributes['Count'] ?? 1);
422 16
        while ($whichColumn < $column) {
423 15
            $this->setColumnWidth($whichColumn, $defaultWidth);
424 15
            ++$whichColumn;
425
        }
426 16
        while (($whichColumn < ($column + $columnCount)) && ($whichColumn <= $maxCol)) {
427 16
            $this->setColumnWidth($whichColumn, $columnWidth);
428 16
            if ($hidden) {
429 5
                $this->setColumnInvisible($whichColumn);
430
            }
431 16
            ++$whichColumn;
432
        }
433
434 16
        return $whichColumn;
435
    }
436
437 26
    private function processColumnWidths(?SimpleXMLElement $sheet, int $maxCol): void
438
    {
439 26
        if ((!$this->readDataOnly) && $sheet !== null && (isset($sheet->Cols))) {
440
            //    Column Widths
441 26
            $defaultWidth = 0;
442 26
            $columnAttributes = $sheet->Cols->attributes();
443 26
            if ($columnAttributes !== null) {
444 26
                $defaultWidth = $columnAttributes['DefaultSizePts'] / 5.4;
445
            }
446 26
            $whichColumn = 0;
447 26
            foreach ($sheet->Cols->ColInfo as $columnOverride) {
448 16
                $whichColumn = $this->processColumnLoop($whichColumn, $maxCol, $columnOverride, $defaultWidth);
449
            }
450 26
            while ($whichColumn <= $maxCol) {
451 15
                $this->setColumnWidth($whichColumn, $defaultWidth);
452 15
                ++$whichColumn;
453
            }
454
        }
455
    }
456
457 15
    private function setRowHeight(int $whichRow, float $defaultHeight): void
458
    {
459 15
        $this->spreadsheet
460 15
            ->getActiveSheet()
461 15
            ->getRowDimension($whichRow)
462 15
            ->setRowHeight($defaultHeight);
463
    }
464
465 4
    private function setRowInvisible(int $whichRow): void
466
    {
467 4
        $this->spreadsheet
468 4
            ->getActiveSheet()
469 4
            ->getRowDimension($whichRow)
470 4
            ->setVisible(false);
471
    }
472
473 15
    private function processRowLoop(int $whichRow, int $maxRow, ?SimpleXMLElement $rowOverride, float $defaultHeight): int
474
    {
475 15
        $rowOverride = self::testSimpleXml($rowOverride);
476 15
        $rowAttributes = self::testSimpleXml($rowOverride->attributes());
477 15
        $row = $rowAttributes['No'];
478 15
        $rowHeight = (float) $rowAttributes['Unit'];
479 15
        $hidden = (isset($rowAttributes['Hidden'])) && ((string) $rowAttributes['Hidden'] == '1');
480 15
        $rowCount = (int) ($rowAttributes['Count'] ?? 1);
481 15
        while ($whichRow < $row) {
482 7
            ++$whichRow;
483 7
            $this->setRowHeight($whichRow, $defaultHeight);
484
        }
485 15
        while (($whichRow < ($row + $rowCount)) && ($whichRow < $maxRow)) {
486 15
            ++$whichRow;
487 15
            $this->setRowHeight($whichRow, $rowHeight);
488 15
            if ($hidden) {
489 4
                $this->setRowInvisible($whichRow);
490
            }
491
        }
492
493 15
        return $whichRow;
494
    }
495
496 26
    private function processRowHeights(?SimpleXMLElement $sheet, int $maxRow): void
497
    {
498 26
        if ((!$this->readDataOnly) && $sheet !== null && (isset($sheet->Rows))) {
499
            //    Row Heights
500 26
            $defaultHeight = 0;
501 26
            $rowAttributes = $sheet->Rows->attributes();
502 26
            if ($rowAttributes !== null) {
503 26
                $defaultHeight = (float) $rowAttributes['DefaultSizePts'];
504
            }
505 26
            $whichRow = 0;
506
507 26
            foreach ($sheet->Rows->RowInfo as $rowOverride) {
508 15
                $whichRow = $this->processRowLoop($whichRow, $maxRow, $rowOverride, $defaultHeight);
509
            }
510
            // never executed, I can't figure out any circumstances
511
            // under which it would be executed, and, even if
512
            // such exist, I'm not convinced this is needed.
513
            //while ($whichRow < $maxRow) {
514
            //    ++$whichRow;
515
            //    $this->spreadsheet->getActiveSheet()->getRowDimension($whichRow)->setRowHeight($defaultHeight);
516
            //}
517
        }
518
    }
519
520 27
    private function processDefinedNames(?SimpleXMLElement $gnmXML): void
521
    {
522
        //    Loop through definedNames (global named ranges)
523 27
        if ($gnmXML !== null && isset($gnmXML->Names)) {
524 10
            foreach ($gnmXML->Names->Name as $definedName) {
525 8
                $name = (string) $definedName->name;
526 8
                $value = (string) $definedName->value;
527 8
                if (stripos($value, '#REF!') !== false || empty($value)) {
528 6
                    continue;
529
                }
530
531 8
                $value = str_replace("\\'", "''", $value);
532 8
                [$worksheetName] = Worksheet::extractSheetTitle($value, true, true);
533 8
                $worksheet = $this->spreadsheet->getSheetByName($worksheetName);
534
                // Worksheet might still be null if we're only loading selected sheets rather than the full spreadsheet
535 8
                if ($worksheet !== null) {
536 6
                    $this->spreadsheet->addDefinedName(DefinedName::createInstance($name, $worksheet, $value));
537
                }
538
            }
539
        }
540
    }
541
542 4
    private function parseRichText(string $is): RichText
543
    {
544 4
        $value = new RichText();
545 4
        $value->createText($is);
546
547 4
        return $value;
548
    }
549
550 23
    private function loadCell(
551
        SimpleXMLElement $cell,
552
        string $worksheetName,
553
        SimpleXMLElement $cellAttributes,
554
        string $column,
555
        int $row
556
    ): void {
557 23
        $ValueType = $cellAttributes->ValueType;
558 23
        $ExprID = (string) $cellAttributes->ExprID;
559 23
        $rows = (int) ($cellAttributes->Rows ?? 0);
560 23
        $cols = (int) ($cellAttributes->Cols ?? 0);
561 23
        $type = DataType::TYPE_FORMULA;
562 23
        $isArrayFormula = ($rows > 0 && $cols > 0);
563 23
        $arrayFormulaRange = $isArrayFormula ? $this->getArrayFormulaRange($column, $row, $cols, $rows) : null;
564 23
        if ($ExprID > '') {
565 19
            if (((string) $cell) > '') {
566
                // Formula
567 19
                $this->expressions[$ExprID] = [
568 19
                    'column' => (int) $cellAttributes->Col,
569 19
                    'row' => (int) $cellAttributes->Row,
570 19
                    'formula' => (string) $cell,
571 19
                ];
572
            } else {
573
                // Shared Formula
574 10
                $expression = $this->expressions[$ExprID];
575
576 10
                $cell = $this->referenceHelper->updateFormulaReferences(
577 10
                    $expression['formula'],
578 10
                    'A1',
579 10
                    $cellAttributes->Col - $expression['column'],
580 10
                    $cellAttributes->Row - $expression['row'],
581 10
                    $worksheetName
582 10
                );
583
            }
584 19
            $type = DataType::TYPE_FORMULA;
585 23
        } elseif ($isArrayFormula === false) {
586 23
            $vtype = (string) $ValueType;
587 23
            if (array_key_exists($vtype, self::$mappings['dataType'])) {
588 23
                $type = self::$mappings['dataType'][$vtype];
589
            }
590 23
            if ($vtype === '20') { //    Boolean
591 4
                $cell = $cell == 'TRUE';
592
            }
593
        }
594
595 23
        $this->spreadsheet->getActiveSheet()->getCell($column . $row)->setValueExplicit((string) $cell, $type);
596 23
        if ($arrayFormulaRange === null) {
597 23
            $this->spreadsheet->getActiveSheet()->getCell($column . $row)->setFormulaAttributes(null);
598
        } else {
599 10
            $this->spreadsheet->getActiveSheet()->getCell($column . $row)->setFormulaAttributes(['t' => 'array', 'ref' => $arrayFormulaRange]);
600
        }
601 23
        if (isset($cellAttributes->ValueFormat)) {
602 4
            $this->spreadsheet->getActiveSheet()->getCell($column . $row)
603 4
                ->getStyle()->getNumberFormat()
604 4
                ->setFormatCode((string) $cellAttributes->ValueFormat);
605
        }
606
    }
607
608 10
    private function getArrayFormulaRange(string $column, int $row, int $cols, int $rows): string
609
    {
610 10
        $arrayFormulaRange = $column . $row;
611 10
        $arrayFormulaRange .= ':'
612 10
            . Coordinate::stringFromColumnIndex(
613 10
                Coordinate::columnIndexFromString($column)
614 10
                + $cols - 1
615 10
            )
616 10
            . (string) ($row + $rows - 1);
617
618 10
        return $arrayFormulaRange;
619
    }
620
}
621