Skip to content

Instantly share code, notes, and snippets.

@developernotes
Created July 12, 2013 19:19
Show Gist options
  • Save developernotes/5987011 to your computer and use it in GitHub Desktop.
Save developernotes/5987011 to your computer and use it in GitHub Desktop.
(defun string-replace (old-value new-value source)
"replace old-value with new-value from source"
(with-temp-buffer
(insert source)
(goto-char (point-min))
(while (search-forward old-value nil t)
(replace-match new-value nil t))
(buffer-substring (point-min) (point-max))))
(defun org-export-to-csv (file)
(interactive "F Org-mode file to convert to CSV: ")
(find-file file)
(let* ((value-tree (org-element-parse-buffer))
(field-tree (org-element-parse-buffer 'headline))
(values (org-element-map value-tree 'paragraph
(lambda (elm)
(string-replace "\n" "" (org-element-interpret-data elm)))))
(fields (delete-dups (org-element-map field-tree 'headline
(lambda (elm)
(when (not (= (org-element-property :level elm) 1))
(org-element-property :title elm))))))
(count 0))
(message (mapconcat (lambda (e) e) fields ","))
(message (mapconcat (lambda (e)
(if (= count (- (length fields) 1))
(progn
(setq count 0)
(format "%s" e))
(progn
(let ((display-format
(if (= count 0)
"\n%s"
"%s")))
(setq count (+ 1 count))
(format display-format e))))) values ","))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment