emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Calling org babel to each row in a table
@ 2013-06-30 15:25 Joe Bogner
  2013-06-30 23:33 ` Eric Schulte
  0 siblings, 1 reply; 2+ messages in thread
From: Joe Bogner @ 2013-06-30 15:25 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1342 bytes --]

I often need to transform a table or list of values into a block of text.
For example, if I have a list of 4 files that I want to generate a SQL
script for.

After hunting around, this is the best I came up with:

#+name: table
| File |
| a    |
| b    |
| c    |
| d    |

#+name: template
#+begin_src org :results verbatim :var name="abc"
DROP TABLE $name

CREATE TABLE $name (name varchar(100))

BULK INSERT $name FROM '\\1.1.1.1\$name.txt' ...
#+end_src

#+name: apply-template
#+begin_src emacs-lisp :results silent :var table=()
(let (result-table)
    (dolist (line (cdr table))
        (setq result-table (cons (concat "#+call: template(\"" (car line)
"\") :results raw drawer") result-table)))
    (mapconcat 'identity (nreverse result-table) "\n"))
#+end_src

#+call: apply-template(table) :results org :exports both

#+RESULTS: apply-template(table):results org :exports both
#+BEGIN_SRC org
#+END_SRC



Is there a more straightforward method to apply this type of
transformation? Ideally I would skip the emacs-lisp block and use some
syntax to apply a org-babel block for each row in a table.


It sounded similar to this:
http://thread.gmane.org/gmane.emacs.orgmode/69326/focus=69340*, *but that
didn't have a full example for me to build off of

Is there a more straightforward to accomplish this transformation?

Thanks,
Joe

[-- Attachment #2: Type: text/html, Size: 2092 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Calling org babel to each row in a table
  2013-06-30 15:25 Calling org babel to each row in a table Joe Bogner
@ 2013-06-30 23:33 ` Eric Schulte
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Schulte @ 2013-06-30 23:33 UTC (permalink / raw)
  To: Joe Bogner; +Cc: emacs-orgmode

Joe Bogner <joebogner@gmail.com> writes:

> I often need to transform a table or list of values into a block of text.
> For example, if I have a list of 4 files that I want to generate a SQL
> script for.
>
> After hunting around, this is the best I came up with:
>
> #+name: table
> | File |
> | a    |
> | b    |
> | c    |
> | d    |
>
> #+name: template
> #+begin_src org :results verbatim :var name="abc"
> DROP TABLE $name
>
> CREATE TABLE $name (name varchar(100))
>
> BULK INSERT $name FROM '\\1.1.1.1\$name.txt' ...
> #+end_src
>
> #+name: apply-template
> #+begin_src emacs-lisp :results silent :var table=()
> (let (result-table)
>     (dolist (line (cdr table))
>         (setq result-table (cons (concat "#+call: template(\"" (car line)
> "\") :results raw drawer") result-table)))
>     (mapconcat 'identity (nreverse result-table) "\n"))
> #+end_src
>
> #+call: apply-template(table) :results org :exports both
>
> #+RESULTS: apply-template(table):results org :exports both
> #+BEGIN_SRC org
> #+END_SRC
>
>
>
> Is there a more straightforward method to apply this type of
> transformation? Ideally I would skip the emacs-lisp block and use some
> syntax to apply a org-babel block for each row in a table.
>
>
> It sounded similar to this:
> http://thread.gmane.org/gmane.emacs.orgmode/69326/focus=69340*, *but that
> didn't have a full example for me to build off of
>
> Is there a more straightforward to accomplish this transformation?
>

Hi Joe,

There is no way to map a code block over the rows of a table.  You could
use the `sbe' macro and a spreadsheet formula to call a code block on
multiple table cells, but the results would be inserted back into the
table.

| A       | -->A<--       |
| B       | -->B<--       |
| eric    | -->eric<--    |
| schulte | -->schulte<-- |
#+TBLFM: $2='(sbe foo (in $$1))

#+name: foo
#+begin_src emacs-lisp :var in="foo"
  (format "-->%s<--" in)
#+end_src

Possibly you could use the sbe macro to simplify the elisp code block in
your example.

Best,

>
> Thanks,
> Joe

-- 
Eric Schulte
http://cs.unm.edu/~eschulte

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-07-01  0:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-30 15:25 Calling org babel to each row in a table Joe Bogner
2013-06-30 23:33 ` Eric Schulte

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).