1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Namespaces; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Border; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Borders; |
12
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Conditional; |
13
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill; |
14
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font; |
15
|
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat; |
16
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Protection; |
17
|
|
|
|
18
|
|
|
class Style extends WriterPart |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Write styles to XML format. |
22
|
|
|
* |
23
|
|
|
* @return string XML Output |
24
|
|
|
*/ |
25
|
375 |
|
public function writeStyles(Spreadsheet $spreadsheet): string |
26
|
|
|
{ |
27
|
|
|
// Create XML writer |
28
|
375 |
|
$objWriter = null; |
29
|
375 |
|
if ($this->getParentWriter()->getUseDiskCaching()) { |
30
|
|
|
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
31
|
|
|
} else { |
32
|
375 |
|
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// XML header |
36
|
375 |
|
$objWriter->startDocument('1.0', 'UTF-8', 'yes'); |
37
|
|
|
|
38
|
|
|
// styleSheet |
39
|
375 |
|
$objWriter->startElement('styleSheet'); |
40
|
375 |
|
$objWriter->writeAttribute('xml:space', 'preserve'); |
41
|
375 |
|
$objWriter->writeAttribute('xmlns', Namespaces::MAIN); |
42
|
|
|
|
43
|
|
|
// numFmts |
44
|
375 |
|
$objWriter->startElement('numFmts'); |
45
|
375 |
|
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getNumFmtHashTable()->count()); |
46
|
|
|
|
47
|
|
|
// numFmt |
48
|
375 |
|
for ($i = 0; $i < $this->getParentWriter()->getNumFmtHashTable()->count(); ++$i) { |
49
|
45 |
|
$this->writeNumFmt($objWriter, $this->getParentWriter()->getNumFmtHashTable()->getByIndex($i), $i); |
50
|
|
|
} |
51
|
|
|
|
52
|
375 |
|
$objWriter->endElement(); |
53
|
|
|
|
54
|
|
|
// fonts |
55
|
375 |
|
$objWriter->startElement('fonts'); |
56
|
375 |
|
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getFontHashTable()->count()); |
57
|
|
|
|
58
|
|
|
// font |
59
|
375 |
|
for ($i = 0; $i < $this->getParentWriter()->getFontHashTable()->count(); ++$i) { |
60
|
374 |
|
$thisfont = $this->getParentWriter()->getFontHashTable()->getByIndex($i); |
61
|
374 |
|
if ($thisfont !== null) { |
62
|
374 |
|
$this->writeFont($objWriter, $thisfont); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
375 |
|
$objWriter->endElement(); |
67
|
|
|
|
68
|
|
|
// fills |
69
|
375 |
|
$objWriter->startElement('fills'); |
70
|
375 |
|
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getFillHashTable()->count()); |
71
|
|
|
|
72
|
|
|
// fill |
73
|
375 |
|
for ($i = 0; $i < $this->getParentWriter()->getFillHashTable()->count(); ++$i) { |
74
|
374 |
|
$thisfill = $this->getParentWriter()->getFillHashTable()->getByIndex($i); |
75
|
374 |
|
if ($thisfill !== null) { |
76
|
374 |
|
$this->writeFill($objWriter, $thisfill); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
375 |
|
$objWriter->endElement(); |
81
|
|
|
|
82
|
|
|
// borders |
83
|
375 |
|
$objWriter->startElement('borders'); |
84
|
375 |
|
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getBordersHashTable()->count()); |
85
|
|
|
|
86
|
|
|
// border |
87
|
375 |
|
for ($i = 0; $i < $this->getParentWriter()->getBordersHashTable()->count(); ++$i) { |
88
|
374 |
|
$thisborder = $this->getParentWriter()->getBordersHashTable()->getByIndex($i); |
89
|
374 |
|
if ($thisborder !== null) { |
90
|
374 |
|
$this->writeBorder($objWriter, $thisborder); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
375 |
|
$objWriter->endElement(); |
95
|
|
|
|
96
|
|
|
// cellStyleXfs |
97
|
375 |
|
$objWriter->startElement('cellStyleXfs'); |
98
|
375 |
|
$objWriter->writeAttribute('count', '1'); |
99
|
|
|
|
100
|
|
|
// xf |
101
|
375 |
|
$objWriter->startElement('xf'); |
102
|
375 |
|
$objWriter->writeAttribute('numFmtId', '0'); |
103
|
375 |
|
$objWriter->writeAttribute('fontId', '0'); |
104
|
375 |
|
$objWriter->writeAttribute('fillId', '0'); |
105
|
375 |
|
$objWriter->writeAttribute('borderId', '0'); |
106
|
375 |
|
$objWriter->endElement(); |
107
|
|
|
|
108
|
375 |
|
$objWriter->endElement(); |
109
|
|
|
|
110
|
|
|
// cellXfs |
111
|
375 |
|
$objWriter->startElement('cellXfs'); |
112
|
375 |
|
$objWriter->writeAttribute('count', (string) count($spreadsheet->getCellXfCollection())); |
113
|
|
|
|
114
|
|
|
// xf |
115
|
375 |
|
$alignment = new Alignment(); |
116
|
375 |
|
$defaultAlignHash = $alignment->getHashCode(); |
117
|
375 |
|
if ($defaultAlignHash !== $spreadsheet->getDefaultStyle()->getAlignment()->getHashCode()) { |
118
|
10 |
|
$defaultAlignHash = ''; |
119
|
|
|
} |
120
|
375 |
|
foreach ($spreadsheet->getCellXfCollection() as $cellXf) { |
121
|
375 |
|
$this->writeCellStyleXf($objWriter, $cellXf, $spreadsheet, $defaultAlignHash); |
122
|
|
|
} |
123
|
|
|
|
124
|
375 |
|
$objWriter->endElement(); |
125
|
|
|
|
126
|
|
|
// cellStyles |
127
|
375 |
|
$objWriter->startElement('cellStyles'); |
128
|
375 |
|
$objWriter->writeAttribute('count', '1'); |
129
|
|
|
|
130
|
|
|
// cellStyle |
131
|
375 |
|
$objWriter->startElement('cellStyle'); |
132
|
375 |
|
$objWriter->writeAttribute('name', 'Normal'); |
133
|
375 |
|
$objWriter->writeAttribute('xfId', '0'); |
134
|
375 |
|
$objWriter->writeAttribute('builtinId', '0'); |
135
|
375 |
|
$objWriter->endElement(); |
136
|
|
|
|
137
|
375 |
|
$objWriter->endElement(); |
138
|
|
|
|
139
|
|
|
// dxfs |
140
|
375 |
|
$objWriter->startElement('dxfs'); |
141
|
375 |
|
$objWriter->writeAttribute('count', (string) $this->getParentWriter()->getStylesConditionalHashTable()->count()); |
142
|
|
|
|
143
|
|
|
// dxf |
144
|
375 |
|
for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) { |
145
|
|
|
/** @var ?Conditional */ |
146
|
33 |
|
$thisstyle = $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i); |
147
|
33 |
|
if ($thisstyle !== null) { |
148
|
33 |
|
$this->writeCellStyleDxf($objWriter, $thisstyle->getStyle()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
375 |
|
$objWriter->endElement(); |
153
|
|
|
|
154
|
|
|
// tableStyles |
155
|
375 |
|
$objWriter->startElement('tableStyles'); |
156
|
375 |
|
$objWriter->writeAttribute('defaultTableStyle', 'TableStyleMedium9'); |
157
|
375 |
|
$objWriter->writeAttribute('defaultPivotStyle', 'PivotTableStyle1'); |
158
|
375 |
|
$objWriter->endElement(); |
159
|
|
|
|
160
|
375 |
|
$objWriter->endElement(); |
161
|
|
|
|
162
|
|
|
// Return |
163
|
375 |
|
return $objWriter->getData(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Write Fill. |
168
|
|
|
*/ |
169
|
375 |
|
private function writeFill(XMLWriter $objWriter, Fill $fill): void |
170
|
|
|
{ |
171
|
|
|
// Check if this is a pattern type or gradient type |
172
|
|
|
if ( |
173
|
375 |
|
$fill->getFillType() === Fill::FILL_GRADIENT_LINEAR |
174
|
375 |
|
|| $fill->getFillType() === Fill::FILL_GRADIENT_PATH |
175
|
|
|
) { |
176
|
|
|
// Gradient fill |
177
|
6 |
|
$this->writeGradientFill($objWriter, $fill); |
178
|
375 |
|
} elseif ($fill->getFillType() !== null) { |
179
|
|
|
// Pattern fill |
180
|
375 |
|
$this->writePatternFill($objWriter, $fill); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Write Gradient Fill. |
186
|
|
|
*/ |
187
|
6 |
|
private function writeGradientFill(XMLWriter $objWriter, Fill $fill): void |
188
|
|
|
{ |
189
|
|
|
// fill |
190
|
6 |
|
$objWriter->startElement('fill'); |
191
|
|
|
|
192
|
|
|
// gradientFill |
193
|
6 |
|
$objWriter->startElement('gradientFill'); |
194
|
6 |
|
$objWriter->writeAttribute('type', (string) $fill->getFillType()); |
195
|
6 |
|
$objWriter->writeAttribute('degree', (string) $fill->getRotation()); |
196
|
|
|
|
197
|
|
|
// stop |
198
|
6 |
|
$objWriter->startElement('stop'); |
199
|
6 |
|
$objWriter->writeAttribute('position', '0'); |
200
|
|
|
|
201
|
|
|
// color |
202
|
6 |
|
if (!empty($fill->getStartColor()->getARGB())) { |
203
|
6 |
|
$objWriter->startElement('color'); |
204
|
6 |
|
$objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB()); |
205
|
6 |
|
$objWriter->endElement(); |
206
|
|
|
} |
207
|
|
|
|
208
|
6 |
|
$objWriter->endElement(); |
209
|
|
|
|
210
|
|
|
// stop |
211
|
6 |
|
$objWriter->startElement('stop'); |
212
|
6 |
|
$objWriter->writeAttribute('position', '1'); |
213
|
|
|
|
214
|
|
|
// color |
215
|
6 |
|
if (!empty($fill->getEndColor()->getARGB())) { |
216
|
6 |
|
$objWriter->startElement('color'); |
217
|
6 |
|
$objWriter->writeAttribute('rgb', $fill->getEndColor()->getARGB()); |
218
|
6 |
|
$objWriter->endElement(); |
219
|
|
|
} |
220
|
|
|
|
221
|
6 |
|
$objWriter->endElement(); |
222
|
|
|
|
223
|
6 |
|
$objWriter->endElement(); |
224
|
|
|
|
225
|
6 |
|
$objWriter->endElement(); |
226
|
|
|
} |
227
|
|
|
|
228
|
375 |
|
private static function writePatternColors(Fill $fill): bool |
229
|
|
|
{ |
230
|
375 |
|
if ($fill->getFillType() === Fill::FILL_NONE) { |
231
|
374 |
|
return false; |
232
|
|
|
} |
233
|
|
|
|
234
|
375 |
|
return $fill->getFillType() === Fill::FILL_SOLID || $fill->getColorsChanged(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Write Pattern Fill. |
239
|
|
|
*/ |
240
|
375 |
|
private function writePatternFill(XMLWriter $objWriter, Fill $fill): void |
241
|
|
|
{ |
242
|
|
|
// fill |
243
|
375 |
|
$objWriter->startElement('fill'); |
244
|
|
|
|
245
|
|
|
// patternFill |
246
|
375 |
|
$objWriter->startElement('patternFill'); |
247
|
375 |
|
if ($fill->getFillType()) { |
248
|
375 |
|
$objWriter->writeAttribute('patternType', (string) $fill->getFillType()); |
249
|
|
|
} |
250
|
|
|
|
251
|
375 |
|
if (self::writePatternColors($fill)) { |
252
|
|
|
// fgColor |
253
|
46 |
|
if ($fill->getStartColor()->getARGB()) { |
254
|
41 |
|
if (!$fill->getEndColor()->getARGB() && $fill->getFillType() === Fill::FILL_SOLID) { |
255
|
13 |
|
$objWriter->startElement('bgColor'); |
256
|
13 |
|
$objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB()); |
257
|
|
|
} else { |
258
|
29 |
|
$objWriter->startElement('fgColor'); |
259
|
29 |
|
$objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB()); |
260
|
|
|
} |
261
|
41 |
|
$objWriter->endElement(); |
262
|
|
|
} |
263
|
|
|
// bgColor |
264
|
46 |
|
if ($fill->getEndColor()->getARGB()) { |
265
|
34 |
|
$objWriter->startElement('bgColor'); |
266
|
34 |
|
$objWriter->writeAttribute('rgb', $fill->getEndColor()->getARGB()); |
267
|
34 |
|
$objWriter->endElement(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
375 |
|
$objWriter->endElement(); |
272
|
|
|
|
273
|
375 |
|
$objWriter->endElement(); |
274
|
|
|
} |
275
|
|
|
|
276
|
375 |
|
private function startFont(XMLWriter $objWriter, bool &$fontStarted): void |
277
|
|
|
{ |
278
|
375 |
|
if (!$fontStarted) { |
279
|
375 |
|
$fontStarted = true; |
280
|
375 |
|
$objWriter->startElement('font'); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Write Font. |
286
|
|
|
*/ |
287
|
375 |
|
private function writeFont(XMLWriter $objWriter, Font $font): void |
288
|
|
|
{ |
289
|
375 |
|
$fontStarted = false; |
290
|
|
|
// font |
291
|
|
|
// Weird! The order of these elements actually makes a difference when opening Xlsx |
292
|
|
|
// files in Excel2003 with the compatibility pack. It's not documented behaviour, |
293
|
|
|
// and makes for a real WTF! |
294
|
|
|
|
295
|
|
|
// Bold. We explicitly write this element also when false (like MS Office Excel 2007 does |
296
|
|
|
// for conditional formatting). Otherwise it will apparently not be picked up in conditional |
297
|
|
|
// formatting style dialog |
298
|
375 |
|
if ($font->getBold() !== null) { |
299
|
374 |
|
$this->startFont($objWriter, $fontStarted); |
300
|
374 |
|
$objWriter->startElement('b'); |
301
|
374 |
|
$objWriter->writeAttribute('val', $font->getBold() ? '1' : '0'); |
302
|
374 |
|
$objWriter->endElement(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
// Italic |
306
|
375 |
|
if ($font->getItalic() !== null) { |
307
|
374 |
|
$this->startFont($objWriter, $fontStarted); |
308
|
374 |
|
$objWriter->startElement('i'); |
309
|
374 |
|
$objWriter->writeAttribute('val', $font->getItalic() ? '1' : '0'); |
310
|
374 |
|
$objWriter->endElement(); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
// Strikethrough |
314
|
375 |
|
if ($font->getStrikethrough() !== null) { |
315
|
374 |
|
$this->startFont($objWriter, $fontStarted); |
316
|
374 |
|
$objWriter->startElement('strike'); |
317
|
374 |
|
$objWriter->writeAttribute('val', $font->getStrikethrough() ? '1' : '0'); |
318
|
374 |
|
$objWriter->endElement(); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
// Underline |
322
|
375 |
|
if ($font->getUnderline() !== null) { |
323
|
374 |
|
$this->startFont($objWriter, $fontStarted); |
324
|
374 |
|
$objWriter->startElement('u'); |
325
|
374 |
|
$objWriter->writeAttribute('val', $font->getUnderline()); |
326
|
374 |
|
$objWriter->endElement(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// Superscript / subscript |
330
|
375 |
|
if ($font->getSuperscript() === true || $font->getSubscript() === true) { |
331
|
2 |
|
$this->startFont($objWriter, $fontStarted); |
332
|
2 |
|
$objWriter->startElement('vertAlign'); |
333
|
2 |
|
if ($font->getSuperscript() === true) { |
334
|
2 |
|
$objWriter->writeAttribute('val', 'superscript'); |
335
|
2 |
|
} elseif ($font->getSubscript() === true) { |
336
|
2 |
|
$objWriter->writeAttribute('val', 'subscript'); |
337
|
|
|
} |
338
|
2 |
|
$objWriter->endElement(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
// Size |
342
|
375 |
|
if ($font->getSize() !== null) { |
343
|
374 |
|
$this->startFont($objWriter, $fontStarted); |
344
|
374 |
|
$objWriter->startElement('sz'); |
345
|
374 |
|
$objWriter->writeAttribute('val', StringHelper::formatNumber($font->getSize())); |
346
|
374 |
|
$objWriter->endElement(); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
// Foreground color |
350
|
375 |
|
if ($font->getColor()->getARGB() !== null) { |
351
|
375 |
|
$this->startFont($objWriter, $fontStarted); |
352
|
375 |
|
$objWriter->startElement('color'); |
353
|
375 |
|
$objWriter->writeAttribute('rgb', $font->getColor()->getARGB()); |
354
|
375 |
|
$objWriter->endElement(); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
// Name |
358
|
375 |
|
if ($font->getName() !== null) { |
359
|
374 |
|
$this->startFont($objWriter, $fontStarted); |
360
|
374 |
|
$objWriter->startElement('name'); |
361
|
374 |
|
$objWriter->writeAttribute('val', $font->getName()); |
362
|
374 |
|
$objWriter->endElement(); |
363
|
|
|
} |
364
|
|
|
|
365
|
375 |
|
if (!empty($font->getScheme())) { |
366
|
53 |
|
$this->startFont($objWriter, $fontStarted); |
367
|
53 |
|
$objWriter->startElement('scheme'); |
368
|
53 |
|
$objWriter->writeAttribute('val', $font->getScheme()); |
369
|
53 |
|
$objWriter->endElement(); |
370
|
|
|
} |
371
|
|
|
|
372
|
375 |
|
if ($fontStarted) { |
373
|
375 |
|
$objWriter->endElement(); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Write Border. |
379
|
|
|
*/ |
380
|
375 |
|
private function writeBorder(XMLWriter $objWriter, Borders $borders): void |
381
|
|
|
{ |
382
|
|
|
// Write border |
383
|
375 |
|
$objWriter->startElement('border'); |
384
|
|
|
// Diagonal? |
385
|
375 |
|
switch ($borders->getDiagonalDirection()) { |
386
|
|
|
case Borders::DIAGONAL_UP: |
387
|
3 |
|
$objWriter->writeAttribute('diagonalUp', 'true'); |
388
|
3 |
|
$objWriter->writeAttribute('diagonalDown', 'false'); |
389
|
|
|
|
390
|
3 |
|
break; |
391
|
|
|
case Borders::DIAGONAL_DOWN: |
392
|
2 |
|
$objWriter->writeAttribute('diagonalUp', 'false'); |
393
|
2 |
|
$objWriter->writeAttribute('diagonalDown', 'true'); |
394
|
|
|
|
395
|
2 |
|
break; |
396
|
|
|
case Borders::DIAGONAL_BOTH: |
397
|
3 |
|
$objWriter->writeAttribute('diagonalUp', 'true'); |
398
|
3 |
|
$objWriter->writeAttribute('diagonalDown', 'true'); |
399
|
|
|
|
400
|
3 |
|
break; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
// BorderPr |
404
|
375 |
|
$this->writeBorderPr($objWriter, 'left', $borders->getLeft()); |
405
|
375 |
|
$this->writeBorderPr($objWriter, 'right', $borders->getRight()); |
406
|
375 |
|
$this->writeBorderPr($objWriter, 'top', $borders->getTop()); |
407
|
375 |
|
$this->writeBorderPr($objWriter, 'bottom', $borders->getBottom()); |
408
|
375 |
|
$this->writeBorderPr($objWriter, 'diagonal', $borders->getDiagonal()); |
409
|
375 |
|
$objWriter->endElement(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Write Cell Style Xf. |
414
|
|
|
*/ |
415
|
375 |
|
private function writeCellStyleXf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $style, Spreadsheet $spreadsheet, string $defaultAlignHash): void |
416
|
|
|
{ |
417
|
|
|
// xf |
418
|
375 |
|
$objWriter->startElement('xf'); |
419
|
375 |
|
$objWriter->writeAttribute('xfId', '0'); |
420
|
375 |
|
$objWriter->writeAttribute('fontId', (string) (int) $this->getParentWriter()->getFontHashTable()->getIndexForHashCode($style->getFont()->getHashCode())); |
421
|
375 |
|
if ($style->getQuotePrefix()) { |
422
|
4 |
|
$objWriter->writeAttribute('quotePrefix', '1'); |
423
|
|
|
} |
424
|
|
|
|
425
|
375 |
|
if ($style->getNumberFormat()->getBuiltInFormatCode() === false) { |
426
|
45 |
|
$objWriter->writeAttribute('numFmtId', (string) (int) ($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($style->getNumberFormat()->getHashCode()) + 164)); |
427
|
|
|
} else { |
428
|
375 |
|
$objWriter->writeAttribute('numFmtId', (string) (int) $style->getNumberFormat()->getBuiltInFormatCode()); |
429
|
|
|
} |
430
|
|
|
|
431
|
375 |
|
$objWriter->writeAttribute('fillId', (string) (int) $this->getParentWriter()->getFillHashTable()->getIndexForHashCode($style->getFill()->getHashCode())); |
432
|
375 |
|
$objWriter->writeAttribute('borderId', (string) (int) $this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($style->getBorders()->getHashCode())); |
433
|
|
|
|
434
|
|
|
// Apply styles? |
435
|
375 |
|
$objWriter->writeAttribute('applyFont', ($spreadsheet->getDefaultStyle()->getFont()->getHashCode() != $style->getFont()->getHashCode()) ? '1' : '0'); |
436
|
375 |
|
$objWriter->writeAttribute('applyNumberFormat', ($spreadsheet->getDefaultStyle()->getNumberFormat()->getHashCode() != $style->getNumberFormat()->getHashCode()) ? '1' : '0'); |
437
|
375 |
|
$objWriter->writeAttribute('applyFill', ($spreadsheet->getDefaultStyle()->getFill()->getHashCode() != $style->getFill()->getHashCode()) ? '1' : '0'); |
438
|
375 |
|
$objWriter->writeAttribute('applyBorder', ($spreadsheet->getDefaultStyle()->getBorders()->getHashCode() != $style->getBorders()->getHashCode()) ? '1' : '0'); |
439
|
375 |
|
if ($defaultAlignHash !== '' && $defaultAlignHash === $style->getAlignment()->getHashCode()) { |
440
|
365 |
|
$applyAlignment = '0'; |
441
|
|
|
} else { |
442
|
62 |
|
$applyAlignment = '1'; |
443
|
|
|
} |
444
|
375 |
|
$objWriter->writeAttribute('applyAlignment', $applyAlignment); |
445
|
375 |
|
if ($style->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $style->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) { |
446
|
25 |
|
$objWriter->writeAttribute('applyProtection', 'true'); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
// alignment |
450
|
375 |
|
if ($applyAlignment === '1') { |
451
|
62 |
|
$objWriter->startElement('alignment'); |
452
|
62 |
|
$vertical = Alignment::VERTICAL_ALIGNMENT_FOR_XLSX[$style->getAlignment()->getVertical()] ?? ''; |
453
|
62 |
|
$horizontal = Alignment::HORIZONTAL_ALIGNMENT_FOR_XLSX[$style->getAlignment()->getHorizontal()] ?? ''; |
454
|
62 |
|
if ($horizontal !== '') { |
455
|
48 |
|
$objWriter->writeAttribute('horizontal', $horizontal); |
456
|
|
|
} |
457
|
62 |
|
if ($vertical !== '') { |
458
|
62 |
|
$objWriter->writeAttribute('vertical', $vertical); |
459
|
|
|
} |
460
|
62 |
|
$justifyLastLine = $style->getAlignment()->getJustifyLastLine(); |
461
|
62 |
|
if (is_bool($justifyLastLine)) { |
462
|
1 |
|
$objWriter->writeAttribute('justifyLastLine', (string) (int) $justifyLastLine); |
463
|
|
|
} |
464
|
|
|
|
465
|
62 |
|
if ($style->getAlignment()->getTextRotation() >= 0) { |
466
|
61 |
|
$textRotation = $style->getAlignment()->getTextRotation(); |
467
|
|
|
} else { |
468
|
6 |
|
$textRotation = 90 - $style->getAlignment()->getTextRotation(); |
469
|
|
|
} |
470
|
62 |
|
$objWriter->writeAttribute('textRotation', (string) $textRotation); |
471
|
|
|
|
472
|
62 |
|
$objWriter->writeAttribute('wrapText', ($style->getAlignment()->getWrapText() ? 'true' : 'false')); |
473
|
62 |
|
$objWriter->writeAttribute('shrinkToFit', ($style->getAlignment()->getShrinkToFit() ? 'true' : 'false')); |
474
|
|
|
|
475
|
62 |
|
if ($style->getAlignment()->getIndent() > 0) { |
476
|
1 |
|
$objWriter->writeAttribute('indent', (string) $style->getAlignment()->getIndent()); |
477
|
|
|
} |
478
|
62 |
|
if ($style->getAlignment()->getReadOrder() > 0) { |
479
|
|
|
$objWriter->writeAttribute('readingOrder', (string) $style->getAlignment()->getReadOrder()); |
480
|
|
|
} |
481
|
62 |
|
$objWriter->endElement(); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
// protection |
485
|
375 |
|
if ($style->getProtection()->getLocked() != Protection::PROTECTION_INHERIT || $style->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) { |
486
|
25 |
|
$objWriter->startElement('protection'); |
487
|
25 |
|
if ($style->getProtection()->getLocked() != Protection::PROTECTION_INHERIT) { |
488
|
19 |
|
$objWriter->writeAttribute('locked', ($style->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED ? 'true' : 'false')); |
489
|
|
|
} |
490
|
25 |
|
if ($style->getProtection()->getHidden() != Protection::PROTECTION_INHERIT) { |
491
|
12 |
|
$objWriter->writeAttribute('hidden', ($style->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED ? 'true' : 'false')); |
492
|
|
|
} |
493
|
25 |
|
$objWriter->endElement(); |
494
|
|
|
} |
495
|
|
|
|
496
|
375 |
|
$objWriter->endElement(); |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Write Cell Style Dxf. |
501
|
|
|
*/ |
502
|
33 |
|
private function writeCellStyleDxf(XMLWriter $objWriter, \PhpOffice\PhpSpreadsheet\Style\Style $style): void |
503
|
|
|
{ |
504
|
|
|
// dxf |
505
|
33 |
|
$objWriter->startElement('dxf'); |
506
|
|
|
|
507
|
|
|
// font |
508
|
33 |
|
$this->writeFont($objWriter, $style->getFont()); |
509
|
|
|
|
510
|
|
|
// numFmt |
511
|
33 |
|
$this->writeNumFmt($objWriter, $style->getNumberFormat()); |
512
|
|
|
|
513
|
|
|
// fill |
514
|
33 |
|
$this->writeFill($objWriter, $style->getFill()); |
515
|
|
|
|
516
|
|
|
// border |
517
|
33 |
|
$this->writeBorder($objWriter, $style->getBorders()); |
518
|
|
|
|
519
|
33 |
|
$objWriter->endElement(); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* Write BorderPr. |
524
|
|
|
* |
525
|
|
|
* @param string $name Element name |
526
|
|
|
*/ |
527
|
375 |
|
private function writeBorderPr(XMLWriter $objWriter, string $name, Border $border): void |
528
|
|
|
{ |
529
|
|
|
// Write BorderPr |
530
|
375 |
|
if ($border->getBorderStyle() === Border::BORDER_OMIT) { |
531
|
33 |
|
return; |
532
|
|
|
} |
533
|
374 |
|
$objWriter->startElement($name); |
534
|
374 |
|
if ($border->getBorderStyle() !== Border::BORDER_NONE) { |
535
|
33 |
|
$objWriter->writeAttribute('style', $border->getBorderStyle()); |
536
|
|
|
|
537
|
|
|
// color |
538
|
33 |
|
if ($border->getColor()->getARGB() !== null) { |
539
|
33 |
|
$objWriter->startElement('color'); |
540
|
33 |
|
$objWriter->writeAttribute('rgb', $border->getColor()->getARGB()); |
541
|
33 |
|
$objWriter->endElement(); |
542
|
|
|
} |
543
|
|
|
} |
544
|
374 |
|
$objWriter->endElement(); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Write NumberFormat. |
549
|
|
|
* |
550
|
|
|
* @param int $id Number Format identifier |
551
|
|
|
*/ |
552
|
73 |
|
private function writeNumFmt(XMLWriter $objWriter, ?NumberFormat $numberFormat, int $id = 0): void |
553
|
|
|
{ |
554
|
|
|
// Translate formatcode |
555
|
73 |
|
$formatCode = ($numberFormat === null) ? null : $numberFormat->getFormatCode(); |
556
|
|
|
|
557
|
|
|
// numFmt |
558
|
73 |
|
if ($formatCode !== null) { |
559
|
47 |
|
$objWriter->startElement('numFmt'); |
560
|
47 |
|
$objWriter->writeAttribute('numFmtId', (string) ($id + 164)); |
561
|
47 |
|
$objWriter->writeAttribute('formatCode', $formatCode); |
562
|
47 |
|
$objWriter->endElement(); |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Get an array of all styles. |
568
|
|
|
* |
569
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Style\Style[] All styles in PhpSpreadsheet |
570
|
|
|
*/ |
571
|
374 |
|
public function allStyles(Spreadsheet $spreadsheet): array |
572
|
|
|
{ |
573
|
374 |
|
return $spreadsheet->getCellXfCollection(); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
/** |
577
|
|
|
* Get an array of all conditional styles. |
578
|
|
|
* |
579
|
|
|
* @return Conditional[] All conditional styles in PhpSpreadsheet |
580
|
|
|
*/ |
581
|
375 |
|
public function allConditionalStyles(Spreadsheet $spreadsheet): array |
582
|
|
|
{ |
583
|
|
|
// Get an array of all styles |
584
|
375 |
|
$aStyles = []; |
585
|
|
|
|
586
|
375 |
|
$sheetCount = $spreadsheet->getSheetCount(); |
587
|
375 |
|
for ($i = 0; $i < $sheetCount; ++$i) { |
588
|
375 |
|
foreach ($spreadsheet->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) { |
589
|
33 |
|
foreach ($conditionalStyles as $conditionalStyle) { |
590
|
33 |
|
$aStyles[] = $conditionalStyle; |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
|
595
|
375 |
|
return $aStyles; |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Get an array of all fills. |
600
|
|
|
* |
601
|
|
|
* @return Fill[] All fills in PhpSpreadsheet |
602
|
|
|
*/ |
603
|
374 |
|
public function allFills(Spreadsheet $spreadsheet): array |
604
|
|
|
{ |
605
|
|
|
// Get an array of unique fills |
606
|
374 |
|
$aFills = []; |
607
|
|
|
|
608
|
|
|
// Two first fills are predefined |
609
|
374 |
|
$fill0 = new Fill(); |
610
|
374 |
|
$fill0->setFillType(Fill::FILL_NONE); |
611
|
374 |
|
$aFills[] = $fill0; |
612
|
|
|
|
613
|
374 |
|
$fill1 = new Fill(); |
614
|
374 |
|
$fill1->setFillType(Fill::FILL_PATTERN_GRAY125); |
615
|
374 |
|
$aFills[] = $fill1; |
616
|
|
|
// The remaining fills |
617
|
374 |
|
$aStyles = $this->allStyles($spreadsheet); |
618
|
374 |
|
foreach ($aStyles as $style) { |
619
|
374 |
|
if (!isset($aFills[$style->getFill()->getHashCode()])) { |
620
|
374 |
|
$aFills[$style->getFill()->getHashCode()] = $style->getFill(); |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
374 |
|
return $aFills; |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
/** |
628
|
|
|
* Get an array of all fonts. |
629
|
|
|
* |
630
|
|
|
* @return Font[] All fonts in PhpSpreadsheet |
631
|
|
|
*/ |
632
|
374 |
|
public function allFonts(Spreadsheet $spreadsheet): array |
633
|
|
|
{ |
634
|
|
|
// Get an array of unique fonts |
635
|
374 |
|
$aFonts = []; |
636
|
374 |
|
$aStyles = $this->allStyles($spreadsheet); |
637
|
|
|
|
638
|
374 |
|
foreach ($aStyles as $style) { |
639
|
374 |
|
if (!isset($aFonts[$style->getFont()->getHashCode()])) { |
640
|
374 |
|
$aFonts[$style->getFont()->getHashCode()] = $style->getFont(); |
641
|
|
|
} |
642
|
|
|
} |
643
|
|
|
|
644
|
374 |
|
return $aFonts; |
645
|
|
|
} |
646
|
|
|
|
647
|
|
|
/** |
648
|
|
|
* Get an array of all borders. |
649
|
|
|
* |
650
|
|
|
* @return Borders[] All borders in PhpSpreadsheet |
651
|
|
|
*/ |
652
|
374 |
|
public function allBorders(Spreadsheet $spreadsheet): array |
653
|
|
|
{ |
654
|
|
|
// Get an array of unique borders |
655
|
374 |
|
$aBorders = []; |
656
|
374 |
|
$aStyles = $this->allStyles($spreadsheet); |
657
|
|
|
|
658
|
374 |
|
foreach ($aStyles as $style) { |
659
|
374 |
|
if (!isset($aBorders[$style->getBorders()->getHashCode()])) { |
660
|
374 |
|
$aBorders[$style->getBorders()->getHashCode()] = $style->getBorders(); |
661
|
|
|
} |
662
|
|
|
} |
663
|
|
|
|
664
|
374 |
|
return $aBorders; |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
/** |
668
|
|
|
* Get an array of all number formats. |
669
|
|
|
* |
670
|
|
|
* @return NumberFormat[] All number formats in PhpSpreadsheet |
671
|
|
|
*/ |
672
|
374 |
|
public function allNumberFormats(Spreadsheet $spreadsheet): array |
673
|
|
|
{ |
674
|
|
|
// Get an array of unique number formats |
675
|
374 |
|
$aNumFmts = []; |
676
|
374 |
|
$aStyles = $this->allStyles($spreadsheet); |
677
|
|
|
|
678
|
374 |
|
foreach ($aStyles as $style) { |
679
|
374 |
|
if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !isset($aNumFmts[$style->getNumberFormat()->getHashCode()])) { |
680
|
45 |
|
$aNumFmts[$style->getNumberFormat()->getHashCode()] = $style->getNumberFormat(); |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
|
684
|
374 |
|
return $aNumFmts; |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|