Skip to content

Commit f131e97

Browse files
authored
Merge pull request #22 from jeanne007/format_numeric_as_text
Fix numeric format as text.
2 parents 71218b8 + fcf6f1f commit f131e97

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The package will auto-detect numeric fields and can be used with custom formats.
108108
```php
109109
Column::make('total')->exportFormat('0.00'),
110110
Column::make('count')->exportFormat('#,##0'),
111-
Column::make('average')->exportFormat('#,##0.00),
111+
Column::make('average')->exportFormat('#,##0.00'),
112112
```
113113

114114
## Date Fields Formatting
@@ -152,6 +152,16 @@ Valid date formats can be adjusted on `datatables-export.php` config file.
152152
]
153153
```
154154

155+
## Force Numeric Field As Text Format
156+
157+
Option to force auto-detected numeric value as text format.
158+
159+
```php
160+
Column::make('id')->exportFormat('@'),
161+
Column::make('id')->exportFormat(NumberFormat::FORMAT_GENERAL),
162+
Column::make('id')->exportFormat(NumberFormat::FORMAT_TEXT),
163+
```
164+
155165
## Contributing
156166

157167
Please see [CONTRIBUTING](https://github.com/yajra/laravel-datatables-export/blob/master/.github/CONTRIBUTING.md) for details.

src/Jobs/DataTableExportJob.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
namespace Yajra\DataTables\Jobs;
44

5-
use OpenSpout\Common\Helper\CellTypeHelper;
6-
use OpenSpout\Common\Type;
7-
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
8-
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
95
use Carbon\Carbon;
106
use Illuminate\Bus\Batchable;
117
use Illuminate\Bus\Queueable;
@@ -16,11 +12,13 @@
1612
use Illuminate\Queue\SerializesModels;
1713
use Illuminate\Support\Arr;
1814
use Illuminate\Support\Facades\Auth;
19-
use Illuminate\Support\Facades\File;
2015
use Illuminate\Support\Facades\Storage;
2116
use Illuminate\Support\Str;
17+
use OpenSpout\Common\Helper\CellTypeHelper;
18+
use OpenSpout\Common\Type;
19+
use OpenSpout\Writer\Common\Creator\Style\StyleBuilder;
20+
use OpenSpout\Writer\Common\Creator\WriterEntityFactory;
2221
use PhpOffice\PhpSpreadsheet\Shared\Date;
23-
use Yajra\DataTables\Exceptions\Exception;
2422
use Yajra\DataTables\Html\Column;
2523
use Yajra\DataTables\Services\DataTable;
2624

@@ -113,13 +111,20 @@ public function handle()
113111
WriterEntityFactory::createCell($date, (new StyleBuilder)->setFormat($format)->build())
114112
);
115113
} else {
116-
$format = $column['exportFormat']
117-
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
118-
: null;
119-
120-
$value = $this->isNumeric($value) ? (float) $value : $value;
121-
122-
$cells->push(WriterEntityFactory::createCell($value, $format));
114+
if ($this->wantsText($column)) {
115+
$cells->push(
116+
WriterEntityFactory::createCell($value,
117+
(new StyleBuilder)->setFormat($column['exportFormat'])->build())
118+
);
119+
} else {
120+
$format = $column['exportFormat']
121+
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
122+
: null;
123+
124+
$value = $this->isNumeric($value) ? (float) $value : $value;
125+
126+
$cells->push(WriterEntityFactory::createCell($value, $format));
127+
}
123128
}
124129
});
125130

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

155160
return is_numeric($value);
156161
}
162+
163+
/**
164+
* @param \Yajra\DataTables\Html\Column $column
165+
* @return bool
166+
*/
167+
protected function wantsText(Column $column): bool
168+
{
169+
if (! isset($column['exportFormat'])) {
170+
return false;
171+
}
172+
173+
return in_array($column['exportFormat'], config('datatables-export.text_formats', ['@']));
174+
}
157175
}

src/config/datatables-export.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@
8181
NumberFormat::FORMAT_DATE_YYYYMMDDSLASH,
8282
],
8383

84+
/*
85+
|--------------------------------------------------------------------------
86+
| Valid Text Formats
87+
|--------------------------------------------------------------------------
88+
|
89+
| List of valid text formats to be used.
90+
|
91+
*/
92+
'text_formats' => [
93+
'@',
94+
NumberFormat::FORMAT_GENERAL,
95+
NumberFormat::FORMAT_TEXT
96+
],
97+
8498
/*
8599
|--------------------------------------------------------------------------
86100
| Purge Options

0 commit comments

Comments
 (0)