Skip to content

Commit aeccdb3

Browse files
oleibmanMark Baker
and
Mark Baker
authored
XLSX Reader and Empty Fill Tag (PHPOffice#2011)
Openpyxl can generate the xml tag `<patternFill/>`, possibly even as a default style. Excel has no problem with this, treating it as "fill none", but PhpSpreadsheet has a glitch because it treats it as "fill solid white". So, when PhpSpreadsheet loads and saves such a file, the result at first appears as if gridlines are disabled; in fact, the gridlines are merely invisible behind the cells with their solid white fill. This PR makes PhpSpreadsheet behave the same as Excel in this circumstance. Co-authored-by: Mark Baker <[email protected]>
1 parent 6282035 commit aeccdb3

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use PhpOffice\PhpSpreadsheet\Style\Border;
3131
use PhpOffice\PhpSpreadsheet\Style\Borders;
3232
use PhpOffice\PhpSpreadsheet\Style\Color;
33+
use PhpOffice\PhpSpreadsheet\Style\Fill;
3334
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
3435
use PhpOffice\PhpSpreadsheet\Style\Protection;
3536
use PhpOffice\PhpSpreadsheet\Style\Style;
@@ -1646,7 +1647,7 @@ private static function readStyle(Style $docStyle, $style): void
16461647
$docStyle->getFill()->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
16471648
$docStyle->getFill()->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
16481649
} elseif ($style->fill->patternFill) {
1649-
$patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : 'solid';
1650+
$patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : Fill::FILL_NONE;
16501651
$docStyle->getFill()->setFillType($patternType);
16511652
if ($style->fill->patternFill->fgColor) {
16521653
$docStyle->getFill()->getStartColor()->setARGB(self::readColor($style->fill->patternFill->fgColor, true));

src/PhpSpreadsheet/Reader/Xlsx/Styles.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillSty
9797
$fillStyle->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
9898
$fillStyle->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
9999
} elseif ($fillStyleXml->patternFill) {
100-
$patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : 'solid';
100+
$patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : Fill::FILL_NONE;
101101
$fillStyle->setFillType($patternType);
102102
if ($fillStyleXml->patternFill->fgColor) {
103103
$fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader;
4+
5+
use PhpOffice\PhpSpreadsheet\IOFactory;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DefaultFillTest extends TestCase
9+
{
10+
public function testDefaultFill(): void
11+
{
12+
// default fill pattern doesn't specify filltype
13+
$filename = 'tests/data/Reader/XLSX/pr1769g.py.xlsx';
14+
$file = 'zip://';
15+
$file .= $filename;
16+
$file .= '#xl/styles.xml';
17+
$data = file_get_contents($file);
18+
// confirm that file contains expected empty xml tag
19+
if ($data === false) {
20+
self::fail('Unable to read file');
21+
} else {
22+
self::assertStringContainsString('<patternFill/>', $data);
23+
}
24+
$reader = IOFactory::createReader('Xlsx');
25+
$spreadsheet = $reader->load($filename);
26+
$sheet = $spreadsheet->getActiveSheet();
27+
self::assertSame('none', $sheet->getCell('A1')->getStyle()->getFill()->getFillType());
28+
self::assertSame('none', $sheet->getCell('D4')->getStyle()->getFill()->getFillType());
29+
self::assertSame('none', $sheet->getCell('J16')->getStyle()->getFill()->getFillType());
30+
self::assertSame('solid', $sheet->getCell('C2')->getStyle()->getFill()->getFillType());
31+
}
32+
}
4.64 KB
Binary file not shown.

0 commit comments

Comments
 (0)