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

feat: limit imports #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
/.idea
/build
.phpunit.result.cache
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ Import a `csv` with specific delimiter, enclosure characters and "gbk" encoding:
$collection = (new FastExcel)->configureCsv(';', '#', 'gbk')->import('file.csv');
```

Importing only the first 100 lines from a `csv` :

```php
$collection = (new FastExcel)->limitRows(100)->import('file.csv');
```

Import and insert to database:

```php
Expand Down
15 changes: 15 additions & 0 deletions src/FastExcel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class FastExcel
*/
private $start_row = 1;

/**
* @var ?int
*/
private $end_row = null;

/**
* @var bool
*/
Expand Down Expand Up @@ -127,6 +132,16 @@ public function startRow(int $row)
return $this;
}

/**
* @return $this
*/
public function limitRows(int $row = null)
{
$this->end_row = $row;

return $this;
}

/**
* @return $this
*/
Expand Down
7 changes: 6 additions & 1 deletion src/Importable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Trait Importable.
*
* @property int $start_row
* @property ?int $end_row
* @property bool $transpose
* @property bool $with_header
*/
Expand Down Expand Up @@ -140,7 +141,7 @@ private function importSheet(SheetInterface $sheet, callable $callback = null)

foreach ($sheet->getRowIterator() as $k => $rowAsObject) {
$row = $rowAsObject->toArray();
if ($k >= $this->start_row) {
if ($k >= $this->start_row ) {
if ($this->with_header) {
if ($k == $this->start_row) {
$headers = $this->toStrings($row);
Expand All @@ -161,6 +162,10 @@ private function importSheet(SheetInterface $sheet, callable $callback = null)
$collection[] = empty($headers) ? $row : array_combine($headers, $row);
}
}

if ($k > $this->end_row) {
break;
}
}

if ($this->transpose) {
Expand Down
36 changes: 36 additions & 0 deletions tests/LimitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rap2hpoutre\FastExcel\Tests;

use Rap2hpoutre\FastExcel\FastExcel;

/**
* Class ChunkTest.
*/
class LimitTest extends TestCase
{
public function collectionGenerator($n)
{
for ($i = 1; $i <= $n; $i++) {
yield collect(['a' => 'b', 'c' => 'd']);
}
}

public function arrayGenerator($n)
{
for ($i = 1; $i <= $n; $i++) {
yield ['a' => 'b', 'c' => 'd'];
}
}

public function testWithGenerator()
{
(new FastExcel($this->collectionGenerator(100)))->export(__DIR__.'/test-generator.xlsx');
$result = (new FastExcel())->limitRows(10)->import(__DIR__.'/test-generator.xlsx');
$this->assertEquals(
collect($this->arrayGenerator(10)),
$result
);
unlink(__DIR__.'/test-generator.xlsx');
}
}