Rebuild backend target
This commit is contained in:
@@ -1,150 +1,134 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||||
<title>Excel Contact Import & Export</title>
|
||||
<script src="/backend/xlsx/xlsx.full.min.js"></script>
|
||||
<script src="/backend/handsontable/handsontable/dist/handsontable.full.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/handsontable/handsontable/dist/handsontable.full.min.css">
|
||||
<title>Excel Contact Import & Export</title>
|
||||
<script src="/backend/xlsx/xlsx.full.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/tabulator/css/tabulator.min.css">
|
||||
<script src="/backend/tabulator/js/tabulator.min.js"></script>
|
||||
<link rel="stylesheet" href="/backend/ios.css">
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
text-align: center;
|
||||
background-color: #f4f4f4;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
background: white;
|
||||
padding: 20px;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||
display: inline-block;
|
||||
}
|
||||
button {
|
||||
margin: 10px;
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
background-color: red;
|
||||
color: white;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
}
|
||||
#import-table {
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Excel Export & Import</h2>
|
||||
<button id="export-button">Kontakt Excel Export</button>
|
||||
<input type="file" id="file-input" accept=".xlsx">
|
||||
<button id="send-button">Daten Senden</button>
|
||||
<div id="import-table"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
let importedData = [];
|
||||
let hot; // Handsontable instance
|
||||
const url = sessionStorage.getItem("url");
|
||||
const stationNumber = sessionStorage.getItem("stationNumber");
|
||||
|
||||
function exportExcel() {
|
||||
fetch(url + '/backend/api/getContact', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({'station': sessionStorage.getItem("stationNumber")})
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data || data.length === 0) {
|
||||
alert("Keine Daten zum Exportieren.");
|
||||
return;
|
||||
}
|
||||
|
||||
const headers = Object.keys(data[0]);
|
||||
const rows = data.map(obj => headers.map(header => obj[header]));
|
||||
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...rows]);
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "Contacts");
|
||||
XLSX.writeFile(workbook, "ContactsExport.xlsx");
|
||||
})
|
||||
.catch(error => alert("Fehler beim Exportieren: " + error.message));
|
||||
}
|
||||
|
||||
function importExcelData(event) {
|
||||
const fileInput = document.getElementById("file-input");
|
||||
if (!fileInput.files.length) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(event) {
|
||||
const workbook = XLSX.read(event.target.result, { type: "binary" });
|
||||
const sheet = workbook.Sheets[workbook.SheetNames[0]];
|
||||
const jsonData = XLSX.utils.sheet_to_json(sheet, { header: 1 });
|
||||
|
||||
if (jsonData.length < 2) return;
|
||||
|
||||
const headers = jsonData[0];
|
||||
importedData = jsonData.slice(1);
|
||||
|
||||
if (hot) {
|
||||
hot.loadData(importedData);
|
||||
hot.updateSettings({ colHeaders: headers });
|
||||
} else {
|
||||
const container = document.getElementById("import-table");
|
||||
hot = new Handsontable(container, {
|
||||
data: importedData,
|
||||
colHeaders: headers,
|
||||
rowHeaders: true,
|
||||
width: '100%',
|
||||
height: 300,
|
||||
stretchH: 'all',
|
||||
autoColumnSize: false,
|
||||
manualColumnResize: true,
|
||||
colWidths: 120,
|
||||
wordWrap: false,
|
||||
licenseKey: 'non-commercial-and-evaluation'
|
||||
});
|
||||
}
|
||||
};
|
||||
reader.readAsBinaryString(fileInput.files[0]);
|
||||
}
|
||||
|
||||
function sendExcelData() {
|
||||
const jsonData = importedData.map(row => {
|
||||
let obj = {};
|
||||
hot.getColHeader().forEach((header, index) => {
|
||||
obj[header] = row[index] || "";
|
||||
});
|
||||
return obj;
|
||||
});
|
||||
|
||||
if (!url) {
|
||||
alert("Backend URL is not defined.");
|
||||
return;
|
||||
}
|
||||
fetch(url + '/backend/api/updateContacts', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ station: stationNumber, data: jsonData })
|
||||
})
|
||||
.then(response => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(result => alert("Daten erfolgreich gesendet!"))
|
||||
.catch(error => alert("Fehler beim Senden der Daten: " + error.message));
|
||||
}
|
||||
|
||||
document.getElementById("export-button").addEventListener("click", exportExcel);
|
||||
document.getElementById("file-input").addEventListener("change", importExcelData);
|
||||
document.getElementById("send-button").addEventListener("click", sendExcelData);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
<style>
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; padding: 20px; font-family: Arial, sans-serif; text-align: center; background-color: #f4f4f4; }
|
||||
.container { display: inline-block; width: min(1400px, 100%); padding: 20px; background: white; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,.1); }
|
||||
.button-area { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 10px; }
|
||||
button { margin: 10px 0; padding: 10px 20px; border: 0; background: red; color: white; border-radius: 5px; cursor: pointer; }
|
||||
button:hover:not(:disabled) { background: #c90000; }
|
||||
button:disabled { background: #999; cursor: not-allowed; }
|
||||
input[type="file"] { max-width: 100%; }
|
||||
#import-table { width: 100%; max-width: 100%; margin-top: 20px; text-align: left; }
|
||||
#status-message { min-height: 22px; margin-top: 12px; font-size: 14px; }
|
||||
.status-success { color: #087a20; } .status-error { color: #b00020; } .status-info { color: #333; }
|
||||
.tabulator { width: 100%; max-width: 100%; border: 1px solid #ccc; font-size: 13px; }
|
||||
.tabulator .tabulator-header, .tabulator .tabulator-header .tabulator-col { background: #ededed; }
|
||||
.tabulator-cell.tabulator-editing { border: 2px solid red !important; }
|
||||
@media (max-width:700px) { body { padding:10px; } .container { padding:12px; } .button-area { flex-direction:column; } button,input[type="file"] { width:100%; } }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h2>Excel Export & Import</h2>
|
||||
<div class="button-area">
|
||||
<button id="export-button" type="button">Kontakt Excel Export</button>
|
||||
<input type="file" id="file-input" accept=".xlsx,.xls">
|
||||
<button id="send-button" type="button" disabled>Daten senden</button>
|
||||
</div>
|
||||
<div id="status-message" class="status-info" role="status" aria-live="polite"></div>
|
||||
<div id="import-table"></div>
|
||||
</div>
|
||||
<script>
|
||||
document.addEventListener("DOMContentLoaded", function () {
|
||||
let table = null;
|
||||
let importedHeaders = [];
|
||||
const url = sessionStorage.getItem("url");
|
||||
const stationNumber = sessionStorage.getItem("stationNumber");
|
||||
const fixedHeaders = null;
|
||||
const sourceHeaders = null;
|
||||
const sendButton = document.getElementById("send-button");
|
||||
const fileInput = document.getElementById("file-input");
|
||||
const statusMessage = document.getElementById("status-message");
|
||||
|
||||
function setStatus(message, type = "info") { statusMessage.textContent = message; statusMessage.className = "status-" + type; }
|
||||
function asText(value) { return value === undefined || value === null ? "" : String(value); }
|
||||
function requireSession() {
|
||||
if (!url) { setStatus("Backend-URL ist in der Session nicht definiert.", "error"); return false; }
|
||||
if (!stationNumber) { setStatus("Stationsnummer ist in der Session nicht definiert.", "error"); return false; }
|
||||
return true;
|
||||
}
|
||||
function makeColumns(headers) { return headers.map(header => ({ title: header, field: header, editor: "input", minWidth: 120, headerSort: true })); }
|
||||
async function showTable(data, headers) {
|
||||
importedHeaders = headers;
|
||||
if (table) { table.setColumns(makeColumns(headers)); await table.replaceData(data); return; }
|
||||
table = new Tabulator("#import-table", { data, height: Math.max(300, window.innerHeight - 210) + "px", layout: "fitColumns", responsiveLayout: "collapse", movableColumns: true, placeholder: "Keine Daten importiert", rowHeader: { formatter: "rownum", headerSort: false, hozAlign: "center", frozen: true, width: 50 }, columns: makeColumns(headers) });
|
||||
}
|
||||
async function exportExcel() {
|
||||
if (!requireSession()) return;
|
||||
setStatus("Exportdaten werden geladen ...");
|
||||
try {
|
||||
const response = await fetch(url + "/backend/api/getContact", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ station: stationNumber }) });
|
||||
if (!response.ok) throw new Error("HTTP " + response.status + ": " + await response.text());
|
||||
const data = await response.json();
|
||||
if (!Array.isArray(data) || !data.length) throw new Error("Keine Daten zum Exportieren vorhanden.");
|
||||
const headers = fixedHeaders || Object.keys(data[0]);
|
||||
const keys = sourceHeaders || headers;
|
||||
const rows = data.map(item => keys.map(key => asText(item[key])));
|
||||
const worksheet = XLSX.utils.aoa_to_sheet([headers, ...rows]);
|
||||
worksheet["!cols"] = headers.map(header => ({ wch: Math.max(15, header.length + 2) }));
|
||||
const workbook = XLSX.utils.book_new();
|
||||
XLSX.utils.book_append_sheet(workbook, worksheet, "Contacts");
|
||||
XLSX.writeFile(workbook, "ContactsExport.xlsx");
|
||||
setStatus(data.length + " Zeile(n) exportiert.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Exportieren: " + error.message, "error"); }
|
||||
}
|
||||
function importExcelData() {
|
||||
const file = fileInput.files[0];
|
||||
if (!file) return;
|
||||
sendButton.disabled = true; setStatus("Excel-Datei wird eingelesen ...");
|
||||
const reader = new FileReader();
|
||||
reader.onload = async event => {
|
||||
try {
|
||||
const workbook = XLSX.read(event.target.result, { type: "array", cellDates: false });
|
||||
if (!workbook.SheetNames.length) throw new Error("Die Datei enthält kein Arbeitsblatt.");
|
||||
const rows = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[0]], { header: 1, defval: "", raw: false });
|
||||
if (rows.length < 2) throw new Error("Die Datei enthält keine Datenzeilen.");
|
||||
const headers = rows[0].map(value => asText(value).trim());
|
||||
if (headers.some(header => !header)) throw new Error("Eine oder mehrere Spaltenüberschriften sind leer.");
|
||||
if (new Set(headers).size !== headers.length) throw new Error("Spaltenüberschriften dürfen nicht doppelt vorkommen.");
|
||||
const data = rows.slice(1).filter(row => row.some(value => asText(value).trim() !== "")).map(row => Object.fromEntries(headers.map((header, index) => [header, asText(row[index])] )));
|
||||
if (!data.length) throw new Error("Keine gefüllten Datenzeilen gefunden.");
|
||||
await showTable(data, headers); sendButton.disabled = false; setStatus(data.length + " Zeile(n) importiert.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Import: " + error.message, "error"); }
|
||||
};
|
||||
reader.onerror = () => setStatus("Die Datei konnte nicht gelesen werden.", "error");
|
||||
reader.readAsArrayBuffer(file);
|
||||
}
|
||||
async function sendExcelData() {
|
||||
if (!table || !importedHeaders.length) { setStatus("Es wurden noch keine Daten importiert.", "error"); return; }
|
||||
if (!requireSession()) return;
|
||||
const data = table.getData().map(row => Object.fromEntries(importedHeaders.map(header => [header, asText(row[header])] )));
|
||||
sendButton.disabled = true; setStatus("Daten werden gesendet ...");
|
||||
try {
|
||||
const response = await fetch(url + "/backend/api/updateContacts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ station: stationNumber, data }) });
|
||||
if (!response.ok) throw new Error("HTTP " + response.status + ": " + await response.text());
|
||||
const contentType = response.headers.get("content-type") || "";
|
||||
if (contentType.includes("application/json")) await response.json();
|
||||
setStatus(data.length + " Zeile(n) erfolgreich gesendet.", "success");
|
||||
} catch (error) { console.error(error); setStatus("Fehler beim Senden: " + error.message, "error"); }
|
||||
finally { sendButton.disabled = false; }
|
||||
}
|
||||
function resizeImportTable() {
|
||||
if (!table) return;
|
||||
table.setHeight(Math.max(300, window.innerHeight - 210));
|
||||
table.redraw(true);
|
||||
}
|
||||
|
||||
window.addEventListener("resize", resizeImportTable);
|
||||
window.addEventListener("orientationchange", resizeImportTable);
|
||||
document.getElementById("export-button").addEventListener("click", exportExcel);
|
||||
fileInput.addEventListener("change", importExcelData);
|
||||
sendButton.addEventListener("click", sendExcelData);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user