Reports: don't use gnc:html-table-append-column!

This function is inefficient. Each column appended will require
scanning every row of existing html-table, scanning the row's elements
and appending the desired data.

It is much more efficient to build a html-table by append rows instead
-- html-table rows are stored in reverse, and each appended row is
built using (cons newrow existing-rows).
This commit is contained in:
Christopher Lam 2020-12-26 23:47:07 +08:00
parent 76e6b99395
commit ef8f8bdcd7
3 changed files with 31 additions and 35 deletions

View File

@ -427,8 +427,10 @@ new, totally cool report, consult the mailing list ~a.")
(if (not (null? list-val))
(let ((table (gnc:make-html-table)))
(gnc:html-table-append-column!
table (map symbol->string list-val))
(for-each
(lambda (cell)
(gnc:html-table-append-row! table (list (symbol->string cell))))
list-val)
(gnc:html-table-set-style! table "table"
'attribute (list "style" "width:200px"))
(gnc:html-table-set-caption! table

View File

@ -35,6 +35,7 @@
(use-modules (gnucash app-utils))
(use-modules (gnucash reports cash-flow-calc))
(use-modules (gnucash report))
(use-modules (srfi srfi-26))
(define reportname (N_ "Cash Flow Barchart"))
@ -327,6 +328,17 @@
(if (and non-zeros show-table?)
(let* ((table (gnc:make-html-table)))
(define (add-row date in out net)
(gnc:html-table-append-row!
table
(cons date
(map (cut gnc:make-html-table-cell/markup "number-cell" <>)
(append
(if show-in? (list in) '())
(if show-out? (list out) '())
(if show-net? (list net) '()))))))
(gnc:html-table-set-col-headers!
table (append (list (G_ "Date"))
(if show-in? (list (G_ "Money In")) '())
@ -335,22 +347,9 @@
(gnc:html-document-add-object!
doc (gnc:make-html-text (gnc:html-markup-h3 (G_ "Overview:"))))
(gnc:html-table-append-column! table (append date-string-list (list "Total")))
(if show-in?
(gnc:html-table-append-column! table (append in-list (list total-in))))
(if show-out?
(gnc:html-table-append-column! table (append out-list (list total-out))))
(if show-net?
(gnc:html-table-append-column! table (append net-list (list total-net))))
;; set numeric columns to align right
(for-each
(lambda (col)
(gnc:html-table-set-col-style!
table col "td"
'attribute (list "class" "number-cell")))
'(1 2 3))
(for-each add-row date-string-list in-list out-list net-list)
(add-row (G_ "Total") total-in total-out total-net)
(gnc:html-document-add-object! doc table))))

View File

@ -229,11 +229,7 @@
;; This exchanges the commodity-collector 'c' to one single
;; 'report-currency' according to the exchange-fn. Returns a gnc:monetary
(define (collector->monetary c date)
(if (not (number? date))
(throw 'wrong))
(gnc:sum-collector-commodity
c report-currency
(lambda (a b) (exchange-fn a b date))))
(gnc:sum-collector-commodity c report-currency (cut exchange-fn <> <> date)))
;; gets an account alist balances
;; output: (list acc bal0 bal1 bal2 ...)
@ -425,21 +421,20 @@
(list (G_ "Net Profit"))
(list (G_ "Net Worth")))
'())))
(gnc:html-table-append-column! table date-string-list)
(when show-sep?
(gnc:html-table-append-column! table minuend-balances)
(gnc:html-table-append-column! table subtrahend-balances))
(if show-net?
(gnc:html-table-append-column! table difference-balances))
;; set numeric columns to align right
(for-each
(lambda (col)
(gnc:html-table-set-col-style!
table col "td"
'attribute (list "class" "number-cell")))
'(1 2 3))
(lambda (date minuend subtrahend difference)
(gnc:html-table-append-row!
table
(cons date
(map
(cut gnc:make-html-table-cell/markup "number-cell" <>)
(append (if show-sep? (list minuend subtrahend) '())
(if show-net? (list difference) '()))))))
date-string-list
minuend-balances
subtrahend-balances
difference-balances)
(gnc:html-document-add-object! document table)))