Custom Formatting Values / Value Binders: #4173
Raphsuntei
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
No jokes, it took me ~8 hours to figure out, why my "simple" csv import suddenly converted numbers strangely.
That will usually only happen, if you live in a country with regular number formats, and not the US, which might also be, why there is no documentation about this. In germany and most parts of the world, we use the . as thousands seperators, and the , for decimals. My banktransactions csv, officially provided by my bank, obviously has them included.
The problem:
1.500 becomes 1.5
because under the hood, values are converted automatically.
In my case, i was trying to find the problem with myself, but values wereconverted, before a programmer has a chance to manipulate them.
There actually is a page about this on the Docs, but it is NOT linked in the menu bar of the docs as of right now!
https://docs.laravel-excel.com/3.1/imports/custom-formatting-values.html
Even the title "Custom Formating Values" is misleading, because values are converted, not just displayed in a different way.
This page helps to solve the issue, especially by Disabling intelligent formatting.
Disable intelligent formatting
If you want to disable the intelligent formatting of values, you can extend your import class with \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder. In this case all values are passed on as strings.
namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
class UsersImport extends \PhpOffice\PhpSpreadsheet\Cell\StringValueBinder implements WithCustomValueBinder, ToModel
{
}
Please put this page into the menu bar! It will save a lot of people a headache.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions