Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.00 should not be left empty in the rows. #19

Closed
rickmacgillis opened this issue Sep 16, 2018 · 2 comments
Closed

0.00 should not be left empty in the rows. #19

rickmacgillis opened this issue Sep 16, 2018 · 2 comments

Comments

@rickmacgillis
Copy link

When exporting a column of data set as decimal(8.2) in MySQL, the column will only add data to the CSV file if the entry is greater than 0.00.

I also noticed that boolean false gets omitted as if it were null, as well. It sounds a lot like something somewhere is using empty() to check if something should be set to an empty string in the CSV.

I don't have time right now to look into it further, but hopefully the package maintainer or someone else will look into the issue. (FYI, to keep things simple, a 0 in lieu of a boolean false would be great in the CSV.)

@patrickbrouwers
Copy link
Member

This most likely bottles down to the parent package PhpSpreadsheet and their DefaultValueBinder. You can either create custom action with a custom WithMapping and WithColumnFormatting or create your own value binder \PhpOffice\PhpSpreadsheet\Cell::setValueBinder(new YourValueBinder)
The issue for sure isn't located in this package, as we only pass on the data from eloquent to phpspreadsheet.

@rickmacgillis
Copy link
Author

To anyone else facing this issue, or the one described in issue #18, I've found a way to resolve both problems. Below is a base class I created to allow all other exporters to extend. The classes that extend it just define the columns in protected $columns in the order you'd like them exported. The base class takes care of the rest.

<?php

namespace App\Nova\Actions\Excel;

use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\LaravelNovaExcel\Actions\DownloadExcel as DownloadExcelBase;

class DownloadExcel extends DownloadExcelBase implements WithMapping
{
	protected $columns = [];
	
	public function __construct()
	{
		$this->withWriterType(Excel::CSV);
		$this->withHeadings($this->columns);
	}
	
	public function map($row): array
	{
		$out = [];
		foreach ($this->columns as $column) {
			
			$value = $row->{$column};
			$this->parseValue($value);
			$out[] = $value;
			
		}
		
		return $out;
	}
	
	protected function parseValue(&$value)
	{
		if (is_bool($value)) {
			$value = $value === false ? "0" : "1";
		}
	}
}

@SpartnerNL SpartnerNL locked as resolved and limited conversation to collaborators Sep 17, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants