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

[PROPOSAL] Passing index to map() #2693

Closed
darron1217 opened this issue Jun 1, 2020 · 3 comments
Closed

[PROPOSAL] Passing index to map() #2693

darron1217 opened this issue Jun 1, 2020 · 3 comments
Labels

Comments

@darron1217
Copy link

darron1217 commented Jun 1, 2020

Prerequisites

Versions

  • PHP version: PHP 7.4.5 (cli) (built: Apr 14 2020 16:17:19) ( NTS Visual C++ 2017 x64 )
  • Laravel version: 5.5.49
  • Package version: 3.1.19

Description

Export Class which implements WithMapping interface has map($row) function to manipulate rows.

I needed to use index, but I couldn't get index data from it.

How about passing index parameter to map()?

Example

Make those lines
https://github.com/Maatwebsite/Laravel-Excel/blob/90977aa13e687f7a11eda97a5037267919cfd1f0/src/Sheet.php#L530-L538

into this (just adding $index is enough!)

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

            return ArrayHelper::ensureMultipleRows(
                static::mapArraybleRow($row)
            );
        })->toArray();

Also
https://github.com/Maatwebsite/Laravel-Excel/blob/90977aa13e687f7a11eda97a5037267919cfd1f0/src/Sheet.php#L310-L322

into

        foreach ($this->worksheet->getRowIterator($startRow, $endRow) as $index => $row) {
            $row = (new Row($row, $headingRow))->toArray($nullValue, $calculateFormulas, $formatData);

            if ($import instanceof WithMapping) {
                $row = $import->map($row, $index);
            }

            $rows[] = $row;

            if ($import instanceof WithProgressBar) {
                $import->getConsoleOutput()->progressAdvance();
            }
        }

Also
https://github.com/Maatwebsite/Laravel-Excel/blob/90977aa13e687f7a11eda97a5037267919cfd1f0/src/Concerns/WithMapping.php#L7-L12

into

    /**
     * @param mixed $row
     * @param int $index
     *
     * @return array
     */
    public function map($row, $index): array;

Additional Information

@patrickbrouwers
Copy link
Member

That would be a breaking change, so that won't be possible. The next release will have a RemembersRowNumber that will help you with this situation.

@allestaire
Copy link

Is this functionality already on version 3?

@bkkrishna
Copy link

bkkrishna commented Mar 4, 2023

+1. Is this feature added in v3? A simple workaround is here though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants