Skip to content

Commit 72b7702

Browse files
authored
Add iterateOnlyExistingCells to Constructors (#3727)
* Add iterateOnlyExistingCells to Constructors Fix #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). * Update Column.php * Update .php-cs-fixer.dist.php * Update Row.php * Update Column.php * Update ByColumnAndRowTest.php
1 parent 5029e81 commit 72b7702

File tree

8 files changed

+50
-19
lines changed

8 files changed

+50
-19
lines changed

.php-cs-fixer.dist.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
'no_unneeded_final_method' => true,
133133
'no_unreachable_default_argument_value' => true,
134134
'no_unset_cast' => true,
135-
'no_unset_on_property' => true,
135+
'no_unset_on_property' => false,
136136
'no_unused_imports' => true,
137137
'no_useless_else' => true,
138138
'no_useless_return' => true,

src/PhpSpreadsheet/Worksheet/Column.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Column
66
{
7-
private ?Worksheet $worksheet;
7+
private Worksheet $worksheet;
88

99
/**
1010
* Column index.
@@ -28,7 +28,7 @@ public function __construct(Worksheet $worksheet, $columnIndex = 'A')
2828
*/
2929
public function __destruct()
3030
{
31-
$this->worksheet = null;
31+
unset($this->worksheet);
3232
}
3333

3434
/**
@@ -45,9 +45,9 @@ public function getColumnIndex(): string
4545
* @param int $startRow The row number at which to start iterating
4646
* @param int $endRow Optionally, the row number at which to stop iterating
4747
*/
48-
public function getCellIterator($startRow = 1, $endRow = null): ColumnCellIterator
48+
public function getCellIterator($startRow = 1, $endRow = null, bool $iterateOnlyExistingCells = false): ColumnCellIterator
4949
{
50-
return new ColumnCellIterator($this->getWorksheet(), $this->columnIndex, $startRow, $endRow);
50+
return new ColumnCellIterator($this->worksheet, $this->columnIndex, $startRow, $endRow, $iterateOnlyExistingCells);
5151
}
5252

5353
/**
@@ -56,9 +56,9 @@ public function getCellIterator($startRow = 1, $endRow = null): ColumnCellIterat
5656
* @param int $startRow The row number at which to start iterating
5757
* @param int $endRow Optionally, the row number at which to stop iterating
5858
*/
59-
public function getRowIterator($startRow = 1, $endRow = null): ColumnCellIterator
59+
public function getRowIterator($startRow = 1, $endRow = null, bool $iterateOnlyExistingCells = false): ColumnCellIterator
6060
{
61-
return $this->getCellIterator($startRow, $endRow);
61+
return $this->getCellIterator($startRow, $endRow, $iterateOnlyExistingCells);
6262
}
6363

6464
/**
@@ -108,7 +108,6 @@ public function isEmpty(int $definitionOfEmptyFlags = 0, $startRow = 1, $endRow
108108
*/
109109
public function getWorksheet(): Worksheet
110110
{
111-
// @phpstan-ignore-next-line
112111
return $this->worksheet;
113112
}
114113
}

src/PhpSpreadsheet/Worksheet/ColumnCellIterator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ class ColumnCellIterator extends CellIterator
4343
* @param int $startRow The row number at which to start iterating
4444
* @param int $endRow Optionally, the row number at which to stop iterating
4545
*/
46-
public function __construct(Worksheet $worksheet, $columnIndex = 'A', int $startRow = 1, $endRow = null)
46+
public function __construct(Worksheet $worksheet, $columnIndex = 'A', $startRow = 1, $endRow = null, bool $iterateOnlyExistingCells = false)
4747
{
4848
// Set subject
4949
$this->worksheet = $worksheet;
5050
$this->cellCollection = $worksheet->getCellCollection();
5151
$this->columnIndex = Coordinate::columnIndexFromString($columnIndex);
5252
$this->resetEnd($endRow);
5353
$this->resetStart($startRow);
54+
$this->setIterateOnlyExistingCells($iterateOnlyExistingCells);
5455
}
5556

5657
/**

src/PhpSpreadsheet/Worksheet/Row.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Row
66
{
7-
private ?Worksheet $worksheet;
7+
private Worksheet $worksheet;
88

99
/**
1010
* Row index.
@@ -30,7 +30,7 @@ public function __construct(Worksheet $worksheet, $rowIndex = 1)
3030
*/
3131
public function __destruct()
3232
{
33-
$this->worksheet = null;
33+
unset($this->worksheet);
3434
}
3535

3636
/**
@@ -47,9 +47,9 @@ public function getRowIndex(): int
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 getCellIterator($startColumn = 'A', $endColumn = null): RowCellIterator
50+
public function getCellIterator($startColumn = 'A', $endColumn = null, bool $iterateOnlyExistingCells = false): RowCellIterator
5151
{
52-
return new RowCellIterator($this->getWorksheet(), $this->rowIndex, $startColumn, $endColumn);
52+
return new RowCellIterator($this->worksheet, $this->rowIndex, $startColumn, $endColumn, $iterateOnlyExistingCells);
5353
}
5454

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

6666
/**
@@ -110,7 +110,6 @@ public function isEmpty(int $definitionOfEmptyFlags = 0, $startColumn = 'A', $en
110110
*/
111111
public function getWorksheet(): Worksheet
112112
{
113-
// @phpstan-ignore-next-line
114113
return $this->worksheet;
115114
}
116115
}

src/PhpSpreadsheet/Worksheet/RowCellIterator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ class RowCellIterator extends CellIterator
4545
* @param string $startColumn The column address at which to start iterating
4646
* @param string $endColumn Optionally, the column address at which to stop iterating
4747
*/
48-
public function __construct(Worksheet $worksheet, $rowIndex = 1, string $startColumn = 'A', $endColumn = null)
48+
public function __construct(Worksheet $worksheet, $rowIndex = 1, $startColumn = 'A', $endColumn = null, bool $iterateOnlyExistingCells = false)
4949
{
5050
// Set subject and row index
5151
$this->worksheet = $worksheet;
5252
$this->cellCollection = $worksheet->getCellCollection();
5353
$this->rowIndex = $rowIndex;
5454
$this->resetEnd($endColumn);
5555
$this->resetStart($startColumn);
56+
$this->setIterateOnlyExistingCells($iterateOnlyExistingCells);
5657
}
5758

5859
/**
Lines changed: 32 additions & 0 deletions
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/PhpSpreadsheetTests/Worksheet/ByColumnAndRowTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ public function testGetCommentByColumnAndRow(): void
204204
->getComment('B2')
205205
->getText()->createTextRun('My Test Comment');
206206

207-
$comment /** @scrutinizer ignore-deprecated */
208-
= $sheet->getCommentByColumnAndRow(2, 2);
207+
$comment = /** @scrutinizer ignore-deprecated */ $sheet->getCommentByColumnAndRow(2, 2);
209208
self::assertInstanceOf(Comment::class, $comment);
210209
self::assertSame('My Test Comment', $comment->getText()->getPlainText());
211210
$spreadsheet->disconnectWorksheets();

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

8.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)