hledger icon indicating copy to clipboard operation
hledger copied to clipboard

CSV import rules, define commodity style

Open PSLLSP opened this issue 1 year ago • 1 comments

I would like to have an option to define commodity style in CSV import rules, to support keyword commodity.

Example:

$ cat demo-eur.csv
2023-01-23,test,1 234.56,EUR
$ cat demo-eur.csv.rules
# hledger import rules, test

date  %1
description DEMO|%2

amount %3 %4

account1 expenses:assorted
account2 assets:cash

# debug
comment \n%1,%2,%3,%4

When I create journal with hledger print then hledger uses amount format from CSV file and there is no way to overwrite commodity format in CSV import rules file.

$ hledger -f demo-eur.csv print
2023-01-23 DEMO|test
    ; 2023-01-23,test,1 234.56,EUR
    expenses:assorted     1 234.56 EUR
    assets:cash          -1 234.56 EUR

I can use command line option to define "commodity", that is a current solution:

$ hledger -f demo-eur.csv print -c "1.00 EUR"
2023-01-23 DEMO|test
    ; 2023-01-23,test,1 234.56,EUR
    expenses:assorted     1234.56 EUR
    assets:cash          -1234.56 EUR

or

$ hledger -f demo-eur.csv print -c "1,000.00 EUR"
2023-01-23 DEMO|test
    ; 2023-01-23,test,1 234.56,EUR
    expenses:assorted     1,234.56 EUR
    assets:cash          -1,234.56 EUR

Is it possible to add support for keyword commodity to CSV import file rules?

When CSV file has several currencies, it starts to be complicated, each currency has to be defined on command line. I do not store details here, just output (csv file and import rules evolved...). I prefer to define default commodity formats in CSV import rules...

$ hledger -f demo-eur.csv print
2023-01-24 DEMO|test2
    ; 2023-01-24,test2,2 345.56,USD,2 456.78,EUR
    expenses:assorted      2 345.56 USD @@ 2 456.78 EUR
    assets:cash          -2 345.56 USD @@ -2 456.78 EUR
    equity:conversion
$ hledger -f demo-eur.csv print -c "1.00 EUR" -c "1.00 USD"
2023-01-24 DEMO|test2
    ; 2023-01-24,test2,2 345.56,USD,2 456.78,EUR
    expenses:assorted      2345.56 USD @@ 2456.78 EUR
    assets:cash          -2345.56 USD @@ -2456.78 EUR
    equity:conversion

PSLLSP avatar Jan 30 '24 09:01 PSLLSP

Belated reply: my usual workaround when I need features not supported by a file format is to add a parent journal file that includes it; and add directives there. I do this a lot with timedot. Would that be enough, @PSLLSP ?

Unfortunately, it doesn't work for CSV; currently we don't support including those. (We should fix that.)

Adding a new CSV commodity rule that works just like journal's commodity directive seems.. possible, though it complicates the rules language further. I don't see any obvious problem with the idea, except possibly it will lead to requests for more features that if not satisfied would leave things feeling inconsistent.

simonmichael avatar Oct 02 '25 03:10 simonmichael