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

[Bug]: WithMapping does not provide index and / or rowOffset #4285

Open
1 task done
boxsnake opened this issue Mar 22, 2025 · 1 comment
Open
1 task done

[Bug]: WithMapping does not provide index and / or rowOffset #4285

boxsnake opened this issue Mar 22, 2025 · 1 comment
Labels

Comments

@boxsnake
Copy link

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

  • Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.62

What version of Laravel are you using?

11.36.1

What version of PHP are you using?

8.3.17

Describe your issue

In my case, I am going to generate a worksheet with following situations:

  • each data row mapped to 4 sheet rows
  • a formula is in the rows

Hope rowIndex or rowOffset (excel row number) can be passed to map so that it can be easier in such case.

How can the issue be reproduced?

<?php
class DemoExport implements WithMapping, FromCollection
{
    protected Collection $rows;

    public function __construct(
        Collection $rows
    ) {
        $this->rows = $rows;
    }

    public function map($row): array
    {
        // In my case, I cast a `index` field to my data
        // However, it is not so elegent to inject a new field only for render
        $formula = sprintf('=F%u*G%u', $row->index * 4, $row->index * 4 + 3);
        return [
          [
            // Row 1
          ],
          [
            // Row 2
          ],
          [
            // Row 3
          ],
          [
            // Row 4
            $formula
          ]
        ];
    }
}

What should be the expected behaviour?

<?php
class DemoExport implements WithMapping, FromCollection
{
    protected Collection $rows;

    public function __construct(
        Collection $rows
    ) {
        $this->rows = $rows;
    }

    public function map($row, $index): array
    {
        $formula = sprintf('=F%u*G%u', $index * 4, $index * 4 + 3);
        return [
          [
            // Row 1
          ],
          [
            // Row 2
          ],
          [
            // Row 3
          ],
          [
            // Row 4
            $formula
          ]
        ];
    }
}

or

<?php
class DemoExport implements WithMapping, FromCollection
{
    protected Collection $rows;

    public function __construct(
        Collection $rows
    ) {
        $this->rows = $rows;
    }

    public function map($row, $index): array
    {
        // `$startRow` is where this map started
        $formula = sprintf('=F%u*G%u', $startRow, $startRow + 3);
        return [
          [
            // Row 1
          ],
          [
            // Row 2
          ],
          [
            // Row 3
          ],
          [
            // Row 4
            $formula
          ]
        ];
    }
}
@boxsnake boxsnake added the bug label Mar 22, 2025
@patrickbrouwers
Copy link
Member

If you use collection you can call the map on the collection itself and not use the withmapping concern.

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

2 participants