V the Following Script Demonstrates How to Export a Coach Table As an Excel File
Total Page:16
File Type:pdf, Size:1020Kb
1 Introduction Often times a table in a Coach contains information that the user would like a local copy in a format such as CSV or Excel. The following script demonstrates how to export a Coach Table as an Excel file.
2 Table Export Script Please follow the steps mentioned below in order to use this your application, Drag 'Custom HTML' component from your coach palette. Copy paste the script mentioned below.
//Modify the value of these variable for your coach var tableIdsToExport = "Table0,Table1";
// a comma delimited list of the ids of the tables that you want to include in the excel export // if you include a tableId that does not exist in your coach, you will recieve a client side error message
//You do not need to modify the following part of the script
var form = document.createElement('div'); form.innerHTML = '
'; //Work around a bug in IE: http://support.microsoft.com/default.aspx/kb/927917 document.getElementsByTagName("H1")[0].appendChild(form); //document.body.appendChild(form); function addExportData(csvTable) { if (document.forms.exportData == null || document.forms.exportData.data == null) { return; } document.forms.exportData.data.value = document.forms.exportData.data.value + "\n\n" + csvTable; } function doSubmitExport() { var tableArr = tableIdsToExport.split(","); for (var i=0;i function addTableToCSV(tableId) { var table; try { table = document.getElementById(tableId); var a = table.innerHTML; //replace existing commas with semi-colons. Poor mans way of handling embedded commas in a csv file a = a.replace(/,/g, ";"); //insert commas at the end of a table cell a = a.replace(/<\/td>/g, ","); a = a.replace(/<\/TD>/g, ","); a = a.replace(/<\/th>/g, ","); a = a.replace(/<\/TH>/g, ","); //insert a newline tag at the end of every row. Need to do this before removing all tags a = a.replace(/<\/tr>/g, "---newline---"); a = a.replace(/<\/TR>/g, "---newline---"); //remove html tags a = a.replace(/<\/?[^>]+(>|$)/g, ""); //remove whitespace (regexs found via google) a = a.replace(/\r/g, " "); a = a.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, ""); a = a.replace(/'/g, ""); a = a.replace(/ +/g, " "); a = a.replace(/^\s/g, ""); a = a.replace(/\s$/g, ""); //put newlines in a = a.replace(/---newline---/g, "\n"); //replace   which the coach designer inserts a = a.replace(/\ /g, " "); //a now holds a resonable csv that I can put in excel //alert(a); addExportData(a); return true; } catch (e) { alert("Table Export Error: " + e); } return true; } Modify the information mentioned in the line 5. var tableIdsToExport = "Table0,Table1"; Note: provide a comma delimited list of the ids of the tables that you want to include in the excel export. It shall display Excel export icon on the coach. Once user clicks on the icon it exports all the table information in to a CSV file.