emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Transforming table and then exporting as CSV
@ 2021-04-29 15:01 Pankaj Jangid
  2021-05-01 17:41 ` Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Pankaj Jangid @ 2021-04-29 15:01 UTC (permalink / raw)
  To: emacs-orgmode

I have the following requirement. For this I have written the code that
follows.

Bank requires from me a CSV file in this format:

--8<---------------cut here---------------start------------->8---
ACCOUNT NO,DR/CR,AMOUNT,NARRATION
50.......658,C,xxxxx.xx,APR2021
50.......590,C,xxxxx.xx,APR2021
50.......904,C,xxxxx.xx,APR2021
06.......965,C,xxxxx.xx,APR2021
50.......489,C,xxxxx.xx,APR2021
50.......680,C,xxxxx.xx,APR2021
50.......357,C,xxxxx.xx,APR2021
--8<---------------cut here---------------end--------------->8---

But this doesn’t have names of the people. First column shows the
account numbers. So I have created an org-table like this

--8<---------------cut here---------------start------------->8---
* April 2021
  :PROPERTIES:
  :TABLE_EXPORT_FILE: enet_apr_2021/0684SAL2904.001.csv
  :TABLE_EXPORT_FORMAT: orgtbl-to-csv
  :END:
  #+NAME: APR2021
  | NAME                  |     ACCOUNT NO | DR/CR |    AMOUNT | NARRATION |
  |-----------------------+----------------+-------+-----------+-----------|
  | Dh........ma...arwal  | 50.........658 | C     |  xxxxx.xx | APR2021   |
  | Pr........ar          | 50.........590 | C     |  xxxxx.xx | APR2021   |
  | Ol........y           | 50.........904 | C     |  xxxxx.xx | APR2021   |
  | Sa........gid         | 06.........965 | C     |  xxxxx.xx | APR2021   |
  | Pa........gid         | 50.........489 | C     |  xxxxx.xx | APR2021   |
  | Ma........ar          | 50.........680 | C     |  xxxxx.xx | APR2021   |
  | Sh........agi         | 50.........357 | C     |  xxxxx.xx | APR2021   |
  |-----------------------+----------------+-------+-----------+-----------|
  | TOTAL                 |                |       | xxxxxx.xx |           |
  #+TBLFM: $4=$4;%.2f
--8<---------------cut here---------------end--------------->8---

I have written below code for the conversion. This code removes the
first column and last row. And then run ‘org-table-export‘. This works
fine.

#+begin_src elisp
(require 'org)

(defun enet-export (name)
  (interactive "MTable Name: ")
  (outline-show-all)
  (let ((case-fold-search t))
    (goto-char (point-min))
    (if (search-forward-regexp (concat "#\\+NAME: +" name) nil t)
        (progn
          (forward-line)
          (let* ((beg (org-table-begin))
                 (end (org-table-end))
                 (txt (buffer-substring-no-properties beg end))
                 (file (org-entry-get (point) "TABLE_EXPORT_FILE"))
                 (format (org-entry-get (point) "TABLE_EXPORT_FORMAT")))
            (with-temp-buffer
              (insert txt)
              (goto-char (point-min))
              (org-table-next-field)
              (org-table-delete-column)
              (goto-char (org-table-end))
              (backward-char)
              (org-table-kill-row)
              (org-table-kill-row)
              (org-entry-put (point) "TABLE_EXPORT_FILE" file)
              (org-entry-put (point) "TABLE_EXPORT_FORMAT" format)
              (make-directory (file-name-directory file) t)
              (org-table-export))))
      (message "Not Found"))))
#+end_src

Same org file has multiple tables, so I am asking for “Table Name”. I
want to do two improvements:

1. When asking for table name, I want to offer all the table names in
   the file as completion options.

2. Current table at point as default

3. I want to run the function from command line. Like,

--8<---------------cut here---------------start------------->8---
   emacs --batch enet_2021_22.org -l enet.el --eval="(enet-export \"APR2021\")"
--8<---------------cut here---------------end--------------->8---

   Do I need to change the (interactive...) part in any way to enable
   this?

--
Regards
Pankaj



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

* Re: Transforming table and then exporting as CSV
  2021-04-29 15:01 Transforming table and then exporting as CSV Pankaj Jangid
@ 2021-05-01 17:41 ` Kyle Meyer
  2021-05-03 13:55   ` Pankaj Jangid
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2021-05-01 17:41 UTC (permalink / raw)
  To: Pankaj Jangid; +Cc: emacs-orgmode

Pankaj Jangid writes:
[...]
> Same org file has multiple tables, so I am asking for “Table Name”. I
> want to do two improvements:
>
> 1. When asking for table name, I want to offer all the table names in
>    the file as completion options.

You could collect names with something like

  (require 'subr-x)

  (let (names)
    (org-table-map-tables
     (lambda ()
       (when-let ((name (org-element-property :name (org-element-at-point))))
         (push name names)))
     'quiet)
    (nreverse names))

and then pass those as the collection to completing-read.

> 2. Current table at point as default

You could get that with

  (and (org-at-table-p)
       (save-excursion
         (goto-char (org-table-begin))
         (org-element-property :name (org-element-at-point))))

and pass that as the default for completing-read.

> 3. I want to run the function from command line. Like,
>
> --8<---------------cut here---------------start------------->8---
>    emacs --batch enet_2021_22.org -l enet.el --eval="(enet-export \"APR2021\")"
> --8<---------------cut here---------------end--------------->8---
>
>    Do I need to change the (interactive...) part in any way to enable
>    this?

I think that'd be the cleanest way, yes.  You could change

  (interactive "MTable Name: ")

into

  (interactive (list (completing-read ...)))


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

* Re: Transforming table and then exporting as CSV
  2021-05-01 17:41 ` Kyle Meyer
@ 2021-05-03 13:55   ` Pankaj Jangid
  0 siblings, 0 replies; 3+ messages in thread
From: Pankaj Jangid @ 2021-05-03 13:55 UTC (permalink / raw)
  To: emacs-orgmode

Kyle Meyer <kyle@kyleam.com> writes:

...
> I think that'd be the cleanest way, yes.  You could change
...

Thanks Kyle. Learned a bit more of Elisp.

Regards.. Pankaj



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

end of thread, other threads:[~2021-05-03 13:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-04-29 15:01 Transforming table and then exporting as CSV Pankaj Jangid
2021-05-01 17:41 ` Kyle Meyer
2021-05-03 13:55   ` Pankaj Jangid

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).