Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ The package will auto-detect numeric fields and can be used with custom formats.
```php
Column::make('total')->exportFormat('0.00'),
Column::make('count')->exportFormat('#,##0'),
Column::make('average')->exportFormat('#,##0.00),
Column::make('average')->exportFormat('#,##0.00'),
```

## Date Fields Formatting
Expand Down Expand Up @@ -152,6 +152,16 @@ Valid date formats can be adjusted on `datatables-export.php` config file.
]
```

## Force Numeric Field As Text Format

Option to force auto-detected numeric value as text format.

```php
Column::make('id')->exportFormat('@'),
Column::make('id')->exportFormat(NumberFormat::FORMAT_GENERAL),
Column::make('id')->exportFormat(NumberFormat::FORMAT_TEXT),
```

## Contributing

Please see [CONTRIBUTING](https://github.com/yajra/laravel-datatables-export/blob/master/.github/CONTRIBUTING.md) for details.
Expand Down
44 changes: 31 additions & 13 deletions src/Jobs/DataTableExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Yajra\DataTables\Jobs;

use OpenSpout\Common\Helper\CellTypeHelper;
use OpenSpout\Common\Type;
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use Carbon\Carbon;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
Expand All @@ -16,11 +12,13 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use OpenSpout\Common\Helper\CellTypeHelper;
use OpenSpout\Common\Type;
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Yajra\DataTables\Exceptions\Exception;
use Yajra\DataTables\Html\Column;
use Yajra\DataTables\Services\DataTable;

Expand Down Expand Up @@ -113,13 +111,20 @@ public function handle()
WriterEntityFactory::createCell($date, (new StyleBuilder)->setFormat($format)->build())
);
} else {
$format = $column['exportFormat']
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
: null;

$value = $this->isNumeric($value) ? (float) $value : $value;

$cells->push(WriterEntityFactory::createCell($value, $format));
if ($this->wantsText($column)) {
$cells->push(
WriterEntityFactory::createCell($value,
(new StyleBuilder)->setFormat($column['exportFormat'])->build())
);
} else {
$format = $column['exportFormat']
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
: null;

$value = $this->isNumeric($value) ? (float) $value : $value;

$cells->push(WriterEntityFactory::createCell($value, $format));
}
}
});

Expand Down Expand Up @@ -154,4 +159,17 @@ protected function isNumeric($value): bool

return is_numeric($value);
}

/**
* @param \Yajra\DataTables\Html\Column $column
* @return bool
*/
protected function wantsText(Column $column): bool
{
if (! isset($column['exportFormat'])) {
return false;
}

return in_array($column['exportFormat'], config('datatables-export.text_formats', ['@']));
}
}
14 changes: 14 additions & 0 deletions src/config/datatables-export.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
],

/*
|--------------------------------------------------------------------------
| Valid Text Formats
|--------------------------------------------------------------------------
|
| List of valid text formats to be used.
|
*/
'text_formats' => [
'@',
NumberFormat::FORMAT_GENERAL,
NumberFormat::FORMAT_TEXT
],

/*
|--------------------------------------------------------------------------
| Purge Options
Expand Down