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