title: HTML table to xlsx spreadsheet
body: <p>I'm building a chrome extension and I need to download a table created dinamically with xlsx extension. This is the relevant code:</p>
  
  <p><strong>HTML</strong></p>
  
  <pre><code>&lt;div class="tab"&gt;
  &lt;table class="sortable" id="mytab"&gt;
  &lt;tr class="visible"&gt;
    &lt;th&gt;Name&lt;/th&gt;
    &lt;th&gt;Age&lt;/th&gt;
  &lt;/tr&gt;
  &lt;tr id="t0"&gt;
    &lt;td id="d0"&gt;&lt;/td&gt;
    &lt;td id="p0"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;!--Here are 299 other rows--&gt;
  &lt;tr id="t300"&gt;
    &lt;td id="d300"&gt;&lt;/td&gt;
    &lt;td id="p300"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The table contains 300 rows with numerical IDs and in the .css file I set the display:none for every cell.
  I use a for loop to dinamically add content and make the cells visible if not empty. This is the js that fills the table checking the arrays <em>names</em> and <em>ages</em>:</p>
  
  <pre><code>for(var i=0; i&lt;301; i++){
    if ((names[i]!=undefined)&amp;&amp;(ages[i]!=undefined)){
        document.getElementById('t'+i).style.display="table-row";
        document.getElementById('d'+i).innerHTML=names[i];
        document.getElementById('d'+i).style.display="table-cell";
        document.getElementById('p'+i).innerHTML=ages[i];
        document.getElementById('p'+i).style.display="table-cell";
  
    }
    else break;
  }
  </code></pre>
  
  <p>It perfectly work but I now need to make the table downloadable. If I go with rightclick+save as... and then I change the extension to .xlsx it produces the spreadsheet I want, though it saves every html tag and it's not practical to do. So I would like an easy way to just right click and save the pure valuse as xlsx table. I already created a context menu entry to do this using</p>
  
  <pre><code>chrome.contextMenus.create({"title": "Save results", "onclick": download});
  </code></pre>
  
  <p>where <em>download</em> is the following function:</p>
  
  <pre><code>function download(){
    chrome.downloads.download({
    url: window.location.href,
    saveAs: true,
    filename: "Table.xlsx"
    });
  }
  </code></pre>
  
  <p>However, doing so it only saves the table header, without values added dinamically. So I checked here on stackoverflow and on google to find a way to download the table. None of the solutions I found was helpful:</p>
  
  <pre><code>window.open('data:application/vnd.ms-excel,' + encodeURIComponent(document.getElementById('mytab').outerHTML));
  </code></pre>
  
  <p>This one returns a blank file without extension and many other solutions I found did the same. Also, I don't want it to download as CSV and would prefer a solution with vanilla javascript.</p>
  
  <p>My question is: is there a way to download my table as xlsx with a simple click on the menu entry?
  Thanks in advance.</p>