Skip to content

Commit d6622c1

Browse files
author
Owen Leibman
committed
Handle Some Edge Cases
For example, Style file without a Fills section.
1 parent 9c45567 commit d6622c1

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+17-22
Original file line numberDiff line numberDiff line change
@@ -541,28 +541,11 @@ public function load(string $filename, int $flags = 0): Spreadsheet
541541
$xmlStyles = $this->loadZip("$dir/$xpath[Target]", $mainNS);
542542
}
543543

544-
$fills = [];
545-
$fonts = [];
546-
$borders = [];
547-
$xfTags = [];
548-
$cellXfTags = [];
549-
if (count($xmlStyles) > 0) {
550-
foreach ($xmlStyles->fills->fill as $fill) {
551-
$fills[] = $fill;
552-
}
553-
foreach ($xmlStyles->fonts->font as $font) {
554-
$fonts[] = $font;
555-
}
556-
foreach ($xmlStyles->borders->border as $border) {
557-
$borders[] = $border;
558-
}
559-
foreach ($xmlStyles->cellXfs->xf as $xf) {
560-
$xfTags[] = $xf;
561-
}
562-
foreach ($xmlStyles->cellStyleXfs->xf as $xf) {
563-
$cellXfTags[] = $xf;
564-
}
565-
}
544+
$fills = self::extractStyles($xmlStyles, 'fills', 'fill');
545+
$fonts = self::extractStyles($xmlStyles, 'fonts', 'font');
546+
$borders = self::extractStyles($xmlStyles, 'borders', 'border');
547+
$xfTags = self::extractStyles($xmlStyles, 'cellXfs', 'xf');
548+
$cellXfTags = self::extractStyles($xmlStyles, 'cellStyleXfs', 'xf');
566549

567550
$styles = [];
568551
$cellStyles = [];
@@ -2094,4 +2077,16 @@ private function readAutoFilterTablesInTablesFile(
20942077
}
20952078
}
20962079
}
2080+
2081+
private static function extractStyles(?SimpleXMLElement $sxml, string $node1, string $node2): array
2082+
{
2083+
$array = [];
2084+
if ($sxml && $sxml->{$node1}->{$node2}) {
2085+
foreach ($sxml->{$node1}->{$node2} as $node) {
2086+
$array[] = $node;
2087+
}
2088+
}
2089+
2090+
return $array;
2091+
}
20972092
}

0 commit comments

Comments
 (0)