Fill Pattern Start and End Colors (#2444)
* Fill Pattern Start and End Colors Fix #2441. The Fill constructor sets start color to white and end color to black and the Xlsx writer writes these values to the output file. This appears to be the wrong setting for all 7 LIGHT* pattern types, 2 of the 7 DARK* patterns (DARKGRAY and DARKTRELLIS), and 1 of the 3 GRAY patterns (GRAY0625). When the wrong colors are written at save time, those patterns are not as expected. Xls writer does not appear to have the same problem. The XML does not require either a start or end color, and the omission of these colors in the file being read was responsible for the problem. The code is changed to mimic that behavior by omitting the color tags at write time if they have not changed from when they were created by the Fill constructor (they will be written for gradient or solid patterns regardless). This is another change which is easier to confirm via samples rather than tests. There are separate samples for Xlsx and Xls; as Excel will be quick to warn you, Xls is not as fully functional as Xlsx with respect to fill patterns. The samples do include a cell where one of the cells (LightGrid in C11) explicitly specifies the "default" colors. * Scrutinizer It somehow ascribed to me a problem in code which was unchanged by this PR. Correct it anyhow, along with some Phpstan fixes (errors now ignored because of change). * Added Tests Also corrected some docBlock problems with Style/*/parent and getSharedComponent. * Create 2 Abstract Methods Scrutinizer complained that 2 methods found in all Supervisor sub-types were not defined in Supervisor. Add abstract methods to satisfy it. * Scrutinizer Ignoring Typehints Try this instead. * Slight Improvement Better handling of Style->getParent().
This commit is contained in:
parent
a7f687fe5c
commit
67bf45d700
|
|
@ -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\\<string\\>\\) does not accept array\\<bool\\|float\\|int\\|string\\>\\.$#"
|
||||
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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . '/../Header.php';
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xls;
|
||||
|
||||
$helper->log('Read spreadsheet');
|
||||
$reader = new Xls();
|
||||
$spreadsheet = $reader->load(__DIR__ . '/../templates/47_xlsfill.xls');
|
||||
|
||||
// Save
|
||||
$helper->write($spreadsheet, __FILE__, ['Xls']);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . '/../Header.php';
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
||||
|
||||
$helper->log('Read spreadsheet');
|
||||
$reader = new Xlsx();
|
||||
$spreadsheet = $reader->load(__DIR__ . '/../templates/47_xlsxfill.xlsx');
|
||||
|
||||
// Save
|
||||
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
$sharedComponent = $parent->getSharedComponent();
|
||||
if ($sharedComponent instanceof Fill) {
|
||||
if ($this->parentPropertyName === 'endColor') {
|
||||
return $sharedComponent->getEndColor();
|
||||
}
|
||||
if ($this->parentPropertyName === 'startColor') {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
if ($this->getColorsChanged()) {
|
||||
$this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
|
||||
$this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
|
||||
}
|
||||
|
||||
return $exportedArray;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue