diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ebcb3b..ec6ba5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p ### Added - Craft 4 support +- Support for formatting cell values ## 2.1.5 - 2020-01-27 ### Added diff --git a/README.md b/README.md index 776ec3e..3ddebd0 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,12 @@ If you want to append content dynamically, say from a loop, you can use the `app {% endfor %} ``` -To generate an CSV: +### To generate an CSV: ```twig {% do beam.csv() %} ``` -To generate an XLSX: +### To generate an XLSX: ```twig {% do beam.xlsx() %} ``` @@ -85,6 +85,35 @@ To overwrite the content: [ 'another+test@example.com', 'Jane Doe' ], [ 'third+test@example.com', 'Trond Johansen' ], ]) %} -``` +``` + +### Custom cell formatting is supported for XLSX: + +```twig +{% set options = { + header: ['Email', 'Name', { text: 'Number', type: 'number' }, { text: 'Date', type: 'date' }], + content: [ + [ 'test@example.com', 'John Doe', 100000, '2022-06-10'], + [ 'another+test@example.com', 'Jane Doe', 252323, '2022-06-22'], + [ 'third+test@example.com', 'Trond Johansen', 30, '2022-06-22'], + [ 'third+test@example.com', 'Trond Johansen', 6233, '2023-06-22'], + ] +} %} +{% set beam = craft.beam.create(options) %} +{% do beam.xlsx() %} +``` + +These types are supported: + +| Format Type | Maps to the following cell format | +|-------------|-------------------------------------------| +| string | @ | +| integer | 0 | +| date | YYYY-MM-DD | +| datetime | YYYY-MM-DD HH:MM:SS | +| time | HH:MM:SS | +| price | #,##0.00 | +| dollar | [$$-1009]#,##0.00;[RED]-[$$-1009]#,##0.00 | +| euro | #,##0.00 [$€-407];[RED]-#,##0.00 [$€-407] | Brought to you by [Superbig](https://superbig.co) diff --git a/composer.json b/composer.json index a045715..29a2709 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "require": { "craftcms/cms": "^4.0.0", "league/csv": "^8.1|^9.0", - "mk-j/php_xlsxwriter": "^0.32.0" + "mk-j/php_xlsxwriter": "^0.38.0" }, "repositories": [ { diff --git a/src/services/BeamService.php b/src/services/BeamService.php index 746a060..c1a978e 100644 --- a/src/services/BeamService.php +++ b/src/services/BeamService.php @@ -60,6 +60,7 @@ public function csv(BeamModel $model) $csv->setOutputBOM(Writer::BOM_UTF8); if (!empty($header)) { + $headerValues = array_map(fn($value) => is_array($value) ? $value['text'] ?? 'No text set' : $value, $header); $csv->insertOne($header); } @@ -101,7 +102,13 @@ public function xlsx(BeamModel $model) if (!empty($header)) { $headers = []; foreach ($header as $header) { - $headers[ $header ] = 'string'; + if (is_array($header)) { + $text = $header['text'] ?? 'No text set'; + $type = $this->normalizeCellFormat($header['type'] ?? 'string'); + $headers[ $text ] = $type; + } else { + $headers[ $header ] = 'string'; + } } // Insert the headers $writer->writeSheetHeader($sheetName, $headers); @@ -174,4 +181,19 @@ public function unhashConfig(string $hash): array return $config; } + + private function normalizeCellFormat(string $type) { + $types = [ + 'number' => 'integer', + 'date' => 'date', + 'datetime' => 'datetime', + 'time' => 'time', + 'dollar' => 'dollar', + 'euro' => 'euro', + 'price' => 'price', + 'string' => 'string', + ]; + + return $types[$type] ?? 'string'; + } }