diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 23b3d630..36e9eacf 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5510,66 +5510,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Spreadsheet.php - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Alignment.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Border.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getStyleArray\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Border.php - - - - message: "#^Parameter \\#1 \\$parent of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Supervisor\\:\\:bindParent\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style, \\$this\\(PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Border\\) given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Border.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Borders.php - - - - message: "#^Parameter \\#1 \\$parent of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Supervisor\\:\\:bindParent\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style, \\$this\\(PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Borders\\) given\\.$#" - count: 10 - path: src/PhpSpreadsheet/Style/Borders.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Color.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getStyleArray\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Color.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Border\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Fill\\:\\:getColor\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Color.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Border\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Fill\\:\\:getEndColor\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Color.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Border\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Fill\\:\\:getStartColor\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Color.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Color\\:\\:getColourComponent\\(\\) should return int\\|string but returns float\\|int\\|string\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Color.php - - message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Conditional\\:\\:\\$condition \\(array\\\\) does not accept array\\\\.$#" count: 1 @@ -5740,36 +5680,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Style/ConditionalFormatting/ConditionalFormattingRuleExtension.php - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Fill.php - - - - message: "#^Parameter \\#1 \\$parent of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Supervisor\\:\\:bindParent\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style, \\$this\\(PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Fill\\) given\\.$#" - count: 2 - path: src/PhpSpreadsheet/Style/Fill.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Font.php - - - - message: "#^Parameter \\#1 \\$parent of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Supervisor\\:\\:bindParent\\(\\) expects PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style, \\$this\\(PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\) given\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Font.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat.php - - - - message: "#^Property PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\:\\:\\$builtInFormatCode \\(int\\|false\\) does not accept bool\\|int\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/NumberFormat.php - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\NumberFormat\\\\DateFormatter\\:\\:escapeQuotesCallback\\(\\) has parameter \\$matches with no type specified\\.$#" count: 1 @@ -5900,21 +5810,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Style/NumberFormat/PercentageFormatter.php - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getSharedComponent\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Protection.php - - - - message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getCellXfByIndex\\(\\)\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Style.php - - - - message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\:\\:getParent\\(\\) should return PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet but returns PhpOffice\\\\PhpSpreadsheet\\\\Spreadsheet\\|PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Style\\.$#" - count: 1 - path: src/PhpSpreadsheet/Style/Style.php - - message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#" count: 1 diff --git a/samples/Basic/47_xlsfill.php b/samples/Basic/47_xlsfill.php new file mode 100644 index 00000000..217d7dca --- /dev/null +++ b/samples/Basic/47_xlsfill.php @@ -0,0 +1,13 @@ +log('Read spreadsheet'); +$reader = new Xls(); +$spreadsheet = $reader->load(__DIR__ . '/../templates/47_xlsfill.xls'); + +// Save +$helper->write($spreadsheet, __FILE__, ['Xls']); +$spreadsheet->disconnectWorksheets(); diff --git a/samples/Basic/47_xlsxfill.php b/samples/Basic/47_xlsxfill.php new file mode 100644 index 00000000..c19d789d --- /dev/null +++ b/samples/Basic/47_xlsxfill.php @@ -0,0 +1,13 @@ +log('Read spreadsheet'); +$reader = new Xlsx(); +$spreadsheet = $reader->load(__DIR__ . '/../templates/47_xlsxfill.xlsx'); + +// Save +$helper->write($spreadsheet, __FILE__, ['Xlsx']); +$spreadsheet->disconnectWorksheets(); diff --git a/samples/templates/47_xlsfill.xls b/samples/templates/47_xlsfill.xls new file mode 100644 index 00000000..71887217 Binary files /dev/null and b/samples/templates/47_xlsfill.xls differ diff --git a/samples/templates/47_xlsxfill.xlsx b/samples/templates/47_xlsxfill.xlsx new file mode 100644 index 00000000..ace23614 Binary files /dev/null and b/samples/templates/47_xlsxfill.xlsx differ diff --git a/src/PhpSpreadsheet/Style/Alignment.php b/src/PhpSpreadsheet/Style/Alignment.php index 1b3ed6ac..83ac5b0d 100644 --- a/src/PhpSpreadsheet/Style/Alignment.php +++ b/src/PhpSpreadsheet/Style/Alignment.php @@ -111,7 +111,10 @@ class Alignment extends Supervisor */ public function getSharedComponent() { - return $this->parent->getSharedComponent()->getAlignment(); + /** @var Style */ + $parent = $this->parent; + + return $parent->getSharedComponent()->getAlignment(); } /** diff --git a/src/PhpSpreadsheet/Style/Border.php b/src/PhpSpreadsheet/Style/Border.php index 9fabf366..a5ec980a 100644 --- a/src/PhpSpreadsheet/Style/Border.php +++ b/src/PhpSpreadsheet/Style/Border.php @@ -70,8 +70,11 @@ class Border extends Supervisor */ public function getSharedComponent() { + /** @var Style */ + $parent = $this->parent; + /** @var Borders $sharedComponent */ - $sharedComponent = $this->parent->getSharedComponent(); + $sharedComponent = $parent->getSharedComponent(); switch ($this->parentPropertyName) { case 'bottom': return $sharedComponent->getBottom(); @@ -97,7 +100,10 @@ class Border extends Supervisor */ public function getStyleArray($array) { - return $this->parent->getStyleArray([$this->parentPropertyName => $array]); + /** @var Style */ + $parent = $this->parent; + + return $parent->getStyleArray([$this->parentPropertyName => $array]); } /** diff --git a/src/PhpSpreadsheet/Style/Borders.php b/src/PhpSpreadsheet/Style/Borders.php index 5d92f935..56a52709 100644 --- a/src/PhpSpreadsheet/Style/Borders.php +++ b/src/PhpSpreadsheet/Style/Borders.php @@ -140,7 +140,10 @@ class Borders extends Supervisor */ public function getSharedComponent() { - return $this->parent->getSharedComponent()->getBorders(); + /** @var Style */ + $parent = $this->parent; + + return $parent->getSharedComponent()->getBorders(); } /** diff --git a/src/PhpSpreadsheet/Style/Color.php b/src/PhpSpreadsheet/Style/Color.php index d9b9830c..c2d4f749 100644 --- a/src/PhpSpreadsheet/Style/Color.php +++ b/src/PhpSpreadsheet/Style/Color.php @@ -45,6 +45,9 @@ class Color extends Supervisor */ protected $argb; + /** @var bool */ + private $hasChanged = false; + /** * Create a new Color. * @@ -75,12 +78,15 @@ class Color extends Supervisor */ public function getSharedComponent() { + /** @var Style */ + $parent = $this->parent; /** @var Border|Fill $sharedComponent */ - $sharedComponent = $this->parent->getSharedComponent(); - if ($this->parentPropertyName === 'endColor') { - return $sharedComponent->getEndColor(); - } - if ($this->parentPropertyName === 'startColor') { + $sharedComponent = $parent->getSharedComponent(); + if ($sharedComponent instanceof Fill) { + if ($this->parentPropertyName === 'endColor') { + return $sharedComponent->getEndColor(); + } + return $sharedComponent->getStartColor(); } @@ -96,7 +102,10 @@ class Color extends Supervisor */ public function getStyleArray($array) { - return $this->parent->getStyleArray([$this->parentPropertyName => $array]); + /** @var Style */ + $parent = $this->parent; + + return $parent->getStyleArray([$this->parentPropertyName => $array]); } /** @@ -153,6 +162,7 @@ class Color extends Supervisor */ public function setARGB(?string $colorValue = self::COLOR_BLACK) { + $this->hasChanged = true; if ($colorValue === '' || $colorValue === null) { $colorValue = self::COLOR_BLACK; } elseif (!$this->validateColor($colorValue, self::VALIDATE_ARGB_SIZE)) { @@ -190,6 +200,7 @@ class Color extends Supervisor */ public function setRGB(?string $colorValue = self::COLOR_BLACK) { + $this->hasChanged = true; if ($colorValue === '' || $colorValue === null) { $colorValue = '000000'; } elseif (!$this->validateColor($colorValue, self::VALIDATE_RGB_SIZE)) { @@ -220,7 +231,7 @@ class Color extends Supervisor { $colour = substr($rgbValue, $offset, 2); - return ($hex) ? $colour : hexdec($colour); + return ($hex) ? $colour : (int) hexdec($colour); } /** @@ -410,4 +421,13 @@ class Color extends Supervisor return $exportedArray; } + + public function getHasChanged(): bool + { + if ($this->isSupervisor) { + return $this->getSharedComponent()->hasChanged; + } + + return $this->hasChanged; + } } diff --git a/src/PhpSpreadsheet/Style/Fill.php b/src/PhpSpreadsheet/Style/Fill.php index 1a7331eb..bd87a792 100644 --- a/src/PhpSpreadsheet/Style/Fill.php +++ b/src/PhpSpreadsheet/Style/Fill.php @@ -49,7 +49,7 @@ class Fill extends Supervisor * * @var float */ - protected $rotation = 0; + protected $rotation = 0.0; /** * Start color. @@ -65,6 +65,9 @@ class Fill extends Supervisor */ protected $endColor; + /** @var bool */ + private $colorChanged = false; + /** * Create a new Fill. * @@ -102,7 +105,10 @@ class Fill extends Supervisor */ public function getSharedComponent() { - return $this->parent->getSharedComponent()->getFill(); + /** @var Style */ + $parent = $this->parent; + + return $parent->getSharedComponent()->getFill(); } /** @@ -124,7 +130,7 @@ class Fill extends Supervisor * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray( * [ * 'fillType' => Fill::FILL_GRADIENT_LINEAR, - * 'rotation' => 0, + * 'rotation' => 0.0, * 'startColor' => [ * 'rgb' => '000000' * ], @@ -248,6 +254,7 @@ class Fill extends Supervisor */ public function setStartColor(Color $color) { + $this->colorChanged = true; // make sure parameter is a real color and not a supervisor $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; @@ -278,6 +285,7 @@ class Fill extends Supervisor */ public function setEndColor(Color $color) { + $this->colorChanged = true; // make sure parameter is a real color and not a supervisor $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color; @@ -291,6 +299,17 @@ class Fill extends Supervisor return $this; } + public function getColorsChanged(): bool + { + if ($this->isSupervisor) { + $changed = $this->getSharedComponent()->colorChanged; + } else { + $changed = $this->colorChanged; + } + + return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged(); + } + /** * Get hash code. * @@ -308,6 +327,7 @@ class Fill extends Supervisor $this->getRotation() . ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') . ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') . + ((string) $this->getColorsChanged()) . __CLASS__ ); } @@ -315,10 +335,12 @@ class Fill extends Supervisor 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()); + if ($this->getColorsChanged()) { + $this->exportArray2($exportedArray, 'endColor', $this->getEndColor()); + $this->exportArray2($exportedArray, 'startColor', $this->getStartColor()); + } return $exportedArray; } diff --git a/src/PhpSpreadsheet/Style/Font.php b/src/PhpSpreadsheet/Style/Font.php index 63a75cc5..13fe2b67 100644 --- a/src/PhpSpreadsheet/Style/Font.php +++ b/src/PhpSpreadsheet/Style/Font.php @@ -122,7 +122,10 @@ class Font extends Supervisor */ public function getSharedComponent() { - return $this->parent->getSharedComponent()->getFont(); + /** @var Style */ + $parent = $this->parent; + + return $parent->getSharedComponent()->getFont(); } /** diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index a4569283..536b1d54 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -104,7 +104,10 @@ class NumberFormat extends Supervisor */ public function getSharedComponent() { - return $this->parent->getSharedComponent()->getNumberFormat(); + /** @var Style */ + $parent = $this->parent; + + return $parent->getSharedComponent()->getNumberFormat(); } /** @@ -157,7 +160,7 @@ class NumberFormat extends Supervisor if ($this->isSupervisor) { return $this->getSharedComponent()->getFormatCode(); } - if ($this->builtInFormatCode !== false) { + if (is_int($this->builtInFormatCode)) { return self::builtInFormatCode($this->builtInFormatCode); } @@ -352,7 +355,7 @@ class NumberFormat extends Supervisor * * @param string $formatCodeIndex * - * @return bool|int + * @return false|int */ public static function builtInFormatCodeIndex($formatCodeIndex) { diff --git a/src/PhpSpreadsheet/Style/Protection.php b/src/PhpSpreadsheet/Style/Protection.php index fc15e3b8..1c174e72 100644 --- a/src/PhpSpreadsheet/Style/Protection.php +++ b/src/PhpSpreadsheet/Style/Protection.php @@ -53,7 +53,10 @@ class Protection extends Supervisor */ public function getSharedComponent() { - return $this->parent->getSharedComponent()->getProtection(); + /** @var Style */ + $parent = $this->parent; + + return $parent->getSharedComponent()->getProtection(); } /** diff --git a/src/PhpSpreadsheet/Style/Style.php b/src/PhpSpreadsheet/Style/Style.php index 5823929d..fdb15451 100644 --- a/src/PhpSpreadsheet/Style/Style.php +++ b/src/PhpSpreadsheet/Style/Style.php @@ -132,17 +132,15 @@ class Style extends Supervisor $xfIndex = 0; } - return $this->parent->getCellXfByIndex($xfIndex); + return $activeSheet->getParent()->getCellXfByIndex($xfIndex); } /** * Get parent. Only used for style supervisor. - * - * @return Spreadsheet */ - public function getParent() + public function getParent(): Spreadsheet { - return $this->parent; + return $this->getActiveSheet()->getParent(); } /** diff --git a/src/PhpSpreadsheet/Style/Supervisor.php b/src/PhpSpreadsheet/Style/Supervisor.php index 7f655bef..8a5c350d 100644 --- a/src/PhpSpreadsheet/Style/Supervisor.php +++ b/src/PhpSpreadsheet/Style/Supervisor.php @@ -18,7 +18,7 @@ abstract class Supervisor implements IComparable /** * Parent. Only used for supervisor. * - * @var Spreadsheet|Style + * @var Spreadsheet|Supervisor */ protected $parent; @@ -45,7 +45,7 @@ abstract class Supervisor implements IComparable /** * Bind parent. Only used for supervisor. * - * @param Spreadsheet|Style $parent + * @param Spreadsheet|Supervisor $parent * @param null|string $parentPropertyName * * @return $this @@ -155,4 +155,21 @@ abstract class Supervisor implements IComparable $exportedArray[$index] = $objOrValue; } } + + /** + * Get the shared style component for the currently active cell in currently active sheet. + * Only used for style supervisor. + * + * @return mixed + */ + abstract public function getSharedComponent(); + + /** + * Build style array from subcomponents. + * + * @param array $array + * + * @return array + */ + abstract public function getStyleArray($array); } diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Style.php b/src/PhpSpreadsheet/Writer/Xlsx/Style.php index bf306a80..cb2e3850 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Style.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Style.php @@ -201,6 +201,15 @@ class Style extends WriterPart $objWriter->endElement(); } + private static function writePatternColors(Fill $fill): bool + { + if ($fill->getFillType() === Fill::FILL_NONE) { + return false; + } + + return $fill->getFillType() === Fill::FILL_SOLID || $fill->getColorsChanged(); + } + /** * Write Pattern Fill. */ @@ -213,15 +222,13 @@ class Style extends WriterPart $objWriter->startElement('patternFill'); $objWriter->writeAttribute('patternType', $fill->getFillType()); - if ($fill->getFillType() !== Fill::FILL_NONE) { + if (self::writePatternColors($fill)) { // fgColor if ($fill->getStartColor()->getARGB()) { $objWriter->startElement('fgColor'); $objWriter->writeAttribute('rgb', $fill->getStartColor()->getARGB()); $objWriter->endElement(); } - } - if ($fill->getFillType() !== Fill::FILL_NONE) { // bgColor if ($fill->getEndColor()->getARGB()) { $objWriter->startElement('bgColor'); diff --git a/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php b/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php index e11e5f29..a062b4cf 100644 --- a/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php +++ b/tests/PhpSpreadsheetTests/Style/ExportArrayTest.php @@ -51,6 +51,7 @@ class ExportArrayTest extends TestCase self::AssertEquals($cell1style->getFont()->getHashCode(), $cell2style->getFont()->getHashCode()); self::AssertEquals($cell1style->getFill()->getHashCode(), $cell2style->getFill()->getHashCode()); self::AssertEquals($cell1style->getProtection()->getHashCode(), $cell2style->getProtection()->getHashCode()); + $spreadsheet->disconnectWorksheets(); } public function testStyleFromArrayCopy(): void @@ -89,6 +90,7 @@ class ExportArrayTest extends TestCase self::AssertEquals($cell1style->getFill()->getStartColor()->getHashCode(), $cell2style->getFill()->getStartColor()->getHashCode()); self::AssertEquals($cell1style->getFill()->getEndColor()->getHashCode(), $cell2style->getFill()->getEndColor()->getHashCode()); + $spreadsheet->disconnectWorksheets(); } public function testNumberFormat(): void @@ -112,6 +114,7 @@ class ExportArrayTest extends TestCase self::assertEquals('$ 12,345.679', $cell2->getFormattedValue()); self::AssertEquals($cell1style->getNumberFormat()->getHashCode(), $cell2style->getNumberFormat()->getHashCode()); + $spreadsheet->disconnectWorksheets(); } public function testNumberFormatFromArray(): void @@ -139,6 +142,7 @@ class ExportArrayTest extends TestCase 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()); + $spreadsheet->disconnectWorksheets(); } public function testStackedRotation(): void @@ -157,5 +161,128 @@ class ExportArrayTest extends TestCase $cell2style->applyFromArray($styleArray); self::AssertEquals($cell1style->getAlignment()->getTextRotation(), $cell2style->getAlignment()->getTextRotation()); + $spreadsheet->disconnectWorksheets(); + } + + public function testFillColors(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + + $cell1 = $sheet->getCell('A2'); + $cell1style = $cell1->getStyle(); + $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125); + $cell1style->getFill()->getStartColor()->setArgb('FF112233'); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 0.0, + 'endColor' => ['argb' => 'FF000000'], + 'startColor' => ['argb' => 'FF112233'], + ], + $styleArray['fill'], + 'changed start color with setArgb' + ); + + $cell1 = $sheet->getCell('A1'); + $cell1style = $cell1->getStyle(); + $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 0.0, + ], + $styleArray['fill'], + 'default colors' + ); + + $cell1 = $sheet->getCell('A3'); + $cell1style = $cell1->getStyle(); + $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125); + $cell1style->getFill()->getEndColor()->setArgb('FF112233'); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 0.0, + 'endColor' => ['argb' => 'FF112233'], + 'startColor' => ['argb' => 'FFFFFFFF'], + ], + $styleArray['fill'], + 'changed end color with setArgb' + ); + + $cell1 = $sheet->getCell('A4'); + $cell1style = $cell1->getStyle(); + $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125); + $cell1style->getFill()->setEndColor(new Color('FF0000FF')); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 0.0, + 'endColor' => ['argb' => 'FF0000FF'], + 'startColor' => ['argb' => 'FFFFFFFF'], + ], + $styleArray['fill'], + 'changed end color with setEndColor' + ); + + $cell1 = $sheet->getCell('A5'); + $cell1style = $cell1->getStyle(); + $cell1style->getFill()->setFillType(Fill::FILL_PATTERN_GRAY125); + $cell1style->getFill()->setStartColor(new Color('FF0000FF')); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 0.0, + 'startColor' => ['argb' => 'FF0000FF'], + 'endColor' => ['argb' => 'FF000000'], + ], + $styleArray['fill'], + 'changed start color with setStartColor' + ); + + $cell1 = $sheet->getCell('A6'); + $cell1->getStyle()->getFill()->applyFromArray( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 45.0, + 'startColor' => ['argb' => 'FF00FFFF'], + ] + ); + $cell1style = $cell1->getStyle(); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 45.0, + 'startColor' => ['argb' => 'FF00FFFF'], + 'endColor' => ['argb' => 'FF000000'], + ], + $styleArray['fill'], + 'applyFromArray with startColor' + ); + + $cell1 = $sheet->getCell('A7'); + $cell1->getStyle()->getFill()->applyFromArray( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + ] + ); + $cell1style = $cell1->getStyle(); + $styleArray = $cell1style->exportArray(); + self::assertEquals( + [ + 'fillType' => Fill::FILL_PATTERN_GRAY125, + 'rotation' => 0.0, + ], + $styleArray['fill'], + 'applyFromArray without start/endColor' + ); + $spreadsheet->disconnectWorksheets(); } }