From 22ffea810efb04cbdcd9fc14fa24fd7a37d61a38 Mon Sep 17 00:00:00 2001 From: Carlos Cabanes Date: Tue, 10 Mar 2015 10:30:44 +0100 Subject: [PATCH 1/3] Include th support and color for excel export --- tableExport.js | 89 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 31 deletions(-) diff --git a/tableExport.js b/tableExport.js index 1bfaa0fc..29bd2074 100644 --- a/tableExport.js +++ b/tableExport.js @@ -1,25 +1,3 @@ -/*The MIT License (MIT) - -Copyright (c) 2014 https://github.com/kayalshri/ - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -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) { @@ -32,7 +10,7 @@ THE SOFTWARE.*/ pdfLeftMargin:20, escape:'true', htmlContent:'false', - consoleLog:'false' + consoleLog:'true' }; var options = $.extend(defaults, options); @@ -218,14 +196,22 @@ THE SOFTWARE.*/ $(el).find('thead').find('tr').each(function() { excel += ""; $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ + if ($(this).css('display') != 'none'){ + var color = $(this).css('background-color'); + if(defaults.ignoreColumn.indexOf(index) == -1){ + excel += parseItemData($(this),'th',color); + } + } + }); + $(this).filter(':visible').find('td').each(function(index,data) { + if ($(this).css('display') != 'none'){ + var color = $(this).css('background-color'); if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += "" + parseString($(this))+ ""; + excel += parseItemData($(this),'td',color); } } }); excel += ''; - }); @@ -236,12 +222,24 @@ THE SOFTWARE.*/ var colCount=0; $(this).filter(':visible').find('td').each(function(index,data) { if ($(this).css('display') != 'none'){ + //Gets css attributes + var color = $(this).css('background-color'); if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += ""+parseString($(this))+""; + excel += parseItemData($(this),'td',color); } } colCount++; - }); + }); + $(this).filter(':visible').find('th').each(function(index,data) { + if ($(this).css('display') != 'none'){ + var color = $(this).css('background-color'); + if(defaults.ignoreColumn.indexOf(index) == -1){ + excel += parseItemData($(this),'th',color); + } + } + colCount++; + }); + rowCount++; excel += ''; }); @@ -274,7 +272,7 @@ THE SOFTWARE.*/ excelFile += excel; excelFile += ""; excelFile += ""; - + var base64data = "base64," + $.base64.encode(excelFile); window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); @@ -352,8 +350,37 @@ THE SOFTWARE.*/ return content_data; } - + + // Analize row attributes and compose new with them + function parseItemData(item, type,color){ + var spanAtt = false; + var fila = item[0]; + var excelTd=""; + if(color == 'transparent'){ + color = 'rgb(255,255,255)'; + } + //Check for attributes + for(var i=0; i<= fila.attributes.length -1 ; i++){ + if(fila.attributes[i].name == "colspan" || fila.attributes[i].name == "rowspan"){ + excelTd += '<' + type + ' '+fila.attributes[i].name + ' = ' + fila.attributes[i].nodeValue + ' bgcolor = "'+ rgb2hex(color) +'">' + parseString($(item))+ ''; + spanAtt = true; + break; + } + } + if(!spanAtt){ + excelTd += '<' + type + ' bgcolor = "' + rgb2hex(color) + '" >' + parseString($(item)) + ''; + } + return excelTd; + } + + function rgb2hex(rgb){ + rgb = rgb.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i); + return (rgb && rgb.length === 4) ? "#" + + ("0" + parseInt(rgb[1],10).toString(16)).slice(-2) + + ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) + + ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : ''; + } } }); })(jQuery); - + \ No newline at end of file From 5c8f44812e18b529ba8d223211ece4c579036971 Mon Sep 17 00:00:00 2001 From: Carlos Cabanes Date: Tue, 10 Mar 2015 11:16:23 +0100 Subject: [PATCH 2/3] Include support for change excel export name message --- tableExport.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tableExport.js b/tableExport.js index 29bd2074..4f5644a5 100644 --- a/tableExport.js +++ b/tableExport.js @@ -273,8 +273,11 @@ excelFile += ""; excelFile += ""; + //Export function var base64data = "base64," + $.base64.encode(excelFile); - window.open('data:application/vnd.ms-'+defaults.type+';filename=exportData.doc;' + base64data); + var extension = 'xls'; + openDownloadLink("Export" + '.' + extension, 'data:application/vnd.ms-'+defaults.type+';charset=utf-8;filename='+defaults.filename+'.'+extension+';' + base64data); + }else if(defaults.type == 'png'){ html2canvas($(el), { @@ -380,6 +383,16 @@ ("0" + parseInt(rgb[2],10).toString(16)).slice(-2) + ("0" + parseInt(rgb[3],10).toString(16)).slice(-2) : ''; } + + function openDownloadLink(filename, href) { + var a = document.createElement('a'); + document.body.appendChild(a); + a.style = 'display: none'; + a.href = href; + a.download = filename; + a.click(); + document.body.removeChild(a); + } } }); })(jQuery); From 17852da8eaa650b41ec3ca1a40cd5945c0cad4ac Mon Sep 17 00:00:00 2001 From: Carlos Cabanes Date: Tue, 10 Mar 2015 12:17:55 +0100 Subject: [PATCH 3/3] Th and td improvement --- tableExport.js | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/tableExport.js b/tableExport.js index 4f5644a5..eb266790 100644 --- a/tableExport.js +++ b/tableExport.js @@ -195,7 +195,7 @@ // Header $(el).find('thead').find('tr').each(function() { excel += ""; - $(this).filter(':visible').find('th').each(function(index,data) { + $(this).filter(':visible').find('th, td').each(function(index,data) { if ($(this).css('display') != 'none'){ var color = $(this).css('background-color'); if(defaults.ignoreColumn.indexOf(index) == -1){ @@ -203,14 +203,6 @@ } } }); - $(this).filter(':visible').find('td').each(function(index,data) { - if ($(this).css('display') != 'none'){ - var color = $(this).css('background-color'); - if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += parseItemData($(this),'td',color); - } - } - }); excel += ''; }); @@ -220,7 +212,7 @@ $(el).find('tbody').find('tr').each(function() { excel += ""; var colCount=0; - $(this).filter(':visible').find('td').each(function(index,data) { + $(this).filter(':visible').find('td, th').each(function(index,data) { if ($(this).css('display') != 'none'){ //Gets css attributes var color = $(this).css('background-color'); @@ -230,15 +222,7 @@ } colCount++; }); - $(this).filter(':visible').find('th').each(function(index,data) { - if ($(this).css('display') != 'none'){ - var color = $(this).css('background-color'); - if(defaults.ignoreColumn.indexOf(index) == -1){ - excel += parseItemData($(this),'th',color); - } - } - colCount++; - }); + rowCount++; excel += '';