/// <summary> /// 取引行の解析 /// </summary> /// <param name="line">取引行</param> /// <returns>成功フラグ</returns> /// public bool readTransaction(string line) { string[] columns = CsvUtil.SplitCsv(line, false); if (columns.Length < 8) { return(false); } Transaction transaction = new Transaction(); // 日付の処理 string[] ary = columns[0].Split(new char[] { '/' }); try { if (ary.Length == 3) { transaction.date = new DateTime(int.Parse(ary[0]), int.Parse(ary[1]), int.Parse(ary[2]), 0, 0, 0); } else if (ary.Length == 2) { DateTime now = DateTime.Now; int n1 = int.Parse(ary[0]); int n2 = int.Parse(ary[1]); if (n1 >= 2000) { // 年と月のみ: 日は1日とする transaction.date = new DateTime(n1, n2, 1, 0, 0, 0); } else { // 月と日のみ。年は推定する。 int mm = n1; int dd = n2; DateTime d = new DateTime(now.Year, mm, dd, 0, 0, 0); // 同一年として、日付が6ヶ月以上先の場合、昨年とみなす。 // 逆に6ヶ月以上前の場合、翌年とみなす。 TimeSpan ts = d - now; if (ts.TotalDays > 366 / 2) { d = new DateTime(now.Year - 1, mm, dd, 0, 0, 0); } else if (ts.TotalDays < -366 / 2) { d = new DateTime(now.Year + 1, mm, dd, 0, 0, 0); } transaction.date = d; } } else { return(false); } } catch { // 日付が範囲外 (ArgumentRangeOutOfException など) return(false); } // 摘要 transaction.desc = columns[1]; // 入金額/出金額 try { transaction.value = int.Parse(columns[2]); } catch { try { transaction.value = -int.Parse(columns[4]); } catch { return(false); } } // 残高 try { transaction.balance = int.Parse(columns[6]); } catch { // Note: 残高は入っていない場合もある transaction.balance = 0; } mTransactions.Add(transaction); return(true); }
// CSV のフィールド分割 private string[] SplitCsv(string line) { return(CsvUtil.SplitCsv(line, mRule.isTSV)); }
/// <summary> /// アカウント情報を読み込む /// </summary> /// <param name="line">アカウント情報行</param> /// <returns></returns> private bool readAccountInfo(string line, Hashtable nameHash) { string[] columns = CsvUtil.SplitCsv(line, false); if (columns.Length < 3) { return(false); } if (columns.Length >= 5 && columns[4] != "JPY") { return(false); } if (!isCreditCard) { // 銀行口座 string bankName = columns[0]; string branchName = columns[1]; string accountId = columns[2]; if (columns.Length >= 4) { try { balance = int.Parse(columns[3]); hasBalance = true; } catch { hasBalance = false; } } mIdent = bankName; mBankId = bankName; mBranchId = branchName; // getDummyId(branchName).ToString(); mAccountId = accountId; } else { // クレジットカード string cardName = columns[0]; try { // 借入額 balance = -int.Parse(columns[2]); hasBalance = true; } catch { hasBalance = false; } // 末尾の 'カード' という文字を抜く cardName = "CARD_" + Regex.Replace(cardName, @"カード$", ""); // 2カラム目は空の模様 //string balance = columns[2]; mIdent = ""; mBankId = ""; mBranchId = ""; // 重複しないよう、連番を振る int counter; if (!nameHash.ContainsKey(cardName)) { counter = 1; } else { counter = (int)nameHash[cardName]; } mAccountId = cardName + counter.ToString(); nameHash[cardName] = counter + 1; } return(true); }
// 1行解析 // CSV の各カラムはすでに分解されているものとする public Transaction parse(string[] row) { Transaction t = new Transaction(); // 日付 string date = getCol(row, "Date"); if (date != null) { t.date = CsvUtil.parseDate(date); } else { int year = getColInt(row, "Year"); int month = getColInt(row, "Month"); int day = getColInt(row, "Day"); if (year == 0 || month == 0 || day == 0) { return(null); } if (year < 100) { year += 2000; } t.date = new DateTime(year, month, day, 0, 0, 0); } // ID string id = getCol(row, "Id"); if (id != null) { try { t.id = getColInt(row, "Id"); } catch (FormatException) { // just ignore : do not use ID } } // 金額 t.value = getColInt(row, "Income"); t.value -= getColInt(row, "Outgo"); // 残高 t.balance = getColInt(row, "Balance"); // 適用 t.desc = getMultiCol(row, "Desc"); // 備考 t.memo = getMultiCol(row, "Memo"); // トランザクションタイプを自動設定 t.GuessTransType(t.value >= 0); return(t); }