Created
October 8, 2014 07:57
-
-
Save avalade/aa1c30072feb189781e0 to your computer and use it in GitHub Desktop.
Convert HSBC CSV files to Xero Format
This file contains 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
(defun convert-hsbc-to-xero () | |
(interactive) | |
(let ((filename (buffer-file-name)) | |
(new-name (replace-regexp-in-string "\.csv$" "-XERO.csv" (buffer-file-name))) | |
(hsbc-header-start-regexp "^Date") | |
(invalid-rows-regexps '("^$" "Opening Balance" "Closing Balance")) | |
(header-row "*Date,*Amount,Payee,Description,Reference,Check Number\n") | |
(record-regexp "^\\([0-9/]+\\),\\([[:alpha:][:blank:][:digit:]-/]+\\),\\([0-9.]*\\),\\([0-9.]*\\),\\([0-9.]*\\).*$")) | |
(copy-file filename new-name) | |
(find-file new-name) | |
(goto-char 1) | |
(re-search-forward hsbc-header-start-regexp) | |
(end-of-line) | |
(forward-char 1) | |
(delete-region (point-min) (point)) | |
(insert header-row) | |
(while (re-search-forward record-regexp nil t) | |
(let ((date (match-string 1)) | |
(desc (match-string 2)) | |
(withdrawal (- 0 (string-to-number (match-string 3)))) | |
(deposit (string-to-number (match-string 4))) | |
(balance (string-to-number (match-string 5)))) | |
(replace-match (format "%s,%s,,%s,," | |
date | |
(if (< withdrawal 0) withdrawal deposit) | |
desc)))) | |
(mapc | |
(lambda (r) (flush-lines r (point-min) (point-max))) | |
invalid-rows-regexps))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment