feat: 설정 - 엑셀 데이터 내보내기(백업)
CI / build (push) Successful in 44s

- utils/backup.js: 가계부 전체(내역·계좌·분류·고정지출·예산·태그·자주내역)를
  이름 기준 참조로 .xlsx 다중 시트 생성. xlsx 동적 import(별도 청크)
- 웹/PC=다운로드, 앱(네이티브)=Filesystem 저장 + Share
- SettingsView: '데이터 백업' 카드(엑셀로 내보내기 동작 / 가져오기 버튼=준비중)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
ByungCheol
2026-06-28 08:14:19 +09:00
parent 4ffc01f484
commit 457fd8127a
6 changed files with 289 additions and 8 deletions
+84
View File
@@ -0,0 +1,84 @@
// 데이터 백업 — 가계부 전체 데이터를 엑셀(.xlsx)로 내보내기.
// 참조는 이름 기준(계좌명·분류명·태그명)으로 저장해 추후 '가져오기(복구)' 시 재매핑이 단순하도록 한다.
import { Capacitor } from '@capacitor/core'
import { accountApi } from '@/api/accountApi'
async function safe(promise) {
try {
return await promise
} catch {
return []
}
}
function fileName() {
const d = new Date()
const p = (n) => String(n).padStart(2, '0')
return `돈돼지가계부_백업_${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}.xlsx`
}
// 워크북을 플랫폼에 맞게 저장: 웹/PC=다운로드, 앱(네이티브)=파일 저장 후 공유
async function saveWorkbook(XLSX, wb, name) {
if (Capacitor.isNativePlatform()) {
const base64 = XLSX.write(wb, { type: 'base64', bookType: 'xlsx' })
const { Filesystem, Directory } = await import('@capacitor/filesystem')
const { Share } = await import('@capacitor/share')
const res = await Filesystem.writeFile({ path: name, data: base64, directory: Directory.Cache })
try {
await Share.share({ title: name, text: '가계부 백업 파일', url: res.uri })
} catch {
/* 공유 취소 — 파일은 캐시에 저장됨 */
}
} else {
XLSX.writeFile(wb, name)
}
}
export async function exportBackup() {
const XLSX = await import('xlsx')
const [entries, wallets, categories, recurrings, budgets, tags, quick] = await Promise.all([
safe(accountApi.list({})),
safe(accountApi.wallets()),
safe(accountApi.categories()),
safe(accountApi.recurrings()),
safe(accountApi.budgets()),
safe(accountApi.tags()),
safe(accountApi.quickEntries()),
])
const catNameById = {}
categories.forEach((c) => (catNameById[c.id] = c.name))
const wb = XLSX.utils.book_new()
const add = (name, rows) => XLSX.utils.book_append_sheet(wb, XLSX.utils.json_to_sheet(rows), name)
add('내역', entries.map((e) => ({
날짜: e.entryDate, 구분: e.type, 분류: e.category, 금액: e.amount, 메모: e.memo,
계좌: e.walletName, 입금계좌: e.toWalletName, 할부개월: e.installmentMonths,
태그: (e.tags || []).join(','),
})))
add('계좌', wallets.map((w) => ({
종류: w.type, 이름: w.name, '은행/카드사': w.issuer, 계좌번호: w.accountNumber,
카드유형: w.cardType, 개시잔액: w.openingBalance, 개시일: w.openingDate, '평가액(수동)': w.currentValue,
})))
add('분류', categories.map((c) => ({
구분: c.type, 이름: c.name, 대분류: c.parentId ? catNameById[c.parentId] || '' : '',
})))
add('고정지출', recurrings.map((r) => ({
제목: r.title, 구분: r.type, 금액: r.amount, 분류: r.category, 메모: r.memo,
계좌: r.walletName, 입금계좌: r.toWalletName, 주기: r.frequency,
: r.dayOfMonth, 요일: r.dayOfWeek, : r.monthOfYear,
시작일: r.startDate, 종료일: r.endDate, 활성: r.active,
})))
add('예산', budgets.map((b) => ({
분류: b.category, 고정: b.fixed, 기준단위: b.baseUnit, 기준금액: b.baseAmount,
: b.daily, : b.weekly, : b.monthly, : b.yearly,
})))
add('태그', tags.map((t) => ({ 이름: t.name })))
add('자주쓰는내역', quick.map((q) => ({
라벨: q.label, 구분: q.type, 분류: q.category, 금액: q.amount, 메모: q.memo, 계좌: q.walletName,
})))
await saveWorkbook(XLSX, wb, fileName())
}