1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader; |
4
|
|
|
|
5
|
|
|
use DOMAttr; |
6
|
|
|
use DOMDocument; |
7
|
|
|
use DOMElement; |
8
|
|
|
use DOMNode; |
9
|
|
|
use DOMText; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Comment; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Document\Properties; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Helper\Dimension as CssDimension; |
17
|
|
|
use PhpOffice\PhpSpreadsheet\Helper\Html as HelperHtml; |
18
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
19
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
20
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
21
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Color; |
22
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
23
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
24
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Style; |
25
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
26
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
27
|
|
|
use Throwable; |
28
|
|
|
|
29
|
|
|
class Html extends BaseReader |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Sample size to read to determine if it's HTML or not. |
33
|
|
|
*/ |
34
|
|
|
const TEST_SAMPLE_SIZE = 2048; |
35
|
|
|
|
36
|
|
|
private const STARTS_WITH_BOM = '/^(?:\xfe\xff|\xff\xfe|\xEF\xBB\xBF)/'; |
37
|
|
|
|
38
|
|
|
private const DECLARES_CHARSET = '/\\bcharset=/i'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Input encoding. |
42
|
|
|
*/ |
43
|
|
|
protected string $inputEncoding = 'ANSI'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sheet index to read. |
47
|
|
|
*/ |
48
|
|
|
protected int $sheetIndex = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Formats. |
52
|
|
|
*/ |
53
|
|
|
protected array $formats = [ |
54
|
|
|
'h1' => [ |
55
|
|
|
'font' => [ |
56
|
|
|
'bold' => true, |
57
|
|
|
'size' => 24, |
58
|
|
|
], |
59
|
|
|
], // Bold, 24pt |
60
|
|
|
'h2' => [ |
61
|
|
|
'font' => [ |
62
|
|
|
'bold' => true, |
63
|
|
|
'size' => 18, |
64
|
|
|
], |
65
|
|
|
], // Bold, 18pt |
66
|
|
|
'h3' => [ |
67
|
|
|
'font' => [ |
68
|
|
|
'bold' => true, |
69
|
|
|
'size' => 13.5, |
70
|
|
|
], |
71
|
|
|
], // Bold, 13.5pt |
72
|
|
|
'h4' => [ |
73
|
|
|
'font' => [ |
74
|
|
|
'bold' => true, |
75
|
|
|
'size' => 12, |
76
|
|
|
], |
77
|
|
|
], // Bold, 12pt |
78
|
|
|
'h5' => [ |
79
|
|
|
'font' => [ |
80
|
|
|
'bold' => true, |
81
|
|
|
'size' => 10, |
82
|
|
|
], |
83
|
|
|
], // Bold, 10pt |
84
|
|
|
'h6' => [ |
85
|
|
|
'font' => [ |
86
|
|
|
'bold' => true, |
87
|
|
|
'size' => 7.5, |
88
|
|
|
], |
89
|
|
|
], // Bold, 7.5pt |
90
|
|
|
'a' => [ |
91
|
|
|
'font' => [ |
92
|
|
|
'underline' => true, |
93
|
|
|
'color' => [ |
94
|
|
|
'argb' => Color::COLOR_BLUE, |
95
|
|
|
], |
96
|
|
|
], |
97
|
|
|
], // Blue underlined |
98
|
|
|
'hr' => [ |
99
|
|
|
'borders' => [ |
100
|
|
|
'bottom' => [ |
101
|
|
|
'borderStyle' => Border::BORDER_THIN, |
102
|
|
|
'color' => [ |
103
|
|
|
Color::COLOR_BLACK, |
104
|
|
|
], |
105
|
|
|
], |
106
|
|
|
], |
107
|
|
|
], // Bottom border |
108
|
|
|
'strong' => [ |
109
|
|
|
'font' => [ |
110
|
|
|
'bold' => true, |
111
|
|
|
], |
112
|
|
|
], // Bold |
113
|
|
|
'b' => [ |
114
|
|
|
'font' => [ |
115
|
|
|
'bold' => true, |
116
|
|
|
], |
117
|
|
|
], // Bold |
118
|
|
|
'i' => [ |
119
|
|
|
'font' => [ |
120
|
|
|
'italic' => true, |
121
|
|
|
], |
122
|
|
|
], // Italic |
123
|
|
|
'em' => [ |
124
|
|
|
'font' => [ |
125
|
|
|
'italic' => true, |
126
|
|
|
], |
127
|
|
|
], // Italic |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
protected array $rowspan = []; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a new HTML Reader instance. |
134
|
513 |
|
*/ |
135
|
|
|
public function __construct() |
136
|
513 |
|
{ |
137
|
513 |
|
parent::__construct(); |
138
|
|
|
$this->securityScanner = XmlScanner::getInstance($this); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Validate that the current file is an HTML file. |
143
|
493 |
|
*/ |
144
|
|
|
public function canRead(string $filename): bool |
145
|
|
|
{ |
146
|
|
|
// Check if file exists |
147
|
493 |
|
try { |
148
|
1 |
|
$this->openFile($filename); |
149
|
1 |
|
} catch (Exception) { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
492 |
|
|
153
|
|
|
$beginning = preg_replace(self::STARTS_WITH_BOM, '', $this->readBeginning()) ?? ''; |
154
|
492 |
|
|
155
|
492 |
|
$startWithTag = self::startsWithTag($beginning); |
156
|
492 |
|
$containsTags = self::containsTags($beginning); |
157
|
|
|
$endsWithTag = self::endsWithTag($this->readEnding()); |
158
|
492 |
|
|
159
|
|
|
fclose($this->fileHandle); |
160
|
492 |
|
|
161
|
|
|
return $startWithTag && $containsTags && $endsWithTag; |
162
|
|
|
} |
163
|
492 |
|
|
164
|
|
|
private function readBeginning(): string |
165
|
492 |
|
{ |
166
|
|
|
fseek($this->fileHandle, 0); |
167
|
492 |
|
|
168
|
|
|
return (string) fread($this->fileHandle, self::TEST_SAMPLE_SIZE); |
169
|
|
|
} |
170
|
492 |
|
|
171
|
|
|
private function readEnding(): string |
172
|
492 |
|
{ |
173
|
|
|
$meta = stream_get_meta_data($this->fileHandle); |
174
|
492 |
|
// Phpstan incorrectly flags following line for Php8.2-, corrected in 8.3 |
175
|
|
|
$filename = $meta['uri']; //@phpstan-ignore-line |
176
|
492 |
|
|
177
|
492 |
|
clearstatcache(true, $filename); |
178
|
492 |
|
$size = (int) filesize($filename); |
179
|
1 |
|
if ($size === 0) { |
180
|
|
|
return ''; |
181
|
|
|
} |
182
|
491 |
|
|
183
|
491 |
|
$blockSize = self::TEST_SAMPLE_SIZE; |
184
|
44 |
|
if ($size < $blockSize) { |
185
|
|
|
$blockSize = $size; |
186
|
|
|
} |
187
|
491 |
|
|
188
|
|
|
fseek($this->fileHandle, $size - $blockSize); |
189
|
491 |
|
|
190
|
|
|
return (string) fread($this->fileHandle, $blockSize); |
191
|
|
|
} |
192
|
492 |
|
|
193
|
|
|
private static function startsWithTag(string $data): bool |
194
|
492 |
|
{ |
195
|
|
|
return str_starts_with(trim($data), '<'); |
196
|
|
|
} |
197
|
492 |
|
|
198
|
|
|
private static function endsWithTag(string $data): bool |
199
|
492 |
|
{ |
200
|
|
|
return str_ends_with(trim($data), '>'); |
201
|
|
|
} |
202
|
492 |
|
|
203
|
|
|
private static function containsTags(string $data): bool |
204
|
492 |
|
{ |
205
|
|
|
return strlen($data) !== strlen(strip_tags($data)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Loads Spreadsheet from file. |
210
|
474 |
|
*/ |
211
|
|
|
public function loadSpreadsheetFromFile(string $filename): Spreadsheet |
212
|
|
|
{ |
213
|
474 |
|
// Create new Spreadsheet |
214
|
474 |
|
$spreadsheet = new Spreadsheet(); |
215
|
|
|
$spreadsheet->setValueBinder($this->valueBinder); |
216
|
|
|
|
217
|
474 |
|
// Load into this instance |
218
|
|
|
return $this->loadIntoExisting($filename, $spreadsheet); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
// Data Array used for testing only, should write to Spreadsheet object on completion of tests |
222
|
|
|
|
223
|
|
|
protected array $dataArray = []; |
224
|
|
|
|
225
|
|
|
protected int $tableLevel = 0; |
226
|
|
|
|
227
|
|
|
protected array $nestedColumn = ['A']; |
228
|
488 |
|
|
229
|
|
|
protected function setTableStartColumn(string $column): string |
230
|
488 |
|
{ |
231
|
488 |
|
if ($this->tableLevel == 0) { |
232
|
|
|
$column = 'A'; |
233
|
488 |
|
} |
234
|
488 |
|
++$this->tableLevel; |
235
|
|
|
$this->nestedColumn[$this->tableLevel] = $column; |
236
|
488 |
|
|
237
|
|
|
return $this->nestedColumn[$this->tableLevel]; |
238
|
|
|
} |
239
|
484 |
|
|
240
|
|
|
protected function getTableStartColumn(): string |
241
|
484 |
|
{ |
242
|
|
|
return $this->nestedColumn[$this->tableLevel]; |
243
|
|
|
} |
244
|
487 |
|
|
245
|
|
|
protected function releaseTableStartColumn(): string |
246
|
487 |
|
{ |
247
|
|
|
--$this->tableLevel; |
248
|
487 |
|
|
249
|
|
|
return array_pop($this->nestedColumn); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Flush cell. |
254
|
489 |
|
*/ |
255
|
|
|
protected function flushCell(Worksheet $sheet, string $column, int|string $row, mixed &$cellContent, array $attributeArray): void |
256
|
489 |
|
{ |
257
|
|
|
if (is_string($cellContent)) { |
258
|
489 |
|
// Simple String content |
259
|
|
|
if (trim($cellContent) > '') { |
260
|
|
|
// Only actually write it if there's content in the string |
261
|
|
|
// Write to worksheet to be done here... |
262
|
|
|
// ... we return the cell, so we can mess about with styles more easily |
263
|
|
|
|
264
|
475 |
|
// Set cell value explicitly if there is data-type attribute |
265
|
1 |
|
if (isset($attributeArray['data-type'])) { |
266
|
1 |
|
$datatype = $attributeArray['data-type']; |
267
|
|
|
if (in_array($datatype, [DataType::TYPE_STRING, DataType::TYPE_STRING2, DataType::TYPE_INLINE])) { |
268
|
1 |
|
//Prevent to Excel treat string with beginning equal sign or convert big numbers to scientific number |
269
|
1 |
|
if (str_starts_with($cellContent, '=')) { |
270
|
1 |
|
$sheet->getCell($column . $row) |
271
|
1 |
|
->getStyle() |
272
|
|
|
->setQuotePrefix(true); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
if ($datatype === DataType::TYPE_BOOL) { |
276
|
|
|
$cellContent = self::convertBoolean($cellContent); |
|
|
|
|
277
|
1 |
|
if (!is_bool($cellContent)) { |
278
|
1 |
|
$attributeArray['data-type'] = DataType::TYPE_STRING; |
279
|
1 |
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
474 |
|
//catching the Exception and ignoring the invalid data types |
283
|
|
|
try { |
284
|
475 |
|
$sheet->setCellValueExplicit($column . $row, $cellContent, $attributeArray['data-type']); |
285
|
|
|
} catch (SpreadsheetException) { |
286
|
|
|
$sheet->setCellValue($column . $row, $cellContent); |
287
|
|
|
} |
288
|
|
|
} else { |
289
|
|
|
$sheet->setCellValue($column . $row, $cellContent); |
290
|
|
|
} |
291
|
489 |
|
$this->dataArray[$row][$column] = $cellContent; |
292
|
|
|
} |
293
|
|
|
} else { |
294
|
489 |
|
// We have a Rich Text run |
295
|
|
|
// TODO |
296
|
489 |
|
$this->dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent; |
297
|
|
|
} |
298
|
489 |
|
$cellContent = (string) ''; |
299
|
475 |
|
} |
300
|
|
|
|
301
|
|
|
/** @var array<int, array<int, string>> */ |
302
|
489 |
|
private static array $falseTrueArray = []; |
303
|
489 |
|
|
304
|
489 |
|
private function convertBoolean(?string $cellContent): bool|string |
305
|
489 |
|
{ |
306
|
489 |
|
if ($cellContent === '1') { |
307
|
489 |
|
return true; |
308
|
|
|
} |
309
|
489 |
|
if ($cellContent === '0' || $cellContent === '' || $cellContent === null) { |
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
if (empty(self::$falseTrueArray)) { |
313
|
489 |
|
$calc = Calculation::getInstance(); |
314
|
|
|
self::$falseTrueArray = $calc->getFalseTrueArray(); |
315
|
489 |
|
} |
316
|
455 |
|
if (in_array(mb_strtoupper($cellContent), self::$falseTrueArray[1], true)) { |
317
|
|
|
return true; |
318
|
|
|
} |
319
|
455 |
|
if (in_array(mb_strtoupper($cellContent), self::$falseTrueArray[0], true)) { |
320
|
452 |
|
return false; |
321
|
3 |
|
} |
322
|
|
|
|
323
|
|
|
return $cellContent; |
324
|
455 |
|
} |
325
|
|
|
|
326
|
489 |
|
private function processDomElementBody(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child): void |
327
|
|
|
{ |
328
|
|
|
$attributeArray = []; |
329
|
|
|
/** @var DOMAttr $attribute */ |
330
|
|
|
foreach ($child->attributes as $attribute) { |
331
|
|
|
$attributeArray[$attribute->name] = $attribute->value; |
332
|
489 |
|
} |
333
|
|
|
|
334
|
489 |
|
if ($child->nodeName === 'body') { |
335
|
440 |
|
$row = 1; |
336
|
9 |
|
$column = 'A'; |
337
|
9 |
|
$cellContent = ''; |
338
|
9 |
|
$this->tableLevel = 0; |
339
|
9 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
340
|
1 |
|
} else { |
341
|
|
|
$this->processDomElementTitle($sheet, $row, $column, $cellContent, $child, $attributeArray); |
342
|
9 |
|
} |
343
|
2 |
|
} |
344
|
2 |
|
|
345
|
2 |
|
private function processDomElementTitle(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
346
|
|
|
{ |
347
|
|
|
if ($child->nodeName === 'title') { |
348
|
|
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
349
|
440 |
|
|
350
|
|
|
try { |
351
|
|
|
$sheet->setTitle($cellContent, true, true); |
352
|
440 |
|
$sheet->getParent()?->getProperties()?->setTitle($cellContent); |
353
|
2 |
|
} catch (SpreadsheetException) { |
354
|
|
|
// leave default title if too long or illegal chars |
355
|
|
|
} |
356
|
489 |
|
$cellContent = ''; |
357
|
|
|
} else { |
358
|
|
|
$this->processDomElementSpanEtc($sheet, $row, $column, $cellContent, $child, $attributeArray); |
359
|
|
|
} |
360
|
489 |
|
} |
361
|
|
|
|
362
|
489 |
|
private const SPAN_ETC = ['span', 'div', 'font', 'i', 'em', 'strong', 'b']; |
363
|
1 |
|
|
364
|
1 |
|
private function processDomElementSpanEtc(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
365
|
1 |
|
{ |
366
|
1 |
|
if (in_array((string) $child->nodeName, self::SPAN_ETC, true)) { |
367
|
|
|
if (isset($attributeArray['class']) && $attributeArray['class'] === 'comment') { |
368
|
1 |
|
$sheet->getComment($column . $row) |
369
|
|
|
->getText() |
370
|
|
|
->createTextRun($child->textContent); |
371
|
489 |
|
if (isset($attributeArray['dir']) && $attributeArray['dir'] === 'rtl') { |
372
|
|
|
$sheet->getComment($column . $row)->setTextboxDirection(Comment::TEXTBOX_DIRECTION_RTL); |
373
|
|
|
} |
374
|
489 |
|
if (isset($attributeArray['style'])) { |
375
|
|
|
$alignStyle = $attributeArray['style']; |
376
|
489 |
|
if (preg_match('/\\btext-align:\\s*(left|right|center|justify)\\b/', $alignStyle, $matches) === 1) { |
377
|
4 |
|
$sheet->getComment($column . $row)->setAlignment($matches[1]); |
378
|
|
|
} |
379
|
4 |
|
} |
380
|
4 |
|
} else { |
381
|
|
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
382
|
|
|
} |
383
|
1 |
|
|
384
|
1 |
|
if (isset($this->formats[$child->nodeName])) { |
385
|
|
|
$sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
386
|
|
|
} |
387
|
489 |
|
} else { |
388
|
|
|
$this->processDomElementHr($sheet, $row, $column, $cellContent, $child, $attributeArray); |
389
|
|
|
} |
390
|
|
|
} |
391
|
489 |
|
|
392
|
|
|
private function processDomElementHr(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
393
|
489 |
|
{ |
394
|
12 |
|
if ($child->nodeName === 'hr') { |
395
|
|
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
396
|
12 |
|
++$row; |
397
|
3 |
|
if (isset($this->formats[$child->nodeName])) { |
398
|
3 |
|
$sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
399
|
3 |
|
} |
400
|
|
|
++$row; |
401
|
|
|
} |
402
|
3 |
|
// fall through to br |
403
|
10 |
|
$this->processDomElementBr($sheet, $row, $column, $cellContent, $child, $attributeArray); |
404
|
9 |
|
} |
405
|
9 |
|
|
406
|
|
|
private function processDomElementBr(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
407
|
|
|
{ |
408
|
|
|
if ($child->nodeName === 'br' || $child->nodeName === 'hr') { |
409
|
|
|
if ($this->tableLevel > 0) { |
410
|
|
|
// If we're inside a table, replace with a newline and set the cell to wrap |
411
|
12 |
|
$cellContent .= "\n"; |
412
|
|
|
$sheet->getStyle($column . $row)->getAlignment()->setWrapText(true); |
413
|
489 |
|
} else { |
414
|
|
|
// Otherwise flush our existing content and move the row cursor on |
415
|
|
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
416
|
|
|
++$row; |
417
|
|
|
} |
418
|
|
|
} else { |
419
|
489 |
|
$this->processDomElementA($sheet, $row, $column, $cellContent, $child, $attributeArray); |
420
|
|
|
} |
421
|
489 |
|
} |
422
|
2 |
|
|
423
|
|
|
private function processDomElementA(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
424
|
1 |
|
{ |
425
|
1 |
|
if ($child->nodeName === 'a') { |
426
|
1 |
|
foreach ($attributeArray as $attributeName => $attributeValue) { |
427
|
|
|
switch ($attributeName) { |
428
|
2 |
|
case 'href': |
429
|
1 |
|
$sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue); |
430
|
1 |
|
if (isset($this->formats[$child->nodeName])) { |
431
|
|
|
$sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
432
|
2 |
|
} |
433
|
2 |
|
|
434
|
|
|
break; |
435
|
2 |
|
case 'class': |
436
|
1 |
|
if ($attributeValue === 'comment-indicator') { |
437
|
|
|
break; // Ignore - it's just a red square. |
438
|
|
|
} |
439
|
2 |
|
} |
440
|
2 |
|
} |
441
|
|
|
// no idea why this should be needed |
442
|
|
|
//$cellContent .= ' '; |
443
|
489 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
444
|
|
|
} else { |
445
|
|
|
$this->processDomElementH1Etc($sheet, $row, $column, $cellContent, $child, $attributeArray); |
446
|
|
|
} |
447
|
489 |
|
} |
448
|
|
|
|
449
|
489 |
|
private const H1_ETC = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'p']; |
450
|
2 |
|
|
451
|
|
|
private function processDomElementH1Etc(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
452
|
1 |
|
{ |
453
|
1 |
|
if (in_array((string) $child->nodeName, self::H1_ETC, true)) { |
454
|
|
|
if ($this->tableLevel > 0) { |
455
|
2 |
|
// If we're inside a table, replace with a newline |
456
|
1 |
|
$cellContent .= $cellContent ? "\n" : ''; |
457
|
|
|
$sheet->getStyle($column . $row)->getAlignment()->setWrapText(true); |
458
|
2 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
459
|
2 |
|
} else { |
460
|
2 |
|
if ($cellContent > '') { |
461
|
2 |
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
462
|
|
|
++$row; |
463
|
|
|
} |
464
|
489 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
465
|
|
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
466
|
|
|
|
467
|
|
|
if (isset($this->formats[$child->nodeName])) { |
468
|
489 |
|
$sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]); |
469
|
|
|
} |
470
|
489 |
|
|
471
|
11 |
|
++$row; |
472
|
|
|
$column = 'A'; |
473
|
489 |
|
} |
474
|
|
|
} else { |
475
|
|
|
$this->processDomElementLi($sheet, $row, $column, $cellContent, $child, $attributeArray); |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|
479
|
489 |
|
private function processDomElementLi(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
480
|
|
|
{ |
481
|
489 |
|
if ($child->nodeName === 'li') { |
482
|
488 |
|
if ($this->tableLevel > 0) { |
483
|
440 |
|
// If we're inside a table, replace with a newline |
484
|
440 |
|
$cellContent .= $cellContent ? "\n" : ''; |
485
|
440 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
486
|
|
|
} else { |
487
|
488 |
|
if ($cellContent > '') { |
488
|
488 |
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
489
|
488 |
|
} |
490
|
488 |
|
++$row; |
491
|
2 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
492
|
|
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
493
|
488 |
|
$column = 'A'; |
494
|
487 |
|
} |
495
|
487 |
|
} else { |
496
|
2 |
|
$this->processDomElementImg($sheet, $row, $column, $cellContent, $child, $attributeArray); |
497
|
|
|
} |
498
|
487 |
|
} |
499
|
|
|
|
500
|
|
|
private function processDomElementImg(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
501
|
489 |
|
{ |
502
|
|
|
if ($child->nodeName === 'img') { |
503
|
|
|
$this->insertImage($sheet, $column, $row, $attributeArray); |
504
|
|
|
} else { |
505
|
489 |
|
$this->processDomElementTable($sheet, $row, $column, $cellContent, $child, $attributeArray); |
506
|
|
|
} |
507
|
489 |
|
} |
508
|
438 |
|
|
509
|
438 |
|
private string $currentColumn = 'A'; |
510
|
489 |
|
|
511
|
484 |
|
private function processDomElementTable(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
512
|
484 |
|
{ |
513
|
484 |
|
if ($child->nodeName === 'table') { |
514
|
|
|
if (isset($attributeArray['class'])) { |
515
|
483 |
|
$classes = explode(' ', $attributeArray['class']); |
516
|
1 |
|
$sheet->setShowGridlines(in_array('gridlines', $classes, true)); |
517
|
|
|
$sheet->setPrintGridlines(in_array('gridlinesp', $classes, true)); |
518
|
|
|
} |
519
|
483 |
|
$this->currentColumn = 'A'; |
520
|
|
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
521
|
489 |
|
$column = $this->setTableStartColumn($column); |
522
|
|
|
if ($this->tableLevel > 1 && $row > 1) { |
523
|
|
|
--$row; |
524
|
|
|
} |
525
|
489 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
526
|
|
|
$column = $this->releaseTableStartColumn(); |
527
|
489 |
|
if ($this->tableLevel > 1) { |
528
|
489 |
|
++$column; |
529
|
|
|
} else { |
530
|
484 |
|
++$row; |
531
|
|
|
} |
532
|
|
|
} else { |
533
|
|
|
$this->processDomElementTr($sheet, $row, $column, $cellContent, $child, $attributeArray); |
534
|
483 |
|
} |
535
|
|
|
} |
536
|
483 |
|
|
537
|
1 |
|
private function processDomElementTr(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
538
|
1 |
|
{ |
539
|
1 |
|
if ($child->nodeName === 'col') { |
540
|
1 |
|
$this->applyInlineStyle($sheet, -1, $this->currentColumn, $attributeArray); |
541
|
1 |
|
++$this->currentColumn; |
542
|
1 |
|
} elseif ($child->nodeName === 'tr') { |
543
|
1 |
|
$column = $this->getTableStartColumn(); |
544
|
1 |
|
$cellContent = ''; |
545
|
|
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
546
|
|
|
|
547
|
|
|
if (isset($attributeArray['height'])) { |
548
|
483 |
|
$sheet->getRowDimension($row)->setRowHeight($attributeArray['height']); |
549
|
|
|
} |
550
|
483 |
|
|
551
|
1 |
|
++$row; |
552
|
|
|
} else { |
553
|
|
|
$this->processDomElementThTdOther($sheet, $row, $column, $cellContent, $child, $attributeArray); |
554
|
|
|
} |
555
|
483 |
|
} |
556
|
|
|
|
557
|
483 |
|
private function processDomElementThTdOther(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
558
|
1 |
|
{ |
559
|
|
|
if ($child->nodeName !== 'td' && $child->nodeName !== 'th') { |
560
|
|
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
561
|
|
|
} else { |
562
|
483 |
|
$this->processDomElementThTd($sheet, $row, $column, $cellContent, $child, $attributeArray); |
563
|
|
|
} |
564
|
483 |
|
} |
565
|
1 |
|
|
566
|
|
|
private function processDomElementBgcolor(Worksheet $sheet, int $row, string $column, array $attributeArray): void |
567
|
|
|
{ |
568
|
|
|
if (isset($attributeArray['bgcolor'])) { |
569
|
483 |
|
$sheet->getStyle("$column$row")->applyFromArray( |
570
|
|
|
[ |
571
|
483 |
|
'fill' => [ |
572
|
1 |
|
'fillType' => Fill::FILL_SOLID, |
573
|
|
|
'color' => ['rgb' => $this->getStyleColor($attributeArray['bgcolor'])], |
574
|
|
|
], |
575
|
|
|
] |
576
|
483 |
|
); |
577
|
|
|
} |
578
|
483 |
|
} |
579
|
1 |
|
|
580
|
|
|
private function processDomElementWidth(Worksheet $sheet, string $column, array $attributeArray): void |
581
|
|
|
{ |
582
|
|
|
if (isset($attributeArray['width'])) { |
583
|
484 |
|
$sheet->getColumnDimension($column)->setWidth((new CssDimension($attributeArray['width']))->width()); |
584
|
|
|
} |
585
|
484 |
|
} |
586
|
3 |
|
|
587
|
|
|
private function processDomElementHeight(Worksheet $sheet, int $row, array $attributeArray): void |
588
|
484 |
|
{ |
589
|
|
|
if (isset($attributeArray['height'])) { |
590
|
|
|
$sheet->getRowDimension($row)->setRowHeight((new CssDimension($attributeArray['height']))->height()); |
591
|
483 |
|
} |
592
|
|
|
} |
593
|
483 |
|
|
594
|
|
|
private function processDomElementAlign(Worksheet $sheet, int $row, string $column, array $attributeArray): void |
595
|
483 |
|
{ |
596
|
483 |
|
if (isset($attributeArray['align'])) { |
597
|
483 |
|
$sheet->getStyle($column . $row)->getAlignment()->setHorizontal($attributeArray['align']); |
598
|
483 |
|
} |
599
|
483 |
|
} |
600
|
483 |
|
|
601
|
|
|
private function processDomElementVAlign(Worksheet $sheet, int $row, string $column, array $attributeArray): void |
602
|
483 |
|
{ |
603
|
|
|
if (isset($attributeArray['valign'])) { |
604
|
2 |
|
$sheet->getStyle($column . $row)->getAlignment()->setVertical($attributeArray['valign']); |
605
|
2 |
|
} |
606
|
2 |
|
} |
607
|
|
|
|
608
|
2 |
|
private function processDomElementDataFormat(Worksheet $sheet, int $row, string $column, array $attributeArray): void |
609
|
2 |
|
{ |
610
|
2 |
|
if (isset($attributeArray['data-format'])) { |
611
|
|
|
$sheet->getStyle($column . $row)->getNumberFormat()->setFormatCode($attributeArray['data-format']); |
612
|
2 |
|
} |
613
|
2 |
|
} |
614
|
483 |
|
|
615
|
|
|
private function processDomElementThTd(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child, array &$attributeArray): void |
616
|
3 |
|
{ |
617
|
3 |
|
while (isset($this->rowspan[$column . $row])) { |
618
|
3 |
|
++$column; |
619
|
|
|
} |
620
|
3 |
|
$this->processDomElement($child, $sheet, $row, $column, $cellContent); |
621
|
483 |
|
|
622
|
|
|
// apply inline style |
623
|
3 |
|
$this->applyInlineStyle($sheet, $row, $column, $attributeArray); |
624
|
3 |
|
|
625
|
3 |
|
$this->flushCell($sheet, $column, $row, $cellContent, $attributeArray); |
626
|
|
|
|
627
|
3 |
|
$this->processDomElementBgcolor($sheet, $row, $column, $attributeArray); |
628
|
3 |
|
$this->processDomElementWidth($sheet, $column, $attributeArray); |
629
|
|
|
$this->processDomElementHeight($sheet, $row, $attributeArray); |
630
|
|
|
$this->processDomElementAlign($sheet, $row, $column, $attributeArray); |
631
|
483 |
|
$this->processDomElementVAlign($sheet, $row, $column, $attributeArray); |
632
|
|
|
$this->processDomElementDataFormat($sheet, $row, $column, $attributeArray); |
633
|
|
|
|
634
|
489 |
|
if (isset($attributeArray['rowspan'], $attributeArray['colspan'])) { |
635
|
|
|
//create merging rowspan and colspan |
636
|
489 |
|
$columnTo = $column; |
637
|
489 |
|
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) { |
638
|
486 |
|
++$columnTo; |
639
|
486 |
|
} |
640
|
12 |
|
$range = $column . $row . ':' . $columnTo . ($row + (int) $attributeArray['rowspan'] - 1); |
641
|
|
|
foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) { |
642
|
486 |
|
$this->rowspan[$value] = true; |
643
|
|
|
} |
644
|
486 |
|
$sheet->mergeCells($range); |
645
|
|
|
$column = $columnTo; |
646
|
|
|
} elseif (isset($attributeArray['rowspan'])) { |
647
|
|
|
//create merging rowspan |
648
|
489 |
|
$range = $column . $row . ':' . $column . ($row + (int) $attributeArray['rowspan'] - 1); |
649
|
489 |
|
foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) { |
650
|
|
|
$this->rowspan[$value] = true; |
651
|
|
|
} |
652
|
|
|
$sheet->mergeCells($range); |
653
|
|
|
} elseif (isset($attributeArray['colspan'])) { |
654
|
|
|
//create merging colspan |
655
|
|
|
$columnTo = $column; |
656
|
|
|
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) { |
657
|
474 |
|
++$columnTo; |
658
|
|
|
} |
659
|
|
|
$sheet->mergeCells($column . $row . ':' . $columnTo . $row); |
660
|
474 |
|
$column = $columnTo; |
661
|
1 |
|
} |
662
|
|
|
|
663
|
|
|
++$column; |
664
|
|
|
} |
665
|
473 |
|
|
666
|
|
|
protected function processDomElement(DOMNode $element, Worksheet $sheet, int &$row, string &$column, string &$cellContent): void |
667
|
|
|
{ |
668
|
|
|
foreach ($element->childNodes as $child) { |
669
|
473 |
|
if ($child instanceof DOMText) { |
670
|
472 |
|
$domText = (string) preg_replace('/\s+/', ' ', trim($child->nodeValue ?? '')); |
671
|
472 |
|
if ($domText === "\u{a0}") { |
672
|
2 |
|
$domText = ''; |
673
|
2 |
|
} |
674
|
|
|
if (is_string($cellContent)) { |
675
|
473 |
|
// simply append the text if the cell content is a plain text string |
676
|
2 |
|
$cellContent .= $domText; |
677
|
|
|
} |
678
|
471 |
|
// but if we have a rich text run instead, we need to append it correctly |
679
|
|
|
// TODO |
680
|
471 |
|
} elseif ($child instanceof DOMElement) { |
681
|
|
|
$this->processDomElementBody($sheet, $row, $column, $cellContent, $child); |
682
|
|
|
} |
683
|
489 |
|
} |
684
|
|
|
} |
685
|
489 |
|
|
686
|
489 |
|
/** |
687
|
448 |
|
* Loads PhpSpreadsheet from file into PhpSpreadsheet instance. |
688
|
448 |
|
*/ |
689
|
440 |
|
public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Spreadsheet |
690
|
|
|
{ |
691
|
440 |
|
// Validate |
692
|
438 |
|
if (!$this->canRead($filename)) { |
693
|
|
|
throw new Exception($filename . ' is an Invalid HTML file.'); |
694
|
438 |
|
} |
695
|
440 |
|
|
696
|
1 |
|
// Create a new DOM object |
697
|
|
|
$dom = new DOMDocument(); |
698
|
1 |
|
|
699
|
440 |
|
// Reload the HTML file into the DOM object |
700
|
1 |
|
try { |
701
|
|
|
$convert = $this->getSecurityScannerOrThrow()->scanFile($filename); |
702
|
1 |
|
$convert = self::replaceNonAsciiIfNeeded($convert); |
703
|
440 |
|
$loaded = ($convert === null) ? false : $dom->loadHTML($convert); |
704
|
438 |
|
} catch (Throwable $e) { |
705
|
|
|
$loaded = false; |
706
|
438 |
|
} |
707
|
440 |
|
if ($loaded === false) { |
708
|
1 |
|
throw new Exception('Failed to load ' . $filename . ' as a DOM Document', 0, $e ?? null); |
709
|
|
|
} |
710
|
1 |
|
self::loadProperties($dom, $spreadsheet); |
711
|
440 |
|
|
712
|
1 |
|
return $this->loadDocument($dom, $spreadsheet); |
713
|
|
|
} |
714
|
1 |
|
|
715
|
440 |
|
private static function loadProperties(DOMDocument $dom, Spreadsheet $spreadsheet): void |
716
|
438 |
|
{ |
717
|
|
|
$properties = $spreadsheet->getProperties(); |
718
|
438 |
|
foreach ($dom->getElementsByTagName('meta') as $meta) { |
719
|
440 |
|
$metaContent = (string) $meta->getAttribute('content'); |
720
|
1 |
|
if ($metaContent !== '') { |
721
|
|
|
$metaName = (string) $meta->getAttribute('name'); |
722
|
1 |
|
switch ($metaName) { |
723
|
440 |
|
case 'author': |
724
|
438 |
|
$properties->setCreator($metaContent); |
725
|
|
|
|
726
|
438 |
|
break; |
727
|
440 |
|
case 'category': |
728
|
1 |
|
$properties->setCategory($metaContent); |
729
|
|
|
|
730
|
1 |
|
break; |
731
|
440 |
|
case 'company': |
732
|
436 |
|
$properties->setCompany($metaContent); |
733
|
|
|
|
734
|
436 |
|
break; |
735
|
440 |
|
case 'created': |
736
|
1 |
|
$properties->setCreated($metaContent); |
737
|
|
|
|
738
|
1 |
|
break; |
739
|
|
|
case 'description': |
740
|
440 |
|
$properties->setDescription($metaContent); |
741
|
1 |
|
|
742
|
1 |
|
break; |
743
|
1 |
|
case 'keywords': |
744
|
1 |
|
$properties->setKeywords($metaContent); |
745
|
1 |
|
|
746
|
|
|
break; |
747
|
1 |
|
case 'lastModifiedBy': |
748
|
1 |
|
$properties->setLastModifiedBy($metaContent); |
749
|
|
|
|
750
|
|
|
break; |
751
|
|
|
case 'manager': |
752
|
|
|
$properties->setManager($metaContent); |
753
|
489 |
|
|
754
|
1 |
|
break; |
755
|
|
|
case 'modified': |
756
|
|
|
$properties->setModified($metaContent); |
757
|
|
|
|
758
|
12 |
|
break; |
759
|
|
|
case 'subject': |
760
|
12 |
|
$properties->setSubject($metaContent); |
761
|
|
|
|
762
|
|
|
break; |
763
|
490 |
|
case 'title': |
764
|
|
|
$properties->setTitle($metaContent); |
765
|
490 |
|
|
766
|
41 |
|
break; |
767
|
41 |
|
case 'viewport': |
768
|
41 |
|
$properties->setViewport($metaContent); |
769
|
|
|
|
770
|
41 |
|
break; |
771
|
41 |
|
default: |
772
|
|
|
if (preg_match('/^custom[.](bool|date|float|int|string)[.](.+)$/', $metaName, $matches) === 1) { |
773
|
|
|
match ($matches[1]) { |
774
|
490 |
|
'bool' => $properties->setCustomProperty($matches[2], (bool) $metaContent, Properties::PROPERTY_TYPE_BOOLEAN), |
775
|
|
|
'float' => $properties->setCustomProperty($matches[2], (float) $metaContent, Properties::PROPERTY_TYPE_FLOAT), |
776
|
|
|
'int' => $properties->setCustomProperty($matches[2], (int) $metaContent, Properties::PROPERTY_TYPE_INTEGER), |
777
|
|
|
'date' => $properties->setCustomProperty($matches[2], $metaContent, Properties::PROPERTY_TYPE_DATE), |
778
|
|
|
// string |
779
|
|
|
default => $properties->setCustomProperty($matches[2], $metaContent, Properties::PROPERTY_TYPE_STRING), |
780
|
18 |
|
}; |
781
|
|
|
} |
782
|
|
|
} |
783
|
18 |
|
} |
784
|
|
|
} |
785
|
|
|
if (!empty($dom->baseURI)) { |
786
|
|
|
$properties->setHyperlinkBase($dom->baseURI); |
787
|
18 |
|
} |
788
|
18 |
|
} |
789
|
18 |
|
|
790
|
|
|
private static function replaceNonAscii(array $matches): string |
791
|
|
|
{ |
792
|
|
|
return '&#' . mb_ord($matches[0], 'UTF-8') . ';'; |
793
|
18 |
|
} |
794
|
|
|
|
795
|
|
|
private static function replaceNonAsciiIfNeeded(string $convert): ?string |
796
|
18 |
|
{ |
797
|
18 |
|
if (preg_match(self::STARTS_WITH_BOM, $convert) !== 1 && preg_match(self::DECLARES_CHARSET, $convert) !== 1) { |
798
|
18 |
|
$lowend = "\u{80}"; |
799
|
|
|
$highend = "\u{10ffff}"; |
800
|
18 |
|
$regexp = "/[$lowend-$highend]/u"; |
801
|
|
|
/** @var callable $callback */ |
802
|
|
|
$callback = [self::class, 'replaceNonAscii']; |
803
|
|
|
$convert = preg_replace_callback($regexp, $callback, $convert); |
804
|
|
|
} |
805
|
|
|
|
806
|
489 |
|
return $convert; |
807
|
|
|
} |
808
|
489 |
|
|
809
|
2 |
|
/** |
810
|
|
|
* Spreadsheet from content. |
811
|
489 |
|
*/ |
812
|
|
|
public function loadFromString(string $content, ?Spreadsheet $spreadsheet = null): Spreadsheet |
813
|
|
|
{ |
814
|
489 |
|
// Create a new DOM object |
815
|
|
|
$dom = new DOMDocument(); |
816
|
489 |
|
|
817
|
489 |
|
// Reload the HTML file into the DOM object |
818
|
489 |
|
try { |
819
|
489 |
|
$convert = $this->getSecurityScannerOrThrow()->scan($content); |
820
|
489 |
|
$convert = self::replaceNonAsciiIfNeeded($convert); |
821
|
|
|
$loaded = ($convert === null) ? false : $dom->loadHTML($convert); |
822
|
|
|
} catch (Throwable $e) { |
823
|
488 |
|
$loaded = false; |
824
|
|
|
} |
825
|
|
|
if ($loaded === false) { |
826
|
|
|
throw new Exception('Failed to load content as a DOM Document', 0, $e ?? null); |
827
|
|
|
} |
828
|
|
|
$spreadsheet = $spreadsheet ?? new Spreadsheet(); |
829
|
1 |
|
$spreadsheet->setValueBinder($this->valueBinder); |
830
|
|
|
self::loadProperties($dom, $spreadsheet); |
831
|
1 |
|
|
832
|
|
|
return $this->loadDocument($dom, $spreadsheet); |
833
|
|
|
} |
834
|
|
|
|
835
|
|
|
/** |
836
|
|
|
* Loads PhpSpreadsheet from DOMDocument into PhpSpreadsheet instance. |
837
|
|
|
*/ |
838
|
|
|
private function loadDocument(DOMDocument $document, Spreadsheet $spreadsheet): Spreadsheet |
839
|
|
|
{ |
840
|
|
|
while ($spreadsheet->getSheetCount() <= $this->sheetIndex) { |
841
|
2 |
|
$spreadsheet->createSheet(); |
842
|
|
|
} |
843
|
2 |
|
$spreadsheet->setActiveSheetIndex($this->sheetIndex); |
844
|
|
|
|
845
|
2 |
|
// Discard white space |
846
|
|
|
$document->preserveWhiteSpace = false; |
847
|
|
|
|
848
|
|
|
$row = 0; |
849
|
|
|
$column = 'A'; |
850
|
|
|
$content = ''; |
851
|
|
|
$this->rowspan = []; |
852
|
|
|
$this->processDomElement($document, $spreadsheet->getActiveSheet(), $row, $column, $content); |
853
|
|
|
|
854
|
|
|
// Return |
855
|
|
|
return $spreadsheet; |
856
|
|
|
} |
857
|
|
|
|
858
|
483 |
|
/** |
859
|
|
|
* Get sheet index. |
860
|
483 |
|
*/ |
861
|
477 |
|
public function getSheetIndex(): int |
862
|
|
|
{ |
863
|
|
|
return $this->sheetIndex; |
864
|
16 |
|
} |
865
|
1 |
|
|
866
|
16 |
|
/** |
867
|
1 |
|
* Set sheet index. |
868
|
1 |
|
* |
869
|
1 |
|
* @param int $sheetIndex Sheet index |
870
|
|
|
* |
871
|
1 |
|
* @return $this |
872
|
1 |
|
*/ |
873
|
16 |
|
public function setSheetIndex(int $sheetIndex): static |
874
|
1 |
|
{ |
875
|
1 |
|
$this->sheetIndex = $sheetIndex; |
876
|
16 |
|
|
877
|
1 |
|
return $this; |
878
|
1 |
|
} |
879
|
1 |
|
|
880
|
|
|
/** |
881
|
1 |
|
* Apply inline css inline style. |
882
|
1 |
|
* |
883
|
|
|
* NOTES : |
884
|
16 |
|
* Currently only intended for td & th element, |
885
|
|
|
* and only takes 'background-color' and 'color'; property with HEX color |
886
|
|
|
* |
887
|
|
|
* TODO : |
888
|
16 |
|
* - Implement to other propertie, such as border |
889
|
16 |
|
*/ |
890
|
16 |
|
private function applyInlineStyle(Worksheet &$sheet, int $row, string $column, array $attributeArray): void |
891
|
16 |
|
{ |
892
|
16 |
|
if (!isset($attributeArray['style'])) { |
893
|
16 |
|
return; |
894
|
|
|
} |
895
|
16 |
|
|
896
|
12 |
|
if ($row <= 0 || $column === '') { |
897
|
|
|
$cellStyle = new Style(); |
898
|
|
|
} elseif (isset($attributeArray['rowspan'], $attributeArray['colspan'])) { |
899
|
|
|
$columnTo = $column; |
900
|
16 |
|
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) { |
901
|
16 |
|
++$columnTo; |
902
|
3 |
|
} |
903
|
|
|
$range = $column . $row . ':' . $columnTo . ($row + (int) $attributeArray['rowspan'] - 1); |
904
|
3 |
|
$cellStyle = $sheet->getStyle($range); |
905
|
1 |
|
} elseif (isset($attributeArray['rowspan'])) { |
906
|
|
|
$range = $column . $row . ':' . $column . ($row + (int) $attributeArray['rowspan'] - 1); |
907
|
|
|
$cellStyle = $sheet->getStyle($range); |
908
|
3 |
|
} elseif (isset($attributeArray['colspan'])) { |
909
|
|
|
$columnTo = $column; |
910
|
3 |
|
for ($i = 0; $i < (int) $attributeArray['colspan'] - 1; ++$i) { |
911
|
16 |
|
++$columnTo; |
912
|
3 |
|
} |
913
|
|
|
$range = $column . $row . ':' . $columnTo . $row; |
914
|
3 |
|
$cellStyle = $sheet->getStyle($range); |
915
|
1 |
|
} else { |
916
|
|
|
$cellStyle = $sheet->getStyle($column . $row); |
917
|
|
|
} |
918
|
3 |
|
|
919
|
|
|
// add color styles (background & text) from dom element,currently support : td & th, using ONLY inline css style with RGB color |
920
|
3 |
|
$styles = explode(';', $attributeArray['style']); |
921
|
|
|
foreach ($styles as $st) { |
922
|
13 |
|
$value = explode(':', $st); |
923
|
3 |
|
$styleName = isset($value[0]) ? trim($value[0]) : null; |
924
|
|
|
$styleValue = isset($value[1]) ? trim($value[1]) : null; |
925
|
3 |
|
$styleValueString = (string) $styleValue; |
926
|
|
|
|
927
|
11 |
|
if (!$styleName) { |
928
|
1 |
|
continue; |
929
|
|
|
} |
930
|
1 |
|
|
931
|
|
|
switch ($styleName) { |
932
|
11 |
|
case 'background': |
933
|
1 |
|
case 'background-color': |
934
|
|
|
$styleColor = $this->getStyleColor($styleValueString); |
935
|
1 |
|
|
936
|
|
|
if (!$styleColor) { |
937
|
11 |
|
continue 2; |
938
|
1 |
|
} |
939
|
|
|
|
940
|
1 |
|
$cellStyle->applyFromArray(['fill' => ['fillType' => Fill::FILL_SOLID, 'color' => ['rgb' => $styleColor]]]); |
941
|
|
|
|
942
|
11 |
|
break; |
943
|
1 |
|
case 'color': |
944
|
|
|
$styleColor = $this->getStyleColor($styleValueString); |
945
|
1 |
|
|
946
|
|
|
if (!$styleColor) { |
947
|
10 |
|
continue 2; |
948
|
1 |
|
} |
949
|
1 |
|
|
950
|
1 |
|
$cellStyle->applyFromArray(['font' => ['color' => ['rgb' => $styleColor]]]); |
951
|
|
|
|
952
|
1 |
|
break; |
953
|
|
|
|
954
|
10 |
|
case 'border': |
955
|
1 |
|
$this->setBorderStyle($cellStyle, $styleValueString, 'allBorders'); |
956
|
1 |
|
|
957
|
|
|
break; |
958
|
|
|
|
959
|
1 |
|
case 'border-top': |
960
|
|
|
$this->setBorderStyle($cellStyle, $styleValueString, 'top'); |
961
|
10 |
|
|
962
|
1 |
|
break; |
963
|
1 |
|
|
964
|
|
|
case 'border-bottom': |
965
|
|
|
$this->setBorderStyle($cellStyle, $styleValueString, 'bottom'); |
966
|
1 |
|
|
967
|
|
|
break; |
968
|
10 |
|
|
969
|
1 |
|
case 'border-left': |
970
|
|
|
$this->setBorderStyle($cellStyle, $styleValueString, 'left'); |
971
|
1 |
|
|
972
|
|
|
break; |
973
|
10 |
|
|
974
|
|
|
case 'border-right': |
975
|
1 |
|
$this->setBorderStyle($cellStyle, $styleValueString, 'right'); |
976
|
1 |
|
|
977
|
|
|
break; |
978
|
1 |
|
|
979
|
1 |
|
case 'font-size': |
980
|
1 |
|
$cellStyle->getFont()->setSize( |
981
|
|
|
(float) $styleValue |
982
|
1 |
|
); |
983
|
|
|
|
984
|
|
|
break; |
985
|
1 |
|
|
986
|
|
|
case 'font-weight': |
987
|
9 |
|
if ($styleValue === 'bold' || $styleValue >= 500) { |
988
|
1 |
|
$cellStyle->getFont()->setBold(true); |
989
|
|
|
} |
990
|
1 |
|
|
991
|
|
|
break; |
992
|
9 |
|
|
993
|
2 |
|
case 'font-style': |
994
|
|
|
if ($styleValue === 'italic') { |
995
|
2 |
|
$cellStyle->getFont()->setItalic(true); |
996
|
|
|
} |
997
|
9 |
|
|
998
|
2 |
|
break; |
999
|
2 |
|
|
1000
|
2 |
|
case 'font-family': |
1001
|
2 |
|
$cellStyle->getFont()->setName(str_replace('\'', '', $styleValueString)); |
1002
|
|
|
|
1003
|
|
|
break; |
1004
|
2 |
|
|
1005
|
|
|
case 'text-decoration': |
1006
|
7 |
|
switch ($styleValue) { |
1007
|
1 |
|
case 'underline': |
1008
|
1 |
|
$cellStyle->getFont()->setUnderline(Font::UNDERLINE_SINGLE); |
1009
|
1 |
|
|
1010
|
1 |
|
break; |
1011
|
|
|
case 'line-through': |
1012
|
|
|
$cellStyle->getFont()->setStrikethrough(true); |
1013
|
1 |
|
|
1014
|
|
|
break; |
1015
|
6 |
|
} |
1016
|
1 |
|
|
1017
|
1 |
|
break; |
1018
|
1 |
|
|
1019
|
|
|
case 'text-align': |
1020
|
1 |
|
$cellStyle->getAlignment()->setHorizontal($styleValueString); |
1021
|
|
|
|
1022
|
6 |
|
break; |
1023
|
2 |
|
|
1024
|
2 |
|
case 'vertical-align': |
1025
|
2 |
|
$cellStyle->getAlignment()->setVertical($styleValueString); |
1026
|
|
|
|
1027
|
2 |
|
break; |
1028
|
|
|
|
1029
|
|
|
case 'width': |
1030
|
|
|
if ($column !== '') { |
1031
|
|
|
$sheet->getColumnDimension($column)->setWidth( |
1032
|
|
|
(new CssDimension($styleValue ?? ''))->width() |
1033
|
|
|
); |
1034
|
|
|
} |
1035
|
7 |
|
|
1036
|
|
|
break; |
1037
|
7 |
|
|
1038
|
7 |
|
case 'height': |
1039
|
5 |
|
if ($row > 0) { |
1040
|
|
|
$sheet->getRowDimension($row)->setRowHeight( |
1041
|
|
|
(new CssDimension($styleValue ?? ''))->height() |
1042
|
4 |
|
); |
1043
|
|
|
} |
1044
|
|
|
|
1045
|
11 |
|
break; |
1046
|
|
|
|
1047
|
11 |
|
case 'word-wrap': |
1048
|
1 |
|
$cellStyle->getAlignment()->setWrapText( |
1049
|
|
|
$styleValue === 'break-word' |
1050
|
10 |
|
); |
1051
|
|
|
|
1052
|
10 |
|
break; |
1053
|
10 |
|
|
1054
|
6 |
|
case 'text-indent': |
1055
|
|
|
$cellStyle->getAlignment()->setIndent( |
1056
|
10 |
|
(int) str_replace(['px'], '', $styleValueString) |
1057
|
10 |
|
); |
1058
|
10 |
|
|
1059
|
|
|
break; |
1060
|
10 |
|
} |
1061
|
10 |
|
} |
1062
|
9 |
|
} |
1063
|
1 |
|
|
1064
|
|
|
/** |
1065
|
8 |
|
* Check if has #, so we can get clean hex. |
1066
|
8 |
|
*/ |
1067
|
8 |
|
public function getStyleColor(?string $value): string |
1068
|
8 |
|
{ |
1069
|
8 |
|
$value = (string) $value; |
1070
|
|
|
if (str_starts_with($value, '#')) { |
1071
|
8 |
|
return substr($value, 1); |
1072
|
7 |
|
} |
1073
|
|
|
|
1074
|
|
|
return HelperHtml::colourNameLookup($value); |
1075
|
8 |
|
} |
1076
|
5 |
|
|
1077
|
2 |
|
private function insertImage(Worksheet $sheet, string $column, int $row, array $attributes): void |
1078
|
|
|
{ |
1079
|
3 |
|
if (!isset($attributes['src'])) { |
1080
|
|
|
return; |
1081
|
3 |
|
} |
1082
|
1 |
|
$styleArray = self::getStyleArray($attributes); |
1083
|
|
|
|
1084
|
|
|
$src = $attributes['src']; |
1085
|
8 |
|
if (substr($src, 0, 5) !== 'data:') { |
1086
|
8 |
|
$src = urldecode($src); |
1087
|
8 |
|
} |
1088
|
|
|
$width = isset($attributes['width']) ? (float) $attributes['width'] : ($styleArray['width'] ?? null); |
1089
|
8 |
|
$height = isset($attributes['height']) ? (float) $attributes['height'] : ($styleArray['height'] ?? null); |
1090
|
8 |
|
$name = $attributes['alt'] ?? null; |
1091
|
8 |
|
|
1092
|
|
|
$drawing = new Drawing(); |
1093
|
8 |
|
$drawing->setPath($src, false); |
1094
|
|
|
if ($drawing->getPath() === '') { |
1095
|
|
|
return; |
1096
|
|
|
} |
1097
|
|
|
$drawing->setWorksheet($sheet); |
1098
|
|
|
$drawing->setCoordinates($column . $row); |
1099
|
|
|
$drawing->setOffsetX(0); |
1100
|
|
|
$drawing->setOffsetY(10); |
1101
|
10 |
|
$drawing->setResizeProportional(true); |
1102
|
|
|
|
1103
|
10 |
|
if ($name) { |
1104
|
10 |
|
$drawing->setName($name); |
1105
|
4 |
|
} |
1106
|
4 |
|
|
1107
|
4 |
|
if ($width) { |
1108
|
4 |
|
if ($height) { |
1109
|
4 |
|
$drawing->setWidthAndHeight((int) $width, (int) $height); |
1110
|
4 |
|
} else { |
1111
|
4 |
|
$drawing->setWidth((int) $width); |
1112
|
4 |
|
} |
1113
|
4 |
|
} elseif ($height) { |
1114
|
|
|
$drawing->setHeight((int) $height); |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
4 |
|
$sheet->getColumnDimension($column)->setWidth( |
1118
|
2 |
|
$drawing->getWidth() / 6 |
1119
|
2 |
|
); |
1120
|
|
|
|
1121
|
|
|
$sheet->getRowDimension($row)->setRowHeight( |
1122
|
|
|
$drawing->getHeight() * 0.9 |
1123
|
|
|
); |
1124
|
4 |
|
|
1125
|
|
|
if (isset($styleArray['opacity'])) { |
1126
|
|
|
$opacity = $styleArray['opacity']; |
1127
|
|
|
if (is_numeric($opacity)) { |
1128
|
|
|
$drawing->setOpacity((int) ($opacity * 100000)); |
1129
|
10 |
|
} |
1130
|
|
|
} |
1131
|
|
|
} |
1132
|
|
|
|
1133
|
|
|
private static function getStyleArray(array $attributes): array |
1134
|
|
|
{ |
1135
|
|
|
$styleArray = []; |
1136
|
|
|
if (isset($attributes['style'])) { |
1137
|
|
|
$styles = explode(';', $attributes['style']); |
1138
|
|
|
foreach ($styles as $style) { |
1139
|
|
|
$value = explode(':', $style); |
1140
|
|
|
if (count($value) === 2) { |
1141
|
|
|
$arrayKey = trim($value[0]); |
1142
|
|
|
$arrayValue = trim($value[1]); |
1143
|
|
|
if ($arrayKey === 'width') { |
1144
|
|
|
if (substr($arrayValue, -2) === 'px') { |
1145
|
|
|
$arrayValue = (string) (((float) substr($arrayValue, 0, -2))); |
1146
|
|
|
} else { |
1147
|
|
|
$arrayValue = (new CssDimension($arrayValue))->width(); |
1148
|
|
|
} |
1149
|
15 |
|
} elseif ($arrayKey === 'height') { |
1150
|
|
|
if (substr($arrayValue, -2) === 'px') { |
1151
|
15 |
|
$arrayValue = substr($arrayValue, 0, -2); |
1152
|
|
|
} else { |
1153
|
|
|
$arrayValue = (new CssDimension($arrayValue))->height(); |
1154
|
|
|
} |
1155
|
|
|
} |
1156
|
|
|
$styleArray[$arrayKey] = $arrayValue; |
1157
|
3 |
|
} |
1158
|
|
|
} |
1159
|
3 |
|
} |
1160
|
|
|
|
1161
|
|
|
return $styleArray; |
1162
|
3 |
|
} |
1163
|
|
|
|
1164
|
3 |
|
private const BORDER_MAPPINGS = [ |
1165
|
1 |
|
'dash-dot' => Border::BORDER_DASHDOT, |
1166
|
1 |
|
'dash-dot-dot' => Border::BORDER_DASHDOTDOT, |
1167
|
|
|
'dashed' => Border::BORDER_DASHED, |
1168
|
3 |
|
'dotted' => Border::BORDER_DOTTED, |
1169
|
3 |
|
'double' => Border::BORDER_DOUBLE, |
1170
|
3 |
|
'hair' => Border::BORDER_HAIR, |
1171
|
3 |
|
'medium' => Border::BORDER_MEDIUM, |
1172
|
3 |
|
'medium-dashed' => Border::BORDER_MEDIUMDASHED, |
1173
|
|
|
'medium-dash-dot' => Border::BORDER_MEDIUMDASHDOT, |
1174
|
1 |
|
'medium-dash-dot-dot' => Border::BORDER_MEDIUMDASHDOTDOT, |
1175
|
1 |
|
'none' => Border::BORDER_NONE, |
1176
|
|
|
'slant-dash-dot' => Border::BORDER_SLANTDASHDOT, |
1177
|
|
|
'solid' => Border::BORDER_THIN, |
1178
|
|
|
'thick' => Border::BORDER_THICK, |
1179
|
3 |
|
]; |
1180
|
3 |
|
|
1181
|
3 |
|
public static function getBorderMappings(): array |
1182
|
3 |
|
{ |
1183
|
3 |
|
return self::BORDER_MAPPINGS; |
1184
|
3 |
|
} |
1185
|
3 |
|
|
1186
|
3 |
|
/** |
1187
|
|
|
* Map html border style to PhpSpreadsheet border style. |
1188
|
|
|
*/ |
1189
|
|
|
public function getBorderStyle(string $style): ?string |
1190
|
|
|
{ |
1191
|
|
|
return self::BORDER_MAPPINGS[$style] ?? null; |
1192
|
1 |
|
} |
1193
|
|
|
|
1194
|
1 |
|
private function setBorderStyle(Style $cellStyle, string $styleValue, string $type): void |
1195
|
1 |
|
{ |
1196
|
1 |
|
if (trim($styleValue) === Border::BORDER_NONE) { |
1197
|
1 |
|
$borderStyle = Border::BORDER_NONE; |
1198
|
1 |
|
$color = null; |
1199
|
1 |
|
} else { |
1200
|
1 |
|
$borderArray = explode(' ', $styleValue); |
1201
|
1 |
|
$borderCount = count($borderArray); |
1202
|
1 |
|
if ($borderCount >= 3) { |
1203
|
1 |
|
$borderStyle = $borderArray[1]; |
1204
|
|
|
$color = $borderArray[2]; |
1205
|
1 |
|
} else { |
1206
|
|
|
$borderStyle = $borderArray[0]; |
1207
|
1 |
|
$color = $borderArray[1] ?? null; |
1208
|
|
|
} |
1209
|
|
|
} |
1210
|
|
|
|
1211
|
|
|
$cellStyle->applyFromArray([ |
1212
|
|
|
'borders' => [ |
1213
|
|
|
$type => [ |
1214
|
|
|
'borderStyle' => $this->getBorderStyle($borderStyle), |
1215
|
|
|
'color' => ['rgb' => $this->getStyleColor($color)], |
1216
|
|
|
], |
1217
|
|
|
], |
1218
|
|
|
]); |
1219
|
|
|
} |
1220
|
|
|
|
1221
|
|
|
/** |
1222
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
1223
|
|
|
*/ |
1224
|
|
|
public function listWorksheetInfo(string $filename): array |
1225
|
|
|
{ |
1226
|
|
|
$info = []; |
1227
|
|
|
$spreadsheet = new Spreadsheet(); |
1228
|
|
|
$this->loadIntoExisting($filename, $spreadsheet); |
1229
|
|
|
foreach ($spreadsheet->getAllSheets() as $sheet) { |
1230
|
|
|
$newEntry = ['worksheetName' => $sheet->getTitle()]; |
1231
|
|
|
$newEntry['lastColumnLetter'] = $sheet->getHighestDataColumn(); |
1232
|
|
|
$newEntry['lastColumnIndex'] = Coordinate::columnIndexFromString($sheet->getHighestDataColumn()) - 1; |
1233
|
|
|
$newEntry['totalRows'] = $sheet->getHighestDataRow(); |
1234
|
|
|
$newEntry['totalColumns'] = $newEntry['lastColumnIndex'] + 1; |
1235
|
|
|
$info[] = $newEntry; |
1236
|
|
|
} |
1237
|
|
|
$spreadsheet->disconnectWorksheets(); |
1238
|
|
|
|
1239
|
|
|
return $info; |
1240
|
|
|
} |
1241
|
|
|
} |
1242
|
|
|
|