Note that there are some explanatory texts on larger screens.

plurals
  1. PORemoving commas at the beginning of a line in javascript
    primarykey
    data
    text
    <p>I am trying to create a CSV file from a dynamically generated table.I have a code which generates a reasonable CSV.The output of the code is</p> <pre><code>, Emp I D, Emp Name, Emp Title, , 111 , andro1 , developer , , 112 , andro2 , developer , , 113 , andro3 , developer , </code></pre> <p>I would like to remove the commas at the beginning of each line so the csv is well formatted.I also would like to neatly format this CSV so that it opens properly in excel(with heading in bold ,well spaced coulmns etc).</p> <p>I am pasting the sample javascript used by me </p> <p><br> </p> <pre><code> //Modify the value of these variable for your coach var tableIdsToExport = "Table0"; // 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 = '&lt;form name="exportData" method="post" target="_new" action="data-redirect.jsp"&gt;&lt;input type="hidden" name="contentType" value="text/csv"&gt;&lt;input type="hidden" name="data" value=""&gt;&lt;input type="hidden" name="fileName" value="filename=reportData.csv"&gt;&lt;/form&gt;'; //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&lt;tableArr.length;i++) { addTableToCSV(tableArr[i]); alert(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, ";"); //get rid of javascript blocks a = a.replace(/&lt;script(.|\n)*?&lt;\/script&gt;/gi, ""); //insert commas at the end of a table cell a = a.replace(/&lt;\/td&gt;/g, ","); a = a.replace(/&lt;\/TD&gt;/g, ","); a = a.replace(/&lt;\/th&gt;/g, ","); a = a.replace(/&lt;\/TH&gt;/g, ","); //insert a newline tag at the end of every row. Need to do this before removing all tags a = a.replace(/&lt;\/tr&gt;/g, "---newline---"); a = a.replace(/&lt;\/TR&gt;/g, "---newline---"); //remove html tags a = a.replace(/&lt;\/?[^&gt;]+(&gt;|$)/g, ""); //remove whitespace (regexs found via google) a = a.replace(/\r/g, " "); a = a.replace(/[^ A-Za-z0-9`~!@#\$%\^&amp;\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.&gt;,&lt;]/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 &amp;nbsp which the coach designer inserts a = a.replace(/\&amp;nbsp;/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; } &lt;/script&gt; </code></pre> <p><p>One more thing is, in the table there is a column which is empty.that is the reason that the script is returning a comma at the beginning which I would like to remove. when I try to open the csv in excel the content starts at the third row instead of the first row.</p> <p>This is being used in IBM BPM Lombardi application where I am trying to export a a dynamically generated table to excel.</p> <p>I would also like know how the same result can be achieved through jquery Thanks in advance</p> <p>Once this is done I am also planning an export to pdf option.</p> <p>the following expalins what is being done in the script</p> <p>The key component that performs the export is a Custom HTML code block that contains client-side JavaScript. The JavaScript contains the following components:</p> <p>1.A JavaScript variable tableIdsToExport. This is a comma-delimited string that itemizes the table element ids to be exported. You must ensure that the ids match those in your Coach.</p> <p><p>2.A JavaScript variable form that is a dynamically generated HTML form element (not displayable). The URI for the action attribute of this form is a Teamworks JSP that will return the spreadsheet. This form also contains an input field data that contains the table(s) to be exported. <p>3.An 'Export to excel' button. When this this button is selected the doSubmitExport function is executed.</p> <p>4.doSubmitExport function. The algorithm for this function performs two tasks. First, it iterates over each table in tableIdsToExport and calls the addTableToCSV function. After the iteration is complete the function submits the form element contained in the form variable (above).</p> <p>5.addTableToCSV function. This function is called for each table in the tableIdsToExport list and has two basic tasks to perform. First, the HTML table is transformed such that the table elements are replaced by commas and line feeds effectively transforming the table into a CSV. Next, the addTableToExport function is called with the transformed table passed as a parameter.</p> <p>6.addTableToExport function. This function takes a parameter that is the CSV and appends it to the hidden form's data field. Upon submission, the form is posted to the Teamworks data-redirect.jsp which applies the appropriate content type and sends the data back to the browser. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload