-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[book-to-hledger.scm] simple script to export book to hledger format
gnucash-cli book.gnucash --script book-to-hledger.scm > ~/.hledger.journal hledger bs hledger bs Balance Sheet 2023-10-20 || 2023-10-20 =====================++============ Assets || ---------------------++------------ Assets:Current:Bank || -168.3 USD ---------------------++------------ || -168.3 USD =====================++============ Liabilities || ---------------------++------------ ---------------------++------------ || =====================++============ Net: || -168.3 USD
- Loading branch information
1 parent
6afe816
commit 9583370
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
;; this file is meant to be run via the gnucash-cli interface: --script simple-book-add-txn.scm | ||
;; | ||
;; gnucash-cli book.gnucash --script simple-book-add-txn.scm | ||
;; | ||
|
||
(use-modules (gnucash core-utils)) | ||
(use-modules (gnucash engine)) | ||
(use-modules (gnucash app-utils)) | ||
(use-modules (gnucash report)) | ||
(use-modules (ice-9 match)) | ||
|
||
(define iso-date (qof-date-format-get-string QOF-DATE-FORMAT-ISO)) | ||
(define book (gnc-get-current-book)) | ||
(define root (gnc-get-current-root-account)) | ||
(define query (qof-query-create-for-splits)) | ||
(qof-query-set-book query (gnc-get-current-book)) | ||
(xaccQueryAddAccountMatch query (gnc-account-get-descendants root) QOF-GUID-MATCH-ANY QOF-QUERY-AND) | ||
(qof-query-set-sort-order | ||
query | ||
(list SPLIT-TRANS TRANS-DATE-POSTED) | ||
'() | ||
(list QUERY-DEFAULT-SORT)) | ||
|
||
(define (dump-transaction trans) | ||
(format #t "~a ~a\n" | ||
(gnc-print-time64 (xaccTransGetDate trans) iso-date) | ||
(xaccTransGetDescription trans)) | ||
(define (split->account s) | ||
(gnc-account-get-full-name (xaccSplitGetAccount s))) | ||
(define (split->amount s) | ||
(format #f "~a ~a" | ||
(exact->inexact (xaccSplitGetAmount s)) | ||
(gnc-commodity-get-mnemonic (xaccAccountGetCommodity (xaccSplitGetAccount s))))) | ||
(define max-width | ||
(let lp ((splits (xaccTransGetSplitList trans)) (maximum 0)) | ||
(match splits | ||
(() (+ maximum 2)) | ||
((s . rest) | ||
(lp rest (max maximum (+ (string-length (split->account s)) | ||
(string-length (split->amount s))))))))) | ||
(for-each | ||
(lambda (s) | ||
(define txn (xaccSplitGetParent s)) | ||
(define acc-name (split->account s)) | ||
(define amt-str (split->amount s)) | ||
(format #t " ~a~a~a\n" | ||
acc-name | ||
(make-string (- max-width (string-length acc-name) (string-length amt-str)) #\space) | ||
amt-str)) | ||
(xaccTransGetSplitList trans))) | ||
|
||
(define split-has-no-account? (compose null? xaccSplitGetAccount)) | ||
|
||
(let lp ((splits (xaccQueryGetSplitsUniqueTrans query))) | ||
(newline) | ||
(match splits | ||
(() #f) | ||
(((? split-has-no-account?) . rest) (lp rest)) | ||
((split . rest) (dump-transaction (xaccSplitGetParent split)) (lp rest)))) | ||
|
||
(qof-query-destroy query) | ||
(gnc-clear-current-session) |
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