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