[emacs-berlin] Calculating recurring payments with Orgmode table spreadsheet
Tilmann Singer
tils at tils.net
Sat Sep 28 17:06:07 UTC 2024
Hi,
Thanks for posting this entertaining org-mode exercise! Congrats to your
solution – I like the way how it combines an elisp function for the
general logic with an org table formula. Trying to cram everything into
TBLFM directly becomes unwieldy quickly.
From your solution I learnt about the E and L flags. Which made me
wonder whether values other than numbers are passed literally as well?
Ha, apparently for the boolean in the "is-recurrent" column this is the
case, so instead of
> #+TBLFM: $4='(if (string-empty-p "$3") $2 (* $2 (my/months-paid-until-end-of-year "$1")));EL
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
It would be nice if the value in "Payment Date" was passed as org
timestamp as well instead of a string, but I guess that's too much to
ask. I tried, it isn't the case.
Regarding interpreting the timestamp string, I wondered if there is a
more org specific way of doing this and found org-timestamp-from-string.
It returns an org timestamp:
(org-timestamp-from-string "<2024-03-01 Fri>")
=> (timestamp (:type active :raw-value "<2024-03-01 Fri>" :year-start
2024 :month-start 3 :day-start 1 :hour-start nil :minute-start nil
:year-end 2024 :month-end 3 :day-end 1 :hour-end nil :minute-end nil
:begin 1 :end 17 :post-blank 0))
from which the month could be extracted like this:
(plist-get (cadr (org-timestamp-from-string "<2024-03-01 Fri>"))
:month-start)
=> 3
so the function can be changed to:
(defun my/months-paid-until-end-of-year (timestamp)
"Return the number of months from TIMESTAMP until end of the year.
As a number without zero-padding."
(let* ((time (org-timestamp-from-string timestamp))
(month (plist-get (cadr time) :month-start)))
(+ 1 (- 12 month))))
it's more specific than dealing with strings. Maybe a small improvement
over the already great solution.
And a note aside: are you using active timestamps on purpose, as in
"<2024-03-01>" instead of "[2024-03-01]"? At least in my setup, active
timestamps end up in the agenda, which is usually not what I want.
greetings, 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/20240928/47491881/attachment.sig>
More information about the emacs-berlin
mailing list