all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [feature-request] make org-quote-csv-field customizable
@ 2017-08-02 10:26 Thomas von Dein
  2017-08-03 22:13 ` Nicolas Goaziou
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas von Dein @ 2017-08-02 10:26 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

currently when exporting a table to CSV, fields are quoted automatically
if they contain a comma or a quote character. The regexp for this
determination is hard-coded in 'org-quote-csv-field.

This is good for most use cases. However, if you want to import such a
CSV with MS Excel then some unquoted fields will be automatically
converted by Excel into another format.

An example is a field containing 192.168.10.233. 'org-quote-csv-field
doesn't quote it, but Excel - for reasons I cannot fathom - considers
this a number (it doesn't do this when the last octet is below 233
though) and converts it to 19216810233 - which is wrong.

I wrote the following code to circumvent this Excel bug:

(defun tvd-org-quote-csv-field (s)
  "Quote every field and precede it with = to disable excel automatisms."
  (if (string-match "." s)
      (concat "=\"" (mapconcat 'identity
                               (split-string s "\"") "\"\"") "\"")
    s))

(defun table-to-excel ()
  "export current org table to CSV format suitable for MS Excel."
  (interactive)
  ;; quote everything, map temporarily 'org-quote-csv-field
  ;; to my version
  (cl-letf (((symbol-function 'org-quote-csv-field)
             #'tvd-org-quote-csv-field))
           (org-table-export "/tmp/ex.csv" "orgtbl-to-csv")))

To make this customizable, it would be good to have a variable which
contains the regex to determine what to quote. A parameter to
org-table-export would be an even better solution.


best regards, and thanks for this great module!
Tom

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

* Re: [feature-request] make org-quote-csv-field customizable
  2017-08-02 10:26 [feature-request] make org-quote-csv-field customizable Thomas von Dein
@ 2017-08-03 22:13 ` Nicolas Goaziou
  2017-08-03 23:19   ` Tim Cross
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas Goaziou @ 2017-08-03 22:13 UTC (permalink / raw)
  To: Thomas von Dein; +Cc: emacs-orgmode

Hello,

Thomas von Dein <tom@vondein.org> writes:

> currently when exporting a table to CSV, fields are quoted automatically
> if they contain a comma or a quote character. The regexp for this
> determination is hard-coded in 'org-quote-csv-field.
>
> This is good for most use cases. However, if you want to import such a
> CSV with MS Excel then some unquoted fields will be automatically
> converted by Excel into another format.
>
> An example is a field containing 192.168.10.233. 'org-quote-csv-field
> doesn't quote it, but Excel - for reasons I cannot fathom - considers
> this a number (it doesn't do this when the last octet is below 233
> though) and converts it to 19216810233 - which is wrong.
>
> I wrote the following code to circumvent this Excel bug:
>
> (defun tvd-org-quote-csv-field (s)
>   "Quote every field and precede it with = to disable excel automatisms."
>   (if (string-match "." s)
>       (concat "=\"" (mapconcat 'identity
>                                (split-string s "\"") "\"\"") "\"")
>     s))
>
> (defun table-to-excel ()
>   "export current org table to CSV format suitable for MS Excel."
>   (interactive)
>   ;; quote everything, map temporarily 'org-quote-csv-field
>   ;; to my version
>   (cl-letf (((symbol-function 'org-quote-csv-field)
>              #'tvd-org-quote-csv-field))
>            (org-table-export "/tmp/ex.csv" "orgtbl-to-csv")))
>
> To make this customizable, it would be good to have a variable which
> contains the regex to determine what to quote. A parameter to
> org-table-export would be an even better solution.

It is already possible. When asked for a format for conversion, you can
write something like:

  orgtbl-to-csv :fmt tvd-org-quote-csv-field


You can also define your own format:

(defun my-orgtbl-to-Excel (table params)
  "Convert the orgtbl-mode table to Excel.
This does take care of the proper quoting of fields with comma or quotes."
  (orgtbl-to-generic table
		     (org-combine-plists '(:sep "," :fmt tvd-org-quote-csv-field)
					 params)))

and use it when prompted for a format in `org-table-export'.

Regards,

-- 
Nicolas Goaziou

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

* Re: [feature-request] make org-quote-csv-field customizable
  2017-08-03 22:13 ` Nicolas Goaziou
@ 2017-08-03 23:19   ` Tim Cross
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Cross @ 2017-08-03 23:19 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode, Thomas von Dein


and from memory, you can also tell excel what the data type of each
column in a csv file is.

Nicolas Goaziou writes:

> Hello,
>
> Thomas von Dein <tom@vondein.org> writes:
>
>> currently when exporting a table to CSV, fields are quoted automatically
>> if they contain a comma or a quote character. The regexp for this
>> determination is hard-coded in 'org-quote-csv-field.
>>
>> This is good for most use cases. However, if you want to import such a
>> CSV with MS Excel then some unquoted fields will be automatically
>> converted by Excel into another format.
>>
>> An example is a field containing 192.168.10.233. 'org-quote-csv-field
>> doesn't quote it, but Excel - for reasons I cannot fathom - considers
>> this a number (it doesn't do this when the last octet is below 233
>> though) and converts it to 19216810233 - which is wrong.
>>
>> I wrote the following code to circumvent this Excel bug:
>>
>> (defun tvd-org-quote-csv-field (s)
>>   "Quote every field and precede it with = to disable excel automatisms."
>>   (if (string-match "." s)
>>       (concat "=\"" (mapconcat 'identity
>>                                (split-string s "\"") "\"\"") "\"")
>>     s))
>>
>> (defun table-to-excel ()
>>   "export current org table to CSV format suitable for MS Excel."
>>   (interactive)
>>   ;; quote everything, map temporarily 'org-quote-csv-field
>>   ;; to my version
>>   (cl-letf (((symbol-function 'org-quote-csv-field)
>>              #'tvd-org-quote-csv-field))
>>            (org-table-export "/tmp/ex.csv" "orgtbl-to-csv")))
>>
>> To make this customizable, it would be good to have a variable which
>> contains the regex to determine what to quote. A parameter to
>> org-table-export would be an even better solution.
>
> It is already possible. When asked for a format for conversion, you can
> write something like:
>
>   orgtbl-to-csv :fmt tvd-org-quote-csv-field
>
>
> You can also define your own format:
>
> (defun my-orgtbl-to-Excel (table params)
>   "Convert the orgtbl-mode table to Excel.
> This does take care of the proper quoting of fields with comma or quotes."
>   (orgtbl-to-generic table
> 		     (org-combine-plists '(:sep "," :fmt tvd-org-quote-csv-field)
> 					 params)))
>
> and use it when prompted for a format in `org-table-export'.
>
> Regards,


-- 
Tim Cross

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

end of thread, other threads:[~2017-08-03 23:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-08-02 10:26 [feature-request] make org-quote-csv-field customizable Thomas von Dein
2017-08-03 22:13 ` Nicolas Goaziou
2017-08-03 23:19   ` Tim Cross

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.