From 35263fb1de93f4c14bd35a1eee52e635cf7844fb Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Tue, 5 May 2026 11:56:39 +0200 Subject: [PATCH] Cron: Fix leading zeros stripped from Matricule/official_code on XLSX import - refs BT#23385 --- main/cron/import_users_from_xlsx.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/main/cron/import_users_from_xlsx.php b/main/cron/import_users_from_xlsx.php index f7e87ed2149..e30bc929bb5 100644 --- a/main/cron/import_users_from_xlsx.php +++ b/main/cron/import_users_from_xlsx.php @@ -89,7 +89,25 @@ $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType); $phpExcel = $reader->load($xlsxFile); $worksheet = $phpExcel->getActiveSheet(); - $xlsxRows = $worksheet->toArray(); + // toArray() passes all values through NumberFormat::toFormattedString(), which + // reinterprets string-typed cells (e.g. "000123") as numbers when the format code + // is General, silently stripping leading zeros. Read cells directly instead: string- + // typed cells return their raw value unchanged; numeric cells still go through + // formatting and honour any zero-padding format code (e.g. "0000000000"). + $xlsxRows = []; + foreach ($worksheet->getRowIterator() as $row) { + $rowData = []; + $cellIterator = $row->getCellIterator(); + $cellIterator->setIterateOnlyExistingCells(false); + foreach ($cellIterator as $cell) { + if ($cell->getDataType() === \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING) { + $rowData[] = (string) $cell->getValue(); + } else { + $rowData[] = $cell->getFormattedValue(); + } + } + $xlsxRows[] = $rowData; + } } catch (Exception $e) { exit("Error loading XLSX file: {$e->getMessage()}\n"); }