diff --git a/docs/topics/recipes.md b/docs/topics/recipes.md index 86448301..db09642d 100644 --- a/docs/topics/recipes.md +++ b/docs/topics/recipes.md @@ -587,6 +587,13 @@ execution whenever you are setting more than one style property. But the difference may barely be measurable unless you have many different styles in your workbook. +You can perform the opposite function, exporting a Style as an array, +as follows: + +``` php +$styleArray = $spreadsheet->getActiveSheet()->getStyle('A3')->exportArray(); +``` + ### Number formats You often want to format numbers in Excel. For example you may want a @@ -753,69 +760,74 @@ another style array. Array key | Maps to property -------------|------------------- -fill | getFill() -font | getFont() -borders | getBorders() -alignment | getAlignment() -numberFormat | getNumberFormat() -protection | getProtection() - -**\PhpOffice\PhpSpreadsheet\Style\Fill** - -Array key | Maps to property ------------|------------------- -fillType | setFillType() -rotation | setRotation() -startColor | getStartColor() -endColor | getEndColor() -color | getStartColor() - -**\PhpOffice\PhpSpreadsheet\Style\Font** - -Array key | Maps to property -------------|------------------- -name | setName() -bold | setBold() -italic | setItalic() -underline | setUnderline() -strikethrough | setStrikethrough() -color | getColor() -size | setSize() -superscript | setSuperscript() -subscript | setSubscript() - -**\PhpOffice\PhpSpreadsheet\Style\Borders** - -Array key | Maps to property -------------------|------------------- -allBorders | getLeft(); getRight(); getTop(); getBottom() -left | getLeft() -right | getRight() -top | getTop() -bottom | getBottom() -diagonal | getDiagonal() -vertical | getVertical() -horizontal | getHorizontal() -diagonalDirection | setDiagonalDirection() -outline | setOutline() - -**\PhpOffice\PhpSpreadsheet\Style\Border** - -Array key | Maps to property -------------|------------------- -borderStyle | setBorderStyle() -color | getColor() +alignment | setAlignment() +borders | setBorders() +fill | setFill() +font | setFont() +numberFormat | setNumberFormat() +protection | setProtection() +quotePrefix | setQuotePrefix() **\PhpOffice\PhpSpreadsheet\Style\Alignment** Array key | Maps to property ------------|------------------- horizontal | setHorizontal() -vertical | setVertical() -textRotation| setTextRotation() -wrapText | setWrapText() -shrinkToFit | setShrinkToFit() indent | setIndent() +readOrder | setReadOrder() +shrinkToFit | setShrinkToFit() +textRotation| setTextRotation() +vertical | setVertical() +wrapText | setWrapText() + +**\PhpOffice\PhpSpreadsheet\Style\Border** + +Array key | Maps to property +------------|------------------- +borderStyle | setBorderStyle() +color | setColor() + +**\PhpOffice\PhpSpreadsheet\Style\Borders** + +Array key | Maps to property +------------------|------------------- +allBorders | setLeft(); setRight(); setTop(); setBottom() +bottom | setBottom() +diagonal | setDiagonal() +diagonalDirection | setDiagonalDirection() +left | setLeft() +right | setRight() +top | setTop() + +**\PhpOffice\PhpSpreadsheet\Style\Color** + +Array key | Maps to property +------------|------------------- +argb | setARGB() + +**\PhpOffice\PhpSpreadsheet\Style\Fill** + +Array key | Maps to property +-----------|------------------- +color | getStartColor() +endColor | getEndColor() +fillType | setFillType() +rotation | setRotation() +startColor | getStartColor() + +**\PhpOffice\PhpSpreadsheet\Style\Font** + +Array key | Maps to property +------------|------------------- +bold | setBold() +color | getColor() +italic | setItalic() +name | setName() +size | setSize() +strikethrough | setStrikethrough() +subscript | setSubscript() +superscript | setSuperscript() +underline | setUnderline() **\PhpOffice\PhpSpreadsheet\Style\NumberFormat** diff --git a/src/PhpSpreadsheet/Style/Alignment.php b/src/PhpSpreadsheet/Style/Alignment.php index 4d97dd2c..04a089fe 100644 --- a/src/PhpSpreadsheet/Style/Alignment.php +++ b/src/PhpSpreadsheet/Style/Alignment.php @@ -28,6 +28,10 @@ class Alignment extends Supervisor const READORDER_LTR = 1; const READORDER_RTL = 2; + // Special value for Text Rotation + const TEXTROTATION_STACK_EXCEL = 255; + const TEXTROTATION_STACK_PHPSPREADSHEET = -165; // 90 - 255 + /** * Horizontal alignment. * @@ -270,12 +274,12 @@ class Alignment extends Supervisor public function setTextRotation($pValue) { // Excel2007 value 255 => PhpSpreadsheet value -165 - if ($pValue == 255) { - $pValue = -165; + if ($pValue == self::TEXTROTATION_STACK_EXCEL) { + $pValue = self::TEXTROTATION_STACK_PHPSPREADSHEET; } // Set rotation - if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) { + if (($pValue >= -90 && $pValue <= 90) || $pValue == self::TEXTROTATION_STACK_PHPSPREADSHEET) { if ($this->isSupervisor) { $styleArray = $this->getStyleArray(['textRotation' => $pValue]); $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray); @@ -461,4 +465,18 @@ class Alignment extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'horizontal', $this->getHorizontal()); + $this->exportArray2($exportedArray, 'indent', $this->getIndent()); + $this->exportArray2($exportedArray, 'readOrder', $this->getReadOrder()); + $this->exportArray2($exportedArray, 'shrinkToFit', $this->getShrinkToFit()); + $this->exportArray2($exportedArray, 'textRotation', $this->getTextRotation()); + $this->exportArray2($exportedArray, 'vertical', $this->getVertical()); + $this->exportArray2($exportedArray, 'wrapText', $this->getWrapText()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Border.php b/src/PhpSpreadsheet/Style/Border.php index 78ad8b26..1d3096f0 100644 --- a/src/PhpSpreadsheet/Style/Border.php +++ b/src/PhpSpreadsheet/Style/Border.php @@ -74,14 +74,6 @@ class Border extends Supervisor public function getSharedComponent() { switch ($this->parentPropertyName) { - case 'allBorders': - case 'horizontal': - case 'inside': - case 'outline': - case 'vertical': - throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'); - - break; case 'bottom': return $this->parent->getSharedComponent()->getBottom(); case 'diagonal': @@ -93,6 +85,8 @@ class Border extends Supervisor case 'top': return $this->parent->getSharedComponent()->getTop(); } + + throw new PhpSpreadsheetException('Cannot get shared component for a pseudo-border.'); } /** @@ -228,4 +222,13 @@ class Border extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'borderStyle', $this->getBorderStyle()); + $this->exportArray2($exportedArray, 'color', $this->getColor()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Borders.php b/src/PhpSpreadsheet/Style/Borders.php index e75d7eeb..a1acfdd4 100644 --- a/src/PhpSpreadsheet/Style/Borders.php +++ b/src/PhpSpreadsheet/Style/Borders.php @@ -408,4 +408,17 @@ class Borders extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'bottom', $this->getBottom()); + $this->exportArray2($exportedArray, 'diagonal', $this->getDiagonal()); + $this->exportArray2($exportedArray, 'diagonalDirection', $this->getDiagonalDirection()); + $this->exportArray2($exportedArray, 'left', $this->getLeft()); + $this->exportArray2($exportedArray, 'right', $this->getRight()); + $this->exportArray2($exportedArray, 'top', $this->getTop()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Color.php b/src/PhpSpreadsheet/Style/Color.php index d8ba08b2..ad598f11 100644 --- a/src/PhpSpreadsheet/Style/Color.php +++ b/src/PhpSpreadsheet/Style/Color.php @@ -71,14 +71,14 @@ class Color extends Supervisor */ public function getSharedComponent() { - switch ($this->parentPropertyName) { - case 'endColor': - return $this->parent->getSharedComponent()->getEndColor(); - case 'color': - return $this->parent->getSharedComponent()->getColor(); - case 'startColor': - return $this->parent->getSharedComponent()->getStartColor(); + if ($this->parentPropertyName === 'endColor') { + return $this->parent->getSharedComponent()->getEndColor(); } + if ($this->parentPropertyName === 'startColor') { + return $this->parent->getSharedComponent()->getStartColor(); + } + + return $this->parent->getSharedComponent()->getColor(); } /** @@ -262,6 +262,7 @@ class Color extends Supervisor public static function changeBrightness($hex, $adjustPercentage) { $rgba = (strlen($hex) === 8); + $adjustPercentage = max(-1.0, min(1.0, $adjustPercentage)); $red = self::getRed($hex, false); $green = self::getGreen($hex, false); @@ -276,22 +277,6 @@ class Color extends Supervisor $blue += $blue * $adjustPercentage; } - if ($red < 0) { - $red = 0; - } elseif ($red > 255) { - $red = 255; - } - if ($green < 0) { - $green = 0; - } elseif ($green > 255) { - $green = 255; - } - if ($blue < 0) { - $blue = 0; - } elseif ($blue > 255) { - $blue = 255; - } - $rgb = strtoupper( str_pad(dechex((int) $red), 2, '0', 0) . str_pad(dechex((int) $green), 2, '0', 0) . @@ -404,4 +389,12 @@ class Color extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'argb', $this->getARGB()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Fill.php b/src/PhpSpreadsheet/Style/Fill.php index c6baeed0..3891bc47 100644 --- a/src/PhpSpreadsheet/Style/Fill.php +++ b/src/PhpSpreadsheet/Style/Fill.php @@ -311,4 +311,15 @@ class Fill extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'endColor', $this->getEndColor()); + $this->exportArray2($exportedArray, 'fillType', $this->getFillType()); + $this->exportArray2($exportedArray, 'rotation', $this->getRotation()); + $this->exportArray2($exportedArray, 'startColor', $this->getStartColor()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Font.php b/src/PhpSpreadsheet/Style/Font.php index a062a38f..ad405708 100644 --- a/src/PhpSpreadsheet/Style/Font.php +++ b/src/PhpSpreadsheet/Style/Font.php @@ -539,4 +539,20 @@ class Font extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'bold', $this->getBold()); + $this->exportArray2($exportedArray, 'color', $this->getColor()); + $this->exportArray2($exportedArray, 'italic', $this->getItalic()); + $this->exportArray2($exportedArray, 'name', $this->getName()); + $this->exportArray2($exportedArray, 'size', $this->getSize()); + $this->exportArray2($exportedArray, 'strikethrough', $this->getStrikethrough()); + $this->exportArray2($exportedArray, 'subscript', $this->getSubscript()); + $this->exportArray2($exportedArray, 'superscript', $this->getSuperscript()); + $this->exportArray2($exportedArray, 'underline', $this->getUnderline()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 259acabf..0b761bd3 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -870,4 +870,12 @@ class NumberFormat extends Supervisor return $value; } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'formatCode', $this->getFormatCode()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Protection.php b/src/PhpSpreadsheet/Style/Protection.php index f695837d..9ae7d4bb 100644 --- a/src/PhpSpreadsheet/Style/Protection.php +++ b/src/PhpSpreadsheet/Style/Protection.php @@ -183,4 +183,13 @@ class Protection extends Supervisor __CLASS__ ); } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'locked', $this->getLocked()); + $this->exportArray2($exportedArray, 'hidden', $this->getHidden()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index 533a7c38..c1aa319e 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -636,4 +636,18 @@ class Style extends Supervisor { $this->index = $pValue; } + + protected function exportArray1(): array + { + $exportedArray = []; + $this->exportArray2($exportedArray, 'alignment', $this->getAlignment()); + $this->exportArray2($exportedArray, 'borders', $this->getBorders()); + $this->exportArray2($exportedArray, 'fill', $this->getFill()); + $this->exportArray2($exportedArray, 'font', $this->getFont()); + $this->exportArray2($exportedArray, 'numberFormat', $this->getNumberFormat()); + $this->exportArray2($exportedArray, 'protection', $this->getProtection()); + $this->exportArray2($exportedArray, 'quotePrefx', $this->getQuotePrefix()); + + return $exportedArray; + } } diff --git a/src/PhpSpreadsheet/Style/Supervisor.php b/src/PhpSpreadsheet/Style/Supervisor.php index 1a700974..7f655bef 100644 --- a/src/PhpSpreadsheet/Style/Supervisor.php +++ b/src/PhpSpreadsheet/Style/Supervisor.php @@ -114,4 +114,45 @@ abstract class Supervisor implements IComparable } } } + + /** + * Export style as array. + * + * Available to anything which extends this class: + * Alignment, Border, Borders, Color, Fill, Font, + * NumberFormat, Protection, and Style. + */ + final public function exportArray(): array + { + return $this->exportArray1(); + } + + /** + * Abstract method to be implemented in anything which + * extends this class. + * + * This method invokes exportArray2 with the names and values + * of all properties to be included in output array, + * returning that array to exportArray, then to caller. + */ + abstract protected function exportArray1(): array; + + /** + * Populate array from exportArray1. + * This method is available to anything which extends this class. + * The parameter index is the key to be added to the array. + * The parameter objOrValue is either a primitive type, + * which is the value added to the array, + * or a Style object to be recursively added via exportArray. + * + * @param mixed $objOrValue + */ + final protected function exportArray2(array &$exportedArray, string $index, $objOrValue): void + { + if ($objOrValue instanceof self) { + $exportedArray[$index] = $objOrValue->exportArray(); + } else { + $exportedArray[$index] = $objOrValue; + } + } } diff --git a/tests/PhpSpreadsheetTests/Style/AlignmentTest.php b/tests/PhpSpreadsheetTests/Style/AlignmentTest.php new file mode 100644 index 00000000..d10e3211 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/AlignmentTest.php @@ -0,0 +1,94 @@ +getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('Cell1'); + $cell1->getStyle()->getAlignment()->setTextRotation(45); + self::assertEquals(45, $cell1->getStyle()->getAlignment()->getTextRotation()); + $cell2 = $sheet->getCell('A2'); + $cell2->setValue('Cell2'); + $cell2->getStyle()->getAlignment()->setTextRotation(-45); + self::assertEquals(-45, $cell2->getStyle()->getAlignment()->getTextRotation()); + // special value for stacked + $cell3 = $sheet->getCell('A3'); + $cell3->setValue('Cell3'); + $cell3->getStyle()->getAlignment()->setTextRotation(255); + self::assertEquals(-165, $cell3->getStyle()->getAlignment()->getTextRotation()); + } + + public function testRotationTooHigh(): void + { + $this->expectException(PhpSpreadsheetException::class); + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('Cell1'); + $cell1->getStyle()->getAlignment()->setTextRotation(91); + self::assertEquals(0, $cell1->getStyle()->getAlignment()->getTextRotation()); + } + + public function testRotationTooLow(): void + { + $this->expectException(PhpSpreadsheetException::class); + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('Cell1'); + $cell1->getStyle()->getAlignment()->setTextRotation(-91); + self::assertEquals(0, $cell1->getStyle()->getAlignment()->getTextRotation()); + } + + public function testHorizontal(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('X'); + $cell1->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setIndent(1); + self::assertEquals(Alignment::HORIZONTAL_LEFT, $cell1->getStyle()->getAlignment()->getHorizontal()); + self::assertEquals(1, $cell1->getStyle()->getAlignment()->getIndent()); + $cell2 = $sheet->getCell('A2'); + $cell2->setValue('Y'); + $cell2->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT)->setIndent(2); + self::assertEquals(Alignment::HORIZONTAL_RIGHT, $cell2->getStyle()->getAlignment()->getHorizontal()); + self::assertEquals(2, $cell2->getStyle()->getAlignment()->getIndent()); + $cell3 = $sheet->getCell('A3'); + $cell3->setValue('Z'); + // indent not supported for next style - changed to 0 + $cell3->getStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER_CONTINUOUS)->setIndent(3); + self::assertEquals(Alignment::HORIZONTAL_CENTER_CONTINUOUS, $cell3->getStyle()->getAlignment()->getHorizontal()); + self::assertEquals(0, $cell3->getStyle()->getAlignment()->getIndent()); + } + + public function testReadOrder(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('ABC'); + $cell1->getStyle()->getAlignment()->setReadOrder(0); + self::assertEquals(0, $cell1->getStyle()->getAlignment()->getReadOrder()); + $cell1->getStyle()->getAlignment()->setReadOrder(1); + self::assertEquals(1, $cell1->getStyle()->getAlignment()->getReadOrder()); + // following not supported - zero is used instead + $cell1->getStyle()->getAlignment()->setReadOrder(-1); + self::assertEquals(0, $cell1->getStyle()->getAlignment()->getReadOrder()); + $cell1->getStyle()->getAlignment()->setReadOrder(2); + self::assertEquals(2, $cell1->getStyle()->getAlignment()->getReadOrder()); + // following not supported - zero is used instead + $cell1->getStyle()->getAlignment()->setReadOrder(3); + self::assertEquals(0, $cell1->getStyle()->getAlignment()->getReadOrder()); + } +} diff --git a/tests/PhpSpreadsheetTests/Style/BorderTest.php b/tests/PhpSpreadsheetTests/Style/BorderTest.php index f1f1bf56..cf6cd3a9 100644 --- a/tests/PhpSpreadsheetTests/Style/BorderTest.php +++ b/tests/PhpSpreadsheetTests/Style/BorderTest.php @@ -2,13 +2,16 @@ namespace PhpOffice\PhpSpreadsheetTests\Style; +use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Border; +use PhpOffice\PhpSpreadsheet\Style\Borders; +use PhpOffice\PhpSpreadsheet\Style\Color; use PHPUnit\Framework\TestCase; class BorderTest extends TestCase { - public function testCase(): void + public function testAllBorders(): void { $spreadsheet = new Spreadsheet(); $borders = $spreadsheet->getActiveSheet()->getStyle('A1')->getBorders(); @@ -22,5 +25,223 @@ class BorderTest extends TestCase $actual = $bottom->getBorderStyle(); self::assertSame(Border::BORDER_THIN, $actual, 'should have been set via allBorders'); + self::assertSame(Border::BORDER_THIN, $borders->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle()); + } + + public function testAllBordersArray(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getStyle('A1')->getBorders()->applyFromArray(['allBorders' => ['borderStyle' => Border::BORDER_THIN]]); + $borders = $sheet->getCell('A1')->getStyle()->getBorders(); + + self::assertSame(Border::BORDER_THIN, $borders->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle()); + } + + public function testAllBordersArrayNotSupervisor(): void + { + $borders = new Borders(); + $borders->applyFromArray(['allBorders' => ['borderStyle' => Border::BORDER_THIN]]); + + self::assertSame(Border::BORDER_THIN, $borders->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $borders->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $borders->getDiagonal()->getBorderStyle()); + } + + public function testOutline(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $borders = $sheet->getStyle('A1:B2')->getBorders(); + $outline = $borders->getOutline(); + $outline->setBorderStyle(Border::BORDER_THIN); + + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + } + + public function testInside(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $borders = $sheet->getStyle('A1:B2')->getBorders(); + $inside = $borders->getInside(); + $inside->setBorderStyle(Border::BORDER_THIN); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + } + + public function testHorizontal(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $borders = $sheet->getStyle('A1:B2')->getBorders(); + $horizontal = $borders->getHorizontal(); + $horizontal->setBorderStyle(Border::BORDER_THIN); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + } + + public function testVertical(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $borders = $sheet->getStyle('A1:B2')->getBorders(); + $vertical = $borders->getVertical(); + $vertical->setBorderStyle(Border::BORDER_THIN); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('A2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('A2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B1')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B1')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertSame(Border::BORDER_THIN, $sheet->getCell('B2')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertSame(Border::BORDER_NONE, $sheet->getCell('B2')->getStyle()->getBorders()->getTop()->getBorderStyle()); + } + + public function testNoSupervisorAllBorders(): void + { + $this->expectException(PhpSpreadsheetException::class); + $borders = new Borders(); + $borders->getAllBorders(); + } + + public function testNoSupervisorOutline(): void + { + $this->expectException(PhpSpreadsheetException::class); + $borders = new Borders(); + $borders->getOutline(); + } + + public function testNoSupervisorInside(): void + { + $this->expectException(PhpSpreadsheetException::class); + $borders = new Borders(); + $borders->getInside(); + } + + public function testNoSupervisorVertical(): void + { + $this->expectException(PhpSpreadsheetException::class); + $borders = new Borders(); + $borders->getVertical(); + } + + public function testNoSupervisorHorizontal(): void + { + $this->expectException(PhpSpreadsheetException::class); + $borders = new Borders(); + $borders->getHorizontal(); + } + + public function testGetSharedComponentPseudo(): void + { + $this->expectException(PhpSpreadsheetException::class); + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getStyle('A1')->getBorders()->getHorizontal()->setBorderStyle(Border::BORDER_MEDIUM); + $sheet->getStyle('A1')->getBorders()->getHorizontal()->getSharedComponent(); + } + + public function testBorderStyle(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getStyle('A1')->getBorders()->getTop()->setBorderStyle(false); + $sheet->getStyle('A2')->getBorders()->getTop()->setBorderStyle(true); + self::assertEquals(Border::BORDER_NONE, $sheet->getStyle('A1')->getBorders()->getTop()->getBorderStyle()); + self::assertEquals(Border::BORDER_MEDIUM, $sheet->getStyle('A2')->getBorders()->getTop()->getBorderStyle()); + $sheet->getStyle('A3')->getBorders()->getTop()->applyFromArray(['borderStyle' => Border::BORDER_MEDIUM]); + self::assertEquals(Border::BORDER_MEDIUM, $sheet->getStyle('A3')->getBorders()->getTop()->getBorderStyle()); + $border = new Border(); + $border->setBorderStyle(Border::BORDER_THIN)->setColor(new Color('FFFF0000')); + self::assertEquals('FFFF0000', $border->getColor()->getARGB()); + self::assertEquals(Border::BORDER_THIN, $border->getBorderStyle()); + } + + public function testDiagonalDirection(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getStyle('A1')->getBorders()->getDiagonal()->setBorderStyle(Border::BORDER_MEDIUM); + $sheet->getStyle('A1')->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH); + $borders = $sheet->getStyle('A1')->getBorders(); + + self::assertSame(Border::BORDER_MEDIUM, $borders->getDiagonal()->getBorderStyle()); + self::assertSame(Borders::DIAGONAL_BOTH, $borders->getDiagonalDirection()); } } diff --git a/tests/PhpSpreadsheetTests/Style/ColorTest.php b/tests/PhpSpreadsheetTests/Style/ColorTest.php index 9b524e93..04028f87 100644 --- a/tests/PhpSpreadsheetTests/Style/ColorTest.php +++ b/tests/PhpSpreadsheetTests/Style/ColorTest.php @@ -70,4 +70,21 @@ class ColorTest extends TestCase { return require 'tests/data/Style/ColorChangeBrightness.php'; } + + public function testDefaultColor(): void + { + $color = new Color(); + $color->setARGB('FFFF0000'); + self::assertEquals('FFFF0000', $color->getARGB()); + self::assertEquals('FF0000', $color->getRGB()); + $color->setARGB(''); + self::assertEquals(Color::COLOR_BLACK, $color->getARGB()); + self::assertEquals('000000', $color->getRGB()); + $color->setARGB('FFFF0000'); + self::assertEquals('FFFF0000', $color->getARGB()); + self::assertEquals('FF0000', $color->getRGB()); + $color->setRGB(''); + self::assertEquals(Color::COLOR_BLACK, $color->getARGB()); + self::assertEquals('000000', $color->getRGB()); + } } diff --git a/tests/PhpSpreadsheetTests/Style/ConditionalTest.php b/tests/PhpSpreadsheetTests/Style/ConditionalTest.php new file mode 100644 index 00000000..064a52f1 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/ConditionalTest.php @@ -0,0 +1,52 @@ +setConditionType(Conditional::CONDITION_CELLIS); + $condition1->setOperatorType(Conditional::OPERATOR_LESSTHAN); + $condition1->addCondition(0.6); + $condition1->getStyle()->getFill() + ->setFillType(Fill::FILL_SOLID) + ->getEndColor()->setARGB('FFFF0000'); + $conditionclone = clone $condition1; + self::AssertEquals($condition1, $conditionclone); + self::AssertEquals($condition1->getStyle(), $conditionclone->getStyle()); + self::AssertNotSame($condition1->getStyle(), $conditionclone->getStyle()); + } + + public function testVariousAdds(): void + { + $condition1 = new Conditional(); + $condition1->setConditionType(Conditional::CONDITION_CELLIS); + $condition1->setOperatorType(Conditional::OPERATOR_LESSTHAN); + $condition1->addCondition(0.6); + $condition1->getStyle()->getFill() + ->setFillType(Fill::FILL_SOLID) + ->getEndColor()->setARGB('FFFF0000'); + $condition2 = new Conditional(); + $condition2->setConditionType(Conditional::CONDITION_CELLIS); + $condition2->setOperatorType(Conditional::OPERATOR_LESSTHAN); + $condition2->setConditions(0.6); + $condition2->getStyle()->getFill() + ->setFillType(Fill::FILL_SOLID) + ->getEndColor()->setARGB('FFFF0000'); + $condition3 = new Conditional(); + $condition3->setConditionType(Conditional::CONDITION_CELLIS); + $condition3->setOperatorType(Conditional::OPERATOR_LESSTHAN); + $condition3->setConditions([0.6]); + $condition3->getStyle()->getFill() + ->setFillType(Fill::FILL_SOLID) + ->getEndColor()->setARGB('FFFF0000'); + self::AssertEquals($condition1, $condition2); + self::AssertEquals($condition1, $condition3); + } +} diff --git a/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php b/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php new file mode 100644 index 00000000..e11e5f29 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php @@ -0,0 +1,161 @@ +getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('Cell A1'); + $cell1style = $cell1->getStyle(); + $cell1style->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT); + $cell1style->getFont()->getColor()->setARGB('FFFF0000'); + $cell1style->getFont()->setBold(true); + $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125); + $cell1style->getFill()->setStartColor(new Color('FF0000FF')); + $cell1style->getFill()->setEndColor(new Color('FF00FF00')); + $cell1style->getFont()->setUnderline(true); + self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline()); + $cell1style->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED); + $cell1style->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED); + $styleArray = $cell1style->exportArray(); + $cell2 = $sheet->getCell('B1'); + $cell2->setValue('Cell B1'); + $cell2style = $cell2->getStyle(); + $cell2style->applyFromArray($styleArray); + + self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal()); + self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB()); + self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold()); + self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline()); + self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType()); + self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB()); + self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB()); + self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked()); + self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden()); + + self::AssertEquals($cell1style->getHashCode(), $cell2style->getHashCode()); + self::AssertEquals($cell1style->getAlignment()->getHashCode(), $cell2style->getAlignment()->getHashCode()); + self::AssertEquals($cell1style->getFont()->getHashCode(), $cell2style->getFont()->getHashCode()); + self::AssertEquals($cell1style->getFill()->getHashCode(), $cell2style->getFill()->getHashCode()); + self::AssertEquals($cell1style->getProtection()->getHashCode(), $cell2style->getProtection()->getHashCode()); + } + + public function testStyleFromArrayCopy(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('Cell A1'); + $cell1style = $cell1->getStyle(); + $cell1style->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]); + $cell1style->getFont()->getColor()->setARGB('FFFF0000'); + $cell1style->getFont()->applyFromArray(['bold' => true]); + $cell1style->getFill()->applyFromArray(['fillType' => Fill::FILL_PATTERN_GRAY125]); + $cell1style->getFill()->getStartColor()->applyFromArray(['argb' => 'FF0000FF']); + $cell1style->getFill()->getEndColor()->setRGB('00FF00'); + $cell1style->getFill()->setRotation(45); + $cell1style->getFont()->setUnderline(true); + self::assertEquals(Font::UNDERLINE_SINGLE, $cell1style->getFont()->getUnderline()); + $cell1style->getProtection()->applyFromArray(['hidden' => Protection::PROTECTION_UNPROTECTED, 'locked' => Protection::PROTECTION_UNPROTECTED]); + $styleArray = $cell1style->exportArray(); + $cell2 = $sheet->getCell('B1'); + $cell2->setValue('Cell B1'); + $cell2style = $cell2->getStyle(); + $cell2style->applyFromArray($styleArray); + + self::AssertEquals($cell1style->getAlignment()->getHorizontal(), $cell2style->getAlignment()->getHorizontal()); + self::AssertEquals($cell1style->getFont()->getColor()->getARGB(), $cell2style->getFont()->getColor()->getARGB()); + self::AssertEquals($cell1style->getFont()->getBold(), $cell2style->getFont()->getBold()); + self::AssertEquals($cell1style->getFont()->getUnderline(), $cell2style->getFont()->getUnderline()); + self::AssertEquals($cell1style->getFill()->getFillType(), $cell2style->getFill()->getFillType()); + self::AssertEquals($cell1style->getFill()->getRotation(), $cell2style->getFill()->getRotation()); + self::AssertEquals($cell1style->getFill()->getStartColor()->getARGB(), $cell2style->getFill()->getStartColor()->getARGB()); + self::AssertEquals($cell1style->getFill()->getEndColor()->getARGB(), $cell2style->getFill()->getEndColor()->getARGB()); + self::AssertEquals($cell1style->getProtection()->getLocked(), $cell2style->getProtection()->getLocked()); + self::AssertEquals($cell1style->getProtection()->getHidden(), $cell2style->getProtection()->getHidden()); + + self::AssertEquals($cell1style->getFill()->getStartColor()->getHashCode(), $cell2style->getFill()->getStartColor()->getHashCode()); + self::AssertEquals($cell1style->getFill()->getEndColor()->getHashCode(), $cell2style->getFill()->getEndColor()->getHashCode()); + } + + public function testNumberFormat(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1style = $cell1->getStyle(); + $fmt2 = '$ #,##0.000'; + $cell1style->getNumberFormat()->setFormatCode($fmt2); + $cell1style->getFont()->setUnderline(''); + self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline()); + $cell1->setValue(2345.679); + $styleArray = $cell1style->exportArray(); + self::assertEquals('$ 2,345.679', $cell1->getFormattedValue()); + + $cell2 = $sheet->getCell('B1'); + $cell2->setValue(12345.679); + $cell2style = $cell2->getStyle(); + $cell2style->applyFromArray($styleArray); + self::assertEquals('$ 12,345.679', $cell2->getFormattedValue()); + + self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode()); + } + + public function testNumberFormatFromArray(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1style = $cell1->getStyle(); + $fmt2 = '$ #,##0.000'; + $cell1style->getNumberFormat()->applyFromArray(['formatCode' => $fmt2]); + $cell1style->getFont()->setUnderline(''); + self::assertEquals(Font::UNDERLINE_NONE, $cell1style->getFont()->getUnderline()); + $cell1style->getBorders()->getTop()->setBorderStyle(Border::BORDER_THIN); + $cell1->setValue(2345.679); + $styleArray = $cell1style->exportArray(); + self::assertEquals('$ 2,345.679', $cell1->getFormattedValue()); + + $cell2 = $sheet->getCell('B1'); + $cell2->setValue(12345.679); + $cell2style = $cell2->getStyle(); + $cell2style->applyFromArray($styleArray); + self::assertEquals('$ 12,345.679', $cell2->getFormattedValue()); + + self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode()); + self::AssertEquals($cell1style->getBorders()->getHashCode(), $cell2style->getBorders()->getHashCode()); + self::AssertEquals($cell1style->getBorders()->getTop()->getHashCode(), $cell2style->getBorders()->getTop()->getHashCode()); + self::AssertEquals($cell1style->getBorders()->getTop()->getBorderStyle(), $cell2style->getBorders()->getTop()->getBorderStyle()); + } + + public function testStackedRotation(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue('Cell A1'); + $cell1style = $cell1->getStyle(); + $cell1style->getAlignment()->setTextRotation(Alignment::TEXTROTATION_STACK_EXCEL); + self::assertEquals(Alignment::TEXTROTATION_STACK_PHPSPREADSHEET, $cell1style->getAlignment()->getTextRotation()); + $styleArray = $cell1style->exportArray(); + $cell2 = $sheet->getCell('B1'); + $cell2->setValue('Cell B1'); + $cell2style = $cell2->getStyle(); + $cell2style->applyFromArray($styleArray); + + self::AssertEquals($cell1style->getAlignment()->getTextRotation(), $cell2style->getAlignment()->getTextRotation()); + } +} diff --git a/tests/PhpSpreadsheetTests/Style/FillTest.php b/tests/PhpSpreadsheetTests/Style/FillTest.php new file mode 100644 index 00000000..18ee224d --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/FillTest.php @@ -0,0 +1,22 @@ +setFillType(Fill::FILL_PATTERN_GRAY125); + $fill->setStartColor($startColor); + $fill->setEndColor($endColor); + self::assertEquals('FF0000', $fill->getStartColor()->getRGB()); + self::assertEquals('00FFFF', $fill->getEndColor()->getRGB()); + } +} diff --git a/tests/PhpSpreadsheetTests/Style/NumberFormatBuiltinTest.php b/tests/PhpSpreadsheetTests/Style/NumberFormatBuiltinTest.php new file mode 100644 index 00000000..dd3d672a --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/NumberFormatBuiltinTest.php @@ -0,0 +1,35 @@ +getActiveSheet(); + $cell1 = $sheet->getCell('A1'); + $cell1->setValue(1); + $cell1->getStyle()->getNumberFormat()->setBuiltInFormatCode(2); // 0.00 + self::assertEquals('1.00', $cell1->getFormattedValue()); + $cell2 = $sheet->getCell('A2'); + $cell2->setValue(1234); + $cell2->getStyle()->getNumberFormat()->setFormatCode('#,##0'); // builtin 3 + self::assertEquals(3, $cell2->getStyle()->getNumberFormat()->getBuiltinFormatCode()); + self::assertEquals('1,234', $cell2->getFormattedValue()); + $cell3 = $sheet->getCell('A3'); + $cell3->setValue(1234); + $cell3->getStyle()->getNumberFormat()->setFormatCode(''); // General + self::assertEquals(NumberFormat::FORMAT_GENERAL, $cell3->getStyle()->getNumberFormat()->getFormatCode()); + self::assertEquals(0, $cell3->getStyle()->getNumberFormat()->getBuiltinFormatCode()); + self::assertEquals('1234', $cell3->getFormattedValue()); + // non-supervisor + $numberFormat = new NumberFormat(); + $numberFormat->setBuiltInFormatCode(4); + self::assertEquals('#,##0.00', $numberFormat->getFormatCode()); + } +} diff --git a/tests/PhpSpreadsheetTests/Style/StyleTest.php b/tests/PhpSpreadsheetTests/Style/StyleTest.php new file mode 100644 index 00000000..b695a50b --- /dev/null +++ b/tests/PhpSpreadsheetTests/Style/StyleTest.php @@ -0,0 +1,22 @@ +getActiveSheet(); + $cellCoordinate = 'A1'; + $cell1 = $sheet->getCell($cellCoordinate); + $cell1style = $cell1->getStyle(); + self::assertSame($spreadsheet, $cell1style->getParent()); + $styleArray = ['alignment' => ['textRotation' => 45]]; + $outArray = $cell1style->getStyleArray($styleArray); + self::assertEquals($styleArray, $outArray['quotePrefix']); + } +}