Laravel-excel: QUESTION: How do I set bold column styling using columnFormats

Created on 4 Jun 2018  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [x ] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [ x] Checked that your issue isn't already filed.
  • [ x] Checked if no PR was submitted that fixes this problem.

Versions

3.0

  • PHP version: 7.2
  • Laravel version: 5.6
  • Package version: 3.0

Description

How do I set the entire headings row to be bold if I am using a custom export class and using the headings function to set the column headings and if I'm using the map function to set the column values?

class ReferralsExport implements FromCollection, WithMapping, WithColumnFormatting, WithHeadings, ShouldAutoSize
{

    private $users;

    public function __construct(LengthAwarePaginator $users)
    {
        $this->users = $users;
    }

    public function collection()
    {
        return $this->users;
    }

    public function map($user): array
    {
        return [
            $user->name ?? $user->meta->first_name . " " . $user->meta->last_name,
            $user->email,
            $user->company->name,
            $user->is_active ? 'Yes' : 'No',
            $user->created_at,
            $user->referredUsers->count(),
            'THIS IS YET TO BE IMPLEMENTED',
            'THIS IS YET TO BE IMPLEMENTED'
        ];
    }

    /**
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            'A' => '',
            'B' => '',
        ];
    }

    public function headings(): array
    {
        return [
            'Name',
            'Email',
            'Company',
            'Is active',
            'Date registered',
            'Users referred',
            'Paying users referred',
            'Total revenue generated',
        ];
    }
}

Steps to Reproduce

Expected behavior:

Actual behavior:

Additional Information

Any additional information, configuration or data that might be necessary to reproduce the issue.

Most helpful comment

Okay so for anyone else who faces this problem, the documentation as mentioned by other people isn't the best but after digging through the Style classes you have to do it like this:

Add this to your export class:

public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getStyle('A1:N1')->applyFromArray([
                    'font' => [
                        'bold' => true
                    ]
                ]);
            },
        ];
    }
}

Also don't forget to import the Event classes:

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;

class ReferralsExport implements WithEvents {
....

All 7 comments

Thanks for submitting the ticket. Unfortunately the information you provided is incomplete. We need to know which version you use and how to reproduce it. Please include code examples. Before we can pick it up, please check (https://github.com/Maatwebsite/Laravel-Excel/blob/3.0/.github/ISSUE_TEMPLATE.md) and add the missing information. To make processing of this ticket a lot easier, please make sure to check (https://laravel-excel.maatwebsite.nl/docs/3.0/getting-started/contributing) and double-check if you have filled in the issue template correctly. This will allow us to pick up your ticket more efficiently. Issues that follow the guidelines correctly will get priority over other issues.

Any updates on this? Not sure why this silly bot is commenting this but it makes it appear as if it is waiting for a reply.

This "silly bot" is just there to remind people to give the most information possible. When you created the issue it was missing the steps to reproduce, you added them later.

Columnformat is not meant for styling, you need to use cell styling for that, see examples in extending docs: https://laravel-excel.maatwebsite.nl/docs/3.0/export/extending

Okay so for anyone else who faces this problem, the documentation as mentioned by other people isn't the best but after digging through the Style classes you have to do it like this:

Add this to your export class:

public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getStyle('A1:N1')->applyFromArray([
                    'font' => [
                        'bold' => true
                    ]
                ]);
            },
        ];
    }
}

Also don't forget to import the Event classes:

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\AfterSheet;

class ReferralsExport implements WithEvents {
....

The docs are PR-able as well, so if you have suggestions to make it better, go ahead and create a PR. Do note that it's not this package documentation responsibility to document all parent package's methods.

That's true, but any classes that encapsulate or wrap around parent package classes should be well documented, after all we are using this package's classes for a reason, otherwise we'd just use the parent package. I didn't say anything about creating a PR.

All behaviour that is added by this package is documented. Again, if you have suggestion to improve it, please PR it. Just complaining about it doesn't help anybody ;)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vandolphreyes picture vandolphreyes  路  3Comments

disto picture disto  路  3Comments

ellej16 picture ellej16  路  3Comments

lucatamtam picture lucatamtam  路  3Comments

kurianic picture kurianic  路  3Comments