Assuming you meant to define header-args
in properties, as I suggested in a comment, you could
- name the code block with
#+name: my-name
e.g.
- then in order to use the value of the code block, you can execute the code block using
(org-sbe "my-name")
.
Something like this:
* Header level 1
:PROPERTIES:
:header-args: :var foo="somefile.csv"
:END:
** Header level 2
:PROPERTIES:
:header-args: :var foo="foobar"
:END:
#+name: my-name
#+begin_src emacs-lisp
foo
#+end_src
#+RESULTS: my-name
: foobar
| / | | | | |
| | Datetime | Price | Attr | Note |
|---+-------------------------+-------+--------+------|
| | 2021-01-13 13:45:46 UTC | 12.4 | foobar | |
|---+-------------------------+-------+--------+------|
#+TBLFM: @3$3=12.4
#+TBLFM: @3$4='(org-sbe "my-name")
Read the doc string of org-sbe
with C-h f org-sbe
. Note that it is the name of the code block that matters: the name of the variable foo
does not (it's a local variable of the code block, so it's not visible from the outside).
EDIT: Here's a slight modification to deal with multiple variables. The assumption is that the code block returns a list of the values of the variables which are defined in the header.
Then the table formula puts those values in successive rows in the third column:
* Header level 1
:PROPERTIES:
:header-args: :var foo="somefile.csv"
:END:
** Header level 2
:PROPERTIES:
:header-args: :var foo="foobar" :var bar="barfoo"
:END:
#+name: my-name
#+begin_src emacs-lisp :results drawer
(list foo bar)
#+end_src
#+RESULTS: my-name
:results:
(foobar barfoo)
:end:
| / | | | | |
| | Datetime | Price | Attr | Note |
|---+-------------------------+-------+--------+------|
| | 2021-01-13 13:45:46 UTC | 12.4 | foobar | |
| | 2022-01-13 13:45:46 UTC | 13.4 | barfoo | |
|---+-------------------------+-------+--------+------|
#+TBLFM: @3$3=12.4 :: @4$3=13.4
#+TBLFM: @3$4..@>$4='(my-func (- @# 3))
** code :noexport:
#+begin_src emacs-lisp
(defun my-func (n)
(let ((x (read (org-sbe "my-name"))))
(nth n x)))
#+end_src
The my-func
function basically does the "array" indexing: org-sbe
returns the list as a string (unfortunately: that's a quirk of org-babel), so we invoke the lisp reader (read
) to turn it back into a Lisp list and then we extract the n^th element of the list.
The function is called with the row number of the cell minus 3 to make it 0-based.
header-args
properties? AFAICT, the way you specify:header-args:
does not work at all.