For those interested in playing around with the guile stuff, here's a
few functions I wrote for manipulating accounts and transactions...
These will probably show up in src/scm at some point, but you can play
around with them now.

To get started, you can do this:

  $ gnucash --shell
  guile> (load "share/scm/startup/init.scm")
  guile> (define test-group (xaccReadAccountGroup "data/splitdemo.xac"))

and now you've got a group to play with.  These functions are for
either executing a function on each item (the for-each functions) or
for generating a list containing all the items (the -list functions)
on which you could then use the normal for-each or map functions.

So you can say, for example:

  (gnc:group-accounts-for-each (lambda (account)
                                 (display (xaccAccountGetName account))
                                 (newline))
                               test-group)

to print all the account names or

  (map xaccAccountGetName (gnc:group-accounts-list test-group))

to generate a list of the names.

(If someone gets ambitious, we really ought to think about globally
renaming the xacc* functions to gnc_* (or whatever), if we're ever
going to.  This should be done before people write a lot of
code/scripts that depend on the old names.)

(define (gnc:group-accounts-for-each func grp)
  (do ((i 0 (+ i 1))
       (n (xaccGroupGetNumAccounts grp)))
      ((= i n))    
    (func (xaccGroupGetAccount grp i))))

(define (gnc:group-accounts-list grp)
  (define (list-helper grp i n)
    (if (= i n)
        '()
        (cons (xaccGroupGetAccount grp i) (list-helper grp (+ i 1) n))))
  (list-helper grp 0 (xaccGroupGetNumAccounts grp)))

(define (gnc:account-transactions-for-each func acct)
  (do ((i 0 (+ i 1))
       (last-parent #f)
       (n (xaccAccountGetNumSplits acct)))
      ((= i n))
    (let ((parent (xaccSplitGetParent (xaccAccountGetSplit acct i))))
      (if (not (equal? last-parent parent))
          (begin
            (set! last-parent parent)
            (func parent))))))

(define (gnc:account-transactions-list acct)
  (define (list-helper acct last-parent i n)
    (if (= i n)
        '()
        (let ((parent (xaccSplitGetParent (xaccAccountGetSplit acct i))))
          (if (equal? last-parent parent)
              (list-helper acct parent (+ i 1) n)
              (cons parent (list-helper acct parent (+ i 1) n))))))
  (list-helper acct #f 0 (xaccAccountGetNumSplits acct)))


(define (gnc:account-splits-for-each func acct)
  (do ((i 0 (+ i 1))
       (n (xaccAccountGetNumSplits acct)))
      ((= i n))
    (func (xaccSplitGetParent (xaccAccountGetSplit acct i)))))

(define (gnc:account-splits-list acct)
  (define (list-helper acct i n)
    (if (= i n)
        '()
        (cons (xaccAccountGetSplit acct i) (list-helper acct (+ i 1) n))))
  (list-helper acct 0 (xaccAccountGetNumSplits acct)))

-- 
Rob Browning <[EMAIL PROTECTED]> PGP=E80E0D04F521A094 532B97F5D64E3930
----- %< -------------------------------------------- >% ------
The GnuCash / X-Accountant Mailing List
To unsubscribe, send mail to [EMAIL PROTECTED] and
put "unsubscribe gnucash-devel [EMAIL PROTECTED]" in the body

Reply via email to