From e1018e2330fb8d495e5c9c5bdc058f61c8c4120c Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 13 Dec 2020 16:00:06 +0100 Subject: [PATCH] Use worksheet->getHighestColumn/Row for isFiltered There may very well be a good reason the row/col attributes are loaded first. This is passing the test suite though and I think makes more sense than iterating over the Row/Col attrs to get the Col/Row isFiltered bool. --- src/PhpSpreadsheet/Reader/Xlsx.php | 26 +++++++++---------- .../Reader/Xlsx/ColumnAndRowAttributes.php | 23 ++++++---------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 124cc3b2..a3b140b1 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -630,19 +630,6 @@ class Xlsx extends BaseReader $docSheet->setSheetState((string) $eleSheet['state']); } - if ($xmlSheet) { - if (isset($xmlSheet->sheetViews, $xmlSheet->sheetViews->sheetView)) { - $sheetViews = new SheetViews($xmlSheet->sheetViews->sheetView, $docSheet); - $sheetViews->load(); - } - - $sheetViewOptions = new SheetViewOptions($docSheet, $xmlSheet); - $sheetViewOptions->load($this->getReadDataOnly()); - - (new ColumnAndRowAttributes($docSheet, $xmlSheet)) - ->load($this->getReadFilter(), $this->getReadDataOnly()); - } - if ($xmlSheet && $xmlSheet->sheetData && $xmlSheet->sheetData->row) { $cIndex = 1; // Cell Start from 1 foreach ($xmlSheet->sheetData->row as $row) { @@ -756,6 +743,19 @@ class Xlsx extends BaseReader } } + if ($xmlSheet) { + if (isset($xmlSheet->sheetViews, $xmlSheet->sheetViews->sheetView)) { + $sheetViews = new SheetViews($xmlSheet->sheetViews->sheetView, $docSheet); + $sheetViews->load(); + } + + $sheetViewOptions = new SheetViewOptions($docSheet, $xmlSheet); + $sheetViewOptions->load($this->getReadDataOnly()); + + (new ColumnAndRowAttributes($docSheet, $xmlSheet)) + ->load($this->getReadFilter(), $this->getReadDataOnly()); + } + if (!$this->readDataOnly && $xmlSheet && $xmlSheet->conditionalFormatting) { (new ConditionalStyles($docSheet, $xmlSheet, $dxfs))->load(); } diff --git a/src/PhpSpreadsheet/Reader/Xlsx/ColumnAndRowAttributes.php b/src/PhpSpreadsheet/Reader/Xlsx/ColumnAndRowAttributes.php index 8913d6e0..1769950b 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/ColumnAndRowAttributes.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/ColumnAndRowAttributes.php @@ -96,7 +96,7 @@ class ColumnAndRowAttributes extends BaseParserClass foreach ($columnsAttributes as $columnCoordinate => $columnAttributes) { if ( $readFilter === null || - !$this->isFilteredColumn($readFilter, $columnCoordinate, $rowsAttributes) + !$this->isFilteredColumn($readFilter, $columnCoordinate) ) { if (!isset($columnsAttributesAreSet[$columnCoordinate])) { $this->setColumnAttributes($columnCoordinate, $columnAttributes); @@ -109,7 +109,7 @@ class ColumnAndRowAttributes extends BaseParserClass foreach ($rowsAttributes as $rowCoordinate => $rowAttributes) { if ( $readFilter === null || - !$this->isFilteredRow($readFilter, $rowCoordinate, $columnsAttributes) + !$this->isFilteredRow($readFilter, $rowCoordinate) ) { if (!isset($rowsAttributesAreSet[$rowCoordinate])) { $this->setRowAttributes($rowCoordinate, $rowAttributes); @@ -119,17 +119,13 @@ class ColumnAndRowAttributes extends BaseParserClass } } - private function isFilteredColumn(IReadFilter $readFilter, $columnCoordinate, array $rowsAttributes) + private function isFilteredColumn(IReadFilter $readFilter, $columnCoordinate) { - if (empty($rowAttributes)) { - return false; - } - foreach ($rowsAttributes as $rowCoordinate => $rowAttributes) { + for ($rowCoordinate = 1; $rowCoordinate <= $this->worksheet->getHighestRow(); $rowCoordinate++) { if ($readFilter->readCell($columnCoordinate, $rowCoordinate, $this->worksheet->getTitle())) { return false; } } - return true; } @@ -174,17 +170,14 @@ class ColumnAndRowAttributes extends BaseParserClass return $columnAttributes; } - private function isFilteredRow(IReadFilter $readFilter, $rowCoordinate, array $columnsAttributes) + private function isFilteredRow(IReadFilter $readFilter, $rowCoordinate) { - if (empty($columnAttributes)) { - return false; - } - foreach ($columnsAttributes as $columnCoordinate => $columnAttributes) { - if ($readFilter->readCell($columnCoordinate, $rowCoordinate, $this->worksheet->getTitle())) { + $lastColumnIndex = Coordinate::columnIndexFromString($this->worksheet->getHighestColumn()); + for ($columnIndex = 1; $columnIndex <= $lastColumnIndex; $columnIndex++) { + if ($readFilter->readCell(Coordinate::stringFromColumnIndex($columnIndex), $rowCoordinate)) { return false; } } - return true; }