Skip to content

Commit 499f3ee

Browse files
authored
[5.2] fix(export): type numeric answers as numbers in exports
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
1 parent 01420d3 commit 499f3ee

1 file changed

Lines changed: 17 additions & 10 deletions

File tree

lib/Service/SubmissionService.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\IUserSession;
3636
use OCP\Mail\IMailer;
3737

38+
use PhpOffice\PhpSpreadsheet\Cell\DataType;
3839
use PhpOffice\PhpSpreadsheet\IOFactory;
3940
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4041
use PhpOffice\PhpSpreadsheet\Writer\Csv;
@@ -348,17 +349,23 @@ private function exportData(array $header, array $data, string $fileFormat, ?Fil
348349
$activeWorksheet->getStyle([$column, $row])
349350
->getAlignment()
350351
->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);
351365
} 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);
362369
}
363370
}
364371
}

0 commit comments

Comments
 (0)