|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MyWheels\TwigSpreadsheetBundle\Wrapper; |
|
4
|
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
|
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\ColumnDimension; |
|
8
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\RowDimension; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class SheetWrapper. |
|
12
|
|
|
*/ |
|
13
|
|
|
class SheetWrapper extends BaseWrapper |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var int |
|
17
|
|
|
*/ |
|
18
|
|
|
const COLUMN_DEFAULT = 1; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
const ROW_DEFAULT = 1; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var DocumentWrapper |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $documentWrapper; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Worksheet|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $object; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var null|int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $row; |
|
37
|
|
|
/** |
|
38
|
|
|
* @var null|int |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $column; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* SheetWrapper constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $context |
|
46
|
|
|
* @param \Twig_Environment $environment |
|
47
|
|
|
* @param DocumentWrapper $documentWrapper |
|
48
|
|
|
*/ |
|
49
|
108 |
|
public function __construct(array $context, \Twig_Environment $environment, DocumentWrapper $documentWrapper) |
|
50
|
|
|
{ |
|
51
|
108 |
|
parent::__construct($context, $environment); |
|
52
|
|
|
|
|
53
|
108 |
|
$this->documentWrapper = $documentWrapper; |
|
54
|
|
|
|
|
55
|
108 |
|
$this->object = null; |
|
56
|
108 |
|
$this->row = null; |
|
57
|
108 |
|
$this->column = null; |
|
58
|
108 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param int|string|null $index |
|
62
|
|
|
* @param array $properties |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
|
65
|
|
|
* @throws \RuntimeException |
|
66
|
|
|
* @throws \LogicException |
|
67
|
|
|
*/ |
|
68
|
102 |
|
public function start($index, array $properties = []) |
|
69
|
|
|
{ |
|
70
|
102 |
|
if ($this->documentWrapper->getObject() === null) { |
|
71
|
|
|
throw new \LogicException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
102 |
|
if (\is_int($index) && $index < $this->documentWrapper->getObject()->getSheetCount()) { |
|
75
|
|
|
$this->object = $this->documentWrapper->getObject()->setActiveSheetIndex($index); |
|
76
|
102 |
|
} elseif (\is_string($index)) { |
|
77
|
97 |
|
if (!$this->documentWrapper->getObject()->sheetNameExists($index)) { |
|
78
|
|
|
// create new sheet with a name |
|
79
|
97 |
|
$this->documentWrapper->getObject()->createSheet()->setTitle($index); |
|
80
|
|
|
} |
|
81
|
97 |
|
$this->object = $this->documentWrapper->getObject()->setActiveSheetIndexByName($index); |
|
82
|
|
|
} else { |
|
83
|
|
|
// create new sheet without a name |
|
84
|
5 |
|
$this->documentWrapper->getObject()->createSheet(); |
|
85
|
5 |
|
$this->object = $this->documentWrapper->getObject()->setActiveSheetIndex(0); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
102 |
|
$this->parameters['index'] = $index; |
|
89
|
102 |
|
$this->parameters['properties'] = $properties; |
|
90
|
|
|
|
|
91
|
102 |
|
$this->setProperties($properties); |
|
92
|
102 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws \Exception |
|
96
|
|
|
* @throws \LogicException |
|
97
|
|
|
*/ |
|
98
|
94 |
|
public function end() |
|
99
|
|
|
{ |
|
100
|
94 |
|
if ($this->object === null) { |
|
101
|
|
|
throw new \LogicException(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// auto-size columns |
|
105
|
|
|
if ( |
|
106
|
94 |
|
isset($this->parameters['properties']['columnDimension']) && |
|
107
|
94 |
|
\is_array($this->parameters['properties']['columnDimension']) |
|
108
|
|
|
) { |
|
109
|
|
|
/** |
|
110
|
|
|
* @var array $columnDimension |
|
111
|
|
|
*/ |
|
112
|
6 |
|
$columnDimension = $this->parameters['properties']['columnDimension']; |
|
113
|
6 |
|
foreach ($columnDimension as $key => $value) { |
|
114
|
6 |
|
if (isset($value['autoSize'])) { |
|
115
|
6 |
|
if ($key === 'default') { |
|
116
|
|
|
try { |
|
117
|
6 |
|
$cellIterator = $this->object->getRowIterator()->current()->getCellIterator(); |
|
118
|
6 |
|
$cellIterator->setIterateOnlyExistingCells(true); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($cellIterator as $cell) { |
|
121
|
|
|
$this->object->getColumnDimension($cell->getColumn())->setAutoSize($value['autoSize']); |
|
122
|
|
|
} |
|
123
|
6 |
|
} catch (Exception $e) { |
|
124
|
|
|
// ignore exceptions thrown when no cells are defined |
|
125
|
|
|
} |
|
126
|
|
|
} else { |
|
127
|
6 |
|
$this->object->getColumnDimension($key)->setAutoSize($value['autoSize']); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
94 |
|
$this->parameters = []; |
|
134
|
94 |
|
$this->object = null; |
|
135
|
94 |
|
$this->row = null; |
|
136
|
94 |
|
$this->column = null; |
|
137
|
94 |
|
} |
|
138
|
|
|
|
|
139
|
76 |
|
public function increaseRow() |
|
140
|
|
|
{ |
|
141
|
76 |
|
$this->row = $this->row === null ? self::ROW_DEFAULT : $this->row + 1; |
|
142
|
76 |
|
} |
|
143
|
|
|
|
|
144
|
72 |
|
public function increaseColumn() |
|
145
|
|
|
{ |
|
146
|
72 |
|
$this->column = $this->column === null ? self::COLUMN_DEFAULT : $this->column + 1; |
|
147
|
72 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return Worksheet |
|
151
|
|
|
*/ |
|
152
|
86 |
|
public function getObject(): Worksheet |
|
153
|
|
|
{ |
|
154
|
86 |
|
return $this->object; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param Worksheet $object |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setObject(Worksheet $object) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->object = $object; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return int|null |
|
167
|
|
|
*/ |
|
168
|
72 |
|
public function getRow() |
|
169
|
|
|
{ |
|
170
|
72 |
|
return $this->row; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param int|null $row |
|
175
|
|
|
*/ |
|
176
|
3 |
|
public function setRow($row) |
|
177
|
|
|
{ |
|
178
|
3 |
|
$this->row = $row; |
|
179
|
3 |
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return int|null |
|
183
|
|
|
*/ |
|
184
|
72 |
|
public function getColumn() |
|
185
|
|
|
{ |
|
186
|
72 |
|
return $this->column; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param int|null $column |
|
191
|
|
|
*/ |
|
192
|
72 |
|
public function setColumn($column) |
|
193
|
|
|
{ |
|
194
|
72 |
|
$this->column = $column; |
|
195
|
72 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* {@inheritdoc} |
|
199
|
|
|
* |
|
200
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
|
201
|
|
|
*/ |
|
202
|
|
|
protected function configureMappings(): array |
|
203
|
|
|
{ |
|
204
|
|
|
return [ |
|
205
|
|
|
'autoFilter' => function ($value) { $this->object->setAutoFilter($value); }, |
|
206
|
|
|
'columnDimension' => [ |
|
207
|
108 |
|
'__multi' => function ($index = 'default'): ColumnDimension { |
|
208
|
6 |
|
return $index === 'default' ? |
|
209
|
6 |
|
$this->object->getDefaultColumnDimension() : |
|
210
|
6 |
|
$this->object->getColumnDimension($index); |
|
211
|
108 |
|
}, |
|
212
|
|
|
'autoSize' => function ($value, ColumnDimension $object) { $object->setAutoSize($value); }, |
|
213
|
|
|
'collapsed' => function ($value, ColumnDimension $object) { $object->setCollapsed($value); }, |
|
214
|
|
|
'columnIndex' => function ($value, ColumnDimension $object) { $object->setColumnIndex($value); }, |
|
215
|
|
|
'outlineLevel' => function ($value, ColumnDimension $object) { $object->setOutlineLevel($value); }, |
|
216
|
|
|
'visible' => function ($value, ColumnDimension $object) { $object->setVisible($value); }, |
|
217
|
|
|
'width' => function ($value, ColumnDimension $object) { $object->setWidth($value); }, |
|
218
|
|
|
'xfIndex' => function ($value, ColumnDimension $object) { $object->setXfIndex($value); }, |
|
219
|
|
|
], |
|
220
|
|
|
'pageMargins' => [ |
|
221
|
|
|
'top' => function ($value) { $this->object->getPageMargins()->setTop($value); }, |
|
222
|
|
|
'bottom' => function ($value) { $this->object->getPageMargins()->setBottom($value); }, |
|
223
|
|
|
'left' => function ($value) { $this->object->getPageMargins()->setLeft($value); }, |
|
224
|
|
|
'right' => function ($value) { $this->object->getPageMargins()->setRight($value); }, |
|
225
|
|
|
'header' => function ($value) { $this->object->getPageMargins()->setHeader($value); }, |
|
226
|
|
|
'footer' => function ($value) { $this->object->getPageMargins()->setFooter($value); }, |
|
227
|
|
|
], |
|
228
|
|
|
'pageSetup' => [ |
|
229
|
|
|
'fitToHeight' => function ($value) { $this->object->getPageSetup()->setFitToHeight($value); }, |
|
230
|
|
|
'fitToPage' => function ($value) { $this->object->getPageSetup()->setFitToPage($value); }, |
|
231
|
|
|
'fitToWidth' => function ($value) { $this->object->getPageSetup()->setFitToWidth($value); }, |
|
232
|
|
|
'horizontalCentered' => function ($value) { $this->object->getPageSetup()->setHorizontalCentered($value); }, |
|
233
|
|
|
'orientation' => function ($value) { $this->object->getPageSetup()->setOrientation($value); }, |
|
234
|
|
|
'paperSize' => function ($value) { $this->object->getPageSetup()->setPaperSize($value); }, |
|
235
|
|
|
'printArea' => function ($value) { $this->object->getPageSetup()->setPrintArea($value); }, |
|
236
|
|
|
'scale' => function ($value) { $this->object->getPageSetup()->setScale($value); }, |
|
237
|
|
|
'verticalCentered' => function ($value) { $this->object->getPageSetup()->setVerticalCentered($value); }, |
|
238
|
|
|
], |
|
239
|
|
|
'printGridlines' => function ($value) { $this->object->setPrintGridlines($value); }, |
|
240
|
|
|
'protection' => [ |
|
241
|
|
|
'autoFilter' => function ($value) { $this->object->getProtection()->setAutoFilter($value); }, |
|
242
|
|
|
'deleteColumns' => function ($value) { $this->object->getProtection()->setDeleteColumns($value); }, |
|
243
|
|
|
'deleteRows' => function ($value) { $this->object->getProtection()->setDeleteRows($value); }, |
|
244
|
|
|
'formatCells' => function ($value) { $this->object->getProtection()->setFormatCells($value); }, |
|
245
|
|
|
'formatColumns' => function ($value) { $this->object->getProtection()->setFormatColumns($value); }, |
|
246
|
|
|
'formatRows' => function ($value) { $this->object->getProtection()->setFormatRows($value); }, |
|
247
|
|
|
'insertColumns' => function ($value) { $this->object->getProtection()->setInsertColumns($value); }, |
|
248
|
|
|
'insertHyperlinks' => function ($value) { $this->object->getProtection()->setInsertHyperlinks($value); }, |
|
249
|
|
|
'insertRows' => function ($value) { $this->object->getProtection()->setInsertRows($value); }, |
|
250
|
|
|
'objects' => function ($value) { $this->object->getProtection()->setObjects($value); }, |
|
251
|
|
|
'password' => function ($value) { $this->object->getProtection()->setPassword($value); }, |
|
252
|
|
|
'pivotTables' => function ($value) { $this->object->getProtection()->setPivotTables($value); }, |
|
253
|
|
|
'scenarios' => function ($value) { $this->object->getProtection()->setScenarios($value); }, |
|
254
|
|
|
'selectLockedCells' => function ($value) { $this->object->getProtection()->setSelectLockedCells($value); }, |
|
255
|
|
|
'selectUnlockedCells' => function ($value) { $this->object->getProtection()->setSelectUnlockedCells($value); }, |
|
256
|
|
|
'sheet' => function ($value) { $this->object->getProtection()->setSheet($value); }, |
|
257
|
|
|
'sort' => function ($value) { $this->object->getProtection()->setSort($value); }, |
|
258
|
|
|
], |
|
259
|
|
|
'rightToLeft' => function ($value) { $this->object->setRightToLeft($value); }, |
|
260
|
|
|
'rowDimension' => [ |
|
261
|
108 |
|
'__multi' => function ($index = 'default'): RowDimension { |
|
262
|
6 |
|
return $index === 'default' ? |
|
263
|
6 |
|
$this->object->getDefaultRowDimension() : |
|
264
|
6 |
|
$this->object->getRowDimension($index); |
|
265
|
108 |
|
}, |
|
266
|
|
|
'collapsed' => function ($value, RowDimension $object) { $object->setCollapsed($value); }, |
|
267
|
|
|
'outlineLevel' => function ($value, RowDimension $object) { $object->setOutlineLevel($value); }, |
|
268
|
|
|
'rowHeight' => function ($value, RowDimension $object) { $object->setRowHeight($value); }, |
|
269
|
|
|
'rowIndex' => function ($value, RowDimension $object) { $object->setRowIndex($value); }, |
|
270
|
|
|
'visible' => function ($value, RowDimension $object) { $object->setVisible($value); }, |
|
271
|
|
|
'xfIndex' => function ($value, RowDimension $object) { $object->setXfIndex($value); }, |
|
272
|
|
|
'zeroHeight' => function ($value, RowDimension $object) { $object->setZeroHeight($value); }, |
|
273
|
|
|
], |
|
274
|
|
|
'sheetState' => function ($value) { $this->object->setSheetState($value); }, |
|
275
|
|
|
'showGridlines' => function ($value) { $this->object->setShowGridlines($value); }, |
|
276
|
|
|
'tabColor' => function ($value) { $this->object->getTabColor()->setRGB($value); }, |
|
277
|
|
|
'zoomScale' => function ($value) { $this->object->getSheetView()->setZoomScale($value); }, |
|
278
|
|
|
]; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|