Search code examples
javascriptjquerytablesorter

How to achieve line breaks in a single cell in the csv export?


I am using the Output widget for the jQuery plugin Tablesorter.

I am trying to preserve line breaks when I first export to a csv file and then import the file to a spreadsheet tool like Excel or Google spreadsheets.

Getting nice looking line breaks in the html table is easy. Just add some <br> tags.

    <tr>
        <td>value 1<br>value 2<br>value 3</td>
        <td>10</td>
        <td>Koala</td>
        <td>http://www.google.com</td>
    </tr>

Js fiddle: http://jsfiddle.net/yefw6fh6/

When I export the table in the fiddle I get this.

"AlphaNumeric","Numeric","Animals","Sites"
"value 1\nvalue 2\nvalue 3","10","Koala","http://www.google.com"
"value A\nvalue B\nvalue C","234","Ox","http://www.yahoo.com"

When the csv file is imported to a Google spreadsheet this is the result.

See here

The line breaks are replaced by \n. I would like to achieve the following result when the csv file is imported.

See here

All suggestions on how to accomplish this are most welcome.

Thanks!

Update: Working solution

I missed the setting for carriage return. Had to add this setting:

$.tablesorter.output.replaceCR = '\r';

Working fiddle


Solution

  • If you look at the output widget demo page at the bottom of the "Options" section you'll see this note:

    If you need to change the carriage return and/or the tab replacement strings, modify them as follows:

    $.tablesorter.output.replaceCR = '\\n';
    $.tablesorter.output.replaceTab = '\\t';
    

    I'm not sure what would work for you in excel or Google sheets, but you can change those setting to see if anything else works.