V the Following Script Demonstrates How to Export a Coach Table As an Excel File
1 Introduction
v 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.
v The following script demonstrates how to export a Coach Table as an Excel file.
2 Table Export Script
v Please follow the steps mentioned below in order to use this your application,
v Drag 'Custom HTML' component from your coach palette.
v Copy paste the script mentioned below.
<a href="javascript:doSubmitExport()"<img src="/portal/jsp/images/reporting/enabled/export_16x16.gif" align="left">Export</a>
<script>
//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 = '<form name="exportData" method="post" target="_new" action="data-redirect.jsp"<input type="hidden" name="contentType" value="text/csv"<input type="hidden" name="data" value=""<input type="hidden" name="fileName" value="filename=reportData.csv"</form>';
//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<tableArr.length;i++) {
addTableToCSV(tableArr[i]);
}
document.forms["exportData"].submit();
}
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;
}
</script>
v 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.
v It shall display Excel export icon on the coach.
v Once user clicks on the icon it exports all the table information in to a CSV file.