add: js-excel 增加options.transformData配置支持

This commit is contained in:
liyulin 2024-02-25 21:27:19 +08:00
parent a7ac196fcf
commit 07f7966252
15 changed files with 334 additions and 3 deletions

View File

@ -1,7 +1,7 @@
{
"name": "@js-preview/excel",
"type" :"module",
"version": "1.6.1",
"version": "1.6.2",
"description": "",
"main": "lib/index.js",
"files": [

View File

@ -96,7 +96,10 @@ class JsExcelPreview {
if (!workbook._worksheets || workbook._worksheets.length === 0) {
throw new Error('未获取到数据,可能文件格式不正确或文件已损坏');
}
const {workbookData, medias, workbookSource} = transferExcelToSpreadSheet(workbook, this.options);
let {workbookData, medias, workbookSource} = transferExcelToSpreadSheet(workbook, this.options);
if(this.options.transformData && typeof this.options.transformData === 'function' ){
workbookData = this.options.transformData(workbookData);
}
this.mediasSource = medias;
this.workbookDataSource = workbookSource;
this.offset = null;

View File

@ -11,6 +11,10 @@ import '../../packages/js-excel/index.css';
const dom = ref(null);
onMounted(() => {
window.myExcelPreview = jsExcel.init(dom.value, {
transformData: function (workBook){
console.log('transformData', workBook);
return workBook;
}
});
window.myExcelPreview.preview('/vue-office/examples/dist/static/test-files/test.xlsx').then(_=>{

View File

@ -0,0 +1 @@
import{d as r}from"./docx-c1a3d6d9.js";import{d as p}from"./url-de9b02cf.js";import{d as x,_ as l,r as u,o as h,w as v,a as _,c as g,b as w,e as y,f as D,g as B,u as O}from"./index-56333d8c.js";import{P as R,u as f}from"./PreviewWrapper-15dbf1a8.js";import"./_commonjs-dynamic-modules-302442b1.js";typeof window.setImmediate>"u"&&(window.setImmediate=function(e,...o){setTimeout(()=>e(o))});const $=x({name:"VueOfficeDocx",props:{src:[String,ArrayBuffer,Blob],requestOptions:{type:Object,default:()=>({})},options:{type:Object,default:()=>({})}},emits:["rendered","error"],setup(e,{emit:o}){const t=u(null);let n=null;function a(){let c=t.value;r.getData(e.src,e.requestOptions).then(async d=>{n=await r.getBlob(d),r.render(n,c,e.options).then(()=>{o("rendered")}).catch(m=>{r.render("",c,e.options),o("error",m)})}).catch(d=>{r.render("",c,e.options),o("error",d)})}h(()=>{e.src&&a()}),v(()=>e.src,()=>{e.src?a():r.render("",t.value,e.options).then(()=>{o("rendered")})});function s(c){p(c||`vue-office-docx-${new Date().getTime()}.docx`,n)}return{rootRef:t,save:s}}}),b={class:"vue-office-docx"},k={class:"vue-office-docx-main",ref:"rootRef"};function V(e,o,t,n,a,s){return _(),g("div",b,[w("div",k,null,512)])}const i=l($,[["render",V]]);i.install=function(e){e.component(i.name,i)};const E={__name:"DocxDemo",setup(e){function o(){f.hideLoading()}function t(s){console.log("出差",s),f.hideLoading()}const n=location.origin+(location.pathname+"/").replace("//","/")+"static/test-files/test.docx",a=u();return(s,c)=>(_(),y(R,{accept:".docx",placeholder:"请输入docx文件地址","default-src":n},{default:D(d=>[B(O(i),{ref_key:"docxRef",ref:a,src:d.src,style:{flex:"1",height:"0"},onRendered:o,onError:t},null,8,["src"])]),_:1}))}},N=l(E,[["__scopeId","data-v-637d9a60"]]);export{N as default};

View File

@ -0,0 +1 @@
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};

View File

@ -0,0 +1 @@
var p=Object.defineProperty;var c=(i,e,t)=>e in i?p(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var n=(i,e,t)=>(c(i,typeof e!="symbol"?e+"":e,t),t);import{d as r}from"./docx-c1a3d6d9.js";import{d as l}from"./url-de9b02cf.js";import{r as h,o as d,a as u,c as m}from"./index-56333d8c.js";import"./_commonjs-dynamic-modules-302442b1.js";typeof window.setImmediate>"u"&&(window.setImmediate=function(i,...e){setTimeout(()=>i(e))});class w{constructor(e,t={},s={}){n(this,"container",null);n(this,"wrapper",null);n(this,"wrapperMain",null);n(this,"options",{});n(this,"requestOptions",{});n(this,"fileData",null);this.container=e,this.options=t,this.requestOptions=s,this.createWrapper()}createWrapper(){this.wrapper=document.createElement("div"),this.wrapper.className="vue-office-docx",this.wrapperMain=document.createElement("div"),this.wrapperMain.className="vue-office-docx-main",this.wrapper.appendChild(this.wrapperMain),this.container.appendChild(this.wrapper)}setOptions(e){this.options=e}setRequestOptions(e){this.requestOptions=e}preview(e){return new Promise((t,s)=>{r.getData(e,this.requestOptions).then(async o=>{this.fileData=await r.getBlob(o),r.render(this.fileData,this.wrapperMain,this.options).then(()=>{t()}).catch(a=>{r.render("",this.wrapperMain,this.options),s(a)})}).catch(o=>{r.render("",this.wrapperMain,this.options),s(o)})})}save(e){l(e||`js-preview-docx-${new Date().getTime()}.docx`,this.fileData)}destroy(){this.container.removeChild(this.wrapper),this.container=null,this.wrapper=null,this.wrapperMain=null,this.options=null,this.requestOptions=null}}function f(i,e,t){return new w(i,e,t)}const v={init:f};const q={__name:"JsDocxDemo",setup(i){const e=h(null);return d(()=>{let t=v.init(e.value);t.preview("/vue-office/examples/dist/static/test-files/test.docx").then(s=>{console.log("docx preview done"),setTimeout(()=>{t.preview("/vue-office/examples/dist/static/test-files/test2.docx")},3e3)}).catch(s=>{console.log("err",s)})}),(t,s)=>(u(),m("div",{ref_key:"dom",ref:e},null,512))}};export{q as default};

View File

@ -0,0 +1 @@
var c=Object.defineProperty;var p=(o,e,t)=>e in o?c(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var s=(o,e,t)=>(p(o,typeof e!="symbol"?e+"":e,t),t);import{S as u,a as h,b as d,t as w,c as l,l as x,r as f,g as m}from"./hack-8228c08b.js";import{d as v}from"./url-de9b02cf.js";/* empty css */import{r as b,o as k,a as D,c as S}from"./index-56333d8c.js";import"./_commonjs-dynamic-modules-302442b1.js";const _={minColLength:20};class E{constructor(e,t={},r={}){s(this,"container",null);s(this,"wrapper",null);s(this,"wrapperMain",null);s(this,"options",{});s(this,"requestOptions",{});s(this,"mediasSource",[]);s(this,"workbookDataSource",{_worksheets:[]});s(this,"sheetIndex",1);s(this,"ctx",null);s(this,"xs",null);s(this,"offset",null);s(this,"observer",null);s(this,"fileData",null);this.container=e,this.options={..._,...t},this.requestOptions=r,this.createWrapper(),this.initSpreadsheet(),this.hack()}createWrapper(){this.wrapper=document.createElement("div"),this.wrapper.className="vue-office-excel",this.wrapperMain=document.createElement("div"),this.wrapperMain.className="vue-office-excel-main",this.wrapper.appendChild(this.wrapperMain),this.container.appendChild(this.wrapper)}initSpreadsheet(){this.xs=new u(this.wrapperMain,{mode:"read",showToolbar:!1,showContextmenu:this.options.showContextmenu||!1,view:{height:()=>this.wrapper&&this.wrapper.clientHeight||300,width:()=>this.wrapper&&this.wrapper.clientWidth||1200},row:{height:24,len:100},col:{len:26,width:80,indexWidth:60,minWidth:60},autoFocus:!1}).loadData({});let e=this,t=this.xs.bottombar.swapFunc;this.xs.bottombar.swapFunc=function(a){t.call(e.xs.bottombar,a),e.sheetIndex=a,setTimeout(()=>{e.xs.reRender(),h(e.ctx,e.mediasSource,e.workbookDataSource._worksheets[e.sheetIndex],e.offset)})};let r=this.xs.sheet.editor.clear;this.xs.sheet.editor.clear=function(...a){r.apply(e.xs.sheet.editor,a),setTimeout(()=>{h(e.ctx,e.mediasSource,e.workbookDataSource._worksheets[e.sheetIndex],e.offset)})};let i=this.xs.sheet.editor.setOffset;this.xs.sheet.editor.setOffset=function(...a){i.apply(e.xs.sheet.editor,a),e.offset=a[0],h(e.ctx,e.mediasSource,e.workbookDataSource._worksheets[e.sheetIndex],e.offset)};const n=this.wrapperMain.querySelector("canvas");this.ctx=n.getContext("2d")}renderExcel(e){return this.fileData=e,d(e).then(t=>{if(!t._worksheets||t._worksheets.length===0)throw new Error("未获取到数据,可能文件格式不正确或文件已损坏");let{workbookData:r,medias:i,workbookSource:n}=w(t,this.options);this.options.transformData&&typeof this.options.transformData=="function"&&(r=this.options.transformData(r)),this.mediasSource=i,this.workbookDataSource=n,this.offset=null,this.sheetIndex=0,l(),this.xs.loadData(r),h(this.ctx,this.mediasSource,this.workbookDataSource._worksheets[this.sheetIndex],this.offset)}).catch(t=>(this.mediasSource=[],this.workbookDataSource={_worksheets:[]},l(),this.xs.loadData({}),Promise.reject(t)))}hack(){const e=x.debounce(f,200).bind(this,this.wrapperMain);this.observer=new MutationObserver(e);const t={attributes:!0,childList:!0,subtree:!0};this.observer.observe(this.wrapperMain,t),e(this.wrapperMain)}setOptions(e){this.options=e}setRequestOptions(e){this.requestOptions=e}preview(e){return new Promise((t,r)=>{m(e,this.requestOptions).then(i=>{this.renderExcel(i).then(t).catch(n=>{this.xs.loadData({}),r(n)})}).catch(i=>{this.xs.loadData({}),r(i)})})}save(e){v(e||`js-preview-excel-${new Date().getTime()}.xlsx`,this.fileData)}destroy(){this.observer.disconnect(),this.container.removeChild(this.wrapper),this.container=null,this.wrapper=null,this.wrapperMain=null,this.ctx=null,this.xs=null,this.observer=null,this.options=null,this.requestOptions=null,this.mediasSource=null,this.workbookDataSource=null}}function g(o,e,t){return new E(o,e,t)}const O={init:g},T={__name:"JsExcelDemo",setup(o){const e=b(null);return k(()=>{window.myExcelPreview=O.init(e.value,{transformData:function(t){return console.log("transformData",t),t}}),window.myExcelPreview.preview("/vue-office/examples/dist/static/test-files/test.xlsx").then(t=>{console.log("excel preview done",window.myExcelPreview)}).catch(t=>{console.log("err",t)})}),(t,r)=>(D(),S("div",{ref_key:"dom",ref:e,style:{height:"calc(100vh - 50px)"}},null,512))}};export{T as default};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
import{r as d,p as C,_ as I,w as N,q as l,a as p,c as x,u as t,g as i,f as n,s as v,t as S,e as y,m,l as V,v as W,x as A,b as R}from"./index-56333d8c.js";function T(e){const s=d("url"),a=d(e),r=d(e),u=d([]);function b(w){let c=new FileReader;return c.onload=o=>{let _=o.target.result;r.value=_},c.readAsArrayBuffer(w),!1}return{type:s,inputSrc:a,src:r,fileList:u,beforeUpload:b}}let g=d(!1),k;function $(e){k=C.loading(e,0),g.value=!0}function q(){g.value===!0&&(k(),g.value=!1)}const E={loading:g,showLoading:$,hideLoading:q};function F(){return location.href.includes("test")}const j=e=>(W("data-v-6e0b6946"),e=e(),A(),e),z={class:"preview-wrapper"},D={key:0,class:"operate-area"},G=j(()=>R("div",{class:"preview-wrapper-main"},null,-1)),H={__name:"PreviewWrapper",props:{accept:String,placeholder:String,defaultSrc:String},setup(e){const s=e,{type:a,inputSrc:r,src:u,fileList:b,beforeUpload:w}=T(s.defaultSrc);return N(u,()=>{E.showLoading()},{immediate:!0}),(c,o)=>{const _=l("a-radio-button"),B=l("a-radio-group"),L=l("a-input"),h=l("a-button"),U=l("upload-outlined"),P=l("a-upload");return p(),x("div",z,[t(F)()?m("",!0):(p(),x("div",D,[i(B,{value:t(a),"onUpdate:value":o[0]||(o[0]=f=>S(a)?a.value=f:null),"button-style":"solid"},{default:n(()=>[i(_,{value:"url"},{default:n(()=>[v("远程文件地址")]),_:1}),i(_,{value:"upload"},{default:n(()=>[v("上传本地文件")]),_:1})]),_:1},8,["value"]),t(a)==="url"?(p(),y(L,{key:0,value:t(r),"onUpdate:value":o[1]||(o[1]=f=>S(r)?r.value=f:null),placeholder:s.placeholder,style:{width:"600px","margin-left":"10px"}},null,8,["value","placeholder"])):m("",!0),t(a)==="url"?(p(),y(h,{key:1,type:"primary",style:{"margin-left":"10px"},onClick:o[2]||(o[2]=f=>u.value=t(r))},{default:n(()=>[v(" 预览 ")]),_:1})):m("",!0),t(a)!=="url"?(p(),y(P,{key:2,accept:s.accept,action:"",beforeUpload:t(w),"file-list":[]},{default:n(()=>[i(h,{style:{"margin-left":"10px"}},{default:n(()=>[i(U),v(" 选择文件 ")]),_:1})]),_:1},8,["accept","beforeUpload"])):m("",!0)])),V(c.$slots,"default",{src:t(u)},void 0,!0),G])}}},K=I(H,[["__scopeId","data-v-6e0b6946"]]);export{K as P,E as u};

97
examples/dist/assets/docx-c1a3d6d9.js vendored Normal file

File diff suppressed because one or more lines are too long

96
examples/dist/assets/hack-8228c08b.js vendored Normal file

File diff suppressed because one or more lines are too long

121
examples/dist/assets/index-56333d8c.js vendored Normal file

File diff suppressed because one or more lines are too long

1
examples/dist/assets/omit-2b9bee99.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,7 @@
content="width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>@vue-office演示demo</title>
<script type="module" crossorigin src="/vue-office/examples/dist/assets/index-b2b5d1a1.js"></script>
<script type="module" crossorigin src="/vue-office/examples/dist/assets/index-56333d8c.js"></script>
<link rel="stylesheet" href="/vue-office/examples/dist/assets/index-37fb8ca9.css">
</head>
<body>