0

Evaluating expressions inside conditionals works in elisp

(defun usd(amount currency)
  (if (string= currency "USD")
      amount
    (/ amount 3.75)))

(usd 123 "USD") ; => 123
(usd 123 "SAR") ; => 32.8

But doesn't work inside an org table

| amount | currency |    USD |
|--------+----------+--------|
|    123 | USD      |    123 |
|     54 | USD      |     54 |
|    223 | SAR      | #ERROR |
|     54 | USD      |     54 |
|     52 | SAR      | #ERROR |
#+TBLFM: $3='(usd $1 $2)

Why would this error happen? I could solve this problem by having an intermediate column

| amount | currency | intermediate |   USD |
|--------+----------+--------------+-------|
|    123 | USD      |            1 |   123 |
|     54 | USD      |            1 |    54 |
|    223 | SAR      |      0.26667 | 59.47 |
|     54 | USD      |            1 |    54 |
|     52 | SAR      |      0.26667 | 13.87 |
#+TBLFM: $3='(if (string= $2 "USD") 1 .26667)::$4=round($1*$3,2)

But that makes the table unnecessarily bigger.

3
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Aug 14, 2023 at 14:53
  • [This is probably a duplicate] The cell entries are passed as strings even when they look like numbers. Try #+TBLFM: $3='(usd (string-to-number $1) $2). You can see what it did if you turn on formula debugging with C-c { (use the same key to toggle debugging off).
    – NickD
    Commented Aug 14, 2023 at 16:25
  • Thank you guys. I want to upvote you but I don't see the option.
    – azizan
    Commented Aug 15, 2023 at 7:26

0

Browse other questions tagged or ask your own question.