Domanda

I have a list of appointments in the style of:

| Appointment                  | Dur.  |
|------------------------------+-------|
| <2014-02-20 Thu 09:30-18:30> |  ???  |
| <2014-02-22 Sat 09:00-10:00> |  ???  |
|                              |       |

How can I make orgmode calculate the durations of my appointments?

È stato utile?

Soluzione

Org mode allows you to specify formulas for calculating values for a given column. You are allowed to execute elisp code in those formulas. We can use feature to achieve what you want first, we will need to define some functions which find difference between times. You can add the following to your org document

#+begin_src emacs-lisp
  (defun my-get-number-of-minutes (time)
    (+ (* (nth 2 time) 60) (nth 1 time)))

  (defun my-get-time-diff (t1 t2)
    (- (my-get-number-of-minutes t1) (my-get-number-of-minutes t2)))

  (defun my-get-duration (time-string)
    (let* ((times (split-string (substring time-string 16 -1) "-"))
           (minute-diff (my-get-time-diff (parse-time-string (nth 1 times))
                                          (parse-time-string (nth 0 times)))))
      (format "%dhrs %dmins" (/ minute-diff 60) (% minute-diff 60))))
#+end_src

Then do C-cC-c anywhere between the #+begin-src emacs-lisp and #+end-src tags, this will evaluate the elisp.

OR

Simply copy the elisp code (between #+begin_src emacs-lisp #+end-src) to *scratch* buffer and do M-xeval-bufferRET.

Now that we have the functions defined we can tell org-mode the formula to calculate the second column from first column, to do this simply paste the following line below the org-table

#+TBLFM: $2='(my-get-duration $1)

This tells org that value of column 2 ($2) is the function my-get-duration applied 'value of column 1' ($2). Then with point on the line do C-cC-c, if you have done everything well, the second column should be filled with the duration.

NOTE: The code assumes that the datetime will be of the same format as given in your example.

I recommend you read this short article about org-mode's capabilities as a spreadsheet.

UPDATE

If you can reformat your table as follows, you will be able to simplify the formula

| Appointment      | Start |   End | Duration |
|------------------+-------+-------+----------|
| <2014-02-20 Thu> | 09:30 | 18:30 | ???      |
| <2014-02-22 Sat> | 09:00 | 10:00 | ???      |

The formula for calculating the duration now would simply be

#+TBLFM: $4=$3-$2;T
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top