Skip to content

Commit 532ac07

Browse files
committed
Simple Excel export
1 parent 2616f9e commit 532ac07

4 files changed

Lines changed: 39 additions & 8 deletions

File tree

dr-controler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { drLib } from './dr-lib.js';
2-
import { exportCSVFile } from './exportToCSV.js';
2+
import { exportXLSFile } from './exporToXLS.js';
33

44
/**
55
* UI management code.
@@ -77,8 +77,8 @@ async function showQuestionnary(questionaryId) {
7777

7878
setSharingLink(questionaryId);
7979
const {headers, patientsData} = await setPatientList(questionaryId);
80-
document.getElementById('button-download').onclick = () => {
81-
exportCSVFile(headers, patientsData, 'patients');
80+
document.getElementById('button-download').onclick = async () => {
81+
await exportXLSFile(headers, patientsData, 'patients');
8282
}
8383
document.getElementById('questionnary-view').style.display = 'block';
8484
}

dr-patient-view-controler.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { drPatientLib } from "./dr-patient-view-lib.js";
2-
import { exportCSVFile } from "./exportToCSV.js";
2+
import { exportXLSFile } from './exporToXLS.js';
33
import { patientLib } from "./patient-lib.js";
44
/**
55
* Based on
@@ -12,6 +12,8 @@ import { patientLib } from "./patient-lib.js";
1212

1313

1414
let infos;
15+
let username;
16+
let qId;
1517
window.onload = async (event) => {
1618
const { patientApiEndpoint, questionaryId } = getRequestFrormApiEndPoint();
1719
infos = await drPatientLib.setRefresh(patientApiEndpoint, questionaryId, refresh)
@@ -22,8 +24,10 @@ window.onload = async (event) => {
2224
const formTitle = document.getElementById('card-questionnary-details-title');
2325
formTitle.innerHTML = patientLib.getFormTitle(questionaryId);
2426

27+
qId = questionaryId;
28+
2529
// -- set patient Id
26-
const username = infos.user.username;
30+
username = infos.user.username;
2731
document.getElementById('patient-label').innerHTML = username;
2832
}
2933

@@ -43,8 +47,8 @@ const downloadHeaders = Object.assign({
4347

4448
async function refresh (lines) {
4549
// -- set download button
46-
document.getElementById('button-download').onclick = () => {
47-
exportCSVFile(downloadHeaders, lines, username + '-' + questionaryId);
50+
document.getElementById('button-download').onclick = async () => {
51+
await exportXLSFile(downloadHeaders, lines, username + '-' + qId);
4852
};
4953

5054
// -- update table

exporToXLS.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// add XML export to file
2+
import "https://unpkg.com/write-excel-file@1.x/bundle/write-excel-file.min.js";
3+
4+
export async function exportXLSFile(headers, lines, fileTitle) {
5+
try {
6+
console.log({ headers, lines });
7+
8+
const schema = Object.entries(headers).map((h) => {
9+
const key = h[0];
10+
return {
11+
column: h[1],
12+
value: function (e) {
13+
return e[key];
14+
},
15+
};
16+
});
17+
18+
19+
const exportedFilename = fileTitle + ".xlsx" || "export.xlsx";
20+
await writeXlsxFile(lines, {
21+
schema,
22+
fileName: exportedFilename,
23+
});
24+
} catch (e) {
25+
console.log(e, e.stack);
26+
}
27+
}

exportToCSV.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function fenceLine(items) {
55
return '"' + items.map(i => i.replaceAll('"','\'\'')).join('","') + '"\r\n';
66
}
77

8-
export function exportCSVFile(headers, lines, fileTitle) {
8+
export async function exportCSVFile(headers, lines, fileTitle) {
99
let csv = fenceLine(Object.values(headers));
1010

1111
for (const line of lines) {

0 commit comments

Comments
 (0)