Skip to content

Commit 5ee4fbf

Browse files
author
Mark Baker
authored
Implement basic autofilter ranges with Gnumeric Reader (#2057)
* Load basic autofilter ranges with Gnumeric Reader * Handle null values passed to row height/column with/merged cells/autofilters
1 parent 4be9366 commit 5ee4fbf

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
1010
### Added
1111

1212
- Implemented basic AutoFiltering for Ods Reader and Writer [PR #2053](https://github.com/PHPOffice/PhpSpreadsheet/pull/2053)
13+
- Implemented basic AutoFiltering for Gnumeric Reader [PR #2055](https://github.com/PHPOffice/PhpSpreadsheet/pull/2055)
1314
- Improved support for Row and Column ranges in formulae [Issue #1755](https://github.com/PHPOffice/PhpSpreadsheet/issues/1755) [PR #2028](https://github.com/PHPOffice/PhpSpreadsheet/pull/2028)
1415
- Implemented URLENCODE() Web Function
1516
- Implemented the CHITEST(), CHISQ.DIST() and CHISQ.INV() and equivalent Statistical functions, for both left- and right-tailed distributions.

docs/references/features-cross-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@
13141314
<td style="text-align: center; color: orange;">●</td>
13151315
<td></td>
13161316
<td style="text-align: center; color: orange;">●</td>
1317-
<td></td>
1317+
<td style="text-align: center; color: orange;">●</td>
13181318
<td></td>
13191319
<td></td>
13201320
<td style="text-align: center; color: orange;">●</td>

src/PhpSpreadsheet/Reader/Gnumeric.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ public function loadIntoExisting(string $pFilename, Spreadsheet $spreadsheet): S
482482
$this->processColumnWidths($sheet, $maxCol);
483483
$this->processRowHeights($sheet, $maxRow);
484484
$this->processMergedCells($sheet);
485+
$this->processAutofilter($sheet);
485486

486487
++$worksheetID;
487488
}
@@ -514,10 +515,10 @@ private function addBorderStyle(SimpleXMLElement $srssb, array &$styleArray, str
514515
}
515516
}
516517

517-
private function processMergedCells(SimpleXMLElement $sheet): void
518+
private function processMergedCells(?SimpleXMLElement $sheet): void
518519
{
519520
// Handle Merged Cells in this worksheet
520-
if (isset($sheet->MergedRegions)) {
521+
if ($sheet !== null && isset($sheet->MergedRegions)) {
521522
foreach ($sheet->MergedRegions->Merge as $mergeCells) {
522523
if (strpos($mergeCells, ':') !== false) {
523524
$this->spreadsheet->getActiveSheet()->mergeCells($mergeCells);
@@ -526,6 +527,20 @@ private function processMergedCells(SimpleXMLElement $sheet): void
526527
}
527528
}
528529

530+
private function processAutofilter(?SimpleXMLElement $sheet): void
531+
{
532+
if ($sheet !== null && isset($sheet->Filters)) {
533+
foreach ($sheet->Filters->Filter as $autofilter) {
534+
if ($autofilter !== null) {
535+
$attributes = $autofilter->attributes();
536+
if (isset($attributes['Area'])) {
537+
$this->spreadsheet->getActiveSheet()->setAutoFilter((string) $attributes['Area']);
538+
}
539+
}
540+
}
541+
}
542+
}
543+
529544
private function setColumnWidth(int $whichColumn, float $defaultWidth): void
530545
{
531546
$columnDimension = $this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1));
@@ -564,9 +579,9 @@ private function processColumnLoop(int $whichColumn, int $maxCol, SimpleXMLEleme
564579
return $whichColumn;
565580
}
566581

567-
private function processColumnWidths(SimpleXMLElement $sheet, int $maxCol): void
582+
private function processColumnWidths(?SimpleXMLElement $sheet, int $maxCol): void
568583
{
569-
if ((!$this->readDataOnly) && (isset($sheet->Cols))) {
584+
if ((!$this->readDataOnly) && $sheet !== null && (isset($sheet->Cols))) {
570585
// Column Widths
571586
$defaultWidth = 0;
572587
$columnAttributes = $sheet->Cols->attributes();
@@ -622,9 +637,9 @@ private function processRowLoop(int $whichRow, int $maxRow, SimpleXMLElement $ro
622637
return $whichRow;
623638
}
624639

625-
private function processRowHeights(SimpleXMLElement $sheet, int $maxRow): void
640+
private function processRowHeights(?SimpleXMLElement $sheet, int $maxRow): void
626641
{
627-
if ((!$this->readDataOnly) && (isset($sheet->Rows))) {
642+
if ((!$this->readDataOnly) && $sheet !== null && (isset($sheet->Rows))) {
628643
// Row Heights
629644
$defaultHeight = 0;
630645
$rowAttributes = $sheet->Rows->attributes();
@@ -646,10 +661,10 @@ private function processRowHeights(SimpleXMLElement $sheet, int $maxRow): void
646661
}
647662
}
648663

649-
private function processDefinedNames(SimpleXMLElement $gnmXML): void
664+
private function processDefinedNames(?SimpleXMLElement $gnmXML): void
650665
{
651666
// Loop through definedNames (global named ranges)
652-
if (isset($gnmXML->Names)) {
667+
if ($gnmXML !== null && isset($gnmXML->Names)) {
653668
foreach ($gnmXML->Names->Name as $definedName) {
654669
$name = (string) $definedName->name;
655670
$value = (string) $definedName->value;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Gnumeric;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
6+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class AutoFilterTest extends TestCase
10+
{
11+
/**
12+
* @var Spreadsheet
13+
*/
14+
private $spreadsheet;
15+
16+
protected function setUp(): void
17+
{
18+
$filename = 'tests/data/Reader/Gnumeric/Autofilter_Basic.gnumeric';
19+
$reader = new Gnumeric();
20+
$this->spreadsheet = $reader->load($filename);
21+
}
22+
23+
public function testAutoFilterRange(): void
24+
{
25+
$worksheet = $this->spreadsheet->getActiveSheet();
26+
27+
$autoFilterRange = $worksheet->getAutoFilter()->getRange();
28+
29+
self::assertSame('A1:D57', $autoFilterRange);
30+
}
31+
}
Binary file not shown.

0 commit comments

Comments
 (0)