2

I want to insert a new row at line 2 in a table (preferably using TBLFM), and I want it to be prefilled with the current date (col 1) and time (col 2).

How do I insert a new row (something like #+TBLFM: @2..@-1=@#-1?) ?

Here is my current table:

| date             | start |   end | lunch |    total | expected |    + time |
|------------------+-------+-------+-------+----------+----------+-----------|
| [2016-08-02 Tue] | 08:58 | 17:00 |  0:15 | 07:47:00 | 07:30:00 |  00:17:00 |
| [2016-08-01 Mon] | 08:37 | 16:12 |  0:15 | 07:20:00 | 07:30:00 | -00:10:00 |
|------------------+-------+-------+-------+----------+----------+-----------|
|                  |       |       |       |          |          |  00:07:00 |
#+TBLFM: @2$5..@-1$5=$3-$2-$4;T::@2$7..@-1$7=$5-$6;T::@>$7=vsum(@2$7..@-1$7);T

2 Answers 2

1

The following line inserts a new row, but with the content of cell 1 "#ERROR".

#+TBLFM: @2$1='(let ((org-table-fix-formulas-confirm 0)) (org-table-insert-row))

The part with setting org-table-fix-formulas-confirm to 0 is so that it doesn't update all the indices in the formulae.

So using two formulae, the following works:

| date             | start |   end |
|------------------+-------+-------|
| [2016-08-01 Mon] | 08:00 | 16:00 |
|------------------+-------+-------|
#+TBLFM: @2$1='(let ((org-table-fix-formulas-confirm 0)) (org-table-insert-row))
#+TBLFM: @2$1='(format-time-string "[%Y-%m-%d %a]")::@2$2='(format-time-string "%H:%M")
#+TBLFM: @2$3='(format-time-string "%H:%M")
0

Note: My answer is derived directly from Pål GD's great answer. I only figured out how to prevent the #ERROR message.

Call the elisp functions inside let declaration section and return the date string.

Like this

(let ((org-table-fix-formulas-confirm 0) (x (format-time-string "[%Y-%m-%d %a]")) (y (org-table-insert-row))) x)

Below is complete example for your answer code

| date             | start |   end |
|------------------+-------+-------|
| [2020-08-12 Wed] | 09:10 | 09:10 |
| [2016-08-01 Mon] | 08:00 | 16:00 |
|------------------+-------+-------|
#+TBLFM: @3$1='(let ((org-table-fix-formulas-confirm 0) (x (format-time-string "[%Y-%m-%d %a]")) (y (org-table-insert-row))) x)::@3$2='(format-time-string "%H:%M")::@3$3='(format-time-string "%H:%M")

Thank you for asking this question!


This answer was tested using:


emacs version: GNU Emacs 25.2.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.10)
org-mode version: Org mode version 9.1.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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