Files
thetool/scripts/constructionconsent/convert-xlsx.php
2025-03-17 08:56:54 +00:00

77 lines
2.5 KiB
PHP

<DOCTYPE html>
<html>
<head>
<title>Test XLSX</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
#output { width: 100%; height: 400px; margin-top: 10px; }
</style>
</head>
<body>
<input type="file" id="fileInput" accept=".xlsx">
<script src="/plugins/xlsx/xlsx.min.js"></script>
<script>
document.getElementById('fileInput').addEventListener('change', handleFile);
function handleFile(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (e) => {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, {type: 'array', cellStyles: true});
const jsonData = convertToJson(workbook);
downloadJson(jsonData);
};
reader.readAsArrayBuffer(file);
}
function convertToJson(workbook) {
const result = [];
const headers = ["FLR", "KG", "GST", "EZ", "Vorname", "Nachname", "Straße", "PLZ, Ort"];
// Process first worksheet
const worksheet = workbook.Sheets[workbook.SheetNames[0]];
const range = XLSX.utils.decode_range(worksheet['!ref']);
for(let rowNum = 1; rowNum <= range.e.r; rowNum++) {
const rowData = { fields: {}, bg_colors: {} };
headers.forEach((header, colIdx) => {
const cellAddress = XLSX.utils.encode_cell({r: rowNum, c: colIdx});
const cell = worksheet[cellAddress];
// Get cell value
const value = cell ? cell.v : null;
rowData.fields[header] = value;
// Get background color
let hexColor = 'none';
console.log(cell);
if (cell?.s?.fgColor?.rgb) {
hexColor = `#${cell.s.fgColor.rgb}`; // Remove alpha channel
}
rowData.bg_colors[header] = hexColor;
});
result.push(rowData);
}
return result;
}
function downloadJson(data) {
const blob = new Blob([JSON.stringify(data, null, 2)], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'output.json';
a.click();
}
</script>
</body>
</html>