Spaces:
Sleeping
Sleeping
Descagar de datos en PF en XLSX
Browse files
tecnicas/static/js/download-table-xlsx.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
document.addEventListener("DOMContentLoaded", function () {
|
| 2 |
+
const btn = document.getElementById("download-xls-btn");
|
| 3 |
+
if (!btn) return;
|
| 4 |
+
|
| 5 |
+
btn.addEventListener("click", async function () {
|
| 6 |
+
if (typeof XLSX === "undefined") {
|
| 7 |
+
alert(
|
| 8 |
+
"Librerías XLSX no están cargadas."
|
| 9 |
+
);
|
| 10 |
+
return;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
const BOOK = XLSX.utils.book_new();
|
| 14 |
+
|
| 15 |
+
const sections = document.querySelectorAll("section[data-tester]");
|
| 16 |
+
if (!sections || sections.length === 0) {
|
| 17 |
+
alert("No hay tablas para descargar.");
|
| 18 |
+
return;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
let fileXlsxName = "";
|
| 22 |
+
|
| 23 |
+
sections.forEach((sec) => {
|
| 24 |
+
const tester = sec.getAttribute("data-tester") || "tester";
|
| 25 |
+
const sessionName = sec.getAttribute("data-session-name") || "";
|
| 26 |
+
const sessionCode = sec.getAttribute("data-session-code") || "";
|
| 27 |
+
|
| 28 |
+
const sheetName = `${tester}`;
|
| 29 |
+
|
| 30 |
+
fileXlsxName = `datos_sesion_${sessionName ? sessionName.trim() : sessionCode.trim()
|
| 31 |
+
}`;
|
| 32 |
+
|
| 33 |
+
const table = sec.querySelector("table");
|
| 34 |
+
if (!table) return;
|
| 35 |
+
|
| 36 |
+
// Build CSV
|
| 37 |
+
const rows = [];
|
| 38 |
+
const headerCells = Array.from(table.querySelectorAll("thead tr th"));
|
| 39 |
+
const headers = headerCells.map((h) => h.textContent.trim());
|
| 40 |
+
rows.push(headers.map(escapeCsv).join(","));
|
| 41 |
+
|
| 42 |
+
const trs = table.querySelectorAll("tbody tr");
|
| 43 |
+
trs.forEach((tr) => {
|
| 44 |
+
const tds = Array.from(tr.querySelectorAll("td"));
|
| 45 |
+
const values = tds.map((td) => escapeCsv(td.textContent.trim()));
|
| 46 |
+
rows.push(values.join(","));
|
| 47 |
+
});
|
| 48 |
+
|
| 49 |
+
const csvContent = rows.join("\r\n");
|
| 50 |
+
const sheet = XLSX.read(csvContent, { type: "string" }).Sheets["Sheet1"];
|
| 51 |
+
XLSX.utils.book_append_sheet(BOOK, sheet, sheetName);
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
try {
|
| 55 |
+
XLSX.writeFile(BOOK, `${fileXlsxName}.xlsx`);
|
| 56 |
+
} catch (err) {
|
| 57 |
+
console.error(err);
|
| 58 |
+
alert("Error al generar el archivo XLSX: " + err.message);
|
| 59 |
+
}
|
| 60 |
+
});
|
| 61 |
+
|
| 62 |
+
const escapeCsv = (val) => {
|
| 63 |
+
if (val == null) return "";
|
| 64 |
+
let normalVal = val.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
|
| 65 |
+
const needsQuotes = /[",\n,]/.test(normalVal);
|
| 66 |
+
let v = String(normalVal).replace(/"/g, '""');
|
| 67 |
+
if (needsQuotes) v = `"${v}"`;
|
| 68 |
+
return v;
|
| 69 |
+
};
|
| 70 |
+
});
|
tecnicas/templates/tecnicas/manage_sesions/details-session-pf.html
CHANGED
|
@@ -244,11 +244,16 @@
|
|
| 244 |
{% include "../components/table_pf.html" with data=data_tester session_name=sesion.nombre_sesion session_code=sesion.codigo_sesion %}
|
| 245 |
{% endfor %}
|
| 246 |
</article>
|
| 247 |
-
<div class="flex justify-end mt-3">
|
| 248 |
<button id="download-csv-btn" class="cts-btn-general cts-btn-primary btn-push">
|
| 249 |
-
Descargar datos
|
|
|
|
|
|
|
|
|
|
| 250 |
</button>
|
| 251 |
</div>
|
|
|
|
|
|
|
| 252 |
{% else %}
|
| 253 |
{% include "../components/error-message.html" with message='Sin datos por mostrar aún' %}
|
| 254 |
{% endif %}
|
|
|
|
| 244 |
{% include "../components/table_pf.html" with data=data_tester session_name=sesion.nombre_sesion session_code=sesion.codigo_sesion %}
|
| 245 |
{% endfor %}
|
| 246 |
</article>
|
| 247 |
+
<div class="flex justify-end mt-3 gap-4 flex-wrap">
|
| 248 |
<button id="download-csv-btn" class="cts-btn-general cts-btn-primary btn-push">
|
| 249 |
+
Descargar datos CSV en zip
|
| 250 |
+
</button>
|
| 251 |
+
<button id="download-xls-btn" class="cts-btn-general cts-btn-primary btn-push">
|
| 252 |
+
Descargar datos en XLSX
|
| 253 |
</button>
|
| 254 |
</div>
|
| 255 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js"></script>
|
| 256 |
+
<script src="{% static 'js/download-table-xlsx.js' %}"></script>
|
| 257 |
{% else %}
|
| 258 |
{% include "../components/error-message.html" with message='Sin datos por mostrar aún' %}
|
| 259 |
{% endif %}
|