Export tables to Excel script - How to make correctly?

Hi all!
For my needs, I found a script that exports a table in Excel.

The script code with export-to-excel button

<script>
var tableToExcel = (function() {
  var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
  return function(table, name) {
    if (!table.nodeType) table = document.getElementById(table)
    var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
    window.location.href = uri + base64(format(template, ctx))
  }
})()
</script> 
<input type="button" onclick="tableToExcel('view_290')" value="Export to Excel">

"view_290" - instead, insert ID for your DIV block

But there are some difficulties in its implementation. I'm not a programmer, so I do not know how to achieve a good result.
For example, how to place the script in a Custom Javascript table Builder, and that he worked there?
I have the script works only on the target page, for example, if you place it in the box for instructions.


And that's why it only works if the instructions field is in the Add-form or Edit-form entries (that is, there should be any input field, for example, text). If the embed script in the description field - the script is cut off and nothing happens.


The second problem-need to specify in the script table ID. But in the generated Knack tables only have a class, ID is in the div. So exported not only to table, and all the contents of the DIV block.


Any ideas on this?

Hi Stevan!
Thanks for the info , I’ll try this method .

Hi Alexander,

If you need frequent export to excel then you can use webmerge if that fits your budget. http://www.webmerge.me

You can use the same kind of code as the one described in http://helpdesk.knackhq.com/support/discussions/topics/5000019533.

You will only need to create your excel template with merged fields and write the code in knack API and Code / Javascript. (If your refer to that example I would replace the $('#view_XX').submit(function(event) by $('#view_XX').click(function(event) )

You can assign the event to a knack submit button the text of which can be modified by using a very short code as described in : http://helpdesk.knackhq.com/support/solutions/articles/5000447560-code-examples#replacesubmittext

Hello Stevan!
Yes, I know that there are export table, but this includes export to CSV format. and I need it in xls (or .xlsx) to not do the double conversion formats. This is useful for my staff and clients. We are working in Excel for their needs.

Hi Alexander,

Did you try " record exporting" in the table view option?