hhurz / tableExport.jquery.plugin

jQuery plugin to export a html table to JSON, XML, CSV, TSV, TXT, SQL, Word, Excel, PNG and PDF
MIT License
986 stars 716 forks source link

Multiple lines #124

Open kamparR opened 7 years ago

kamparR commented 7 years ago

In function parseString there is: `var text = htmlData.replace(/\n/g,'\u2028').replace(/<br\s*[\/]?>/gi, '\u2060'); var obj = $('

').html(text).contents(); text = '';

        $.each(obj.text().split("\u2028"), function(i, v) {
          if (i > 0)
            text += " ";
          text += $.trim(v);
        });

        $.each(text.split("\u2060"), function(i, v) {
          if (i > 0)
            result += "\n";
          result += $.trim(v).replace(/\u00AD/g, ""); // remove soft hyphens
        });`

Isn't that error? There shouldn't be: text += "\n" ? Because for now you can't export text with multiple lines (separated by character: \n) for example to csv. Current code replace \n with \u2028, split text by this and join with space instead of new line character

hhurz commented 7 years ago

I don't think that this is an error. Let me show why by an example: The following html code

    <table>
      <thead>
      <tr>
        <th>column 1</th>
        <th>column 2</th>
      </tr>
      </thead>
      <tbody>
      <tr>
        <td>A</td>
        <td>
          Line 1<br>
          Line 2<br>
          Line 3
        </td>
      </tr>
      <tr>
        <td>B</td>
        <td>
          Line 1
          Line 2
          Line 3
        </td>
      </tr>
      <tr>
        <td>C</td>
        <td> </td>
      </tr>
      </tbody>
    </table>

will be rendered by the browser to this: issue124

As you can see multiple lines, separated by a newline character, will be concatenated into one line. Where as multiple lines, separated by a <br> tag will be wraped. This behavior should also be achieved by the parseString function. Thus when you export the example to excel you will get this:

issue124_2

I hope this answers your question. Thanks.

aronmgv commented 6 years ago

Hey @hhurz ,

I am trying to accomplish this multiple lines cells export representation.. But it seems my <br> tags are not converted to the new lines..

HTML:

<td>
    AAA<br>
    BBB<br>
    CCC
</td>

This is exported into CSV as "AAABBBCCC"....

CSV Export settings:

elm.tableExport({
    type: 'csv',
    escape: false,
    ignoreColumn: colIgnore,
    maxNestedTables: 2,
    preserve : {
        leadingWS: true,
        trailingWS: true
    },
    csvUseBOM: (d?d.csvUseBOM:false),
    fileName: (s?s:'TableExport') + formattedDate()
});

I would either try to accomplish this: image

Or write a function to convert <br> into ", ".. So instead of "AAABBBCCC" I would have in cell "AAA, BBB, CCC". That could be a new feature where you would define a delimiter in case multiline cells..

Thanks again, Michal

hhurz commented 6 years ago

Hi, thanks for reporting this issue. It's a regression bug and will be fixed in next release. Normal behavior should be that new lines will be turned into spaces and <b> tags into new lines, as I described it above in 2016

aronmgv commented 6 years ago

How would new lines work for csv? Or for json/xml?

hhurz commented 6 years ago

Working fine. For my understanding new lines do not violate the specs of those formats. When opening such a csv file in Excel you get the result as you would acomplish it (see above). The json result was accepted by online validators and could successfully open it in my json viewer. Same for xml.

aronmgv commented 6 years ago

Thx again, waiting for the update :)

aronmgv commented 6 years ago

Updated to 1.9.15 and bug still present.. Both scenarios (new lines or <br>) result into merged oneliner..

hhurz commented 6 years ago

Working for me 🤔 Here you see the result when exporting table "issue124" from the testfile as csv:

issue124

aronmgv commented 6 years ago

Not for me:

image

image

image

image

image

hhurz commented 6 years ago

Ok, got it: The detection of br tags containing attributes doesn't work. That needs to be fixed... ...Done. See v1.9.16, thanks

aronmgv commented 6 years ago

Hey, tested and wokrs!

However I came to point out configuration where I have <div></div> within cell to devide content to the new lines.. This does not work and is exported as single continuous line :/

image

I would like to clarify here what is the recommended way to split content into multiple lines within cell not using angular and your library will respect it?

Other question here what about the proposed attribute for separator in case of new lines? So by default it would be \n, and it can be configured to any valid string ,, or |, or :.. etc - this would be nice feature to have :)

hhurz commented 6 years ago

I think you could solve this by using one of the provided callback functions onCellHtmlData or onCellData. Both are called during parsing of every cell. onCellHtmlData is called at the beginning and onCellData at the end of the parse process. Look in the clossed issues (e.g #44) for onCellHtmlData to get more information.

Regarding the proposed attribute: I put this on to my feature request list...

jwiel86 commented 6 years ago

Hi! I'm trying to display multiple lines with an xlsx format. Any suggestions?

hhurz commented 5 years ago

@jwiel86 What have you tried until now?

smrrrazu commented 5 years ago

In my case <br/> works. But <br> does not.