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