[GNC] GNUCash & Macros

Kevin Buckley kevin.m.buckley at gmail.com
Fri Dec 31 03:22:42 EST 2021


On Wed, 26 May 2021 at 23:11, Jack Frillman via gnucash-user
<gnucash-user at gnucash.org> wrote:
>
> I have repetitive task I do every day and I would like to reduce the
> number of steps to do that task.
>
> More specifically I import stock/mutual fund and precious metal prices
> from a CSV file. Currently I go through the multi-step process the GUI
> walks you through. This is easy enough but I can be ultra lazy at times
> and would like to execute that import process using a macro if possible.
>

Whilst I can't suggest a "macro" within Gnucash, I can suggest another
way to achieve the same thing, assumimg you are happy doing a bit
of shell scripting, plain text editing, and store your GnuCash files in
uncompressed XML format.

Cave! Make a backup: indeed, make lots of backups.

I don't have a stock price stanza to hand at time of writing, but to
flesh out the approach, here is a Currency Price stanza that tells
you the EUR/AUD conversion rate on a given date, as recorded
within GnuCash, where the Euro is the "commodity".

<gnc:pricedb version="1">
  <price>
    <price:id type="guid">158b0e4b8ded0dfc150c6ac15a27bcc8</price:id>
    <price:commodity>
      <cmdty:space>ISO4217</cmdty:space>
      <cmdty:id>EUR</cmdty:id>
    </price:commodity>
    <price:currency>
      <cmdty:space>ISO4217</cmdty:space>
      <cmdty:id>AUD</cmdty:id>
    </price:currency>
    <price:time>
      <ts:date>2021-07-01 18:59:00 +0800</ts:date>
    </price:time>
    <price:source>user:price</price:source>
    <price:type>transaction</price:type>
    <price:value>15837/10000</price:value>
  </price>
</gnc:pricedb>

If you look at the actual data within the <price></price> entry there,
you might start
to think that, were you to create another stanza, and alter the values
in just the

<price:id type="guid">
<ts:date>
<price:value>

fields, and add that in between the

<gnc:pricedb version="1">
</gnc:pricedb>

then, the next time you opened up your GnuCash file, GnuCash might be clever
enough to do the right thing and present you with the new data.

The good news is: GnuCash is more than clever enough.

It's a similar exercise for Stock commodiy price data, with, if I
recall correctly,
(been a while since I last did it) a couple of extra attributes that
are the "links"
to the stock definition, but, once you have identifed a Stock commodiy price
data stanza, in your current XML file, for the Stock you are
interested in, it will
be obvious which values you need to maintain for the new price data entry to be
associated with the correct Stock/Commidity, and which you need  to alter.

Alternatively, should you have any doubts about the above concept:

Take a copy of a current GnuCash file
Open the file up and import one new Stock/Commodity price.
Save the new file
Take a copy of  the new file
Compare the differences

What you need then, is a template for a <price> entry stanza into which you
substitute placeholders, for the date and the price, with the date and price,
data extracted from your CSV file.

The hardest bit is likely to be generating the GUID for each entry.

I have used that approach both for adding Stock/Commidity prices, and Invoices,
in the past.

I could even go so far as to suggest that you could have a template
for generating
some SQL "INSERT" commands that you squirt into the backend GnuCash Datsbase
via a monitor, but I can't claim to have ever done that, being nore
than happy with the
XML format.


Hoping that gives you enough to build on (as opposed to giving you
enough rope to
hang yoursefl with?) not least as few people on here are ever going to
suggest editing
the XML file as a solution, but you should know that you can, if you
are careful,
Kevin

PS
Did I mention you should make lots of backups?


More information about the gnucash-user mailing list