Skip to content

Commit 2865e21

Browse files
committed
Fix numeric format as text.
1 parent 71218b8 commit 2865e21

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/Jobs/DataTableExportJob.php

Lines changed: 29 additions & 14 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

@@ -40,9 +38,9 @@ class DataTableExportJob implements ShouldQueue, ShouldBeUnique
4038
/**
4139
* Create a new job instance.
4240
*
43-
* @param array $dataTable
44-
* @param array $request
45-
* @param null $user
41+
* @param array $dataTable
42+
* @param array $request
43+
* @param null $user
4644
*/
4745
public function __construct(array $dataTable, array $request, $user = null)
4846
{
@@ -78,7 +76,7 @@ public function handle()
7876

7977
$type = Str::startsWith(request('exportType'), Type::CSV) ? Type::CSV : Type::XLSX;
8078
$disk = config('datatables-export.disk', 'local');
81-
$filename = $this->batchId.'.'.$type;
79+
$filename = $this->batchId . '.' . $type;
8280

8381
$path = Storage::disk($disk)->path($filename);
8482

@@ -88,7 +86,7 @@ public function handle()
8886
$columns = $oTable->html()->getColumns()->filter->exportable;
8987
$writer->addRow(
9088
WriterEntityFactory::createRowFromArray(
91-
$columns->map(fn ($column) => strip_tags($column['title']))->toArray()
89+
$columns->map(fn($column) => strip_tags($column['title']))->toArray()
9290
)
9391
);
9492

@@ -112,12 +110,16 @@ public function handle()
112110
$cells->push(
113111
WriterEntityFactory::createCell($date, (new StyleBuilder)->setFormat($format)->build())
114112
);
113+
} else if ($this->wantsText($column)) {
114+
$cells->push(
115+
WriterEntityFactory::createCell($value, (new StyleBuilder)->setFormat($column['exportFormat'])->build())
116+
);
115117
} else {
116118
$format = $column['exportFormat']
117119
? (new StyleBuilder)->setFormat($column['exportFormat'])->build()
118120
: null;
119121

120-
$value = $this->isNumeric($value) ? (float) $value : $value;
122+
$value = $this->isNumeric($value) ? (float)$value : $value;
121123

122124
$cells->push(WriterEntityFactory::createCell($value, $format));
123125
}
@@ -129,7 +131,7 @@ public function handle()
129131
}
130132

131133
/**
132-
* @param \Yajra\DataTables\Html\Column $column
134+
* @param \Yajra\DataTables\Html\Column $column
133135
* @return bool
134136
*/
135137
protected function wantsDateFormat(Column $column): bool
@@ -142,7 +144,7 @@ protected function wantsDateFormat(Column $column): bool
142144
}
143145

144146
/**
145-
* @param mixed $value
147+
* @param mixed $value
146148
* @return bool
147149
*/
148150
protected function isNumeric($value): bool
@@ -154,4 +156,17 @@ protected function isNumeric($value): bool
154156

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

src/config/datatables-export.php

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

84+
/*
85+
* List of valid text formats to be used.
86+
*/
87+
'text_formats' => [
88+
'@',
89+
NumberFormat::FORMAT_GENERAL,
90+
NumberFormat::FORMAT_TEXT
91+
],
92+
8493
/*
8594
|--------------------------------------------------------------------------
8695
| Purge Options

0 commit comments

Comments
 (0)