Search code examples
emacsorg-modeorg-table

emacs org mode: (conditional) if cell is empty then


How do you express the following conditional for an org mode table?

for every cell between the 2nd and 3rd hline: 
  if the cell is empty, 
    set the contents of the cell to todays date.
  else
    leave the cells contents as they are.

so, given the following table, i would like to insert todays date in the empty cell.

|------------------|
| date             |
|------------------|
| [2014-05-23 Fri] |
| [2014-05-24 Sat] |
|                  |
|------------------|

Solution

  • The custom calc function appendToday will do what you want. It handles the case where all fields between the hlines are empty as well.

    (defmath appendToday (idx v)
      (let ((d (date (month (now)) (day (now))))
            (len (vlen v)))
        (if (<= idx len)
          (if (equal (cadr v) 0)
            d
            (nth idx v)
          )
          d
        )
      )
    )
    

    The table before evaluation looks like:

    |------------------|
    | <2014-05-26 Mon> |
    |                  |
    |                  |
    |                  |
    |                  |
    |                  |
    |------------------|
    #+TBLFM: @I..@II$1=appendToday(@#,@I..@II$1)
    

    The table after evaluation looks like:

    |------------------|
    | <2014-05-26 Mon> |
    | <2014-05-28 Wed> |
    | <2014-05-28 Wed> |
    | <2014-05-28 Wed> |
    | <2014-05-28 Wed> |
    | <2014-05-28 Wed> |
    |------------------|
    #+TBLFM: @I..@II$1=appendToday(@#,@I..@II$1)