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]: ErrorException: Undefined variable: sheetExport #4065

Closed
1 task done
jerevive opened this issue Jan 3, 2024 · 6 comments
Closed
1 task done

[Bug]: ErrorException: Undefined variable: sheetExport #4065

jerevive opened this issue Jan 3, 2024 · 6 comments
Labels

Comments

@jerevive
Copy link

jerevive commented Jan 3, 2024

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.26

What version of Laravel are you using?

5.8.38

What version of PHP are you using?

7.3.33

Describe your issue

ErrorException: Undefined variable: sheetExport in /www/UserManagerTest/vendor/maatwebsite/excel/src/Jobs/QueueExport.php:76

How can the issue be reproduced?

    /**
     * @param Writer $writer
     *
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     */
    public function handle(Writer $writer)
    {
        (new LocalizeJob($this->export))->handle($this, function () use ($writer) {
            $writer->open($this->export);

            $sheetExports = [$this->export];
            if ($this->export instanceof WithMultipleSheets) {
                $sheetExports = $this->export->sheets();
            }

            // Pre-create the worksheets
            foreach ($sheetExports as $sheetIndex => $sheetExport) {
                $sheet = $writer->addNewSheet($sheetIndex);
                $sheet->open($sheetExport);
            }

            // Write to temp file with empty sheets.
            $writer->write($sheetExport, $this->temporaryFile, $this->writerType);
        });
    }

What should be the expected behaviour?

norlmal

@jerevive jerevive added the bug label Jan 3, 2024
@horgolzari98
Copy link

The error you're encountering seems to be related to an undefined variable $sheetExport within the handle method of the QueueExport.php file. The error occurs specifically in this line:

$writer->write($sheetExport, $this->temporaryFile, $this->writerType);

It appears that there's a typo or a variable naming issue. The correct variable name should be $sheetExports (plural), as that is the array you defined earlier:

$sheetExports = [$this->export];

However, within the loop, you are iterating over $sheetExports (plural):

foreach ($sheetExports as $sheetIndex => $sheetExport) {
    $sheet = $writer->addNewSheet($sheetIndex);
    $sheet->open($sheetExport);
}

So, when you try to access $sheetExport outside the loop, it's undefined. To fix this issue, you should use $sheetExports instead of $sheetExport in the write method:

$writer->write($sheetExports, $this->temporaryFile, $this->writerType);

Here's the corrected version of the handle method:

public function handle(Writer $writer)
{
    (new LocalizeJob($this->export))->handle($this, function () use ($writer) {
        $writer->open($this->export);

        $sheetExports = [$this->export];
        if ($this->export instanceof WithMultipleSheets) {
            $sheetExports = $this->export->sheets();
        }

        // Pre-create the worksheets
        foreach ($sheetExports as $sheetIndex => $sheetExport) {
            $sheet = $writer->addNewSheet($sheetIndex);
            $sheet->open($sheetExport);
        }

        // Write to temp file with empty sheets.
        $writer->write($sheetExports, $this->temporaryFile, $this->writerType);
    });
}

Make sure to replace the problematic line with the corrected one, and the error should be resolved.

@jerevive
Copy link
Author

jerevive commented Jan 4, 2024

Thank you for your answer, this part of the code is the source code, documents for maatwebsite/excel/SRC/Jobs/QueueExport. PHP, I directly modify the source code is not very good @horgolzari98

@patrickbrouwers
Copy link
Member

Sounds like your sheets() method doesn't return sheet objects.

@jerevive
Copy link
Author

jerevive commented Jan 5, 2024

I nested a layer of laravel job synchronously and put it on the queue and executed it on the cli @patrickbrouwers

@patrickbrouwers
Copy link
Member

I'm not sure I understand what you mean. Can you show the code of the sheets() method

@patrickbrouwers
Copy link
Member

Closing as this is a usage error. I've added a more explicit exception when this happens.

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

3 participants