0 votes
by (360 points)

Hi everybody,

I want my users to be able to download a table, such as

<table>
<tbody>
<tr>
<td>team skills</td>
<td><<print $comp["teamskills"]>>/6</td>
<td><<printPercentage "teamskills">></td>
<td><<printBar "teamskills">></td>
</tr>
<tr>
<td>communication skills</td>
<td><<print $comp["commskills"]>>/6</td>
<td><<printPercentage "commskills">></td>
<td><<printBar "commskills">></td>
</tr>
<tr>
<td>tolerance</td>
<td><<print $comp["tolerance"]>>/6</td>
<td><<printPercentage "tolerance">></td>
<td><<printBar "tolerance">></td>
</tr>
<tr>
<td>responsibility</td>
<td><<print $comp["responsibility"]>>/6</td>
<td><<printPercentage "responsibility">></td>
<td><<printBar "responsibility">></td>
</tr>
</tbody>
</table>

as an Excel file. Is there a quick way to do so?

I tried using this code (pasted js in Story Javascript and tried to call the function using button & run, but it did not work.

1 Answer

0 votes
by (590 points)

I don't know anything about exporting tables, but I do know that if you are adding a function via the Story JS, you need to format it like this:

window.foo = function() {
    // code goes here
};

Then you can call the function in your story using <<run foo()>> or <<script>>foo()<</script>>

by (360 points)

Thank you, TipsyMongoose.

 

Here is what I did:

Javascript:

window.exportToExcel = function(that, id, hasHeader, removeLinks, removeImages, removeInputParams) {
if (that == null || typeof that === 'undefined') {
    console.log('Sender is required');
    return false;
}

if (!(that instanceof HTMLAnchorElement)) {
    console.log('Sender must be an anchor element');
    return false;
}

if (id == null || typeof id === 'undefined') {
    console.log('Table id is required');
    return false;
}
if (hasHeader == null || typeof hasHeader === 'undefined') {
    hasHeader = true;
}
if (removeLinks == null || typeof removeLinks === 'undefined') {
    removeLinks = true;
}
if (removeImages == null || typeof removeImages === 'undefined') {
    removeImages = false;
}
if (removeInputParams == null || typeof removeInputParams === 'undefined') {
    removeInputParams = true;
}

var tab_text = "<table border='2px'>";
var textRange;

tab = $(id).get(0);

if (tab == null || typeof tab === 'undefined') {
    console.log('Table not found');
    return;
}

var j = 0;

if (hasHeader && tab.rows.length > 0) {
    var row = tab.rows[0];
    tab_text += "<tr bgcolor='#87AFC6'>";
    for (var l = 0; l < row.cells.length; l++) {
        if ($(tab.rows[0].cells[l]).is(':visible')) {//export visible cols only
            tab_text += "<td>" + row.cells[l].innerHTML + "</td>";
        }
    }
    tab_text += "</tr>";
    j++;
}

for (; j < tab.rows.length; j++) {
    var row = tab.rows[j];
    tab_text += "<tr>";
    for (var l = 0; l < row.cells.length; l++) {
        if ($(tab.rows[j].cells[l]).is(':visible')) {//export visible cols only
            tab_text += "<td>" + row.cells[l].innerHTML + "</td>";
        }
    }
    tab_text += "</tr>";
}

tab_text = tab_text + "</table>";
if (removeLinks)
    tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");
if (removeImages)
    tab_text = tab_text.replace(/<img[^>]*>/gi, ""); 
if (removeInputParams)
    tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, "");

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
{
    myIframe.document.open("txt/html", "replace");
    myIframe.document.write(tab_text);
    myIframe.document.close();
    myIframe.focus();
    sa = myIframe.document.execCommand("SaveAs", true, document.title + ".xls");
    return true;
}
else {
    //other browser tested on IE 11
    var result = "data:application/vnd.ms-excel," + encodeURIComponent(tab_text);
    that.href = result;
    that.download = document.title + ".xls";
    return true;
}
}

 

The passage which holds the table reads:

<table id="myTable">
<tbody>
<tr>
<td><<print $variable[0]>></td>
<td><<print $variable[1]>></td>
<td><<print $variable[2]>></td>
</tr>
<tr>
<td><<print $variable[3]>></td>
<td><<print $variable[4]>></td>
<td><<print $variable[5]>></td>
</tr>
<tr>
<td><<print $variable[6]>></td>
<td><<print $variable[7]>></td>
<td><<print $variable[8]>></td>
</tr>
<tr>
<td><<print $variable[9]>></td>
<td><<print $variable[0]>></td>
<td><<print $variable[1]>></td>
</tr>
<tr>
<td><<print $variable[2]>></td>
<td><<print $variable[3]>></td>
<td><<print $variable[4]>></td>
</tr>
<tr>
<td><<print $variable[5]>></td>
<td><<print $variable[6]>></td>
<td><<print $variable[7]>></td>
</tr>
</tbody>
</table>


<iframe id="myIframe" style="opacity: 0; width: 100%; height: 0px;" seamless="seamless"></iframe>

<<button "Export table">>
	<<run exportToExcel(this, 'myTable')>>
<</button>>

 

When the button is clicked, nothing happens. Neither twine nor the console tell me, what could be the problem.

...