Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern Fill style should default to 'solid' if there is a pattern fill with colour but no style #2050

Merged
merged 3 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.