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]: Rows with Drawings broken since v3.1.47 #4134

Open
1 task done
aaronhuisinga opened this issue May 17, 2024 · 1 comment
Open
1 task done

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

aaronhuisinga opened this issue May 17, 2024 · 1 comment
Labels

Comments

@aaronhuisinga
Copy link
Contributor

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.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 aaronhuisinga changed the title [Bug]: Drawings and Headers broken since v3.1.47 [Bug]: Rows with Drawings broken since v3.1.47 May 17, 2024
@aaronhuisinga
Copy link
Contributor Author

aaronhuisinga commented Jun 8, 2024

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

@aaronhuisinga aaronhuisinga reopened this Jun 8, 2024
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

1 participant