change template and import o use also sheet number of XLS

This commit is contained in:
2026-05-09 15:39:43 +02:00
parent f514b3d2c7
commit 56eee99a67
4 changed files with 320 additions and 57 deletions
@@ -15,8 +15,9 @@ $stmt = $pdo->prepare("
start_column,
target_table,
source_type,
xls_sheet_index,
sample_xlsx,
idclient,
idclient,
clientname,
idschema,
schemaname,
@@ -39,6 +40,14 @@ if (!in_array($sourceType, ['XLS', 'API', 'PDF'], true)) {
$sourceType = 'XLS';
}
$xlsSheetIndex = isset($template['xls_sheet_index']) && $template['xls_sheet_index'] !== null
? (int)$template['xls_sheet_index']
: 0;
if ($xlsSheetIndex < 0) {
$xlsSheetIndex = 0;
}
$clientName = $template['clientname'] ?: '';
$schemaName = $template['schemaname'] ?: '';
$schemajson = $template['schemajson'] ? json_decode($template['schemajson'], true) : [];
@@ -267,7 +276,8 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
Source: <strong><?php echo htmlspecialchars($sourceType); ?></strong>
<?php if ($sourceType === 'XLS'): ?>
|
Header Row: <span id="headerRow"><?php echo $template['header_row']; ?></span> |
Sheet Number: <span id="xlsSheetIndex"><?php echo (int)$xlsSheetIndex; ?></span> |
Header Row: <span id="headerRow"><?php echo (int)$template['header_row']; ?></span> |
Start Column: <span id="startColumn"><?php echo htmlspecialchars($template['start_column']); ?></span>
<?php endif; ?>
</p>
@@ -656,7 +666,30 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
let workbook = XLSX.read(data, {
type: 'array'
});
let sheet = workbook.Sheets[workbook.SheetNames[0]];
const selectedSheetIndex = <?php echo (int)$xlsSheetIndex; ?>;
if (!workbook.SheetNames || workbook.SheetNames.length === 0) {
alert("No sheets found in this XLS file.");
return;
}
if (!workbook.SheetNames[selectedSheetIndex]) {
alert(
"The selected sheet number " + selectedSheetIndex +
" does not exist in this XLS file. Available sheets: " +
workbook.SheetNames.map((name, index) => index + " = " + name).join(", ")
);
return;
}
const selectedSheetName = workbook.SheetNames[selectedSheetIndex];
let sheet = workbook.Sheets[selectedSheetName];
console.log("Selected XLS sheet:", {
index: selectedSheetIndex,
name: selectedSheetName
});
// Read sheet range to determine column offset
const sheetRange = XLSX.utils.decode_range(sheet['!ref'] || 'A1');
@@ -745,7 +778,8 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
const uniqueLabels = [...new Set(knownLabels)];
console.group('🔍 Auto-detect header row');
console.log('Sheet name:', workbook.SheetNames[0]);
console.log('Sheet index:', selectedSheetIndex);
console.log('Sheet name:', selectedSheetName);
console.log('Total rows in sheet:', sheetData.length);
console.log('Labels from schema field titles:', knownLabels);
console.log('Unique labels to match against:', uniqueLabels);
@@ -884,8 +918,10 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
function saveXlsHeaders(headers, headerRow, startColumn) {
const payload = {
template_id: <?php echo $id; ?>,
xls_headers: JSON.stringify(headers)
xls_headers: JSON.stringify(headers),
xls_sheet_index: <?php echo (int)$xlsSheetIndex; ?>
};
if (headerRow !== undefined) payload.header_row = headerRow;
if (startColumn !== undefined) payload.start_column = startColumn;
@@ -897,8 +933,18 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
body: JSON.stringify(payload)
}).then(response => response.json())
.then(data => {
if (!data.success) console.error("❌ Error saving XLS headers:", data.message);
else console.log("✅ Saved headers, header_row:", headerRow, "start_column:", startColumn);
if (!data.success) {
console.error("❌ Error saving XLS headers:", data.message);
} else {
console.log(
"✅ Saved headers, header_row:",
headerRow,
"start_column:",
startColumn,
"xls_sheet_index:",
<?php echo (int)$xlsSheetIndex; ?>
);
}
})
.catch(error => console.error("❌ Fetch error:", error));
}