94 lines
4.2 KiB
PHP
94 lines
4.2 KiB
PHP
<?php
|
|
// Sopprime eventuali output di errori (li logghiamo invece di mostrarli)
|
|
ob_start();
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Inizia la sessione
|
|
session_start();
|
|
|
|
// Includi PHPSpreadsheet
|
|
require_once '../../vendor/autoload.php';
|
|
|
|
$response = ['error' => '', 'rows' => [], 'columns' => [], 'template_id' => 0];
|
|
|
|
try {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['excel_file'])) {
|
|
$template_id = isset($_POST['template_id']) ? intval($_POST['template_id']) : 0;
|
|
$header_row = isset($_POST['header_row']) ? intval($_POST['header_row']) : 1;
|
|
$start_column = isset($_POST['start_column']) ? intval($_POST['start_column']) : 1;
|
|
|
|
$file = $_FILES['excel_file'];
|
|
$fileError = $file['error'];
|
|
|
|
if ($fileError === UPLOAD_ERR_OK) {
|
|
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file['tmp_name']);
|
|
$worksheet = $spreadsheet->getActiveSheet();
|
|
$highestRow = $worksheet->getHighestRow();
|
|
$highestColumn = $worksheet->getHighestColumn();
|
|
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
|
|
|
|
$startRow = max(1, $header_row);
|
|
$startColumn = max(1, $start_column);
|
|
|
|
// Debug dei parametri
|
|
error_log("Processing - startRow: $startRow, startColumn: $startColumn, highestRow: $highestRow, highestColumn: $highestColumn, highestColumnIndex: $highestColumnIndex");
|
|
|
|
// Validazione degli indici
|
|
if ($startRow > $highestRow) {
|
|
$response['error'] = "La riga di partenza ($startRow) supera il numero totale di righe ($highestRow).";
|
|
} elseif ($startColumn > $highestColumnIndex) {
|
|
$response['error'] = "La colonna di partenza ($startColumn) supera il numero totale di colonne ($highestColumnIndex).";
|
|
} else {
|
|
$excelData = [];
|
|
// Estrai la riga degli header
|
|
$headerRowData = [];
|
|
for ($col = $startColumn; $col <= $highestColumnIndex; $col++) {
|
|
$columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col);
|
|
$cell = $worksheet->getCell($columnLetter . $header_row);
|
|
$cellValue = $cell ? $cell->getCalculatedValue() : ''; // Usa getCalculatedValue per le formule
|
|
$headerRowData[] = htmlspecialchars($cellValue ?: '');
|
|
}
|
|
|
|
// Estrai i dati a partire dalla riga successiva
|
|
for ($row = $startRow + 1; $row <= $highestRow; $row++) {
|
|
$rowData = [];
|
|
for ($col = $startColumn; $col <= $highestColumnIndex; $col++) {
|
|
$columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col);
|
|
$cell = $worksheet->getCell($columnLetter . $row);
|
|
$cellValue = $cell ? $cell->getCalculatedValue() : ''; // Usa getCalculatedValue per le formule
|
|
$rowData[] = htmlspecialchars($cellValue ?: '');
|
|
}
|
|
if (!empty(array_filter($rowData))) {
|
|
$excelData[] = $rowData;
|
|
}
|
|
}
|
|
|
|
// Salva i dati in sessione
|
|
$_SESSION['excel_data'] = $excelData;
|
|
$_SESSION['template_id'] = $template_id;
|
|
$_SESSION['headers'] = $headerRowData; // Salva gli header in sessione
|
|
|
|
$response['rows'] = $excelData;
|
|
$response['columns'] = $headerRowData; // Usa gli header reali
|
|
$response['template_id'] = $template_id;
|
|
}
|
|
} else {
|
|
$response['error'] = "Errore nell'upload del file: Codice errore $fileError.";
|
|
}
|
|
} else {
|
|
$response['error'] = "Richiesta non valida.";
|
|
}
|
|
} catch (Exception $e) {
|
|
$response['error'] = "Errore durante il caricamento del file: " . $e->getMessage();
|
|
error_log("Exception in process_import_xls.php: " . $e->getMessage());
|
|
}
|
|
|
|
// Pulisce qualsiasi output indesiderato
|
|
ob_end_clean();
|
|
|
|
// Invia la risposta JSON
|
|
header('Content-Type: application/json');
|
|
echo json_encode($response);
|
|
exit;
|