Created
June 7, 2017 03:42
-
-
Save tw0517tw/b4e302d29d48aca9547af59dae91cef4 to your computer and use it in GitHub Desktop.
把我 MoneyCare 的記帳資料換成 秒速記帳 的格式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| // npm i csv-parse | |
| const parseCsv = require('csv-parse/lib/sync'); | |
| // npm i csv-stringufy | |
| const stringifyCsv = require('csv-stringify/lib/sync'); | |
| // npm i date-fns | |
| const { format, differenceInSeconds } = require('date-fns'); | |
| const incomeFile = fs.readFileSync('./accountIncome.csv', 'utf8'); | |
| const incomeData = parseCsv(incomeFile, { columns: true }) | |
| const expenseFile = fs.readFileSync('./account.csv', 'utf8'); | |
| const expenseData = parseCsv(expenseFile, { columns: true }) | |
| const transformedIncomeData = incomeData.map(row => { | |
| const newRow = {}; | |
| newRow.日期時間 = format(new Date(row.myDate), 'YYYY-MM-DD HH:mm'); | |
| newRow.類別 = row.category; | |
| newRow.品項 = row.name; | |
| newRow.註記 = row.memo; | |
| newRow.價格 = row.money; | |
| newRow.匯率 = 1; | |
| newRow.帳戶 = '現金'; | |
| newRow.轉帳至帳戶 = ''; | |
| newRow.轉帳至金額 = ''; | |
| return newRow | |
| }); | |
| const transformedExpenseData = expenseData.map(row => { | |
| const newRow = {}; | |
| newRow.日期時間 = format(new Date(row.myDate), 'YYYY-MM-DD HH:mm'); | |
| newRow.類別 = row.category; | |
| newRow.品項 = row.name; | |
| newRow.註記 = row.memo; | |
| newRow.價格 = row.money; | |
| newRow.匯率 = 1; | |
| newRow.帳戶 = row.payStyle; | |
| newRow.轉帳至帳戶 = ''; | |
| newRow.轉帳至金額 = ''; | |
| return newRow | |
| }); | |
| const newData = transformedIncomeData.concat(transformedExpenseData).sort((a, b) => { | |
| return differenceInSeconds(new Date(a.日期時間), new Date(b.日期時間)) | |
| }) | |
| fs.writeFileSync('newdata.csv', stringifyCsv(newData, { header: true}), { encoding: 'utf8'}); | |
| console.log('done! check "newdata.csv"!'); |
worship
worship
worship
worship
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
worship