Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ODS read filter on number-columns-repeated cell #3149

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/PhpSpreadsheet/Reader/Ods.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,15 @@ public function loadIntoExisting($filename, Spreadsheet $spreadsheet)
foreach ($childNode->childNodes as $cellData) {
if ($this->getReadFilter() !== null) {
if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
++$columnID;
if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) {
$colRepeats = (int) $cellData->getAttributeNS($tableNs, 'number-columns-repeated');
} else {
$colRepeats = 1;
}

for ($i = 0; $i < $colRepeats; ++$i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a loop, and not simply $columnID += $colRepeats;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Got it; $columnID is the column address, not its index

++$columnID;
}

continue;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Ods/RepeatedColumnsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;

use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PHPUnit\Framework\TestCase;

class RepeatedColumnsTest extends TestCase
{
/**
* @var Ods
*/
private $reader;

protected function setUp(): void
{
$this->reader = new Ods();
}

public function testDefinedNames(): void
{
$this->reader->setReadFilter(
new class () implements IReadFilter {
public function readCell($columnAddress, $row, $worksheetName = ''): bool
{
return in_array($columnAddress, ['A', 'C', 'E', 'G', 'J', 'K'], true);
}
}
);
$spreadsheet = $this->reader->load('tests/data/Reader/Ods/RepeatedCells.ods');
$worksheet = $spreadsheet->getActiveSheet();

self::assertEquals('TestA', $worksheet->getCell('A1')->getValue());
self::assertNull($worksheet->getCell('C1')->getValue());
self::assertEquals('TestE', $worksheet->getCell('E1')->getValue());
self::assertEquals('TestG', $worksheet->getCell('G1')->getValue());
self::assertEquals('A', $worksheet->getCell('J1')->getValue());
self::assertEquals('TestK', $worksheet->getCell('K1')->getValue());
}
}
Binary file added tests/data/Reader/Ods/RepeatedCells.ods
Binary file not shown.