SpartnerNL / Laravel-Excel

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

[Bug]: Rows with Drawings broken since v3.1.47 #4134

Closed aaronhuisinga closed 1 week ago

aaronhuisinga commented 3 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.x

What version of PHP are you using?

8.3.0

Describe your issue

A bug was introduced in #3873 that broke how drawing behave and results in incorrect exports. I left a comment on it when it was merged originally but I think it was lost in the flow of things and still hasn't been resolved. We had been locked at v3.1.46 until we needed to update to Laravel 11 which required a newer version of this package. We discovered then that this bug still exists.

What this did pre-change was add an image to cell A1, leave the rest of the first row blank, and add footers in row 2. After that, student data was printed.

With the drawing being inserted at the end, it appears to break things pretty badly. I don't think we're doing anything we aren't supposed to be in our export logic, and so it seems to me this is an unintended change.

How can the issue be reproduced?

<?php

namespace App\Exports;

use App\Models\Student;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

class StudentProgressExport implements FromQuery, ShouldAutoSize, ShouldQueue, WithDrawings, WithHeadings, WithMapping
{
    use Exportable;

    public int $student;
    public string $logo;

    public function __construct($student, $logo)
    {
        $this->student = $student;
        $this->logo = $logo;
    }

    /**
     * Build the query for export.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function query()
    {
        return Student::active();
    }

    /**
     * Map the Student fields for export.
     *
     * @param Student $student
     *
     * @return array
     */
    public function map($student): array
    {
        return [
            $student->Name,
            $student->Email,
            $student->Completed1 ? '✓' : '',
            $student->Completed2 ? '✓' : '',
            $student->CompletedAt,
        ];
    }

    /**
     * Create an array of fields to be included in the export.
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            [
                '',
            ],
            [
                'Name',
                'Email',
                'Test 1',
                'Test 2',
                'Completion Date',
            ]
        ];
    }

    public function drawings()
    {
        $image_contents = file_get_contents($this->logo);
        $temp_image = tempnam(sys_get_temp_dir(), 'logo-');
        file_put_contents($temp_image, $image_contents);

        $drawing = new Drawing();
        $drawing->setName('Logo');
        $drawing->setDescription(__('general.logo'));
        $drawing->setPath($temp_image);
        $drawing->setHeight(90);
        $drawing->setCoordinates('A1');

        return $drawing;
    }
}
broken

What should be the expected behaviour?

Pre v3.1.47, the drawing properly sits above the rest of the spreadsheet data.

correct
aaronhuisinga commented 2 months ago

I somehow accidentally closed this issue despite it not being fixed. I have no idea how/why I did that!

stale[bot] commented 1 week ago

This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.