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