Skip to content

Commit ae0cd46

Browse files
authored
Add exportArray Method for Styles (#1580)
Issue #580 has gone stale since I started work on this. Nevertheless, this implements an exportArray function as an exact counterpart of applyFromArry. I chose the name exportArray to avoid confusion with the existing method getStyleArray, which does something completely different. This change also increases coverage for all the Style classes to 100%, with the exception of Style.php itself. There were several (unchanged) places in Style.php where I did not have sufficient understanding of what was supposed to be happening, so could not create tests. All properties used by applyFromArray are exported by this method. Note that conditional styles are not covered; this is consistent with the fact that they are not covered by applyFromArray. The method is implemented as a final public function in Style/Supervisor, which calls abstract protected function exportArray1, which is implemented in each of the subclasses, and which calls final protected function exportArray2 in Style/Supervisor. So exportArray is usable for any of the subclasses as well. The new method is added to the documentation. The existing documentation for applyFromArray was alphabetized to make it easier to follow. One property (Style quotePrefix) was added to the documentation. Some Borders pseudo-properties (vertical, horizontal, and outline) were documented as usable by applyFromArray, but aren't actually supported - they were removed. The documentation of the properties seemed to use setProperty and getProperty fairly randomly - it now uses setProperty exclusively. New constants were added for the textRotation "angles" used to create a "stacked" cell. I felt that changing the readers and writers to use these constants was beyond the scope of this change, but it is on my to-do list.
1 parent cc209d0 commit ae0cd46

19 files changed

+840
-78
lines changed

docs/topics/recipes.md

+55-43
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ execution whenever you are setting more than one style property. But the
587587
difference may barely be measurable unless you have many different
588588
styles in your workbook.
589589

590+
You can perform the opposite function, exporting a Style as an array,
591+
as follows:
592+
593+
``` php
594+
$styleArray = $spreadsheet->getActiveSheet()->getStyle('A3')->exportArray();
595+
```
596+
590597
### Number formats
591598

592599
You often want to format numbers in Excel. For example you may want a
@@ -753,69 +760,74 @@ another style array.
753760

754761
Array key | Maps to property
755762
-------------|-------------------
756-
fill | getFill()
757-
font | getFont()
758-
borders | getBorders()
759-
alignment | getAlignment()
760-
numberFormat | getNumberFormat()
761-
protection | getProtection()
763+
alignment | setAlignment()
764+
borders | setBorders()
765+
fill | setFill()
766+
font | setFont()
767+
numberFormat | setNumberFormat()
768+
protection | setProtection()
769+
quotePrefix | setQuotePrefix()
762770

763-
**\PhpOffice\PhpSpreadsheet\Style\Fill**
771+
**\PhpOffice\PhpSpreadsheet\Style\Alignment**
764772

765-
Array key | Maps to property
766-
-----------|-------------------
767-
fillType | setFillType()
768-
rotation | setRotation()
769-
startColor | getStartColor()
770-
endColor | getEndColor()
771-
color | getStartColor()
773+
Array key | Maps to property
774+
------------|-------------------
775+
horizontal | setHorizontal()
776+
indent | setIndent()
777+
readOrder | setReadOrder()
778+
shrinkToFit | setShrinkToFit()
779+
textRotation| setTextRotation()
780+
vertical | setVertical()
781+
wrapText | setWrapText()
772782

773-
**\PhpOffice\PhpSpreadsheet\Style\Font**
783+
**\PhpOffice\PhpSpreadsheet\Style\Border**
774784

775785
Array key | Maps to property
776786
------------|-------------------
777-
name | setName()
778-
bold | setBold()
779-
italic | setItalic()
780-
underline | setUnderline()
781-
strikethrough | setStrikethrough()
782-
color | getColor()
783-
size | setSize()
784-
superscript | setSuperscript()
785-
subscript | setSubscript()
787+
borderStyle | setBorderStyle()
788+
color | setColor()
786789

787790
**\PhpOffice\PhpSpreadsheet\Style\Borders**
788791

789792
Array key | Maps to property
790793
------------------|-------------------
791-
allBorders | getLeft(); getRight(); getTop(); getBottom()
792-
left | getLeft()
793-
right | getRight()
794-
top | getTop()
795-
bottom | getBottom()
796-
diagonal | getDiagonal()
797-
vertical | getVertical()
798-
horizontal | getHorizontal()
794+
allBorders | setLeft(); setRight(); setTop(); setBottom()
795+
bottom | setBottom()
796+
diagonal | setDiagonal()
799797
diagonalDirection | setDiagonalDirection()
800-
outline | setOutline()
798+
left | setLeft()
799+
right | setRight()
800+
top | setTop()
801801

802-
**\PhpOffice\PhpSpreadsheet\Style\Border**
802+
**\PhpOffice\PhpSpreadsheet\Style\Color**
803803

804804
Array key | Maps to property
805805
------------|-------------------
806-
borderStyle | setBorderStyle()
807-
color | getColor()
806+
argb | setARGB()
808807

809-
**\PhpOffice\PhpSpreadsheet\Style\Alignment**
808+
**\PhpOffice\PhpSpreadsheet\Style\Fill**
809+
810+
Array key | Maps to property
811+
-----------|-------------------
812+
color | getStartColor()
813+
endColor | getEndColor()
814+
fillType | setFillType()
815+
rotation | setRotation()
816+
startColor | getStartColor()
817+
818+
**\PhpOffice\PhpSpreadsheet\Style\Font**
810819

811820
Array key | Maps to property
812821
------------|-------------------
813-
horizontal | setHorizontal()
814-
vertical | setVertical()
815-
textRotation| setTextRotation()
816-
wrapText | setWrapText()
817-
shrinkToFit | setShrinkToFit()
818-
indent | setIndent()
822+
bold | setBold()
823+
color | getColor()
824+
italic | setItalic()
825+
name | setName()
826+
size | setSize()
827+
strikethrough | setStrikethrough()
828+
subscript | setSubscript()
829+
superscript | setSuperscript()
830+
underline | setUnderline()
819831

820832
**\PhpOffice\PhpSpreadsheet\Style\NumberFormat**
821833

src/PhpSpreadsheet/Style/Alignment.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class Alignment extends Supervisor
2828
const READORDER_LTR = 1;
2929
const READORDER_RTL = 2;
3030

31+
// Special value for Text Rotation
32+
const TEXTROTATION_STACK_EXCEL = 255;
33+
const TEXTROTATION_STACK_PHPSPREADSHEET = -165; // 90 - 255
34+
3135
/**
3236
* Horizontal alignment.
3337
*
@@ -270,12 +274,12 @@ public function getTextRotation()
270274
public function setTextRotation($pValue)
271275
{
272276
// Excel2007 value 255 => PhpSpreadsheet value -165
273-
if ($pValue == 255) {
274-
$pValue = -165;
277+
if ($pValue == self::TEXTROTATION_STACK_EXCEL) {
278+
$pValue = self::TEXTROTATION_STACK_PHPSPREADSHEET;
275279
}
276280

277281
// Set rotation
278-
if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
282+
if (($pValue >= -90 && $pValue <= 90) || $pValue == self::TEXTROTATION_STACK_PHPSPREADSHEET) {
279283
if ($this->isSupervisor) {
280284
$styleArray = $this->getStyleArray(['textRotation' => $pValue]);
281285
$this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
@@ -461,4 +465,18 @@ public function getHashCode()
461465
__CLASS__
462466
);
463467
}
468+
469+
protected function exportArray1(): array
470+
{
471+
$exportedArray = [];
472+
$this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal());
473+
$this->exportArray2($exportedArray, 'indent', $this->getIndent());
474+
$this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder());
475+
$this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit());
476+
$this->exportArray2($exportedArray, 'textRotation', $this->getTextRotation());
477+
$this->exportArray2($exportedArray, 'vertical', $this->getVertical());
478+
$this->exportArray2($exportedArray, 'wrapText', $this->getWrapText());
479+
480+
return $exportedArray;
481+
}
464482
}

src/PhpSpreadsheet/Style/Border.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,6 @@ public function __construct($isSupervisor = false, $isConditional = false)
7474
public function getSharedComponent()
7575
{
7676
switch ($this->parentPropertyName) {
77-
case 'allBorders':
78-
case 'horizontal':
79-
case 'inside':
80-
case 'outline':
81-
case 'vertical':
82-
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
83-
84-
break;
8577
case 'bottom':
8678
return $this->parent->getSharedComponent()->getBottom();
8779
case 'diagonal':
@@ -93,6 +85,8 @@ public function getSharedComponent()
9385
case 'top':
9486
return $this->parent->getSharedComponent()->getTop();
9587
}
88+
89+
throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.');
9690
}
9791

9892
/**
@@ -228,4 +222,13 @@ public function getHashCode()
228222
__CLASS__
229223
);
230224
}
225+
226+
protected function exportArray1(): array
227+
{
228+
$exportedArray = [];
229+
$this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle());
230+
$this->exportArray2($exportedArray, 'color', $this->getColor());
231+
232+
return $exportedArray;
233+
}
231234
}

src/PhpSpreadsheet/Style/Borders.php

+13
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,17 @@ public function getHashCode()
408408
__CLASS__
409409
);
410410
}
411+
412+
protected function exportArray1(): array
413+
{
414+
$exportedArray = [];
415+
$this->exportArray2($exportedArray, 'bottom', $this->getBottom());
416+
$this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal());
417+
$this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection());
418+
$this->exportArray2($exportedArray, 'left', $this->getLeft());
419+
$this->exportArray2($exportedArray, 'right', $this->getRight());
420+
$this->exportArray2($exportedArray, 'top', $this->getTop());
421+
422+
return $exportedArray;
423+
}
411424
}

src/PhpSpreadsheet/Style/Color.php

+16-23
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ public function __construct($pARGB = self::COLOR_BLACK, $isSupervisor = false, $
7171
*/
7272
public function getSharedComponent()
7373
{
74-
switch ($this->parentPropertyName) {
75-
case 'endColor':
76-
return $this->parent->getSharedComponent()->getEndColor();
77-
case 'color':
78-
return $this->parent->getSharedComponent()->getColor();
79-
case 'startColor':
80-
return $this->parent->getSharedComponent()->getStartColor();
74+
if ($this->parentPropertyName === 'endColor') {
75+
return $this->parent->getSharedComponent()->getEndColor();
8176
}
77+
if ($this->parentPropertyName === 'startColor') {
78+
return $this->parent->getSharedComponent()->getStartColor();
79+
}
80+
81+
return $this->parent->getSharedComponent()->getColor();
8282
}
8383

8484
/**
@@ -262,6 +262,7 @@ public static function getBlue($RGB, $hex = true)
262262
public static function changeBrightness($hex, $adjustPercentage)
263263
{
264264
$rgba = (strlen($hex) === 8);
265+
$adjustPercentage = max(-1.0, min(1.0, $adjustPercentage));
265266

266267
$red = self::getRed($hex, false);
267268
$green = self::getGreen($hex, false);
@@ -276,22 +277,6 @@ public static function changeBrightness($hex, $adjustPercentage)
276277
$blue += $blue * $adjustPercentage;
277278
}
278279

279-
if ($red < 0) {
280-
$red = 0;
281-
} elseif ($red > 255) {
282-
$red = 255;
283-
}
284-
if ($green < 0) {
285-
$green = 0;
286-
} elseif ($green > 255) {
287-
$green = 255;
288-
}
289-
if ($blue < 0) {
290-
$blue = 0;
291-
} elseif ($blue > 255) {
292-
$blue = 255;
293-
}
294-
295280
$rgb = strtoupper(
296281
str_pad(dechex((int) $red), 2, '0', 0) .
297282
str_pad(dechex((int) $green), 2, '0', 0) .
@@ -404,4 +389,12 @@ public function getHashCode()
404389
__CLASS__
405390
);
406391
}
392+
393+
protected function exportArray1(): array
394+
{
395+
$exportedArray = [];
396+
$this->exportArray2($exportedArray, 'argb', $this->getARGB());
397+
398+
return $exportedArray;
399+
}
407400
}

src/PhpSpreadsheet/Style/Fill.php

+11
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,15 @@ public function getHashCode()
311311
__CLASS__
312312
);
313313
}
314+
315+
protected function exportArray1(): array
316+
{
317+
$exportedArray = [];
318+
$this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
319+
$this->exportArray2($exportedArray, 'fillType', $this->getFillType());
320+
$this->exportArray2($exportedArray, 'rotation', $this->getRotation());
321+
$this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
322+
323+
return $exportedArray;
324+
}
314325
}

src/PhpSpreadsheet/Style/Font.php

+16
Original file line numberDiff line numberDiff line change
@@ -539,4 +539,20 @@ public function getHashCode()
539539
__CLASS__
540540
);
541541
}
542+
543+
protected function exportArray1(): array
544+
{
545+
$exportedArray = [];
546+
$this->exportArray2($exportedArray, 'bold', $this->getBold());
547+
$this->exportArray2($exportedArray, 'color', $this->getColor());
548+
$this->exportArray2($exportedArray, 'italic', $this->getItalic());
549+
$this->exportArray2($exportedArray, 'name', $this->getName());
550+
$this->exportArray2($exportedArray, 'size', $this->getSize());
551+
$this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough());
552+
$this->exportArray2($exportedArray, 'subscript', $this->getSubscript());
553+
$this->exportArray2($exportedArray, 'superscript', $this->getSuperscript());
554+
$this->exportArray2($exportedArray, 'underline', $this->getUnderline());
555+
556+
return $exportedArray;
557+
}
542558
}

src/PhpSpreadsheet/Style/NumberFormat.php

+8
Original file line numberDiff line numberDiff line change
@@ -870,4 +870,12 @@ public static function toFormattedString($value, $format, $callBack = null)
870870

871871
return $value;
872872
}
873+
874+
protected function exportArray1(): array
875+
{
876+
$exportedArray = [];
877+
$this->exportArray2($exportedArray, 'formatCode', $this->getFormatCode());
878+
879+
return $exportedArray;
880+
}
873881
}

src/PhpSpreadsheet/Style/Protection.php

+9
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,13 @@ public function getHashCode()
183183
__CLASS__
184184
);
185185
}
186+
187+
protected function exportArray1(): array
188+
{
189+
$exportedArray = [];
190+
$this->exportArray2($exportedArray, 'locked', $this->getLocked());
191+
$this->exportArray2($exportedArray, 'hidden', $this->getHidden());
192+
193+
return $exportedArray;
194+
}
186195
}

src/PhpSpreadsheet/Style/Style.php

+14
Original file line numberDiff line numberDiff line change
@@ -636,4 +636,18 @@ public function setIndex($pValue): void
636636
{
637637
$this->index = $pValue;
638638
}
639+
640+
protected function exportArray1(): array
641+
{
642+
$exportedArray = [];
643+
$this->exportArray2($exportedArray, 'alignment', $this->getAlignment());
644+
$this->exportArray2($exportedArray, 'borders', $this->getBorders());
645+
$this->exportArray2($exportedArray, 'fill', $this->getFill());
646+
$this->exportArray2($exportedArray, 'font', $this->getFont());
647+
$this->exportArray2($exportedArray, 'numberFormat', $this->getNumberFormat());
648+
$this->exportArray2($exportedArray, 'protection', $this->getProtection());
649+
$this->exportArray2($exportedArray, 'quotePrefx', $this->getQuotePrefix());
650+
651+
return $exportedArray;
652+
}
639653
}

0 commit comments

Comments
 (0)