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\Xlsx;
4
 
5
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
6
use PhpOffice\PhpSpreadsheet\Worksheet\Table\TableStyle;
7
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
8
use SimpleXMLElement;
9
 
10
class TableReader
11
{
12
    private Worksheet $worksheet;
13
 
14
    private SimpleXMLElement $tableXml;
15
 
16
    /** @var array|SimpleXMLElement */
17
    private $tableAttributes;
18
 
19
    public function __construct(Worksheet $workSheet, SimpleXMLElement $tableXml)
20
    {
21
        $this->worksheet = $workSheet;
22
        $this->tableXml = $tableXml;
23
    }
24
 
25
    /**
26
     * Loads Table into the Worksheet.
27
     */
28
    public function load(): void
29
    {
30
        $this->tableAttributes = $this->tableXml->attributes() ?? [];
31
        // Remove all "$" in the table range
32
        $tableRange = (string) preg_replace('/\$/', '', $this->tableAttributes['ref'] ?? '');
33
        if (str_contains($tableRange, ':')) {
34
            $this->readTable($tableRange);
35
        }
36
    }
37
 
38
    /**
39
     * Read Table from xml.
40
     */
41
    private function readTable(string $tableRange): void
42
    {
43
        $table = new Table($tableRange);
44
        $table->setName((string) ($this->tableAttributes['displayName'] ?? ''));
45
        $table->setShowHeaderRow(((string) ($this->tableAttributes['headerRowCount'] ?? '')) !== '0');
46
        $table->setShowTotalsRow(((string) ($this->tableAttributes['totalsRowCount'] ?? '')) === '1');
47
 
48
        $this->readTableAutoFilter($table, $this->tableXml->autoFilter);
49
        $this->readTableColumns($table, $this->tableXml->tableColumns);
50
        $this->readTableStyle($table, $this->tableXml->tableStyleInfo);
51
 
52
        (new AutoFilter($table, $this->tableXml))->load();
53
        $this->worksheet->addTable($table);
54
    }
55
 
56
    /**
57
     * Reads TableAutoFilter from xml.
58
     */
59
    private function readTableAutoFilter(Table $table, SimpleXMLElement $autoFilterXml): void
60
    {
61
        if ($autoFilterXml->filterColumn === null) {
62
            $table->setAllowFilter(false);
63
 
64
            return;
65
        }
66
 
67
        foreach ($autoFilterXml->filterColumn as $filterColumn) {
68
            $attributes = $filterColumn->attributes() ?? ['colId' => 0, 'hiddenButton' => 0];
69
            $column = $table->getColumnByOffset((int) $attributes['colId']);
70
            $column->setShowFilterButton(((string) $attributes['hiddenButton']) !== '1');
71
        }
72
    }
73
 
74
    /**
75
     * Reads TableColumns from xml.
76
     */
77
    private function readTableColumns(Table $table, SimpleXMLElement $tableColumnsXml): void
78
    {
79
        $offset = 0;
80
        foreach ($tableColumnsXml->tableColumn as $tableColumn) {
81
            $attributes = $tableColumn->attributes() ?? ['totalsRowLabel' => 0, 'totalsRowFunction' => 0];
82
            $column = $table->getColumnByOffset($offset++);
83
 
84
            if ($table->getShowTotalsRow()) {
85
                if ($attributes['totalsRowLabel']) {
86
                    $column->setTotalsRowLabel((string) $attributes['totalsRowLabel']);
87
                }
88
 
89
                if ($attributes['totalsRowFunction']) {
90
                    $column->setTotalsRowFunction((string) $attributes['totalsRowFunction']);
91
                }
92
            }
93
 
94
            if ($tableColumn->calculatedColumnFormula) {
95
                $column->setColumnFormula((string) $tableColumn->calculatedColumnFormula);
96
            }
97
        }
98
    }
99
 
100
    /**
101
     * Reads TableStyle from xml.
102
     */
103
    private function readTableStyle(Table $table, SimpleXMLElement $tableStyleInfoXml): void
104
    {
105
        $tableStyle = new TableStyle();
106
        $attributes = $tableStyleInfoXml->attributes();
107
        if ($attributes !== null) {
108
            $tableStyle->setTheme((string) $attributes['name']);
109
            $tableStyle->setShowRowStripes((string) $attributes['showRowStripes'] === '1');
110
            $tableStyle->setShowColumnStripes((string) $attributes['showColumnStripes'] === '1');
111
            $tableStyle->setShowFirstColumn((string) $attributes['showFirstColumn'] === '1');
112
            $tableStyle->setShowLastColumn((string) $attributes['showLastColumn'] === '1');
113
        }
114
        $table->setStyle($tableStyle);
115
    }
116
}