1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
<?php
/**
* PHPExcel
*
* Copyright (c) 2006 - 2012 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel_Style
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.8, 2012-10-12
*/
/**
* PHPExcel_Style
*
* @category PHPExcel
* @package PHPExcel_Style
* @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Style implements PHPExcel_IComparable
{
/**
* Font
*
* @var PHPExcel_Style_Font
*/
private $_font;
/**
* Fill
*
* @var PHPExcel_Style_Fill
*/
private $_fill;
/**
* Borders
*
* @var PHPExcel_Style_Borders
*/
private $_borders;
/**
* Alignment
*
* @var PHPExcel_Style_Alignment
*/
private $_alignment;
/**
* Number Format
*
* @var PHPExcel_Style_NumberFormat
*/
private $_numberFormat;
/**
* Conditional styles
*
* @var PHPExcel_Style_Conditional[]
*/
private $_conditionalStyles;
/**
* Protection
*
* @var PHPExcel_Style_Protection
*/
private $_protection;
/**
* Style supervisor?
*
* @var boolean
*/
private $_isSupervisor;
/**
* Parent. Only used for style supervisor
*
* @var PHPExcel
*/
private $_parent;
/**
* Index of style in collection. Only used for real style.
*
* @var int
*/
private $_index;
/**
* Create a new PHPExcel_Style
*
* @param boolean $isSupervisor Flag indicating if this is a supervisor or not
* Leave this value at default unless you understand exactly what
* its ramifications are
* @param boolean $isConditional Flag indicating if this is a conditional style or not
* Leave this value at default unless you understand exactly what
* its ramifications are
*/
public function __construct($isSupervisor = false, $isConditional = false)
{
// Supervisor?
$this->_isSupervisor = $isSupervisor;
// Initialise values
$this->_conditionalStyles = array();
$this->_font = new PHPExcel_Style_Font($isSupervisor, $isConditional);
$this->_fill = new PHPExcel_Style_Fill($isSupervisor, $isConditional);
$this->_borders = new PHPExcel_Style_Borders($isSupervisor, $isConditional);
$this->_alignment = new PHPExcel_Style_Alignment($isSupervisor, $isConditional);
$this->_numberFormat = new PHPExcel_Style_NumberFormat($isSupervisor, $isConditional);
$this->_protection = new PHPExcel_Style_Protection($isSupervisor, $isConditional);
// bind parent if we are a supervisor
if ($isSupervisor) {
$this->_font->bindParent($this);
$this->_fill->bindParent($this);
$this->_borders->bindParent($this);
$this->_alignment->bindParent($this);
$this->_numberFormat->bindParent($this);
$this->_protection->bindParent($this);
}
}
/**
* Bind parent. Only used for supervisor
*
* @param PHPExcel $parent
* @return PHPExcel_Style
*/
public function bindParent($parent)
{
$this->_parent = $parent;
return $this;
}
/**
* Is this a supervisor or a real style component?
*
* @return boolean
*/
public function getIsSupervisor()
{
return $this->_isSupervisor;
}
/**
* Get the shared style component for the currently active cell in currently active sheet.
* Only used for style supervisor
*
* @return PHPExcel_Style
*/
public function getSharedComponent()
{
$activeSheet = $this->getActiveSheet();
$selectedCell = $this->getActiveCell(); // e.g. 'A1'
if ($activeSheet->cellExists($selectedCell)) {
$xfIndex = $activeSheet->getCell($selectedCell)->getXfIndex();
} else {
$xfIndex = 0;
}
return $this->_parent->getCellXfByIndex($xfIndex);
}
/**
* Get the currently active sheet. Only used for supervisor
*
* @return PHPExcel_Worksheet
*/
public function getActiveSheet()
{
return $this->_parent->getActiveSheet();
}
/**
* Get the currently active cell coordinate in currently active sheet.
* Only used for supervisor
*
* @return string E.g. 'A1'
*/
public function getSelectedCells()
{
return $this->_parent->getActiveSheet()->getSelectedCells();
}
/**
* Get the currently active cell coordinate in currently active sheet.
* Only used for supervisor
*
* @return string E.g. 'A1'
*/
public function getActiveCell()
{
return $this->_parent->getActiveSheet()->getActiveCell();
}
/**
* Get parent. Only used for style supervisor
*
* @return PHPExcel
*/
public function getParent()
{
return $this->_parent;
}
/**
* Apply styles from array
*
* <code>
* $objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray(
* array(
* 'font' => array(
* 'name' => 'Arial',
* 'bold' => true,
* 'italic' => false,
* 'underline' => PHPExcel_Style_Font::UNDERLINE_DOUBLE,
* 'strike' => false,
* 'color' => array(
* 'rgb' => '808080'
* )
* ),
* 'borders' => array(
* 'bottom' => array(
* 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
* 'color' => array(
* 'rgb' => '808080'
* )
* ),
* 'top' => array(
* 'style' => PHPExcel_Style_Border::BORDER_DASHDOT,
* 'color' => array(
* 'rgb' => '808080'
* )
* )
* )
* )
* );
* </code>
*
* @param array $pStyles Array containing style information
* @param boolean $pAdvanced Advanced mode for setting borders.
* @throws Exception
* @return PHPExcel_Style
*/
public function applyFromArray($pStyles = null, $pAdvanced = true) {
if (is_array($pStyles)) {
if ($this->_isSupervisor) {
$pRange = $this->getSelectedCells();
// Uppercase coordinate
$pRange = strtoupper($pRange);
// Is it a cell range or a single cell?
if (strpos($pRange, ':') === false) {
$rangeA = $pRange;
$rangeB = $pRange;
} else {
list($rangeA, $rangeB) = explode(':', $pRange);
}
// Calculate range outer borders
$rangeStart = PHPExcel_Cell::coordinateFromString($rangeA);
$rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB);
// Translate column into index
$rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]) - 1;
$rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]) - 1;
// Make sure we can loop upwards on rows and columns
if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
$tmp = $rangeStart;
$rangeStart = $rangeEnd;
$rangeEnd = $tmp;
}
// ADVANCED MODE:
if ($pAdvanced && isset($pStyles['borders'])) {
// 'allborders' is a shorthand property for 'outline' and 'inside' and
// it applies to components that have not been set explicitly
if (isset($pStyles['borders']['allborders'])) {
foreach (array('outline', 'inside') as $component) {
if (!isset($pStyles['borders'][$component])) {
$pStyles['borders'][$component] = $pStyles['borders']['allborders'];
}
}
unset($pStyles['borders']['allborders']); // not needed any more
}
// 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left'
// it applies to components that have not been set explicitly
if (isset($pStyles['borders']['outline'])) {
foreach (array('top', 'right', 'bottom', 'left') as $component) {
if (!isset($pStyles['borders'][$component])) {
$pStyles['borders'][$component] = $pStyles['borders']['outline'];
}
}
unset($pStyles['borders']['outline']); // not needed any more
}
// 'inside' is a shorthand property for 'vertical' and 'horizontal'
// it applies to components that have not been set explicitly
if (isset($pStyles['borders']['inside'])) {
foreach (array('vertical', 'horizontal') as $component) {
if (!isset($pStyles['borders'][$component])) {
$pStyles['borders'][$component] = $pStyles['borders']['inside'];
}
}
unset($pStyles['borders']['inside']); // not needed any more
}
// width and height characteristics of selection, 1, 2, or 3 (for 3 or more)
$xMax = min($rangeEnd[0] - $rangeStart[0] + 1, 3);
$yMax = min($rangeEnd[1] - $rangeStart[1] + 1, 3);
// loop through up to 3 x 3 = 9 regions
for ($x = 1; $x <= $xMax; ++$x) {
// start column index for region
$colStart = ($x == 3) ?
PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0])
: PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] + $x - 1);
// end column index for region
$colEnd = ($x == 1) ?
PHPExcel_Cell::stringFromColumnIndex($rangeStart[0])
: PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] - $xMax + $x);
for ($y = 1; $y <= $yMax; ++$y) {
// which edges are touching the region
$edges = array();
// are we at left edge
if ($x == 1) {
$edges[] = 'left';
}
// are we at right edge
if ($x == $xMax) {
$edges[] = 'right';
}
// are we at top edge?
if ($y == 1) {
$edges[] = 'top';
}
// are we at bottom edge?
if ($y == $yMax) {
$edges[] = 'bottom';
}
// start row index for region
$rowStart = ($y == 3) ?
$rangeEnd[1] : $rangeStart[1] + $y - 1;
// end row index for region
$rowEnd = ($y == 1) ?
$rangeStart[1] : $rangeEnd[1] - $yMax + $y;
// build range for region
$range = $colStart . $rowStart . ':' . $colEnd . $rowEnd;
// retrieve relevant style array for region
$regionStyles = $pStyles;
unset($regionStyles['borders']['inside']);
// what are the inner edges of the region when looking at the selection
$innerEdges = array_diff( array('top', 'right', 'bottom', 'left'), $edges );
// inner edges that are not touching the region should take the 'inside' border properties if they have been set
foreach ($innerEdges as $innerEdge) {
switch ($innerEdge) {
case 'top':
case 'bottom':
// should pick up 'horizontal' border property if set
if (isset($pStyles['borders']['horizontal'])) {
$regionStyles['borders'][$innerEdge] = $pStyles['borders']['horizontal'];
} else {
unset($regionStyles['borders'][$innerEdge]);
}
break;
case 'left':
case 'right':
// should pick up 'vertical' border property if set
if (isset($pStyles['borders']['vertical'])) {
$regionStyles['borders'][$innerEdge] = $pStyles['borders']['vertical'];
} else {
unset($regionStyles['borders'][$innerEdge]);
}
break;
}
}
// apply region style to region by calling applyFromArray() in simple mode
$this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false);
}
}
return $this;
}
// SIMPLE MODE:
// Selection type, inspect
if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
$selectionType = 'COLUMN';
} else if (preg_match('/^A[0-9]+:XFD[0-9]+$/', $pRange)) {
$selectionType = 'ROW';
} else {
$selectionType = 'CELL';
}
// First loop through columns, rows, or cells to find out which styles are affected by this operation
switch ($selectionType) {
case 'COLUMN':
$oldXfIndexes = array();
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
$oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true;
}
break;
case 'ROW':
$oldXfIndexes = array();
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() == null) {
$oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style
} else {
$oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true;
}
}
break;
case 'CELL':
$oldXfIndexes = array();
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
$oldXfIndexes[$this->getActiveSheet()->getCellByColumnAndRow($col, $row)->getXfIndex()] = true;
}
}
break;
}
// clone each of the affected styles, apply the style arrray, and add the new styles to the workbook
$workbook = $this->getActiveSheet()->getParent();
foreach ($oldXfIndexes as $oldXfIndex => $dummy) {
$style = $workbook->getCellXfByIndex($oldXfIndex);
$newStyle = clone $style;
$newStyle->applyFromArray($pStyles);
if ($existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode())) {
// there is already such cell Xf in our collection
$newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
} else {
// we don't have such a cell Xf, need to add
$workbook->addCellXf($newStyle);
$newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
}
}
// Loop through columns, rows, or cells again and update the XF index
switch ($selectionType) {
case 'COLUMN':
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
$columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col);
$oldXfIndex = $columnDimension->getXfIndex();
$columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
}
break;
case 'ROW':
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
$rowDimension = $this->getActiveSheet()->getRowDimension($row);
$oldXfIndex = $rowDimension->getXfIndex() === null ?
0 : $rowDimension->getXfIndex(); // row without explicit style should be formatted based on default style
$rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
}
break;
case 'CELL':
for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
$cell = $this->getActiveSheet()->getCellByColumnAndRow($col, $row);
$oldXfIndex = $cell->getXfIndex();
$cell->setXfIndex($newXfIndexes[$oldXfIndex]);
}
}
break;
}
} else {
// not a supervisor, just apply the style array directly on style object
if (array_key_exists('fill', $pStyles)) {
$this->getFill()->applyFromArray($pStyles['fill']);
}
if (array_key_exists('font', $pStyles)) {
$this->getFont()->applyFromArray($pStyles['font']);
}
if (array_key_exists('borders', $pStyles)) {
$this->getBorders()->applyFromArray($pStyles['borders']);
}
if (array_key_exists('alignment', $pStyles)) {
$this->getAlignment()->applyFromArray($pStyles['alignment']);
}
if (array_key_exists('numberformat', $pStyles)) {
$this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
}
if (array_key_exists('protection', $pStyles)) {
$this->getProtection()->applyFromArray($pStyles['protection']);
}
}
} else {
throw new Exception("Invalid style array passed.");
}
return $this;
}
/**
* Get Fill
*
* @return PHPExcel_Style_Fill
*/
public function getFill() {
return $this->_fill;
}
/**
* Get Font
*
* @return PHPExcel_Style_Font
*/
public function getFont() {
return $this->_font;
}
/**
* Set font
*
* @param PHPExcel_Style_Font $font
* @return PHPExcel_Style
*/
public function setFont(PHPExcel_Style_Font $font)
{
$this->_font = $font;
return $this;
}
/**
* Get Borders
*
* @return PHPExcel_Style_Borders
*/
public function getBorders() {
return $this->_borders;
}
/**
* Get Alignment
*
* @return PHPExcel_Style_Alignment
*/
public function getAlignment() {
return $this->_alignment;
}
/**
* Get Number Format
*
* @return PHPExcel_Style_NumberFormat
*/
public function getNumberFormat() {
return $this->_numberFormat;
}
/**
* Get Conditional Styles. Only used on supervisor.
*
* @return PHPExcel_Style_Conditional[]
*/
public function getConditionalStyles() {
return $this->getActiveSheet()->getConditionalStyles($this->getActiveCell());
}
/**
* Set Conditional Styles. Only used on supervisor.
*
* @param PHPExcel_Style_Conditional[] $pValue Array of condtional styles
* @return PHPExcel_Style
*/
public function setConditionalStyles($pValue = null) {
if (is_array($pValue)) {
$this->getActiveSheet()->setConditionalStyles($this->getSelectedCells(), $pValue);
}
return $this;
}
/**
* Get Protection
*
* @return PHPExcel_Style_Protection
*/
public function getProtection() {
return $this->_protection;
}
/**
* Get hash code
*
* @return string Hash code
*/
public function getHashCode() {
$hashConditionals = '';
foreach ($this->_conditionalStyles as $conditional) {
$hashConditionals .= $conditional->getHashCode();
}
return md5(
$this->_fill->getHashCode()
. $this->_font->getHashCode()
. $this->_borders->getHashCode()
. $this->_alignment->getHashCode()
. $this->_numberFormat->getHashCode()
. $hashConditionals
. $this->_protection->getHashCode()
. __CLASS__
);
}
/**
* Get own index in style collection
*
* @return int
*/
public function getIndex()
{
return $this->_index;
}
/**
* Set own index in style collection
*
* @param int $pValue
*/
public function setIndex($pValue)
{
$this->_index = $pValue;
}
/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
public function __clone() {
$vars = get_object_vars($this);
foreach ($vars as $key => $value) {
if ((is_object($value)) && ($key != '_parent')) {
$this->$key = clone $value;
} else {
$this->$key = $value;
}
}
}
}
|