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\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
7
use PhpOffice\PhpSpreadsheet\Worksheet\Pane;
8
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
9
use SimpleXMLElement;
10
 
11
class SheetViews extends BaseParserClass
12
{
13
    private SimpleXMLElement $sheetViewXml;
14
 
15
    private SimpleXMLElement $sheetViewAttributes;
16
 
17
    private Worksheet $worksheet;
18
 
19
    private string $activePane = '';
20
 
21
    public function __construct(SimpleXMLElement $sheetViewXml, Worksheet $workSheet)
22
    {
23
        $this->sheetViewXml = $sheetViewXml;
24
        $this->sheetViewAttributes = Xlsx::testSimpleXml($sheetViewXml->attributes());
25
        $this->worksheet = $workSheet;
26
    }
27
 
28
    public function load(): void
29
    {
30
        $this->topLeft();
31
        $this->zoomScale();
32
        $this->view();
33
        $this->gridLines();
34
        $this->headers();
35
        $this->direction();
36
        $this->showZeros();
37
 
38
        $usesPanes = false;
39
        if (isset($this->sheetViewXml->pane)) {
40
            $this->pane();
41
            $usesPanes = true;
42
        }
43
        if (isset($this->sheetViewXml->selection)) {
44
            foreach ($this->sheetViewXml->selection as $selection) {
45
                $this->selection($selection, $usesPanes);
46
            }
47
        }
48
    }
49
 
50
    private function zoomScale(): void
51
    {
52
        if (isset($this->sheetViewAttributes->zoomScale)) {
53
            $zoomScale = (int) ($this->sheetViewAttributes->zoomScale);
54
            if ($zoomScale <= 0) {
55
                // setZoomScale will throw an Exception if the scale is less than or equals 0
56
                // that is OK when manually creating documents, but we should be able to read all documents
57
                $zoomScale = 100;
58
            }
59
 
60
            $this->worksheet->getSheetView()->setZoomScale($zoomScale);
61
        }
62
 
63
        if (isset($this->sheetViewAttributes->zoomScaleNormal)) {
64
            $zoomScaleNormal = (int) ($this->sheetViewAttributes->zoomScaleNormal);
65
            if ($zoomScaleNormal <= 0) {
66
                // setZoomScaleNormal will throw an Exception if the scale is less than or equals 0
67
                // that is OK when manually creating documents, but we should be able to read all documents
68
                $zoomScaleNormal = 100;
69
            }
70
 
71
            $this->worksheet->getSheetView()->setZoomScaleNormal($zoomScaleNormal);
72
        }
73
 
74
        if (isset($this->sheetViewAttributes->zoomScalePageLayoutView)) {
75
            $zoomScaleNormal = (int) ($this->sheetViewAttributes->zoomScalePageLayoutView);
76
            if ($zoomScaleNormal > 0) {
77
                $this->worksheet->getSheetView()->setZoomScalePageLayoutView($zoomScaleNormal);
78
            }
79
        }
80
 
81
        if (isset($this->sheetViewAttributes->zoomScaleSheetLayoutView)) {
82
            $zoomScaleNormal = (int) ($this->sheetViewAttributes->zoomScaleSheetLayoutView);
83
            if ($zoomScaleNormal > 0) {
84
                $this->worksheet->getSheetView()->setZoomScaleSheetLayoutView($zoomScaleNormal);
85
            }
86
        }
87
    }
88
 
89
    private function view(): void
90
    {
91
        if (isset($this->sheetViewAttributes->view)) {
92
            $this->worksheet->getSheetView()->setView((string) $this->sheetViewAttributes->view);
93
        }
94
    }
95
 
96
    private function topLeft(): void
97
    {
98
        if (isset($this->sheetViewAttributes->topLeftCell)) {
99
            $this->worksheet->setTopLeftCell($this->sheetViewAttributes->topLeftCell);
100
        }
101
    }
102
 
103
    private function gridLines(): void
104
    {
105
        if (isset($this->sheetViewAttributes->showGridLines)) {
106
            $this->worksheet->setShowGridLines(
107
                self::boolean((string) $this->sheetViewAttributes->showGridLines)
108
            );
109
        }
110
    }
111
 
112
    private function headers(): void
113
    {
114
        if (isset($this->sheetViewAttributes->showRowColHeaders)) {
115
            $this->worksheet->setShowRowColHeaders(
116
                self::boolean((string) $this->sheetViewAttributes->showRowColHeaders)
117
            );
118
        }
119
    }
120
 
121
    private function direction(): void
122
    {
123
        if (isset($this->sheetViewAttributes->rightToLeft)) {
124
            $this->worksheet->setRightToLeft(
125
                self::boolean((string) $this->sheetViewAttributes->rightToLeft)
126
            );
127
        }
128
    }
129
 
130
    private function showZeros(): void
131
    {
132
        if (isset($this->sheetViewAttributes->showZeros)) {
133
            $this->worksheet->getSheetView()->setShowZeros(
134
                self::boolean((string) $this->sheetViewAttributes->showZeros)
135
            );
136
        }
137
    }
138
 
139
    private function pane(): void
140
    {
141
        $xSplit = 0;
142
        $ySplit = 0;
143
        $topLeftCell = null;
144
        $paneAttributes = $this->sheetViewXml->pane->attributes();
145
 
146
        if (isset($paneAttributes->xSplit)) {
147
            $xSplit = (int) ($paneAttributes->xSplit);
148
            $this->worksheet->setXSplit($xSplit);
149
        }
150
 
151
        if (isset($paneAttributes->ySplit)) {
152
            $ySplit = (int) ($paneAttributes->ySplit);
153
            $this->worksheet->setYSplit($ySplit);
154
        }
155
        $paneState = isset($paneAttributes->state) ? ((string) $paneAttributes->state) : '';
156
        $this->worksheet->setPaneState($paneState);
157
        if (isset($paneAttributes->topLeftCell)) {
158
            $topLeftCell = (string) $paneAttributes->topLeftCell;
159
            $this->worksheet->setPaneTopLeftCell($topLeftCell);
160
            if ($paneState === Worksheet::PANE_FROZEN) {
161
                $this->worksheet->setTopLeftCell($topLeftCell);
162
            }
163
        }
164
        $activePane = isset($paneAttributes->activePane) ? ((string) $paneAttributes->activePane) : 'topLeft';
165
        $this->worksheet->setActivePane($activePane);
166
        $this->activePane = $activePane;
167
        if ($paneState === Worksheet::PANE_FROZEN || $paneState === Worksheet::PANE_FROZENSPLIT) {
168
            $this->worksheet->freezePane(
169
                Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1),
170
                $topLeftCell,
171
                $paneState === Worksheet::PANE_FROZENSPLIT
172
            );
173
        }
174
    }
175
 
176
    private function selection(?SimpleXMLElement $selection, bool $usesPanes): void
177
    {
178
        $attributes = ($selection === null) ? null : $selection->attributes();
179
        if ($attributes !== null) {
180
            $position = (string) $attributes->pane;
181
            if ($usesPanes && $position === '') {
182
                $position = 'topLeft';
183
            }
184
            $activeCell = (string) $attributes->activeCell;
185
            $sqref = (string) $attributes->sqref;
186
            $sqref = explode(' ', $sqref);
187
            $sqref = $sqref[0];
188
            if ($position === '') {
189
                $this->worksheet->setSelectedCells($sqref);
190
            } else {
191
                $pane = new Pane($position, $sqref, $activeCell);
192
                $this->worksheet->setPane($position, $pane);
193
                if ($position === $this->activePane && $sqref !== '') {
194
                    $this->worksheet->setSelectedCells($sqref);
195
                }
196
            }
197
        }
198
    }
199
}