|
35 | 35 | use OCP\IUserSession; |
36 | 36 | use OCP\Mail\IMailer; |
37 | 37 |
|
| 38 | +use PhpOffice\PhpSpreadsheet\Cell\DataType; |
38 | 39 | use PhpOffice\PhpSpreadsheet\IOFactory; |
39 | 40 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
40 | 41 | use PhpOffice\PhpSpreadsheet\Writer\Csv; |
@@ -348,17 +349,23 @@ private function exportData(array $header, array $data, string $fileFormat, ?Fil |
348 | 349 | $activeWorksheet->getStyle([$column, $row]) |
349 | 350 | ->getAlignment() |
350 | 351 | ->setWrapText(true); |
| 352 | + } elseif (!is_string($value)) { |
| 353 | + $activeWorksheet->setCellValue([$column, $row], $value); |
| 354 | + } elseif ($fileFormat === 'csv') { |
| 355 | + // CSV is escaped (not typed) to neutralise formula-triggering values when opened in a spreadsheet app. |
| 356 | + $activeWorksheet->getCell([$column, $row]) |
| 357 | + ->setValueExplicit($this->escapeCSV($value), DataType::TYPE_STRING); |
| 358 | + } elseif (is_numeric($value) |
| 359 | + && !in_array($value[0], ['=', '+', '-', '@'], true) |
| 360 | + && (string)(+$value) === $value) { |
| 361 | + // Store numeric-looking answers (e.g. Radio labels "1"-"5") as numbers so they can be |
| 362 | + // aggregated. The round-trip check keeps values whose canonical numeric form would differ |
| 363 | + // (leading-zero phone numbers, "1e5", oversized ids, ...) as text. |
| 364 | + $activeWorksheet->setCellValue([$column, $row], +$value); |
351 | 365 | } else { |
352 | | - // Explicitly set the type of the value to string for values that start with '=' to prevent it being interpreted as formulas |
353 | | - if (is_string($value)) { |
354 | | - $activeWorksheet->getCell([$column, $row]) |
355 | | - ->setValueExplicit($fileFormat === 'csv' |
356 | | - ? $this->escapeCSV($value) |
357 | | - : $value, |
358 | | - ); |
359 | | - } else { |
360 | | - $activeWorksheet->setCellValue([$column, $row], $value); |
361 | | - } |
| 366 | + // Explicitly type as string so values that start with '=' are not interpreted as formulas. |
| 367 | + $activeWorksheet->getCell([$column, $row]) |
| 368 | + ->setValueExplicit($value, DataType::TYPE_STRING); |
362 | 369 | } |
363 | 370 | } |
364 | 371 | } |
|
0 commit comments