Skip to content

Commit

Permalink
feat: add add file handler
Browse files Browse the repository at this point in the history
  • Loading branch information
khajornritdacha committed Dec 20, 2023
1 parent c3182ea commit 3c5ea4a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 53 deletions.
19 changes: 14 additions & 5 deletions src/components/DropMember.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<script lang="ts">
import { onDrop } from '$lib';
import DropFile from 'svelte-parts/DropFile.svelte'
import DropFile from 'svelte-parts/DropFile.svelte';
import { data_store } from '../store';
import { loadFromSheet } from '$lib/utils/sheetService';
export async function onDrop(files: File[]) {
const file = files[0];
const raw_sheet = await file.arrayBuffer();
console.log(raw_sheet);
data_store.set(loadFromSheet(raw_sheet));
}
</script>

<!-- TODO: weird bug with DropFile -->
<DropFile onDrop={onDrop}>
<!-- <div class="text-3xl bg-red-500 hover:bg-red-600 text-gray-800 font-bold py-6 px-6 rounded-2xl inline-flex items-center cursor-pointer">
<DropFile {onDrop}>
<!-- <div class="text-3xl bg-red-500 hover:bg-red-600 text-gray-800 font-bold py-6 px-6 rounded-2xl inline-flex items-center cursor-pointer">
<svg class="h-6 w-6 mr-2 text-gray-800" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"> <path stroke="none" d="M0 0h24v24H0z"/> <path d="M7 18a4.6 4.4 0 0 1 0 -9h0a5 4.5 0 0 1 11 2h1a3.5 3.5 0 0 1 0 7h-1" /> <polyline points="9 15 12 12 15 15" /> <line x1="12" y1="12" x2="12" y2="21" /></svg>
<span class="font-bold text-3xl text-gray-800">Upload file</span>
</div> -->
</DropFile>
</DropFile>
4 changes: 2 additions & 2 deletions src/components/GroupInformation.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { data } from '../store';
import { data_store } from '../store';
export let group_cnt: number;
export let day: number;
Expand All @@ -12,7 +12,7 @@
<h2 class="py-3 text-2xl flex font-bold">Number of members :</h2>
<input
type="number"
bind:value={$data.length}
bind:value={$data_store.length}
disabled
class="p-2 ml-2 rounded grow"
id="n_member"
Expand Down
9 changes: 0 additions & 9 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
// place files you want to import through the `$lib` alias in this folder.
import { read } from 'xlsx';

export async function onDrop(files: File[]) {
const file = files[0];
const raw_sheet = await file.arrayBuffer();
const workbook = read(raw_sheet);
console.log(workbook);
}

export function onDownload() {
return;
}
8 changes: 8 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface Person {
names: string;
roles: string;
baan: string;
ex_camp: string;
gender: string;
id: number;
}
19 changes: 19 additions & 0 deletions src/lib/utils/sheetService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { read } from 'xlsx';

export function loadFromSheet(raw_sheet: ArrayBuffer) {
const workbook = read(raw_sheet);
const data = [];
for (let i = 2; ; i++) {
if (!workbook.Sheets['database'][`A${i}`]) break;
data.push({
names: workbook.Sheets['database'][`A${i}`].v,
roles: workbook.Sheets['database'][`D${i}`].v,
baan: workbook.Sheets['database'][`E${i}`].v,
ex_camp: workbook.Sheets['database'][`F${i}`].v,
gender: workbook.Sheets['database'][`G${i}`].v,
id: i - 1
});
}
console.log(data);
return data;
}
39 changes: 2 additions & 37 deletions src/store.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,4 @@
import { writable } from 'svelte/store';
import * as XLSX from 'xlsx';
import type { Person } from '$lib/types';

export interface Person {
names: string;
roles: string;
baan: string;
ex_camp: string;
gender: string;
id: number;
}

function createData() {
const { subscribe, set } = writable<Person[]>([]);

function load(raw_sheet: ArrayBuffer) {
const workbook = XLSX.read(raw_sheet);
const data = [];
for (let i = 2; ; i++) {
if (!workbook.Sheets['database'][`A${i}`]) break;
data.push({
names: workbook.Sheets['database'][`A${i}`].v,
roles: workbook.Sheets['database'][`D${i}`].v,
baan: workbook.Sheets['database'][`E${i}`].v,
ex_camp: workbook.Sheets['database'][`F${i}`].v,
gender: workbook.Sheets['database'][`G${i}`].v,
id: i - 1
});
}
set(data);
}

return {
subscribe,
load: (raw_sheet: ArrayBuffer) => load(raw_sheet)
};
}

export const data = createData();
export const data_store = writable<Person[]>([]);

0 comments on commit 3c5ea4a

Please sign in to comment.