3

I'm trying to figure out how to reference a cell from a table that is located in a different file. I know I can use remote(tablename, @1$1) to reference a remote table within the same file. Is there a similar way to reference a remote table from a different file?

Marius Pop
  • 133
  • 5

1 Answers1

3

It's a bit tricky and a patch towards accepting the "normal" file::name syntax would definitely be appreciated!

Anyway, to refer to a remote file you must use the ID cf. org-table-get-remote-range. I don't know if it was always the case, but IDs seem to be linked to headings now.

Example with org-id

First: (require 'org-id)

Create two files in /tmp,

rasmus:[tmp] $ cat a.org
* foo
#+name: tab-1
| a | 1 |
| b | 2 |
| c | 3 |

rasmus:[tmp] $ cat b.org
| x | 4 |  |
| y | 5 |  |
| z | 6 |  |

In a.org place the cursor somewhere under * foo and do M-x org-id-get-create. This will add an ID property to * foo and store the location in ~/.emacs.d/.org-id-locations or similar. In my case the ID is 58efda96-fc03-4c25-91e3-30c987959618

Now in b.org you can use the ID as the first argument to your REMOTE call. E.g. to copy the second column in the table in a.org to the third column in the table in b.org you could use this formula:

#+TBLFM: $3=remote(58efda96-fc03-4c25-91e3-30c987959618, @@#$2)

So my files now look like this:

rasmus:[tmp] $ cat a.org
* foo
:PROPERTIES:
:ID:       58efda96-fc03-4c25-91e3-30c987959618
:END:
#+name: tab-1
| a | 1 |
| b | 2 |
| c | 3 |

rasmus:[tmp] $ cat b.org
| x | 4 | 1 |
| y | 5 | 2 |
| z | 6 | 3 |
#+TBLFM: $3=remote(58efda96-fc03-4c25-91e3-30c987959618, @@#$2)

Caveats

AFAIR you could once assign an #+ID: to a table, but this is gone. Thus, you might only be able to refer to the first table in a second. I didn't test this. I think remote referencing is nice, but it would be great if someone would add standard org-link-search support.

Only at export time

Alternatively, if you only need to update the table in c.org at export time you could #+include the table in an noexport heading and use the standard #+name reference. You'd need to update tables at export time. There's probably a more elegant way than the one proposed in c.org below

rasmus:[tmp] $ cat c.org 
| x | 4 |   |
| y | 5 |   |
| z | 6 |   |
#+TBLFM: $3=remote(tab-1, @@#$2)

* misc                                                             :noexport:
#+include: "a.org::tab-1"
#+begin_src emacs-lisp
  (add-hook 'org-export-before-parsing-hook
            (defun rasmus/ox-update-tbl (backend)
              (when (re-search-forward org-table-line-regexp nil t)
                (org-table-recalculate t))))
#+end_src
rasmus
  • 2,682
  • 13
  • 20