Fixed: correctly show datetime in spreadsheet list

This commit is contained in:
Julien Fastré 2022-10-25 09:25:30 +02:00
parent c396635163
commit 781253a854

View File

@ -20,11 +20,12 @@ use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use RuntimeException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use function array_key_exists;
use function array_keys;
use function array_map;
@ -80,7 +81,7 @@ class SpreadsheetListFormatter implements FormatterInterface
*
* @uses appendAggregatorForm
*
* @param type $exportAlias
* @param string $exportAlias
*/
public function buildForm(
FormBuilderInterface $builder,
@ -144,8 +145,6 @@ class SpreadsheetListFormatter implements FormatterInterface
$i = 1;
foreach ($result as $row) {
$line = [];
if (true === $this->formatterData['numerotation']) {
$worksheet->setCellValue('A' . ($i + 1), (string) $i);
}
@ -155,13 +154,22 @@ class SpreadsheetListFormatter implements FormatterInterface
foreach ($row as $key => $value) {
$row = $a . ($i + 1);
if ($value instanceof DateTimeInterface) {
$worksheet->setCellValue($row, Date::PHPToExcel($value));
$worksheet->getStyle($row)
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_DDMMYYYY);
$formattedValue = $this->getLabel($key, $value);
if ($formattedValue instanceof DateTimeInterface) {
$worksheet->setCellValue($row, Date::PHPToExcel($formattedValue));
if ($formattedValue->format('His') === '000000') {
$worksheet->getStyle($row)
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_DDMMYYYY);
} else {
$worksheet->getStyle($row)
->getNumberFormat()
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
}
} else {
$worksheet->setCellValue($row, $this->getLabel($key, $value));
$worksheet->setCellValue($row, $formattedValue);
}
++$a;
}
@ -259,6 +267,10 @@ class SpreadsheetListFormatter implements FormatterInterface
foreach ($keys as $key) {
// get an array with all values for this key if possible
$values = array_map(static function ($v) use ($key) {
if (!array_key_exists($key, $v)) {
throw new RuntimeException(sprintf('This key does not exists: %s. Available keys are %s', $key, implode(', ', array_keys($v))));
}
return $v[$key];
}, $this->result);
// store the label in the labelsCache property