support multi-line values in excel invoices (#1507)
This commit is contained in:
@@ -11,6 +11,7 @@ Perform EACH version specific task between your version and the new one, otherwi
|
||||
## [1.8](https://github.com/kevinpapst/kimai2/releases/tag/1.8)
|
||||
|
||||
- New mailer library: check if emails are still working (eg. by using the "password forgotten" function) or if you need to adjust your configuration, [see docs at symfony.com](https://symfony.com/doc/current/components/mailer.html#transport)
|
||||
- Support for line breaks in multiline invoice fields for spreadsheets (check your invoice templates after the update)
|
||||
|
||||
Permission changes:
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace App\Invoice\Renderer;
|
||||
|
||||
use App\Entity\InvoiceDocument;
|
||||
use App\Invoice\InvoiceModel;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
@@ -59,6 +60,8 @@ abstract class AbstractSpreadsheetRenderer extends AbstractRenderer
|
||||
|
||||
$entryRow = 0;
|
||||
|
||||
Cell::setValueBinder(new AdvancedValueBinder());
|
||||
|
||||
foreach ($worksheet->getRowIterator() as $row) {
|
||||
$sheetValues = false;
|
||||
foreach ($row->getCellIterator() as $cell) {
|
||||
|
||||
58
src/Invoice/Renderer/AdvancedValueBinder.php
Normal file
58
src/Invoice/Renderer/AdvancedValueBinder.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Invoice\Renderer;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
|
||||
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
|
||||
class AdvancedValueBinder extends DefaultValueBinder implements IValueBinder
|
||||
{
|
||||
/**
|
||||
* Bind value to a cell.
|
||||
*
|
||||
* @param Cell $cell Cell to bind value to
|
||||
* @param mixed $value Value to bind in cell
|
||||
*
|
||||
* @throws \PhpOffice\PhpSpreadsheet\Exception
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function bindValue(Cell $cell, $value = null)
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$value = StringHelper::sanitizeUTF8($value);
|
||||
}
|
||||
|
||||
$dataType = parent::dataTypeForValue($value);
|
||||
|
||||
if ($dataType === DataType::TYPE_STRING && !$value instanceof RichText) {
|
||||
// Check for newline character "\n"
|
||||
if (strpos($value, "\n") !== false) {
|
||||
$cell->setValueExplicit($value, DataType::TYPE_STRING);
|
||||
$cell->getWorksheet()->getStyle($cell->getCoordinate())->getAlignment()->setWrapText(true);
|
||||
|
||||
$amount = substr_count($value, "\n");
|
||||
$dimension = $cell->getWorksheet()->getRowDimension($cell->getRow());
|
||||
if ($dimension->getRowHeight() !== -1) {
|
||||
$defaultHeight = $cell->getWorksheet()->getDefaultRowDimension()->getRowHeight();
|
||||
$dimension->setRowHeight($defaultHeight * ($amount + 1));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::bindValue($cell, $value);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
* @covers \App\Invoice\Renderer\CsvRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractSpreadsheetRenderer
|
||||
* @covers \App\Invoice\Renderer\AdvancedValueBinder
|
||||
* @group integration
|
||||
*/
|
||||
class CsvRendererTest extends TestCase
|
||||
|
||||
@@ -108,6 +108,7 @@ trait RendererTestTrait
|
||||
|
||||
$customer = new Customer();
|
||||
$customer->setName('customer,with/special#name');
|
||||
$customer->setAddress('Foo' . PHP_EOL . 'Street' . PHP_EOL . '1111 City');
|
||||
$customer->setCurrency('EUR');
|
||||
$customer->setMetaField((new CustomerMeta())->setName('foo-customer')->setValue('bar-customer')->setIsVisible(true));
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
* @covers \App\Invoice\Renderer\XlsxRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractRenderer
|
||||
* @covers \App\Invoice\Renderer\AbstractSpreadsheetRenderer
|
||||
* @covers \App\Invoice\Renderer\AdvancedValueBinder
|
||||
* @group integration
|
||||
*/
|
||||
class XlsxRendererTest extends TestCase
|
||||
|
||||
Reference in New Issue
Block a user