UNPKG

amotify-ex-spreadsheet

Version:
2 lines (1 loc) 2.82 kB
var u=(e,r,o)=>new Promise((t,l)=>{var h=n=>{try{s(o.next(n))}catch(i){l(i)}},a=n=>{try{s(o.throw(n))}catch(i){l(i)}},s=n=>n.done?t(n.value):Promise.resolve(n.value).then(h,a);s((o=o.apply(e,r)).next())});import m from"@mingoo/jsmin";const g=e=>new Promise((r,o)=>{if(!e)return r(!1);let t=document.createElement("script");t.src=e,t.addEventListener("load",()=>{r(!0)}),m(document.body).append(t)}),c=(e,r)=>new c.fn.SpreadSheet(e,r);c.fn=c.prototype={},m.extend({SpreadSheet:function(e,r){this.ok=!0;try{this.workbook=e,this.fileName=r}catch(o){console.log(o),this.ok=!1}return this},isValid:function(){return this.ok},getWorkbook:function(){return this.workbook},updateFileName:function(e){return this.fileName=e,this},updateMerges:function(e){this.toJson().sheets.forEach((o,t)=>{let l=e[t];const h=this.workbook.getWorksheet(o.sheetName);for(let[a,s]of l)a&&s&&h.mergeCells(a+":"+s)})},updateStyles:function(e){this.toJson().sheets.forEach((o,t)=>{let l=e[t];const h=this.workbook.getWorksheet(o.sheetName);for(let a of l){let{type:s,target:n,values:i}=a,d=m.flatArray(n);for(let[p,f]of Object.entries(i))for(let y of d)h["get"+s.toCapital()](y)[p]=f}})},toJson:function(){let e={fileName:this.fileName,sheets:[]};return this.workbook.eachSheet((r,o)=>{let t={sheetName:r.name,rows:[]};r.eachRow((l,h)=>{let[a,...s]=l.values;t.rows.push(s)}),e.sheets.push(t)}),e},export:function(e){return u(this,null,function*(){e=e||"xlsx";const r=yield this.workbook[e].writeBuffer(),o=new Blob([r],{type:"application/octet-binary"}),t=document.createElement("a");t.href=(global.URL||global.webkitURL).createObjectURL(o),t.download=this.fileName+"."+e,t.click(),t.remove()})}},c),c.fn.SpreadSheet.prototype=c.fn,c.fn.SpreadSheet.prototype.constructor=c;let S="https://cdn.amotify.com/@env/@foreign/exceljs.min.js";const k={readFromFile:e=>u(void 0,null,function*(){if(global.ExcelJS||(yield g(S)),!global.ExcelJS)return;let r=e.type,o=e.name.replace(/\.[^/.]+$/,"");if(r.match(/csv/)){const a=(yield e.text()).split(/[\n\r]+/).map(i=>i.split(/,/)),s=new ExcelJS.Workbook,n=s.addWorksheet("Sheet1");return a.forEach(i=>n.addRow(i)),c(s,o)}const t=new ExcelJS.Workbook,l=(yield e.convert("binaryString")).target.result;return c(yield t.xlsx.load(l),o)}),readFromJson:e=>u(void 0,null,function*(){if(global.ExcelJS||(yield g(S)),!global.ExcelJS)return;let{sheets:r,fileName:o=""}=e;o=o||m.uuid.gen();const t=new ExcelJS.Workbook;return r.forEach((l,h)=>{let{sheetName:a,rows:s,merges:n=[],styles:i=[]}=l;a=a||"\u30B7\u30FC\u30C8"+(h+1),t.addWorksheet(a);const d=t.getWorksheet(a);d.addRows(s);for(let[p,f]of n)p&&f&&d.mergeCells(p+":"+f);for(let p of i){let{type:f,target:y,values:w}=p,b=m.flatArray(y);for(let[x,v]of Object.entries(w))for(let J of b)d["get"+f.toCapital()](J)[x]=v}}),c(t,o)})};export{k as SpreadSheet,k as default};