julien-c's picture
julien-c HF staff
npm run build
2dfc950 verified
raw
history blame
1.85 kB
import y from"https://cdn.jsdelivr.net/npm/exceljs@4.4.0/+esm";class d{constructor(e){Object.defineProperties(this,{_:{value:e},sheetNames:{value:e.worksheets.map(t=>t.name),enumerable:!0}})}static async load(e){const t=new y.Workbook;return await t.xlsx.load(e),new d(t)}sheet(e,t){const o=typeof e=="number"?this.sheetNames[e]:this.sheetNames.includes(e=`${e}`)?e:null;if(o==null)throw new Error(`Sheet not found: ${e}`);const n=this._.getWorksheet(o);return g(n,t)}}function g(r,{range:e,headers:t}={}){let[[o,n],[i,u]]=k(e,r);const m=t?r._rows[n++]:null;let s=new Set(["#"]);for(let l=o;l<=i;l++){const c=m?p(m.findCell(l+1)):null;let a=c&&c+""||v(l);for(;s.has(a);)a+="_";s.add(a)}s=new Array(o).concat(Array.from(s));const h=new Array(u-n+1);for(let l=n;l<=u;l++){const c=h[l-n]=Object.create(null,{"#":{value:l+1}}),a=r.getRow(l+1);if(a.hasValues)for(let f=o;f<=i;f++){const w=p(a.findCell(f+1));w!=null&&(c[s[f+1]]=w)}}return h.columns=s.filter(()=>!0),h}function p(r){if(!r)return;const{value:e}=r;if(e&&typeof e=="object"&&!(e instanceof Date)){if(e.formula||e.sharedFormula)return e.result&&e.result.error?NaN:e.result;if(e.richText)return x(e);if(e.text){let{text:t}=e;return t.richText&&(t=x(t)),e.hyperlink&&e.hyperlink!==t?`${e.hyperlink} ${t}`:t}return e}return e}function x(r){return r.richText.map(e=>e.text).join("")}function k(r=":",{columnCount:e,rowCount:t}){if(r=`${r}`,!r.match(/^[A-Z]*\d*:[A-Z]*\d*$/))throw new Error("Malformed range specifier");const[[o=0,n=0],[i=e-1,u=t-1]]=r.split(":").map(A);return[[o,n],[i,u]]}function v(r){let e="";r++;do e=String.fromCharCode(64+(r%26||26))+e;while(r=Math.floor((r-1)/26));return e}function A(r){const[,e,t]=r.match(/^([A-Z]*)(\d*)$/);let o=0;if(e)for(let n=0;n<e.length;n++)o+=Math.pow(26,e.length-n-1)*(e.charCodeAt(n)-64);return[o?o-1:void 0,t?+t-1:void 0]}export{d as Workbook};