Skip to content

Commit

Permalink
feat: add custom cell formatting
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
sjelfull committed Oct 3, 2022
1 parent b48d828 commit 0057207
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() %}
```
Expand All @@ -85,6 +85,35 @@ To overwrite the content:
[ '[email protected]', 'Jane Doe' ],
[ '[email protected]', 'Trond Johansen' ],
]) %}
```
```

### Custom cell formatting is supported for XLSX:

```twig
{% set options = {
header: ['Email', 'Name', { text: 'Number', type: 'number' }, { text: 'Date', type: 'date' }],
content: [
[ '[email protected]', 'John Doe', 100000, '2022-06-10'],
[ '[email protected]', 'Jane Doe', 252323, '2022-06-22'],
[ '[email protected]', 'Trond Johansen', 30, '2022-06-22'],
[ '[email protected]', '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)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
24 changes: 23 additions & 1 deletion src/services/BeamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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';
}
}

0 comments on commit 0057207

Please sign in to comment.