Skip to content

Commit bbfb5ea

Browse files
committed
Add iterateOnlyExistingCells to Constructors
Fix PHPOffice#3721. That issue can already be handled, but requires several statements when one ought to suffice. Adding an extra parameter to the RowCellIterator and ColumnCellIterator constructors is useful, easy, and becomes especially practical now that our supported Php releases all support named parameters (see new test Issue3721Test).
1 parent bd633b1 commit bbfb5ea

File tree

6 files changed

+44
-10
lines changed

6 files changed

+44
-10
lines changed

src/PhpSpreadsheet/Worksheet/Column.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public function getColumnIndex(): string
5353
* @param int $startRow The row number at which to start iterating
5454
* @param int $endRow Optionally, the row number at which to stop iterating
5555
*/
56-
public function getCellIterator($startRow = 1, $endRow = null): ColumnCellIterator
56+
public function getCellIterator($startRow = 1, $endRow = null, bool $iterateOnlyExistingCells = false): ColumnCellIterator
5757
{
58-
return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow);
58+
return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow, $iterateOnlyExistingCells);
5959
}
6060

6161
/**
@@ -64,9 +64,9 @@ public function getCellIterator($startRow = 1, $endRow = null): ColumnCellIterat
6464
* @param int $startRow The row number at which to start iterating
6565
* @param int $endRow Optionally, the row number at which to stop iterating
6666
*/
67-
public function getRowIterator($startRow = 1, $endRow = null): ColumnCellIterator
67+
public function getRowIterator($startRow = 1, $endRow = null, bool $iterateOnlyExistingCells = false): ColumnCellIterator
6868
{
69-
return $this->getCellIterator($startRow, $endRow);
69+
return $this->getCellIterator($startRow, $endRow, $iterateOnlyExistingCells);
7070
}
7171

7272
/**

src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ class ColumnCellIterator extends CellIterator
4747
* @param int $startRow The row number at which to start iterating
4848
* @param int $endRow Optionally, the row number at which to stop iterating
4949
*/
50-
public function __construct(Worksheet $worksheet, $columnIndex = 'A', $startRow = 1, $endRow = null)
50+
public function __construct(Worksheet $worksheet, $columnIndex = 'A', $startRow = 1, $endRow = null, bool $iterateOnlyExistingCells = false)
5151
{
5252
// Set subject
5353
$this->worksheet = $worksheet;
5454
$this->cellCollection = $worksheet->getCellCollection();
5555
$this->columnIndex = Coordinate::columnIndexFromString($columnIndex);
5656
$this->resetEnd($endRow);
5757
$this->resetStart($startRow);
58+
$this->setIterateOnlyExistingCells($iterateOnlyExistingCells);
5859
}
5960

6061
/**

src/PhpSpreadsheet/Worksheet/Row.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public function getRowIndex(): int
5252
* @param string $startColumn The column address at which to start iterating
5353
* @param string $endColumn Optionally, the column address at which to stop iterating
5454
*/
55-
public function getCellIterator($startColumn = 'A', $endColumn = null): RowCellIterator
55+
public function getCellIterator($startColumn = 'A', $endColumn = null, bool $iterateOnlyExistingCells = false): RowCellIterator
5656
{
57-
return new RowCellIterator($this->worksheet, $this->rowIndex, $startColumn, $endColumn);
57+
return new RowCellIterator($this->worksheet, $this->rowIndex, $startColumn, $endColumn, $iterateOnlyExistingCells);
5858
}
5959

6060
/**
@@ -63,9 +63,9 @@ public function getCellIterator($startColumn = 'A', $endColumn = null): RowCellI
6363
* @param string $startColumn The column address at which to start iterating
6464
* @param string $endColumn Optionally, the column address at which to stop iterating
6565
*/
66-
public function getColumnIterator($startColumn = 'A', $endColumn = null): RowCellIterator
66+
public function getColumnIterator($startColumn = 'A', $endColumn = null, bool $iterateOnlyExistingCells = false): RowCellIterator
6767
{
68-
return $this->getCellIterator($startColumn, $endColumn);
68+
return $this->getCellIterator($startColumn, $endColumn, $iterateOnlyExistingCells);
6969
}
7070

7171
/**

src/PhpSpreadsheet/Worksheet/RowCellIterator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ class RowCellIterator extends CellIterator
4747
* @param string $startColumn The column address at which to start iterating
4848
* @param string $endColumn Optionally, the column address at which to stop iterating
4949
*/
50-
public function __construct(Worksheet $worksheet, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
50+
public function __construct(Worksheet $worksheet, $rowIndex = 1, $startColumn = 'A', $endColumn = null, bool $iterateOnlyExistingCells = false)
5151
{
5252
// Set subject and row index
5353
$this->worksheet = $worksheet;
5454
$this->cellCollection = $worksheet->getCellCollection();
5555
$this->rowIndex = $rowIndex;
5656
$this->resetEnd($endColumn);
5757
$this->resetStart($startColumn);
58+
$this->setIterateOnlyExistingCells($iterateOnlyExistingCells);
5859
}
5960

6061
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Ods;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue3721Test extends TestCase
11+
{
12+
public function testIssue2810(): void
13+
{
14+
// Problems with getHighestDataColumn
15+
$filename = 'tests/data/Reader/Ods/issue.3721.ods';
16+
$reader = new Ods();
17+
$spreadsheet = $reader->load($filename);
18+
$sheet = $spreadsheet->getSheetByNameOrThrow('sheet with data');
19+
$origHigh = $sheet->getHighestDataColumn();
20+
self::assertSame('C', $origHigh);
21+
$cells = [];
22+
foreach ($sheet->getRowIterator() as $row) {
23+
foreach ($row->getCellIterator(iterateOnlyExistingCells: true) as $cell) {
24+
$cells[] = $cell->getCoordinate();
25+
}
26+
}
27+
self::assertSame(['A1', 'B1', 'C1', 'A2', 'B2', 'C2'], $cells);
28+
self::assertSame('C', $sheet->getHighestDataColumn());
29+
self::assertSame('BL', $sheet->getHighestColumn());
30+
$spreadsheet->disconnectWorksheets();
31+
}
32+
}

tests/data/Reader/Ods/issue.3721.ods

8.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)