1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use DateTimeZone; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Copyright (c) 2006 - 2016 PhpSpreadsheet |
11
|
|
|
* |
12
|
|
|
* This library is free software; you can redistribute it and/or |
13
|
|
|
* modify it under the terms of the GNU Lesser General Public |
14
|
|
|
* License as published by the Free Software Foundation; either |
15
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This library is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20
|
|
|
* Lesser General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Lesser General Public |
23
|
|
|
* License along with this library; if not, write to the Free Software |
24
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
25
|
|
|
* |
26
|
|
|
* @category PhpSpreadsheet |
27
|
|
|
* @copyright Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
28
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
29
|
|
|
*/ |
30
|
|
|
class Ods extends BaseReader implements IReader |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Formats |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $styles = []; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new Ods Reader instance |
41
|
|
|
*/ |
42
|
2 |
|
public function __construct() |
43
|
|
|
{ |
44
|
2 |
|
$this->readFilter = new DefaultReadFilter(); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Can the current IReader read the file? |
49
|
|
|
* |
50
|
|
|
* @param string $pFilename |
51
|
|
|
* @throws Exception |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
2 |
|
public function canRead($pFilename) |
55
|
|
|
{ |
56
|
2 |
|
File::assertFile($pFilename); |
57
|
|
|
|
58
|
2 |
|
$zipClass = \PhpOffice\PhpSpreadsheet\Settings::getZipClass(); |
59
|
|
|
|
60
|
2 |
|
$mimeType = 'UNKNOWN'; |
61
|
|
|
// Load file |
62
|
2 |
|
$zip = new $zipClass(); |
63
|
2 |
|
if ($zip->open($pFilename) === true) { |
64
|
|
|
// check if it is an OOXML archive |
65
|
2 |
|
$stat = $zip->statName('mimetype'); |
66
|
2 |
|
if ($stat && ($stat['size'] <= 255)) { |
67
|
2 |
|
$mimeType = $zip->getFromName($stat['name']); |
68
|
|
|
} elseif ($stat = $zip->statName('META-INF/manifest.xml')) { |
|
|
|
|
69
|
|
|
$xml = simplexml_load_string( |
70
|
|
|
$this->securityScan($zip->getFromName('META-INF/manifest.xml')), |
71
|
|
|
'SimpleXMLElement', |
72
|
|
|
\PhpOffice\PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
73
|
|
|
); |
74
|
|
|
$namespacesContent = $xml->getNamespaces(true); |
75
|
|
|
if (isset($namespacesContent['manifest'])) { |
76
|
|
|
$manifest = $xml->children($namespacesContent['manifest']); |
77
|
|
|
foreach ($manifest as $manifestDataSet) { |
78
|
|
|
$manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']); |
79
|
|
|
if ($manifestAttributes->{'full-path'} == '/') { |
80
|
|
|
$mimeType = (string) $manifestAttributes->{'media-type'}; |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
$zip->close(); |
88
|
|
|
|
89
|
2 |
|
return $mimeType === 'application/vnd.oasis.opendocument.spreadsheet'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Reads names of the worksheets from a file, without parsing the whole file to a PhpSpreadsheet object |
97
|
|
|
* |
98
|
|
|
* @param string $pFilename |
99
|
|
|
* @throws Exception |
100
|
|
|
*/ |
101
|
|
|
public function listWorksheetNames($pFilename) |
102
|
|
|
{ |
103
|
|
|
File::assertFile($pFilename); |
104
|
|
|
|
105
|
|
|
$zipClass = \PhpOffice\PhpSpreadsheet\Settings::getZipClass(); |
106
|
|
|
|
107
|
|
|
$zip = new $zipClass(); |
108
|
|
|
if (!$zip->open($pFilename)) { |
109
|
|
|
throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$worksheetNames = []; |
113
|
|
|
|
114
|
|
|
$xml = new XMLReader(); |
115
|
|
|
$res = $xml->xml( |
|
|
|
|
116
|
|
|
$this->securityScanFile('zip://' . realpath($pFilename) . '#content.xml'), |
117
|
|
|
null, |
118
|
|
|
\PhpOffice\PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
119
|
|
|
); |
120
|
|
|
$xml->setParserProperty(2, true); |
121
|
|
|
|
122
|
|
|
// Step into the first level of content of the XML |
123
|
|
|
$xml->read(); |
124
|
|
|
while ($xml->read()) { |
125
|
|
|
// Quickly jump through to the office:body node |
126
|
|
|
while ($xml->name !== 'office:body') { |
127
|
|
|
if ($xml->isEmptyElement) { |
128
|
|
|
$xml->read(); |
129
|
|
|
} else { |
130
|
|
|
$xml->next(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
// Now read each node until we find our first table:table node |
134
|
|
|
while ($xml->read()) { |
135
|
|
|
if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) { |
136
|
|
|
// Loop through each table:table node reading the table:name attribute for each worksheet name |
137
|
|
|
do { |
138
|
|
|
$worksheetNames[] = $xml->getAttribute('table:name'); |
139
|
|
|
$xml->next(); |
140
|
|
|
} while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $worksheetNames; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns) |
150
|
|
|
* |
151
|
|
|
* @param string $pFilename |
152
|
|
|
* @throws Exception |
153
|
|
|
*/ |
154
|
|
|
public function listWorksheetInfo($pFilename) |
155
|
|
|
{ |
156
|
|
|
File::assertFile($pFilename); |
157
|
|
|
|
158
|
|
|
$worksheetInfo = []; |
159
|
|
|
|
160
|
|
|
$zipClass = \PhpOffice\PhpSpreadsheet\Settings::getZipClass(); |
161
|
|
|
|
162
|
|
|
$zip = new $zipClass(); |
163
|
|
|
if (!$zip->open($pFilename)) { |
164
|
|
|
throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$xml = new XMLReader(); |
168
|
|
|
$res = $xml->xml( |
|
|
|
|
169
|
|
|
$this->securityScanFile('zip://' . realpath($pFilename) . '#content.xml'), |
170
|
|
|
null, |
171
|
|
|
\PhpOffice\PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
172
|
|
|
); |
173
|
|
|
$xml->setParserProperty(2, true); |
174
|
|
|
|
175
|
|
|
// Step into the first level of content of the XML |
176
|
|
|
$xml->read(); |
177
|
|
|
while ($xml->read()) { |
178
|
|
|
// Quickly jump through to the office:body node |
179
|
|
|
while ($xml->name !== 'office:body') { |
180
|
|
|
if ($xml->isEmptyElement) { |
181
|
|
|
$xml->read(); |
182
|
|
|
} else { |
183
|
|
|
$xml->next(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
// Now read each node until we find our first table:table node |
187
|
|
|
while ($xml->read()) { |
188
|
|
|
if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) { |
189
|
|
|
$worksheetNames[] = $xml->getAttribute('table:name'); |
|
|
|
|
190
|
|
|
|
191
|
|
|
$tmpInfo = [ |
192
|
|
|
'worksheetName' => $xml->getAttribute('table:name'), |
193
|
|
|
'lastColumnLetter' => 'A', |
194
|
|
|
'lastColumnIndex' => 0, |
195
|
|
|
'totalRows' => 0, |
196
|
|
|
'totalColumns' => 0, |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
// Loop through each child node of the table:table element reading |
200
|
|
|
$currCells = 0; |
201
|
|
|
do { |
202
|
|
|
$xml->read(); |
203
|
|
|
if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) { |
204
|
|
|
$rowspan = $xml->getAttribute('table:number-rows-repeated'); |
205
|
|
|
$rowspan = empty($rowspan) ? 1 : $rowspan; |
206
|
|
|
$tmpInfo['totalRows'] += $rowspan; |
207
|
|
|
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells); |
208
|
|
|
$currCells = 0; |
209
|
|
|
// Step into the row |
210
|
|
|
$xml->read(); |
211
|
|
|
do { |
212
|
|
|
if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) { |
213
|
|
|
if (!$xml->isEmptyElement) { |
214
|
|
|
++$currCells; |
215
|
|
|
$xml->next(); |
216
|
|
|
} else { |
217
|
|
|
$xml->read(); |
218
|
|
|
} |
219
|
|
|
} elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) { |
220
|
|
|
$mergeSize = $xml->getAttribute('table:number-columns-repeated'); |
221
|
|
|
$currCells += $mergeSize; |
222
|
|
|
$xml->read(); |
223
|
|
|
} |
224
|
|
|
} while ($xml->name != 'table:table-row'); |
225
|
|
|
} |
226
|
|
|
} while ($xml->name != 'table:table'); |
227
|
|
|
|
228
|
|
|
$tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells); |
229
|
|
|
$tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1; |
230
|
|
|
$tmpInfo['lastColumnLetter'] = \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']); |
231
|
|
|
$worksheetInfo[] = $tmpInfo; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $worksheetInfo; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Loads PhpSpreadsheet from file |
241
|
|
|
* |
242
|
|
|
* @param string $pFilename |
243
|
|
|
* @throws Exception |
244
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet |
245
|
|
|
*/ |
246
|
1 |
|
public function load($pFilename) |
247
|
|
|
{ |
248
|
|
|
// Create new Spreadsheet |
249
|
1 |
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); |
250
|
|
|
|
251
|
|
|
// Load into this instance |
252
|
1 |
|
return $this->loadIntoExisting($pFilename, $spreadsheet); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
View Code Duplication |
private static function identifyFixedStyleValue($styleList, &$styleAttributeValue) |
|
|
|
|
256
|
|
|
{ |
257
|
|
|
$styleAttributeValue = strtolower($styleAttributeValue); |
258
|
|
|
foreach ($styleList as $style) { |
259
|
|
|
if ($styleAttributeValue == strtolower($style)) { |
260
|
|
|
$styleAttributeValue = $style; |
261
|
|
|
|
262
|
|
|
return true; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return false; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance |
271
|
|
|
* |
272
|
|
|
* @param string $pFilename |
273
|
|
|
* @param \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet |
274
|
|
|
* @throws Exception |
275
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet |
276
|
|
|
*/ |
277
|
1 |
|
public function loadIntoExisting($pFilename, \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet) |
278
|
|
|
{ |
279
|
1 |
|
File::assertFile($pFilename); |
280
|
|
|
|
281
|
1 |
|
$timezoneObj = new DateTimeZone('Europe/London'); |
282
|
1 |
|
$GMT = new \DateTimeZone('UTC'); |
283
|
|
|
|
284
|
1 |
|
$zipClass = \PhpOffice\PhpSpreadsheet\Settings::getZipClass(); |
285
|
|
|
|
286
|
1 |
|
$zip = new $zipClass(); |
287
|
1 |
|
if (!$zip->open($pFilename)) { |
288
|
|
|
throw new Exception('Could not open ' . $pFilename . ' for reading! Error opening file.'); |
289
|
|
|
} |
290
|
|
|
|
291
|
1 |
|
$xml = simplexml_load_string( |
292
|
1 |
|
$this->securityScan($zip->getFromName('meta.xml')), |
293
|
1 |
|
'SimpleXMLElement', |
294
|
1 |
|
\PhpOffice\PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
295
|
|
|
); |
296
|
1 |
|
$namespacesMeta = $xml->getNamespaces(true); |
297
|
|
|
|
298
|
1 |
|
$docProps = $spreadsheet->getProperties(); |
299
|
1 |
|
$officeProperty = $xml->children($namespacesMeta['office']); |
300
|
1 |
|
foreach ($officeProperty as $officePropertyData) { |
301
|
1 |
|
$officePropertyDC = []; |
302
|
1 |
|
if (isset($namespacesMeta['dc'])) { |
303
|
1 |
|
$officePropertyDC = $officePropertyData->children($namespacesMeta['dc']); |
304
|
|
|
} |
305
|
1 |
|
foreach ($officePropertyDC as $propertyName => $propertyValue) { |
306
|
1 |
|
$propertyValue = (string) $propertyValue; |
307
|
|
|
switch ($propertyName) { |
308
|
1 |
|
case 'title': |
309
|
1 |
|
$docProps->setTitle($propertyValue); |
310
|
1 |
|
break; |
311
|
1 |
|
case 'subject': |
312
|
1 |
|
$docProps->setSubject($propertyValue); |
313
|
1 |
|
break; |
314
|
1 |
|
case 'creator': |
315
|
|
|
$docProps->setCreator($propertyValue); |
316
|
|
|
$docProps->setLastModifiedBy($propertyValue); |
317
|
|
|
break; |
318
|
1 |
|
case 'date': |
319
|
1 |
|
$creationDate = strtotime($propertyValue); |
320
|
1 |
|
$docProps->setCreated($creationDate); |
|
|
|
|
321
|
1 |
|
$docProps->setModified($creationDate); |
|
|
|
|
322
|
1 |
|
break; |
323
|
1 |
|
case 'description': |
324
|
1 |
|
$docProps->setDescription($propertyValue); |
325
|
1 |
|
break; |
326
|
|
|
} |
327
|
|
|
} |
328
|
1 |
|
$officePropertyMeta = []; |
329
|
1 |
|
if (isset($namespacesMeta['dc'])) { |
330
|
1 |
|
$officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']); |
331
|
|
|
} |
332
|
1 |
|
foreach ($officePropertyMeta as $propertyName => $propertyValue) { |
333
|
1 |
|
$propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']); |
334
|
1 |
|
$propertyValue = (string) $propertyValue; |
335
|
|
|
switch ($propertyName) { |
336
|
1 |
|
case 'initial-creator': |
337
|
1 |
|
$docProps->setCreator($propertyValue); |
338
|
1 |
|
break; |
339
|
1 |
|
case 'keyword': |
340
|
1 |
|
$docProps->setKeywords($propertyValue); |
341
|
1 |
|
break; |
342
|
1 |
|
case 'creation-date': |
343
|
1 |
|
$creationDate = strtotime($propertyValue); |
344
|
1 |
|
$docProps->setCreated($creationDate); |
|
|
|
|
345
|
1 |
|
break; |
346
|
1 |
|
case 'user-defined': |
347
|
1 |
|
$propertyValueType = \PhpOffice\PhpSpreadsheet\Document\Properties::PROPERTY_TYPE_STRING; |
348
|
1 |
|
foreach ($propertyValueAttributes as $key => $value) { |
349
|
1 |
|
if ($key == 'name') { |
350
|
1 |
|
$propertyValueName = (string) $value; |
351
|
|
|
} elseif ($key == 'value-type') { |
352
|
|
|
switch ($value) { |
353
|
|
|
case 'date': |
354
|
|
|
$propertyValue = \PhpOffice\PhpSpreadsheet\Document\Properties::convertProperty($propertyValue, 'date'); |
355
|
|
|
$propertyValueType = \PhpOffice\PhpSpreadsheet\Document\Properties::PROPERTY_TYPE_DATE; |
356
|
|
|
break; |
357
|
|
|
case 'boolean': |
358
|
|
|
$propertyValue = \PhpOffice\PhpSpreadsheet\Document\Properties::convertProperty($propertyValue, 'bool'); |
359
|
|
|
$propertyValueType = \PhpOffice\PhpSpreadsheet\Document\Properties::PROPERTY_TYPE_BOOLEAN; |
360
|
|
|
break; |
361
|
|
|
case 'float': |
362
|
|
|
$propertyValue = \PhpOffice\PhpSpreadsheet\Document\Properties::convertProperty($propertyValue, 'r4'); |
363
|
|
|
$propertyValueType = \PhpOffice\PhpSpreadsheet\Document\Properties::PROPERTY_TYPE_FLOAT; |
364
|
|
|
break; |
365
|
|
|
default: |
366
|
1 |
|
$propertyValueType = \PhpOffice\PhpSpreadsheet\Document\Properties::PROPERTY_TYPE_STRING; |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
} |
370
|
1 |
|
$docProps->setCustomProperty($propertyValueName, $propertyValue, $propertyValueType); |
|
|
|
|
371
|
1 |
|
break; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
1 |
|
$xml = simplexml_load_string( |
377
|
1 |
|
$this->securityScan($zip->getFromName('content.xml')), |
378
|
1 |
|
'SimpleXMLElement', |
379
|
1 |
|
\PhpOffice\PhpSpreadsheet\Settings::getLibXmlLoaderOptions() |
380
|
|
|
); |
381
|
1 |
|
$namespacesContent = $xml->getNamespaces(true); |
382
|
|
|
|
383
|
1 |
|
$workbook = $xml->children($namespacesContent['office']); |
384
|
1 |
|
foreach ($workbook->body->spreadsheet as $workbookData) { |
385
|
1 |
|
$workbookData = $workbookData->children($namespacesContent['table']); |
386
|
1 |
|
$worksheetID = 0; |
387
|
1 |
|
foreach ($workbookData->table as $worksheetDataSet) { |
388
|
1 |
|
$worksheetData = $worksheetDataSet->children($namespacesContent['table']); |
389
|
1 |
|
$worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']); |
390
|
1 |
|
if ((isset($this->loadSheetsOnly)) && (isset($worksheetDataAttributes['name'])) && |
391
|
1 |
|
(!in_array($worksheetDataAttributes['name'], $this->loadSheetsOnly))) { |
392
|
|
|
continue; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
// Create new Worksheet |
396
|
1 |
|
$spreadsheet->createSheet(); |
397
|
1 |
|
$spreadsheet->setActiveSheetIndex($worksheetID); |
398
|
1 |
|
if (isset($worksheetDataAttributes['name'])) { |
399
|
1 |
|
$worksheetName = (string) $worksheetDataAttributes['name']; |
400
|
|
|
// Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in |
401
|
|
|
// formula cells... during the load, all formulae should be correct, and we're simply |
402
|
|
|
// bringing the worksheet name in line with the formula, not the reverse |
403
|
1 |
|
$spreadsheet->getActiveSheet()->setTitle($worksheetName, false); |
404
|
|
|
} |
405
|
|
|
|
406
|
1 |
|
$rowID = 1; |
407
|
1 |
|
foreach ($worksheetData as $key => $rowData) { |
408
|
|
|
switch ($key) { |
409
|
1 |
|
case 'table-header-rows': |
410
|
|
|
foreach ($rowData as $keyRowData => $cellData) { |
411
|
|
|
$rowData = $cellData; |
412
|
|
|
break; |
413
|
|
|
} |
414
|
|
|
break; |
415
|
1 |
|
case 'table-row': |
416
|
1 |
|
$rowDataTableAttributes = $rowData->attributes($namespacesContent['table']); |
417
|
1 |
|
$rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ? $rowDataTableAttributes['number-rows-repeated'] : 1; |
418
|
1 |
|
$columnID = 'A'; |
419
|
1 |
|
foreach ($rowData as $key => $cellData) { |
420
|
1 |
View Code Duplication |
if ($this->getReadFilter() !== null) { |
|
|
|
|
421
|
1 |
|
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) { |
|
|
|
|
422
|
|
|
++$columnID; |
423
|
|
|
continue; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
1 |
|
$cellDataText = (isset($namespacesContent['text'])) ? $cellData->children($namespacesContent['text']) : ''; |
428
|
1 |
|
$cellDataOffice = $cellData->children($namespacesContent['office']); |
429
|
1 |
|
$cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']); |
430
|
1 |
|
$cellDataTableAttributes = $cellData->attributes($namespacesContent['table']); |
431
|
|
|
|
432
|
1 |
|
$type = $formatting = $hyperlink = null; |
433
|
1 |
|
$hasCalculatedValue = false; |
434
|
1 |
|
$cellDataFormula = ''; |
435
|
1 |
|
if (isset($cellDataTableAttributes['formula'])) { |
436
|
1 |
|
$cellDataFormula = $cellDataTableAttributes['formula']; |
437
|
1 |
|
$hasCalculatedValue = true; |
438
|
|
|
} |
439
|
|
|
|
440
|
1 |
|
if (isset($cellDataOffice->annotation)) { |
441
|
1 |
|
$annotationText = $cellDataOffice->annotation->children($namespacesContent['text']); |
442
|
1 |
|
$textArray = []; |
443
|
1 |
|
foreach ($annotationText as $t) { |
444
|
1 |
|
if (isset($t->span)) { |
445
|
|
|
foreach ($t->span as $text) { |
446
|
|
|
$textArray[] = (string) $text; |
447
|
|
|
} |
448
|
|
|
} else { |
449
|
1 |
|
$textArray[] = (string) $t; |
450
|
|
|
} |
451
|
|
|
} |
452
|
1 |
|
$text = implode("\n", $textArray); |
453
|
1 |
|
$spreadsheet->getActiveSheet()->getComment($columnID . $rowID)->setText($this->parseRichText($text)); |
454
|
|
|
// ->setAuthor( $author ) |
|
|
|
|
455
|
|
|
} |
456
|
|
|
|
457
|
1 |
|
if (isset($cellDataText->p)) { |
458
|
|
|
// Consolidate if there are multiple p records (maybe with spans as well) |
459
|
1 |
|
$dataArray = []; |
460
|
|
|
// Text can have multiple text:p and within those, multiple text:span. |
461
|
|
|
// text:p newlines, but text:span does not. |
462
|
|
|
// Also, here we assume there is no text data is span fields are specified, since |
463
|
|
|
// we have no way of knowing proper positioning anyway. |
464
|
1 |
|
foreach ($cellDataText->p as $pData) { |
465
|
1 |
|
if (isset($pData->span)) { |
466
|
|
|
// span sections do not newline, so we just create one large string here |
467
|
1 |
|
$spanSection = ''; |
468
|
1 |
|
foreach ($pData->span as $spanData) { |
469
|
1 |
|
$spanSection .= $spanData; |
470
|
|
|
} |
471
|
1 |
|
array_push($dataArray, $spanSection); |
472
|
|
|
} elseif (isset($pData->a)) { |
473
|
|
|
//Reading the hyperlinks in p |
474
|
1 |
|
array_push($dataArray, $pData->a); |
475
|
|
|
} else { |
476
|
1 |
|
array_push($dataArray, $pData); |
477
|
|
|
} |
478
|
|
|
} |
479
|
1 |
|
$allCellDataText = implode($dataArray, "\n"); |
480
|
|
|
|
481
|
1 |
|
switch ($cellDataOfficeAttributes['value-type']) { |
482
|
1 |
|
case 'string': |
483
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING; |
484
|
1 |
|
$dataValue = $allCellDataText; |
485
|
1 |
|
if (isset($dataValue->a)) { |
486
|
|
|
$dataValue = $dataValue->a; |
487
|
|
|
$cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']); |
488
|
|
|
$hyperlink = $cellXLinkAttributes['href']; |
489
|
|
|
} |
490
|
1 |
|
break; |
491
|
1 |
|
case 'boolean': |
492
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_BOOL; |
493
|
1 |
|
$dataValue = ($allCellDataText == 'TRUE') ? true : false; |
494
|
1 |
|
break; |
495
|
1 |
View Code Duplication |
case 'percentage': |
|
|
|
|
496
|
|
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC; |
497
|
|
|
$dataValue = (float) $cellDataOfficeAttributes['value']; |
498
|
|
|
if (floor($dataValue) == $dataValue) { |
499
|
|
|
$dataValue = (integer) $dataValue; |
500
|
|
|
} |
501
|
|
|
$formatting = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_PERCENTAGE_00; |
502
|
|
|
break; |
503
|
1 |
View Code Duplication |
case 'currency': |
|
|
|
|
504
|
|
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC; |
505
|
|
|
$dataValue = (float) $cellDataOfficeAttributes['value']; |
506
|
|
|
if (floor($dataValue) == $dataValue) { |
507
|
|
|
$dataValue = (integer) $dataValue; |
508
|
|
|
} |
509
|
|
|
$formatting = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE; |
510
|
|
|
break; |
511
|
1 |
|
case 'float': |
512
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC; |
513
|
1 |
|
$dataValue = (float) $cellDataOfficeAttributes['value']; |
514
|
1 |
|
if (floor($dataValue) == $dataValue) { |
515
|
1 |
|
if ($dataValue == (integer) $dataValue) { |
516
|
1 |
|
$dataValue = (integer) $dataValue; |
517
|
|
|
} else { |
518
|
|
|
$dataValue = (float) $dataValue; |
519
|
|
|
} |
520
|
|
|
} |
521
|
1 |
|
break; |
522
|
1 |
|
case 'date': |
523
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC; |
524
|
1 |
|
$dateObj = new DateTime($cellDataOfficeAttributes['date-value'], $GMT); |
525
|
1 |
|
$dateObj->setTimeZone($timezoneObj); |
526
|
1 |
|
list($year, $month, $day, $hour, $minute, $second) = explode(' ', $dateObj->format('Y m d H i s')); |
527
|
1 |
|
$dataValue = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($year, $month, $day, $hour, $minute, $second); |
528
|
1 |
|
if ($dataValue != floor($dataValue)) { |
529
|
1 |
|
$formatting = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_XLSX15 . ' ' . \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME4; |
530
|
|
|
} else { |
531
|
1 |
|
$formatting = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_XLSX15; |
532
|
|
|
} |
533
|
1 |
|
break; |
534
|
1 |
|
case 'time': |
535
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC; |
536
|
1 |
|
$dataValue = \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel(strtotime('01-01-1970 ' . implode(':', sscanf($cellDataOfficeAttributes['time-value'], 'PT%dH%dM%dS')))); |
537
|
1 |
|
$formatting = \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_TIME4; |
538
|
1 |
|
break; |
539
|
|
|
} |
540
|
|
|
} else { |
541
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NULL; |
542
|
1 |
|
$dataValue = null; |
543
|
|
|
} |
544
|
|
|
|
545
|
1 |
|
if ($hasCalculatedValue) { |
546
|
1 |
|
$type = \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_FORMULA; |
547
|
1 |
|
$cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=') + 1); |
548
|
1 |
|
$temp = explode('"', $cellDataFormula); |
549
|
1 |
|
$tKey = false; |
550
|
1 |
|
foreach ($temp as &$value) { |
551
|
|
|
// Only replace in alternate array entries (i.e. non-quoted blocks) |
552
|
1 |
|
if ($tKey = !$tKey) { |
553
|
1 |
|
$value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/Ui', '$1!$2:$3', $value); // Cell range reference in another sheet |
554
|
1 |
|
$value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/Ui', '$1!$2', $value); // Cell reference in another sheet |
555
|
1 |
|
$value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/Ui', '$1:$2', $value); // Cell range reference |
556
|
1 |
|
$value = preg_replace('/\[\.([^\.]+)\]/Ui', '$1', $value); // Simple cell reference |
557
|
1 |
|
$value = \PhpOffice\PhpSpreadsheet\Calculation::translateSeparator(';', ',', $value, $inBraces); |
558
|
|
|
} |
559
|
|
|
} |
560
|
1 |
|
unset($value); |
561
|
|
|
// Then rebuild the formula string |
562
|
1 |
|
$cellDataFormula = implode('"', $temp); |
563
|
|
|
} |
564
|
|
|
|
565
|
1 |
|
$colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ? $cellDataTableAttributes['number-columns-repeated'] : 1; |
566
|
1 |
|
if ($type !== null) { |
567
|
1 |
|
for ($i = 0; $i < $colRepeats; ++$i) { |
568
|
1 |
|
if ($i > 0) { |
569
|
1 |
|
++$columnID; |
570
|
|
|
} |
571
|
1 |
|
if ($type !== \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NULL) { |
572
|
1 |
|
for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) { |
573
|
1 |
|
$rID = $rowID + $rowAdjust; |
574
|
1 |
|
$spreadsheet->getActiveSheet()->getCell($columnID . $rID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue), $type); |
|
|
|
|
575
|
1 |
|
if ($hasCalculatedValue) { |
576
|
1 |
|
$spreadsheet->getActiveSheet()->getCell($columnID . $rID)->setCalculatedValue($dataValue); |
577
|
|
|
} |
578
|
1 |
|
if ($formatting !== null) { |
579
|
1 |
|
$spreadsheet->getActiveSheet()->getStyle($columnID . $rID)->getNumberFormat()->setFormatCode($formatting); |
580
|
|
|
} else { |
581
|
1 |
|
$spreadsheet->getActiveSheet()->getStyle($columnID . $rID)->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_GENERAL); |
582
|
|
|
} |
583
|
1 |
|
if ($hyperlink !== null) { |
584
|
|
|
$spreadsheet->getActiveSheet()->getCell($columnID . $rID)->getHyperlink()->setUrl($hyperlink); |
585
|
|
|
} |
586
|
|
|
} |
587
|
|
|
} |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
// Merged cells |
592
|
1 |
|
if ((isset($cellDataTableAttributes['number-columns-spanned'])) || (isset($cellDataTableAttributes['number-rows-spanned']))) { |
593
|
1 |
|
if (($type !== \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NULL) || (!$this->readDataOnly)) { |
594
|
1 |
|
$columnTo = $columnID; |
595
|
1 |
View Code Duplication |
if (isset($cellDataTableAttributes['number-columns-spanned'])) { |
|
|
|
|
596
|
1 |
|
$columnTo = \PhpOffice\PhpSpreadsheet\Cell::stringFromColumnIndex(\PhpOffice\PhpSpreadsheet\Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-spanned'] - 2); |
597
|
|
|
} |
598
|
1 |
|
$rowTo = $rowID; |
599
|
1 |
|
if (isset($cellDataTableAttributes['number-rows-spanned'])) { |
600
|
1 |
|
$rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1; |
601
|
|
|
} |
602
|
1 |
|
$cellRange = $columnID . $rowID . ':' . $columnTo . $rowTo; |
603
|
1 |
|
$spreadsheet->getActiveSheet()->mergeCells($cellRange); |
604
|
|
|
} |
605
|
|
|
} |
606
|
|
|
|
607
|
1 |
|
++$columnID; |
608
|
|
|
} |
609
|
1 |
|
$rowID += $rowRepeats; |
610
|
1 |
|
break; |
611
|
|
|
} |
612
|
|
|
} |
613
|
1 |
|
++$worksheetID; |
614
|
|
|
} |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
// Return |
618
|
1 |
|
return $spreadsheet; |
619
|
|
|
} |
620
|
|
|
|
621
|
1 |
|
private function parseRichText($is = '') |
622
|
|
|
{ |
623
|
1 |
|
$value = new \PhpOffice\PhpSpreadsheet\RichText(); |
624
|
|
|
|
625
|
1 |
|
$value->createText($is); |
626
|
|
|
|
627
|
1 |
|
return $value; |
628
|
|
|
} |
629
|
|
|
} |
630
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.