Skip to content

Commit

Permalink
Fix for malformed tables
Browse files Browse the repository at this point in the history
  • Loading branch information
fab120 committed Feb 23, 2021
1 parent a1f0efb commit de1af45
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All notable changes to `excel-reader` will be documented in this file.

## v1.1.1 - 2021-02-23
- Fix for malformed tables

## v1.1.0 - 2021-02-23
- `ExcelReader` now requires `FileInterface` instead of `string` in `__construct`
- Deprecated `ExcelReader::createFromFile` in favor of `ExcelReader::createFromPath`
Expand Down
13 changes: 12 additions & 1 deletion src/Libraries/BoxSpout2Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function read(string $path, bool $hasHeaders, $sheetId): array
$function = is_int($sheetId) ? 'getIndex' : 'getName';

$headers = [];
$headersCount = 0;
$filler = [];
$data = [];

$slugify = $hasHeaders ? new Slugify() : null;
Expand All @@ -47,11 +49,20 @@ public function read(string $path, bool $hasHeaders, $sheetId): array
if ($first) {
$headers = array_map(static fn ($cell) => $slugify->slugify($cell), $row);

$headersCount = count($headers);
$filler = array_fill(0, $headersCount, null);

$first = false;
continue;
}

$data[] = $row;
$currentData = $row + $filler;

if ($hasHeaders && count($currentData) > $headersCount) {
$currentData = array_slice($currentData, 0, $headersCount);
}

$data[] = $currentData;
}

break;
Expand Down
22 changes: 17 additions & 5 deletions src/Libraries/BoxSpout3Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ public function read(string $path, bool $hasHeaders, $sheetId): array
$function = is_int($sheetId) ? 'getIndex' : 'getName';

$headers = [];
$headersCount = 0;
$filler = [];
$data = [];

$slugify = $hasHeaders ? new Slugify() : null;

foreach ($reader->getSheetIterator() as $sheet) {
if ($sheet->$function() !== $sheetId) {
continue;
}

$slugify = new Slugify();
$first = $hasHeaders;

/** @var \Box\Spout\Common\Entity\Row $row */
Expand All @@ -47,14 +50,23 @@ public function read(string $path, bool $hasHeaders, $sheetId): array
$row->getCells(),
);

$headersCount = count($headers);
$filler = array_fill(0, $headersCount, null);

$first = false;
continue;
}

$data[] = array_map(
static fn (Cell $cell) => $cell->getValue(),
$row->getCells(),
);
$currentData = array_map(
static fn (Cell $cell) => $cell->getValue(),
$row->getCells(),
) + $filler;

if ($hasHeaders && count($currentData) > $headersCount) {
$currentData = array_slice($currentData, 0, $headersCount);
}

$data[] = $currentData;
}

break;
Expand Down
27 changes: 27 additions & 0 deletions tests/Unit/ExcelReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Webnuvola\ExcelReader\Tests\Unit;

use DateTime;
use RuntimeException;
use Webnuvola\ExcelReader\ExcelReader;
use Webnuvola\ExcelReader\ExcelReaderManager;
use Webnuvola\ExcelReader\Libraries\BoxSpout2Library;
Expand Down Expand Up @@ -134,4 +135,30 @@ public function read_file_from_string()

$this->assertCount(700, $excel);
}

/** @test */
public function malformed_table_with_headers()
{
$excel = ExcelReader::createFromPath(__DIR__.'/../resources/malformed-table.xlsx')
->read();

$this->assertEquals([
['column-a' => 'Value A1', 'column-b' => 'Value B1', 'column-c' => 'Value C1', 'column-d' => 'Value D1'],
['column-a' => 'Value A2', 'column-b' => 'Value B2', 'column-c' => 'Value C2', 'column-d' => null],
], $excel);
}

/** @test */
public function malformed_table_without_headers()
{
$excel = ExcelReader::createFromPath(__DIR__.'/../resources/malformed-table.xlsx')
->withoutHeaders()
->read();

$this->assertEquals([
['Column A', 'Column B', 'Column C', 'Column D'],
['Value A1', 'Value B1', 'Value C1', 'Value D1', 'Value E1'],
['Value A2', 'Value B2', 'Value C2'],
], $excel);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function temporary_file_is_deleted()
$file = FileFactory::createFromString(self::$excelContent, 'xlsx');

$path = $file->getPath();
$this->assertTrue(file_exists($path));
$this->assertFileExists($path);

unset($file);
$this->assertFalse(file_exists($path));
$this->assertFileDoesNotExist($path);
}
}
Binary file added tests/resources/malformed-table.xlsx
Binary file not shown.

0 comments on commit de1af45

Please sign in to comment.