0
| BookmarkLevel | BookmarkPageNumber | ActualPageNumber |
|---------------+--------------------+------------------|
|             1 |                 11 |                  |
|             2 |                 11 |                  |
|             2 |                 34 |                  |
|             1 |                 59 |                  |
|             2 |                 59 |                  |
|             3 |                    |                3 |
|             3 |                    |               10 |
|             2 |                    |               17 |
|             3 |                    |               17 |
|             3 |                    |               29 |
|             3 |                    |               35 |
|             3 |                    |               39 |
|             3 |                    |               43 |
|             4 |                    |               43 |
|             4 |                    |               45 |

#+tblfm: $2=$3+58

Where BookmarkPageNumber is empty, I would like to use ActualPageNumber (from the TOC) to calculate its values (by adding a constant to it). How should tblfm be written for the effect?

1 Answer 1

1

In this particular situation, you could use a range formula to fill in rows 7 through the last row:

#+TBLFM: @7$2..@>$2 = $3 + 58

Counting rows is probably not what you want to do though and is error-prone, so you could say

#+TBLFM: $2 = if ($2 > 0, $2, $3+58)

leaving the non-empty entries alone, and changing the empty entries (which in a numerical context evaluate to 0).

See Formula Syntax for Calc but be forewarned that the conversions that Org mode makes behind the scenes sometimes make it frustrating to figure out a formula that works. The formula debugger is your friend but is not a panacea.

Another possibility is using Lisp for the formula and handling the conversions yourself. E.g. here is a more robust check for empty vs 0 cells:

 #+TBLFM: $2 = '(if (not (equal $2 "")) $2 (+ (string-to-number $3) 58))

See Formula syntax for Lisp.

2
  • In case we are dealing with strings or integers that use 0 as a null value, can $2 > 0 be rewritten as (not $2 nil) or something?
    – Sati
    Oct 9, 2020 at 14:31
  • Edited to add some additional info to help with this.
    – NickD
    Oct 9, 2020 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.