• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PhpSpreadsheet / 17999085524

25 Sep 2025 06:34AM UTC coverage: 95.867% (+0.3%) from 95.602%
17999085524

Pull #4662

github

web-flow
Merge 0dac8acdb into e3cac6f1f
Pull Request #4662: WIP Do Not Install

45116 of 47061 relevant lines covered (95.87%)

373.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.56
/src/PhpSpreadsheet/Reader/Ods.php
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\Shared\StringHelper;
23
use PhpOffice\PhpSpreadsheet\Spreadsheet;
24
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
25
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
26
use Throwable;
27
use XMLReader;
28
use ZipArchive;
29

30
class Ods extends BaseReader
31
{
32
    const INITIAL_FILE = 'content.xml';
33

34
    /**
35
     * Create a new Ods Reader instance.
36
     */
37
    public function __construct()
113✔
38
    {
39
        parent::__construct();
113✔
40
        $this->securityScanner = XmlScanner::getInstance($this);
113✔
41
    }
42

43
    /**
44
     * Can the current IReader read the file?
45
     */
46
    public function canRead(string $filename): bool
21✔
47
    {
48
        $mimeType = 'UNKNOWN';
21✔
49

50
        // Load file
51

52
        if (File::testFileNoThrow($filename, '')) {
21✔
53
            $zip = new ZipArchive();
5✔
54
            if ($zip->open($filename) === true) {
5✔
55
                // check if it is an OOXML archive
56
                $stat = $zip->statName('mimetype');
5✔
57
                if (!empty($stat) && ($stat['size'] <= 255)) {
5✔
58
                    $mimeType = $zip->getFromName($stat['name']);
4✔
59
                } elseif ($zip->statName('META-INF/manifest.xml')) {
1✔
60
                    $xml = simplexml_load_string(
1✔
61
                        $this->getSecurityScannerOrThrow()
1✔
62
                            ->scan(
1✔
63
                                $zip->getFromName(
1✔
64
                                    'META-INF/manifest.xml'
1✔
65
                                )
1✔
66
                            )
1✔
67
                    );
1✔
68
                    if ($xml !== false) {
1✔
69
                        $namespacesContent = $xml->getNamespaces(true);
1✔
70
                        if (isset($namespacesContent['manifest'])) {
1✔
71
                            $manifest = $xml->children($namespacesContent['manifest']);
1✔
72
                            foreach ($manifest as $manifestDataSet) {
1✔
73
                                $manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']);
1✔
74
                                if ($manifestAttributes && $manifestAttributes->{'full-path'} == '/') {
1✔
75
                                    $mimeType = (string) $manifestAttributes->{'media-type'};
1✔
76

77
                                    break;
1✔
78
                                }
79
                            }
80
                        }
81
                    }
82
                }
83

84
                $zip->close();
5✔
85
            }
86
        }
87

88
        return $mimeType === 'application/vnd.oasis.opendocument.spreadsheet';
21✔
89
    }
90

91
    /**
92
     * Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object.
93
     *
94
     * @return string[]
95
     */
96
    public function listWorksheetNames(string $filename): array
6✔
97
    {
98
        File::assertFile($filename, self::INITIAL_FILE);
6✔
99

100
        $worksheetNames = [];
2✔
101

102
        $xml = new XMLReader();
2✔
103
        $xml->xml(
2✔
104
            $this->getSecurityScannerOrThrow()
2✔
105
                ->scanFile(
2✔
106
                    'zip://' . realpath($filename) . '#' . self::INITIAL_FILE
2✔
107
                )
2✔
108
        );
2✔
109
        $xml->setParserProperty(2, true);
2✔
110

111
        // Step into the first level of content of the XML
112
        $xml->read();
2✔
113
        while ($xml->read()) {
2✔
114
            // Quickly jump through to the office:body node
115
            while ($xml->name !== 'office:body') {
2✔
116
                if ($xml->isEmptyElement) {
2✔
117
                    $xml->read();
2✔
118
                } else {
119
                    $xml->next();
2✔
120
                }
121
            }
122
            // Now read each node until we find our first table:table node
123
            while ($xml->read()) {
2✔
124
                $xmlName = $xml->name;
2✔
125
                if ($xmlName == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
2✔
126
                    // Loop through each table:table node reading the table:name attribute for each worksheet name
127
                    do {
128
                        $worksheetName = $xml->getAttribute('table:name');
2✔
129
                        if (!empty($worksheetName)) {
2✔
130
                            $worksheetNames[] = $worksheetName;
2✔
131
                        }
132
                        $xml->next();
2✔
133
                    } while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT);
2✔
134
                }
135
            }
136
        }
137

138
        return $worksheetNames;
2✔
139
    }
140

141
    /**
142
     * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
143
     *
144
     * @return array<int, array{worksheetName: string, lastColumnLetter: string, lastColumnIndex: int, totalRows: int, totalColumns: int, sheetState: string}>
145
     */
146
    public function listWorksheetInfo(string $filename): array
6✔
147
    {
148
        File::assertFile($filename, self::INITIAL_FILE);
6✔
149

150
        $worksheetInfo = [];
2✔
151

152
        $xml = new XMLReader();
2✔
153
        $xml->xml(
2✔
154
            $this->getSecurityScannerOrThrow()
2✔
155
                ->scanFile(
2✔
156
                    'zip://' . realpath($filename) . '#' . self::INITIAL_FILE
2✔
157
                )
2✔
158
        );
2✔
159
        $xml->setParserProperty(2, true);
2✔
160

161
        // Step into the first level of content of the XML
162
        $xml->read();
2✔
163
        $tableVisibility = [];
2✔
164
        $lastTableStyle = '';
2✔
165

166
        while ($xml->read()) {
2✔
167
            if ($xml->name === 'style:style') {
2✔
168
                $styleType = $xml->getAttribute('style:family');
2✔
169
                if ($styleType === 'table') {
2✔
170
                    $lastTableStyle = $xml->getAttribute('style:name');
2✔
171
                }
172
            } elseif ($xml->name === 'style:table-properties') {
2✔
173
                $visibility = $xml->getAttribute('table:display');
2✔
174
                $tableVisibility[$lastTableStyle] = ($visibility === 'false') ? Worksheet::SHEETSTATE_HIDDEN : Worksheet::SHEETSTATE_VISIBLE;
2✔
175
            } elseif ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
2✔
176
                $worksheetNames[] = $xml->getAttribute('table:name');
2✔
177

178
                $styleName = $xml->getAttribute('table:style-name') ?? '';
2✔
179
                $visibility = $tableVisibility[$styleName] ?? '';
2✔
180
                $tmpInfo = [
2✔
181
                    'worksheetName' => (string) $xml->getAttribute('table:name'),
2✔
182
                    'lastColumnLetter' => 'A',
2✔
183
                    'lastColumnIndex' => 0,
2✔
184
                    'totalRows' => 0,
2✔
185
                    'totalColumns' => 0,
2✔
186
                    'sheetState' => $visibility,
2✔
187
                ];
2✔
188

189
                // Loop through each child node of the table:table element reading
190
                $currCells = 0;
2✔
191
                do {
192
                    $xml->read();
2✔
193
                    if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
2✔
194
                        $rowspan = $xml->getAttribute('table:number-rows-repeated');
2✔
195
                        $rowspan = empty($rowspan) ? 1 : (int) $rowspan;
2✔
196
                        $tmpInfo['totalRows'] += $rowspan;
2✔
197
                        $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
2✔
198
                        $currCells = 0;
2✔
199
                        // Step into the row
200
                        $xml->read();
2✔
201
                        do {
202
                            $doread = true;
2✔
203
                            if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
2✔
204
                                if (!$xml->isEmptyElement) {
2✔
205
                                    ++$currCells;
2✔
206
                                    $xml->next();
2✔
207
                                    $doread = false;
2✔
208
                                }
209
                            } elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
1✔
210
                                $mergeSize = $xml->getAttribute('table:number-columns-repeated');
1✔
211
                                $currCells += (int) $mergeSize;
1✔
212
                            }
213
                            if ($doread) {
2✔
214
                                $xml->read();
1✔
215
                            }
216
                        } while ($xml->name != 'table:table-row');
2✔
217
                    }
218
                } while ($xml->name != 'table:table');
2✔
219

220
                $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
2✔
221
                $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
2✔
222
                $tmpInfo['lastColumnLetter'] = Coordinate::stringFromColumnIndex($tmpInfo['lastColumnIndex'] + 1);
2✔
223
                $worksheetInfo[] = $tmpInfo;
2✔
224
            }
225
        }
226

227
        return $worksheetInfo;
2✔
228
    }
229

230
    /**
231
     * Loads PhpSpreadsheet from file.
232
     */
233
    protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
80✔
234
    {
235
        $spreadsheet = $this->newSpreadsheet();
80✔
236
        $spreadsheet->setValueBinder($this->valueBinder);
80✔
237
        $spreadsheet->removeSheetByIndex(0);
80✔
238

239
        // Load into this instance
240
        return $this->loadIntoExisting($filename, $spreadsheet);
80✔
241
    }
242

243
    /**
244
     * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
245
     */
246
    public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet
84✔
247
    {
248
        File::assertFile($filename, self::INITIAL_FILE);
84✔
249

250
        $zip = new ZipArchive();
80✔
251
        $zip->open($filename);
80✔
252

253
        // Meta
254

255
        $xml = @simplexml_load_string(
80✔
256
            $this->getSecurityScannerOrThrow()
80✔
257
                ->scan($zip->getFromName('meta.xml'))
80✔
258
        );
80✔
259
        if ($xml === false) {
80✔
260
            throw new Exception('Unable to read data from {$pFilename}');
1✔
261
        }
262

263
        /** @var array{meta?: string, office?: string, dc?: string} */
264
        $namespacesMeta = $xml->getNamespaces(true);
79✔
265

266
        (new DocumentProperties($spreadsheet))->load($xml, $namespacesMeta);
79✔
267

268
        // Styles
269

270
        $dom = new DOMDocument('1.01', 'UTF-8');
79✔
271
        $dom->loadXML(
79✔
272
            $this->getSecurityScannerOrThrow()
79✔
273
                ->scan($zip->getFromName('styles.xml'))
79✔
274
        );
79✔
275

276
        $pageSettings = new PageSettings($dom);
79✔
277

278
        // Main Content
279

280
        $dom = new DOMDocument('1.01', 'UTF-8');
79✔
281
        $dom->loadXML(
79✔
282
            $this->getSecurityScannerOrThrow()
79✔
283
                ->scan($zip->getFromName(self::INITIAL_FILE))
79✔
284
        );
79✔
285

286
        $officeNs = (string) $dom->lookupNamespaceUri('office');
79✔
287
        $tableNs = (string) $dom->lookupNamespaceUri('table');
79✔
288
        $textNs = (string) $dom->lookupNamespaceUri('text');
79✔
289
        $xlinkNs = (string) $dom->lookupNamespaceUri('xlink');
79✔
290
        $styleNs = (string) $dom->lookupNamespaceUri('style');
79✔
291

292
        $pageSettings->readStyleCrossReferences($dom);
79✔
293

294
        $autoFilterReader = new AutoFilter($spreadsheet, $tableNs);
79✔
295
        $definedNameReader = new DefinedNames($spreadsheet, $tableNs);
79✔
296
        $columnWidths = [];
79✔
297
        $automaticStyle0 = $dom->getElementsByTagNameNS($officeNs, 'automatic-styles')->item(0);
79✔
298
        $automaticStyles = ($automaticStyle0 === null) ? [] : $automaticStyle0->getElementsByTagNameNS($styleNs, 'style');
79✔
299
        foreach ($automaticStyles as $automaticStyle) {
79✔
300
            $styleName = $automaticStyle->getAttributeNS($styleNs, 'name');
79✔
301
            $styleFamily = $automaticStyle->getAttributeNS($styleNs, 'family');
79✔
302
            if ($styleFamily === 'table-column') {
79✔
303
                $tcprops = $automaticStyle->getElementsByTagNameNS($styleNs, 'table-column-properties');
48✔
304
                $tcprop = $tcprops->item(0);
48✔
305
                if ($tcprop !== null) {
48✔
306
                    $columnWidth = $tcprop->getAttributeNs($styleNs, 'column-width');
48✔
307
                    $columnWidths[$styleName] = $columnWidth;
48✔
308
                }
309
            }
310
        }
311

312
        // Content
313
        $item0 = $dom->getElementsByTagNameNS($officeNs, 'body')->item(0);
79✔
314
        $spreadsheets = ($item0 === null) ? [] : $item0->getElementsByTagNameNS($officeNs, 'spreadsheet');
79✔
315

316
        foreach ($spreadsheets as $workbookData) {
79✔
317
            /** @var DOMElement $workbookData */
318
            $tables = $workbookData->getElementsByTagNameNS($tableNs, 'table');
79✔
319

320
            $worksheetID = 0;
79✔
321
            $sheetCreated = false;
79✔
322
            foreach ($tables as $worksheetDataSet) {
79✔
323
                /** @var DOMElement $worksheetDataSet */
324
                $worksheetName = $worksheetDataSet->getAttributeNS($tableNs, 'name');
79✔
325

326
                // Check loadSheetsOnly
327
                if (
328
                    $this->loadSheetsOnly !== null
79✔
329
                    && $worksheetName
330
                    && !in_array($worksheetName, $this->loadSheetsOnly)
79✔
331
                ) {
332
                    continue;
5✔
333
                }
334

335
                $worksheetStyleName = $worksheetDataSet->getAttributeNS($tableNs, 'style-name');
76✔
336

337
                // Create sheet
338
                $spreadsheet->createSheet();
76✔
339
                $sheetCreated = true;
76✔
340
                $spreadsheet->setActiveSheetIndex($worksheetID);
76✔
341

342
                if ($worksheetName || is_numeric($worksheetName)) {
76✔
343
                    // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
344
                    // formula cells... during the load, all formulae should be correct, and we're simply
345
                    // bringing the worksheet name in line with the formula, not the reverse
346
                    $spreadsheet->getActiveSheet()->setTitle((string) $worksheetName, false, false);
76✔
347
                }
348

349
                // Go through every child of table element
350
                $rowID = 1;
76✔
351
                $tableColumnIndex = 1;
76✔
352
                foreach ($worksheetDataSet->childNodes as $childNode) {
76✔
353
                    /** @var DOMElement $childNode */
354

355
                    // Filter elements which are not under the "table" ns
356
                    if ($childNode->namespaceURI != $tableNs) {
76✔
357
                        continue;
45✔
358
                    }
359

360
                    $key = self::extractNodeName($childNode->nodeName);
76✔
361

362
                    switch ($key) {
363
                        case 'table-header-rows':
76✔
364
                        case 'table-rows':
76✔
365
                            $this->processTableHeaderRows(
1✔
366
                                $childNode,
1✔
367
                                $tableNs,
1✔
368
                                $rowID,
1✔
369
                                $worksheetName,
1✔
370
                                $officeNs,
1✔
371
                                $textNs,
1✔
372
                                $xlinkNs,
1✔
373
                                $spreadsheet
1✔
374
                            );
1✔
375

376
                            break;
1✔
377
                        case 'table-row-group':
76✔
378
                            $this->processTableRowGroup(
1✔
379
                                $childNode,
1✔
380
                                $tableNs,
1✔
381
                                $rowID,
1✔
382
                                $worksheetName,
1✔
383
                                $officeNs,
1✔
384
                                $textNs,
1✔
385
                                $xlinkNs,
1✔
386
                                $spreadsheet
1✔
387
                            );
1✔
388

389
                            break;
1✔
390
                        case 'table-header-columns':
76✔
391
                        case 'table-columns':
76✔
392
                            $this->processTableHeaderColumns(
×
393
                                $childNode,
×
394
                                $tableNs,
×
395
                                $columnWidths,
×
396
                                $tableColumnIndex,
×
397
                                $spreadsheet
×
398
                            );
×
399

400
                            break;
×
401
                        case 'table-column-group':
76✔
402
                            $this->processTableColumnGroup(
×
403
                                $childNode,
×
404
                                $tableNs,
×
405
                                $columnWidths,
×
406
                                $tableColumnIndex,
×
407
                                $spreadsheet
×
408
                            );
×
409

410
                            break;
×
411
                        case 'table-column':
76✔
412
                            $this->processTableColumn(
45✔
413
                                $childNode,
45✔
414
                                $tableNs,
45✔
415
                                $columnWidths,
45✔
416
                                $tableColumnIndex,
45✔
417
                                $spreadsheet
45✔
418
                            );
45✔
419

420
                            break;
45✔
421
                        case 'table-row':
75✔
422
                            $this->processTableRow(
75✔
423
                                $childNode,
75✔
424
                                $tableNs,
75✔
425
                                $rowID,
75✔
426
                                $worksheetName,
75✔
427
                                $officeNs,
75✔
428
                                $textNs,
75✔
429
                                $xlinkNs,
75✔
430
                                $spreadsheet
75✔
431
                            );
75✔
432

433
                            break;
75✔
434
                    }
435
                }
436
                $pageSettings->setVisibilityForWorksheet(
76✔
437
                    $spreadsheet->getActiveSheet(),
76✔
438
                    $worksheetStyleName
76✔
439
                );
76✔
440
                $pageSettings->setPrintSettingsForWorksheet(
76✔
441
                    $spreadsheet->getActiveSheet(),
76✔
442
                    $worksheetStyleName
76✔
443
                );
76✔
444
                ++$worksheetID;
76✔
445
            }
446
            if ($this->createBlankSheetIfNoneRead && !$sheetCreated) {
79✔
447
                $spreadsheet->createSheet();
1✔
448
            }
449

450
            $autoFilterReader->read($workbookData);
79✔
451
            $definedNameReader->read($workbookData);
79✔
452
        }
453
        $spreadsheet->setActiveSheetIndex(0);
79✔
454

455
        if ($zip->locateName('settings.xml') !== false) {
77✔
456
            $this->processSettings($zip, $spreadsheet);
70✔
457
        }
458

459
        // Return
460
        return $spreadsheet;
77✔
461
    }
462

463
    private function processTableHeaderRows(
1✔
464
        DOMElement $childNode,
465
        string $tableNs,
466
        int &$rowID,
467
        string $worksheetName,
468
        string $officeNs,
469
        string $textNs,
470
        string $xlinkNs,
471
        Spreadsheet $spreadsheet
472
    ): void {
473
        foreach ($childNode->childNodes as $grandchildNode) {
1✔
474
            /** @var DOMElement $grandchildNode */
475
            $grandkey = self::extractNodeName($grandchildNode->nodeName);
1✔
476
            switch ($grandkey) {
477
                case 'table-row':
1✔
478
                    $this->processTableRow(
1✔
479
                        $grandchildNode,
1✔
480
                        $tableNs,
1✔
481
                        $rowID,
1✔
482
                        $worksheetName,
1✔
483
                        $officeNs,
1✔
484
                        $textNs,
1✔
485
                        $xlinkNs,
1✔
486
                        $spreadsheet
1✔
487
                    );
1✔
488

489
                    break;
1✔
490
            }
491
        }
492
    }
493

494
    private function processTableRowGroup(
1✔
495
        DOMElement $childNode,
496
        string $tableNs,
497
        int &$rowID,
498
        string $worksheetName,
499
        string $officeNs,
500
        string $textNs,
501
        string $xlinkNs,
502
        Spreadsheet $spreadsheet
503
    ): void {
504
        foreach ($childNode->childNodes as $grandchildNode) {
1✔
505
            /** @var DOMElement $grandchildNode */
506
            $grandkey = self::extractNodeName($grandchildNode->nodeName);
1✔
507
            switch ($grandkey) {
508
                case 'table-row':
1✔
509
                    $this->processTableRow(
1✔
510
                        $grandchildNode,
1✔
511
                        $tableNs,
1✔
512
                        $rowID,
1✔
513
                        $worksheetName,
1✔
514
                        $officeNs,
1✔
515
                        $textNs,
1✔
516
                        $xlinkNs,
1✔
517
                        $spreadsheet
1✔
518
                    );
1✔
519

520
                    break;
1✔
521
                case 'table-header-rows':
×
522
                case 'table-rows':
×
523
                    $this->processTableHeaderRows(
×
524
                        $grandchildNode,
×
525
                        $tableNs,
×
526
                        $rowID,
×
527
                        $worksheetName,
×
528
                        $officeNs,
×
529
                        $textNs,
×
530
                        $xlinkNs,
×
531
                        $spreadsheet
×
532
                    );
×
533

534
                    break;
×
535
                case 'table-row-group':
×
536
                    $this->processTableRowGroup(
×
537
                        $grandchildNode,
×
538
                        $tableNs,
×
539
                        $rowID,
×
540
                        $worksheetName,
×
541
                        $officeNs,
×
542
                        $textNs,
×
543
                        $xlinkNs,
×
544
                        $spreadsheet
×
545
                    );
×
546

547
                    break;
×
548
            }
549
        }
550
    }
551

552
    private function processTableRow(
75✔
553
        DOMElement $childNode,
554
        string $tableNs,
555
        int &$rowID,
556
        string $worksheetName,
557
        string $officeNs,
558
        string $textNs,
559
        string $xlinkNs,
560
        Spreadsheet $spreadsheet
561
    ): void {
562
        if ($childNode->hasAttributeNS($tableNs, 'number-rows-repeated')) {
75✔
563
            $rowRepeats = (int) $childNode->getAttributeNS($tableNs, 'number-rows-repeated');
26✔
564
        } else {
565
            $rowRepeats = 1;
75✔
566
        }
567

568
        $columnID = 'A';
75✔
569
        /** @var DOMElement|DOMText $cellData */
570
        foreach ($childNode->childNodes as $cellData) {
75✔
571
            if ($cellData instanceof DOMText) {
75✔
572
                continue; // should just be whitespace
2✔
573
            }
574
            if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
75✔
575
                if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
2✔
576
                    $colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
1✔
577
                } else {
578
                    $colRepeats = 1;
2✔
579
                }
580

581
                for ($i = 0; $i < $colRepeats; ++$i) {
2✔
582
                    StringHelper::stringIncrement($columnID);
2✔
583
                }
584

585
                continue;
2✔
586
            }
587

588
            // Initialize variables
589
            $formatting = $hyperlink = null;
75✔
590
            $hasCalculatedValue = false;
75✔
591
            $cellDataFormula = '';
75✔
592
            $cellDataType = '';
75✔
593
            $cellDataRef = '';
75✔
594

595
            if ($cellData->hasAttributeNS($tableNs, 'formula')) {
75✔
596
                $cellDataFormula = $cellData->getAttributeNS($tableNs, 'formula');
29✔
597
                $hasCalculatedValue = true;
29✔
598
            }
599
            if ($cellData->hasAttributeNS($tableNs, 'number-matrix-columns-spanned')) {
75✔
600
                if ($cellData->hasAttributeNS($tableNs, 'number-matrix-rows-spanned')) {
12✔
601
                    $cellDataType = 'array';
12✔
602
                    $arrayRow = (int) $cellData->getAttributeNS($tableNs, 'number-matrix-rows-spanned');
12✔
603
                    $arrayCol = (int) $cellData->getAttributeNS($tableNs, 'number-matrix-columns-spanned');
12✔
604
                    $lastRow = $rowID + $arrayRow - 1;
12✔
605
                    $lastCol = $columnID;
12✔
606
                    while ($arrayCol > 1) {
12✔
607
                        StringHelper::stringIncrement($lastCol);
7✔
608
                        --$arrayCol;
7✔
609
                    }
610
                    $cellDataRef = "$columnID$rowID:$lastCol$lastRow";
12✔
611
                }
612
            }
613

614
            // Annotations
615
            $annotation = $cellData->getElementsByTagNameNS($officeNs, 'annotation');
75✔
616

617
            if ($annotation->length > 0 && $annotation->item(0) !== null) {
75✔
618
                $textNode = $annotation->item(0)->getElementsByTagNameNS($textNs, 'p');
11✔
619
                $textNodeLength = $textNode->length;
11✔
620
                $newLineOwed = false;
11✔
621
                for ($textNodeIndex = 0; $textNodeIndex < $textNodeLength; ++$textNodeIndex) {
11✔
622
                    $textNodeItem = $textNode->item($textNodeIndex);
11✔
623
                    if ($textNodeItem !== null) {
11✔
624
                        $text = $this->scanElementForText($textNodeItem);
11✔
625
                        if ($newLineOwed) {
11✔
626
                            $spreadsheet->getActiveSheet()
1✔
627
                                ->getComment($columnID . $rowID)
1✔
628
                                ->getText()
1✔
629
                                ->createText("\n");
1✔
630
                        }
631
                        $newLineOwed = true;
11✔
632

633
                        $spreadsheet->getActiveSheet()
11✔
634
                            ->getComment($columnID . $rowID)
11✔
635
                            ->getText()
11✔
636
                            ->createText(
11✔
637
                                $this->parseRichText($text)
11✔
638
                            );
11✔
639
                    }
640
                }
641
            }
642

643
            // Content
644

645
            /** @var DOMElement[] $paragraphs */
646
            $paragraphs = [];
75✔
647

648
            foreach ($cellData->childNodes as $item) {
75✔
649
                /** @var DOMElement $item */
650

651
                // Filter text:p elements
652
                if ($item->nodeName == 'text:p') {
75✔
653
                    $paragraphs[] = $item;
75✔
654
                }
655
            }
656

657
            if (count($paragraphs) > 0) {
75✔
658
                // Consolidate if there are multiple p records (maybe with spans as well)
659
                $dataArray = [];
75✔
660

661
                // Text can have multiple text:p and within those, multiple text:span.
662
                // text:p newlines, but text:span does not.
663
                // Also, here we assume there is no text data is span fields are specified, since
664
                // we have no way of knowing proper positioning anyway.
665

666
                foreach ($paragraphs as $pData) {
75✔
667
                    $dataArray[] = $this->scanElementForText($pData);
75✔
668
                }
669
                $allCellDataText = implode("\n", $dataArray);
75✔
670

671
                $type = $cellData->getAttributeNS($officeNs, 'value-type');
75✔
672

673
                switch ($type) {
674
                    case 'string':
75✔
675
                        $type = DataType::TYPE_STRING;
49✔
676
                        $dataValue = $allCellDataText;
49✔
677

678
                        foreach ($paragraphs as $paragraph) {
49✔
679
                            $link = $paragraph->getElementsByTagNameNS($textNs, 'a');
49✔
680
                            if ($link->length > 0 && $link->item(0) !== null) {
49✔
681
                                $hyperlink = $link->item(0)->getAttributeNS($xlinkNs, 'href');
7✔
682
                            }
683
                        }
684

685
                        break;
49✔
686
                    case 'boolean':
51✔
687
                        $type = DataType::TYPE_BOOL;
9✔
688
                        $dataValue = ($cellData->getAttributeNS($officeNs, 'boolean-value') === 'true') ? true : false;
9✔
689

690
                        break;
9✔
691
                    case 'percentage':
49✔
692
                        $type = DataType::TYPE_NUMERIC;
4✔
693
                        $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
4✔
694

695
                        // percentage should always be float
696
                        //if (floor($dataValue) == $dataValue) {
697
                        //    $dataValue = (int) $dataValue;
698
                        //}
699
                        $formatting = NumberFormat::FORMAT_PERCENTAGE_00;
4✔
700

701
                        break;
4✔
702
                    case 'currency':
49✔
703
                        $type = DataType::TYPE_NUMERIC;
4✔
704
                        $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
4✔
705

706
                        if (floor($dataValue) == $dataValue) {
4✔
707
                            $dataValue = (int) $dataValue;
4✔
708
                        }
709
                        $formatting = NumberFormat::FORMAT_CURRENCY_USD_INTEGER;
4✔
710

711
                        break;
4✔
712
                    case 'float':
45✔
713
                        $type = DataType::TYPE_NUMERIC;
45✔
714
                        $dataValue = (float) $cellData->getAttributeNS($officeNs, 'value');
45✔
715

716
                        if (floor($dataValue) == $dataValue) {
45✔
717
                            if ($dataValue == (int) $dataValue) {
41✔
718
                                $dataValue = (int) $dataValue;
41✔
719
                            }
720
                        }
721

722
                        break;
45✔
723
                    case 'date':
10✔
724
                        $type = DataType::TYPE_NUMERIC;
9✔
725
                        $value = $cellData->getAttributeNS($officeNs, 'date-value');
9✔
726
                        $dataValue = Date::convertIsoDate($value);
9✔
727

728
                        if ($dataValue != floor($dataValue)) {
9✔
729
                            $formatting = NumberFormat::FORMAT_DATE_XLSX15
6✔
730
                                . ' '
6✔
731
                                . NumberFormat::FORMAT_DATE_TIME4;
6✔
732
                        } else {
733
                            $formatting = NumberFormat::FORMAT_DATE_XLSX15;
9✔
734
                        }
735

736
                        break;
9✔
737
                    case 'time':
7✔
738
                        $type = DataType::TYPE_NUMERIC;
6✔
739

740
                        $timeValue = $cellData->getAttributeNS($officeNs, 'time-value');
6✔
741

742
                        $dataValue = Date::PHPToExcel(
6✔
743
                            strtotime(
6✔
744
                                '01-01-1970 ' . implode(':', sscanf($timeValue, 'PT%dH%dM%dS') ?? [])
6✔
745
                            )
6✔
746
                        );
6✔
747
                        $formatting = NumberFormat::FORMAT_DATE_TIME4;
6✔
748

749
                        break;
6✔
750
                    default:
751
                        $dataValue = null;
1✔
752
                }
753
            } else {
754
                $type = DataType::TYPE_NULL;
43✔
755
                $dataValue = null;
43✔
756
            }
757

758
            if ($hasCalculatedValue) {
75✔
759
                $type = DataType::TYPE_FORMULA;
29✔
760
                $cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=') + 1);
29✔
761
                $cellDataFormula = FormulaTranslator::convertToExcelFormulaValue($cellDataFormula);
29✔
762
            }
763

764
            if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
75✔
765
                $colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
38✔
766
            } else {
767
                $colRepeats = 1;
75✔
768
            }
769

770
            if ($type !== null) { // @phpstan-ignore-line
75✔
771
                for ($i = 0; $i < $colRepeats; ++$i) {
75✔
772
                    if ($i > 0) {
75✔
773
                        StringHelper::stringIncrement($columnID);
38✔
774
                    }
775

776
                    if ($type !== DataType::TYPE_NULL) {
75✔
777
                        for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) {
75✔
778
                            $rID = $rowID + $rowAdjust;
75✔
779

780
                            $cell = $spreadsheet->getActiveSheet()
75✔
781
                                ->getCell($columnID . $rID);
75✔
782

783
                            // Set value
784
                            if ($hasCalculatedValue) {
75✔
785
                                $cell->setValueExplicit($cellDataFormula, $type);
29✔
786
                                if ($cellDataType === 'array') {
29✔
787
                                    $cell->setFormulaAttributes(['t' => 'array', 'ref' => $cellDataRef]);
12✔
788
                                }
789
                            } elseif ($type !== '' || $dataValue !== null) {
71✔
790
                                $cell->setValueExplicit($dataValue, $type);
71✔
791
                            }
792

793
                            if ($hasCalculatedValue) {
75✔
794
                                $cell->setCalculatedValue($dataValue, $type === DataType::TYPE_NUMERIC);
29✔
795
                            }
796

797
                            // Set other properties
798
                            if ($formatting !== null) {
75✔
799
                                $spreadsheet->getActiveSheet()
13✔
800
                                    ->getStyle($columnID . $rID)
13✔
801
                                    ->getNumberFormat()
13✔
802
                                    ->setFormatCode($formatting);
13✔
803
                            } else {
804
                                $spreadsheet->getActiveSheet()
75✔
805
                                    ->getStyle($columnID . $rID)
75✔
806
                                    ->getNumberFormat()
75✔
807
                                    ->setFormatCode(NumberFormat::FORMAT_GENERAL);
75✔
808
                            }
809

810
                            if ($hyperlink !== null) {
75✔
811
                                if ($hyperlink[0] === '#') {
7✔
812
                                    $hyperlink = 'sheet://' . substr($hyperlink, 1);
1✔
813
                                }
814
                                $cell->getHyperlink()
7✔
815
                                    ->setUrl($hyperlink);
7✔
816
                            }
817
                        }
818
                    }
819
                }
820
            }
821

822
            // Merged cells
823
            $this->processMergedCells($cellData, $tableNs, $type, $columnID, $rowID, $spreadsheet);
75✔
824

825
            StringHelper::stringIncrement($columnID);
75✔
826
        }
827
        $rowID += $rowRepeats;
75✔
828
    }
829

830
    private static function extractNodeName(string $key): string
76✔
831
    {
832
        // Remove ns from node name
833
        if (str_contains($key, ':')) {
76✔
834
            $keyChunks = explode(':', $key);
76✔
835
            $key = array_pop($keyChunks);
76✔
836
        }
837

838
        return $key;
76✔
839
    }
840

841
    /**
842
     * @param string[] $columnWidths
843
     */
844
    private function processTableHeaderColumns(
×
845
        DOMElement $childNode,
846
        string $tableNs,
847
        array $columnWidths,
848
        int &$tableColumnIndex,
849
        Spreadsheet $spreadsheet
850
    ): void {
851
        foreach ($childNode->childNodes as $grandchildNode) {
×
852
            /** @var DOMElement $grandchildNode */
853
            $grandkey = self::extractNodeName($grandchildNode->nodeName);
×
854
            switch ($grandkey) {
855
                case 'table-column':
×
856
                    $this->processTableColumn(
×
857
                        $grandchildNode,
×
858
                        $tableNs,
×
859
                        $columnWidths,
×
860
                        $tableColumnIndex,
×
861
                        $spreadsheet
×
862
                    );
×
863

864
                    break;
×
865
            }
866
        }
867
    }
868

869
    /**
870
     * @param string[] $columnWidths
871
     */
872
    private function processTableColumnGroup(
×
873
        DOMElement $childNode,
874
        string $tableNs,
875
        array $columnWidths,
876
        int &$tableColumnIndex,
877
        Spreadsheet $spreadsheet
878
    ): void {
879
        foreach ($childNode->childNodes as $grandchildNode) {
×
880
            /** @var DOMElement $grandchildNode */
881
            $grandkey = self::extractNodeName($grandchildNode->nodeName);
×
882
            switch ($grandkey) {
883
                case 'table-column':
×
884
                    $this->processTableColumn(
×
885
                        $grandchildNode,
×
886
                        $tableNs,
×
887
                        $columnWidths,
×
888
                        $tableColumnIndex,
×
889
                        $spreadsheet
×
890
                    );
×
891

892
                    break;
×
893
                case 'table-header-columns':
×
894
                case 'table-columns':
×
895
                    $this->processTableHeaderColumns(
×
896
                        $grandchildNode,
×
897
                        $tableNs,
×
898
                        $columnWidths,
×
899
                        $tableColumnIndex,
×
900
                        $spreadsheet
×
901
                    );
×
902

903
                    break;
×
904
                case 'table-column-group':
×
905
                    $this->processTableColumnGroup(
×
906
                        $grandchildNode,
×
907
                        $tableNs,
×
908
                        $columnWidths,
×
909
                        $tableColumnIndex,
×
910
                        $spreadsheet
×
911
                    );
×
912

913
                    break;
×
914
            }
915
        }
916
    }
917

918
    /**
919
     * @param string[] $columnWidths
920
     */
921
    private function processTableColumn(
45✔
922
        DOMElement $childNode,
923
        string $tableNs,
924
        array $columnWidths,
925
        int &$tableColumnIndex,
926
        Spreadsheet $spreadsheet
927
    ): void {
928
        if ($childNode->hasAttributeNS($tableNs, 'number-columns-repeated')) {
45✔
929
            $rowRepeats = (int) $childNode->getAttributeNS($tableNs, 'number-columns-repeated');
40✔
930
        } else {
931
            $rowRepeats = 1;
19✔
932
        }
933
        $tableStyleName = $childNode->getAttributeNS($tableNs, 'style-name');
45✔
934
        if (isset($columnWidths[$tableStyleName])) {
45✔
935
            $columnWidth = new HelperDimension($columnWidths[$tableStyleName]);
45✔
936
            $tableColumnString = Coordinate::stringFromColumnIndex($tableColumnIndex);
45✔
937
            for ($rowRepeats2 = $rowRepeats; $rowRepeats2 > 0; --$rowRepeats2) {
45✔
938
                /** @var string $tableColumnString */
939
                $spreadsheet->getActiveSheet()
45✔
940
                    ->getColumnDimension($tableColumnString)
45✔
941
                    ->setWidth($columnWidth->toUnit('cm'), 'cm');
45✔
942
                StringHelper::stringIncrement($tableColumnString);
45✔
943
            }
944
        }
945
        $tableColumnIndex += $rowRepeats;
45✔
946
    }
947

948
    private function processSettings(ZipArchive $zip, Spreadsheet $spreadsheet): void
70✔
949
    {
950
        $dom = new DOMDocument('1.01', 'UTF-8');
70✔
951
        $dom->loadXML(
70✔
952
            $this->getSecurityScannerOrThrow()
70✔
953
                ->scan($zip->getFromName('settings.xml'))
70✔
954
        );
70✔
955
        $configNs = (string) $dom->lookupNamespaceUri('config');
70✔
956
        $officeNs = (string) $dom->lookupNamespaceUri('office');
70✔
957
        $settings = $dom->getElementsByTagNameNS($officeNs, 'settings')
70✔
958
            ->item(0);
70✔
959
        if ($settings !== null) {
70✔
960
            $this->lookForActiveSheet($settings, $spreadsheet, $configNs);
70✔
961
            $this->lookForSelectedCells($settings, $spreadsheet, $configNs);
70✔
962
        }
963
    }
964

965
    private function lookForActiveSheet(DOMElement $settings, Spreadsheet $spreadsheet, string $configNs): void
70✔
966
    {
967
        /** @var DOMElement $t */
968
        foreach ($settings->getElementsByTagNameNS($configNs, 'config-item') as $t) {
70✔
969
            if ($t->getAttributeNs($configNs, 'name') === 'ActiveTable') {
69✔
970
                try {
971
                    $spreadsheet->setActiveSheetIndexByName($t->nodeValue ?? '');
69✔
972
                } catch (Throwable) {
2✔
973
                    // do nothing
974
                }
975

976
                break;
69✔
977
            }
978
        }
979
    }
980

981
    private function lookForSelectedCells(DOMElement $settings, Spreadsheet $spreadsheet, string $configNs): void
70✔
982
    {
983
        /** @var DOMElement $t */
984
        foreach ($settings->getElementsByTagNameNS($configNs, 'config-item-map-named') as $t) {
70✔
985
            if ($t->getAttributeNs($configNs, 'name') === 'Tables') {
69✔
986
                foreach ($t->getElementsByTagNameNS($configNs, 'config-item-map-entry') as $ws) {
69✔
987
                    $setRow = $setCol = '';
69✔
988
                    $wsname = $ws->getAttributeNs($configNs, 'name');
69✔
989
                    foreach ($ws->getElementsByTagNameNS($configNs, 'config-item') as $configItem) {
69✔
990
                        $attrName = $configItem->getAttributeNs($configNs, 'name');
69✔
991
                        if ($attrName === 'CursorPositionX') {
69✔
992
                            $setCol = $configItem->nodeValue;
69✔
993
                        }
994
                        if ($attrName === 'CursorPositionY') {
69✔
995
                            $setRow = $configItem->nodeValue;
69✔
996
                        }
997
                    }
998
                    $this->setSelected($spreadsheet, $wsname, "$setCol", "$setRow");
69✔
999
                }
1000

1001
                break;
69✔
1002
            }
1003
        }
1004
    }
1005

1006
    private function setSelected(Spreadsheet $spreadsheet, string $wsname, string $setCol, string $setRow): void
69✔
1007
    {
1008
        if (is_numeric($setCol) && is_numeric($setRow)) {
69✔
1009
            $sheet = $spreadsheet->getSheetByName($wsname);
69✔
1010
            if ($sheet !== null) {
69✔
1011
                $sheet->setSelectedCells([(int) $setCol + 1, (int) $setRow + 1]);
68✔
1012
            }
1013
        }
1014
    }
1015

1016
    /**
1017
     * Recursively scan element.
1018
     */
1019
    protected function scanElementForText(DOMNode $element): string
75✔
1020
    {
1021
        $str = '';
75✔
1022
        foreach ($element->childNodes as $child) {
75✔
1023
            /** @var DOMNode $child */
1024
            if ($child->nodeType == XML_TEXT_NODE) {
75✔
1025
                $str .= $child->nodeValue;
75✔
1026
            } elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:line-break') {
18✔
1027
                $str .= "\n";
1✔
1028
            } elseif ($child->nodeType == XML_ELEMENT_NODE && $child->nodeName == 'text:s') {
18✔
1029
                // It's a space
1030

1031
                // Multiple spaces?
1032
                $attributes = $child->attributes;
6✔
1033
                /** @var ?DOMAttr $cAttr */
1034
                $cAttr = ($attributes === null) ? null : $attributes->getNamedItem('c');
6✔
1035
                $multiplier = self::getMultiplier($cAttr);
6✔
1036
                $str .= str_repeat(' ', $multiplier);
6✔
1037
            }
1038

1039
            if ($child->hasChildNodes()) {
75✔
1040
                $str .= $this->scanElementForText($child);
16✔
1041
            }
1042
        }
1043

1044
        return $str;
75✔
1045
    }
1046

1047
    private static function getMultiplier(?DOMAttr $cAttr): int
6✔
1048
    {
1049
        if ($cAttr) {
6✔
1050
            $multiplier = (int) $cAttr->nodeValue;
6✔
1051
        } else {
1052
            $multiplier = 1;
6✔
1053
        }
1054

1055
        return $multiplier;
6✔
1056
    }
1057

1058
    private function parseRichText(string $is): RichText
11✔
1059
    {
1060
        $value = new RichText();
11✔
1061
        $value->createText($is);
11✔
1062

1063
        return $value;
11✔
1064
    }
1065

1066
    private function processMergedCells(
75✔
1067
        DOMElement $cellData,
1068
        string $tableNs,
1069
        string $type,
1070
        string $columnID,
1071
        int $rowID,
1072
        Spreadsheet $spreadsheet
1073
    ): void {
1074
        if (
1075
            $cellData->hasAttributeNS($tableNs, 'number-columns-spanned')
75✔
1076
            || $cellData->hasAttributeNS($tableNs, 'number-rows-spanned')
75✔
1077
        ) {
1078
            if (($type !== DataType::TYPE_NULL) || ($this->readDataOnly === false)) {
18✔
1079
                $columnTo = $columnID;
18✔
1080

1081
                if ($cellData->hasAttributeNS($tableNs, 'number-columns-spanned')) {
18✔
1082
                    $columnIndex = Coordinate::columnIndexFromString($columnID);
17✔
1083
                    $columnIndex += (int) $cellData->getAttributeNS($tableNs, 'number-columns-spanned');
17✔
1084
                    $columnIndex -= 2;
17✔
1085

1086
                    $columnTo = Coordinate::stringFromColumnIndex($columnIndex + 1);
17✔
1087
                }
1088

1089
                $rowTo = $rowID;
18✔
1090

1091
                if ($cellData->hasAttributeNS($tableNs, 'number-rows-spanned')) {
18✔
1092
                    $rowTo = $rowTo + (int) $cellData->getAttributeNS($tableNs, 'number-rows-spanned') - 1;
18✔
1093
                }
1094

1095
                $cellRange = $columnID . $rowID . ':' . $columnTo . $rowTo;
18✔
1096
                $spreadsheet->getActiveSheet()->mergeCells($cellRange, Worksheet::MERGE_CELL_CONTENT_HIDE);
18✔
1097
            }
1098
        }
1099
    }
1100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc