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