Skip to content

Commit

Permalink
Fixed SkipsEmptyRows support with the WithColumnLimit concern (#3455)
Browse files Browse the repository at this point in the history
* Fixed SkipsEmptyRows support with the WithColumnLimit concern

* Fixed Code Style

* Added spaces in the PHPDoc related to the isEmpty function

Co-authored-by: Francesco Silvestro <francy891@hotmai.it>
  • Loading branch information
fsilvestro and Francesco Silvestro authored Dec 22, 2021
1 parent 1e3a1f6 commit ad981fd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Fixed

- Fix `SkipsEmptyRows` support with the `WithColumnLimit` concern
- formatColumn added range support

## [3.1.34] - 2021-12-2
Expand Down
6 changes: 4 additions & 2 deletions src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,13 @@ public function toArray($nullValue = null, $calculateFormulas = false, $formatDa
}

/**
* @param bool $calculateFormulas
* @param string|null $endColumn
* @return bool
*/
public function isEmpty($calculateFormulas = false): bool
public function isEmpty($calculateFormulas = false, ?string $endColumn = null): bool
{
return count(array_filter($this->toArray(null, $calculateFormulas, false))) === 0;
return count(array_filter($this->toArray(null, $calculateFormulas, false, $endColumn))) === 0;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public function toArray($import, int $startRow = null, $nullValue = null, $calcu
foreach ($this->worksheet->getRowIterator($startRow, $endRow) as $index => $row) {
$row = new Row($row, $headingRow);

if ($import instanceof SkipsEmptyRows && $row->isEmpty($calculateFormulas)) {
if ($import instanceof SkipsEmptyRows && $row->isEmpty($calculateFormulas, $endColumn)) {
continue;
}

Expand Down
46 changes: 46 additions & 0 deletions tests/Concerns/WithColumnLimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Maatwebsite\Excel\Tests\Concerns;

use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\ToArray;
use Maatwebsite\Excel\Concerns\WithColumnLimit;
use Maatwebsite\Excel\Tests\TestCase;
Expand Down Expand Up @@ -52,4 +53,49 @@ public function endColumn(): string

$import->import('import-users.xlsx');
}

/**
* @test
*/
public function can_import_to_array_with_column_limit_and_skips_empty_rows()
{
$import = new class implements ToArray, WithColumnLimit, SkipsEmptyRows
{
use Importable;

/**
* @param array $array
*/
public function array(array $array)
{
Assert::assertEquals([
[
'Test1',
'Test2',
null,
null,
],
[
'Test3',
'Test4',
null,
null,
],
[
'Test5',
'Test6',
null,
null,
],
], $array);
}

public function endColumn(): string
{
return 'D';
}
};

$import->import('import-empty-rows.xlsx');
}
}

0 comments on commit ad981fd

Please sign in to comment.