Passed
Pull Request — master (#4380)
by Owen
11:28
created

Gnumeric::setRowHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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