Skip to content

Instantly share code, notes, and snippets.

@tw0517tw
Created June 7, 2017 03:42
Show Gist options
  • Select an option

  • Save tw0517tw/b4e302d29d48aca9547af59dae91cef4 to your computer and use it in GitHub Desktop.

Select an option

Save tw0517tw/b4e302d29d48aca9547af59dae91cef4 to your computer and use it in GitHub Desktop.
把我 MoneyCare 的記帳資料換成 秒速記帳 的格式
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"!');
@hrs113355
Copy link

worship

@braincover
Copy link

worship

@chargo
Copy link

chargo commented Jun 7, 2017

worship

@Philip616
Copy link

worship

@jackypan1989
Copy link

worship

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment