table2csv icon indicating copy to clipboard operation
table2csv copied to clipboard

Number of columns based on each row's number of th td

Open Meow-ops opened this issue 6 years ago • 1 comments

For the code to work in my page I had to do the following modification

  function convert(table) {
    var output = "";
    var rows = table.find("tr").not(options.excludeRows);
    //var numCols = rows.first().find("td,th").filter(":visible").not(options.excludeColumns).length;
    
    rows.each(function (ignore, elem) {
      var numCols = $(elem).find("td,th").length;
      $(elem).find("td,th").filter(":visible").not(options.excludeColumns).each(function (i, col) {
        var column = $(col); // Strip whitespaces

        var content = options.trimContent ? $.trim(column.text()) : column.text();
        output += options.quoteFields ? quote(content) : content;

        if (i !== numCols - 1) {
          output += options.separator;
        } else {
          output += options.newline;
        }
      });
    });
    return output;
  }

If you think that is useful you could modify your code.

Meow-ops avatar Apr 24 '19 15:04 Meow-ops

This shows an interesting shortcoming of the plugin, but I'm not so sure it will be easy to integrate.

From what I understand, in your case you had a table with a different number of columns on each row, therefore you needed to compute the numCols value on each of them to correctly place the separator and newline characters.

Now, this would mean that you probably don't have any header on your table, and so don't want/expect them in the csv. The problem with this situation is: how do you identify which data is on each column? In the csv the problem is even worse: if every row could have a different amount of values, how do you know which data is held by each "comma-separated section"?

Without a concrete example I'm guessing, but I imagine that you were processing some sort of automatically generated html table, possibly a table used to build also the layout of the data on the page (which is super bad practice, but many automated tools still build or export html pages that way...)

If the intent when using this plugin is to extract some table data from html to be processed with some other automated tool, a certain regularity in the extracted data is essential. To the very least, each row should have the same number of "commas", leaving some fields empty if there is no corresponding cell in the html row. But this would work only in the case all the "extra" cells are at the end of each row, otherwise the data would end up in the wrong csv "column". Imagine this case:

<table>
  <tr>
    <td>A</td>
    <td>B</td>
    <td>C</td>
  </tr>
  <tr>
    <td>a</td>
    <td>c</td>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
  </tr>
</table>

The "logical" (i.e. expected) mapping should be:

A,B,C
a,,c
1,2,3

but the plugin would yield instead:

A,B,C
a,c,
1,2,3

And I don't see any way it could know how to build the expected result instead.

So you see, it's not that trivial for the generic case...

OmbraDiFenice avatar Oct 20 '19 08:10 OmbraDiFenice