Skip to content

Commit

Permalink
Pattern Fill style should default to 'solid' if there is a pattern fi…
Browse files Browse the repository at this point in the history
…ll with colour but no style (#2050)

* Pattern Fill style should default to 'solid' if there is a pattern fill style for a conditional; though may need to check if there are defined fg/bg colours as well; and only set a fill style if there are defined colurs
  • Loading branch information
Mark Baker authored Apr 30, 2021
1 parent cc5c020 commit d555b5d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Nothing.

### Fixed

- Correct default fill style for conditional without a pattern defined [Issue #2035](https://github.com/PHPOffice/PhpSpreadsheet/issues/2035) [PR #2050](https://github.com/PHPOffice/PhpSpreadsheet/pull/2050)
- Fixed issue where array key check for existince before accessing arrays in Xlsx.php. [PR #1970](https://github.com/PHPOffice/PhpSpreadsheet/pull/1970)
- Fixed issue with quoted strings in number format mask rendered with toFormattedString() [Issue 1972#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1972) [PR #1978](https://github.com/PHPOffice/PhpSpreadsheet/pull/1978)
- Fixed issue with percentage formats in number format mask rendered with toFormattedString() [Issue 1929#](https://github.com/PHPOffice/PhpSpreadsheet/issues/1929) [PR #1928](https://github.com/PHPOffice/PhpSpreadsheet/pull/1928)
Expand Down
14 changes: 9 additions & 5 deletions src/PhpSpreadsheet/Reader/Xlsx/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,21 @@ public static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyl
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)
);
} elseif ($fillStyleXml->patternFill) {
$patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
? (string) $fillStyleXml->patternFill['patternType']
: Fill::FILL_NONE;

$fillStyle->setFillType($patternType);
$defaultFillStyle = Fill::FILL_NONE;
if ($fillStyleXml->patternFill->fgColor) {
$fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
$defaultFillStyle = Fill::FILL_SOLID;
}
if ($fillStyleXml->patternFill->bgColor) {
$fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
$defaultFillStyle = Fill::FILL_SOLID;
}

$patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
? (string) $fillStyleXml->patternFill['patternType']
: $defaultFillStyle;

$fillStyle->setFillType($patternType);
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/PhpSpreadsheet/Writer/Xls/Style/ColorMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ public static function lookup(Color $color, int $defaultIndex = 0x00): int
return self::$colorMap["#{$colorRgb}"];
}

// TODO Try and map RGB value to nearest colour within the define pallette
// $red = Color::getRed($colorRgb, false);
// $green = Color::getGreen($colorRgb, false);
// $blue = Color::getBlue($colorRgb, false);

// $paletteSpace = 3;
// $newColor = ($red * $paletteSpace / 256) * ($paletteSpace * $paletteSpace) +
// ($green * $paletteSpace / 256) * $paletteSpace +
// ($blue * $paletteSpace / 256);

return $defaultIndex;
}
}
11 changes: 11 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/DefaultFillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ public function testDefaultFill(): void
self::assertSame('none', $sheet->getCell('J16')->getStyle()->getFill()->getFillType());
self::assertSame('solid', $sheet->getCell('C2')->getStyle()->getFill()->getFillType());
}

public function testDefaultConditionalFill(): void
{
// default fill pattern for a conditional style where the filltype is not defined
$filename = 'tests/data/Reader/XLSX/pr2050cf-fill.xlsx';
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($filename);

$style = $spreadsheet->getActiveSheet()->getConditionalStyles('A1')[0]->getStyle();
self::assertSame('solid', $style->getFill()->getFillType());
}
}
Binary file added tests/data/Reader/XLSX/pr2050cf-fill.xlsx
Binary file not shown.

0 comments on commit d555b5d

Please sign in to comment.