Skip to content

Commit

Permalink
feat: download xlsx instead of csv
Browse files Browse the repository at this point in the history
  • Loading branch information
starvy committed Feb 24, 2024
1 parent c09a6b0 commit a46d48b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
25 changes: 18 additions & 7 deletions apps/web/src/routes/admin/(authenticated)/users/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@
export let data: PageServerData;
// TODO: xlsx download
const downloadCsv = async () => {
const csv = await trpc().users.csv.query();
const blob = new Blob([csv], { type: 'text/csv' });
const downloadXlsx = async (e: Event) => {
const base64 = await trpc().users.xlsx.query();
const byteCharacters = atob(base64);
// Create a Uint8Array
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
// Create a Blob from the Uint8Array
const blob = new Blob([byteArray], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
});
// download the file
const anchor = window.document.createElement('a');
anchor.href = window.URL.createObjectURL(blob);
anchor.download = 'users.csv';
document.body.appendChild(anchor);
anchor.download = 'vysledky.xlsx';
anchor.click();
document.body.removeChild(anchor);
window.URL.revokeObjectURL(anchor.href);
};
Expand Down Expand Up @@ -77,7 +88,7 @@
icon="material-symbols:add-circle-outline-rounded"
title="Přidat uživatele"
/>
<Button on:click={downloadCsv} icon="material-symbols:download" title="Stáhnout CSV" />
<Button on:click={downloadXlsx} icon="material-symbols:download" title="Stáhnout výsledky" />
</div>
</div>
<div class="mx-auto mx-auto mb-6 flex max-w-screen-xl flex-col px-4 py-3 md:px-6 md:px-6">
Expand Down
6 changes: 3 additions & 3 deletions packages/trpc/server/services/testService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ResultCellMap extends Map<string, Record[]> {
}
}

export const exportXlsx = async (): Promise<void> => {
export const exportXlsx = async (): Promise<string> => {
const workbook = XLXS.utils.book_new();
XLXS.set_fs(fs);

Expand Down Expand Up @@ -230,7 +230,7 @@ export const exportXlsx = async (): Promise<void> => {
const userSheet = getUserSheet(usersDb, resultCell);
XLXS.utils.book_append_sheet(workbook, userSheet, 'users');

XLXS.writeFileXLSX(workbook, 'vysledky.xlsx');
return XLXS.write(workbook, { bookType: 'xlsx', type: 'base64' });
};

const getUserSheet = (users: User[], resultCell: ResultCellMap): XLXS.WorkSheet => {
Expand Down Expand Up @@ -283,7 +283,7 @@ const getSumXlsxFnString = (headers: string[], row: number, correctRow: number):
};
})
.filter((h) => h.s.startsWith('Q'))
.map(({i}) => i);
.map(({ i }) => i);

const [colMin, colMax] = [colRange[0], colRange[colRange.length - 1]].map((c) => ALPHABET[c]);
const correctRange = `${colMin}$${correctRow}:${colMax}$${correctRow}`;
Expand Down

0 comments on commit a46d48b

Please sign in to comment.