Skip to content

Commit

Permalink
make adapter configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Feb 7, 2024
1 parent 74ef228 commit d224b70
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ These changes apply automatically to SecurityAdmin and ModelAdmin through extens
To make import easier, import specs are replaced by a sample file that is ready to use for the user.
This import file can be further customised by implementing `sampleImportData` that should return an array of rows.

## Choosing your adapter

You can choose your preferred adapter in yml. Accepted values are:
- csv: PhpSpreadsheet,OpenSpout,League,Native
- xlsx: PhpSpreadsheet,OpenSpout,Simple,Native

```yml
LeKoala\ExcelImportExport\ExcelImportExport:
preferred_csv_adapter: 'Native'
preferred_xlsx_adapter: 'Native'
```

## Configure exported fields

All fields are exported by default (not just summary fields that are useless by themselves)
Expand Down
7 changes: 7 additions & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use LeKoala\SpreadCompat\SpreadCompat;
use LeKoala\ExcelImportExport\ExcelImportExport;

SpreadCompat::$preferredCsvAdapter = ExcelImportExport::config()->preferred_csv_adapter ?? 'Native';
SpreadCompat::$preferredXslxAdapter = ExcelImportExport::config()->preferred_xlsx_adapter ?? 'Native';
3 changes: 3 additions & 0 deletions _config/excel-import-export.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ After:
- "#corecache"
---
LeKoala\ExcelImportExport\ExcelImportExport:
preferred_csv_adapter: 'Native'
preferred_xlsx_adapter: 'PhpSpreadsheet'
default_creator: 'SilverStripe'
default_extension: "xlsx" # could be csv
allowed_extensions:
- csv
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"silverstripe/recipe-plugin": "^2",
"silverstripe/vendor-plugin": "^2",
"silverstripe/framework": "^5",
"lekoala/spread-compat": "^0.5.1"
"lekoala/spread-compat": "^0.6"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
3 changes: 1 addition & 2 deletions src/ExcelGridFieldExportButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,10 @@ public function handleExport($gridField, $request = null)

if ($ext != 'csv') {
$end = ExcelImportExport::getLetter(count($this->getRealExportColumns($gridField)));
$opts['creator'] = "SilverStripe";
$opts['creator'] = ExcelImportExport::config()->default_creator;
$opts['autofilter'] = "A1:{$end}1";
}

SpreadCompat::$preferredCsvAdapter = SpreadCompat::NATIVE;
SpreadCompat::output($data, $fileName, ...$opts);
exit();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ExcelImportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static function sampleFileForClass($class)
}

$options = new Options();
$options->creator = "SilverStripe";
$options->creator = ExcelImportExport::config()->default_creator;
SpreadCompat::output($data, $fileName, $options);
exit();
}
Expand Down

0 comments on commit d224b70

Please sign in to comment.