0

I was wondering if it was possible to format text when it is copied/selected so the content of a <table> could be formatted to fit a csv format i.e. separate every cell by a comma?

If so, how do you proceed to do that? I thought of using Regex, but my data can be very complex

Found nothing on the web on this. You are welcome to use jQuery and JavaScript`.

<table style="width:100%">
<tr>
 <td>Traverse Avant de Bas</td>
 <td>2016/08/18 à 08:19:05</td>
 <td>Omal</td>
 <td>1.01-BQ</td>
 <td>BOYLSTON 27D</td>
</tr>
<tr>
 <td>Traverse Avant de Bas</td>
 <td>2016/08/18 à 08:19:05</td>
 <td>Omal</td>
 <td>1.01-BQ</td>
 <td>BOYLSTON 27D</td>
</tr>
<tr>
 <td>Traverse Avant de Bas</td>
 <td>2016/08/18 à 08:19:05</td>
 <td>Omal</td>
 <td>1.01-BQ</td>
 <td>BOYLSTON 27D</td>
</tr>
<tr>
 <td>Traverse Avant de Bas</td>
 <td>2016/08/18 à 08:19:05</td>
 <td>Omal</td>
 <td>1.01-BQ</td>
 <td>BOYLSTON 27D</td>
</tr>
</table>

If you copy this table you end up with a lot of space between each data.

EDIT

Even though the link provided by mplungjan anwser my specific problem, i am still wondering how one could format a copied string.

Community
  • 1
  • 1
Chax
  • 1,041
  • 2
  • 14
  • 36
  • Use this to copy js http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript after date use each to format – Piotr Kazuś Aug 18 '16 at 13:55
  • If you select that table and paste it into Excel, it will make a spreadsheet for you. When I save it I get three rows of semicolon separated data - if your separator is comma, you will get a comma separated file – mplungjan Aug 18 '16 at 13:55
  • @mplungjan When i pasted my `` with actual data it didn't worked in Excel
    – Chax Aug 18 '16 at 13:59
  • 1
    http://stackoverflow.com/questions/5524143/how-can-i-export-tables-to-excel-from-a-webpage – mplungjan Aug 18 '16 at 14:03
  • @mplungjan this is perfect! I already use Datatable and i did'nt know about this option. You should add your comment as an answer so i can mark it as accepted – Chax Aug 18 '16 at 14:05
  • I did one better :) – mplungjan Aug 18 '16 at 14:06
  • @mplungjan While this address my problem, i'm curious on how i could format the copied string? Post edited – Chax Aug 18 '16 at 14:09
  • Perhaps on click or mousedown? Clone the table and format – mplungjan Aug 18 '16 at 14:18
  • @mplungjan I know it is possible to capture right-click with `mousedown`, but how do you send formatted information when the right menu option is copy. `ctrl-c` would be easier though. I found [this](http://stackoverflow.com/a/22581382/3229805) which explain how to create a `button` that copy content in the clipboard. Same logic for `ctrl-c` – Chax Aug 18 '16 at 16:04

1 Answers1

1

what about using css ? https://jsfiddle.net/maky/c3dhn5mj/1/

td:after {
content: ", ";
}
fernando
  • 814
  • 1
  • 9
  • 24
  • The idea is good, but when i make the comma transparent they disapear for good (cannot paste them back)... any idea on how to fix this. I cannot display comma on my webpage (boss doesn't want to) – Chax Aug 18 '16 at 14:02