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

Allow to add OpenSpout\Common\Entity\Cell instances as data collection values #306

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kusab85
Copy link

@kusab85 kusab85 commented Jan 12, 2023

Add ability to feed FastExcel exporter (directly or via callback) with data consisting of instances of OpenSpout\Common\Entity\Cell,
which gives possibility to style every column (or even every cell) independently.

For example code like this:

$money_style = (new Style())->setCellAlignment(CellAlignment::RIGHT)->setFormat('$ # ##0.00');
$data        = [
    [
        'Employee' => 'William Smith',
        'Salary'   => Cell::fromValue(3400.00, $money_style),
        'Tax'      => Cell::fromValue(420.40, $money_style),
    ],
    [
        'Employee' => 'Dany Carey',
        'Salary'   => Cell::fromValue(4300.00, $money_style),
        'Tax'      => Cell::fromValue(220.40, $money_style),
    ],
    [
        'Employee' => 'Matt Cameron',
        'Salary'   => Cell::fromValue(3300.00, $money_style),
        'Tax'      => Cell::fromValue(222.43, $money_style),
    ],
];

(new FastExcel($data))
    ->headerStyle((new Style())->setCellAlignment(CellAlignment::CENTER)->setFontBold())
    ->configureWriterUsing(function ($writer) {
        $writer->getOptions()->setColumnWidth(20, 1);
        $writer->getOptions()->setColumnWidth(10, 2, 3);
    })
    ->export($file);

gives:

obraz

Or code like this:

$data = [
    [
        'col1' => Cell::fromValue('row1 col1', (new Style())->setFontBold()->setCellAlignment(CellAlignment::RIGHT)),
        'col2' => 'row1 col2'
    ],
    [
        'col1' => Cell::fromValue('row2 col1', (new Style())->setFontItalic()->setCellAlignment(CellAlignment::RIGHT)),
        'col2' => Cell::fromValue('row2 col2', (new Style())->setFontSize(20)->setBackgroundColor(Color::LIGHT_GREEN)),
    ],
    [
        'col1' => 'row3 col1',
        'col2' => Cell::fromValue('row3 col2', (new Style())->setBackgroundColor(Color::YELLOW)),
    ],
];

(new FastExcel($data))
    ->headerStyle((new Style())->setCellAlignment(CellAlignment::CENTER)->setFontBold())
    ->rowsStyle((new Style())->setBackgroundColor(Color::LIGHT_BLUE))
    ->configureWriterUsing( function ($writer ) {
        $writer->getOptions()->setColumnWidth(20, 1, 2);
    })
    ->export($file);

gives:

obraz

@kusab85 kusab85 force-pushed the feature-allow-cells-as-data-items branch 7 times, most recently from 820e5a1 to 6ed8c5c Compare January 13, 2023 14:30
Apply StyleCI patch

Fix  Codacy Static Code Analysis Issues

- The method exportOrDownload() has an NPath complexity of 300. The configured NPath comp1lexity threshold is 200.
- The method exportOrDownload() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
- Avoid using static access to class '\OpenSpout\Common\Entity\Row' in method 'writeHeader'.

Fix  test with 'ubuntu-latest, 8.1, lowest' build

Extract Cell and Row creations to single methods

Remove static Cell creation from Exportable trait

Eat this  Codacy Static Code Analyzer

Enough joking , Codacy
@kusab85 kusab85 force-pushed the feature-allow-cells-as-data-items branch from 6ed8c5c to 09d0381 Compare February 14, 2023 13:25
@duypdx
Copy link

duypdx commented Oct 23, 2024

@rap2hpoutre Oh I found this very helpful, could you please take a look at it for me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants