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] WithColumnFormatting not working well #2931

Closed
5 tasks done
RayhanYulanda opened this issue Nov 23, 2020 · 13 comments
Closed
5 tasks done

[BUG] WithColumnFormatting not working well #2931

RayhanYulanda opened this issue Nov 23, 2020 · 13 comments
Labels

Comments

@RayhanYulanda
Copy link

RayhanYulanda commented Nov 23, 2020

Prerequisites

Versions

  • PHP version: 7.4
  • Laravel version: 6.2
  • Package version: 3.1.23

Description

I tried using WithColumnFormatting but it doesn't work at all.

public function columnFormats(): array
    {
        return [
            'A' => NumberFormat::FORMAT_TEXT,
            'G' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }

Steps to Reproduce

This is the result of G column
2020-12-01
2021-01-01
2021-03-01
2021-01-01
2021-01-01

This is the result of A column
1,96101E+17
1,96101E+17
1,96101E+17
1,96101E+17
1,96101E+17

Expected behavior:
I'm expecting it works well for format column :')

@patrickbrouwers
Copy link
Member

Have a look here: PHPOffice/PhpSpreadsheet#168

@RayhanYulanda
Copy link
Author

For FORMAT_TEXT i can solve it with cells bind but how about FORMAT_DATE_DDMMYYYY? I tried using cells bind but still can't work @patrickbrouwers

@patrickbrouwers
Copy link
Member

Try declaring a cell range, like G1:G100 and see if it applies then. If not, it might be a bug in PhpSpreadsheet

@c00p3r
Copy link

c00p3r commented Dec 9, 2020

@patrickbrouwers having same issue here...
could u plz elaborate how to set the cell range?
searched the docs backwards and forwards couple times... no luck :(

@patrickbrouwers
Copy link
Member

public function columnFormats(): array
    {
        return [
            'G1:G1000' => NumberFormat::FORMAT_DATE_DDMMYYYY,
        ];
    }

``

@c00p3r
Copy link

c00p3r commented Dec 9, 2020

oooh
I thought u were talking about special method ))
Thanks! I'll give it a try

@c00p3r
Copy link

c00p3r commented Dec 10, 2020

@patrickbrouwers this method is not being called during import
I debug it
same as 'prepareForValidation' and 'withValidator'
so it's not helping unfortunately

@RayhanYulanda I managed to fix it this way
for my xlsx file I've set all cell to TEXT format (not in code, but in Excel)
then in Laravel when u parse and validate - it works just fine
if u need Carbon object u parse the text value Carbon::parse('2/4/2020')

@patrickbrouwers
Copy link
Member

Columnformatting is only implemented for export yes, I'm okay with a PR that would add it for imports too.

@c00p3r
Copy link

c00p3r commented Dec 14, 2020

@RayhanYulanda and whoever sees this
there's also a config value u can change

    'value_binder' => [
        'default' => PhpOffice\PhpSpreadsheet\Cell\StringValueBinder::class,
    ],

this way (I guess) all values from your file will be parsed as strings

@RayhanYulanda
Copy link
Author

@RayhanYulanda and whoever sees this
there's also a config value u can change

    'value_binder' => [
        'default' => PhpOffice\PhpSpreadsheet\Cell\StringValueBinder::class,
    ],

this way (I guess) all values from your file will be parsed as strings

I dont want all of cells to be string because there is should be date value

@patrickbrouwers
Copy link
Member

You can implement your own value binder, there your have all control.

@yogithesymbian
Copy link

yogithesymbian commented Oct 12, 2021

solved by @c00p3r

    // customize
    public function map($events): array
    {
        return [
                $events->location,
                Date::dateTimeToExcel(Carbon::parse($events->date))
        ];
    }

because my api return as string , then in excel alr custom format but the value on cell is still string, so we need pas in mapping about to re map

@mohammedobaid1234
Copy link

mohammedobaid1234 commented Jun 19, 2023

This solution I think the best

public function columnFormats(): array
    {
        return [
            'H' => NumberFormat::FORMAT_TEXT,
        ];
    }
public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                $event->sheet->getStyle('H')->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_STRING);
            },
        ];
    }

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

5 participants