From 2efa7f4f02d7fc4af2983e292519e110d2647f9e Mon Sep 17 00:00:00 2001 From: Marvin Lehmann Date: Fri, 29 Oct 2021 16:42:24 +0200 Subject: [PATCH] Add Wish Tally excel import --- src/locales/en.json | 4 +- src/routes/wish/_excelImport.svelte | 93 +++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/locales/en.json b/src/locales/en.json index 647dfa40..d28038eb 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -263,6 +263,7 @@ "default": "Paimon.moe Export", "takagg": "TakaGG Gacha Export", "genshinwishes": "GenshinWishes Export", + "wishtally": "WishTally", "notice": [ "This feature still in BETA please backup first by going to Setting then Export to Excel!", "Wish with the same timestamp and reward name will NOT be touched (so existing wish will not be rewritten)", @@ -272,7 +273,8 @@ "selectFile": { "default": "Drag & drop Paimon.moe excel file here, or click here to select", "takagg": "Drag & drop TakaGG gacha export excel file here, or click here to select", - "genshinwishes": "Drag & drop GenshinWishes csv file here, or click here to select" + "genshinwishes": "Drag & drop GenshinWishes csv file here, or click here to select", + "wishtally": "Drag & drop Wish Tally excel file here, or click here to select" }, "processing": "Processing...", "addedOn": "Inserted on the:", diff --git a/src/routes/wish/_excelImport.svelte b/src/routes/wish/_excelImport.svelte index 3dcf4371..ca3f3fef 100644 --- a/src/routes/wish/_excelImport.svelte +++ b/src/routes/wish/_excelImport.svelte @@ -428,6 +428,84 @@ loading = false; } + async function readWishTallyExcel(workbook) { + const bannerCategories = { + 'character-event': 'Character Event Wish History', + 'weapon-event': 'Weapon Event Wish History', + standard: 'Permanent Wish History', + beginners: 'Novice Wish History', + }; + + const weapons = Object.values(weaponList); + const chars = Object.values(characters); + + for (const [id, category] of Object.entries(bannerCategories)) { + const sheet = workbook.getWorksheet(category); + const wishes = []; + sheet.eachRow((row, index) => { + if (index === 1) return; + const type = row.getCell(4).text.toLowerCase(); + let time = row.getCell(5); + const fullName = row.getCell(6).text; + + if (time.type === ValueType.Date) { + time = dayjs.utc(time.value).format('YYYY-MM-DD HH:mm:ss'); + } else { + time = time.text; + } + + let name = ''; + if (type === 'weapon') { + const weapon = weapons.find((e) => e.name === fullName); + if (weapon === undefined) { + pushToast($t('wish.excel.errorUnknownItem'), 'error'); + error = { + banner: category, + line: index, + name: fullName, + type, + }; + step = 2; + loading = false; + throw 'unknown reward name'; + } + + name = weapon.id; + } else if (type === 'character') { + const character = chars.find((e) => e.name === fullName); + if (character === undefined) { + pushToast($t('wish.excel.errorUnknownItem'), 'error'); + error = { + banner: category, + line: index, + name: fullName, + type, + }; + step = 2; + loading = false; + throw 'unknown reward name'; + } + + name = character.id; + } + + if (name === '') { + pushToast($t('wish.excel.errorUnknownItem'), 'error'); + loading = false; + throw 'unknown reward name'; + } + + wishes.push([type, time, name]); + }); + + console.log('from excel', category, wishes.length); + await parseData(id, wishes); + } + + step = 1; + loading = false; + } + function readCSV(file) { const reader = new FileReader(); reader.onload = () => { @@ -450,12 +528,14 @@ loading = false; } + const readFunc = { + 'default': readPaimonExcel, + 'takagg': readGachaExportExcel, + 'wishtally': readWishTallyExcel + } + try { - if (selectedType === 'default') { - readPaimonExcel(workbook); - } else { - readGachaExportExcel(workbook); - } + readFunc[selectedType](workbook); } catch (err) { console.log(err); pushToast($t('wish.excel.errorReadExcel'), 'error'); @@ -546,6 +626,9 @@ > {$t('wish.excel.genshinwishes')} +