2

I have an expandable table in html:

enter image description here

How can I convert it into excel file with grouping, something like this:

enter image description here

Nessi
  • 276
  • 1
  • 4
  • 23

1 Answers1

1

Im not to sure about the grouping in XLS but the main way we convert data into XL is to use CSV

You can parse the data from your HTML into an array then export that array to CSV

Here is the code to make your html table downloadable to CSV

var data = [
   ['Foo', 'programmer'],
   ['Bar', 'bus driver'],
   ['Moo', 'Reindeer Hunter']
];
 
 
function download_csv() {
    var csv = 'Name,Title\n';
    data.forEach(function(row) {
            csv += row.join(',');
            csv += "\n";
    });
 
    console.log(csv);
    var hiddenElement = document.createElement('a');
    hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
    hiddenElement.target = '_blank';
    hiddenElement.download = 'people.csv';
    hiddenElement.click();
}

 
<button onclick="download_csv()">Download CSV</button> 
JonoJames
  • 1,117
  • 8
  • 22
  • 1
    thanks for the code, but the depth will be lost, am I wrong? – Nessi Dec 16 '19 at 10:49
  • Well you could use and array in an array and it should still retain the depth – JonoJames Dec 16 '19 at 10:50
  • 1
    what do you mean with "and array"? – Nessi Dec 16 '19 at 10:51
  • you can check it: https://stackoverflow.com/questions/59243239/export-expandable-table-to-csv-using-javascript – Nessi Dec 16 '19 at 10:51
  • What format would you get if you exported the XLS with the depth to csv ... If that csv retains the depth then adjust the code above to ensure that there is depth retention so instead of download_csv() function using it as an array try use a JSON object that indicates the depth. The image you posted looks like hide columns in XLS.... I think what you are trying to do with depth is shift a column right with the depth .That would be easier to achieve as JSON – JonoJames Dec 16 '19 at 10:55
  • WHat you could do however is use the JSON to first generate the data into CSV then use a second array of the groups to run in a macro and group with a VBA macro https://stackoverflow.com/questions/13337267/grouping-rows-in-vba/14967281#14967281 – JonoJames Dec 16 '19 at 11:12