From d8c69cc5b565ef09882eb3c13ee29dc73559e1c8 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Mon, 2 Jun 2025 13:40:13 +0100 Subject: [PATCH] Update 12-export.md --- packages/actions/docs/12-export.md | 33 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/actions/docs/12-export.md b/packages/actions/docs/12-export.md index 9fc94aa0be..2031304f15 100644 --- a/packages/actions/docs/12-export.md +++ b/packages/actions/docs/12-export.md @@ -656,10 +656,10 @@ public function makeXlsxRow(array $values, ?Style $style = null): Row } ``` -You can replace `Row::fromValues()` with `Row::fromValuesWithStyles()`, which allows you to pass an array of styles for each cell in the row: +When a user exports, they can choose which columns to export. As such, the `$this->columnMap` property may be used to determine which columns are being exported and in which order. You can replace `Row::fromValues()` with an array of `Cell` objects, which allow you to style them individually using [OpenSpout `Style` objects](https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#styling). A `StyleMerger` can be used to merge the default style with the custom style for a cell, allowing you to apply additional styles on top of the default ones: ```php -use OpenSpout\Common\Entity\Row; +use OpenSpout\Common\Entity\Cell;use OpenSpout\Common\Entity\Row; use OpenSpout\Common\Entity\Style\Style; use OpenSpout\Writer\Common\Manager\Style\StyleMerger; @@ -670,18 +670,29 @@ public function makeXlsxRow(array $values, ?Style $style = null): Row { $styleMerger = new StyleMerger(); - return Row::fromValuesWithStyles($values, $style, [ - 2 => (new Style())->setFontUnderline(), - 3 => $styleMerger->merge( - (new Style())->setFontSize(16), - $style, - ), - ]); + $cells = []; + + foreach (array_keys($this->columnMap) as $columnIndex => $column) { + $cells[] = match ($column) { + 'name' => Cell::fromValue( + $values[$columnIndex], + $styleMerger->merge( + (new Style())->setFontUnderline(), + $style, + ), + ), + 'price' => Cell::fromValue( + $values[$columnIndex], + (new Style())->setFontSize(12), + ), + default => Cell::fromValue($values[$columnIndex]), + }, + } + + return new Row($cells, $style); } ``` -An [OpenSpout `Style` object](https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#styling) is used to define the style for each cell. The column index should index the array of styles. A `StyleMerger` can be used to merge the default style with the custom style for a cell, allowing you to apply additional styles on top of the default ones. - ### Customizing the XLSX writer If you want to pass "options" to the [OpenSpout XLSX `Writer`](https://github.com/openspout/openspout/blob/4.x/docs/documentation.md#column-widths), you can return an `OpenSpout\Writer\XLSX\Options` instance from the `getXlsxWriterOptions()` method of the exporter class: