mirror of
https://github.com/501351981/vue-office.git
synced 2025-06-15 07:00:00 +08:00
2 lines
3.2 KiB
JavaScript
2 lines
3.2 KiB
JavaScript
import{l as R,r as B,S as T,a as m,g as D,b as $,t as L,c as k}from"./hack-8228c08b.js";import{d as q}from"./url-de9b02cf.js";import{d as F,_ as V,r as w,o as W,n as j,h as I,w as M,a as y,c as N,b as P,i as A,e as H,f as U,j as z,g as G,u as J}from"./index-56333d8c.js";import{P as K,u as O}from"./PreviewWrapper-15dbf1a8.js";import"./_commonjs-dynamic-modules-302442b1.js";const E={minColLength:20},Q=F({name:"VueOfficeExcel",props:{src:[String,ArrayBuffer,Blob],requestOptions:{type:Object,default:()=>({})},options:{type:Object,default:()=>({...E})}},emits:["rendered","error"],setup(e,{emit:f}){const c=w(null),n=w(null);let s={_worksheets:[]},r=[],o=0,d=null,t=null,l=null,_=null;function p(i){_=i,$(i).then(u=>{if(!u._worksheets||u._worksheets.length===0)throw new Error("未获取到数据,可能文件格式不正确或文件已损坏");let{workbookData:h,medias:g,workbookSource:a}=L(u,{...E,...e.options});e.options.transformData&&typeof e.options.transformData=="function"&&(h=e.options.transformData(h)),r=g,s=a,l=null,o=0,k(),t.loadData(h),m(d,r,s._worksheets[o],l,e.options),f("rendered")}).catch(u=>{console.warn(u),r=[],s={_worksheets:[]},k(),t&&t.loadData({}),f("error",u)})}const v=R.debounce(B,200).bind(this,n.value),b=new MutationObserver(v),S={attributes:!0,childList:!0,subtree:!0};W(()=>{j(()=>{b.observe(n.value,S),v(n),t=new T(n.value,{mode:"read",showToolbar:!1,showContextmenu:e.options.showContextmenu||!1,view:{height:()=>c.value&&c.value.clientHeight||300,width:()=>c.value&&c.value.clientWidth||1200},row:{height:24,len:100},col:{len:26,width:80,indexWidth:60,minWidth:60},autoFocus:!1}).loadData({});let i=t.bottombar.swapFunc;t.bottombar.swapFunc=function(a){i.call(t.bottombar,a),o=a,setTimeout(()=>{t.reRender(),m(d,r,s._worksheets[o],l,e.options)})};let u=t.sheet.editor.clear;t.sheet.editor.clear=function(...a){u.apply(t.sheet.editor,a),setTimeout(()=>{m(d,r,s._worksheets[o],l,e.options)})};let h=t.sheet.editor.setOffset;t.sheet.editor.setOffset=function(...a){h.apply(t.sheet.editor,a),l=a[0],m(d,r,s._worksheets[o],l,e.options)},d=n.value.querySelector("canvas").getContext("2d"),e.src&&D(e.src,e.requestOptions).then(p).catch(a=>{t.loadData({}),f("error",a)})})}),I(()=>{b.disconnect(),t=null}),M(()=>e.src,()=>{e.src?D(e.src,e.requestOptions).then(p).catch(i=>{t.loadData({}),f("error",i)}):t.loadData({})});function C(i){q(i||`vue-office-excel-${new Date().getTime()}.xlsx`,_)}return{wrapperRef:c,rootRef:n,save:C}}}),X={class:"vue-office-excel",ref:"wrapperRef"},Y={class:"vue-office-excel-main",ref:"rootRef"};function Z(e,f,c,n,s,r){return y(),N("div",X,[P("div",Y,null,512)],512)}const x=V(Q,[["render",Z]]);x.install=function(e){e.component(x.name,x)};const se={__name:"ExcelDemo",setup(e){function f(){O.hideLoading()}function c(o){console.log("出差",o),O.hideLoading()}function n(o){return console.log("transformData",o),o}const s=location.origin+(location.pathname+"/").replace("//","/")+"static/test-files/test.xlsx",r=w();return(o,d)=>{const t=A("loading");return y(),H(K,{accept:".xlsx",placeholder:"请输入xlsx文件地址","default-src":s},{default:U(l=>[z(G(J(x),{ref_key:"docxRef",ref:r,src:l.src,options:{transformData:n},style:{flex:"1",height:"0"},onRendered:f,onError:c},null,8,["src","options"]),[[t,!0]])]),_:1})}}};export{se as default};
|