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: Row::toArray() respecting end column #3979

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ All notable changes to this project will be documented in this file.
## [Unreleased]

### Fixed
- Bug preventing WithChunkReading from working with multiple sheets when using ToCollection or ToArray
- Bug preventing WithChunkReading from working with multiple sheets when using ToCollection or ToArray
- Row::toArray() now always respects the endColumn
- Bug that could delete the import file before all the jobs had finished when using WithChunkReading and ShouldQueueWithoutChain
- Fixed issue where isEmptyWhen was not being called when using OnEachRow


## [3.1.47] - 2023-02-16

- Support Laravel 10
Expand Down
8 changes: 7 additions & 1 deletion src/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Row implements ArrayAccess
*/
protected $rowCacheFormatData;

/**
* @var string|null
*/
protected $rowCacheEndColumn;

/**
* @param SpreadsheetRow $row
* @param array $headingRow
Expand Down Expand Up @@ -83,7 +88,7 @@ public function toCollection($nullValue = null, $calculateFormulas = false, $for
*/
public function toArray($nullValue = null, $calculateFormulas = false, $formatData = true, ?string $endColumn = null)
{
if (is_array($this->rowCache) && ($this->rowCacheFormatData === $formatData)) {
if (is_array($this->rowCache) && ($this->rowCacheFormatData === $formatData) && ($this->rowCacheEndColumn === $endColumn)) {
return $this->rowCache;
}

Expand Down Expand Up @@ -112,6 +117,7 @@ public function toArray($nullValue = null, $calculateFormulas = false, $formatDa

$this->rowCache = $cells;
$this->rowCacheFormatData = $formatData;
$this->rowCacheEndColumn = $endColumn;

return $cells;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Concerns/OnEachRowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,32 @@ public function onRow(Row $row)

$this->assertEquals(2, $import->called);
}

/**
* @test
*/
public function it_respects_the_end_column()
{
$import = new class implements OnEachRow
{
use Importable;

/**
* @param Row $row
*/
public function onRow(Row $row)
{
// Accessing a row as an array calls toArray() without an end
// column. This saves the row in the cache, so we have to
// invalidate the cache once the end column changes
$row[0];

Assert::assertEquals([
'test',
], $row->toArray(null, false, true, 'A'));
}
};

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