Skip to content

Commit

Permalink
Fix ODS read filter on number-columns-repeated cell (#3149)
Browse files Browse the repository at this point in the history
  • Loading branch information
JokubasR authored Nov 6, 2022
1 parent a5868a0 commit e05e354
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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) {
++$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.

0 comments on commit e05e354

Please sign in to comment.