SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.22k stars 1.91k forks source link

[Bug]: FromArray exports only 1000 rows #4162

Open ItprojektRepo opened 2 months ago

ItprojektRepo commented 2 months ago

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

What version of Laravel Excel are you using?

3.1.55

What version of Laravel are you using?

11.10.0

What version of PHP are you using?

8.2.7

Describe your issue

FromArray export has only 1000 rows and only last data is present. Found that problem is in Sheet.php -> appendRows function It chunks 1000 and every time starts from cell A1. Meaning it overwrites existing data instead of appending

`public function appendRows($rows, $sheetExport) { if (method_exists($sheetExport, 'prepareRows')) { $rows = $sheetExport->prepareRows($rows); }

    $rows = $rows instanceof LazyCollection ? $rows : new Collection($rows);

    $rows->flatMap(function ($row) use ($sheetExport) {
        if ($sheetExport instanceof WithMapping) {
            $row = $sheetExport->map($row);
        }

        if ($sheetExport instanceof WithCustomValueBinder) {
            SpreadsheetCell::setValueBinder($sheetExport);
        }

        return ArrayHelper::ensureMultipleRows(
            static::mapArraybleRow($row)
        );
    })->chunk(1000)->each(function ($rows) use ($sheetExport) {
        $this->append(
            $rows->toArray(),
            $sheetExport instanceof WithCustomStartCell ? $sheetExport->startCell() : null,
            $this->hasStrictNullComparison($sheetExport)
        );
    });
}`

In here changing ->chunk(1000) to ->chunk(10000) prints all data

Reproducing: Export array that has more than 1000 rows. Using FromArray.

Currently using Concerns FromArray, WithTitle, WithCustomValueBinder

How can the issue be reproduced?

Reproducing: Export array that has more than 1000 rows. Using FromArray.

Currently using Concerns FromArray, WithTitle, WithCustomValueBinder

What should be the expected behaviour?

First rows are overwritten by rows that are over 1000

ItprojektRepo commented 2 months ago

Update.

First cell has to have a value in it. I had cell A1 empty and then it occured. If I put random valu in there then everything works.

So problem is instead in hasRows function `private function hasRows(): bool { $startCell = 'A1'; if ($this->exportable instanceof WithCustomStartCell) { $startCell = $this->exportable->startCell(); }

    return $this->worksheet->cellExists($startCell);
}

`

owaishussain commented 1 month ago

@ItprojektRepo is it resolved on your end?

kukumagi commented 1 month ago

@ItprojektRepo is it resolved on your end?

I filled A1 cell with a static value for it to work. If I leave it blank then it overwrites first 1000 rows. And looking at this logic it will always overwrite every 1000 rows if I would have a blank value in column A.

So I simply made changes so my code would work, but the bug still exists.

iactor commented 3 weeks ago

Replacing blank data with non blank data, such as "-", can solve the problem

patrickbrouwers commented 3 weeks ago

I don't see an easy other way to check if we are inserting in the first row, or if we are appending. If someone has a better way of doing this, please create a PR :)