xlsx improvements (#1518)

This commit is contained in:
Adrian Rudnik
2020-03-20 18:29:26 +01:00
committed by GitHub
parent b8c5323ece
commit fb2ce0d30a
3 changed files with 67 additions and 4 deletions

View File

@@ -74,7 +74,10 @@ abstract class AbstractSpreadsheetRenderer
'customer' => [],
'project' => [],
'activity' => [],
'description' => [],
'description' => [
'maxWidth' => 50,
'wrapText' => false,
],
'exported' => [],
'tags' => [],
'hourlyRate' => [],
@@ -264,8 +267,31 @@ abstract class AbstractSpreadsheetRenderer
}
if (isset($columns['description']) && !isset($columns['description']['render'])) {
$columns['description']['render'] = function (Worksheet $sheet, int $row, int $column, ExportItemInterface $entity) {
$sheet->setCellValueByColumnAndRow($column, $row, $entity->getDescription());
$maxWidth = array_key_exists('maxWidth', $columns['description']) ? intval($columns['description']['maxWidth']) : null;
$wrapText = array_key_exists('wrapText', $columns['description']) ? (bool) $columns['description']['wrapText'] : false;
// This column has a column-only formatter to set the maximum width of a column.
// It needs to be executed once, so we use this as a flag on when to skip it.
$isColumnFormatted = false;
$columns['description']['render'] = function (Worksheet $sheet, int $row, int $column, ExportItemInterface $entity) use (&$isColumnFormatted, $maxWidth, $wrapText) {
$cell = $sheet->getCellByColumnAndRow($column, $row);
$cell->setValue($entity->getDescription());
// Apply wrap text if configured
if ($wrapText) {
$cell->getStyle()->getAlignment()->setWrapText(true);
}
// Apply max width, only needs to be once per column
if (!$isColumnFormatted) {
if (null !== $maxWidth) {
$sheet->getColumnDimensionByColumn($column)
->setWidth($maxWidth);
}
$isColumnFormatted = true;
}
};
}
@@ -464,6 +490,12 @@ abstract class AbstractSpreadsheetRenderer
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// Set default row height to automatic, so we can specify wrap text columns later on
// without bloating the output file as we would need to store stylesheet info for every cell.
// LibreOffice is still not considering this flag, @see https://github.com/PHPOffice/PHPExcel/issues/588
// with no solution implemented so nothing we can do about it there.
$sheet->getDefaultRowDimension()->setRowHeight(-1);
$recordsHeaderColumn = 1;
$recordsHeaderRow = 1;