From a71bb8146c916a3d020ab7f5a1ed62c333b3f95a Mon Sep 17 00:00:00 2001 From: Nicolas Pascual Date: Mon, 2 May 2016 18:06:39 -0300 Subject: [PATCH 1/5] fix for issue #30 Rowspan and Colspan not properly added to excel file --- tableExport.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tableExport.js b/tableExport.js index 1bfaa0fc..ec0f8d1d 100644 --- a/tableExport.js +++ b/tableExport.js @@ -220,8 +220,10 @@ THE SOFTWARE.*/ $(this).filter(':visible').find('th').each(function(index,data) { if ($(this).css('display') != 'none'){ if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += "" + parseString($(this))+ ""; - } + colSpan = ($(this).prop('colSpan') > 0) ? $(this).prop('colSpan') : 1; + rowSpan = ($(this).prop('rowSpan') > 0) ? $(this).prop('rowSpan') : 1; + excel += "" + parseString($(this))+ ""; + } } }); excel += ''; From 03d193caaba36f310e30ce471136a42deab1c00e Mon Sep 17 00:00:00 2001 From: Nicolas Pascual Date: Mon, 2 May 2016 18:20:55 -0300 Subject: [PATCH 2/5] specify worksheet name by data-name attribute in table element --- tableExport.js | 679 +++++++++++++++++++++++++------------------------ 1 file changed, 340 insertions(+), 339 deletions(-) diff --git a/tableExport.js b/tableExport.js index ec0f8d1d..247eebbf 100644 --- a/tableExport.js +++ b/tableExport.js @@ -20,342 +20,343 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ -(function($){ - $.fn.extend({ - tableExport: function(options) { - var defaults = { - separator: ',', - ignoreColumn: [], - tableName:'yourTableName', - type:'csv', - pdfFontSize:14, - pdfLeftMargin:20, - escape:'true', - htmlContent:'false', - consoleLog:'false' - }; - - var options = $.extend(defaults, options); - var el = this; - - if(defaults.type == 'csv' || defaults.type == 'txt'){ - - // Header - var tdData =""; - $(el).find('thead').find('tr').each(function() { - tdData += "\n"; - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - tdData += '"' + parseString($(this)) + '"' + defaults.separator; - } - } - - }); - tdData = $.trim(tdData); - tdData = $.trim(tdData).substring(0, tdData.length -1); - }); - - // Row vs Column - $(el).find('tbody').find('tr').each(function() { - tdData += "\n"; - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - tdData += '"'+ parseString($(this)) + '"'+ defaults.separator; - } - } - }); - //tdData = $.trim(tdData); - tdData = $.trim(tdData).substring(0, tdData.length -1); - }); - - //output - if(defaults.consoleLog == 'true'){ - console.log(tdData); - } - var base64data = "base64," + $.base64.encode(tdData); - window.open('data:application/'+defaults.type+';filename=exportData;' + base64data); - }else if(defaults.type == 'sql'){ - - // Header - var tdData ="INSERT INTO `"+defaults.tableName+"` ("; - $(el).find('thead').find('tr').each(function() { - - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - tdData += '`' + parseString($(this)) + '`,' ; - } - } - - }); - tdData = $.trim(tdData); - tdData = $.trim(tdData).substring(0, tdData.length -1); - }); - tdData += ") VALUES "; - // Row vs Column - $(el).find('tbody').find('tr').each(function() { - tdData += "("; - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - tdData += '"'+ parseString($(this)) + '",'; - } - } - }); - - tdData = $.trim(tdData).substring(0, tdData.length -1); - tdData += "),"; - }); - tdData = $.trim(tdData).substring(0, tdData.length -1); - tdData += ";"; - - //output - //console.log(tdData); - - if(defaults.consoleLog == 'true'){ - console.log(tdData); - } - - var base64data = "base64," + $.base64.encode(tdData); - window.open('data:application/sql;filename=exportData;' + base64data); - - - }else if(defaults.type == 'json'){ - - var jsonHeaderArray = []; - $(el).find('thead').find('tr').each(function() { - var tdData =""; - var jsonArrayTd = []; - - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - jsonArrayTd.push(parseString($(this))); - } - } - }); - jsonHeaderArray.push(jsonArrayTd); - - }); - - var jsonArray = []; - $(el).find('tbody').find('tr').each(function() { - var tdData =""; - var jsonArrayTd = []; - - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - jsonArrayTd.push(parseString($(this))); - } - } - }); - jsonArray.push(jsonArrayTd); - - }); - - var jsonExportArray =[]; - jsonExportArray.push({header:jsonHeaderArray,data:jsonArray}); - - //Return as JSON - //console.log(JSON.stringify(jsonExportArray)); - - //Return as Array - //console.log(jsonExportArray); - if(defaults.consoleLog == 'true'){ - console.log(JSON.stringify(jsonExportArray)); - } - var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray)); - window.open('data:application/json;filename=exportData;' + base64data); - }else if(defaults.type == 'xml'){ - - var xml = ''; - xml += ''; - - // Header - $(el).find('thead').find('tr').each(function() { - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - xml += "" + parseString($(this)) + ""; - } - } - }); - }); - xml += ''; - - // Row Vs Column - var rowCount=1; - $(el).find('tbody').find('tr').each(function() { - xml += ''; - var colCount=0; - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - xml += ""+parseString($(this))+""; - } - } - colCount++; - }); - rowCount++; - xml += ''; - }); - xml += '' - - if(defaults.consoleLog == 'true'){ - console.log(xml); - } - - var base64data = "base64," + $.base64.encode(xml); - window.open('data:application/xml;filename=exportData;' + base64data); - - }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){ - //console.log($(this).html()); - var excel=""; - // Header - $(el).find('thead').find('tr').each(function() { - excel += ""; - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - colSpan = ($(this).prop('colSpan') > 0) ? $(this).prop('colSpan') : 1; - rowSpan = ($(this).prop('rowSpan') > 0) ? $(this).prop('rowSpan') : 1; - excel += ""; - } - } - }); - excel += ''; - - }); - - - // Row Vs Column - var rowCount=1; - $(el).find('tbody').find('tr').each(function() { - excel += ""; - var colCount=0; - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += ""; - } - } - colCount++; - }); - rowCount++; - excel += ''; - }); - excel += '
" + parseString($(this))+ "
"+parseString($(this))+"
' - - if(defaults.consoleLog == 'true'){ - console.log(excel); - } - - var excelFile = ""; - excelFile += ""; - excelFile += ""; - excelFile += ""; - excelFile += ""; - excelFile += excel; - excelFile += ""; - excelFile += ""; - - var base64data = "base64," + $.base64.encode(excelFile); - window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); - - }else if(defaults.type == 'png'){ - html2canvas($(el), { - onrendered: function(canvas) { - var img = canvas.toDataURL("image/png"); - window.open(img); - - - } - }); - }else if(defaults.type == 'pdf'){ - - var doc = new jsPDF('p','pt', 'a4', true); - doc.setFontSize(defaults.pdfFontSize); - - // Header - var startColPosition=defaults.pdfLeftMargin; - $(el).find('thead').find('tr').each(function() { - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - var colPosition = startColPosition+ (index * 50); - doc.text(colPosition,20, parseString($(this))); - } - } - }); - }); - - - // Row Vs Column - var startRowPosition = 20; var page =1;var rowPosition=0; - $(el).find('tbody').find('tr').each(function(index,data) { - rowCalc = index+1; - - if (rowCalc % 26 == 0){ - doc.addPage(); - page++; - startRowPosition=startRowPosition+10; - } - rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280); - - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - if(defaults.ignoreColumn.indexOf(index) == -1){ - var colPosition = startColPosition+ (index * 50); - doc.text(colPosition,rowPosition, parseString($(this))); - } - } - - }); - - }); - - // Output as Data URI - doc.output('datauri'); - - } - - - function parseString(data){ - - if(defaults.htmlContent == 'true'){ - content_data = data.html().trim(); - }else{ - content_data = data.text().trim(); - } - - if(defaults.escape == 'true'){ - content_data = escape(content_data); - } - - - - return content_data; - } - - } - }); - })(jQuery); - +(function ($) { + $.fn.extend({ + tableExport: function (options) { + var defaults = { + separator: ',', + ignoreColumn: [], + tableName: 'yourTableName', + type: 'csv', + pdfFontSize: 14, + pdfLeftMargin: 20, + escape: 'true', + htmlContent: 'false', + consoleLog: 'false' + }; + + var options = $.extend(defaults, options); + var el = this; + + if (defaults.type == 'csv' || defaults.type == 'txt') { + + // Header + var tdData = ""; + $(el).find('thead').find('tr').each(function () { + tdData += "\n"; + $(this).filter(':visible').find('th').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + tdData += '"' + parseString($(this)) + '"' + defaults.separator; + } + } + + }); + tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length - 1); + }); + + // Row vs Column + $(el).find('tbody').find('tr').each(function () { + tdData += "\n"; + $(this).filter(':visible').find('td').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + tdData += '"' + parseString($(this)) + '"' + defaults.separator; + } + } + }); + //tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length - 1); + }); + + //output + if (defaults.consoleLog == 'true') { + console.log(tdData); + } + var base64data = "base64," + $.base64.encode(tdData); + window.open('data:application/' + defaults.type + ';filename=exportData;' + base64data); + } else if (defaults.type == 'sql') { + + // Header + var tdData = "INSERT INTO `" + defaults.tableName + "` ("; + $(el).find('thead').find('tr').each(function () { + + $(this).filter(':visible').find('th').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + tdData += '`' + parseString($(this)) + '`,'; + } + } + + }); + tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length - 1); + }); + tdData += ") VALUES "; + // Row vs Column + $(el).find('tbody').find('tr').each(function () { + tdData += "("; + $(this).filter(':visible').find('td').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + tdData += '"' + parseString($(this)) + '",'; + } + } + }); + + tdData = $.trim(tdData).substring(0, tdData.length - 1); + tdData += "),"; + }); + tdData = $.trim(tdData).substring(0, tdData.length - 1); + tdData += ";"; + + //output + //console.log(tdData); + + if (defaults.consoleLog == 'true') { + console.log(tdData); + } + + var base64data = "base64," + $.base64.encode(tdData); + window.open('data:application/sql;filename=exportData;' + base64data); + + + } else if (defaults.type == 'json') { + + var jsonHeaderArray = []; + $(el).find('thead').find('tr').each(function () { + var tdData = ""; + var jsonArrayTd = []; + + $(this).filter(':visible').find('th').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + jsonArrayTd.push(parseString($(this))); + } + } + }); + jsonHeaderArray.push(jsonArrayTd); + + }); + + var jsonArray = []; + $(el).find('tbody').find('tr').each(function () { + var tdData = ""; + var jsonArrayTd = []; + + $(this).filter(':visible').find('td').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + jsonArrayTd.push(parseString($(this))); + } + } + }); + jsonArray.push(jsonArrayTd); + + }); + + var jsonExportArray = []; + jsonExportArray.push({ header: jsonHeaderArray, data: jsonArray }); + + //Return as JSON + //console.log(JSON.stringify(jsonExportArray)); + + //Return as Array + //console.log(jsonExportArray); + if (defaults.consoleLog == 'true') { + console.log(JSON.stringify(jsonExportArray)); + } + var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray)); + window.open('data:application/json;filename=exportData;' + base64data); + } else if (defaults.type == 'xml') { + + var xml = ''; + xml += ''; + + // Header + $(el).find('thead').find('tr').each(function () { + $(this).filter(':visible').find('th').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + xml += "" + parseString($(this)) + ""; + } + } + }); + }); + xml += ''; + + // Row Vs Column + var rowCount = 1; + $(el).find('tbody').find('tr').each(function () { + xml += ''; + var colCount = 0; + $(this).filter(':visible').find('td').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + xml += "" + parseString($(this)) + ""; + } + } + colCount++; + }); + rowCount++; + xml += ''; + }); + xml += '' + + if (defaults.consoleLog == 'true') { + console.log(xml); + } + + var base64data = "base64," + $.base64.encode(xml); + window.open('data:application/xml;filename=exportData;' + base64data); + + } else if (defaults.type == 'excel' || defaults.type == 'doc' || defaults.type == 'powerpoint') { + //console.log($(this).html()); + var excel = ""; + // Header + $(el).find('thead').find('tr').each(function () { + excel += ""; + $(this).filter(':visible').find('th').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + colSpan = ($(this).prop('colSpan') > 0) ? $(this).prop('colSpan') : 1; + rowSpan = ($(this).prop('rowSpan') > 0) ? $(this).prop('rowSpan') : 1; + excel += ""; + } + } + }); + excel += ''; + + }); + + + // Row Vs Column + var rowCount = 1; + $(el).find('tbody').find('tr').each(function () { + excel += ""; + var colCount = 0; + $(this).filter(':visible').find('td').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + excel += ""; + } + } + colCount++; + }); + rowCount++; + excel += ''; + }); + excel += '
" + parseString($(this)) + "
" + parseString($(this)) + "
' + + if (defaults.consoleLog == 'true') { + console.log(excel); + } + var sheetName = $(el).attr('data-name'); + sheetName = sheetName != null && sheetName != "" ? sheetName : "worksheet"; + var excelFile = ""; + excelFile += ""; + excelFile += ""; + excelFile += ""; + excelFile += ""; + excelFile += excel; + excelFile += ""; + excelFile += ""; + + var base64data = "base64," + $.base64.encode(excelFile); + window.open('data:application/vnd.ms-' + defaults.type + ';filename=exportData.doc;' + base64data); + + } else if (defaults.type == 'png') { + html2canvas($(el), { + onrendered: function (canvas) { + var img = canvas.toDataURL("image/png"); + window.open(img); + + + } + }); + } else if (defaults.type == 'pdf') { + + var doc = new jsPDF('p', 'pt', 'a4', true); + doc.setFontSize(defaults.pdfFontSize); + + // Header + var startColPosition = defaults.pdfLeftMargin; + $(el).find('thead').find('tr').each(function () { + $(this).filter(':visible').find('th').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + var colPosition = startColPosition + (index * 50); + doc.text(colPosition, 20, parseString($(this))); + } + } + }); + }); + + + // Row Vs Column + var startRowPosition = 20; var page = 1; var rowPosition = 0; + $(el).find('tbody').find('tr').each(function (index, data) { + rowCalc = index + 1; + + if (rowCalc % 26 == 0) { + doc.addPage(); + page++; + startRowPosition = startRowPosition + 10; + } + rowPosition = (startRowPosition + (rowCalc * 10)) - ((page - 1) * 280); + + $(this).filter(':visible').find('td').each(function (index, data) { + if ($(this).css('display') != 'none') { + if (defaults.ignoreColumn.indexOf(index) == -1) { + var colPosition = startColPosition + (index * 50); + doc.text(colPosition, rowPosition, parseString($(this))); + } + } + + }); + + }); + + // Output as Data URI + doc.output('datauri'); + + } + + + function parseString(data) { + + if (defaults.htmlContent == 'true') { + content_data = data.html().trim(); + } else { + content_data = data.text().trim(); + } + + if (defaults.escape == 'true') { + content_data = escape(content_data); + } + + + + return content_data; + } + + } + }); +})(jQuery); + From f3044d3214ff8b7fbef66a68d0da9f3b6a2d034b Mon Sep 17 00:00:00 2001 From: Nicolas Pascual Date: Mon, 2 May 2016 18:25:55 -0300 Subject: [PATCH 3/5] fix line endings --- tableExport.js | 677 ++++++++++++++++++++++++------------------------- 1 file changed, 337 insertions(+), 340 deletions(-) diff --git a/tableExport.js b/tableExport.js index 247eebbf..b9fcce4d 100644 --- a/tableExport.js +++ b/tableExport.js @@ -20,343 +20,340 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/ -(function ($) { - $.fn.extend({ - tableExport: function (options) { - var defaults = { - separator: ',', - ignoreColumn: [], - tableName: 'yourTableName', - type: 'csv', - pdfFontSize: 14, - pdfLeftMargin: 20, - escape: 'true', - htmlContent: 'false', - consoleLog: 'false' - }; - - var options = $.extend(defaults, options); - var el = this; - - if (defaults.type == 'csv' || defaults.type == 'txt') { - - // Header - var tdData = ""; - $(el).find('thead').find('tr').each(function () { - tdData += "\n"; - $(this).filter(':visible').find('th').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - tdData += '"' + parseString($(this)) + '"' + defaults.separator; - } - } - - }); - tdData = $.trim(tdData); - tdData = $.trim(tdData).substring(0, tdData.length - 1); - }); - - // Row vs Column - $(el).find('tbody').find('tr').each(function () { - tdData += "\n"; - $(this).filter(':visible').find('td').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - tdData += '"' + parseString($(this)) + '"' + defaults.separator; - } - } - }); - //tdData = $.trim(tdData); - tdData = $.trim(tdData).substring(0, tdData.length - 1); - }); - - //output - if (defaults.consoleLog == 'true') { - console.log(tdData); - } - var base64data = "base64," + $.base64.encode(tdData); - window.open('data:application/' + defaults.type + ';filename=exportData;' + base64data); - } else if (defaults.type == 'sql') { - - // Header - var tdData = "INSERT INTO `" + defaults.tableName + "` ("; - $(el).find('thead').find('tr').each(function () { - - $(this).filter(':visible').find('th').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - tdData += '`' + parseString($(this)) + '`,'; - } - } - - }); - tdData = $.trim(tdData); - tdData = $.trim(tdData).substring(0, tdData.length - 1); - }); - tdData += ") VALUES "; - // Row vs Column - $(el).find('tbody').find('tr').each(function () { - tdData += "("; - $(this).filter(':visible').find('td').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - tdData += '"' + parseString($(this)) + '",'; - } - } - }); - - tdData = $.trim(tdData).substring(0, tdData.length - 1); - tdData += "),"; - }); - tdData = $.trim(tdData).substring(0, tdData.length - 1); - tdData += ";"; - - //output - //console.log(tdData); - - if (defaults.consoleLog == 'true') { - console.log(tdData); - } - - var base64data = "base64," + $.base64.encode(tdData); - window.open('data:application/sql;filename=exportData;' + base64data); - - - } else if (defaults.type == 'json') { - - var jsonHeaderArray = []; - $(el).find('thead').find('tr').each(function () { - var tdData = ""; - var jsonArrayTd = []; - - $(this).filter(':visible').find('th').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - jsonArrayTd.push(parseString($(this))); - } - } - }); - jsonHeaderArray.push(jsonArrayTd); - - }); - - var jsonArray = []; - $(el).find('tbody').find('tr').each(function () { - var tdData = ""; - var jsonArrayTd = []; - - $(this).filter(':visible').find('td').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - jsonArrayTd.push(parseString($(this))); - } - } - }); - jsonArray.push(jsonArrayTd); - - }); - - var jsonExportArray = []; - jsonExportArray.push({ header: jsonHeaderArray, data: jsonArray }); - - //Return as JSON - //console.log(JSON.stringify(jsonExportArray)); - - //Return as Array - //console.log(jsonExportArray); - if (defaults.consoleLog == 'true') { - console.log(JSON.stringify(jsonExportArray)); - } - var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray)); - window.open('data:application/json;filename=exportData;' + base64data); - } else if (defaults.type == 'xml') { - - var xml = ''; - xml += ''; - - // Header - $(el).find('thead').find('tr').each(function () { - $(this).filter(':visible').find('th').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - xml += "" + parseString($(this)) + ""; - } - } - }); - }); - xml += ''; - - // Row Vs Column - var rowCount = 1; - $(el).find('tbody').find('tr').each(function () { - xml += ''; - var colCount = 0; - $(this).filter(':visible').find('td').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - xml += "" + parseString($(this)) + ""; - } - } - colCount++; - }); - rowCount++; - xml += ''; - }); - xml += '' - - if (defaults.consoleLog == 'true') { - console.log(xml); - } - - var base64data = "base64," + $.base64.encode(xml); - window.open('data:application/xml;filename=exportData;' + base64data); - - } else if (defaults.type == 'excel' || defaults.type == 'doc' || defaults.type == 'powerpoint') { - //console.log($(this).html()); - var excel = ""; - // Header - $(el).find('thead').find('tr').each(function () { - excel += ""; - $(this).filter(':visible').find('th').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - colSpan = ($(this).prop('colSpan') > 0) ? $(this).prop('colSpan') : 1; - rowSpan = ($(this).prop('rowSpan') > 0) ? $(this).prop('rowSpan') : 1; - excel += ""; - } - } - }); - excel += ''; - - }); - - - // Row Vs Column - var rowCount = 1; - $(el).find('tbody').find('tr').each(function () { - excel += ""; - var colCount = 0; - $(this).filter(':visible').find('td').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - excel += ""; - } - } - colCount++; - }); - rowCount++; - excel += ''; - }); - excel += '
" + parseString($(this)) + "
" + parseString($(this)) + "
' - - if (defaults.consoleLog == 'true') { - console.log(excel); - } - var sheetName = $(el).attr('data-name'); - sheetName = sheetName != null && sheetName != "" ? sheetName : "worksheet"; - var excelFile = ""; - excelFile += ""; - excelFile += ""; - excelFile += ""; - excelFile += ""; - excelFile += excel; - excelFile += ""; - excelFile += ""; - - var base64data = "base64," + $.base64.encode(excelFile); - window.open('data:application/vnd.ms-' + defaults.type + ';filename=exportData.doc;' + base64data); - - } else if (defaults.type == 'png') { - html2canvas($(el), { - onrendered: function (canvas) { - var img = canvas.toDataURL("image/png"); - window.open(img); - - - } - }); - } else if (defaults.type == 'pdf') { - - var doc = new jsPDF('p', 'pt', 'a4', true); - doc.setFontSize(defaults.pdfFontSize); - - // Header - var startColPosition = defaults.pdfLeftMargin; - $(el).find('thead').find('tr').each(function () { - $(this).filter(':visible').find('th').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - var colPosition = startColPosition + (index * 50); - doc.text(colPosition, 20, parseString($(this))); - } - } - }); - }); - - - // Row Vs Column - var startRowPosition = 20; var page = 1; var rowPosition = 0; - $(el).find('tbody').find('tr').each(function (index, data) { - rowCalc = index + 1; - - if (rowCalc % 26 == 0) { - doc.addPage(); - page++; - startRowPosition = startRowPosition + 10; - } - rowPosition = (startRowPosition + (rowCalc * 10)) - ((page - 1) * 280); - - $(this).filter(':visible').find('td').each(function (index, data) { - if ($(this).css('display') != 'none') { - if (defaults.ignoreColumn.indexOf(index) == -1) { - var colPosition = startColPosition + (index * 50); - doc.text(colPosition, rowPosition, parseString($(this))); - } - } - - }); - - }); - - // Output as Data URI - doc.output('datauri'); - - } - - - function parseString(data) { - - if (defaults.htmlContent == 'true') { - content_data = data.html().trim(); - } else { - content_data = data.text().trim(); - } - - if (defaults.escape == 'true') { - content_data = escape(content_data); - } - - - - return content_data; - } - - } - }); -})(jQuery); - +(function($){ + $.fn.extend({ + tableExport: function(options) { + var defaults = { + separator: ',', + ignoreColumn: [], + tableName:'yourTableName', + type:'csv', + pdfFontSize:14, + pdfLeftMargin:20, + escape:'true', + htmlContent:'false', + consoleLog:'false' + }; + + var options = $.extend(defaults, options); + var el = this; + + if(defaults.type == 'csv' || defaults.type == 'txt'){ + + // Header + var tdData =""; + $(el).find('thead').find('tr').each(function() { + tdData += "\n"; + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + tdData += '"' + parseString($(this)) + '"' + defaults.separator; + } + } + + }); + tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length -1); + }); + + // Row vs Column + $(el).find('tbody').find('tr').each(function() { + tdData += "\n"; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + tdData += '"'+ parseString($(this)) + '"'+ defaults.separator; + } + } + }); + //tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length -1); + }); + + //output + if(defaults.consoleLog == 'true'){ + console.log(tdData); + } + var base64data = "base64," + $.base64.encode(tdData); + window.open('data:application/'+defaults.type+';filename=exportData;' + base64data); + }else if(defaults.type == 'sql'){ + + // Header + var tdData ="INSERT INTO `"+defaults.tableName+"` ("; + $(el).find('thead').find('tr').each(function() { + + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + tdData += '`' + parseString($(this)) + '`,' ; + } + } + + }); + tdData = $.trim(tdData); + tdData = $.trim(tdData).substring(0, tdData.length -1); + }); + tdData += ") VALUES "; + // Row vs Column + $(el).find('tbody').find('tr').each(function() { + tdData += "("; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + tdData += '"'+ parseString($(this)) + '",'; + } + } + }); + + tdData = $.trim(tdData).substring(0, tdData.length -1); + tdData += "),"; + }); + tdData = $.trim(tdData).substring(0, tdData.length -1); + tdData += ";"; + + //output + //console.log(tdData); + + if(defaults.consoleLog == 'true'){ + console.log(tdData); + } + + var base64data = "base64," + $.base64.encode(tdData); + window.open('data:application/sql;filename=exportData;' + base64data); + + + }else if(defaults.type == 'json'){ + + var jsonHeaderArray = []; + $(el).find('thead').find('tr').each(function() { + var tdData =""; + var jsonArrayTd = []; + + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + jsonArrayTd.push(parseString($(this))); + } + } + }); + jsonHeaderArray.push(jsonArrayTd); + + }); + + var jsonArray = []; + $(el).find('tbody').find('tr').each(function() { + var tdData =""; + var jsonArrayTd = []; + + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + jsonArrayTd.push(parseString($(this))); + } + } + }); + jsonArray.push(jsonArrayTd); + + }); + + var jsonExportArray =[]; + jsonExportArray.push({header:jsonHeaderArray,data:jsonArray}); + + //Return as JSON + //console.log(JSON.stringify(jsonExportArray)); + + //Return as Array + //console.log(jsonExportArray); + if(defaults.consoleLog == 'true'){ + console.log(JSON.stringify(jsonExportArray)); + } + var base64data = "base64," + $.base64.encode(JSON.stringify(jsonExportArray)); + window.open('data:application/json;filename=exportData;' + base64data); + }else if(defaults.type == 'xml'){ + + var xml = ''; + xml += ''; + + // Header + $(el).find('thead').find('tr').each(function() { + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + xml += "" + parseString($(this)) + ""; + } + } + }); + }); + xml += ''; + + // Row Vs Column + var rowCount=1; + $(el).find('tbody').find('tr').each(function() { + xml += ''; + var colCount=0; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + xml += ""+parseString($(this))+""; + } + } + colCount++; + }); + rowCount++; + xml += ''; + }); + xml += '' + + if(defaults.consoleLog == 'true'){ + console.log(xml); + } + + var base64data = "base64," + $.base64.encode(xml); + window.open('data:application/xml;filename=exportData;' + base64data); + + }else if(defaults.type == 'excel' || defaults.type == 'doc'|| defaults.type == 'powerpoint' ){ + //console.log($(this).html()); + var excel=""; + // Header + $(el).find('thead').find('tr').each(function() { + excel += ""; + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + excel += ""; + } + } + }); + excel += ''; + + }); + + + // Row Vs Column + var rowCount=1; + $(el).find('tbody').find('tr').each(function() { + excel += ""; + var colCount=0; + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + excel += ""; + } + } + colCount++; + }); + rowCount++; + excel += ''; + }); + excel += '
" + parseString($(this))+ "
"+parseString($(this))+"
' + + if(defaults.consoleLog == 'true'){ + console.log(excel); + } + + var excelFile = ""; + excelFile += ""; + excelFile += ""; + excelFile += ""; + excelFile += ""; + excelFile += excel; + excelFile += ""; + excelFile += ""; + + var base64data = "base64," + $.base64.encode(excelFile); + window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); + + }else if(defaults.type == 'png'){ + html2canvas($(el), { + onrendered: function(canvas) { + var img = canvas.toDataURL("image/png"); + window.open(img); + + + } + }); + }else if(defaults.type == 'pdf'){ + + var doc = new jsPDF('p','pt', 'a4', true); + doc.setFontSize(defaults.pdfFontSize); + + // Header + var startColPosition=defaults.pdfLeftMargin; + $(el).find('thead').find('tr').each(function() { + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + var colPosition = startColPosition+ (index * 50); + doc.text(colPosition,20, parseString($(this))); + } + } + }); + }); + + + // Row Vs Column + var startRowPosition = 20; var page =1;var rowPosition=0; + $(el).find('tbody').find('tr').each(function(index,data) { + rowCalc = index+1; + + if (rowCalc % 26 == 0){ + doc.addPage(); + page++; + startRowPosition=startRowPosition+10; + } + rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280); + + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + if(defaults.ignoreColumn.indexOf(index) == -1){ + var colPosition = startColPosition+ (index * 50); + doc.text(colPosition,rowPosition, parseString($(this))); + } + } + + }); + + }); + + // Output as Data URI + doc.output('datauri'); + + } + + + function parseString(data){ + + if(defaults.htmlContent == 'true'){ + content_data = data.html().trim(); + }else{ + content_data = data.text().trim(); + } + + if(defaults.escape == 'true'){ + content_data = escape(content_data); + } + + + + return content_data; + } + + } + }); + })(jQuery); + \ No newline at end of file From 054fbbf89a7ea6289785717d11bd0d76a7626887 Mon Sep 17 00:00:00 2001 From: Nicolas Pascual Date: Mon, 2 May 2016 18:33:58 -0300 Subject: [PATCH 4/5] specify worksheet name by data-name attribute in table element --- tableExport.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tableExport.js b/tableExport.js index b9fcce4d..2ef7cf0b 100644 --- a/tableExport.js +++ b/tableExport.js @@ -250,7 +250,8 @@ THE SOFTWARE.*/ if(defaults.consoleLog == 'true'){ console.log(excel); } - + var worksheetName = $(el).attr('data-name'); + worksheetName = worksheetName != null && worksheetName != "" ? worksheetName : "worksheet"; var excelFile = ""; excelFile += ""; excelFile += "