SpartnerNL / Laravel-Excel

🚀 Supercharged Excel exports and imports in Laravel
https://laravel-excel.com
MIT License
12.27k stars 1.92k forks source link

[QUESTION] Hidden characters in exported file #2419

Closed ChristianBundgaard closed 4 years ago

ChristianBundgaard commented 5 years ago

Prerequisites

Versions

Description

I'm using this package to export a TSV feed for Google Shopping. It has a long list of fields, ex 'custom_label_0' and 'identifierexists'. Everything works fine, except for those fields with a header that includes an ''.

Google Support says, it is because the feed includes some hidden characters, and that it can be identified with this tool: https://meyerweb.com/eric/tools/dencoder/

Correctly, if I copy "custom_label_0" from my file and encode it, I get:

"custom_label_0" ==>
%22custom_%E2%80%8B%E2%80%8Blabel_%E2%80%8B%E2%80%8B0%22

And if I just write it, it becomes...

"custom_label_0" ==>
%22custom_label_0%22

Any suggestions? I'm just using the basic functions from this package, and I can't see what can be done differently.

patrickbrouwers commented 4 years ago

Can you show your code? I don't think this package does any data transformation before inserting, perhaps PhpSpreadsheet does.

ChristianBundgaard commented 4 years ago

@patrickbrouwers

I suppose, it can be PhpSpreadsheet, but in that case... is it possible to do anything to avoid it?

namespace App\Exports;

use App\Product;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\Exportable;

ini_set('memory_limit', '2048M');

class ProductExport implements FromQuery, WithMapping, WithHeadings
{
    use Exportable;

    public function query()
    {
        // Query
    }

    public function map($product): array
    {
        return [
            $product->id,
            $product->name,
            $product->shortDescriptionWithVariables(),
            url($product->slug ? $product->slug->name : ''),
            ! empty($product->images) ? $product->images[0] : $product->thumbnail,
            $product->availability && $product->buyable ? 'in stock' : 'out of stock',
            $product->formatted_price,
            $product->category ? $product->category->name : null,
            $product->brand_name,
            $product->ean,
            ! empty($product->ean) ? 'yes' : 'no',
            'new',
            $product->primaryColor ? $product->primaryColor->name : null,
            $product->feed_name,
        ];
    }

    public function headings(): array
    {
        return [
            'id',
            'title',
            'description',
            'link',
            'image_link',
            'availability',
            'price',
            'product_type',
            'brand',
            'gtin',
            'identifier_​exists',
            'condition',
            'color',
            'custom_label_0',
        ];
    }
}
patrickbrouwers commented 4 years ago

I was most interested in the map logic, but I assume you don't do any "weird" things there?

I'm not sure how to avoid, I don't have any experience with TSV. I'm afraid you'll have to step through the PhpSpreadsheet code and see what part adds those hidden chars. Perhaps something to do with encoding.

ChristianBundgaard commented 4 years ago

@patrickbrouwers, I've added the mapping now - but no, I don't think so.

Hmm okay, thanks for your answer.

patrickbrouwers commented 4 years ago

If there's anything we can fix in the package for it, feel free to PR it

patrickbrouwers commented 4 years ago

Bit late perhaps, but maybe using a custom heading key formatter might be a step into the right direction:

https://docs.laravel-excel.com/3.1/imports/heading-row.html#custom-formatter

I'll close this tickets as it's quite old, feel free to reopen if needed.