diff --git a/packages/actions/docs/07-prebuilt-actions/09-export.md b/packages/actions/docs/07-prebuilt-actions/09-export.md index f6d568257b..8a63442b1e 100644 --- a/packages/actions/docs/07-prebuilt-actions/09-export.md +++ b/packages/actions/docs/07-prebuilt-actions/09-export.md @@ -497,24 +497,26 @@ You can only specify a single character, otherwise an exception will be thrown. ## Styling headers -If you want to style the headers of the XLSX file, you may override the `getHeadingStyle()` method on the exporter class, returning a `CellStyle` object: +If you want to style the headers of the XLSX file, you may override the `getHeadingStyle()` method on the exporter class, returning a `Style` object: ```php -use Filament\Actions\Exports\CellStyle; -use Filament\Support\Enums\VerticalAlignment; +use OpenSpout\Common\Entity\Style\CellAlignment; +use OpenSpout\Common\Entity\Style\CellVerticalAlignment; +use OpenSpout\Common\Entity\Style\Color; +use OpenSpout\Common\Entity\Style\Style; -public function getHeadingStyle(): CellStyle +public function getHeadingStyle(): Style { - return CellStyle::make() - ->bold() - ->italic() - ->size(14) - ->family('Consolas') - ->color('rgb(255, 255, 77)') - ->backgroundColor('#000000') - ->alignment('center') - ->verticalAlignment(VerticalAlignment::Center); - } + return (new Style()) + ->setFontBold() + ->setFontItalic() + ->setFontSize(14) + ->setFontName('Consolas') + ->setFontColor(Color::rgb(255, 255, 77)) + ->setBackgroundColor(Color::rgb(0, 0, 0)) + ->setCellAlignment(CellAlignment::CENTER) + ->setCellVerticalAlignment(CellVerticalAlignment::CENTER); +} ``` ## Customizing the export job diff --git a/packages/actions/src/Exports/CellStyle.php b/packages/actions/src/Exports/CellStyle.php deleted file mode 100644 index 86a91e459e..0000000000 --- a/packages/actions/src/Exports/CellStyle.php +++ /dev/null @@ -1,122 +0,0 @@ -bold = $bold; - - return $this; - } - - public function italic(bool $italic = true): static - { - $this->italic = $italic; - - return $this; - } - - public function underline(bool $underline = true): static - { - $this->underline = $underline; - - return $this; - } - - public function strikethrough(bool $strikethrough = true): static - { - $this->strikethrough = $strikethrough; - - return $this; - } - - public function size(int $size): static - { - $this->size = $size; - - return $this; - } - - public function family(string $fontFamily): static - { - $this->family = ucfirst(strtolower($fontFamily)); - - return $this; - } - - public function color(string $color): static - { - if (str($color)->startsWith('#')) { - $this->color = Hex::fromString($color); - } elseif (str($color)->startsWith('rgb')) { - $this->color = Rgb::fromString($color); - } - - return $this; - } - - public function backgroundColor(string $backgroundColor): static - { - if (str($backgroundColor)->startsWith('#')) { - $this->backgroundColor = Hex::fromString($backgroundColor); - } elseif (str($backgroundColor)->startsWith('rgb')) { - $this->backgroundColor = Rgb::fromString($backgroundColor); - } - - return $this; - } - - public function alignment(Alignment|string $alignment): static - { - if (is_string($alignment)) { - $alignment = Alignment::tryFrom($alignment); - } - - $this->alignment = $alignment; - - return $this; - } - - public function verticalAlignment(VerticalAlignment|string $verticalAlignment): static - { - if (is_string($verticalAlignment)) { - $verticalAlignment = VerticalAlignment::tryFrom($verticalAlignment); - } - - $this->verticalAlignment = $verticalAlignment; - - return $this; - } -} diff --git a/packages/actions/src/Exports/Exporter.php b/packages/actions/src/Exports/Exporter.php index 4375d7dbbb..bf83278fcd 100644 --- a/packages/actions/src/Exports/Exporter.php +++ b/packages/actions/src/Exports/Exporter.php @@ -8,6 +8,7 @@ use Filament\Actions\Exports\Models\Export; use Filament\Forms\Components\Component; use Illuminate\Database\Eloquent\Model; use Illuminate\Queue\Middleware\WithoutOverlapping; +use OpenSpout\Common\Entity\Style\Style; abstract class Exporter { @@ -168,9 +169,9 @@ abstract class Exporter } /** - * @return CellStyle|null + * @return Style|null */ - public function getHeadingStyle(): ?CellStyle + public function getHeadingStyle(): ?Style { return null; } diff --git a/packages/actions/src/Exports/Jobs/CreateXlsxFile.php b/packages/actions/src/Exports/Jobs/CreateXlsxFile.php index d7aa15c790..77c3afa990 100644 --- a/packages/actions/src/Exports/Jobs/CreateXlsxFile.php +++ b/packages/actions/src/Exports/Jobs/CreateXlsxFile.php @@ -2,11 +2,8 @@ namespace Filament\Actions\Exports\Jobs; -use Filament\Actions\Exports\CellStyle; use Filament\Actions\Exports\Exporter; use Filament\Actions\Exports\Models\Export; -use Filament\Support\Enums\Alignment; -use Filament\Support\Enums\VerticalAlignment; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Contracts\Queue\ShouldQueue; @@ -17,9 +14,6 @@ use Illuminate\Queue\SerializesModels; use League\Csv\Reader as CsvReader; use League\Csv\Statement; use OpenSpout\Common\Entity\Row; -use OpenSpout\Common\Entity\Style\CellAlignment; -use OpenSpout\Common\Entity\Style\CellVerticalAlignment; -use OpenSpout\Common\Entity\Style\Color; use OpenSpout\Common\Entity\Style\Style; use OpenSpout\Writer\XLSX\Writer; @@ -56,21 +50,17 @@ class CreateXlsxFile implements ShouldQueue $writer = app(Writer::class); $writer->openToFile($temporaryFile = tempnam(sys_get_temp_dir(), $this->export->file_name)); - $headersStyle = $this->exporter->getHeadingStyle() - ? $this->setStyle($this->exporter->getHeadingStyle()) - : null; + $headersStyle = $this->exporter->getHeadingStyle(); $csvDelimiter = $this->exporter::getCsvDelimiter(); - $writeRowsFromFile = function (string $file, Style $headersStyle = null) use ($csvDelimiter, $disk, $writer) { + $writeRowsFromFile = function (string $file, Style $style = null) use ($csvDelimiter, $disk, $writer) { $csvReader = CsvReader::createFromStream($disk->readStream($file)); $csvReader->setDelimiter($csvDelimiter); $csvResults = Statement::create()->process($csvReader); foreach ($csvResults->getRecords() as $row) { - $headersStyle - ? $writer->addRow(Row::fromValues($row, $headersStyle)) - : $writer->addRow(Row::fromValues($row)); + $writer->addRow(Row::fromValues($row, $style)); } }; @@ -99,48 +89,4 @@ class CreateXlsxFile implements ShouldQueue unlink($temporaryFile); } - - /** - * @param CellStyle $cellStyle - * @return Style - */ - protected function setStyle(CellStyle $cellStyle): Style - { - $headingStyle = new Style(); - - $styles = collect(get_object_vars($cellStyle)) - ->reject(fn (mixed $style): bool => $style === null || $style === false); - - foreach ($styles as $style => $value) { - match ($style) { - 'bold' => $headingStyle->setFontBold(), - 'italic' => $headingStyle->setFontItalic(), - 'underline' => $headingStyle->setFontUnderline(), - 'strikethrough' => $headingStyle->setFontStrikethrough(), - 'size' => $headingStyle->setFontSize($value), - 'family' => $headingStyle->setFontName($value), - 'color' => $headingStyle->setFontColor( - Color::rgb($value->red(), $value->green(), $value->blue()) - ), - 'backgroundColor' => $headingStyle->setBackgroundColor( - Color::rgb($value->red(), $value->green(), $value->blue()) - ), - 'verticalAlignment' => match ($styles->get('verticalAlignment')) { - VerticalAlignment::Start => $headingStyle->setCellVerticalAlignment(CellVerticalAlignment::TOP), - VerticalAlignment::Center => $headingStyle->setCellVerticalAlignment(CellVerticalAlignment::CENTER), - VerticalAlignment::End => $headingStyle->setCellVerticalAlignment(CellVerticalAlignment::BOTTOM), - default => null, - }, - 'alignment' => match ($styles->get('alignment')) { - Alignment::Start => $headingStyle->setCellAlignment(CellAlignment::LEFT), - Alignment::Center => $headingStyle->setCellAlignment(CellAlignment::CENTER), - Alignment::End => $headingStyle->setCellAlignment(CellAlignment::RIGHT), - default => null, - }, - default => null, - }; - } - - return $headingStyle; - } }