Skip to content

Commit

Permalink
Handle Some Edge Cases
Browse files Browse the repository at this point in the history
For example, Style file without a Fills section.
  • Loading branch information
Owen Leibman committed Dec 30, 2021
1 parent 9c45567 commit d6622c1
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,28 +541,11 @@ public function load(string $filename, int $flags = 0): Spreadsheet
$xmlStyles = $this->loadZip("$dir/$xpath[Target]", $mainNS);
}

$fills = [];
$fonts = [];
$borders = [];
$xfTags = [];
$cellXfTags = [];
if (count($xmlStyles) > 0) {
foreach ($xmlStyles->fills->fill as $fill) {
$fills[] = $fill;
}
foreach ($xmlStyles->fonts->font as $font) {
$fonts[] = $font;
}
foreach ($xmlStyles->borders->border as $border) {
$borders[] = $border;
}
foreach ($xmlStyles->cellXfs->xf as $xf) {
$xfTags[] = $xf;
}
foreach ($xmlStyles->cellStyleXfs->xf as $xf) {
$cellXfTags[] = $xf;
}
}
$fills = self::extractStyles($xmlStyles, 'fills', 'fill');
$fonts = self::extractStyles($xmlStyles, 'fonts', 'font');
$borders = self::extractStyles($xmlStyles, 'borders', 'border');
$xfTags = self::extractStyles($xmlStyles, 'cellXfs', 'xf');
$cellXfTags = self::extractStyles($xmlStyles, 'cellStyleXfs', 'xf');

$styles = [];
$cellStyles = [];
Expand Down Expand Up @@ -2094,4 +2077,16 @@ private function readAutoFilterTablesInTablesFile(
}
}
}

private static function extractStyles(?SimpleXMLElement $sxml, string $node1, string $node2): array
{
$array = [];
if ($sxml && $sxml->{$node1}->{$node2}) {
foreach ($sxml->{$node1}->{$node2} as $node) {
$array[] = $node;
}
}

return $array;
}
}

0 comments on commit d6622c1

Please sign in to comment.