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-a38f8b6f.js";import{d as q}from"./url-de9b02cf.js";import{d as F,_ as V,r as x,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-846e3dd9.js";import{P as K,u as O}from"./PreviewWrapper-a72046cf.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(t,{emit:f}){const c=x(null),s=x(null);let r={_worksheets:[]},n=[],o=0,d=null,e=null,l=null,w=null;function _(i){w=i,$(i).then(u=>{if(!u._worksheets||u._worksheets.length===0)throw new Error("未获取到数据,可能文件格式不正确或文件已损坏");let{workbookData:h,medias:g,workbookSource:a}=L(u,{...E,...t.options});t.options.transformData&&typeof t.options.transformData=="function"&&(h=t.options.transformData(h)),n=g,r=a,l=null,o=0,k(),e.loadData(h),m(d,n,r._worksheets[o],l),f("rendered")}).catch(u=>{console.warn(u),n=[],r={_worksheets:[]},k(),e&&e.loadData({}),f("error",u)})}const v=R.debounce(B,200).bind(this,s.value),b=new MutationObserver(v),S={attributes:!0,childList:!0,subtree:!0};W(()=>{j(()=>{b.observe(s.value,S),v(s),e=new T(s.value,{mode:"read",showToolbar:!1,showContextmenu:t.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=e.bottombar.swapFunc;e.bottombar.swapFunc=function(a){i.call(e.bottombar,a),o=a,setTimeout(()=>{e.reRender(),m(d,n,r._worksheets[o],l)})};let u=e.sheet.editor.clear;e.sheet.editor.clear=function(...a){u.apply(e.sheet.editor,a),setTimeout(()=>{m(d,n,r._worksheets[o],l)})};let h=e.sheet.editor.setOffset;e.sheet.editor.setOffset=function(...a){h.apply(e.sheet.editor,a),l=a[0],m(d,n,r._worksheets[o],l)},d=s.value.querySelector("canvas").getContext("2d"),t.src&&D(t.src,t.requestOptions).then(_).catch(a=>{e.loadData({}),f("error",a)})})}),I(()=>{b.disconnect(),e=null}),M(()=>t.src,()=>{t.src?D(t.src,t.requestOptions).then(_).catch(i=>{e.loadData({}),f("error",i)}):e.loadData({})});function C(i){q(i||`vue-office-excel-${new Date().getTime()}.xlsx`,w)}return{wrapperRef:c,rootRef:s,save:C}}}),X={class:"vue-office-excel",ref:"wrapperRef"},Y={class:"vue-office-excel-main",ref:"rootRef"};function Z(t,f,c,s,r,n){return y(),N("div",X,[P("div",Y,null,512)],512)}const p=V(Q,[["render",Z]]);p.install=function(t){t.component(p.name,p)};const re={__name:"ExcelDemo",setup(t){function f(){O.hideLoading()}function c(o){console.log("出差",o),O.hideLoading()}function s(o){return console.log("transformData",o),o}const r=location.origin+(location.pathname+"/").replace("//","/")+"static/test-files/test.xlsx",n=x();return(o,d)=>{const e=A("loading");return y(),H(K,{accept:".xlsx",placeholder:"请输入xlsx文件地址","default-src":r},{default:U(l=>[z(G(J(p),{ref_key:"docxRef",ref:n,src:l.src,options:{transformData:s},style:{flex:"1",height:"0"},onRendered:f,onError:c},null,8,["src","options"]),[[e,!0]])]),_:1})}}};export{re as default};
|