Skip to content

Commit

Permalink
Add middleware for trimming/converting to null for cell values
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbrouwers committed Jan 16, 2024
1 parent f6db323 commit 9a3ebc0
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

41 changes: 28 additions & 13 deletions config/excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
| you can enable it by setting read_only to false.
|
*/
'read_only' => true,
'read_only' => true,

/*
|--------------------------------------------------------------------------
Expand All @@ -110,7 +110,7 @@
| Available options: none|slug|custom
|
*/
'heading_row' => [
'heading_row' => [
'formatter' => 'slug',
],

Expand All @@ -122,7 +122,7 @@
| Configure e.g. delimiter, enclosure and line ending for CSV imports.
|
*/
'csv' => [
'csv' => [
'delimiter' => null,
'enclosure' => '"',
'escape_character' => '\\',
Expand All @@ -138,7 +138,7 @@
| Configure e.g. default title, creator, subject,...
|
*/
'properties' => [
'properties' => [
'creator' => '',
'lastModifiedBy' => '',
'title' => '',
Expand All @@ -150,6 +150,21 @@
'company' => '',
],

/*
|--------------------------------------------------------------------------
| Cell Middleware
|--------------------------------------------------------------------------
|
| Configure middleware that is executed on getting a cell value
|
*/
'cells' => [
'middleware' => [
//\Maatwebsite\Excel\Middleware\TrimCellValue::class,
//\Maatwebsite\Excel\Middleware\ConvertEmptyCellValuesToNull::class,
],
]

],

/*
Expand Down Expand Up @@ -207,11 +222,11 @@
| [x] PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder::class
|
*/
'value_binder' => [
'value_binder' => [
'default' => Maatwebsite\Excel\DefaultValueBinder::class,
],

'cache' => [
'cache' => [
/*
|--------------------------------------------------------------------------
| Default cell caching driver
Expand All @@ -228,7 +243,7 @@
| Drivers: memory|illuminate|batch
|
*/
'driver' => 'memory',
'driver' => 'memory',

/*
|--------------------------------------------------------------------------
Expand All @@ -240,7 +255,7 @@
| Here you can tweak the memory limit to your liking.
|
*/
'batch' => [
'batch' => [
'memory_limit' => 60000,
],

Expand All @@ -256,7 +271,7 @@
| at "null" it will use the default store.
|
*/
'illuminate' => [
'illuminate' => [
'store' => null,
],

Expand Down Expand Up @@ -310,7 +325,7 @@
| and the create file (file).
|
*/
'local_path' => storage_path('framework/cache/laravel-excel'),
'local_path' => storage_path('framework/cache/laravel-excel'),

/*
|--------------------------------------------------------------------------
Expand All @@ -322,7 +337,7 @@
| If omitted the default permissions of the filesystem will be used.
|
*/
'local_permissions' => [
'local_permissions' => [
// 'dir' => 0755,
// 'file' => 0644,
],
Expand All @@ -341,8 +356,8 @@
| in conjunction with queued imports and exports.
|
*/
'remote_disk' => null,
'remote_prefix' => null,
'remote_disk' => null,
'remote_prefix' => null,

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Maatwebsite\Excel;

use Illuminate\Support\Facades\Pipeline;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Cell\Cell as SpreadsheetCell;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
Expand Down Expand Up @@ -77,6 +78,6 @@ public function getValue($nullValue = null, $calculateFormulas = false, $formatD
}
}

return $value;
return Pipeline::send($value)->through(config('excel.imports.cells.middleware', []))->thenReturn();
}
}
15 changes: 15 additions & 0 deletions src/Middleware/CellMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Maatwebsite\Excel\Middleware;

use Maatwebsite\Excel\Cell;

abstract class CellMiddleware
{
/**
* @param mixed $value
*
* @return mixed
*/
abstract public function __invoke($value, callable $next);
}
18 changes: 18 additions & 0 deletions src/Middleware/ConvertEmptyCellValuesToNull.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Maatwebsite\Excel\Middleware;

class ConvertEmptyCellValuesToNull extends CellMiddleware
{
/**
* @param mixed $value
*
* @return mixed
*/
public function __invoke($value, callable $next)
{
return $next(
$value === '' ? null : $value
);
}
}
24 changes: 24 additions & 0 deletions src/Middleware/TrimCellValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Maatwebsite\Excel\Middleware;

class TrimCellValue extends CellMiddleware
{

/**
* @param mixed $value
*
* @return mixed
*/
public function __invoke($value, callable $next)
{
if (!is_string($value)) {
return $next($value);
}

// Remove whitespace, BOM and zero width spaces.
$cleaned = preg_replace('~^[\s\x{FEFF}\x{200B}]+|[\s\x{FEFF}\x{200B}]+$~u', '', $value) ?? trim($value);

return $next($cleaned);
}
}
52 changes: 52 additions & 0 deletions tests/CellTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Maatwebsite\Excel\Tests;

use Maatwebsite\Excel\Cell;
use Maatwebsite\Excel\Middleware\ConvertEmptyCellValuesToNull;
use Maatwebsite\Excel\Middleware\TrimCellValue;

class CellTest extends TestCase
{
/**
* @test
*/
public function can_get_cell_value()
{
$worksheet = $this->read(__DIR__ . '/Data/Disks/Local/import-middleware.xlsx', 'Xlsx');

$this->assertEquals('test', Cell::make($worksheet->getActiveSheet(), 'A1')->getValue());

// By default spaces are not removed
$this->assertEquals(' ', Cell::make($worksheet->getActiveSheet(), 'A2')->getValue());
}

/**
* @test
*/
public function can_trim_empty_cells()
{
config()->set('excel.imports.cells.middleware', [
TrimCellValue::class,
]);

$worksheet = $this->read(__DIR__ . '/Data/Disks/Local/import-middleware.xlsx', 'Xlsx');

$this->assertEquals('', Cell::make($worksheet->getActiveSheet(), 'A2')->getValue());
}

/**
* @test
*/
public function convert_empty_cells_to_null()
{
config()->set('excel.imports.cells.middleware', [
TrimCellValue::class,
ConvertEmptyCellValuesToNull::class,
]);

$worksheet = $this->read(__DIR__ . '/Data/Disks/Local/import-middleware.xlsx', 'Xlsx');

$this->assertEquals(null, Cell::make($worksheet->getActiveSheet(), 'A2')->getValue());
}
}
3 changes: 2 additions & 1 deletion tests/Data/Disks/Local/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
!csv-with-comma.csv
!import-users-with-grouped-headers.xlsx
!import-users-with-mixed-headings.xlsx
!import-batches-with-date.xlsx
!import-batches-with-date.xlsx
!import-middleware.xlsx
Binary file added tests/Data/Disks/Local/import-middleware.xlsx
Binary file not shown.

0 comments on commit 9a3ebc0

Please sign in to comment.