[emacs-berlin] Calculating recurring payments with Orgmode table spreadsheet
Tilmann Singer
tils at tils.net
Mon Sep 30 15:48:37 UTC 2024
jman <jman at city17.xyz> writes:
>> it's possible to interpret $3 as boolean directly:
>>
>> #+TBLFM: $4='(if (not $3) $2 (* $2 (my/months-paid-until-end-of-year "$1")));EL
>
> I've tried this but I couldn't make it work.
Oops, turns out I've only tried it with the condition inverted - (if $3
...) instead of (if (not $3) ...):
#+TBLFM: $4='(if $3 (* $2 (my/months-paid-until-end-of-year "$1")) $2);L
it works, but for a totally different reason than I thought. Consider
this example formula which fills the second column depending on the
truthiness of the first column:
| t | is truthy |
| | is falsy |
#+TBLFM: $2='(if $1 "is truthy" "is falsy");L
I naively assumed that the value of $1 would somehow be "passed" as
parameter into the elisp code given for $2, and an empty cell value
would be passed as nil. However the formula debugger shows that instead
it interpolates the string content of the cell and then evaluates the
resulting code – for the first row this means:
@r$c-> '(if $1 "is truthy" "is falsy")
$1-> '(if t "is truthy" "is falsy")
Result: is truthy
which somehow makes sense, but for the second row it's this:
@r$c-> '(if $1 "is truthy" "is falsy")
$1-> '(if "is truthy" "is falsy")
Result: is falsy
so the condition in (if "is truthy" "is falsy") is actually the string
"is truthy", which is indeed truthy (as every other string), and
therefore the if expression returns "is falsy". A weird twist which
accidentally made one variant work for me but not a strategy to write
understandable code :)
Knowing this now, I can imagine that putting interpolateable snippets
into an org table can be helpful, e.g. t and nil into the "is-recurrent"
column.
Even something like this works:
| (+ 1 1) | 3 |
#+TBLFM: $2='(+ 1 $1);L
Thanks for introducing me to the formula debugger, I didn't know it
existed!
Note aside – I didn't notice any difference when adding the E flag in
any of the above examples.
cheers, Til
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 853 bytes
Desc: not available
URL: <http://mailb.org/pipermail/emacs-berlin/attachments/20240930/559dbca8/attachment.sig>
More information about the emacs-berlin
mailing list