Skip to content

Commit

Permalink
Calculate row range for a single-row AutoFilter range
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Oct 8, 2022
1 parent 339a593 commit 4fa11d1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/PhpSpreadsheet/Worksheet/AutoFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ public function showHideRows()
}
}

$rangeEnd[1] = $this->checkAutoExtendRange($rangeStart[1], $rangeEnd[1]);

// Execute the column tests for each row in the autoFilter range to determine show/hide,
for ($row = $rangeStart[1] + 1; $row <= $rangeEnd[1]; ++$row) {
$result = true;
Expand All @@ -1055,6 +1057,24 @@ public function showHideRows()
return $this;
}

/**
* Magic Range Auto-sizing.
* For a single row rangeSet, we follow MS Excel rules, and search for the first empty row to determine our range.
*/
protected function checkAutoExtendRange(int $startRow, int $endRow): int
{
if ($startRow === $endRow && $this->workSheet !== null) {
$rowIterator = $this->workSheet->getRowIterator($startRow + 1);
foreach ($rowIterator as $row) {
if ($row->isEmpty(CellIterator::TREAT_NULL_VALUE_AS_EMPTY_CELL | CellIterator::TREAT_EMPTY_STRING_AS_EMPTY_CELL) === true) {
return $row->getRowIndex() - 1;
}
}
}

return $endRow;
}

/**
* Implement PHP __clone to create a deep clone, not just a shallow copy.
*/
Expand Down

0 comments on commit 4fa11d1

Please sign in to comment.