orgmode region to table

1.4k Views Asked by At

I have a region:

Items Quantity Amount
"Item A" 423 63
"Item B and C" 27 169
"Item D " 6 199

I would like to create org-table of the above region:

|Items          | Quantity| Amount|
----------------|------------------
|"Item A"       |  423    | 63    |
|"Item A and C" |  27     | 169   |
|"Item D"       |  6      | 199   |

The current suggestion is to select the region and use C-c | to create the table but this creates a table based on spaces between the elements. It is not able to group the items column. Is there a way I can group

2

There are 2 best solutions below

0
sanenr9 On

In these cases i normally create an emacs macro:

position the cursor on the second line (As your first line does not contain the quotes, it will not work there) of your table. Press f3 and then:

  • C-a
  • Then press the pipe: "|"
  • press "delete" to remove the first double quote
  • search for the next double quote C-s "
  • remove the double quote and add another pipe sign
  • move one word further with C-right
  • add another pipe
  • C-e
  • add another pipe
  • move one line down with the cursor
  • press f4 to close the marco

When you now press f4 it should execute the same again. If everything went fine, then you can execute the command easily hundreds of times through C- any number you want and "f4".

After that is done, you can simply also add pipes in the first line once. Now pressing "tab" inside the org-table will align everything.

Hope that helps.

0
dptd On

You can write your own Emacs Lisp function which will convert all the white-chars (not only spaces) between the two sexp and replace them with , in the currently active region.

(defun my/convert-region ()
  (interactive)
  (save-window-excursion
    (narrow-to-region (region-beginning) (region-end))
    (goto-char (point-min))
    (forward-sexp)
    (while (< (point) (buffer-end 1))
      (let ((beg (point)))
        (if (= (point) (line-end-position))
            (forward-char)
          (progn
            (forward-sexp)
            (backward-word)
            (delete-region beg (point)))
          (insert ",")))
      (forward-sexp))
    (goto-char (point-min))
    (widen)))

I checked this on such active region:

Items        Quantity   Amount
"Item A" 423 63
"Item B and C" 27 169
"Item D " 6 199

After running M-x my/convert-region it was changed to:

Items,Quantity,Amount
"Item A",423,63
"Item B and C",27,169
"Item D ",6,199

You can add this function to your config file and bind it to some keys.

If you want this function to convert this region to the org-table in the same function just add org-table-convert-region call.

(defun my/convert-region-to-org-table ()
  (interactive)
  (save-window-excursion
    (narrow-to-region (region-beginning) (region-end))
    (goto-char (point-min))
    (forward-sexp)
    (while (< (point) (buffer-end 1))
      (let ((beg (point)))
        (if (= (point) (line-end-position))
            (forward-char)
          (progn
            (forward-sexp)
            (backward-word)
            (delete-region beg (point)))
          (insert ",")))
      (forward-sexp))
    (org-table-convert-region (point-min) (point-max) '(4))
    (goto-char (point-min))
    (widen)))

For the same input:

Items        Quantity   Amount
"Item A" 423 63
"Item B and C" 27 169
"Item D " 6 199

I got the following result:

| Items        | Quantity | Amount |
| Item A       |      423 |     63 |
| Item B and C |       27 |    169 |
| Item D       |        6 |    199 |

Unfortunately the quotation marks are being removed by org-mode during the conversion.

(defun my/convert-region-to-org-table-with-header ()
  (interactive)
  (save-window-excursion
    (narrow-to-region (region-beginning) (region-end))
    (goto-char (point-min))
    (forward-sexp)
    (while (< (point) (buffer-end 1))
      (let ((beg (point)))
        (if (= (point) (line-end-position))
            (forward-char)
          (progn
            (forward-sexp)
            (backward-word)
            (delete-region beg (point)))
          (insert ",")))
      (forward-sexp))
    (org-table-convert-region (point-min) (point-max) '(4))
    (goto-char (point-min))
    (org-ctrl-c-minus)
    (widen)))

Then the output will look like this:

| Items        | Quantity | Amount |
|--------------+----------+--------|
| Item A       |      423 |     63 |
| Item B and C |       27 |    169 |
| Item D       |        6 |    199 |

I can explain how the code works if anyone is interested. This answer is already pretty long so I am not going to do that now when not sure if it will be useful for anyone.

Hope that helps.