UNPKG

covert-json-to-excel

Version:

传入json,可自定义表格标题名称和列数、头部名称、过滤列和绑定生成开始与成功的回调函数

2 lines (1 loc) 6.76 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).customJson2excel=e()}(this,function(){"use strict";function a(t){return(a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function o(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function l(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=new Array(e);n<e;n++)o[n]=t[n];return o}function c(t,e){var n;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return l(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?l(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var o=0,e=function(){};return{s:e,n:function(){return o>=t.length?{done:!0}:{done:!1,value:t[o++]}},e:function(t){throw t},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var r,a=!0,i=!1;return{s:function(){n=t[Symbol.iterator]()},n:function(){var t=n.next();return a=t.done,t},e:function(t){i=!0,r=t},f:function(){try{a||null==n.return||n.return()}finally{if(i)throw r}}}}return function(){function u(t){var e=t.data,n=void 0===e?[]:e,o=t.exportFields,r=void 0!==o&&o,a=t.fields,i=void 0!==a&&a,l=t.footer,c=void 0===l?[]:l,s=t.keyMap,f=void 0===s?[]:s,e=t.headstyle,o=void 0===e?{}:e,a=t.name,l=void 0===a?"excel":a,s=t.title,e=void 0===s?[]:s,a=t.type,s=void 0===a?"xls":a,a=t.onStart,a=void 0===a?function(){}:a,t=t.onSuccess,t=void 0===t?function(){}:t;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,u),this.data=n,this.exportFields=r,this.fields=i,this.footer=c,this.headstyle=o,this.keyMap=f,this.name=l,this.title=e,this.type=s,this.onStart=a,this.onSuccess=t}var t,e,n;return t=u,(e=[{key:"downloadFields",value:function(){return void 0!==this.fields?this.fields:void 0!==this.exportFields?this.exportFields:void 0}},{key:"toChsKeys",value:function(t,r){var a=[];return t.map(function(t){var e={},n=c(r);try{for(n.s();!(o=n.n()).done;){var o=o.value;o.key&&o.title&&("function"==typeof o.filter?e[o.title]=o.filter(t):e[o.title]=void 0===t[o.key]?"":t[o.key])}}catch(t){n.e(t)}finally{n.f()}a.push(e)}),a}},{key:"generate",value:function(){if(this.data.length){this.onStart();var t=this.getProcessedJson(this.data,this.downloadFields());return this.keyMap instanceof Array&&0!==this.keyMap.length&&(t=this.toChsKeys(t,this.keyMap)),"csv"==this.type?this.export(this.jsonToCSV(t),"".concat(this.name,".").concat(this.type),"application/csv"):this.export(this.jsonToXLS(t),"".concat(this.name,".").concat(this.type),"application/vnd.ms-excel")}}},{key:"download",value:function(t,e){var n=document.createElement("a"),t=window.URL.createObjectURL(t);n.href=t,n.setAttribute("download",e),n.innerHTML="downloading...",n.style.display="none",document.body.appendChild(n),setTimeout(function(){n.click(),document.body.removeChild(n),setTimeout(function(){self.URL.revokeObjectURL(n.href)},250)},66)}},{key:"export",value:function(o,r,a){var i=this;new Promise(function(t,e){var n=i.base64ToBlob(o,a);t(i.download(n,r))}).then(function(t){i.onSuccess()}).catch(function(t){})}},{key:"jsonToXLS",value:function(t){var e,o="<thead><tr>";if(0!=this.title.length){var n=c(this.title);try{for(n.s();!(r=n.n()).done;){var r=r.value;o+="<th colspan=".concat(r.colspan,">").concat(r.name)}}catch(t){n.e(t)}finally{n.f()}o+="<th></tr>"}for(e in t[0])o+='<th style="'.concat(this.headstyle[e]||"",'">').concat(e,"</th>");if(o+="</tr></thead>",o+="<tbody>",t.map(function(t,e){for(var n in o+="<tbody><tr>",t)"string"==typeof t[n]&&(t[n]=t[n].replace(/<br\/>|\n/g,"<br style='mso-data-placement:same-cell;'/>")),o+="<td style=\"mso-number-format:'@';\">".concat(t[n],"</td>");o+="</tr></tbody>"}),0!=this.footer.length){o+="<tfooter><tr>";var a=c(this.footer);try{for(a.s();!(i=a.n()).done;){var i=i.value;o+="<th colspan=".concat(i.colspan,">").concat(i.name)}}catch(t){a.e(t)}finally{a.f()}o+="<th></tr></tr></tfooter>"}return'<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\x3c!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--\x3e</head><body><table>${table}</table></body></html>'.replace("${table}",o)}},{key:"jsonToCSV",value:function(t){var e,o="";if(0!=this.title.length){var n=c(this.title);try{for(n.s();!(r=n.n()).done;){var r=r.value;o+="".concat(r.name)}}catch(t){n.e(t)}finally{n.f()}o+="\r\n"}for(e in t[0])o+=e+",";if(o=o.slice(0,o.length-1),o+="\r\n",t.map(function(t){for(var e in t){var n=t[e]+"";n.match(/[,"\n]/)&&(n='"'+n.replace(/\"/g,'""')+'"'),o+=n+","}o=o.slice(0,o.length-1),o+="\r\n"}),0!=this.footer.length){var a=c(this.footer);try{for(a.s();!(i=a.n()).done;){var i=i.value;o+="".concat(i.name)}}catch(t){a.e(t)}finally{a.f()}o+="\r\n"}return o}},{key:"getProcessedJson",value:function(t,e){var a=this.getKeys(t,e),i=[],l=this;return t.map(function(t,e){var n,o={};for(n in a){var r=a[n];o[n]=l.getNestedData(r,t)}i.push(o)}),i}},{key:"getKeys",value:function(t,e){if(e)return e;var n,o={};for(n in t[0])o[n]=n;return o}},{key:"parseExtraData",value:function(t,e){var n="";if(Array.isArray(t))for(var o=0;o<t.length;o++)n+=e.replace("${data}",t[o]);else n+=e.replace("${data}",t);return n}},{key:"callItemCallback",value:function(t,e){return"object"===a(t)&&"function"==typeof t.callback?t.callback(e):e}},{key:"getNestedData",value:function(t,e){for(var n=null,o=("object"===a(t)?t.field:t).split("."),n=e[o[0]],r=1;r<o.length;r++)n=n[o[r]];return n=null==(n=this.callItemCallback(t,n))?"":n}},{key:"base64ToBlob",value:function(t,e){for(var t=window.btoa(window.unescape(encodeURIComponent(t))),n=atob(t),o=n.length,r=new Uint8ClampedArray(o);o--;)r[o]=n.charCodeAt(o);return new Blob([r],{type:e})}}])&&o(t.prototype,e),n&&o(t,n),u}()});