Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Reader;
4
 
5
use DOMAttr;
6
use DOMDocument;
7
use DOMElement;
8
use DOMNode;
9
use DOMText;
10
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
11
use PhpOffice\PhpSpreadsheet\Cell\DataType;
12
use PhpOffice\PhpSpreadsheet\Helper\Dimension as HelperDimension;
13
use PhpOffice\PhpSpreadsheet\Reader\Ods\AutoFilter;
14
use PhpOffice\PhpSpreadsheet\Reader\Ods\DefinedNames;
15
use PhpOffice\PhpSpreadsheet\Reader\Ods\FormulaTranslator;
16
use PhpOffice\PhpSpreadsheet\Reader\Ods\PageSettings;
17
use PhpOffice\PhpSpreadsheet\Reader\Ods\Properties as DocumentProperties;
18
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
19
use PhpOffice\PhpSpreadsheet\RichText\RichText;
20
use PhpOffice\PhpSpreadsheet\Shared\Date;
21
use PhpOffice\PhpSpreadsheet\Shared\File;
22
use PhpOffice\PhpSpreadsheet\Spreadsheet;
23
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
24
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
25
use Throwable;
26
use XMLReader;
27
use ZipArchive;
28
 
29
class Ods extends BaseReader
30
{
31
    const INITIAL_FILE = 'content.xml';
32
 
33
    /**
34
     * Create a new Ods Reader instance.
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
        $this->securityScanner = XmlScanner::getInstance($this);
40
    }
41
 
42
    /**
43
     * Can the current IReader read the file?
44
     */
45
    public function canRead(string $filename): bool
46
    {
47
        $mimeType = 'UNKNOWN';
48
 
49
        // Load file
50
 
51
        if (File::testFileNoThrow($filename, '')) {
52
            $zip = new ZipArchive();
53
            if ($zip->open($filename) === true) {
54
                // check if it is an OOXML archive
55
                $stat = $zip->statName('mimetype');
56
                if (!empty($stat) && ($stat['size'] <= 255)) {
57
                    $mimeType = $zip->getFromName($stat['name']);
58
                } elseif ($zip->statName('META-INF/manifest.xml')) {
59
                    $xml = simplexml_load_string(
60
                        $this->getSecurityScannerOrThrow()
61
                            ->scan(
62
                                $zip->getFromName(
63
                                    'META-INF/manifest.xml'
64
                                )
65
                            )
66
                    );
67
                    if ($xml !== false) {
68
                        $namespacesContent = $xml->getNamespaces(true);
69
                        if (isset($namespacesContent['manifest'])) {
70
                            $manifest = $xml->children($namespacesContent['manifest']);
71
                            foreach ($manifest as $manifestDataSet) {
72
                                $manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']);
73
                                if ($manifestAttributes && $manifestAttributes->{'full-path'} == '/') {
74
                                    $mimeType = (string) $manifestAttributes->{'media-type'};
75
 
76
                                    break;
77
                                }
78
                            }
79
                        }
80
                    }
81
                }
82
 
83
                $zip->close();
84
            }
85
        }
86
 
87
        return $mimeType === 'application/vnd.oasis.opendocument.spreadsheet';
88
    }
89
 
90
    /**
91
     * Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
92
     *
93
     * @return string[]
94
     */
95
    public function listWorksheetNames(string $filename): array
96
    {
97
        File::assertFile($filename, self::INITIAL_FILE);
98
 
99
        $worksheetNames = [];
100
 
101
        $xml = new XMLReader();
102
        $xml->xml(
103
            $this->getSecurityScannerOrThrow()
104
                ->scanFile(
105
                    'zip://' . realpath($filename) . '#' . self::INITIAL_FILE
106
                )
107
        );
108
        $xml->setParserProperty(2, true);
109
 
110
        // Step into the first level of content of the XML
111
        $xml->read();
112
        while ($xml->read()) {
113
            // Quickly jump through to the office:body node
114
            while ($xml->name !== 'office:body') {
115
                if ($xml->isEmptyElement) {
116
                    $xml->read();
117
                } else {
118
                    $xml->next();
119
                }
120
            }
121
            // Now read each node until we find our first table:table node
122
            while ($xml->read()) {
123
                $xmlName = $xml->name;
124
                if ($xmlName == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
125
                    // Loop through each table:table node reading the table:name attribute for each worksheet name
126
                    do {
127
                        $worksheetName = $xml->getAttribute('table:name');
128
                        if (!empty($worksheetName)) {
129
                            $worksheetNames[] = $worksheetName;
130
                        }
131
                        $xml->next();
132
                    } while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT);
133
                }
134
            }
135
        }
136
 
137
        return $worksheetNames;
138
    }
139
 
140
    /**
141
     * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
142
     */
143
    public function listWorksheetInfo(string $filename): array
144
    {
145
        File::assertFile($filename, self::INITIAL_FILE);
146
 
147
        $worksheetInfo = [];
148
 
149
        $xml = new XMLReader();
150
        $xml->xml(
151
            $this->getSecurityScannerOrThrow()
152
                ->scanFile(
153
                    'zip://' . realpath($filename) . '#' . self::INITIAL_FILE
154
                )
155
        );
156
        $xml->setParserProperty(2, true);
157
 
158
        // Step into the first level of content of the XML
159
        $xml->read();
160
        $tableVisibility = [];
161
        $lastTableStyle = '';
162
 
163
        while ($xml->read()) {
164
            if ($xml->name === 'style:style') {
165
                $styleType = $xml->getAttribute('style:family');
166
                if ($styleType === 'table') {
167
                    $lastTableStyle = $xml->getAttribute('style:name');
168
                }
169
            } elseif ($xml->name === 'style:table-properties') {
170
                $visibility = $xml->getAttribute('table:display');
171
                $tableVisibility[$lastTableStyle] = ($visibility === 'false') ? Worksheet::SHEETSTATE_HIDDEN : Worksheet::SHEETSTATE_VISIBLE;
172
            } elseif ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
173
                $worksheetNames[] = $xml->getAttribute('table:name');
174
 
175
                $styleName = $xml->getAttribute('table:style-name') ?? '';
176
                $visibility = $tableVisibility[$styleName] ?? '';
177
                $tmpInfo = [
178
                    'worksheetName' => $xml->getAttribute('table:name'),
179
                    'lastColumnLetter' => 'A',
180
                    'lastColumnIndex' => 0,
181
                    'totalRows' => 0,
182
                    'totalColumns' => 0,
183
                    'sheetState' => $visibility,
184
                ];
185
 
186
                // Loop through each child node of the table:table element reading
187
                $currCells = 0;
188
                do {
189
                    $xml->read();
190
                    if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
191
                        $rowspan = $xml->getAttribute('table:number-rows-repeated');
192
                        $rowspan = empty($rowspan) ? 1 : $rowspan;
193
                        $tmpInfo['totalRows'] += $rowspan;
194
                        $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
195
                        $currCells = 0;
196
                        // Step into the row
197
                        $xml->read();
198
                        do {
199
                            $doread = true;
200
                            if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
201
                                if (!$xml->isEmptyElement) {
202
                                    ++$currCells;
203
                                    $xml->next();
204
                                    $doread = false;
205
                                }
206
                            } elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
207
                                $mergeSize = $xml->getAttribute('table:number-columns-repeated');
208
                                $currCells += (int) $mergeSize;
209
                            }
210
                            if ($doread) {
211
                                $xml->read();
212
                            }
213
                        } while ($xml->name != 'table:table-row');
214
                    }
215
                } while ($xml->name != 'table:table');
216
 
217
                $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
218
                $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
219
                $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
220
                $worksheetInfo[] = $tmpInfo;
221
            }
222
        }
223
 
224
        return $worksheetInfo;
225
    }
226
 
227
    /**
228
     * Loads PhpSpreadsheet from file.
229
     */
230
    protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
231
    {
232
        // Create new Spreadsheet
233
        $spreadsheet = new Spreadsheet();
234
        $spreadsheet->setValueBinder($this->valueBinder);
235
        $spreadsheet->removeSheetByIndex(0);
236
 
237
        // Load into this instance
238
        return $this->loadIntoExisting($filename, $spreadsheet);
239
    }
240
 
241
    /**
242
     * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
243
     */
244
    public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
245
    {
246
        File::assertFile($filename, self::INITIAL_FILE);
247
 
248
        $zip = new ZipArchive();
249
        $zip->open($filename);
250
 
251
        // Meta
252
 
253
        $xml = @simplexml_load_string(
254
            $this->getSecurityScannerOrThrow()
255
                ->scan($zip->getFromName('meta.xml'))
256
        );
257
        if ($xml === false) {
258
            throw new Exception('Unable to read data from {$pFilename}');
259
        }
260
 
261
        $namespacesMeta = $xml->getNamespaces(true);
262
 
263
        (new DocumentProperties($spreadsheet))->load($xml, $namespacesMeta);
264
 
265
        // Styles
266
 
267
        $dom = new DOMDocument('1.01', 'UTF-8');
268
        $dom->loadXML(
269
            $this->getSecurityScannerOrThrow()
270
                ->scan($zip->getFromName('styles.xml'))
271
        );
272
 
273
        $pageSettings = new PageSettings($dom);
274
 
275
        // Main Content
276
 
277
        $dom = new DOMDocument('1.01', 'UTF-8');
278
        $dom->loadXML(
279
            $this->getSecurityScannerOrThrow()
280
                ->scan($zip->getFromName(self::INITIAL_FILE))
281
        );
282
 
283
        $officeNs = (string) $dom->lookupNamespaceUri('office');
284
        $tableNs = (string) $dom->lookupNamespaceUri('table');
285
        $textNs = (string) $dom->lookupNamespaceUri('text');
286
        $xlinkNs = (string) $dom->lookupNamespaceUri('xlink');
287
        $styleNs = (string) $dom->lookupNamespaceUri('style');
288
 
289
        $pageSettings->readStyleCrossReferences($dom);
290
 
291
        $autoFilterReader = new AutoFilter($spreadsheet, $tableNs);
292
        $definedNameReader = new DefinedNames($spreadsheet, $tableNs);
293
        $columnWidths = [];
294
        $automaticStyle0 = $dom->getElementsByTagNameNS($officeNs, 'automatic-styles')->item(0);
295
        $automaticStyles = ($automaticStyle0 === null) ? [] : $automaticStyle0->getElementsByTagNameNS($styleNs, 'style');
296
        foreach ($automaticStyles as $automaticStyle) {
297
            $styleName = $automaticStyle->getAttributeNS($styleNs, 'name');
298
            $styleFamily = $automaticStyle->getAttributeNS($styleNs, 'family');
299
            if ($styleFamily === 'table-column') {
300
                $tcprops = $automaticStyle->getElementsByTagNameNS($styleNs, 'table-column-properties');
301
                $tcprop = $tcprops->item(0);
302
                if ($tcprop !== null) {
303
                    $columnWidth = $tcprop->getAttributeNs($styleNs, 'column-width');
304
                    $columnWidths[$styleName] = $columnWidth;
305
                }
306
            }
307
        }
308
 
309
        // Content
310
        $item0 = $dom->getElementsByTagNameNS($officeNs, 'body')->item(0);
311
        $spreadsheets = ($item0 === null) ? [] : $item0->getElementsByTagNameNS($officeNs, 'spreadsheet');
312
 
313
        foreach ($spreadsheets as $workbookData) {
314
            /** @var DOMElement $workbookData */
315
            $tables = $workbookData->getElementsByTagNameNS($tableNs, 'table');
316
 
317
            $worksheetID = 0;
318
            foreach ($tables as $worksheetDataSet) {
319
                /** @var DOMElement $worksheetDataSet */
320
                $worksheetName = $worksheetDataSet->getAttributeNS($tableNs, 'name');
321
 
322
                // Check loadSheetsOnly
323
                if (
324
                    $this->loadSheetsOnly !== null
325
                    && $worksheetName
326
                    && !in_array($worksheetName, $this->loadSheetsOnly)
327
                ) {
328
                    continue;
329
                }
330
 
331
                $worksheetStyleName = $worksheetDataSet->getAttributeNS($tableNs, 'style-name');
332
 
333
                // Create sheet
334
                $spreadsheet->createSheet();
335
                $spreadsheet->setActiveSheetIndex($worksheetID);
336
 
337
                if ($worksheetName || is_numeric($worksheetName)) {
338
                    // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
339
                    // formula cells... during the load, all formulae should be correct, and we're simply
340
                    // bringing the worksheet name in line with the formula, not the reverse
341
                    $spreadsheet->getActiveSheet()->setTitle((string) $worksheetName, false, false);
342
                }
343
 
344
                // Go through every child of table element
345
                $rowID = 1;
346
                $tableColumnIndex = 1;
347
                foreach ($worksheetDataSet->childNodes as $childNode) {
348
                    /** @var DOMElement $childNode */
349
 
350
                    // Filter elements which are not under the "table" ns
351
                    if ($childNode->namespaceURI != $tableNs) {
352
                        continue;
353
                    }
354
 
355
                    $key = $childNode->nodeName;
356
 
357
                    // Remove ns from node name
358
                    if (str_contains($key, ':')) {
359
                        $keyChunks = explode(':', $key);
360
                        $key = array_pop($keyChunks);
361
                    }
362
 
363
                    switch ($key) {
364
                        case 'table-header-rows':
365
                            /// TODO :: Figure this out. This is only a partial implementation I guess.
366
                            //          ($rowData it's not used at all and I'm not sure that PHPExcel
367
                            //          has an API for this)
368
 
369
//                            foreach ($rowData as $keyRowData => $cellData) {
370
//                                $rowData = $cellData;
371
//                                break;
372
//                            }
373
                            break;
374
                        case 'table-column':
375
                            if ($childNode->hasAttributeNS($tableNs, 'number-columns-repeated')) {
376
                                $rowRepeats = (int) $childNode->getAttributeNS($tableNs, 'number-columns-repeated');
377
                            } else {
378
                                $rowRepeats = 1;
379
                            }
380
                            $tableStyleName = $childNode->getAttributeNS($tableNs, 'style-name');
381
                            if (isset($columnWidths[$tableStyleName])) {
382
                                $columnWidth = new HelperDimension($columnWidths[$tableStyleName]);
383
                                $tableColumnString = Coordinate::stringFromColumnIndex($tableColumnIndex);
384
                                for ($rowRepeats2 = $rowRepeats; $rowRepeats2 > 0; --$rowRepeats2) {
385
                                    $spreadsheet->getActiveSheet()
386
                                        ->getColumnDimension($tableColumnString)
387
                                        ->setWidth($columnWidth->toUnit('cm'), 'cm');
388
                                    ++$tableColumnString;
389
                                }
390
                            }
391
                            $tableColumnIndex += $rowRepeats;
392
 
393
                            break;
394
                        case 'table-row':
395
                            if ($childNode->hasAttributeNS($tableNs, 'number-rows-repeated')) {
396
                                $rowRepeats = (int) $childNode->getAttributeNS($tableNs, 'number-rows-repeated');
397
                            } else {
398
                                $rowRepeats = 1;
399
                            }
400
 
401
                            $columnID = 'A';
402
                            /** @var DOMElement|DOMText $cellData */
403
                            foreach ($childNode->childNodes as $cellData) {
404
                                if ($cellData instanceof DOMText) {
405
                                    continue; // should just be whitespace
406
                                }
407
                                if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
408
                                    if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
409
                                        $colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
410
                                    } else {
411
                                        $colRepeats = 1;
412
                                    }
413
 
414
                                    for ($i = 0; $i < $colRepeats; ++$i) {
415
                                        ++$columnID;
416
                                    }
417
 
418
                                    continue;
419
                                }
420
 
421
                                // Initialize variables
422
                                $formatting = $hyperlink = null;
423
                                $hasCalculatedValue = false;
424
                                $cellDataFormula = '';
425
                                $cellDataType = '';
426
                                $cellDataRef = '';
427
 
428
                                if ($cellData->hasAttributeNS($tableNs, 'formula')) {
429
                                    $cellDataFormula = $cellData->getAttributeNS($tableNs, 'formula');
430
                                    $hasCalculatedValue = true;
431
                                }
432
                                if ($cellData->hasAttributeNS($tableNs, 'number-matrix-columns-spanned')) {
433
                                    if ($cellData->hasAttributeNS($tableNs, 'number-matrix-rows-spanned')) {
434
                                        $cellDataType = 'array';
435
                                        $arrayRow = (int) $cellData->getAttributeNS($tableNs, 'number-matrix-rows-spanned');
436
                                        $arrayCol = (int) $cellData->getAttributeNS($tableNs, 'number-matrix-columns-spanned');
437
                                        $lastRow = $rowID + $arrayRow - 1;
438
                                        $lastCol = $columnID;
439
                                        while ($arrayCol > 1) {
440
                                            ++$lastCol;
441
                                            --$arrayCol;
442
                                        }
443
                                        $cellDataRef = "$columnID$rowID:$lastCol$lastRow";
444
                                    }
445
                                }
446
 
447
                                // Annotations
448
                                $annotation = $cellData->getElementsByTagNameNS($officeNs, 'annotation');
449
 
450
                                if ($annotation->length > 0 && $annotation->item(0) !== null) {
451
                                    $textNode = $annotation->item(0)->getElementsByTagNameNS($textNs, 'p');
452
                                    $textNodeLength = $textNode->length;
453
                                    $newLineOwed = false;
454
                                    for ($textNodeIndex = 0; $textNodeIndex < $textNodeLength; ++$textNodeIndex) {
455
                                        $textNodeItem = $textNode->item($textNodeIndex);
456
                                        if ($textNodeItem !== null) {
457
                                            $text = $this->scanElementForText($textNodeItem);
458
                                            if ($newLineOwed) {
459
                                                $spreadsheet->getActiveSheet()
460
                                                    ->getComment($columnID . $rowID)
461
                                                    ->getText()
462
                                                    ->createText("\n");
463
                                            }
464
                                            $newLineOwed = true;
465
 
466
                                            $spreadsheet->getActiveSheet()
467
                                                ->getComment($columnID . $rowID)
468
                                                ->getText()
469
                                                ->createText($this->parseRichText($text));
470
                                        }
471
                                    }
472
                                }
473
 
474
                                // Content
475
 
476
                                /** @var DOMElement[] $paragraphs */
477
                                $paragraphs = [];
478
 
479
                                foreach ($cellData->childNodes as $item) {
480
                                    /** @var DOMElement $item */
481
 
482
                                    // Filter text:p elements
483
                                    if ($item->nodeName == 'text:p') {
484
                                        $paragraphs[] = $item;
485
                                    }
486
                                }
487
 
488
                                if (count($paragraphs) > 0) {
489
                                    // Consolidate if there are multiple p records (maybe with spans as well)
490
                                    $dataArray = [];
491
 
492
                                    // Text can have multiple text:p and within those, multiple text:span.
493
                                    // text:p newlines, but text:span does not.
494
                                    // Also, here we assume there is no text data is span fields are specified, since
495
                                    // we have no way of knowing proper positioning anyway.
496
 
497
                                    foreach ($paragraphs as $pData) {
498
                                        $dataArray[] = $this->scanElementForText($pData);
499
                                    }
500
                                    $allCellDataText = implode("\n", $dataArray);
501
 
502
                                    $type = $cellData->getAttributeNS($officeNs, 'value-type');
503
 
504
                                    switch ($type) {
505
                                        case 'string':
506
                                            $type = DataType::TYPE_STRING;
507
                                            $dataValue = $allCellDataText;
508
 
509
                                            foreach ($paragraphs as $paragraph) {
510
                                                $link = $paragraph->getElementsByTagNameNS($textNs, 'a');
511
                                                if ($link->length > 0 && $link->item(0) !== null) {
512
                                                    $hyperlink = $link->item(0)->getAttributeNS($xlinkNs, 'href');
513
                                                }
514
                                            }
515
 
516
                                            break;
517
                                        case 'boolean':
518
                                            $type = DataType::TYPE_BOOL;
519
                                            $dataValue = ($cellData->getAttributeNS($officeNs, 'boolean-value') === 'true') ? true : false;
520
 
521
                                            break;
522
                                        case 'percentage':
523
                                            $type = DataType::TYPE_NUMERIC;
524
                                            $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
525
 
526
                                            // percentage should always be float
527
                                            //if (floor($dataValue) == $dataValue) {
528
                                            //    $dataValue = (int) $dataValue;
529
                                            //}
530
                                            $formatting = NumberFormat::FORMAT_PERCENTAGE_00;
531
 
532
                                            break;
533
                                        case 'currency':
534
                                            $type = DataType::TYPE_NUMERIC;
535
                                            $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
536
 
537
                                            if (floor($dataValue) == $dataValue) {
538
                                                $dataValue = (int) $dataValue;
539
                                            }
540
                                            $formatting = NumberFormat::FORMAT_CURRENCY_USD_INTEGER;
541
 
542
                                            break;
543
                                        case 'float':
544
                                            $type = DataType::TYPE_NUMERIC;
545
                                            $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
546
 
547
                                            if (floor($dataValue) == $dataValue) {
548
                                                if ($dataValue == (int) $dataValue) {
549
                                                    $dataValue = (int) $dataValue;
550
                                                }
551
                                            }
552
 
553
                                            break;
554
                                        case 'date':
555
                                            $type = DataType::TYPE_NUMERIC;
556
                                            $value = $cellData->getAttributeNS($officeNs, 'date-value');
557
                                            $dataValue = Date::convertIsoDate($value);
558
 
559
                                            if ($dataValue != floor($dataValue)) {
560
                                                $formatting = NumberFormat::FORMAT_DATE_XLSX15
561
                                                    . ' '
562
                                                    . NumberFormat::FORMAT_DATE_TIME4;
563
                                            } else {
564
                                                $formatting = NumberFormat::FORMAT_DATE_XLSX15;
565
                                            }
566
 
567
                                            break;
568
                                        case 'time':
569
                                            $type = DataType::TYPE_NUMERIC;
570
 
571
                                            $timeValue = $cellData->getAttributeNS($officeNs, 'time-value');
572
 
573
                                            $dataValue = Date::PHPToExcel(
574
                                                strtotime(
575
                                                    '01-01-1970 ' . implode(':', sscanf($timeValue, 'PT%dH%dM%dS') ?? [])
576
                                                )
577
                                            );
578
                                            $formatting = NumberFormat::FORMAT_DATE_TIME4;
579
 
580
                                            break;
581
                                        default:
582
                                            $dataValue = null;
583
                                    }
584
                                } else {
585
                                    $type = DataType::TYPE_NULL;
586
                                    $dataValue = null;
587
                                }
588
 
589
                                if ($hasCalculatedValue) {
590
                                    $type = DataType::TYPE_FORMULA;
591
                                    $cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=') + 1);
592
                                    $cellDataFormula = FormulaTranslator::convertToExcelFormulaValue($cellDataFormula);
593
                                }
594
 
595
                                if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
596
                                    $colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
597
                                } else {
598
                                    $colRepeats = 1;
599
                                }
600
 
601
                                if ($type !== null) { // @phpstan-ignore-line
602
                                    for ($i = 0; $i < $colRepeats; ++$i) {
603
                                        if ($i > 0) {
604
                                            ++$columnID;
605
                                        }
606
 
607
                                        if ($type !== DataType::TYPE_NULL) {
608
                                            for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) {
609
                                                $rID = $rowID + $rowAdjust;
610
 
611
                                                $cell = $spreadsheet->getActiveSheet()
612
                                                    ->getCell($columnID . $rID);
613
 
614
                                                // Set value
615
                                                if ($hasCalculatedValue) {
616
                                                    $cell->setValueExplicit($cellDataFormula, $type);
617
                                                    if ($cellDataType === 'array') {
618
                                                        $cell->setFormulaAttributes(['t' => 'array', 'ref' => $cellDataRef]);
619
                                                    }
620
                                                } else {
621
                                                    $cell->setValueExplicit($dataValue, $type);
622
                                                }
623
 
624
                                                if ($hasCalculatedValue) {
625
                                                    $cell->setCalculatedValue($dataValue, $type === DataType::TYPE_NUMERIC);
626
                                                }
627
 
628
                                                // Set other properties
629
                                                if ($formatting !== null) {
630
                                                    $spreadsheet->getActiveSheet()
631
                                                        ->getStyle($columnID . $rID)
632
                                                        ->getNumberFormat()
633
                                                        ->setFormatCode($formatting);
634
                                                } else {
635
                                                    $spreadsheet->getActiveSheet()
636
                                                        ->getStyle($columnID . $rID)
637
                                                        ->getNumberFormat()
638
                                                        ->setFormatCode(NumberFormat::FORMAT_GENERAL);
639
                                                }
640
 
641
                                                if ($hyperlink !== null) {
642
                                                    if ($hyperlink[0] === '#') {
643
                                                        $hyperlink = 'sheet://' . substr($hyperlink, 1);
644
                                                    }
645
                                                    $cell->getHyperlink()
646
                                                        ->setUrl($hyperlink);
647
                                                }
648
                                            }
649
                                        }
650
                                    }
651
                                }
652
 
653
                                // Merged cells
654
                                $this->processMergedCells($cellData, $tableNs, $type, $columnID, $rowID, $spreadsheet);
655
 
656
                                ++$columnID;
657
                            }
658
                            $rowID += $rowRepeats;
659
 
660
                            break;
661
                    }
662
                }
663
                $pageSettings->setVisibilityForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
664
                $pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName);
665
                ++$worksheetID;
666
            }
667
 
668
            $autoFilterReader->read($workbookData);
669
            $definedNameReader->read($workbookData);
670
        }
671
        $spreadsheet->setActiveSheetIndex(0);
672
 
673
        if ($zip->locateName('settings.xml') !== false) {
674
            $this->processSettings($zip, $spreadsheet);
675
        }
676
 
677
        // Return
678
        return $spreadsheet;
679
    }
680
 
681
    private function processSettings(ZipArchive $zip, Spreadsheet $spreadsheet): void
682
    {
683
        $dom = new DOMDocument('1.01', 'UTF-8');
684
        $dom->loadXML(
685
            $this->getSecurityScannerOrThrow()
686
                ->scan($zip->getFromName('settings.xml'))
687
        );
688
        //$xlinkNs = $dom->lookupNamespaceUri('xlink');
689
        $configNs = (string) $dom->lookupNamespaceUri('config');
690
        //$oooNs = $dom->lookupNamespaceUri('ooo');
691
        $officeNs = (string) $dom->lookupNamespaceUri('office');
692
        $settings = $dom->getElementsByTagNameNS($officeNs, 'settings')
693
            ->item(0);
694
        if ($settings !== null) {
695
            $this->lookForActiveSheet($settings, $spreadsheet, $configNs);
696
            $this->lookForSelectedCells($settings, $spreadsheet, $configNs);
697
        }
698
    }
699
 
700
    private function lookForActiveSheet(DOMElement $settings, Spreadsheet $spreadsheet, string $configNs): void
701
    {
702
        /** @var DOMElement $t */
703
        foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) {
704
            if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') {
705
                try {
706
                    $spreadsheet->setActiveSheetIndexByName($t->nodeValue ?? '');
707
                } catch (Throwable) {
708
                    // do nothing
709
                }
710
 
711
                break;
712
            }
713
        }
714
    }
715
 
716
    private function lookForSelectedCells(DOMElement $settings, Spreadsheet $spreadsheet, string $configNs): void
717
    {
718
        /** @var DOMElement $t */
719
        foreach ($settings->getElementsByTagNameNS($configNs, 'config-item-map-named') as $t) {
720
            if ($t->getAttributeNs($configNs, 'name') === 'Tables') {
721
                foreach ($t->getElementsByTagNameNS($configNs, 'config-item-map-entry') as $ws) {
722
                    $setRow = $setCol = '';
723
                    $wsname = $ws->getAttributeNs($configNs, 'name');
724
                    foreach ($ws->getElementsByTagNameNS($configNs, 'config-item') as $configItem) {
725
                        $attrName = $configItem->getAttributeNs($configNs, 'name');
726
                        if ($attrName === 'CursorPositionX') {
727
                            $setCol = $configItem->nodeValue;
728
                        }
729
                        if ($attrName === 'CursorPositionY') {
730
                            $setRow = $configItem->nodeValue;
731
                        }
732
                    }
733
                    $this->setSelected($spreadsheet, $wsname, "$setCol", "$setRow");
734
                }
735
 
736
                break;
737
            }
738
        }
739
    }
740
 
741
    private function setSelected(Spreadsheet $spreadsheet, string $wsname, string $setCol, string $setRow): void
742
    {
743
        if (is_numeric($setCol) && is_numeric($setRow)) {
744
            $sheet = $spreadsheet->getSheetByName($wsname);
745
            if ($sheet !== null) {
746
                $sheet->setSelectedCells([(int) $setCol + 1, (int) $setRow + 1]);
747
            }
748
        }
749
    }
750
 
751
    /**
752
     * Recursively scan element.
753
     */
754
    protected function scanElementForText(DOMNode $element): string
755
    {
756
        $str = '';
757
        foreach ($element->childNodes as $child) {
758
            /** @var DOMNode $child */
759
            if ($child->nodeType == XML_TEXT_NODE) {
760
                $str .= $child->nodeValue;
761
            } elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:line-break') {
762
                $str .= "\n";
763
            } elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:s') {
764
                // It's a space
765
 
766
                // Multiple spaces?
767
                $attributes = $child->attributes;
768
                /** @var ?DOMAttr $cAttr */
769
                $cAttr = ($attributes === null) ? null : $attributes->getNamedItem('c');
770
                $multiplier = self::getMultiplier($cAttr);
771
                $str .= str_repeat(' ', $multiplier);
772
            }
773
 
774
            if ($child->hasChildNodes()) {
775
                $str .= $this->scanElementForText($child);
776
            }
777
        }
778
 
779
        return $str;
780
    }
781
 
782
    private static function getMultiplier(?DOMAttr $cAttr): int
783
    {
784
        if ($cAttr) {
785
            $multiplier = (int) $cAttr->nodeValue;
786
        } else {
787
            $multiplier = 1;
788
        }
789
 
790
        return $multiplier;
791
    }
792
 
793
    private function parseRichText(string $is): RichText
794
    {
795
        $value = new RichText();
796
        $value->createText($is);
797
 
798
        return $value;
799
    }
800
 
801
    private function processMergedCells(
802
        DOMElement $cellData,
803
        string $tableNs,
804
        string $type,
805
        string $columnID,
806
        int $rowID,
807
        Spreadsheet $spreadsheet
808
    ): void {
809
        if (
810
            $cellData->hasAttributeNS($tableNs, 'number-columns-spanned')
811
            || $cellData->hasAttributeNS($tableNs, 'number-rows-spanned')
812
        ) {
813
            if (($type !== DataType::TYPE_NULL) || ($this->readDataOnly === false)) {
814
                $columnTo = $columnID;
815
 
816
                if ($cellData->hasAttributeNS($tableNs, 'number-columns-spanned')) {
817
                    $columnIndex = Coordinate::columnIndexFromString($columnID);
818
                    $columnIndex += (int) $cellData->getAttributeNS($tableNs, 'number-columns-spanned');
819
                    $columnIndex -= 2;
820
 
821
                    $columnTo = Coordinate::stringFromColumnIndex($columnIndex + 1);
822
                }
823
 
824
                $rowTo = $rowID;
825
 
826
                if ($cellData->hasAttributeNS($tableNs, 'number-rows-spanned')) {
827
                    $rowTo = $rowTo + (int) $cellData->getAttributeNS($tableNs, 'number-rows-spanned') - 1;
828
                }
829
 
830
                $cellRange = $columnID . $rowID . ':' . $columnTo . $rowTo;
831
                $spreadsheet->getActiveSheet()->mergeCells($cellRange, Worksheet::MERGE_CELL_CONTENT_HIDE);
832
            }
833
        }
834
    }
835
}