@8sheets/data-grid 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/calc/adjust.js +1 -1
- package/calc/compile.js +1 -1
- package/calc/functions/array.js +1 -1
- package/calc/functions/date.js +1 -1
- package/calc/functions/dbcs.js +1 -0
- package/calc/functions/financial.js +1 -1
- package/calc/functions/helpers.js +1 -1
- package/calc/functions/index.js +7 -1
- package/calc/functions/logical.js +1 -1
- package/calc/functions/lookup.js +1 -1
- package/calc/functions/math.js +1 -1
- package/calc/functions/stat.js +1 -1
- package/calc/functions/text.js +1 -1
- package/calc/parser.js +2 -2
- package/calc/values.js +1 -1
- package/calcx/functions/text.js +1 -1
- package/cells/axis-ops.js +1 -1
- package/cells/cf-engine.js +1 -1
- package/cells/format-tabs.js +3 -3
- package/cells/name-box.js +1 -1
- package/celltypes/dropdowns.js +1 -1
- package/core/address.js +1 -1
- package/core/autofill.js +1 -0
- package/core/cond-format.js +1 -1
- package/core/default-names.js +1 -1
- package/core/input.js +1 -1
- package/core/tables.js +1 -1
- package/core/workbook.js +1 -1
- package/i18n/en/common.js +1 -1
- package/i18n/en/shell.js +2 -1
- package/i18n/pl/common.js +1 -1
- package/i18n/pl/shell.js +2 -1
- package/io/csv.js +8 -8
- package/io/xlsx-write.js +2 -2
- package/package.json +1 -1
- package/table-view/version.js +1 -1
- package/ui/icons.js +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A data grid for the web in plain JavaScript modules with no runtime dependencies. Open a CSV or Excel file, or pass your records, and search, filter, sort, group, total, pivot and export them as CSV, Excel or PDF. Files are read in a Worker as a stream, the page keeps a bounded DOM, and a million records stay responsive. Server data plugs in through a query contract, so search, filters and totals cover the whole result.
|
|
4
4
|
|
|
5
|
-
This is version 1.0.
|
|
5
|
+
This is version 1.0.2. Components for [React](https://www.npmjs.com/package/@8sheets/data-grid-react), [Vue](https://www.npmjs.com/package/@8sheets/data-grid-vue) and [Angular](https://www.npmjs.com/package/@8sheets/data-grid-angular) build on it. For a full Excel-compatible workbook, see the Spreadsheet, [`@8sheets/core`](https://www.npmjs.com/package/@8sheets/core).
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
package/calc/adjust.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{colName as j,MAX_COLS as E,MAX_ROWS as S}from"../core/address.js";import{tokenize as O}from"./parser.js";function N(e,s,r){return e<s?e:r>0?e+r:e<s-r?null:e+r}function
|
|
1
|
+
import{colName as j,MAX_COLS as E,MAX_ROWS as S}from"../core/address.js";import{tokenize as O}from"./parser.js";function N(e,s,r){return e<s?e:r>0?e+r:e<s-r?null:e+r}function M(e,s,r,o,R){if(o>0){const k=e>=r?e+o:e,p=s>=r?Math.min(s+o,R-1):s;return k>=R?null:[k,p]}const $=r-o;if(e>=r&&s<$)return null;const y=e<r?e:e<$?r:e+o,u=s<r?s:s<$?r-1:s+o;return[y,u]}const b=(e,s,r,o)=>`${r?"$":""}${j(e)}${o?"$":""}${s+1}`;function X(e,s,r,o){const R=(i,h)=>!s.span||i>=s.span[0]&&h<=s.span[1];let $;try{$=O(e)}catch{return e}const y=o.toUpperCase(),u=s.axis==="row";let k="",p=0;const v=(i,h,d)=>{k+=e.slice(p,i)+d,p=h};for(let i=0;i<$.length;i++){const h=$[i];let d=r,c=null,A=i;h.t==="sheet"&&(c=h,d=h.v.book===null&&h.v.sheet2===null&&h.v.sheet.toUpperCase()===y,A=i+1);const l=$[A];if(l===void 0)break;if(l.t==="cell"){const t=l.v,m=$[A+1],a=$[A+2];if(m?.t==="op"&&m.v===":"&&a?.t==="cell"){const n=a.v,F=u?[Math.min(t.c,n.c),Math.max(t.c,n.c)]:[Math.min(t.r,n.r),Math.max(t.r,n.r)];if(d&&R(F[0],F[1])){const x=c?e.slice(c.s,c.e):"",f=u?M(Math.min(t.r,n.r),Math.max(t.r,n.r),s.at,s.n,S):M(Math.min(t.c,n.c),Math.max(t.c,n.c),s.at,s.n,E),C=c?c.s:l.s;if(f===null)v(C,a.e,`${x}#REF!`);else if(u){const w=t.r<=n.r;v(C,a.e,`${x}${b(t.c,w?f[0]:f[1],t.cAbs,t.rAbs)}:${b(n.c,w?f[1]:f[0],n.cAbs,n.rAbs)}`)}else{const w=t.c<=n.c;v(C,a.e,`${x}${b(w?f[0]:f[1],t.r,t.cAbs,t.rAbs)}:${b(w?f[1]:f[0],n.r,n.cAbs,n.rAbs)}`)}}i=A+2;continue}if(d&&R(u?t.c:t.r,u?t.c:t.r)){const n=N(u?t.r:t.c,s.at,s.n),F=u?S:E,x=c?c.s:l.s,f=c?e.slice(c.s,c.e):"";n===null||n>=F?v(x,l.e,`${f}#REF!`):v(x,l.e,f+(u?b(t.c,n,t.cAbs,t.rAbs):b(n,t.r,t.cAbs,t.rAbs)))}i=A}else if(l.t==="rows"&&u&&d&&!s.span){const t=l.v,m=M(t.r0,t.r1,s.at,s.n,S),a=c?c.s:l.s,n=c?e.slice(c.s,c.e):"";m===null?v(a,l.e,`${n}#REF!`):v(a,l.e,`${n}${t.a0?"$":""}${m[0]+1}:${t.a1?"$":""}${m[1]+1}`),i=A}else if(l.t==="cols"&&!u&&d&&!s.span){const t=l.v,m=M(t.c0,t.c1,s.at,s.n,E),a=c?c.s:l.s,n=c?e.slice(c.s,c.e):"";m===null?v(a,l.e,`${n}#REF!`):v(a,l.e,`${n}${t.a0?"$":""}${j(m[0])}:${t.a1?"$":""}${j(m[1])}`),i=A}else c&&(i=A-1)}return k+e.slice(p)}function _(e,s){const r=s.axis==="row",o=r?M(e.r0,e.r1,s.at,s.n,S):M(e.c0,e.c1,s.at,s.n,E);return o===null?null:r?{...e,r0:o[0],r1:o[1]}:{...e,c0:o[0],c1:o[1]}}export{X as adjustFormula,_ as adjustRange,b as cellText};
|
package/calc/compile.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{MAX_COLS as A,MAX_ROWS as d}from"../core/address.js";import{Arr as x,compareValues as C,ERR as m,Lambda as w,num as L,OMITTED as M,Ref as y,RefList as g,str as V,XlError as b}from"./values.js";import{FUNCS as E}from"./functions/registry.js";const R=5e-16;class N{constructor(e){this.cx=e,this.refs=[],this.names=[],this.tables=[],this.isVolatile=!1,this.mayArray=!1,this.external=!1,this.arrayCtx=!1,this.scopes=[],this.slots=0,this.rangeSlots=new Set}compileTop(e){const i=this.any(e),{slots:n,cx:a}=this;return n===0?i:(c,t,s)=>{const r=a.frame;a.frame=new Array(n);try{return i(c,t,s)}finally{a.frame=r}}}sheetOf(e){const{cx:i}=this;if(e==null)return t=>t;if(e.book!==null&&e.book!==void 0){const t=i.externalSheet?.(e.book,e.sheet)??null;return t===null&&(this.external=!0),()=>t}const n=e.sheet;let a=-1,c=null;return()=>(a!==i.sheetGen&&(c=i.sheetByName(n),a=i.sheetGen),c)}cellRefClosure(e){const i=this.sheetOf(e.sheet),{rAbs:n,cAbs:a,r:c,c:t}=e;return this.refs.push({sheet:e.sheet,rAbs0:n,r0:c,rAbs1:n,r1:c,cAbs0:a,c0:t,cAbs1:a,c1:t}),e.sheet?.sheet2?this.sheet3d(e.sheet,(s,r,o)=>{const h=n?c:r+c,u=a?t:o+t;return new y(s,h,u,h,u)}):(s,r,o)=>{const h=n?c:s+c,u=a?t:r+t,f=i(o);return f===null||h<0||u<0||h>=d||u>=A?m.REF:new y(f,h,u,h,u)}}sheet3d(e,i){const{cx:n}=this;return(a,c)=>{const t=n.sheetsBetween(e.sheet,e.sheet2);return t===null?m.REF:new g(t.map(s=>i(s,a,c)))}}rangeClosure(e){const{a:i,b:n}=e,a=this.sheetOf(e.sheet);this.refs.push({sheet:e.sheet,rAbs0:i.rAbs,r0:i.r,rAbs1:n.rAbs,r1:n.r,cAbs0:i.cAbs,c0:i.c,cAbs1:n.cAbs,c1:n.c});const c=(t,s,r)=>{const o=i.rAbs?i.r:s+i.r,h=i.cAbs?i.c:r+i.c,u=n.rAbs?n.r:s+n.r,f=n.cAbs?n.c:r+n.c;return o<0||h<0||u<0||f<0||o>=d||u>=d||h>=A||f>=A?m.REF:new y(t,Math.min(o,u),Math.min(h,f),Math.max(o,u),Math.max(h,f))};return e.sheet?.sheet2?this.sheet3d(e.sheet,c):(t,s,r)=>{const o=a(r);return o===null?m.REF:c(o,t,s)}}rowsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:e.a0,r0:e.r0,rAbs1:e.a1,r1:e.r1,cAbs0:!0,c0:0,cAbs1:!0,c1:A-1}),(n,a,c)=>{const t=e.a0?e.r0:n+e.r0,s=e.a1?e.r1:n+e.r1,r=i(c);return r===null||t<0||s<0||t>=d||s>=d?m.REF:new y(r,Math.min(t,s),0,Math.max(t,s),A-1)}}colsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:!0,r0:0,rAbs1:!0,r1:d-1,cAbs0:e.a0,c0:e.c0,cAbs1:e.a1,c1:e.c1}),(n,a,c)=>{const t=e.a0?e.c0:a+e.c0,s=e.a1?e.c1:a+e.c1,r=i(c);return r===null||t<0||s<0||t>=A||s>=A?m.REF:new y(r,0,Math.min(t,s),d-1,Math.max(t,s))}}any(e){const{cx:i}=this;switch(e.t){case"lit":{const n=e.v;return()=>n}case"omit":return()=>M;case"paren":return this.any(e.e);case"cell":return this.cellRefClosure(e);case"range":return this.rangeClosure(e);case"rows":return this.rowsClosure(e);case"cols":return this.colsClosure(e);case"array":{const n=x.of2D(e.rows);return()=>n}case"union":{const n=e.items.map(a=>this.any(a));return(a,c,t)=>{const s=[];for(const r of n){const o=r(a,c,t);if(o instanceof y)s.push(o);else if(o instanceof g)s.push(...o.refs);else return o instanceof b?o:m.VALUE}return new g(s)}}case"intersect":{const n=this.any(e.l),a=this.any(e.r);return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);if(r instanceof b)return r;if(o instanceof b)return o;if(!(r instanceof y)||!(o instanceof y)||r.sheet!==o.sheet)return m.VALUE;const h=Math.max(r.r0,o.r0),u=Math.min(r.r1,o.r1),f=Math.max(r.c0,o.c0),l=Math.min(r.c1,o.c1);return h>u||f>l?m.NULL:new y(r.sheet,h,f,u,l)}}case"rangeOp":{const n=this.any(e.l),a=this.any(e.r);return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);return r instanceof b?r:o instanceof b?o:!(r instanceof y)||!(o instanceof y)||r.sheet!==o.sheet?m.REF:new y(r.sheet,Math.min(r.r0,o.r0),Math.min(r.c0,o.c0),Math.max(r.r1,o.r1),Math.max(r.c1,o.c1))}}case"implicit":{const n=this.any(e.e);return(a,c,t)=>{const s=n(a,c,t);return s instanceof y?s.isCell?i.read(s.sheet,s.r0,s.c0):i.intersect(s,a,c):s instanceof x?s.data[0]:s}}case"spillRef":{const n=this.any(e.e);return(a,c,t)=>{const s=n(a,c,t);return s instanceof y?i.spillRange(s):m.REF}}case"name":return this.nameNode(e);case"table":return this.tables.push({name:e.name,spec:e.spec}),(n,a,c)=>i.resolveTable(e.name,e.spec,n,a,c);case"call":return this.call(e);case"apply":{const n=this.any(e.fn),a=e.args.map(c=>this.any(c));return(c,t,s)=>{const r=n(c,t,s);return r instanceof b?r:r instanceof w?i.invoke(r,a.map(o=>o(c,t,s)),c,t,s):m.VALUE}}default:return this.value(e)}}nameNode(e){const{cx:i}=this,n=this.lookupVar(e.name);if(n!==null){const s=n;return()=>i.frame[s]}this.names.push(e.name);const a=e.name,c=e.sheet?this.sheetOf(e.sheet):s=>s,t=e.sheet?void 0:E.get(a.toUpperCase());if(t!==void 0){const s=new w([],null,null);return s.native=t,s.label=a.toUpperCase(),(r,o,h)=>{const u=i.evalName(a,r,o,h);return u===m.NAME?s:u}}return(s,r,o)=>i.evalName(a,s,r,c(o)??o)}lookupVar(e){const i=e.toUpperCase();for(let n=this.scopes.length-1;n>=0;n--){const a=this.scopes[n].get(i);if(a!==void 0)return a}return null}deref(e){const{cx:i}=this;return this.arrayCtx?(n,a,c)=>{const t=e(n,a,c);return t instanceof y||t instanceof g?i.refToValue(t):t}:(n,a,c)=>{const t=e(n,a,c);return t instanceof y||t instanceof g?i.refAuto(t,n,a):t}}value(e){const{cx:i}=this;switch(e.t){case"lit":{const n=e.v;return()=>n}case"paren":return this.value(e.e);case"cell":{const{rAbs:n,cAbs:a,r:c,c:t}=e;if(this.refs.push({sheet:e.sheet,rAbs0:n,r0:c,rAbs1:n,r1:c,cAbs0:a,c0:t,cAbs1:a,c1:t}),e.sheet?.sheet2)return()=>m.VALUE;const s=e.sheet?this.sheetOf(e.sheet):null;return s===null?!n&&!a?(r,o,h)=>{const u=r+c,f=o+t;return u<0||f<0||u>=d||f>=A?m.REF:i.read(h,u,f)}:(r,o,h)=>{const u=n?c:r+c,f=a?t:o+t;return u<0||f<0||u>=d||f>=A?m.REF:i.read(h,u,f)}:(r,o,h)=>{const u=n?c:r+c,f=a?t:o+t,l=s(h);return l===null||u<0||f<0||u>=d||f>=A?m.REF:i.read(l,u,f)}}case"range":case"rows":case"cols":return this.arrayCtx||(this.mayArray=!0),this.deref(this.any(e));case"neg":{const n=this.value(e.e);return(a,c,t)=>{const s=n(a,c,t);return typeof s=="number"?-s:i.unary(s,r=>-r)}}case"pct":{const n=this.value(e.e);return(a,c,t)=>{const s=n(a,c,t);return typeof s=="number"?s/100:i.unary(s,r=>r/100)}}case"bin":return this.binary(e);default:return!this.arrayCtx&&this.mayYieldRange(e)&&(this.mayArray=!0),this.deref(this.any(e))}}mayYieldRange(e){switch(e.t){case"paren":return this.mayYieldRange(e.e);case"range":case"rows":case"cols":case"union":case"rangeOp":case"intersect":case"spillRef":case"apply":return!0;case"table":return!/^\[\[#This Row\],\[[^\]]+\]\]$/i.test(e.spec);case"name":{if(e.sheet)return!0;const i=this.lookupVar(e.name);return i!==null?this.rangeSlots.has(i):this.cx.nameMayYieldRange?.(e.name)??!0}case"call":{if(e.name==="LET"||e.name==="LAMBDA"||this.lookupVar(e.name)!==null)return!0;const i=E.get(e.name);return i===void 0||i.refResult?!0:i.passes?e.args.some((n,a)=>i.passes(a,e.args.length)&&this.mayYieldRange(n)):!1}default:return!1}}binary(e){const{cx:i}=this,n=this.value(e.l),a=this.value(e.r);switch(e.op){case"+":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);if(typeof r=="number"&&typeof o=="number"){const h=r+o;return Math.abs(h)<Math.abs(r)*R?0:h}return i.slowBin("+",r,o)};case"-":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);if(typeof r=="number"&&typeof o=="number"){const h=r-o;return Math.abs(h)<Math.abs(r)*R?0:h}return i.slowBin("-",r,o)};case"*":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);return typeof r=="number"&&typeof o=="number"?r*o:i.slowBin("*",r,o)};case"/":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);return typeof r=="number"&&typeof o=="number"&&o!==0?r/o:i.slowBin("/",r,o)};case"^":return(c,t,s)=>i.slowBin("^",n(c,t,s),a(c,t,s));case"&":return(c,t,s)=>i.slowBin("&",n(c,t,s),a(c,t,s));default:{const c=e.op;return(t,s,r)=>i.slowBin(c,n(t,s,r),a(t,s,r))}}}call(e){const{cx:i}=this,{name:n,args:a}=e;if(n==="LET")return this.compileLet(a);if(n==="LAMBDA")return this.compileLambda(a);const c=this.lookupVar(n);if(c!==null){const s=c,r=a.map(o=>this.any(o));return(o,h,u)=>{const f=i.frame[s];return f instanceof w?i.invoke(f,r.map(l=>l(o,h,u)),o,h,u):m.VALUE}}const t=E.get(n);if(t===void 0){this.names.push(n);const s=a.map(r=>this.any(r));return(r,o,h)=>{const u=i.evalName(n,r,o,h);return u instanceof w?i.invoke(u,s.map(f=>f(r,o,h)),r,o,h):m.NAME}}if((a.length<t.min||a.length>t.max)&&!(a.length===1&&a[0].t==="omit"&&t.min===0))return()=>m.VALUE;if(t.volatile&&(this.isVolatile=!0),t.notInExcel&&(this.notInExcel=!0),t.frozen&&(this.external=!0),t.kind==="lazy"||t.kind==="raw"){const s=this.arrayCtx;t.array&&(this.arrayCtx=!0);const r=a.map(h=>this.any(h));this.arrayCtx=s;const{fn:o}=t;return t.kind==="lazy"?(h,u,f)=>{try{return o(i,r,h,u,f)}catch(l){if(l instanceof b)return l;throw l}}:(h,u,f)=>{const l=new Array(r.length);for(let p=0;p<r.length;p++)l[p]=r[p](h,u,f);try{return o(i,l,h,u,f)}catch(p){if(p instanceof b)return p;throw p}}}return this.lifted(t,a)}lifted(e,i){const{cx:n}=this,{fn:a,errors:c}=e,t=i.map(r=>r.t==="omit"?()=>M:this.value(r)),s=t.length;if(s===1){const r=t[0];return(o,h,u)=>{const f=r(o,h,u);if(f instanceof x)return n.liftArr(a,[f],c);if(!c&&f instanceof b)return f;try{return a(f)}catch(l){if(l instanceof b)return l;throw l}}}if(s===2){const[r,o]=t;return(h,u,f)=>{const l=r(h,u,f),p=o(h,u,f);if(l instanceof x||p instanceof x)return n.liftArr(a,[l,p],c);if(!c){if(l instanceof b)return l;if(p instanceof b)return p}try{return a(l,p)}catch(v){if(v instanceof b)return v;throw v}}}return(r,o,h)=>{const u=new Array(s);let f=!1;for(let l=0;l<s;l++){const p=t[l](r,o,h);u[l]=p,p instanceof x&&(f=!0)}if(f)return n.liftArr(a,u,c);if(!c){for(let l=0;l<s;l++)if(u[l]instanceof b)return u[l]}try{return a(...u)}catch(l){if(l instanceof b)return l;throw l}}}compileLet(e){const{cx:i}=this;if(e.length<3||e.length%2===0)return()=>m.VALUE;const n=new Map;this.scopes.push(n);const a=[];for(let t=0;t+1<e.length;t+=2){const s=e[t];if(s.t!=="name")return this.scopes.pop(),()=>m.VALUE;const r=this.any(e[t+1]),o=this.slots++;this.mayYieldRange(e[t+1])&&this.rangeSlots.add(o),n.set(s.name.toUpperCase(),o),a.push(o,r)}const c=this.any(e[e.length-1]);return this.scopes.pop(),(t,s,r)=>{for(let o=0;o<a.length;o+=2)i.frame[a[o]]=a[o+1](t,s,r);return c(t,s,r)}}compileLambda(e){const{cx:i}=this;if(e.length===0)return()=>m.VALUE;const n=new Map;this.scopes.push(n);const a=[];for(let t=0;t<e.length-1;t++){const s=e[t];if(s.t!=="name")return this.scopes.pop(),()=>m.VALUE;const r=this.slots++;this.rangeSlots.add(r),n.set(s.name.toUpperCase(),r),a.push(r)}const c=this.any(e[e.length-1]);return this.scopes.pop(),()=>new w(a,c,i.frame)}}export{N as Compiler,C as compareValues,L as num,V as str};
|
|
1
|
+
import{MAX_COLS as A,MAX_ROWS as d}from"../core/address.js";import{Arr as x,compareValues as L,ERR as p,Lambda as w,num as k,OMITTED as M,Ref as m,RefList as g,REJECTED as R,str as V,XlError as b}from"./values.js";import{FUNCS as E}from"./functions/registry.js";const C=5e-16;class B{constructor(e){this.cx=e,this.refs=[],this.names=[],this.tables=[],this.isVolatile=!1,this.mayArray=!1,this.external=!1,this.arrayCtx=!1,this.scopes=[],this.slots=0,this.rangeSlots=new Set}compileTop(e){const i=this.any(e),{slots:a,cx:o}=this;return a===0?i:(c,t,s)=>{const r=o.frame;o.frame=new Array(a);try{return i(c,t,s)}finally{o.frame=r}}}sheetOf(e){const{cx:i}=this;if(e==null)return t=>t;if(e.book!==null&&e.book!==void 0){const t=i.externalSheet?.(e.book,e.sheet)??null;return t===null&&(this.external=!0),()=>t}const a=e.sheet;let o=-1,c=null;return()=>(o!==i.sheetGen&&(c=i.sheetByName(a),o=i.sheetGen),c)}cellRefClosure(e){const i=this.sheetOf(e.sheet),{rAbs:a,cAbs:o,r:c,c:t}=e;return this.refs.push({sheet:e.sheet,rAbs0:a,r0:c,rAbs1:a,r1:c,cAbs0:o,c0:t,cAbs1:o,c1:t}),e.sheet?.sheet2?this.sheet3d(e.sheet,(s,r,n)=>{const h=a?c:r+c,u=o?t:n+t;return new m(s,h,u,h,u)}):(s,r,n)=>{const h=a?c:s+c,u=o?t:r+t,l=i(n);return l===null||h<0||u<0||h>=d||u>=A?p.REF:new m(l,h,u,h,u)}}sheet3d(e,i){const{cx:a}=this;return(o,c)=>{const t=a.sheetsBetween(e.sheet,e.sheet2);return t===null?p.REF:new g(t.map(s=>i(s,o,c)))}}rangeClosure(e){const{a:i,b:a}=e,o=this.sheetOf(e.sheet);this.refs.push({sheet:e.sheet,rAbs0:i.rAbs,r0:i.r,rAbs1:a.rAbs,r1:a.r,cAbs0:i.cAbs,c0:i.c,cAbs1:a.cAbs,c1:a.c});const c=(t,s,r)=>{const n=i.rAbs?i.r:s+i.r,h=i.cAbs?i.c:r+i.c,u=a.rAbs?a.r:s+a.r,l=a.cAbs?a.c:r+a.c;return n<0||h<0||u<0||l<0||n>=d||u>=d||h>=A||l>=A?p.REF:new m(t,Math.min(n,u),Math.min(h,l),Math.max(n,u),Math.max(h,l))};return e.sheet?.sheet2?this.sheet3d(e.sheet,c):(t,s,r)=>{const n=o(r);return n===null?p.REF:c(n,t,s)}}rowsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:e.a0,r0:e.r0,rAbs1:e.a1,r1:e.r1,cAbs0:!0,c0:0,cAbs1:!0,c1:A-1}),(a,o,c)=>{const t=e.a0?e.r0:a+e.r0,s=e.a1?e.r1:a+e.r1,r=i(c);return r===null||t<0||s<0||t>=d||s>=d?p.REF:new m(r,Math.min(t,s),0,Math.max(t,s),A-1)}}colsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:!0,r0:0,rAbs1:!0,r1:d-1,cAbs0:e.a0,c0:e.c0,cAbs1:e.a1,c1:e.c1}),(a,o,c)=>{const t=e.a0?e.c0:o+e.c0,s=e.a1?e.c1:o+e.c1,r=i(c);return r===null||t<0||s<0||t>=A||s>=A?p.REF:new m(r,0,Math.min(t,s),d-1,Math.max(t,s))}}any(e){const{cx:i}=this;switch(e.t){case"lit":{const a=e.v;return()=>a}case"omit":return()=>M;case"paren":return this.any(e.e);case"cell":return this.cellRefClosure(e);case"range":return this.rangeClosure(e);case"rows":return this.rowsClosure(e);case"cols":return this.colsClosure(e);case"array":{const a=x.of2D(e.rows);return()=>a}case"union":{const a=e.items.map(o=>this.any(o));return(o,c,t)=>{const s=[];for(const r of a){const n=r(o,c,t);if(n instanceof m)s.push(n);else if(n instanceof g)s.push(...n.refs);else return n instanceof b?n:p.VALUE}return new g(s)}}case"intersect":{const a=this.any(e.l),o=this.any(e.r);return(c,t,s)=>{const r=a(c,t,s),n=o(c,t,s);if(r instanceof b)return r;if(n instanceof b)return n;if(!(r instanceof m)||!(n instanceof m)||r.sheet!==n.sheet)return p.VALUE;const h=Math.max(r.r0,n.r0),u=Math.min(r.r1,n.r1),l=Math.max(r.c0,n.c0),f=Math.min(r.c1,n.c1);return h>u||l>f?p.NULL:new m(r.sheet,h,l,u,f)}}case"rangeOp":{const a=this.any(e.l),o=this.any(e.r);return(c,t,s)=>{const r=a(c,t,s),n=o(c,t,s);return r instanceof b?r:n instanceof b?n:!(r instanceof m)||!(n instanceof m)||r.sheet!==n.sheet?p.REF:new m(r.sheet,Math.min(r.r0,n.r0),Math.min(r.c0,n.c0),Math.max(r.r1,n.r1),Math.max(r.c1,n.c1))}}case"implicit":{const a=this.any(e.e);return(o,c,t)=>{const s=a(o,c,t);return s instanceof m?s.isCell?i.read(s.sheet,s.r0,s.c0):i.intersect(s,o,c):s instanceof x?s.data[0]:s}}case"spillRef":{const a=this.any(e.e);return(o,c,t)=>{const s=a(o,c,t);return s instanceof m?i.spillRange(s):p.REF}}case"name":return this.nameNode(e);case"table":return this.tables.push({name:e.name,spec:e.spec}),(a,o,c)=>i.resolveTable(e.name,e.spec,a,o,c);case"call":return this.call(e);case"apply":{const a=this.any(e.fn),o=e.args.map(c=>this.any(c));return(c,t,s)=>{const r=a(c,t,s);return r instanceof b?r:r instanceof w?i.invoke(r,o.map(n=>n(c,t,s)),c,t,s):p.VALUE}}default:return this.value(e)}}nameNode(e){const{cx:i}=this,a=this.lookupVar(e.name);if(a!==null){const s=a;return()=>i.frame[s]}this.names.push(e.name);const o=e.name,c=e.sheet?this.sheetOf(e.sheet):s=>s,t=e.sheet?void 0:E.get(o.toUpperCase());if(t!==void 0){const s=new w([],null,null);return s.native=t,s.label=o.toUpperCase(),(r,n,h)=>{const u=i.evalName(o,r,n,h);return u===p.NAME?s:u}}return(s,r,n)=>i.evalName(o,s,r,c(n)??n)}lookupVar(e){const i=e.toUpperCase();for(let a=this.scopes.length-1;a>=0;a--){const o=this.scopes[a].get(i);if(o!==void 0)return o}return null}deref(e){const{cx:i}=this;return this.arrayCtx?(a,o,c)=>{const t=e(a,o,c);return t instanceof m||t instanceof g?i.refToValue(t):t}:(a,o,c)=>{const t=e(a,o,c);return t instanceof m||t instanceof g?i.refAuto(t,a,o):t}}value(e){const{cx:i}=this;switch(e.t){case"lit":{const a=e.v;return()=>a}case"paren":return this.value(e.e);case"cell":{const{rAbs:a,cAbs:o,r:c,c:t}=e;if(this.refs.push({sheet:e.sheet,rAbs0:a,r0:c,rAbs1:a,r1:c,cAbs0:o,c0:t,cAbs1:o,c1:t}),e.sheet?.sheet2)return()=>p.VALUE;const s=e.sheet?this.sheetOf(e.sheet):null;return s===null?!a&&!o?(r,n,h)=>{const u=r+c,l=n+t;return u<0||l<0||u>=d||l>=A?p.REF:i.read(h,u,l)}:(r,n,h)=>{const u=a?c:r+c,l=o?t:n+t;return u<0||l<0||u>=d||l>=A?p.REF:i.read(h,u,l)}:(r,n,h)=>{const u=a?c:r+c,l=o?t:n+t,f=s(h);return f===null||u<0||l<0||u>=d||l>=A?p.REF:i.read(f,u,l)}}case"range":case"rows":case"cols":return this.arrayCtx||(this.mayArray=!0),this.deref(this.any(e));case"neg":{const a=this.value(e.e);return(o,c,t)=>{const s=a(o,c,t);return typeof s=="number"?-s:i.unary(s,r=>-r)}}case"pct":{const a=this.value(e.e);return(o,c,t)=>{const s=a(o,c,t);return typeof s=="number"?s/100:i.unary(s,r=>r/100)}}case"bin":return this.binary(e);default:return!this.arrayCtx&&this.mayYieldRange(e)&&(this.mayArray=!0),this.deref(this.any(e))}}mayYieldRange(e){switch(e.t){case"paren":return this.mayYieldRange(e.e);case"range":case"rows":case"cols":case"union":case"rangeOp":case"intersect":case"spillRef":case"apply":return!0;case"table":return!/^\[\[#This Row\],\[[^\]]+\]\]$/i.test(e.spec);case"name":{if(e.sheet)return!0;const i=this.lookupVar(e.name);return i!==null?this.rangeSlots.has(i):this.cx.nameMayYieldRange?.(e.name)??!0}case"call":{if(e.name==="LET"||e.name==="LAMBDA"||this.lookupVar(e.name)!==null)return!0;const i=E.get(e.name);return i===void 0||i.refResult?!0:i.passes?e.args.some((a,o)=>i.passes(o,e.args.length)&&this.mayYieldRange(a)):!1}default:return!1}}binary(e){const{cx:i}=this,a=this.value(e.l),o=this.value(e.r);switch(e.op){case"+":return(c,t,s)=>{const r=a(c,t,s),n=o(c,t,s);if(typeof r=="number"&&typeof n=="number"){const h=r+n;return Math.abs(h)<Math.abs(r)*C?0:h}return i.slowBin("+",r,n)};case"-":return(c,t,s)=>{const r=a(c,t,s),n=o(c,t,s);if(typeof r=="number"&&typeof n=="number"){const h=r-n;return Math.abs(h)<Math.abs(r)*C?0:h}return i.slowBin("-",r,n)};case"*":return(c,t,s)=>{const r=a(c,t,s),n=o(c,t,s);return typeof r=="number"&&typeof n=="number"?r*n:i.slowBin("*",r,n)};case"/":return(c,t,s)=>{const r=a(c,t,s),n=o(c,t,s);return typeof r=="number"&&typeof n=="number"&&n!==0?r/n:i.slowBin("/",r,n)};case"^":return(c,t,s)=>i.slowBin("^",a(c,t,s),o(c,t,s));case"&":return(c,t,s)=>i.slowBin("&",a(c,t,s),o(c,t,s));default:{const c=e.op;return(t,s,r)=>i.slowBin(c,a(t,s,r),o(t,s,r))}}}call(e){const{cx:i}=this,{name:a,args:o}=e;if(a==="LET")return this.compileLet(o);if(a==="LAMBDA")return this.compileLambda(o);const c=this.lookupVar(a);if(c!==null){const s=c,r=o.map(n=>this.any(n));return(n,h,u)=>{const l=i.frame[s];return l instanceof w?i.invoke(l,r.map(f=>f(n,h,u)),n,h,u):p.VALUE}}const t=E.get(a);if(t===void 0){this.names.push(a);const s=o.map(r=>this.any(r));return(r,n,h)=>{const u=i.evalName(a,r,n,h);return u instanceof w?i.invoke(u,s.map(l=>l(r,n,h)),r,n,h):p.NAME}}if((o.length<t.min||o.length>t.max)&&!(o.length===1&&o[0].t==="omit"&&t.min===0))return()=>p.VALUE;if(t.volatile&&(this.isVolatile=!0),t.notInExcel&&(this.notInExcel=!0),t.frozen&&(this.external=!0),t.kind==="lazy"||t.kind==="raw"){const s=this.arrayCtx;t.array&&(this.arrayCtx=!0);const r=o.map(h=>this.any(h));this.arrayCtx=s;const{fn:n}=t;return t.kind==="lazy"?(h,u,l)=>{try{return n(i,r,h,u,l)}catch(f){if(f instanceof b)return f;throw f}}:(h,u,l)=>{const f=new Array(r.length);for(let y=0;y<r.length;y++)f[y]=r[y](h,u,l);try{return n(i,f,h,u,l)}catch(y){if(y instanceof b)return y;throw y}}}return this.lifted(t,o)}toolpakArg(e,i){const{cx:a}=this,o=t=>typeof t=="boolean"&&!i?R:t;if(e.t!=="range"&&e.t!=="rows"&&e.t!=="cols"&&e.t!=="union"&&e.t!=="call"&&e.t!=="name"){const t=this.value(e);return(s,r,n)=>o(t(s,r,n))}const c=this.any(e);return(t,s,r)=>{const n=c(t,s,r);return n instanceof g||!a.legacy&&n instanceof m&&(n.r0!==n.r1||n.c0!==n.c1)?R:o(n instanceof m?a.refAuto(n,t,s):n)}}lifted(e,i){const{cx:a}=this,{fn:o,errors:c}=e,t=i.map((r,n)=>r.t==="omit"?()=>M:e.toolpak?this.toolpakArg(r,e.toolpak.logical.includes(n)):this.value(r)),s=t.length;if(s===1){const r=t[0];return(n,h,u)=>{const l=r(n,h,u);if(l instanceof x)return a.liftArr(o,[l],c);if(!c&&l instanceof b)return l;try{return o(l)}catch(f){if(f instanceof b)return f;throw f}}}if(s===2){const[r,n]=t;return(h,u,l)=>{const f=r(h,u,l),y=n(h,u,l);if(f instanceof x||y instanceof x)return a.liftArr(o,[f,y],c);if(!c){if(f instanceof b)return f;if(y instanceof b)return y}try{return o(f,y)}catch(v){if(v instanceof b)return v;throw v}}}return(r,n,h)=>{const u=new Array(s);let l=!1;for(let f=0;f<s;f++){const y=t[f](r,n,h);u[f]=y,y instanceof x&&(l=!0)}if(l)return a.liftArr(o,u,c);if(!c){for(let f=0;f<s;f++)if(u[f]instanceof b)return u[f]}try{return o(...u)}catch(f){if(f instanceof b)return f;throw f}}}compileLet(e){const{cx:i}=this;if(e.length<3||e.length%2===0)return()=>p.VALUE;const a=new Map;this.scopes.push(a);const o=[];for(let t=0;t+1<e.length;t+=2){const s=e[t];if(s.t!=="name")return this.scopes.pop(),()=>p.VALUE;const r=this.any(e[t+1]),n=this.slots++;this.mayYieldRange(e[t+1])&&this.rangeSlots.add(n),a.set(s.name.toUpperCase(),n),o.push(n,r)}const c=this.any(e[e.length-1]);return this.scopes.pop(),(t,s,r)=>{for(let n=0;n<o.length;n+=2)i.frame[o[n]]=o[n+1](t,s,r);return c(t,s,r)}}compileLambda(e){const{cx:i}=this;if(e.length===0)return()=>p.VALUE;const a=new Map;this.scopes.push(a);const o=[];for(let t=0;t<e.length-1;t++){const s=e[t];if(s.t!=="name")return this.scopes.pop(),()=>p.VALUE;const r=this.slots++;this.rangeSlots.add(r),a.set(s.name.toUpperCase(),r),o.push(r)}const c=this.any(e[e.length-1]);return this.scopes.pop(),()=>new w(o,c,i.frame)}}export{B as Compiler,L as compareValues,k as num,V as str};
|
package/calc/functions/array.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as
|
|
1
|
+
import{setCategory as X}from"./registry.js";import{lift as D,raw as m}from"./registry.js";import{Arr as p,bool as _,compareValues as j,ERR as d,num as y,OMITTED as R,Ref as T,str as G,XlError as M}from"../values.js";import{MAX_COLS as K,MAX_ROWS as Y}from"../../core/address.js";X("Lookup & Reference");const C=o=>o===void 0||o===R,L=(o,t,s,c,r)=>C(t)?r:Math.trunc(y(o.scalar(t,s,c))),S=(o,t,s,c,r)=>C(t)?r:_(o.scalar(t,s,c)),O=o=>Array.from({length:o.rows},(t,s)=>o.data.slice(s*o.cols,(s+1)*o.cols)),N=o=>Array.from({length:o.cols},(t,s)=>Array.from({length:o.rows},(c,r)=>o.get(r,s))),g=o=>o===null?0:o;D("SEQUENCE",1,4,(o,t,s,c)=>{const r=Math.trunc(y(o)),n=t===void 0||t===R?1:Math.trunc(y(t)),e=s===void 0||s===R?1:y(s),a=c===void 0||c===R?1:y(c);if(r<0||n<0)throw d.VALUE;if(r===0||n===0)throw d.CALC;if(r*n>1<<27)throw d.NUM;const f=new Array(r*n);for(let l=0;l<f.length;l++)f[l]=e+l*a;return new p(r,n,f)}),D("RANDARRAY",0,5,(o,t,s,c,r)=>{const n=o===void 0||o===R?1:Math.trunc(y(o)),e=t===void 0||t===R?1:Math.trunc(y(t)),a=s===void 0||s===R?0:y(s),f=c===void 0||c===R?1:y(c),l=r===void 0||r===R?!1:_(r);if(n<0||e<0||a>f)throw d.VALUE;if(n===0||e===0)throw d.CALC;const i=new Array(n*e);for(let A=0;A<i.length;A++)i[A]=l?Math.ceil(a)+Math.floor(Math.random()*(Math.floor(f)-Math.ceil(a)+1)):a+Math.random()*(f-a);return new p(n,e,i)},{volatile:!0}),m("FILTER",2,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=o.toArr(t[1]),e=n.cols===1&&n.rows===r.rows,a=n.rows===1&&n.cols===r.cols;if(!e&&!a)throw d.VALUE;const f=n.data.map(u=>{if(u instanceof M)throw u;return u===null?!1:_(u)}),l=e?O(r).filter((u,h)=>f[h]):null,i=e?null:N(r).filter((u,h)=>f[h]);if(e?l.length===0:i.length===0){if(C(t[2]))throw d.CALC;return t[2]}if(e)return p.of2D(l.map(u=>u.map(g)));const w=new p(r.rows,i.length,new Array(r.rows*i.length));return i.forEach((u,h)=>u.forEach((E,V)=>{w.data[V*i.length+h]=g(E)})),w},{array:!0,passes:o=>o===2});function b(o,t){const s=o[0].length,c=Array.from({length:s},(r,n)=>n);return c.sort((r,n)=>{for(let e=0;e<o.length;e++){const a=o[e][r],f=o[e][n],l=a==null||a instanceof M,i=f==null||f instanceof M;if(l||i){if(l&&i)continue;return l?1:-1}const A=j(a,f);if(A!==0)return A*t[e]}return r-n}),c}m("SORT",1,4,(o,t,s,c)=>{const r=o.toArr(t[0]),n=S(o,t[3],s,c,!1),e=C(t[1])?[1]:o.toArr(t[1]).data.map(h=>Math.trunc(y(h))),a=C(t[2])?e.map(()=>1):o.toArr(t[2]).data.map(h=>y(h));if(a.length!==e.length||a.some(h=>h!==1&&h!==-1))throw d.VALUE;const f=n?N(r):O(r),l=n?r.rows:r.cols;for(const h of e)if(h<1||h>l)throw d.VALUE;const i=e.map(h=>f.map(E=>E[h-1])),w=b(i,a).map(h=>f[h]);if(!n)return p.of2D(w.map(h=>h.map(g)));const u=new Array(r.rows*r.cols);return w.forEach((h,E)=>h.forEach((V,$)=>{u[$*r.cols+E]=g(V)})),new p(r.rows,r.cols,u)},{array:!0}),m("SORTBY",2,255,(o,t,s,c)=>{const r=o.toArr(t[0]),n=[],e=[];let a=1;for(;a<t.length;){const w=o.toArr(t[a]),u=a+1<t.length&&!C(t[a+1])&&!(t[a+1]instanceof T)&&!(t[a+1]instanceof p);let h=1,E=1;if(u?(h=y(o.scalar(t[a+1],s,c)),E=2):a+1<t.length&&C(t[a+1])&&(E=2),h!==1&&h!==-1)throw d.VALUE;n.push(w),e.push(h),a+=E}const f=n[0].cols===1;for(const w of n)if(f?w.rows!==r.rows||w.cols!==1:w.cols!==r.cols||w.rows!==1)throw d.VALUE;const l=b(n.map(w=>w.data),e);if(f){const w=O(r);return p.of2D(l.map(u=>w[u].map(g)))}const i=N(r),A=new Array(r.rows*r.cols);return l.forEach((w,u)=>i[w].forEach((h,E)=>{A[E*r.cols+u]=g(h)})),new p(r.rows,r.cols,A)},{array:!0});const k=o=>o.map(t=>t===null?"\0":typeof t=="string"?`s${t.toLowerCase()}`:t instanceof M?`e${t.code}`:`${typeof t}${t}`).join("");m("UNIQUE",1,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=S(o,t[1],s,c,!1),e=S(o,t[2],s,c,!1),a=n?N(r):O(r),f=new Map;for(const w of a){const u=k(w);f.set(u,(f.get(u)??0)+1)}const l=new Set,i=[];for(const w of a){const u=k(w);(e?f.get(u)!==1:l.has(u))||(l.add(u),i.push(w))}if(i.length===0)throw d.CALC;if(!n)return p.of2D(i.map(w=>w.map(g)));const A=new Array(r.rows*i.length);return i.forEach((w,u)=>w.forEach((h,E)=>{A[E*i.length+u]=g(h)})),new p(r.rows,i.length,A)},{array:!0}),m("TRANSPOSE",1,1,(o,t)=>{const s=o.toArr(t[0]),c=new Array(s.data.length);for(let r=0;r<s.rows;r++)for(let n=0;n<s.cols;n++)c[n*s.rows+r]=g(s.get(r,n));return new p(s.cols,s.rows,c)},{array:!0});function I(o,t,s){const c=t.map(f=>o.toArr(f));if(!s){const f=Math.max(...c.map(i=>i.cols)),l=[];for(const i of c)for(let A=0;A<i.rows;A++){const w=new Array(f);for(let u=0;u<f;u++)w[u]=u<i.cols?g(i.get(A,u)):d.NA;l.push(w)}return p.of2D(l)}const r=Math.max(...c.map(f=>f.rows)),n=c.reduce((f,l)=>f+l.cols,0),e=new Array(r*n);let a=0;for(const f of c){for(let l=0;l<f.cols;l++)for(let i=0;i<r;i++)e[i*n+a+l]=i<f.rows?g(f.get(i,l)):d.NA;a+=f.cols}return new p(r,n,e)}m("VSTACK",1,254,(o,t)=>I(o,t,!1),{array:!0}),m("HSTACK",1,254,(o,t)=>I(o,t,!0),{array:!0});function H(o,t,s,c,r){const n=s-t+1,e=r-c+1,a=new Array(n*e);for(let f=0;f<n;f++)for(let l=0;l<e;l++)a[f*e+l]=g(o.get(t+f,c+l));return new p(n,e,a)}m("TAKE",2,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,r.rows),e=L(o,t[2],s,c,r.cols);if(n===0||e===0)throw d.CALC;const a=Math.min(Math.abs(n),r.rows),f=Math.min(Math.abs(e),r.cols),l=n<0?r.rows-a:0,i=e<0?r.cols-f:0;return H(r,l,l+a-1,i,i+f-1)},{array:!0}),m("DROP",2,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,0),e=L(o,t[2],s,c,0),a=r.rows-Math.abs(n),f=r.cols-Math.abs(e);if(a<=0||f<=0)throw d.CALC;const l=n>0?n:0,i=e>0?e:0;return H(r,l,l+a-1,i,i+f-1)},{array:!0}),m("EXPAND",2,4,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,r.rows),e=L(o,t[2],s,c,r.cols),a=C(t[3])?d.NA:o.scalar(t[3],s,c);if(n<r.rows||e<r.cols||n>Y||e>K)throw d.VALUE;const f=new Array(n*e);for(let l=0;l<n;l++)for(let i=0;i<e;i++)f[l*e+i]=l<r.rows&&i<r.cols?g(r.get(l,i)):a;return new p(n,e,f)},{array:!0});function F(o,t,s,c,r){const n=o.toArr(t[0]),e=r?O(n):N(n),a=[];for(let l=1;l<t.length;l++)for(const i of o.toArr(t[l]).data){const A=Math.trunc(y(i)),w=A<0?e.length+A:A-1;if(A===0||w<0||w>=e.length)throw d.VALUE;a.push(e[w])}if(r)return p.of2D(a.map(l=>l.map(g)));const f=new Array(n.rows*a.length);return a.forEach((l,i)=>l.forEach((A,w)=>{f[w*a.length+i]=g(A)})),new p(n.rows,a.length,f)}m("CHOOSEROWS",2,255,(o,t,s,c)=>F(o,t,s,c,!0),{array:!0}),m("CHOOSECOLS",2,255,(o,t,s,c)=>F(o,t,s,c,!1),{array:!0});function P(o,t,s,c){const r=o.toArr(t[0]),n=L(o,t[1],s,c,0),e=S(o,t[2],s,c,!1);if(n<0||n>3)throw d.VALUE;const a=[],f=l=>{(n===1||n===3)&&l===null||(n===2||n===3)&&l instanceof M||a.push(g(l))};if(e)for(let l=0;l<r.cols;l++)for(let i=0;i<r.rows;i++)f(r.get(i,l));else for(const l of r.data)f(l);if(a.length===0)throw d.CALC;return a}m("TOCOL",1,3,(o,t,s,c)=>p.column(P(o,t,s,c)),{array:!0}),m("TOROW",1,3,(o,t,s,c)=>p.row(P(o,t,s,c)),{array:!0});function W(o,t,s,c,r){const n=o.toArr(t[0]);if(n.rows>1&&n.cols>1)throw d.VALUE;const e=L(o,t[1],s,c,0);if(e<1)throw d.NUM;const a=C(t[2])?d.NA:o.scalar(t[2],s,c),f=n.data.length,l=Math.ceil(f/e),i=r?l:e,A=r?e:l,w=new Array(i*A);for(let u=0;u<i*A;u++){const h=u<f?g(n.data[u]):a;r?w[u]=h:w[u%e*A+Math.floor(u/e)]=h}return new p(i,A,w)}m("WRAPROWS",2,3,(o,t,s,c)=>W(o,t,s,c,!0),{array:!0}),m("WRAPCOLS",2,3,(o,t,s,c)=>W(o,t,s,c,!1),{array:!0});function U(o,t,s,c){if(!(t instanceof T))return t;const r=o.trim(t);let{r0:n,c0:e}=t,a=s&2?r.r1:t.r1,f=c&2?r.c1:t.c1;s&2&&r.r1<n&&(a=n),c&2&&r.c1<e&&(f=e);const l=(u,h)=>t.sheet.kind(u,h)!==0,i=u=>{for(let h=e;h<=f;h++)if(l(u,h))return!0;return!1},A=u=>{for(let h=n;h<=a;h++)if(l(h,u))return!0;return!1};if(s&1)for(;n<a&&!i(n);)n++;if(s&2)for(;a>n&&!i(a);)a--;if(c&1)for(;e<f&&!A(e);)e++;if(c&2)for(;f>e&&!A(f);)f--;if(s?n===a&&!i(n):c?e===f&&!A(e):!1)throw d.REF;return new T(t.sheet,n,e,a,f)}m("TRIMRANGE",1,3,(o,t,s,c)=>{const r=n=>{const e=t[n]===void 0||t[n]===null?3:L(o,t[n],s,c,3);if(e<0||e>3)throw d.VALUE;return e};return U(o,t[0],r(1),r(2))},{refResult:!0}),m("_TRO_ALL",1,1,(o,t)=>U(o,t[0],3,3),{refResult:!0,category:"Hidden"}),m("_TRO_LEADING",1,1,(o,t)=>U(o,t[0],1,1),{refResult:!0,category:"Hidden"}),m("_TRO_TRAILING",1,1,(o,t)=>U(o,t[0],2,2),{refResult:!0,category:"Hidden"}),m("ARRAYTOTEXT",1,2,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,0)===1,e=a=>a===null?"":typeof a=="string"?n?`"${a.replace(/"/g,'""')}"`:a:a instanceof M?a.code:G(a);return n?`{${O(r).map(a=>a.map(e).join(",")).join(";")}}`:r.data.map(e).join(", ")},{array:!0}),m("PERCENTOF",2,2,(o,t)=>{const s=r=>{if(!(r instanceof T)&&!(r instanceof p)){if(r instanceof M)throw r;return r===null||r===R?0:y(r)}let n=0;for(const e of o.toArr(r).data){if(e instanceof M)throw e;typeof e=="number"&&(n+=e)}return n},c=s(t[1]);if(c===0)throw d.DIV0;return s(t[0])/c},{array:!0}),m("SINGLE",1,1,(o,t,s,c)=>{const r=o.scalar(t[0],s,c);return r===null?0:r}),m("ANCHORARRAY",1,1,(o,t)=>t[0]instanceof T?o.spillRange(t[0]):d.REF,{refResult:!0});
|
package/calc/functions/date.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as
|
|
1
|
+
import{setCategory as W}from"./registry.js";import{alias as g,lift as i,raw as N}from"./registry.js";import{Arr as I,ERR as f,num as u,OMITTED as C,Ref as K,RefList as F,str as H,XlError as j}from"../values.js";import{partsToSerial as M,serialToParts as h}from"../../core/numfmt.js";import{eachValue as $,toolpakScalar as D}from"./helpers.js";import{parseInput as z}from"../../core/input.js";import{NUM as B}from"../../core/sheet.js";import{days360 as X,yearFrac as Z}from"./financial.js";W("Date & Time");const y=t=>t===void 0||t===C,T=2958465;function c(t){const e=Math.floor(u(t));if(e<0||e>T)throw f.NUM;return e}function q(t,e,r){let o=t;if(o>=0&&o<1900&&(o+=1900),o<0||o>9999)throw f.NUM;const n=o*12+(e-1),s=Math.floor(n/12),a=n-s*12+1,l=M(s,a,1)+r-1;if(l<0||l>T)throw f.NUM;return l}i("DATE",3,3,(t,e,r)=>q(Math.trunc(u(t)),Math.trunc(u(e)),Math.trunc(u(r)))),i("YEAR",1,1,t=>h(c(t)).year),i("MONTH",1,1,t=>h(c(t)).month),i("DAY",1,1,t=>h(c(t)).day);const L=t=>{const e=u(t);if(e<0)throw f.NUM;return Math.round((e-Math.floor(e))*86400)};i("HOUR",1,1,t=>Math.floor(L(t)/3600)%24),i("MINUTE",1,1,t=>Math.floor(L(t)%3600/60)),i("SECOND",1,1,t=>L(t)%60),i("TIME",3,3,(t,e,r)=>{const o=Math.trunc(u(t))*3600+Math.trunc(u(e))*60+Math.trunc(u(r));if(o<0)throw f.NUM;return o%86400/86400});const S=()=>{const t=new Date;return{whole:M(t.getFullYear(),t.getMonth()+1,t.getDate()),frac:(t.getHours()*3600+t.getMinutes()*60+t.getSeconds()+t.getMilliseconds()/1e3)/86400}};i("NOW",0,0,()=>{const t=S();return t.whole+t.frac},{volatile:!0}),i("TODAY",0,0,()=>S().whole,{volatile:!0});const b={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12};function Y(t){const e=t.trim(),r=z(e);if(r.kind===B&&r.format&&/[ymd]/i.test(r.format))return r.value;let o=/^(\d{1,2})[-\s]([A-Za-z]{3,})[-\s,]*(\d{2,4})?$/.exec(e);if(o){const n=b[o[2].slice(0,3).toLowerCase()];if(n){const s=o[3]?Number(o[3])<100?2e3+Number(o[3]):Number(o[3]):new Date().getFullYear();return M(s,n,Number(o[1]))}}if(o=/^([A-Za-z]{3,})\s+(\d{1,2})(?:,?\s*(\d{2,4}))?$/.exec(e),o){const n=b[o[1].slice(0,3).toLowerCase()];if(n){const s=o[3]?Number(o[3])<100?2e3+Number(o[3]):Number(o[3]):new Date().getFullYear();return M(s,n,Number(o[2]))}}return null}i("DATEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const e=t.split(/\s+\d{1,2}:\d{2}/)[0],r=Y(e);if(r===null)throw f.VALUE;return Math.floor(r)}),i("TIMEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const e=/(\d{1,2}):(\d{2})(?::(\d{2}(?:\.\d+)?))?\s*(am|pm|a|p)?/i.exec(t);if(!e)throw f.VALUE;let r=Number(e[1]);if(e[4]){const o=e[4][0].toLowerCase()==="p";if(r>12)throw f.VALUE;r=r%12+(o?12:0)}return(r*3600+Number(e[2])*60+Number(e[3]??0))/86400%1}),i("DAYS",2,2,(t,e)=>c(t)-c(e)),i("DAYS360",2,3,(t,e,r)=>X(c(t),c(e),y(r)?!1:!!u(r)));function P(t,e){const r=h(t),o=r.year*12+(r.month-1)+e,n=Math.floor(o/12),s=o-n*12+1;if(n<1900||n>9999)throw f.NUM;const a=E(n,s);return M(n,s,Math.min(r.day,a))}function E(t,e){return e===2?t===1900||t%4===0&&(t%100!==0||t%400===0)?29:28:[31,28,31,30,31,30,31,31,30,31,30,31][e-1]}i("EDATE",2,2,(t,e)=>P(c(t),Math.trunc(u(e)))),i("EOMONTH",2,2,(t,e)=>{const r=h(c(t)),o=r.year*12+(r.month-1)+Math.trunc(u(e)),n=Math.floor(o/12),s=o-n*12+1;if(n<1900||n>9999)throw f.NUM;return M(n,s,E(n,s))}),i("WEEKDAY",1,2,(t,e)=>{const r=c(t),o=y(e)?1:Math.trunc(u(e)),n=h(r).weekday,s={1:0,2:1,3:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[o];if(s===void 0)throw f.NUM;const a=(n-s+7)%7;return o===3?a:a+1}),i("ISOWEEKNUM",1,1,t=>R(c(t)));function R(t){const e=(h(t).weekday+6)%7,r=t-e+3,o=h(r).year;return Math.floor((r-M(o,1,1))/7)+1}i("WEEKNUM",1,2,(t,e)=>{const r=c(t),o=y(e)?1:Math.trunc(u(e));if(o===21)return R(r);const n={1:0,2:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[o];if(n===void 0)throw f.NUM;const s=M(h(r).year,1,1),a=(h(s).weekday-n+7)%7;return Math.floor((r-s+a)/7)+1});function A(t){if(y(t)||t===null)return[!1,!1,!1,!1,!1,!0,!0];if(typeof t=="string"){if(!/^[01]{7}$/.test(t))throw f.VALUE;return[...t].map(o=>o==="1")}const e=Math.trunc(u(t)),r=new Array(7).fill(!1);if(e>=1&&e<=7)r[(e+4)%7]=!0,r[(e+5)%7]=!0;else if(e>=11&&e<=17)r[e-11]=!0;else throw f.NUM;return r}function U(t,e){const r=new Set;return y(e)||e===null||$(t,e,o=>{if(o instanceof j)throw o;if(typeof o=="number")r.add(Math.floor(o));else if(typeof o=="string"){const n=Y(o);if(n===null)throw f.VALUE;r.add(Math.floor(n))}}),r}const O=(t,e)=>e[(h(t).weekday+6)%7];function V(t,e,r,o){const n=e>=t?1:-1,s=Math.min(t,e),a=Math.max(t,e),l=r.filter(d=>!d).length,w=a-s+1;let m=Math.floor(w/7)*l;for(let d=s+Math.floor(w/7)*7;d<=a;d++)O(d,r)||m++;for(const d of o)d>=s&&d<=a&&!O(d,r)&&m--;return m*n}const p=(t,e,r,o)=>{const n=D(t,e,r,o);if(typeof n=="string"){const s=Y(n);if(s===null)throw f.VALUE;return Math.floor(s)}return c(n)};N("NETWORKDAYS",2,3,(t,e,r,o)=>V(p(t,e[0],r,o),p(t,e[1],r,o),A(void 0),U(t,e[2]))),N("NETWORKDAYS.INTL",2,4,(t,e,r,o)=>V(p(t,e[0],r,o),p(t,e[1],r,o),A(y(e[2])?void 0:t.scalar(e[2],r,o)),U(t,e[3])));function k(t,e,r,o){const n=Math.trunc(e),s=n<0?-1:1;let a=Math.abs(n),l=t;if(r.every(Boolean))throw f.VALUE;for(;a>0;){if(l+=s,l<0||l>T)throw f.NUM;!O(l,r)&&!o.has(l)&&a--}return l}N("WORKDAY",2,3,(t,e,r,o)=>k(p(t,e[0],r,o),u(D(t,e[1],r,o)),A(void 0),U(t,e[2]))),N("WORKDAY.INTL",2,4,(t,e,r,o)=>k(p(t,e[0],r,o),u(D(t,e[1],r,o)),A(y(e[2])?void 0:t.scalar(e[2],r,o)),U(t,e[3]))),i("YEARFRAC",2,3,(t,e,r)=>{const o=y(r)?0:Math.trunc(u(r));if(o<0||o>4)throw f.NUM;return Z(c(t),c(e),o)}),i("DATEDIF",3,3,(t,e,r)=>{const o=c(t),n=c(e);if(o>n)throw f.NUM;const s=h(o),a=h(n),l=H(r).toUpperCase();let w=(a.year-s.year)*12+a.month-s.month;switch(a.day<s.day&&w--,l){case"D":return n-o;case"M":return w;case"Y":return Math.floor(w/12);case"YM":return w%12;case"YD":{let m=a.year;return(a.month<s.month||a.month===s.month&&a.day<s.day)&&m--,n-M(m,s.month,Math.min(s.day,E(m,s.month)))}case"MD":{if(a.day>=s.day)return a.day-s.day;const m=a.month===1?12:a.month-1,d=a.month===1?a.year-1:a.year;return E(d,m)-s.day+a.day}default:throw f.NUM}});export{I as Arr,K as Ref,F as RefList,g as alias};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let a=null;const A=()=>a;function p(t){const e=String(t??"").slice(0,2).toLowerCase();a=e==="ja"||e==="zh"||e==="ko"?e:null}const g=65377,u=65439,c=new Map,f=new Map;for(let t=g;t<=u;t++){const e=String.fromCharCode(t),n=t===65438?"゛":t===65439?"゜":e.normalize("NFKC");c.set(e,n),f.set(n,e)}const h=/[。-゚][゙゚]?/g,s=/[、。「」゛゜ァ-ー]/g;function C(t){return t.replace(/[!-~]/g,e=>String.fromCharCode(e.charCodeAt(0)+65248)).replace(/ /g," ").replace(h,e=>{if(e.length===2){const n=(c.get(e[0])+(e[1]==="゙"?"゙":"゚")).normalize("NFC");if(n.length===1)return n}return Array.from(e,n=>c.get(n)).join("")})}function L(t){return t.replace(/[!-~]/g,e=>String.fromCharCode(e.charCodeAt(0)-65248)).replace(/ /g," ").replace(s,e=>{const n=f.get(e);if(n)return n;const[r,l]=e.normalize("NFD"),i=f.get(r);return!i||l!=="゙"&&l!=="゚"?e:i+(l==="゙"?"゙":"゚")})}const o=t=>t<128||a==="ja"&&t>=g&&t<=u?1:2;function x(t){let e=0;for(let n=0;n<t.length;n++)e+=o(t.charCodeAt(n));return e}function F(t,e){let n=0,r=0;for(;r<t.length&&n<e;)n+=o(t.charCodeAt(r++));return r}function d(t,e){let n=0,r=0;for(;r<t.length&&n+o(t.charCodeAt(r))<=e;)n+=o(t.charCodeAt(r++));return r}export{x as byteLength,A as dbcsLanguage,p as setDbcsLanguage,C as toFullWidth,L as toHalfWidth,F as unitAtByte,d as unitBeforeByte};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as Ot}from"./registry.js";import{lift as Nt,raw as B}from"./registry.js";import{Arr as vt,bool as mt,checked as X,ERR as N,num as h,opt as R,Ref as Ut,RefList as Ft,XlError as Vt}from"../values.js";import{scalarOf as dt}from"./helpers.js";import{partsToSerial as rt,serialToParts as x}from"../../core/numfmt.js";Ot("Financial");const kt=2958465,pt=1e-7,yt=200,y=(t,n,s,o)=>Nt(t,n,s,(...e)=>X(o(...e))),p=t=>{if(!t)throw N.NUM},m=t=>{if(typeof t=="boolean")throw N.VALUE;return h(t)},G=t=>h(R(t,0))!==0?1:0;function D(t){const n=Math.trunc(m(t));if(n<0||n>kt)throw N.VALUE;return n}function F(t){const n=Math.trunc(m(R(t,0)));return p(n>=0&&n<=4),n}function z(t){const n=Math.trunc(m(t));return p(n===1||n===2||n===4),n}const J=t=>t%4===0&&(t%100!==0||t%400===0),ct=(t,n)=>n===2?J(t)?29:28:n===4||n===6||n===9||n===11?30:31;function Z(t){const n=x(t);return n.day>=ct(n.year,n.month)}function P(t,n,s){const o=x(t),e=o.year*12+o.month-1+n,r=Math.floor(e/12),a=e-r*12+1,c=ct(r,a);return rt(r,a,s?c:Math.min(o.day,c))}function $(t,n,s=!1){const o=x(t),e=x(n);let r=o.day,a=e.day;if(s)r===31&&(r=30),a===31&&(a=30);else{const c=f=>f.month===2&&f.day>=ct(f.year,2);c(o)&&(c(e)&&(a=30),r=30),a===31&&r>=30&&(a=30),r===31&&(r=30)}return(e.year-o.year)*360+(e.month-o.month)*30+a-r}const V=(t,n,s)=>s===0?$(t,n):s===4?$(t,n,!0):n-t;function St(t,n){const s=x(t),o=x(n);if(s.year===o.year)return(n-t)/(J(s.year)?366:365);if(o.year===s.year+1&&(s.month>o.month||s.month===o.month&&s.day>=o.day)){const r=J(s.year)&&t<=rt(s.year,2,29)||J(o.year)&&n>=rt(o.year,2,29);return(n-t)/(r?366:365)}let e=0;for(let r=s.year;r<=o.year;r++)e+=J(r)?366:365;return(n-t)/(e/(o.year-s.year+1))}function _(t,n,s=0){const o=Math.trunc(Math.min(t,n)),e=Math.trunc(Math.max(t,n));switch(s){case 1:return St(o,e);case 2:return(e-o)/360;case 3:return(e-o)/365;case 4:return $(o,e,!0)/360;default:return $(o,e)/360}}function at(t,n,s,o){const e=Z(n),r=12/s;let a=1;for(;P(n,-a*r,e)>t;)a++;const c=P(n,-a*r,e),f=P(n,(1-a)*r,e),i=V(c,t,o),u=o===1?f-c:(o===3?365:360)/s,l=o===0||o===4?u-i:f-t;return{n:a,pcd:c,ncd:f,a:i,e:u,dsc:l,f:s}}function tt(t,n,s,o){const e=D(t),r=D(n),a=z(s),c=F(o);return p(e<r),at(e,r,a,c)}function nt(t,n,s=-1){const o=.001*Math.max(1,Math.abs(t(n))),e=u=>Math.abs(t(u))<=o;let r=n;for(let u=0;u<yt;u++){const l=t(r);if(l===0)return r;const M=Math.min(1e-6*(1+Math.abs(r)),(r-s)/2),d=r-l*2*M/(t(r+M)-t(r-M));if(!Number.isFinite(d)||d<=s)break;if(Math.abs(d-r)<pt&&e(d))return d;r=d}let a=n,c=n+.01*(1+Math.abs(n)),f=t(a),i=t(c);for(let u=0;u<yt;u++){if(i===0)return c;let l=c-i*(c-a)/(i-f);if(l<=s&&(l=(c+s)/2),!Number.isFinite(l))break;if(Math.abs(l-c)<pt&&e(l))return l;a=c,f=i,c=l,i=t(c)}throw N.NUM}const K=(t,n)=>(1+t)**n,ot=(t,n)=>t===0?n:(t>-1?Math.expm1(n*Math.log1p(t)):K(t,n)-1)/t,ft=(t,n,s,o,e)=>-(o*K(t,n)+s*(1+t*e)*ot(t,n)),Yt=(t,n,s,o,e)=>-(o+s*(1+t*e)*ot(t,n))/K(t,n),et=(t,n,s,o,e)=>-(o+s*K(t,n))/((1+t*e)*ot(t,n));function st(t,n,s,o,e,r){const a=et(t,s,o,e,r);return n===1?r?0:-o*t:(r?ft(t,n-2,a,o,1)-a:ft(t,n-1,a,o,0))*t}function It(t,n,s,o,e,r){const a=[h(t),h(n),h(s),h(o),h(R(e,0)),G(r)];return p(a[1]>=1&&a[1]<a[2]+1),a}y("PMT",3,5,(t,n,s,o,e)=>et(h(t),h(n),h(s),h(R(o,0)),G(e))),y("PV",3,5,(t,n,s,o,e)=>Yt(h(t),h(n),h(s),h(R(o,0)),G(e))),y("FV",3,5,(t,n,s,o,e)=>ft(h(t),h(n),h(s),h(R(o,0)),G(e))),y("IPMT",4,6,(...t)=>st(...It(...t))),y("PPMT",4,6,(...t)=>{const[n,s,o,e,r,a]=It(...t);return et(n,o,e,r,a)-st(n,s,o,e,r,a)}),y("NPER",3,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=h(R(o,0)),i=G(e);if(r===0){if(a===0)throw N.DIV0;return-(c+f)/a}const u=a*(1+r*i);return Math.log((u-f*r)/(u+c*r))/Math.log1p(r)}),y("RATE",3,6,(t,n,s,o,e,r)=>{const a=h(t),c=h(n),f=h(s),i=h(R(o,0)),u=G(e),l=h(R(r,.1));p(a>0&&l>-1);const M=nt(d=>f*K(d,a)+c*(1+d*u)*ot(d,a)+i,l);return p(M>-1+1e-6),M}),y("ISPMT",4,4,(t,n,s,o)=>{const e=h(s);if(e===0)throw N.DIV0;return h(o)*h(t)*(h(n)/e-1)}),y("PDURATION",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return p(o>0&&e>0&&r>0),(Math.log(r)-Math.log(e))/Math.log1p(o)}),y("RRI",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return p(o>0&&e!==0&&e*r>=0),(r/e)**(1/o)-1});function Dt(t,n,s,o,e,r,a){const c=m(t),f=Math.trunc(m(n)),i=m(s),u=Math.trunc(m(o)),l=Math.trunc(m(e)),M=Math.trunc(m(r));p(c>0&&f>0&&i>0&&u>=1&&l>=u&&l<=f&&(M===0||M===1));let d=0;for(let I=u;I<=l;I++)d+=a(c,I,f,i,M);return d}y("CUMIPMT",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>st(n,s,o,e,0,r))),y("CUMPRINC",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>et(n,o,e,0,r)-st(n,s,o,e,0,r)));function q(t,n){const s=[],o=e=>{for(const r of t.toArr(e).data)if(typeof r=="number")s.push(r);else if(r instanceof Vt)throw r};return n instanceof Ft?n.refs.forEach(o):n instanceof Ut||n instanceof vt?o(n):s.push(h(n)),s}const H=(t,n,s,o)=>h(dt(t,n,s,o)),it=t=>t.some(n=>n>0)&&t.some(n=>n<0),bt=99;function Et(t,n,s){const o=q(t,n),e=q(t,s).map(D);return p(o.length===e.length&&o.length>0),p(e.every(r=>r>=e[0])),[o,e.map(r=>(r-e[0])/365)]}function Q(t,n,s){let o=0;for(let e=0;e<t.length;e++)o+=t[e]/(1+s)**n[e];return o}B("NPV",2,255,(t,n,s,o)=>{const e=H(t,n[0],s,o);if(e===-1)throw N.DIV0;let r=0,a=1;for(let c=1;c<n.length;c++)for(const f of q(t,n[c]))r+=f/(1+e)**a++;return X(r)},{array:!0}),B("IRR",1,2,(t,n,s,o)=>{const e=q(t,n[0]),r=H(t,R(n[1],.1),s,o);return p(r>-1&&it(e)),nt(a=>{let c=0;for(let f=0;f<e.length;f++)c+=e[f]/(1+a)**f;return c},r)},{array:!0}),B("MIRR",3,3,(t,n,s,o)=>{const e=q(t,n[0]),r=H(t,n[1],s,o),a=H(t,n[2],s,o);if(r===-1||a===-1||!it(e))throw N.DIV0;const c=e.length-1;let f=0,i=0;return e.forEach((u,l)=>{u>0?f+=u*(1+a)**(c-l):i+=u/(1+r)**l}),X((f/-i)**(1/c)-1)},{array:!0}),B("XNPV",3,3,(t,n,s,o)=>{const e=H(t,n[0],s,o),[r,a]=Et(t,n[1],n[2]);return p(e>=0),X(Q(r,a,e))},{array:!0});function Bt(t,n,s){const o=t[0],e=u=>u*o>=0,r=s===0?1e-5:s,a=Q(t,n,0)*o>0;let c=0,f=r,i=0;if(a){p(Math.abs(r)<1);const u=-Math.abs(r);e(Q(t,n,u))?[c,f]=[-1,u]:[c,f]=[u,0]}else for(p(r>0),e(Q(t,n,f))||(i=1);!e(Q(t,n,f));)c=f,f*=2,i++,p(i<=bt);for(;++i<=bt;){const u=(c+f)/2,l=Q(t,n,u);if(e(l)?f=u:c=u,f-c<=1e-8*(Math.abs(u)+.5)&&(!a||Math.abs(l)<=2e-8*Math.abs(o)))return u}throw N.NUM}B("XIRR",2,3,(t,n,s,o)=>{const[e,r]=Et(t,n[0],n[1]),a=H(t,R(n[2],.1),s,o);return p(a>-1&&it(e)),X(Bt(e,r,a))},{array:!0}),B("FVSCHEDULE",2,2,(t,n,s,o)=>{let e=m(dt(t,n[0],s,o));for(const r of q(t,n[1]))e*=1+r;return X(e)},{array:!0}),y("SLN",3,3,(t,n,s)=>{const o=h(s);if(o===0)throw N.DIV0;return(h(t)-h(n))/o}),y("SYD",4,4,(t,n,s,o)=>{const[e,r]=[h(s),h(o)];return p(e>0&&r>0&&r<=e),(h(t)-h(n))*(e-r+1)*2/(e*(e+1))}),y("DB",4,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=Math.trunc(h(o)),i=Math.trunc(h(R(e,12)));if(p(r>=0&&a>=0&&c>0&&f>=1&&i>=1&&i<=12),p(f<=(i===12?c:c+1)),a>=r)return 0;const u=Math.round((1-(a/r)**(1/c))*1e3)/1e3;let l=r*u*i/12;if(f===1)return l;let M=0;for(let d=2;d<=Math.min(f,c);d++)M=(r-l)*u,l+=M;return f>c?(r-l)*u*(12-i)/12:M});function ut(t,n,s,o,e){let r=e/s,a;r>=1?(r=1,a=o===1?t:0):a=t*(1-r)**(o-1);const c=t*(1-r)**o;return Math.max(a-Math.max(c,n),0)}y("DDB",4,5,(t,n,s,o,e)=>{const[r,a,c,f]=[h(t),h(n),h(s),h(o)],i=h(R(e,2));return p(r>=0&&a>=0&&c>0&&f>0&&f<=c&&i>0),ut(r,a,c,f,i)});function Rt(t,n,s,o,e,r){const a=Math.ceil(e);let c=t-n,f=0,i=!1,u=0;for(let l=1;l<=a;l++){let M=f;if(!i){const d=ut(t,n,s,l,r);f=c/(o-(l-1)),i=f>d,M=i?f:d,i||(c-=d)}u+=l===a?M*(e+1-a):M}return u}y("VDB",5,7,(t,n,s,o,e,r,a)=>{let c=h(t);const f=h(n),i=h(s);let u=h(o),l=h(e);const M=h(R(r,2)),d=mt(R(a,!1));if(p(c>=0&&f>=0&&i>0&&u>=0&&l>=u&&l<=i&&M>=0),d){const E=Math.floor(u),b=Math.ceil(l);let g=0;for(let A=E+1;A<=b;A++){let C=ut(c,f,i,A,M);A===E+1?C*=Math.min(l,E+1)-u:A===b&&(C*=l+1-b),g+=C}return g}let I=i;return u!==Math.floor(u)&&M>1&&u>=i/2&&(l-=u-i/2,u=i/2,I+=1),c-=Rt(c,f,i,I,u,M),Rt(c,f,i,i-u,l-u,M)});function At(t,n,s,o,e,r,a){const c={cost:m(t),bought:D(n),firstEnd:D(s),salvage:m(o),per:Math.trunc(m(e)),rate:m(r),basis:F(a)};return p(c.basis!==2&&c.cost>=0&&c.salvage>=0&&c.salvage<=c.cost),p(c.per>=0&&c.rate>0&&c.bought<=c.firstEnd),c.frac=_(c.bought,c.firstEnd,c.basis),c}y("AMORLINC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=n*e,c=r*a,f=Math.trunc((n-s-c)/a);return o===0?c:o<=f?a:o===f+1?n-s-a*f-c:0}),y("AMORDEGRC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=1/e,c=(M,d)=>a>=M&&a<=d;p(c(3,4)||c(5,6)||a>6);const f=e*(c(3,4)?1.5:c(5,6)?2:2.5);let i=Math.round(r*f*n),u=n-i,l=u-s;for(let M=0;M<o;M++){if(i=Math.round(f*u),l-=i,l<0)return o-M===1?Math.round(u*.5):0;u-=i}return i}),y("EFFECT",2,2,(t,n)=>{const s=m(t),o=Math.trunc(m(n));return p(s>0&&o>=1),(1+s/o)**o-1}),y("NOMINAL",2,2,(t,n)=>{const s=m(t),o=Math.trunc(m(n));return p(s>0&&o>=1),o*((1+s)**(1/o)-1)});function Ct(t,n,s){const o=m(t),e=Math.trunc(m(n));if(p(e>=0),e===0)throw N.DIV0;const r=Math.trunc(o);return r+s(o-r,e,10**Math.ceil(Math.log10(e)))}y("DOLLARDE",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*e/o)),y("DOLLARFR",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*o/e));function lt(t,n,s,o){const e=D(t),r=D(n),a=m(s);return p(e<r&&r<=P(e,12,!1)&&a>0),o(a,r-e)}y("TBILLEQ",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>365*o/(360-o*e))),y("TBILLPRICE",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>100*(1-o*e/360))),y("TBILLYIELD",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>(100-o)/o*360/e)),y("ACCRINT",6,8,(t,n,s,o,e,r,a,c)=>{const f=D(t),i=D(n),u=D(s),l=m(o),M=m(R(e,1e3)),d=z(r),I=F(a),E=mt(R(c,!0));p(l>0&&M>0&&f<u);const b=!E&&u>i?i:f,g=12/d,A=Z(i);let C=0;for(;P(i,C*g,A)>b;)C--;for(;P(i,(C+1)*g,A)<=b;)C++;let U=0;for(;P(i,C*g,A)<u;C++){const L=P(i,C*g,A),v=P(i,(C+1)*g,A),T=I===1?v-L:(I===3?365:360)/d;U+=V(Math.max(L,b),Math.min(v,u),I)/T}return M*l*U/d}),y("ACCRINTM",3,5,(t,n,s,o,e)=>{const r=D(t),a=D(n),c=m(s),f=m(R(o,1e3)),i=F(e);return p(c>0&&f>0&&r<a),f*c*_(r,a,i)});const j=(t,n)=>y(t,3,4,(s,o,e,r)=>n(tt(s,o,e,r)));j("COUPDAYBS",t=>t.a),j("COUPDAYS",t=>t.e),j("COUPDAYSNC",t=>t.dsc),j("COUPNCD",t=>t.ncd),j("COUPNUM",t=>t.n),j("COUPPCD",t=>t.pcd);const W=(t,n)=>y(t,4,5,(s,o,e,r,a)=>{const c=D(s),f=D(o),i=m(e),u=m(r),l=F(a);return p(c<f&&i>0&&u>0),n(i,u,_(c,f,l))});W("DISC",(t,n,s)=>(n-t)/n/s),W("INTRATE",(t,n,s)=>(n-t)/t/s),W("RECEIVED",(t,n,s)=>{const o=1-n*s;return p(o>0),t/o}),W("PRICEDISC",(t,n,s)=>n*(1-t*s)),W("YIELDDISC",(t,n,s)=>(n/t-1)/s);const gt=(t,n,s)=>y(t,5,6,(o,e,r,a,c,f)=>{const i=D(o),u=D(e),l=D(r),M=m(a),d=m(c),I=F(f);return p(i<u&&l<i&&M>=0&&n(d)),s(M,d,_(l,u,I),_(l,i,I),_(i,u,I))});gt("PRICEMAT",t=>t>=0,(t,n,s,o,e)=>(100+s*t*100)/(1+e*n)-o*t*100),gt("YIELDMAT",t=>t>0,(t,n,s,o,e)=>{const r=n/100+o*t;return(1+s*t-r)/r/e});function Pt(t,n,s,o){const e=100*n/t.f,r=t.dsc/t.e;if(t.n===1)return(o+e)/(1+s/t.f*r)-e*t.a/t.e;const a=1+s/t.f;let c=o/a**(t.n-1+r);for(let f=0;f<t.n;f++)c+=e/a**(f+r);return c-e*t.a/t.e}y("PRICE",6,7,(t,n,s,o,e,r,a)=>{const c=m(s),f=m(o),i=m(e),u=tt(t,n,r,a);return p(c>=0&&f>=0&&i>0),Pt(u,c,f,i)}),y("YIELD",6,7,(t,n,s,o,e,r,a)=>{const c=m(s),f=m(o),i=m(e),u=tt(t,n,r,a);if(p(c>=0&&f>0&&i>0),u.n===1){const l=c/u.f,M=f/100+u.a/u.e*l;return(i/100+l-M)/M*(u.f*u.e/u.dsc)}return nt(l=>Pt(u,c,l,i)-f,.1,-u.f)});function Lt(t,n,s,o,e,r){const a=m(s),c=m(o),f=tt(t,n,e,r);p(a>=0&&c>=0);const i=1+c/f.f,u=100*a/f.f,l=f.dsc/f.e;let M=0,d=0;for(let I=0;I<f.n;I++){const E=I+l,b=(I===f.n-1?u+100:u)/i**E;M+=E*b,d+=b}return[M/d/f.f,i]}y("DURATION",5,6,(...t)=>Lt(...t)[0]),y("MDURATION",5,6,(...t)=>{const[n,s]=Lt(...t);return n/s});function Tt(t,n,s,o,e,r,a,c){const f=D(t),i=D(n),u=D(s),l=D(o),M=m(e),d=m(r),I=z(a),E=F(c);p(i>l&&l>f&&f>u&&M>=0&&d>0);const b=at(f,l,I,E),g=b.n===1?V(f,l,E):b.dsc,A=at(l,i,I,E).n,C=Z(l),U=12/I;let L=0,v=0;for(let k=0,w=l;;k++){const O=P(l,-(k+1)*U,C),S=E===1?w-O:b.e;L+=(O>u?S:V(u,w,E))/S;const ht=Math.max(u,O),Mt=Math.min(f,w);if(Mt>ht&&(v+=V(ht,Mt,E)/S),O<=u)break;w=O}const T=100*M/I,Y=b.n-1+g/b.e;return k=>{const w=1+k/I;let O=d/w**(A+Y)+T*L/w**Y;for(let S=1;S<=A;S++)O+=T/w**(S+Y);return O-T*v}}y("ODDFPRICE",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=m(r),u=Tt(t,n,s,o,e,a,c,f);return p(i>=0),u(i)}),y("ODDFYIELD",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=m(r),u=Tt(t,n,s,o,e,a,c,f);return p(i>0),nt(l=>u(l)-i,.1,-z(c))});function wt(t,n,s,o,e,r,a){const c=D(t),f=D(n),i=D(s),u=m(o),l=m(e),M=z(r),d=F(a);p(f>c&&c>i&&u>=0&&l>0);const I=12/M,E=Z(i);let b=0,g=0,A=0;for(let U=0;;U++){const L=P(i,U*I,E),v=P(i,(U+1)*I,E),T=d===0||d===4?360/M:v-L,Y=v>=f,k=Y?V(L,f,d):T;b+=k/T,v<c?g+=k/T:L<c&&(g+=V(L,c,d)/T);const w=Math.max(c,L),O=Math.min(f,v);if(O>w&&(A+=V(w,O,d)/T),Y)break}const C=100*u/M;return{income:l+b*C,interest:g*C,toGo:A,f:M}}y("ODDLPRICE",7,8,(t,n,s,o,e,r,a,c)=>{const f=m(e),i=wt(t,n,s,o,r,a,c);return p(f>=0),i.income/(1+i.toGo*f/i.f)-i.interest}),y("ODDLYIELD",7,8,(t,n,s,o,e,r,a,c)=>{const f=m(e),i=wt(t,n,s,o,r,a,c);p(f>0);const u=f+i.interest;return(i.income-u)/u*(i.f/i.toGo)});export{$ as days360,_ as yearFrac};
|
|
1
|
+
import{setCategory as Ot}from"./registry.js";import{lift as Nt,raw as B}from"./registry.js";import{Arr as vt,bool as mt,checked as X,ERR as N,num as h,opt as R,Ref as Ut,RefList as Ft,XlError as Vt}from"../values.js";import{scalarOf as kt,toolpakScalar as dt}from"./helpers.js";import{partsToSerial as rt,serialToParts as x}from"../../core/numfmt.js";Ot("Financial");const St=2958465,pt=1e-7,yt=200,y=(t,n,s,o)=>Nt(t,n,s,(...e)=>X(o(...e))),m=t=>{if(!t)throw N.NUM},d=t=>{if(typeof t=="boolean")throw N.VALUE;return h(t)},G=t=>h(R(t,0))!==0?1:0;function D(t){const n=Math.trunc(d(t));if(n<0||n>St)throw N.VALUE;return n}function F(t){const n=Math.trunc(d(R(t,0)));return m(n>=0&&n<=4),n}function j(t){const n=Math.trunc(d(t));return m(n===1||n===2||n===4),n}const z=t=>t%4===0&&(t%100!==0||t%400===0),ct=(t,n)=>n===2?z(t)?29:28:n===4||n===6||n===9||n===11?30:31;function Z(t){const n=x(t);return n.day>=ct(n.year,n.month)}function P(t,n,s){const o=x(t),e=o.year*12+o.month-1+n,r=Math.floor(e/12),a=e-r*12+1,c=ct(r,a);return rt(r,a,s?c:Math.min(o.day,c))}function $(t,n,s=!1){const o=x(t),e=x(n);let r=o.day,a=e.day;if(s)r===31&&(r=30),a===31&&(a=30);else{const c=f=>f.month===2&&f.day>=ct(f.year,2);c(o)&&(c(e)&&(a=30),r=30),a===31&&r>=30&&(a=30),r===31&&(r=30)}return(e.year-o.year)*360+(e.month-o.month)*30+a-r}const V=(t,n,s)=>s===0?$(t,n):s===4?$(t,n,!0):n-t;function Yt(t,n){const s=x(t),o=x(n);if(s.year===o.year)return(n-t)/(z(s.year)?366:365);if(o.year===s.year+1&&(s.month>o.month||s.month===o.month&&s.day>=o.day)){const r=z(s.year)&&t<=rt(s.year,2,29)||z(o.year)&&n>=rt(o.year,2,29);return(n-t)/(r?366:365)}let e=0;for(let r=s.year;r<=o.year;r++)e+=z(r)?366:365;return(n-t)/(e/(o.year-s.year+1))}function _(t,n,s=0){const o=Math.trunc(Math.min(t,n)),e=Math.trunc(Math.max(t,n));switch(s){case 1:return Yt(o,e);case 2:return(e-o)/360;case 3:return(e-o)/365;case 4:return $(o,e,!0)/360;default:return $(o,e)/360}}function at(t,n,s,o){const e=Z(n),r=12/s;let a=1;for(;P(n,-a*r,e)>t;)a++;const c=P(n,-a*r,e),f=P(n,(1-a)*r,e),i=V(c,t,o),u=o===1?f-c:(o===3?365:360)/s,l=o===0||o===4?u-i:f-t;return{n:a,pcd:c,ncd:f,a:i,e:u,dsc:l,f:s}}function tt(t,n,s,o,e=!1){const r=D(t),a=D(n);e&&m(r<a);const c=j(s),f=F(o);return m(r<a),at(r,a,c,f)}function nt(t,n,s=-1){const o=.001*Math.max(1,Math.abs(t(n))),e=u=>Math.abs(t(u))<=o;let r=n;for(let u=0;u<yt;u++){const l=t(r);if(l===0)return r;const M=Math.min(1e-6*(1+Math.abs(r)),(r-s)/2),p=r-l*2*M/(t(r+M)-t(r-M));if(!Number.isFinite(p)||p<=s)break;if(Math.abs(p-r)<pt&&e(p))return p;r=p}let a=n,c=n+.01*(1+Math.abs(n)),f=t(a),i=t(c);for(let u=0;u<yt;u++){if(i===0)return c;let l=c-i*(c-a)/(i-f);if(l<=s&&(l=(c+s)/2),!Number.isFinite(l))break;if(Math.abs(l-c)<pt&&e(l))return l;a=c,f=i,c=l,i=t(c)}throw N.NUM}const J=(t,n)=>(1+t)**n,ot=(t,n)=>t===0?n:(t>-1?Math.expm1(n*Math.log1p(t)):J(t,n)-1)/t,ft=(t,n,s,o,e)=>-(o*J(t,n)+s*(1+t*e)*ot(t,n)),Bt=(t,n,s,o,e)=>-(o+s*(1+t*e)*ot(t,n))/J(t,n),et=(t,n,s,o,e)=>-(o+s*J(t,n))/((1+t*e)*ot(t,n));function st(t,n,s,o,e,r){const a=et(t,s,o,e,r);return n===1?r?0:-o*t:(r?ft(t,n-2,a,o,1)-a:ft(t,n-1,a,o,0))*t}function It(t,n,s,o,e,r){const a=[h(t),h(n),h(s),h(o),h(R(e,0)),G(r)];return m(a[1]>=1&&a[1]<a[2]+1),a}y("PMT",3,5,(t,n,s,o,e)=>et(h(t),h(n),h(s),h(R(o,0)),G(e))),y("PV",3,5,(t,n,s,o,e)=>Bt(h(t),h(n),h(s),h(R(o,0)),G(e))),y("FV",3,5,(t,n,s,o,e)=>ft(h(t),h(n),h(s),h(R(o,0)),G(e))),y("IPMT",4,6,(...t)=>st(...It(...t))),y("PPMT",4,6,(...t)=>{const[n,s,o,e,r,a]=It(...t);return et(n,o,e,r,a)-st(n,s,o,e,r,a)}),y("NPER",3,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=h(R(o,0)),i=G(e);if(r===0){if(a===0)throw N.DIV0;return-(c+f)/a}const u=a*(1+r*i);return Math.log((u-f*r)/(u+c*r))/Math.log1p(r)}),y("RATE",3,6,(t,n,s,o,e,r)=>{const a=h(t),c=h(n),f=h(s),i=h(R(o,0)),u=G(e),l=h(R(r,.1));m(a>0&&l>-1);const M=nt(p=>f*J(p,a)+c*(1+p*u)*ot(p,a)+i,l);return m(M>-1+1e-6),M}),y("ISPMT",4,4,(t,n,s,o)=>{const e=h(s);if(e===0)throw N.DIV0;return h(o)*h(t)*(h(n)/e-1)}),y("PDURATION",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return m(o>0&&e>0&&r>0),(Math.log(r)-Math.log(e))/Math.log1p(o)}),y("RRI",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return m(o>0&&e!==0&&e*r>=0),(r/e)**(1/o)-1});function Dt(t,n,s,o,e,r,a){const c=d(t),f=Math.trunc(d(n)),i=d(s),u=Math.trunc(d(o)),l=Math.trunc(d(e)),M=Math.trunc(d(r));m(c>0&&f>0&&i>0&&u>=1&&l>=u&&l<=f&&(M===0||M===1));let p=0;for(let I=u;I<=l;I++)p+=a(c,I,f,i,M);return p}y("CUMIPMT",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>st(n,s,o,e,0,r))),y("CUMPRINC",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>et(n,o,e,0,r)-st(n,s,o,e,0,r)));function q(t,n){const s=[],o=e=>{for(const r of t.toArr(e).data)if(typeof r=="number")s.push(r);else if(r instanceof Vt)throw r};return n instanceof Ft?n.refs.forEach(o):n instanceof Ut||n instanceof vt?o(n):s.push(h(n)),s}const K=(t,n,s,o)=>h(kt(t,n,s,o)),it=t=>t.some(n=>n>0)&&t.some(n=>n<0),bt=99;function Et(t,n,s){const o=q(t,n),e=q(t,s).map(D);return m(o.length===e.length&&o.length>0),m(e.every(r=>r>=e[0])),[o,e.map(r=>(r-e[0])/365)]}function H(t,n,s){let o=0;for(let e=0;e<t.length;e++)o+=t[e]/(1+s)**n[e];return o}B("NPV",2,255,(t,n,s,o)=>{const e=K(t,n[0],s,o);if(e===-1)throw N.DIV0;let r=0,a=1;for(let c=1;c<n.length;c++)for(const f of q(t,n[c]))r+=f/(1+e)**a++;return X(r)},{array:!0}),B("IRR",1,2,(t,n,s,o)=>{const e=q(t,n[0]),r=K(t,R(n[1],.1),s,o);return m(r>-1&&it(e)),nt(a=>{let c=0;for(let f=0;f<e.length;f++)c+=e[f]/(1+a)**f;return c},r)},{array:!0}),B("MIRR",3,3,(t,n,s,o)=>{const e=q(t,n[0]),r=K(t,n[1],s,o),a=K(t,n[2],s,o);if(r===-1||a===-1||!it(e))throw N.DIV0;const c=e.length-1;let f=0,i=0;return e.forEach((u,l)=>{u>0?f+=u*(1+a)**(c-l):i+=u/(1+r)**l}),X((f/-i)**(1/c)-1)},{array:!0}),B("XNPV",3,3,(t,n,s,o)=>{const e=h(dt(t,n[0],s,o)),[r,a]=Et(t,n[1],n[2]);return m(e>=0),X(H(r,a,e))},{array:!0});function Xt(t,n,s){const o=t[0],e=u=>u*o>=0,r=s===0?1e-5:s,a=H(t,n,0)*o>0;let c=0,f=r,i=0;if(a){m(Math.abs(r)<1);const u=-Math.abs(r);e(H(t,n,u))?[c,f]=[-1,u]:[c,f]=[u,0]}else for(m(r>0),e(H(t,n,f))||(i=1);!e(H(t,n,f));)c=f,f*=2,i++,m(i<=bt);for(;++i<=bt;){const u=(c+f)/2,l=H(t,n,u);if(e(l)?f=u:c=u,f-c<=1e-8*(Math.abs(u)+.5)&&(!a||Math.abs(l)<=2e-8*Math.abs(o)))return u}throw N.NUM}B("XIRR",2,3,(t,n,s,o)=>{const[e,r]=Et(t,n[0],n[1]),a=K(t,R(n[2],.1),s,o);return m(a>-1&&it(e)),X(Xt(e,r,a))},{array:!0}),B("FVSCHEDULE",2,2,(t,n,s,o)=>{let e=h(dt(t,n[0],s,o));for(const r of q(t,n[1]))e*=1+r;return X(e)},{array:!0}),y("SLN",3,3,(t,n,s)=>{const o=h(s);if(o===0)throw N.DIV0;return(h(t)-h(n))/o}),y("SYD",4,4,(t,n,s,o)=>{const[e,r]=[h(s),h(o)];return m(e>0&&r>0&&r<=e),(h(t)-h(n))*(e-r+1)*2/(e*(e+1))}),y("DB",4,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=Math.trunc(h(o)),i=Math.trunc(h(R(e,12)));if(m(r>=0&&a>=0&&c>0&&f>=1&&i>=1&&i<=12),m(f<=(i===12?c:c+1)),r===0)return 0;const u=Math.round((1-(a/r)**(1/c))*1e3)/1e3;let l=r*u*i/12;if(f===1)return l;let M=0;for(let p=2;p<=Math.min(f,c);p++)M=(r-l)*u,l+=M;return f>c?(r-l)*u*(12-i)/12:M});function ut(t,n,s,o,e){let r=e/s,a;r>=1?(r=1,a=o===1?t:0):a=t*(1-r)**(o-1);const c=t*(1-r)**o;return Math.max(a-Math.max(c,n),0)}y("DDB",4,5,(t,n,s,o,e)=>{const[r,a,c,f]=[h(t),h(n),h(s),h(o)],i=h(R(e,2));return m(r>=0&&a>=0&&c>0&&f>0&&f<=c&&i>0),ut(r,a,c,f,i)});function Rt(t,n,s,o,e,r){const a=Math.ceil(e);let c=t-n,f=0,i=!1,u=0;for(let l=1;l<=a;l++){let M=f;if(!i){const p=ut(t,n,s,l,r);f=c/(o-(l-1)),i=f>p,M=i?f:p,i||(c-=p)}u+=l===a?M*(e+1-a):M}return u}y("VDB",5,7,(t,n,s,o,e,r,a)=>{let c=h(t);const f=h(n),i=h(s);let u=h(o),l=h(e);const M=h(R(r,2)),p=mt(R(a,!1));if(m(c>=0&&f>=0&&i>0&&u>=0&&l>=u&&l<=i&&M>=0),p){const E=Math.floor(u),b=Math.ceil(l);let g=0;for(let A=E+1;A<=b;A++){let C=ut(c,f,i,A,M);A===E+1?C*=Math.min(l,E+1)-u:A===b&&(C*=l+1-b),g+=C}return g}let I=i;return u!==Math.floor(u)&&M>1&&u>=i/2&&(l-=u-i/2,u=i/2,I+=1),c-=Rt(c,f,i,I,u,M),Rt(c,f,i,i-u,l-u,M)});function At(t,n,s,o,e,r,a){const c={cost:d(t),bought:D(n),firstEnd:D(s),salvage:d(o),per:Math.trunc(d(e)),rate:d(r),basis:F(a)};return m(c.basis!==2&&c.cost>=0&&c.salvage>=0&&c.salvage<=c.cost),m(c.per>=0&&c.rate>0&&c.bought<=c.firstEnd),c.frac=_(c.bought,c.firstEnd,c.basis),c}y("AMORLINC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=n*e,c=r*a,f=Math.trunc((n-s-c)/a);return o===0?c:o<=f?a:o===f+1?n-s-a*f-c:0}),y("AMORDEGRC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=1/e,c=(M,p)=>a>=M&&a<=p;m(c(3,4)||c(5,6)||a>6);const f=e*(c(3,4)?1.5:c(5,6)?2:2.5);let i=Math.round(r*f*n),u=n-i,l=u-s;for(let M=0;M<o;M++){if(i=Math.round(f*u),l-=i,l<0)return o-M===1?Math.round(u*.5):0;u-=i}return i}),y("EFFECT",2,2,(t,n)=>{const s=d(t),o=Math.trunc(d(n));return m(s>0&&o>=1),(1+s/o)**o-1}),y("NOMINAL",2,2,(t,n)=>{const s=d(t),o=Math.trunc(d(n));return m(s>0&&o>=1),o*((1+s)**(1/o)-1)});function Ct(t,n,s){const o=d(t),e=Math.trunc(d(n));if(m(e>=0),e===0)throw N.DIV0;const r=Math.trunc(o);return r+s(o-r,e,10**Math.ceil(Math.log10(e)))}y("DOLLARDE",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*e/o)),y("DOLLARFR",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*o/e));function lt(t,n,s,o){const e=D(t),r=D(n),a=d(s);return m(e<r&&r<=P(e,12,!1)&&a>0),o(a,r-e)}y("TBILLEQ",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>365*o/(360-o*e))),y("TBILLPRICE",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>100*(1-o*e/360))),y("TBILLYIELD",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>(100-o)/o*360/e)),y("ACCRINT",6,8,(t,n,s,o,e,r,a,c)=>{const f=D(t),i=D(n),u=D(s),l=d(o),M=d(R(e,1e3)),p=j(r),I=F(a),E=mt(R(c,!0));m(l>0&&M>0&&f<u);const b=!E&&u>i?i:f,g=12/p,A=Z(i);let C=0;for(;P(i,C*g,A)>b;)C--;for(;P(i,(C+1)*g,A)<=b;)C++;let U=0;for(;P(i,C*g,A)<u;C++){const L=P(i,C*g,A),v=P(i,(C+1)*g,A),T=I===1?v-L:(I===3?365:360)/p;U+=V(Math.max(L,b),Math.min(v,u),I)/T}return M*l*U/p}),y("ACCRINTM",4,5,(t,n,s,o,e)=>{const r=D(t),a=D(n),c=d(s),f=d(R(o,1e3)),i=F(e);return m(c>0&&f>0&&r<a),f*c*_(r,a,i)});const Q=(t,n)=>y(t,3,4,(s,o,e,r)=>n(tt(s,o,e,r,!0)));Q("COUPDAYBS",t=>t.a),Q("COUPDAYS",t=>t.e),Q("COUPDAYSNC",t=>t.dsc),Q("COUPNCD",t=>t.ncd),Q("COUPNUM",t=>t.n),Q("COUPPCD",t=>t.pcd);const W=(t,n)=>y(t,4,5,(s,o,e,r,a)=>{const c=D(s),f=D(o),i=d(e),u=d(r),l=F(a);return m(c<f&&i>0&&u>0),n(i,u,_(c,f,l))});W("DISC",(t,n,s)=>(n-t)/n/s),W("INTRATE",(t,n,s)=>(n-t)/t/s),W("RECEIVED",(t,n,s)=>{const o=1-n*s;return m(o>0),t/o}),W("PRICEDISC",(t,n,s)=>n*(1-t*s)),W("YIELDDISC",(t,n,s)=>(n/t-1)/s);const gt=(t,n,s)=>y(t,5,6,(o,e,r,a,c,f)=>{const i=D(o),u=D(e),l=D(r),M=d(a),p=d(c),I=F(f);return m(i<u&&l<i&&M>=0&&n(p)),s(M,p,_(l,u,I),_(l,i,I),_(i,u,I))});gt("PRICEMAT",t=>t>=0,(t,n,s,o,e)=>(100+s*t*100)/(1+e*n)-o*t*100),gt("YIELDMAT",t=>t>0,(t,n,s,o,e)=>{const r=n/100+o*t;return(1+s*t-r)/r/e});function Pt(t,n,s,o){const e=100*n/t.f,r=t.dsc/t.e;if(t.n===1)return(o+e)/(1+s/t.f*r)-e*t.a/t.e;const a=1+s/t.f;let c=o/a**(t.n-1+r);for(let f=0;f<t.n;f++)c+=e/a**(f+r);return c-e*t.a/t.e}y("PRICE",6,7,(t,n,s,o,e,r,a)=>{const c=d(s),f=d(o),i=d(e),u=tt(t,n,r,a);return m(c>=0&&f>=0&&i>0),Pt(u,c,f,i)}),y("YIELD",6,7,(t,n,s,o,e,r,a)=>{const c=d(s),f=d(o),i=d(e),u=tt(t,n,r,a);if(m(c>=0&&f>0&&i>0),u.n===1){const l=c/u.f,M=f/100+u.a/u.e*l;return(i/100+l-M)/M*(u.f*u.e/u.dsc)}return nt(l=>Pt(u,c,l,i)-f,.1,-u.f)});function Lt(t,n,s,o,e,r){const a=d(s),c=d(o),f=tt(t,n,e,r);m(a>=0&&c>=0);const i=1+c/f.f,u=100*a/f.f,l=f.dsc/f.e;let M=0,p=0;for(let I=0;I<f.n;I++){const E=I+l,b=(I===f.n-1?u+100:u)/i**E;M+=E*b,p+=b}return[M/p/f.f,i]}y("DURATION",5,6,(...t)=>Lt(...t)[0]),y("MDURATION",5,6,(...t)=>{const[n,s]=Lt(...t);return n/s});function Tt(t,n,s,o,e,r,a,c){const f=D(t),i=D(n),u=D(s),l=D(o),M=d(e),p=d(r),I=j(a),E=F(c);m(i>l&&l>f&&f>u&&M>=0&&p>0);const b=at(f,l,I,E),g=b.n===1?V(f,l,E):b.dsc,A=at(l,i,I,E).n,C=Z(l),U=12/I;let L=0,v=0;for(let k=0,w=l;;k++){const O=P(l,-(k+1)*U,C),S=E===1?w-O:b.e;L+=(O>u?S:V(u,w,E))/S;const ht=Math.max(u,O),Mt=Math.min(f,w);if(Mt>ht&&(v+=V(ht,Mt,E)/S),O<=u)break;w=O}const T=100*M/I,Y=b.n-1+g/b.e;return k=>{const w=1+k/I;let O=p/w**(A+Y)+T*L/w**Y;for(let S=1;S<=A;S++)O+=T/w**(S+Y);return O-T*v}}y("ODDFPRICE",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=d(r),u=Tt(t,n,s,o,e,a,c,f);return m(i>=0),u(i)}),y("ODDFYIELD",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=d(r),u=Tt(t,n,s,o,e,a,c,f);return m(i>0),nt(l=>u(l)-i,.1,-j(c))});function wt(t,n,s,o,e,r,a){const c=D(t),f=D(n),i=D(s),u=d(o),l=d(e),M=j(r),p=F(a);m(f>c&&c>i&&u>=0&&l>0);const I=12/M,E=Z(i);let b=0,g=0,A=0;for(let U=0;;U++){const L=P(i,U*I,E),v=P(i,(U+1)*I,E),T=p===0||p===4?360/M:v-L,Y=v>=f,k=Y?V(L,f,p):T;b+=k/T,v<c?g+=k/T:L<c&&(g+=V(L,c,p)/T);const w=Math.max(c,L),O=Math.min(f,v);if(O>w&&(A+=V(w,O,p)/T),Y)break}const C=100*u/M;return{income:l+b*C,interest:g*C,toGo:A,f:M}}y("ODDLPRICE",7,8,(t,n,s,o,e,r,a,c)=>{const f=d(e),i=wt(t,n,s,o,r,a,c);return m(f>=0),i.income/(1+i.toGo*f/i.f)-i.interest}),y("ODDLYIELD",7,8,(t,n,s,o,e,r,a,c)=>{const f=d(e),i=wt(t,n,s,o,r,a,c);m(f>0);const u=f+i.interest;return(i.income-u)/u*(i.f/i.toGo)});export{$ as days360,_ as yearFrac};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Arr as
|
|
1
|
+
import{Arr as h,bool as A,compareText as L,ERR as l,num as b,OMITTED as a,Ref as y,RefList as m,RichValue as x,str as R,textToNumber as w,XlError as c}from"../values.js";function d(n,o,r){if(o instanceof y)n.scan(o,r);else if(o instanceof m)for(const t of o.refs)n.scan(t,r);else if(o instanceof h)for(const t of o.data)r(t);else o!==a&&o!==void 0&&r(o)}function N(n,o){const r=[];for(const t of o)t instanceof y||t instanceof m||t instanceof h?d(n,t,s=>{if(typeof s=="number")r.push(s);else{if(s instanceof c)throw s;if(s instanceof x)throw l.VALUE}}):t!==a&&t!==void 0&&t!==null?r.push(b(t)):(t===a||t===null)&&r.push(0);return r}function S(n,o){const r=[];for(const t of o)if(t instanceof y||t instanceof m||t instanceof h){const s=!(t instanceof h);d(n,t,f=>{if(typeof f=="number")r.push(f);else if(typeof f=="boolean"&&s)r.push(f?1:0);else if(typeof f=="string")r.push(0);else{if(f instanceof c)throw f;if(f instanceof x)throw l.VALUE}})}else t!==a&&t!==void 0&&t!==null&&r.push(b(t));return r}function g(n,o){return n.toArr(o).data}function T(n,o,r){const t=g(n,o),s=g(n,r);if(t.length!==s.length)throw l.NA;const f=[],u=[];for(let e=0;e<t.length;e++){if(t[e]instanceof c)throw t[e];if(s[e]instanceof c)throw s[e];typeof t[e]=="number"&&typeof s[e]=="number"&&(f.push(t[e]),u.push(s[e]))}return[f,u]}function U(n,o,r,t){const s=n.scalar(o,r,t);if(s instanceof c)throw s;return s}function C(n,o,r,t){if(o instanceof m||!n.legacy&&o instanceof y&&(o.r0!==o.r1||o.c0!==o.c1))throw l.VALUE;const s=U(n,o,r,t);if(typeof s=="boolean")throw l.VALUE;return s}function E(n){let o="^";for(let r=0;r<n.length;r++){const t=n[r];t==="~"&&(n[r+1]==="*"||n[r+1]==="?"||n[r+1]==="~")?o+=n[++r].replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):t==="*"?o+="[\\s\\S]*":t==="?"?o+="[\\s\\S]":o+=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}return new RegExp(`${o}$`,"i")}const V=n=>/[*?~]/.test(n);function O(n){if(n instanceof c)return e=>e===n;if(typeof n=="number")return e=>e===n||typeof e=="string"&&w(e)===n;if(typeof n=="boolean")return e=>e===n;if(n==null||n===a)return e=>e===0||e===null;const o=String(n),r=/^(<=|>=|<>|<|>|=)([\s\S]*)$/.exec(o),t=r?r[1]:"=",s=r?r[2]:o,f=w(s);if(f!==null)switch(t){case"=":return e=>e===f||typeof e=="string"&&w(e)===f;case"<>":return e=>!(e===f||typeof e=="string"&&w(e)===f);case"<":return e=>typeof e=="number"&&e<f;case"<=":return e=>typeof e=="number"&&e<=f;case">":return e=>typeof e=="number"&&e>f;default:return e=>typeof e=="number"&&e>=f}const u=s.toUpperCase();if(u==="TRUE"||u==="FALSE"){const e=u==="TRUE";return t==="<>"?i=>i!==e:i=>i===e}if(t==="="||t==="<>"){if(s==="")return t==="="?i=>i===null:i=>i!==null;if(V(s)){const i=E(s);return t==="="?p=>typeof p=="string"&&i.test(p):p=>!(typeof p=="string"&&i.test(p))}const e=s.toLowerCase();return t==="="?i=>typeof i=="string"&&i.toLowerCase()===e:i=>!(typeof i=="string"&&i.toLowerCase()===e)}return e=>{if(typeof e!="string")return!1;const i=L(e,s);return t==="<"?i<0:t==="<="?i<=0:t===">"?i>0:i>=0}}function k(n,o){const r=n.toArr(o),t=[];for(let s=0;s<r.rows;s++){const f=[];for(let u=0;u<r.cols;u++){const e=r.get(s,u);if(typeof e!="number")throw e instanceof c?e:l.VALUE;f.push(e)}t.push(f)}return t}export{h as Arr,l as ERR,a as OMITTED,y as Ref,m as RefList,c as XlError,A as bool,N as collectNumbers,S as collectNumbersA,d as eachValue,g as flat,V as hasWildcards,O as makeCriteria,k as matrixOf,b as num,T as pairedNumbers,U as scalarOf,R as str,C as toolpakScalar,E as wildcardRegex};
|
package/calc/functions/index.js
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
import"./math.js";import"./logical.js";import"./lookup.js";import"./array.js";import"./text.js";import"./date.js";import"./info.js";import"./stat.js";import"./financial.js";import"./engineering.js";import"./database.js";import"../../celltypes/barcode.js";import"../../calcx/functions/index.js";import"../../sparklines/functions.js";
|
|
1
|
+
import"./math.js";import"./logical.js";import"./lookup.js";import"./array.js";import"./text.js";import"./date.js";import"./info.js";import"./stat.js";import"./financial.js";import"./engineering.js";import"./database.js";import"../../celltypes/barcode.js";import"../../calcx/functions/index.js";import"../../sparklines/functions.js";import{FUNCS as C}from"./registry.js";const D=`ACCRINT ACCRINTM AMORDEGRC AMORLINC BESSELI BESSELJ BESSELK BESSELY BIN2DEC BIN2HEX BIN2OCT COMPLEX
|
|
2
|
+
CONVERT COUPDAYBS COUPDAYS COUPDAYSNC COUPNCD COUPNUM COUPPCD CUMIPMT CUMPRINC DEC2BIN DEC2HEX DEC2OCT DELTA DISC
|
|
3
|
+
DOLLARDE DOLLARFR DURATION EDATE EFFECT EOMONTH ERF ERF.PRECISE ERFC ERFC.PRECISE FACTDOUBLE GESTEP HEX2BIN HEX2DEC
|
|
4
|
+
HEX2OCT IMABS IMAGINARY IMARGUMENT IMCONJUGATE IMCOS IMCOSH IMCOT IMCSC IMCSCH IMDIV IMEXP IMLN IMLOG10 IMLOG2
|
|
5
|
+
IMPOWER IMREAL IMSEC IMSECH IMSIN IMSINH IMSQRT IMSUB IMTAN INTRATE ISEVEN ISODD MDURATION MROUND NOMINAL OCT2BIN
|
|
6
|
+
OCT2DEC OCT2HEX PRICE PRICEDISC PRICEMAT QUOTIENT RECEIVED SQRTPI TBILLEQ TBILLPRICE TBILLYIELD WEEKNUM YEARFRAC YIELD
|
|
7
|
+
YIELDDISC YIELDMAT ODDFPRICE ODDFYIELD ODDLPRICE ODDLYIELD`.split(/\s+/),O={ACCRINT:[7]};for(const E of D){const I=C.get(E);I?.kind==="lift"&&(I.toolpak={logical:O[E]??[]})}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as
|
|
1
|
+
import{setCategory as B}from"./registry.js";import{lazy as N,lift as b,raw as m}from"./registry.js";import{Arr as l,bool as T,ERR as d,isOmitted as V,Lambda as Y,OMITTED as R,Ref as k,RefList as g,XlError as p}from"../values.js";import{compareValues as W}from"../values.js";import{eachValue as X}from"./helpers.js";B("Logical");for(const n of["LET","LAMBDA"])N(n,0,255,()=>{throw new Error(`${n} compiles directly`)});b("TRUE",0,0,()=>!0),b("FALSE",0,0,()=>!1),b("NOT",1,1,n=>!T(n));function C(n,t,r){let o=!1;for(const s of t)if(s instanceof k||s instanceof g||s instanceof l)X(n,s,e=>{if(e instanceof p)throw e;(typeof e=="number"||typeof e=="boolean")&&(o=!0,r(typeof e=="number"?e!==0:e))});else{if(s instanceof p)throw s;typeof s=="string"&&!/^(TRUE|FALSE)$/i.test(s)||(s!==R&&s!==null?(o=!0,r(T(s))):s===R&&(o=!0,r(!1)))}if(!o)throw d.VALUE}m("AND",1,255,(n,t)=>{let r=!0;return C(n,t,o=>{o||(r=!1)}),r},{array:!0}),m("OR",1,255,(n,t)=>{let r=!1;return C(n,t,o=>{o&&(r=!0)}),r},{array:!0}),m("XOR",1,255,(n,t)=>{let r=!1;return C(n,t,o=>{o&&(r=!r)}),r},{array:!0});const y=(n,t,r,o)=>t instanceof k?t.isCell?n.read(t.sheet,t.r0,t.c0):n.refAuto(t,r,o):t instanceof g?d.VALUE:t;function D(n){if(n instanceof p)throw n;return T(n)}function $(n,t,r,o,s,e,f){const a=r?r(s,e,f):!0,i=o?o(s,e,f):!1,c=y(n,a,s,e),u=y(n,i,s,e),O=Math.max(t.rows,c instanceof l?c.rows:1,u instanceof l?u.rows:1),L=Math.max(t.cols,c instanceof l?c.cols:1,u instanceof l?u.cols:1),U=(A,h,M)=>{if(!(A instanceof l))return A;const w=A.rows===1?0:h,S=A.cols===1?0:M;return w<A.rows&&S<A.cols?A.get(w,S):d.NA},F=new Array(O*L);for(let A=0;A<O;A++)for(let h=0;h<L;h++){const M=U(t,A,h);let w;if(M instanceof p)w=M;else try{w=T(M)?U(c,A,h):U(u,A,h)}catch(S){w=S}F[A*L+h]=w===R?0:w}return new l(O,L,F)}N("IF",2,3,(n,t,r,o,s)=>{const e=y(n,t[0](r,o,s),r,o);if(e instanceof l)return $(n,e,t[1],t[2],r,o,s);if(D(e)){const a=t[1](r,o,s);return a===R?0:a}if(t.length<3)return!1;const f=t[2](r,o,s);return f===R?0:f},{passes:n=>n>0}),N("IFS",2,254,(n,t,r,o,s)=>{if(t.length%2!==0)throw d.NA;for(let e=0;e<t.length;e+=2){const f=y(n,t[e](r,o,s),r,o),a=f instanceof l?f.data[0]:f;if(D(a))return t[e+1](r,o,s)}throw d.NA},{passes:n=>n%2===1}),N("IFERROR",2,2,(n,t,r,o,s)=>{let e=y(n,t[0](r,o,s),r,o);if(e instanceof l){const f=y(n,t[1](r,o,s),r,o);return new l(e.rows,e.cols,e.data.map(a=>a instanceof p?f instanceof l?f.data[0]:f:a))}return e instanceof p&&(e=t[1](r,o,s)),e===R?"":e},{passes:()=>!0}),N("IFNA",2,2,(n,t,r,o,s)=>{const e=y(n,t[0](r,o,s),r,o);if(e instanceof l){const f=y(n,t[1](r,o,s),r,o);return new l(e.rows,e.cols,e.data.map(a=>a===d.NA?f instanceof l?f.data[0]:f:a))}return e===d.NA?t[1](r,o,s):e},{passes:()=>!0}),N("SWITCH",3,255,(n,t,r,o,s)=>{const e=i=>{const c=y(n,i(r,o,s),r,o);return c instanceof l?c.data[0]:c},f=e(t[0]);if(f instanceof p)throw f;let a=1;for(;a+1<t.length;a+=2){const i=e(t[a]);if(i instanceof p)throw i;if(typeof i==typeof f&&W(i,f)===0)return t[a+1](r,o,s)}if(a<t.length)return t[a](r,o,s);throw d.NA},{passes:(n,t)=>n>1&&(n%2===0||n===t-1)});const I=n=>{if(n instanceof p)throw n;if(!(n instanceof Y))throw d.VALUE;return n},E=(n,t,r,o)=>{const s=t instanceof k?n.refAuto(t,r,o):t;return s instanceof l?s.data[0]:s};m("MAP",2,255,(n,t,r,o,s)=>{const e=I(t[t.length-1]),f=t.slice(0,-1).map(u=>n.toArr(u)),{rows:a,cols:i}=f[0],c=new Array(a*i);for(let u=0;u<c.length;u++){const O=n.invoke(e,f.map(L=>L.data[u]??d.NA),r,o,s);c[u]=E(n,O,r,o)}return new l(a,i,c)}),m("REDUCE",3,3,(n,t,r,o,s)=>{const e=!V(t[0]),f=I(t[2]),a=n.toArr(t[1]).data;let i=e?t[0]:a[0];for(let c=e?0:1;c<a.length;c++)i=n.invoke(f,[i,a[c]],r,o,s);return i}),m("SCAN",3,3,(n,t,r,o,s)=>{const e=!V(t[0]),f=I(t[2]),a=n.toArr(t[1]);let i=e?E(n,t[0],r,o):a.data[0];const c=new Array(a.data.length);for(let u=0;u<a.data.length;u++){if(u===0&&!e){c[0]=i;continue}i=E(n,n.invoke(f,[i,a.data[u]],r,o,s),r,o),c[u]=i}return new l(a.rows,a.cols,c)}),m("BYROW",2,2,(n,t,r,o,s)=>{const e=n.toArr(t[0]),f=I(t[1]),a=new Array(e.rows);for(let i=0;i<e.rows;i++){const c=new l(1,e.cols,e.data.slice(i*e.cols,(i+1)*e.cols));a[i]=E(n,n.invoke(f,[c],r,o,s),r,o)}return new l(e.rows,1,a)}),m("BYCOL",2,2,(n,t,r,o,s)=>{const e=n.toArr(t[0]),f=I(t[1]),a=new Array(e.cols);for(let i=0;i<e.cols;i++){const c=new Array(e.rows);for(let u=0;u<e.rows;u++)c[u]=e.get(u,i);a[i]=E(n,n.invoke(f,[new l(e.rows,1,c)],r,o,s),r,o)}return new l(1,e.cols,a)}),m("MAKEARRAY",3,3,(n,t,r,o,s)=>{const e=Math.trunc(E(n,t[0],r,o)),f=Math.trunc(E(n,t[1],r,o)),a=I(t[2]);if(!(e>=1)||!(f>=1))throw d.VALUE;const i=new Array(e*f);for(let c=0;c<e;c++)for(let u=0;u<f;u++)i[c*f+u]=E(n,n.invoke(a,[c+1,u+1],r,o,s),r,o);return new l(e,f,i)}),m("ISOMITTED",1,1,(n,t)=>t[0]===void 0||t[0]===R);
|
package/calc/functions/lookup.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as H}from"./registry.js";import{lazy as K,raw as E}from"./registry.js";import{Arr as p,compareValues as F,ERR as w,num as m,OMITTED as B,Ref as A,RefList as T,str as I,XlError as O}from"../values.js";import{hasWildcards as W,scalarOf as y,wildcardRegex as j}from"./helpers.js";import{colName as z,MAX_COLS as C,MAX_ROWS as _,parseRangeRef as G,sheetPrefix as P}from"../../core/address.js";import{FORMULA as Y}from"../../core/sheet.js";H("Lookup & Reference");const q=64,R=n=>n===void 0||n===B;function M(n,t){if(t instanceof O)throw t;if(t instanceof A){const r=n.trim(t),s=t.cols===1,e=t.rows===1,i=s?t.rows:e?t.cols:t.rows*t.cols;if(s)return{ref:t,length:i,usedLength:r.r1-t.r0+1,at:f=>n.read(t.sheet,t.r0+f,t.c0)};if(e)return{ref:t,length:i,usedLength:r.c1-t.c0+1,at:f=>n.read(t.sheet,t.r0,t.c0+f)};const o=n.toArr(t);return{length:o.data.length,usedLength:o.data.length,at:f=>o.data[f]}}return t instanceof p?{length:t.data.length,usedLength:t.data.length,at:r=>t.data[r]}:{length:1,usedLength:1,at:()=>t}}const U=(n,t)=>typeof n==typeof t;function V(n,t,r,s,e=!1){if(r==null&&(r=0),typeof r=="string"&&s&&W(r)){const i=j(r);if(e)for(let o=t.usedLength-1;o>=0;o--){const f=t.at(o);if(typeof f=="string"&&i.test(f))return o}else for(let o=0;o<t.usedLength;o++){const f=t.at(o);if(typeof f=="string"&&i.test(f))return o}return-1}if(!e&&t.ref&&t.usedLength>q){const i=n.exactIndex(t.ref),o=typeof r=="string"?`s${r.toLowerCase()}`:typeof r=="number"?r:`b${r}`,f=i.get(o);return f===void 0?-1:f}if(e){for(let i=t.usedLength-1;i>=0;i--){const o=t.at(i);if(o!==null&&U(o,r)&&F(o,r)===0)return i}return-1}for(let i=0;i<t.usedLength;i++){const o=t.at(i);if(o!==null&&U(o,r)&&F(o,r)===0)return i}return-1}function S(n,t){let r=0,s=n.usedLength-1,e=-1;for(;r<=s;){let i=r+s>>>1,o=i;for(;o<=s;){const f=n.at(o);if(f!==null&&U(f,t))break;o++}if(o>s){s=i-1;continue}i=o,F(n.at(i),t)<=0?(e=i,r=i+1):s=i-1}return e}function J(n,t){let r=-1;for(let s=0;s<n.usedLength;s++){const e=n.at(s);if(!(e===null||!U(e,t)))if(F(e,t)>=0)r=s;else break}return r}E("MATCH",2,3,(n,t,r,s)=>{const e=y(n,t[0],r,s),i=R(t[2])?1:m(n.scalar(t[2],r,s)),o=t[1]instanceof A&&!(t[1].rows===1||t[1].cols===1)?null:t[1];if(o===null)throw w.NA;if(o instanceof p&&o.rows>1&&o.cols>1)throw w.NA;const f=M(n,o);let c;if(i===0?c=V(n,f,e,!0):i>0?c=S(f,e===null?0:e):c=J(f,e===null?0:e),c<0)throw w.NA;return c+1},{array:!0});function X(n,t,r,s,e){const i=y(n,t[0],r,s);if(t[1]instanceof O)throw t[1];const o=Math.trunc(m(n.scalar(t[2],r,s))),f=R(t[3])?!0:!!m(n.scalar(t[3],r,s));if(o<1)throw w.VALUE;const c=t[1],h=i===null?0:i;let a,l;if(c instanceof A)if(e){if(o>c.cols)throw w.REF;a=M(n,new A(c.sheet,c.r0,c.c0,c.r1,c.c0)),l=g=>n.read(c.sheet,c.r0+g,c.c0+o-1)}else{if(o>c.rows)throw w.REF;a=M(n,new A(c.sheet,c.r0,c.c0,c.r0,c.c1)),l=g=>n.read(c.sheet,c.r0+o-1,c.c0+g)}else{const g=n.toArr(c),b=e?g.rows:g.cols;if(o>(e?g.cols:g.rows))throw w.REF;a={length:b,usedLength:b,at:L=>e?g.get(L,0):g.get(0,L)},l=L=>e?g.get(L,o-1):g.get(o-1,L)}const u=f?S(a,h):V(n,a,h,!0);if(u<0)throw w.NA;const d=l(u);return d===null?0:d}E("VLOOKUP",3,4,(n,t,r,s)=>X(n,t,r,s,!0),{array:!0}),E("HLOOKUP",3,4,(n,t,r,s)=>X(n,t,r,s,!1),{array:!0}),E("LOOKUP",2,3,(n,t,r,s)=>{const e=y(n,t[0],r,s),i=e===null?0:e;let o=t[1],f=R(t[2])?null:t[2];if(f===null){const l=n.toArr(o);l.rows>=l.cols?(o=new p(l.rows,1,Array.from({length:l.rows},(u,d)=>l.get(d,0))),f=new p(l.rows,1,Array.from({length:l.rows},(u,d)=>l.get(d,l.cols-1)))):(o=new p(1,l.cols,Array.from({length:l.cols},(u,d)=>l.get(0,d))),f=new p(1,l.cols,Array.from({length:l.cols},(u,d)=>l.get(l.rows-1,d))))}const c=M(n,o),h=S(c,i);if(h<0)throw w.NA;const a=M(n,f).at(h);return a===null?0:a},{array:!0});function k(n,t,r,s,e){const i=r===null?0:r;if(e===2||e===-2){const a=e===2;let l=0,u=t.usedLength-1,d=-1,g=-1,b=-1;for(;l<=u;){const L=l+u>>>1,N=F(t.at(L),i);if(N===0){d=L;break}(a?N<0:N>0)?(a?g=L:b=L,l=L+1):(a?b=L:g=L,u=L-1)}return d>=0?d:s===-1?g:s===1?b:-1}const o=e===-1;if(s===0)return V(n,t,i,!1,o);if(s===2)return V(n,t,i,!0,o);let f=-1,c=null;const h=t.usedLength;for(let a=0;a<h;a++){const l=o?h-1-a:a,u=t.at(l);if(u===null||!U(u,i))continue;const d=F(u,i);if(d===0)return l;(s===-1&&d<0&&(f<0||F(u,c)>0)||s===1&&d>0&&(f<0||F(u,c)<0))&&(f=l,c=u)}return f}function Q(n,t){const r=n.length;for(let s=0;s<r;s++){const e=t<0?r-1-s:s;if(n.at(e)===null)return e}return-1}E("XMATCH",2,4,(n,t,r,s)=>{const e=y(n,t[0],r,s),i=R(t[2])?0:Math.trunc(m(n.scalar(t[2],r,s))),o=R(t[3])?1:Math.trunc(m(n.scalar(t[3],r,s)));if(![0,-1,1,2].includes(i)||![1,-1,2,-2].includes(o))throw w.VALUE;const f=k(n,M(n,t[1]),e,i,o);if(f<0)throw w.NA;return f+1},{array:!0}),E("XLOOKUP",3,6,(n,t,r,s)=>{const e=y(n,t[0],r,s),i=R(t[4])?0:Math.trunc(m(n.scalar(t[4],r,s))),o=R(t[5])?1:Math.trunc(m(n.scalar(t[5],r,s)));if(![0,-1,1,2].includes(i)||![1,-1,2,-2].includes(o))throw w.VALUE;if(t[1]instanceof O)throw t[1];const f=t[1]instanceof A?t[1].rows:n.toArr(t[1]).rows,c=t[1]instanceof A?t[1].cols:n.toArr(t[1]).cols;if(f>1&&c>1)throw w.VALUE;const h=R(t[0])?Q(M(n,t[1]),o):k(n,M(n,t[1]),e,i,o);if(h<0){if(R(t[3]))throw w.NA;return t[3]}const a=c===1,l=t[2];if(l instanceof A)return a?new A(l.sheet,l.r0+h,l.c0,l.r0+h,l.c1):new A(l.sheet,l.r0,l.c0+h,l.r1,l.c0+h);const u=n.toArr(l);return a?u.cols===1?u.get(h,0):new p(1,u.cols,u.data.slice(h*u.cols,(h+1)*u.cols)):u.rows===1?u.get(0,h):new p(u.rows,1,Array.from({length:u.rows},(d,g)=>u.get(g,h)))},{array:!0,refResult:!0}),E("INDEX",2,4,(n,t,r,s)=>{let e=t[0];if(e instanceof O)throw e;const i=R(t[1])?0:Math.trunc(m(n.scalar(t[1],r,s))),o=R(t[2])?0:Math.trunc(m(n.scalar(t[2],r,s)));if(e instanceof T){const a=R(t[3])?1:Math.trunc(m(n.scalar(t[3],r,s)));if(a<1||a>e.refs.length)throw w.REF;e=e.refs[a-1]}if(i<0||o<0)throw w.VALUE;if(e instanceof A){let a=i,l=o;if((t.length===2||R(t[2]))&&e.rows===1&&e.cols>1&&(l=a,a=0),a>e.rows||l>e.cols)throw w.REF;const u=a===0?e.r0:e.r0+a-1,d=a===0?e.r1:u,g=l===0?e.c0:e.c0+l-1,b=l===0?e.c1:g;return new A(e.sheet,u,g,d,b)}const f=n.toArr(e);let c=i,h=o;if(t.length===2&&f.rows===1&&(h=c,c=0,h===0))return f;if(t.length===2&&f.cols===1&&(h=1),c>f.rows||h>f.cols)throw w.REF;return c===0&&h===0?f:c===0?new p(f.rows,1,Array.from({length:f.rows},(a,l)=>f.get(l,h-1))):h===0?new p(1,f.cols,f.data.slice((c-1)*f.cols,c*f.cols)):f.get(c-1,h-1)},{array:!0,refResult:!0}),E("OFFSET",3,5,(n,t,r,s)=>{const e=t[0];if(e instanceof O)throw e;if(!(e instanceof A))throw w.VALUE;const i=Math.trunc(m(n.scalar(t[1],r,s))),o=Math.trunc(m(n.scalar(t[2],r,s))),f=R(t[3])?e.rows:Math.trunc(m(n.scalar(t[3],r,s))),c=R(t[4])?e.cols:Math.trunc(m(n.scalar(t[4],r,s)));if(f===0||c===0)throw w.REF;const h=e.r0+i+(f<0?f+1:0),a=e.c0+o+(c<0?c+1:0),l=h+Math.abs(f)-1,u=a+Math.abs(c)-1;if(h<0||a<0||l>=_||u>=C)throw w.REF;return new A(e.sheet,h,a,l,u)},{volatile:!0,refResult:!0}),E("INDIRECT",1,2,(n,t,r,s,e)=>{const i=I(y(n,t[0],r,s)).trim(),o=R(t[1])?!0:!!y(n,t[1],r,s);let f=e,c=i;const h=i.lastIndexOf("!");if(h>=0){if(f=n.sheetByName(i.slice(0,h).replace(/^'|'$/g,"").replace(/''/g,"'")),f===null)throw w.REF;c=i.slice(h+1)}if(!o){const u=/^R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/i.exec(c);if(!u)throw w.REF;const d=($,D)=>$===void 0?D:$[0]==="["?D+Number($.slice(1,-1)):Number($)-1,g=d(u[1],r),b=d(u[2],s),L=u[3]===void 0&&u[4]===void 0?g:d(u[3],r),N=u[3]===void 0&&u[4]===void 0?b:d(u[4],s);if(g<0||b<0||L>=_||N>=C)throw w.REF;return new A(f,Math.min(g,L),Math.min(b,N),Math.max(g,L),Math.max(b,N))}const a=G(c.replace(/\$/g,""));if(a!==null)return new A(f,a.r0,a.c0,a.r1,a.c1);const l=n.evalName(c,r,s,f);if(l instanceof A)return l;throw w.REF},{volatile:!0,refResult:!0}),E("ROW",0,1,(n,t,r)=>{if(t.length===0||R(t[0]))return r+1;const s=t[0];if(s instanceof O)throw s;if(!(s instanceof A))throw w.VALUE;return s.rows===1?s.r0+1:new p(s.rows,1,Array.from({length:s.rows},(e,i)=>s.r0+i+1))}),E("COLUMN",0,1,(n,t,r,s)=>{if(t.length===0||R(t[0]))return s+1;const e=t[0];if(e instanceof O)throw e;if(!(e instanceof A))throw w.VALUE;return e.cols===1?e.c0+1:new p(1,e.cols,Array.from({length:e.cols},(i,o)=>e.c0+o+1))}),E("ROWS",1,1,(n,t)=>{const r=t[0];if(r instanceof O)throw r;if(r instanceof A||r instanceof p)return r.rows;if(r instanceof T)throw w.REF;return 1}),E("COLUMNS",1,1,(n,t)=>{const r=t[0];if(r instanceof O)throw r;if(r instanceof A||r instanceof p)return r.cols;if(r instanceof T)throw w.REF;return 1}),E("AREAS",1,1,(n,t)=>{if(t[0]instanceof O)throw t[0];if(t[0]instanceof T)return t[0].refs.length;if(t[0]instanceof A)return 1;throw w.VALUE}),E("ADDRESS",2,5,(n,t,r,s)=>{const e=Math.trunc(m(n.scalar(t[0],r,s))),i=Math.trunc(m(n.scalar(t[1],r,s))),o=R(t[2])?1:Math.trunc(m(n.scalar(t[2],r,s))),f=R(t[3])?!0:!!n.scalar(t[3],r,s),c=R(t[4])?null:I(n.scalar(t[4],r,s));if(e<1||i<1||e>_||i>C||o<1||o>4)throw w.VALUE;const h=o===1||o===2,a=o===1||o===3,l=f?`${a?"$":""}${z(i-1)}${h?"$":""}${e}`:`R${h?e:`[${e}]`}C${a?i:`[${i}]`}`;if(c===null)return l;if(c==="")return`!${l}`;const u=/^\[([^\]]*)\](.*)$/.exec(c);return`${u?P(u[2],{book:u[1]}):P(c)}${l}`}),K("CHOOSE",2,255,(n,t,r,s,e)=>{const i=n.scalar(t[0](r,s,e),r,s);if(i instanceof O)throw i;const o=Math.trunc(m(i));if(o<1||o>=t.length)throw w.VALUE;return t[o](r,s,e)},{passes:n=>n>0}),E("FORMULATEXT",1,1,(n,t)=>{const r=t[0];if(!(r instanceof A))throw w.NA;if((r.sheet.kind(r.r0,r.c0)&Y)===0)throw w.NA;if(r.sheet.protection&&n.book.styles.get(r.sheet.style(r.r0,r.c0)).hiddenFormula)throw w.NA;return n.book.editText(r.sheet,r.r0,r.c0)}),E("HYPERLINK",1,2,(n,t,r,s)=>{const e=R(t[1])?t[0]:t[1],i=n.scalar(e,r,s);return i===null?0:i}),E("GETPIVOTDATA",2,255,()=>{throw w.REF});
|
|
1
|
+
import{setCategory as B}from"./registry.js";import{lazy as j,raw as y}from"./registry.js";import{Arr as R,bool as z,compareValues as U,ERR as g,num as p,OMITTED as G,Ref as L,RefList as V,str as _,XlError as O}from"../values.js";import{hasWildcards as Y,scalarOf as F,wildcardRegex as q}from"./helpers.js";import{colName as J,MAX_COLS as S,MAX_ROWS as D,parseRangeRef as Q,sheetPrefix as X}from"../../core/address.js";import{FORMULA as Z}from"../../core/sheet.js";B("Lookup & Reference");const x=64,E=e=>e===void 0||e===G;function $(e,t){if(t instanceof O)throw t;if(t instanceof L){const n=e.trim(t),o=t.cols===1,r=t.rows===1,s=o?t.rows:r?t.cols:t.rows*t.cols;if(o)return{ref:t,length:s,usedLength:n.r1-t.r0+1,at:f=>e.read(t.sheet,t.r0+f,t.c0)};if(r)return{ref:t,length:s,usedLength:n.c1-t.c0+1,at:f=>e.read(t.sheet,t.r0,t.c0+f)};const i=e.toArr(t);return{length:i.data.length,usedLength:i.data.length,at:f=>i.data[f]}}return t instanceof R?{length:t.data.length,usedLength:t.data.length,at:n=>t.data[n]}:{length:1,usedLength:1,at:()=>t}}const C=(e,t)=>typeof e==typeof t;function T(e,t,n,o,r=!1){if(n==null&&(n=0),typeof n=="string"&&o&&Y(n)){const s=q(n);if(r)for(let i=t.usedLength-1;i>=0;i--){const f=t.at(i);if(typeof f=="string"&&s.test(f))return i}else for(let i=0;i<t.usedLength;i++){const f=t.at(i);if(typeof f=="string"&&s.test(f))return i}return-1}if(!r&&t.ref&&t.usedLength>x){const s=e.exactIndex(t.ref),i=typeof n=="string"?`s${n.toLowerCase()}`:typeof n=="number"?n:`b${n}`,f=s.get(i);return f===void 0?-1:f}if(r){for(let s=t.usedLength-1;s>=0;s--){const i=t.at(s);if(i!==null&&C(i,n)&&U(i,n)===0)return s}return-1}for(let s=0;s<t.usedLength;s++){const i=t.at(s);if(i!==null&&C(i,n)&&U(i,n)===0)return s}return-1}function I(e,t){let n=0,o=e.usedLength-1,r=-1;for(;n<=o;){let s=n+o>>>1,i=s;for(;i<=o;){const f=e.at(i);if(f!==null&&C(f,t))break;i++}if(i>o){o=s-1;continue}s=i,U(e.at(s),t)<=0?(r=s,n=s+1):o=s-1}return r}function v(e,t){let n=-1;for(let o=0;o<e.usedLength;o++){const r=e.at(o);if(!(r===null||!C(r,t)))if(U(r,t)>=0)n=o;else break}return n}y("MATCH",2,3,(e,t,n,o)=>b(e,t,[0,2],r=>{const s=F(e,r[0],n,o),i=E(r[2])?1:p(e.scalar(r[2],n,o));if(!(r[1]instanceof L||r[1]instanceof R||r[1]instanceof V))throw p(F(e,r[1],n,o)),g.NA;const f=r[1]instanceof L&&!(r[1].rows===1||r[1].cols===1)?null:r[1];if(f===null)throw g.NA;if(f instanceof R&&f.rows>1&&f.cols>1)throw g.NA;const l=$(e,f);let a;if(i===0?a=T(e,l,s,!0):i>0?a=I(l,s===null?0:s):a=v(l,s===null?0:s),a<0)throw g.NA;return a+1}),{array:!0});function H(e,t,n,o,r,s,i,f){const l=Math.trunc(p(e.scalar(o,s,i)));if(typeof r=="string")throw g.VALUE;const a=E(r)?!0:z(r),w=t===null?0:t;let h,c,u;if(n instanceof L)u=f?n.c1-n.c0+1:n.r1-n.r0+1,h=$(e,f?new L(n.sheet,n.r0,n.c0,n.r1,n.c0):new L(n.sheet,n.r0,n.c0,n.r0,n.c1)),c=m=>f?e.read(n.sheet,n.r0+m,n.c0+l-1):e.read(n.sheet,n.r0+l-1,n.c0+m);else{if(!(n instanceof R))throw p(n),g.NA;const m=n,M=f?m.rows:m.cols;u=f?m.cols:m.rows,h={length:M,usedLength:M,at:N=>f?m.get(N,0):m.get(0,N)},c=N=>f?m.get(N,l-1):m.get(l-1,N)}const d=a?I(h,w):T(e,h,w,!0);if(d<0)throw g.NA;if(l<1)throw g.VALUE;if(l>u)throw g.REF;const A=c(d);return A===null?0:A}const P=e=>e instanceof L&&(e.r0!==e.r1||e.c0!==e.c1)||e instanceof R&&e.data.length>1;function b(e,t,n,o){if(e.legacy)return o(t);const r=n.map(l=>P(t[l])?e.toArr(t[l]):null);if(r.every(l=>l===null))return o(t);const s=Math.max(...r.map(l=>l?.rows??1)),i=Math.max(...r.map(l=>l?.cols??1)),f=new Array(s*i);for(let l=0;l<s;l++)for(let a=0;a<i;a++){const w=t.slice();let h=!1;r.forEach((u,d)=>{u&&(u.rows!==1&&l>=u.rows||u.cols!==1&&a>=u.cols?h=!0:w[n[d]]=u.get(u.rows===1?0:l,u.cols===1?0:a))});let c;if(h)c=g.NA;else try{c=o(w)}catch(u){if(!(u instanceof O))throw u;c=u}c instanceof L?c=e.read(c.sheet,c.r0,c.c0):c instanceof R&&(c=c.data[0]),f[l*i+a]=c===null?0:c}return new R(s,i,f)}function K(e,t,n,o,r){if(t[1]instanceof O)throw t[1];const s=t[1];if(e.legacy||!P(t[0])&&!P(t[3])){const u=E(t[3])?t[3]:F(e,t[3],n,o);return H(e,F(e,t[0],n,o),s,t[2],u,n,o,r)}const i=e.toArr(t[0]),f=E(t[3])?null:e.toArr(t[3]),l=Math.max(i.rows,f?.rows??1),a=Math.max(i.cols,f?.cols??1),w=(u,d,A)=>u.get(u.rows===1?0:d,u.cols===1?0:A),h=(u,d,A)=>(u.rows===1||d<u.rows)&&(u.cols===1||A<u.cols),c=new Array(l*a);for(let u=0;u<l;u++)for(let d=0;d<a;d++){if(!h(i,u,d)||f&&!h(f,u,d)){c[u*a+d]=g.NA;continue}try{c[u*a+d]=H(e,w(i,u,d),s,t[2],f?w(f,u,d):t[3],n,o,r)}catch(A){if(!(A instanceof O))throw A;c[u*a+d]=A}}return new R(l,a,c)}y("VLOOKUP",3,4,(e,t,n,o)=>K(e,t,n,o,!0),{array:!0}),y("HLOOKUP",3,4,(e,t,n,o)=>K(e,t,n,o,!1),{array:!0}),y("LOOKUP",2,3,(e,t,n,o)=>{const r=F(e,t[0],n,o),s=r===null?0:r;let i=t[1],f=E(t[2])?null:t[2];if(f===null){const h=e.toArr(i);h.rows>=h.cols?(i=new R(h.rows,1,Array.from({length:h.rows},(c,u)=>h.get(u,0))),f=new R(h.rows,1,Array.from({length:h.rows},(c,u)=>h.get(u,h.cols-1)))):(i=new R(1,h.cols,Array.from({length:h.cols},(c,u)=>h.get(0,u))),f=new R(1,h.cols,Array.from({length:h.cols},(c,u)=>h.get(h.rows-1,u))))}const l=$(e,i),a=I(l,s);if(a<0)throw g.NA;const w=$(e,f).at(a);return w===null?0:w},{array:!0});function W(e,t,n,o,r){const s=n===null?0:n;if(r===2||r===-2){const w=r===2;let h=0,c=t.usedLength-1,u=-1,d=-1,A=-1;for(;h<=c;){const m=h+c>>>1,M=U(t.at(m),s);if(M===0){u=m;break}(w?M<0:M>0)?(w?d=m:A=m,h=m+1):(w?A=m:d=m,c=m-1)}return u>=0?u:o===-1?d:o===1?A:-1}const i=r===-1;if(o===0)return T(e,t,s,!1,i);if(o===2)return T(e,t,s,!0,i);let f=-1,l=null;const a=t.usedLength;for(let w=0;w<a;w++){const h=i?a-1-w:w,c=t.at(h);if(c===null||!C(c,s))continue;const u=U(c,s);if(u===0)return h;(o===-1&&u<0&&(f<0||U(c,l)>0)||o===1&&u>0&&(f<0||U(c,l)<0))&&(f=h,l=c)}return f}function tt(e,t){const n=e.length;for(let o=0;o<n;o++){const r=t<0?n-1-o:o;if(e.at(r)===null)return r}return-1}y("XMATCH",2,4,(e,t,n,o)=>{const r=F(e,t[0],n,o),s=E(t[2])?0:Math.trunc(p(e.scalar(t[2],n,o))),i=E(t[3])?1:Math.trunc(p(e.scalar(t[3],n,o)));if(![0,-1,1,2].includes(s)||![1,-1,2,-2].includes(i))throw g.VALUE;const f=W(e,$(e,t[1]),r,s,i);if(f<0)throw g.NA;return f+1},{array:!0}),y("XLOOKUP",3,6,(e,t,n,o)=>b(e,t,[0],r=>{const s=F(e,r[0],n,o),i=E(r[4])?0:Math.trunc(p(e.scalar(r[4],n,o))),f=E(r[5])?1:Math.trunc(p(e.scalar(r[5],n,o)));if(![0,-1,1,2].includes(i)||![1,-1,2,-2].includes(f))throw g.VALUE;if(r[1]instanceof O)throw r[1];const l=r[1]instanceof L?r[1].rows:e.toArr(r[1]).rows,a=r[1]instanceof L?r[1].cols:e.toArr(r[1]).cols;if(l>1&&a>1)throw g.VALUE;if(r[2]instanceof O)throw r[2];const w=r[2]instanceof L?r[2].rows:e.toArr(r[2]).rows,h=r[2]instanceof L?r[2].cols:e.toArr(r[2]).cols;if(l*a>1&&(a===1?w!==l:h!==a))throw g.VALUE;const c=E(r[0])?tt($(e,r[1]),f):W(e,$(e,r[1]),s,i,f);if(c<0){if(E(r[3]))throw g.NA;return r[3]}const u=a===1,d=r[2];if(d instanceof L)return u?new L(d.sheet,d.r0+c,d.c0,d.r0+c,d.c1):new L(d.sheet,d.r0,d.c0+c,d.r1,d.c0+c);const A=e.toArr(d);return u?A.cols===1?A.get(c,0):new R(1,A.cols,A.data.slice(c*A.cols,(c+1)*A.cols)):A.rows===1?A.get(0,c):new R(A.rows,1,Array.from({length:A.rows},(m,M)=>A.get(M,c)))}),{array:!0,refResult:!0}),y("INDEX",2,4,(e,t,n,o)=>b(e,t,[1,2],r=>{let s=r[0];if(s instanceof O)throw s;const i=E(r[1])?0:Math.trunc(p(e.scalar(r[1],n,o))),f=E(r[2])?0:Math.trunc(p(e.scalar(r[2],n,o)));if(s instanceof V){const h=E(r[3])?1:Math.trunc(p(e.scalar(r[3],n,o)));if(h<1||h>s.refs.length)throw g.REF;s=s.refs[h-1]}if(i<0||f<0)throw g.VALUE;if(s instanceof L){let h=i,c=f;if((r.length===2||E(r[2]))&&s.rows===1&&s.cols>1&&(c=h,h=0),h>s.rows||c>s.cols)throw g.REF;const u=h===0?s.r0:s.r0+h-1,d=h===0?s.r1:u,A=c===0?s.c0:s.c0+c-1,m=c===0?s.c1:A;return new L(s.sheet,u,A,d,m)}const l=e.toArr(s);let a=i,w=f;if(r.length===2&&l.rows===1&&l.cols>1&&(w=a,a=0,w===0))return l;if(r.length===2&&l.cols===1&&(w=1),a>l.rows||w>l.cols)throw g.REF;return a===0&&w===0?l:a===0?new R(l.rows,1,Array.from({length:l.rows},(h,c)=>l.get(c,w-1))):w===0?new R(1,l.cols,l.data.slice((a-1)*l.cols,a*l.cols)):l.get(a-1,w-1)}),{array:!0,refResult:!0}),y("OFFSET",3,5,(e,t,n,o)=>{const r=t[0];if(r instanceof O)throw r;if(!(r instanceof L))throw g.VALUE;const s=Math.trunc(p(e.scalar(t[1],n,o))),i=Math.trunc(p(e.scalar(t[2],n,o))),f=E(t[3])?r.rows:Math.trunc(p(e.scalar(t[3],n,o))),l=E(t[4])?r.cols:Math.trunc(p(e.scalar(t[4],n,o)));if(f===0||l===0)throw g.REF;const a=r.r0+s+(f<0?f+1:0),w=r.c0+i+(l<0?l+1:0),h=a+Math.abs(f)-1,c=w+Math.abs(l)-1;if(a<0||w<0||h>=D||c>=S)throw g.REF;return new L(r.sheet,a,w,h,c)},{volatile:!0,refResult:!0}),y("INDIRECT",1,2,(e,t,n,o,r)=>{const s=_(F(e,t[0],n,o)).trim(),i=E(t[1])?!0:!!F(e,t[1],n,o);let f=r,l=s;const a=s.lastIndexOf("!");if(a>=0){if(f=e.sheetByName(s.slice(0,a).replace(/^'|'$/g,"").replace(/''/g,"'")),f===null)throw g.REF;l=s.slice(a+1)}if(!i){const c=/^R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/i.exec(l);if(!c)throw g.REF;const u=(N,k)=>N===void 0?k:N[0]==="["?k+Number(N.slice(1,-1)):Number(N)-1,d=u(c[1],n),A=u(c[2],o),m=c[3]===void 0&&c[4]===void 0?d:u(c[3],n),M=c[3]===void 0&&c[4]===void 0?A:u(c[4],o);if(d<0||A<0||m>=D||M>=S)throw g.REF;return new L(f,Math.min(d,m),Math.min(A,M),Math.max(d,m),Math.max(A,M))}const w=Q(l.replace(/\$/g,""));if(w!==null)return new L(f,w.r0,w.c0,w.r1,w.c1);const h=e.evalName(l,n,o,f);if(h instanceof L)return h;throw g.REF},{volatile:!0,refResult:!0}),y("ROW",0,1,(e,t,n)=>{if(t.length===0||E(t[0]))return n+1;const o=t[0];if(o instanceof O)throw o;if(!(o instanceof L))throw g.VALUE;return o.rows===1?o.r0+1:new R(o.rows,1,Array.from({length:o.rows},(r,s)=>o.r0+s+1))}),y("COLUMN",0,1,(e,t,n,o)=>{if(t.length===0||E(t[0]))return o+1;const r=t[0];if(r instanceof O)throw r;if(!(r instanceof L))throw g.VALUE;return r.cols===1?r.c0+1:new R(1,r.cols,Array.from({length:r.cols},(s,i)=>r.c0+i+1))}),y("ROWS",1,1,(e,t)=>{const n=t[0];if(n instanceof O)throw n;if(n instanceof L||n instanceof R)return n.rows;if(n instanceof V)throw g.REF;return 1}),y("COLUMNS",1,1,(e,t)=>{const n=t[0];if(n instanceof O)throw n;if(n instanceof L||n instanceof R)return n.cols;if(n instanceof V)throw g.REF;return 1}),y("AREAS",1,1,(e,t)=>{if(t[0]instanceof O)throw t[0];if(t[0]instanceof V)return t[0].refs.length;if(t[0]instanceof L)return 1;throw g.VALUE}),y("ADDRESS",2,5,(e,t,n,o)=>{const r=Math.trunc(p(e.scalar(t[0],n,o))),s=Math.trunc(p(e.scalar(t[1],n,o))),i=E(t[2])?1:Math.trunc(p(e.scalar(t[2],n,o))),f=E(t[3])?!0:!!e.scalar(t[3],n,o),l=E(t[4])?null:_(e.scalar(t[4],n,o));if(r<1||s<1||r>D||s>S||i<1||i>4)throw g.VALUE;const a=i===1||i===2,w=i===1||i===3,h=f?`${w?"$":""}${J(s-1)}${a?"$":""}${r}`:`R${a?r:`[${r}]`}C${w?s:`[${s}]`}`;if(l===null)return h;if(l==="")return`!${h}`;const c=/^\[([^\]]*)\](.*)$/.exec(l);return`${c?X(c[2],{book:c[1]}):X(l)}${h}`}),j("CHOOSE",2,255,(e,t,n,o,r)=>{const s=e.scalar(t[0](n,o,r),n,o);if(s instanceof O)throw s;const i=Math.trunc(p(s));if(i<1||i>=t.length)throw g.VALUE;return t[i](n,o,r)},{passes:e=>e>0}),y("FORMULATEXT",1,1,(e,t)=>{const n=t[0];if(!(n instanceof L))throw g.NA;if((n.sheet.kind(n.r0,n.c0)&Z)===0)throw g.NA;if(n.sheet.protection&&e.book.styles.get(n.sheet.style(n.r0,n.c0)).hiddenFormula)throw g.NA;return e.book.editText(n.sheet,n.r0,n.c0)}),y("HYPERLINK",1,2,(e,t,n,o)=>{if(E(t[1])){const s=e.scalar(t[0],n,o);return s instanceof O?s:s===null?"":_(s)}const r=e.scalar(t[1],n,o);return r===null?0:r}),y("GETPIVOTDATA",2,255,()=>{throw g.REF});
|
package/calc/functions/math.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as z}from"./registry.js";import{alias as J,lift as u,raw as m}from"./registry.js";import{Arr as d,bool as K,checked as L,ERR as c,int as b,num as i,OMITTED as A,opt as Z,power as $,Ref as p,RefList as O,str as F,XlError as V}from"../values.js";import{collectNumbers as q,eachValue as x,makeCriteria as tt,matrixOf as R}from"./helpers.js";z("Math & Trig");const l=(t,r)=>u(t,1,1,o=>L(r(i(o))));l("ABS",Math.abs),l("ACOS",t=>{if(t<-1||t>1)throw c.NUM;return Math.acos(t)}),l("ACOSH",t=>{if(t<1)throw c.NUM;return Math.acosh(t)}),l("ACOT",t=>Math.PI/2-Math.atan(t)),l("ACOTH",t=>{if(Math.abs(t)<=1)throw c.NUM;return .5*Math.log((t+1)/(t-1))}),l("ASIN",t=>{if(t<-1||t>1)throw c.NUM;return Math.asin(t)}),l("ASINH",Math.asinh),l("ATAN",Math.atan),l("ATANH",t=>{if(t<=-1||t>=1)throw c.NUM;return Math.atanh(t)}),l("COS",Math.cos),l("COSH",Math.cosh),l("COT",t=>{if(t===0)throw c.DIV0;return 1/Math.tan(t)}),l("COTH",t=>{if(t===0)throw c.DIV0;return 1/Math.tanh(t)}),l("CSC",t=>{if(t===0)throw c.DIV0;return 1/Math.sin(t)}),l("CSCH",t=>{if(t===0)throw c.DIV0;return 1/Math.sinh(t)}),l("DEGREES",t=>t*180/Math.PI),l("EXP",Math.exp),l("INT",Math.floor),l("LN",t=>{if(t<=0)throw c.NUM;return Math.log(t)}),l("LOG10",t=>{if(t<=0)throw c.NUM;return Math.log10(t)}),l("RADIANS",t=>t*Math.PI/180),l("SEC",t=>1/Math.cos(t)),l("SECH",t=>1/Math.cosh(t)),l("SIGN",Math.sign),l("SIN",Math.sin),l("SINH",Math.sinh),l("SQRT",t=>{if(t<0)throw c.NUM;return Math.sqrt(t)}),l("SQRTPI",t=>{if(t<0)throw c.NUM;return Math.sqrt(t*Math.PI)}),l("TAN",Math.tan),l("TANH",Math.tanh),u("ATAN2",2,2,(t,r)=>{const o=i(t),n=i(r);if(o===0&&n===0)throw c.DIV0;return Math.atan2(n,o)}),u("LOG",1,2,(t,r)=>{const o=i(t),n=r===void 0||r===A?10:i(r);if(o<=0||n<=0)throw c.NUM;if(n===1)throw c.DIV0;return n===10?Math.log10(o):Math.log(o)/Math.log(n)}),u("PI",0,0,()=>Math.PI),u("POWER",2,2,(t,r)=>{const o=i(t),n=i(r);if(o===0&&n===0)throw c.NUM;if(o===0&&n<0)throw c.DIV0;return L($(o,n))}),u("MOD",2,2,(t,r)=>{const o=i(t),n=i(r);if(n===0)throw c.DIV0;if(Math.abs(o/n)>=2**40)throw c.NUM;return L(o-n*Math.floor(o/n))}),u("QUOTIENT",2,2,(t,r)=>{const o=i(r);if(o===0)throw c.DIV0;return Math.trunc(i(t)/o)});function k(t,r){if(!Number.isFinite(t))throw c.NUM;r>15&&(r=15);const o=10**Math.abs(r),n=r>=0?Math.abs(t)*o:Math.abs(t)/o,e=Number(n.toPrecision(15)),s=Math.floor(e+.5),f=r>=0?s/o:s*o;return t<0?-f:f}function y(t,r,o){r>15&&(r=15);const n=10**Math.abs(r),e=r>=0?Math.abs(t)*n:Math.abs(t)/n,s=Number(e.toPrecision(15)),f=o?Math.ceil(s):Math.floor(s),a=r>=0?f/n:f*n;return t<0?-a:a}u("ROUND",2,2,(t,r)=>k(i(t),b(r))),u("ROUNDUP",2,2,(t,r)=>y(i(t),b(r),!0)),u("ROUNDDOWN",2,2,(t,r)=>y(i(t),b(r),!1)),u("TRUNC",1,2,(t,r)=>y(i(t),r===void 0||r===A?0:b(r),!1)),u("EVEN",1,1,t=>{const r=i(t),o=Math.ceil(Number((Math.abs(r)/2).toPrecision(15)))*2;return r<0?-o:o}),u("ODD",1,1,t=>{const r=Math.abs(i(t));let o=Math.ceil(Number(r.toPrecision(15)));return o%2===0&&(o+=1),i(t)<0?-o:o}),u("MROUND",2,2,(t,r)=>{const o=i(t),n=i(r);if(n===0)return 0;if(o*n<0)throw c.NUM;return k(o/n,0)*n});function g(t,r){const o=Number((t/r).toPrecision(15));return Math.ceil(o)*r}function S(t,r){const o=Number((t/r).toPrecision(15));return Math.floor(o)*r}u("CEILING",2,2,(t,r)=>{const o=i(t),n=i(r);if(n===0)return 0;if(o>0&&n<0)throw c.NUM;return o<0&&n<0?-g(-o,-n):g(o,n)}),u("FLOOR",2,2,(t,r)=>{const o=i(t),n=i(r);if(n===0){if(o===0)return 0;throw c.DIV0}if(o>0&&n<0)throw c.NUM;return o<0&&n<0?-S(-o,-n):S(o,n)}),u("CEILING.MATH",1,3,(t,r,o)=>{const n=i(t),e=Math.abs(r===void 0||r===A?1:i(r))||0;if(e===0)return 0;const s=o===void 0||o===A?0:i(o);return n<0&&s!==0?-g(-n,e):g(n,e)}),u("FLOOR.MATH",1,3,(t,r,o)=>{const n=i(t),e=Math.abs(r===void 0||r===A?1:i(r))||0;if(e===0)return 0;const s=o===void 0||o===A?0:i(o);return n<0&&s!==0?-S(-n,e):S(n,e)}),u("CEILING.PRECISE",1,2,(t,r)=>{const o=Math.abs(r===void 0||r===A?1:i(r));return o===0?0:g(i(t),o)}),J("ISO.CEILING","CEILING.PRECISE"),u("FLOOR.PRECISE",1,2,(t,r)=>{const o=Math.abs(r===void 0||r===A?1:i(r));return o===0?0:S(i(t),o)});function rt(t){if(t<0)throw c.NUM;if(t>170)throw c.NUM;let r=1;for(let o=2;o<=t;o++)r*=o;return r}u("FACT",1,1,t=>rt(Math.floor(i(t)))),u("FACTDOUBLE",1,1,t=>{const r=Math.floor(i(t));if(r<-1)throw c.NUM;let o=1;for(let n=r;n>1;n-=2)o*=n;return L(o)});function P(t,r){if(r<0||t<0||r>t)throw c.NUM;const o=Math.min(r,t-r);let n=1;for(let e=1;e<=o;e++)n=n*(t-o+e)/e;return L(Math.round(n))}u("COMBIN",2,2,(t,r)=>P(Math.trunc(i(t)),Math.trunc(i(r)))),u("COMBINA",2,2,(t,r)=>{const o=Math.trunc(i(t)),n=Math.trunc(i(r));if(o<0||n<0)throw c.NUM;return o===0&&n===0?1:o===0?0:P(o+n-1,n)});function j(t,r){for(;r!==0;)[t,r]=[r,t%r];return t}const H=(t,r)=>{const o=[];for(const n of r)if(n instanceof p||n instanceof O||n instanceof d)x(t,n,e=>{if(e instanceof V)throw e;if(typeof e=="number"){if(e<0)throw c.NUM;o.push(Math.trunc(e))}else if(typeof e=="string")throw c.VALUE});else{const e=Math.trunc(i(n));if(e<0)throw c.NUM;o.push(e)}return o};m("GCD",1,255,(t,r)=>H(t,r).reduce(j,0)),m("LCM",1,255,(t,r)=>{let o=1;for(const n of H(t,r)){if(n===0)return 0;o=o/j(o,n)*n}return L(o)}),m("MULTINOMIAL",1,255,(t,r)=>{const o=H(t,r);let n=0,e=1;for(const s of o)n+=s,e*=P(n,s);return L(e)}),u("ARABIC",1,1,t=>{const r=F(t).trim().toUpperCase();if(r==="")return 0;const o=r[0]==="-",n=o?r.slice(1):r,e={M:1e3,D:500,C:100,L:50,X:10,V:5,I:1};let s=0;for(let f=0;f<n.length;f++){const a=e[n[f]];if(a===void 0)throw c.VALUE;const h=e[n[f+1]]??0;s+=a<h?-a:a}return o?-s:s}),u("ROMAN",1,2,(t,r)=>{const o=Math.trunc(i(t)),n=r===void 0||r===A?0:typeof r=="boolean"?r?0:4:Math.trunc(i(r));if(o<0||o>3999)throw c.VALUE;if(n<0||n>4)throw c.VALUE;if(o===0)return"";const e={0:[[1e3,"M"],[900,"CM"],[500,"D"],[400,"CD"],[100,"C"],[90,"XC"],[50,"L"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],1:[[1e3,"M"],[950,"LM"],[900,"CM"],[500,"D"],[450,"LD"],[400,"CD"],[100,"C"],[95,"VC"],[90,"XC"],[50,"L"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],2:[[1e3,"M"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],3:[[1e3,"M"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],4:[[1e3,"M"],[999,"IM"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[499,"ID"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]]};let s=o,f="";for(const[a,h]of e[n])for(;s>=a;)f+=h,s-=a;return f}),u("BASE",2,3,(t,r,o)=>{const n=Math.trunc(i(t)),e=Math.trunc(i(r)),s=o===void 0||o===A?0:Math.trunc(i(o));if(n<0||n>=2**53||e<2||e>36||s<0||s>255)throw c.NUM;return n.toString(e).toUpperCase().padStart(s,"0")}),u("DECIMAL",2,2,(t,r)=>{const o=F(t).trim().toLowerCase(),n=Math.trunc(i(r));if(n<2||n>36||o.length>255)throw c.NUM;let e=0;for(const s of o){const f=parseInt(s,36);if(Number.isNaN(f)||f>=n)throw c.NUM;e=e*n+f}return L(e)}),u("RAND",0,0,()=>Math.random(),{volatile:!0}),u("RANDBETWEEN",2,2,(t,r)=>{const o=Math.ceil(i(t)),n=Math.floor(i(r));if(o>n)throw c.NUM;return o+Math.floor(Math.random()*(n-o+1))},{volatile:!0}),m("SUM",1,255,(t,r)=>{let o=0;const n={sum:0,count:0};for(const e of r)if(typeof e=="number")o+=e;else if(e instanceof p)t.sumRange(e,n);else if(e instanceof O)for(const s of e.refs)t.sumRange(s,n);else if(e instanceof d){for(const s of e.data)if(typeof s=="number")o+=s;else if(s instanceof V)throw s}else e!==null&&e!==A&&(o+=i(e));return L(o+n.sum)}),m("PRODUCT",1,255,(t,r)=>{let o=1,n=!1;for(const e of q(t,r))o*=e,n=!0;return n?L(o):0}),m("SUMSQ",1,255,(t,r)=>L(q(t,r).reduce((o,n)=>o+n*n,0)));function nt(t,r,o,n){return{row:t.r0+(o-r.r0),col:t.c0+(n-r.c0)}}function G(t,r,o){const[n,e]=o[0],s=n.rows,f=n.cols;for(const[N]of o)if(N.rows!==s||N.cols!==f)throw c.VALUE;let a=0,h=0;const M=(N,E)=>{for(let U=1;U<o.length;U++){const[C,D]=o[U];if(!D(t.read(C.sheet,C.r0+(N-n.r0),C.c0+(E-n.c0))))return!1}return!0},w=e(null),I=(N,E,U)=>{if(!(!e(N)||!M(E,U))&&(h++,r!==null)){const C=nt(r,n,E,U),D=t.read(r.sheet,C.row,C.col);if(D instanceof V)throw D;typeof D=="number"&&(a+=D)}};if(w){const N=Math.min(n.r1,Math.max(n.sheet.maxRow,n.r0)),E=Math.min(n.c1,Math.max(n.sheet.maxCol,n.c0));for(let U=n.c0;U<=E;U++)for(let C=n.r0;C<=N;C++)I(t.read(n.sheet,C,U),C,U)}else t.scan(n,I);return{sum:a,count:h}}const T=t=>{if(t instanceof V)throw t;if(!(t instanceof p))throw c.VALUE;return t},Q=(t,r,o,n)=>{const e=t.scalar(r,o,n);return tt(e)};m("SUMIF",2,3,(t,r,o,n)=>{const e=T(r[0]),s=r[2]===void 0||r[2]===A?e:T(r[2]);return G(t,s,[[e,Q(t,r[1],o,n)]]).sum}),m("SUMIFS",3,255,(t,r,o,n)=>{if(r.length%2===0)throw c.VALUE;const e=[];for(let f=1;f<r.length;f+=2)e.push([T(r[f]),Q(t,r[f+1],o,n)]);const s=T(r[0]);if(s.rows!==e[0][0].rows||s.cols!==e[0][0].cols)throw c.VALUE;return G(t,s,e).sum}),m("SUMPRODUCT",1,255,(t,r)=>{const o=r.map(a=>{if(a instanceof V)throw a;return t.toArr(a)}),{rows:n,cols:e}=o[0];for(const a of o)if(a.rows!==n||a.cols!==e)throw c.VALUE;let s=0;const f=n*e;for(let a=0;a<f;a++){let h=1;for(const M of o){const w=M.data[a];if(w instanceof V)throw w;h*=typeof w=="number"?w:0}s+=h}return L(s)},{array:!0});const ot=(t,r,o)=>{const n=t.toArr(r),e=t.toArr(o);if(n.rows!==e.rows||n.cols!==e.cols)throw c.NA;return[n.data,e.data]},v=(t,r)=>m(t,2,2,(o,n)=>{const[e,s]=ot(o,n[0],n[1]);let f=0;for(let a=0;a<e.length;a++){if(e[a]instanceof V)throw e[a];if(s[a]instanceof V)throw s[a];typeof e[a]=="number"&&typeof s[a]=="number"&&(f+=r(e[a],s[a]))}return L(f)},{array:!0});v("SUMX2MY2",(t,r)=>t*t-r*r),v("SUMX2PY2",(t,r)=>t*t+r*r),v("SUMXMY2",(t,r)=>(t-r)**2),m("SERIESSUM",4,4,(t,r,o,n)=>{const e=i(t.scalar(r[0],o,n)),s=i(t.scalar(r[1],o,n)),f=i(t.scalar(r[2],o,n));let a=0,h=0;for(const M of t.toArr(r[3]).data)a+=i(M)*e**(s+h*f),h++;return L(a)});function W(t,r,o,n,e){const{sheet:s}=r;t.scan(r,(f,a,h)=>{if(!(!o&&(s.rows.isHidden(a)||s.filteredRows?.has(a)))&&!t.isSubtotalCell(s,a,h)){if(f instanceof V){if(n)return;throw f}e.push(f)}})}function Y(t,r,o){const n=r.length,e=()=>r.reduce((a,h)=>a+h,0),s=()=>e()/n,f=a=>{const h=s();return r.reduce((M,w)=>M+(w-h)**2,0)/(a?n:n-1)};switch(t){case 1:if(n===0)throw c.DIV0;return s();case 2:return n;case 3:return o.length;case 4:return n===0?0:Math.max(...r);case 5:return n===0?0:Math.min(...r);case 6:return n===0?0:r.reduce((a,h)=>a*h,1);case 7:if(n<2)throw c.DIV0;return Math.sqrt(f(!1));case 8:if(n===0)throw c.DIV0;return Math.sqrt(f(!0));case 9:return e();case 10:if(n<2)throw c.DIV0;return f(!1);case 11:if(n===0)throw c.DIV0;return f(!0);default:throw c.VALUE}}m("SUBTOTAL",2,255,(t,r,o,n)=>{const e=Math.trunc(i(t.scalar(r[0],o,n))),s=e>=1&&e<=11,f=e>100?e-100:e;if(f<1||f>11)throw c.VALUE;const a=[];for(let h=1;h<r.length;h++){const M=r[h]instanceof O?r[h].refs:[r[h]];for(const w of M){if(!(w instanceof p))throw c.VALUE;W(t,w,s,!1,a)}}return Y(f,a.filter(h=>typeof h=="number"),a)});function X(t,r){const o=t.length;if(o===0||r<0||r>1)throw c.NUM;const n=r*(o-1),e=Math.floor(n),s=n-e;return s===0?t[e]:t[e]+s*(t[e+1]-t[e])}function B(t,r){const o=t.length,n=r*(o+1)-1;if(r<=0||r>=1||n<0||n>o-1)throw c.NUM;const e=Math.floor(n),s=n-e;return s===0?t[e]:t[e]+s*(t[e+1]-t[e])}m("AGGREGATE",3,255,(t,r,o,n)=>{const e=Math.trunc(i(t.scalar(r[0],o,n))),s=Math.trunc(i(t.scalar(r[1],o,n)));if(s<0||s>7)throw c.VALUE;const f=s===1||s===3||s===5||s===7,a=s===2||s===3||s===6||s===7,h=I=>{const N=[],E=I instanceof O?I.refs:[I];for(const U of E)if(U instanceof p)W(t,U,!f,a,N);else if(U instanceof d)for(const C of U.data)if(C instanceof V){if(!a)throw C}else C!==null&&N.push(C);else if(U instanceof V){if(!a)throw U}else U!==A&&U!==null&&U!==void 0&&N.push(i(U));return N};if(e>=1&&e<=11){const I=[];for(let N=2;N<r.length;N++){if(r[N]instanceof d)throw c.VALUE;I.push(...h(r[N]))}return Y(e,I.filter(N=>typeof N=="number"),I)}const M=h(r[2]).filter(I=>typeof I=="number").sort((I,N)=>I-N),w=i(t.scalar(r[3],o,n));switch(e){case 12:return X(M,.5);case 13:return _(M);case 14:{const I=Math.ceil(w);if(I<1||I>M.length)throw c.NUM;return M[M.length-I]}case 15:{const I=Math.ceil(w);if(I<1||I>M.length)throw c.NUM;return M[I-1]}case 16:return X(M,w);case 17:return X(M,w/4);case 18:return B(M,w);case 19:return B(M,w/4);default:throw c.VALUE}},{array:!0});function _(t){const r=new Map;let o=null,n=1;for(const e of t){const s=(r.get(e)??0)+1;r.set(e,s),s>n&&(n=s,o=e)}if(o===null)throw c.NA;return o}function et(t){const r=t.length,o=t.map(e=>e.slice());let n=1;for(let e=0;e<r;e++){let s=e;for(let f=e+1;f<r;f++)Math.abs(o[f][e])>Math.abs(o[s][e])&&(s=f);if(o[s][e]===0)return 0;s!==e&&([o[s],o[e]]=[o[e],o[s]],n=-n),n*=o[e][e];for(let f=e+1;f<r;f++){const a=o[f][e]/o[e][e];for(let h=e;h<r;h++)o[f][h]-=a*o[e][h]}}return n}m("MDETERM",1,1,(t,r)=>{const o=R(t,r[0]);if(o.length!==o[0].length)throw c.VALUE;return et(o)},{array:!0}),m("MINVERSE",1,1,(t,r)=>{const o=R(t,r[0]),n=o.length;if(n!==o[0].length)throw c.VALUE;const e=o.map((s,f)=>[...s,...s.map((a,h)=>f===h?1:0)]);for(let s=0;s<n;s++){let f=s;for(let h=s+1;h<n;h++)Math.abs(e[h][s])>Math.abs(e[f][s])&&(f=h);if(Math.abs(e[f][s])<1e-300)throw c.NUM;[e[f],e[s]]=[e[s],e[f]];const a=e[s][s];for(let h=0;h<2*n;h++)e[s][h]/=a;for(let h=0;h<n;h++){if(h===s)continue;const M=e[h][s];for(let w=0;w<2*n;w++)e[h][w]-=M*e[s][w]}}return d.of2D(e.map(s=>s.slice(n)))},{array:!0}),m("MMULT",2,2,(t,r)=>{const o=R(t,r[0]),n=R(t,r[1]);if(o[0].length!==n.length)throw c.VALUE;const e=[];for(let s=0;s<o.length;s++){const f=[];for(let a=0;a<n[0].length;a++){let h=0;for(let M=0;M<n.length;M++)h+=o[s][M]*n[M][a];f.push(h)}e.push(f)}return d.of2D(e)},{array:!0}),u("MUNIT",1,1,t=>{const r=Math.trunc(i(t));if(r<1)throw c.VALUE;const o=new Array(r*r).fill(0);for(let n=0;n<r;n++)o[n*r+n]=1;return new d(r,r,o)});export{K as bool,_ as modeOf,Z as opt,B as percentileExc,X as percentileInc,G as sumIfs};
|
|
1
|
+
import{setCategory as $}from"./registry.js";import{alias as x,lift as l,raw as m}from"./registry.js";import{Arr as D,bool as tt,checked as L,ERR as c,int as R,num as a,OMITTED as A,opt as rt,power as nt,Ref as p,RefList as b,str as k,XlError as V}from"../values.js";import{collectNumbers as j,eachValue as ot,makeCriteria as et,matrixOf as T,toolpakScalar as P}from"./helpers.js";$("Math & Trig");const M=(t,r)=>l(t,1,1,o=>L(r(a(o)))),st=2**27,g=t=>r=>{if(Math.abs(r)>=st)throw c.NUM;return t(r)},Q=t=>r=>{if(!Number.isFinite(r*r))throw c.NUM;return t(r)},W=t=>r=>{if(!Number.isFinite(Math.exp(Math.abs(r))))throw c.NUM;return t(r)};M("ABS",Math.abs),M("ACOS",t=>{if(t<-1||t>1)throw c.NUM;return Math.acos(t)}),M("ACOSH",Q(t=>{if(t<1)throw c.NUM;return Math.acosh(t)})),M("ACOT",t=>Math.PI/2-Math.atan(t)),M("ACOTH",t=>{if(Math.abs(t)<=1)throw c.NUM;return .5*Math.log((t+1)/(t-1))}),M("ASIN",t=>{if(t<-1||t>1)throw c.NUM;return Math.asin(t)}),M("ASINH",Q(Math.asinh)),M("ATAN",Math.atan),M("ATANH",t=>{if(t<=-1||t>=1)throw c.NUM;return Math.atanh(t)}),M("COS",g(Math.cos)),M("COSH",W(Math.cosh)),M("COT",g(t=>{if(t===0)throw c.DIV0;return 1/Math.tan(t)})),M("COTH",t=>{if(t===0)throw c.DIV0;return 1/Math.tanh(t)}),M("CSC",g(t=>{if(t===0)throw c.DIV0;return 1/Math.sin(t)})),M("CSCH",t=>{if(t===0)throw c.DIV0;return 1/Math.sinh(t)}),M("DEGREES",t=>t*180/Math.PI),M("EXP",Math.exp),M("INT",Math.floor),M("LN",t=>{if(t<=0)throw c.NUM;return Math.log(t)}),M("LOG10",t=>{if(t<=0)throw c.NUM;return Math.log10(t)}),M("RADIANS",t=>t*Math.PI/180),M("SEC",g(t=>1/Math.cos(t))),M("SECH",t=>1/Math.cosh(t)),M("SIGN",Math.sign),M("SIN",g(Math.sin)),M("SINH",W(Math.sinh)),M("SQRT",t=>{if(t<0)throw c.NUM;return Math.sqrt(t)}),M("SQRTPI",t=>{if(t<0)throw c.NUM;return Math.sqrt(t*Math.PI)}),M("TAN",g(Math.tan)),M("TANH",Math.tanh),l("ATAN2",2,2,(t,r)=>{const o=a(t),n=a(r);if(o===0&&n===0)throw c.DIV0;return Math.atan2(n,o)}),l("LOG",1,2,(t,r)=>{const o=a(t),n=r===void 0||r===A?10:a(r);if(o<=0||n<=0)throw c.NUM;if(n===1)throw c.DIV0;return n===10?Math.log10(o):Math.log(o)/Math.log(n)}),l("PI",0,0,()=>Math.PI),l("POWER",2,2,(t,r)=>{const o=a(t),n=a(r);if(o===0&&n===0)throw c.NUM;if(o===0&&n<0)throw c.DIV0;return L(nt(o,n))}),l("MOD",2,2,(t,r)=>{const o=a(t),n=a(r);if(n===0)throw c.DIV0;if(Math.abs(o/n)>=2**40)throw c.NUM;return L(o-n*Math.floor(o/n))}),l("QUOTIENT",2,2,(t,r)=>{const o=a(r);if(o===0)throw c.DIV0;return Math.trunc(a(t)/o)});function Y(t,r){if(!Number.isFinite(t))throw c.NUM;r>15&&(r=15);const o=10**Math.abs(r),n=r>=0?Math.abs(t)*o:Math.abs(t)/o,e=Number(n.toPrecision(15)),s=Math.floor(e+.5),f=r>=0?s/o:s*o;return t<0?-f:f}function H(t,r,o){r>15&&(r=15);const n=10**Math.abs(r),e=r>=0?Math.abs(t)*n:Math.abs(t)/n,s=Number(e.toPrecision(15)),f=o?Math.ceil(s):Math.floor(s),i=r>=0?f/n:f*n;return t<0?-i:i}l("ROUND",2,2,(t,r)=>Y(a(t),R(r))),l("ROUNDUP",2,2,(t,r)=>H(a(t),R(r),!0)),l("ROUNDDOWN",2,2,(t,r)=>H(a(t),R(r),!1)),l("TRUNC",1,2,(t,r)=>H(a(t),r===void 0||r===A?0:R(r),!1)),l("EVEN",1,1,t=>{const r=a(t),o=Math.ceil(Number((Math.abs(r)/2).toPrecision(15)))*2;return r<0?-o:o}),l("ODD",1,1,t=>{const r=Math.abs(a(t));let o=Math.ceil(Number(r.toPrecision(15)));return o%2===0&&(o+=1),a(t)<0?-o:o}),l("MROUND",2,2,(t,r)=>{const o=a(t),n=a(r);if(n===0)return 0;if(o*n<0)throw c.NUM;return Y(o/n,0)*n});function S(t,r){const o=Number((t/r).toPrecision(15));return Math.ceil(o)*r}function O(t,r){const o=Number((t/r).toPrecision(15));return Math.floor(o)*r}l("CEILING",2,2,(t,r)=>{const o=a(t),n=a(r);if(n===0)return 0;if(o>0&&n<0)throw c.NUM;return o<0&&n<0?-S(-o,-n):S(o,n)}),l("FLOOR",2,2,(t,r)=>{const o=a(t),n=a(r);if(n===0){if(o===0)return 0;throw c.DIV0}if(o>0&&n<0)throw c.NUM;return o<0&&n<0?-O(-o,-n):O(o,n)}),l("CEILING.MATH",1,3,(t,r,o)=>{const n=a(t),e=Math.abs(r===void 0||r===A?1:a(r))||0;if(e===0)return 0;const s=o===void 0||o===A?0:a(o);return n<0&&s!==0?-S(-n,e):S(n,e)}),l("FLOOR.MATH",1,3,(t,r,o)=>{const n=a(t),e=Math.abs(r===void 0||r===A?1:a(r))||0;if(e===0)return 0;const s=o===void 0||o===A?0:a(o);return n<0&&s!==0?-O(-n,e):O(n,e)}),l("CEILING.PRECISE",1,2,(t,r)=>{const o=Math.abs(r===void 0||r===A?1:a(r));return o===0?0:S(a(t),o)}),x("ISO.CEILING","CEILING.PRECISE"),l("FLOOR.PRECISE",1,2,(t,r)=>{const o=Math.abs(r===void 0||r===A?1:a(r));return o===0?0:O(a(t),o)});function ct(t){if(t<0)throw c.NUM;if(t>170)throw c.NUM;let r=1;for(let o=2;o<=t;o++)r*=o;return r}l("FACT",1,1,t=>ct(Math.floor(a(t)))),l("FACTDOUBLE",1,1,t=>{const r=Math.floor(a(t));if(r<-1)throw c.NUM;let o=1;for(let n=r;n>1;n-=2)if(o*=n,!Number.isFinite(o))throw c.NUM;return L(o)});function F(t,r){if(r<0||t<0||r>t)throw c.NUM;const o=Math.min(r,t-r);let n=1;for(let e=1;e<=o;e++)n=n*(t-o+e)/e;return L(Math.round(n))}l("COMBIN",2,2,(t,r)=>F(Math.trunc(a(t)),Math.trunc(a(r)))),l("COMBINA",2,2,(t,r)=>{const o=Math.trunc(a(t)),n=Math.trunc(a(r));if(o<0||n<0)throw c.NUM;return o===0&&n===0?1:o===0?0:F(o+n-1,n)});function _(t,r){for(;r!==0;)[t,r]=[r,t%r];return t}const G=(t,r)=>{const o=[];for(const n of r)if(n instanceof p||n instanceof b||n instanceof D)ot(t,n,e=>{if(e instanceof V)throw e;if(typeof e=="number"){if(e<0)throw c.NUM;o.push(Math.trunc(e))}else if(typeof e=="string")throw c.VALUE});else{const e=Math.trunc(a(n));if(e<0)throw c.NUM;o.push(e)}return o};m("GCD",1,255,(t,r)=>G(t,r).reduce(_,0)),m("LCM",1,255,(t,r)=>{let o=1;for(const n of G(t,r)){if(n===0)return 0;o=o/_(o,n)*n}return L(o)}),m("MULTINOMIAL",1,255,(t,r)=>{const o=G(t,r);let n=0,e=1;for(const s of o)n+=s,e*=F(n,s);return L(e)}),l("ARABIC",1,1,t=>{const r=k(t).trim().toUpperCase();if(r==="")return 0;const o=r[0]==="-",n=o?r.slice(1):r,e={M:1e3,D:500,C:100,L:50,X:10,V:5,I:1};let s=0;for(let f=0;f<n.length;f++){const i=e[n[f]];if(i===void 0)throw c.VALUE;const h=e[n[f+1]]??0;s+=i<h?-i:i}return o?-s:s}),l("ROMAN",1,2,(t,r)=>{const o=Math.trunc(a(t)),n=r===void 0||r===A?0:typeof r=="boolean"?r?0:4:Math.trunc(a(r));if(o<0||o>3999)throw c.VALUE;if(n<0||n>4)throw c.VALUE;if(o===0)return"";const e={0:[[1e3,"M"],[900,"CM"],[500,"D"],[400,"CD"],[100,"C"],[90,"XC"],[50,"L"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],1:[[1e3,"M"],[950,"LM"],[900,"CM"],[500,"D"],[450,"LD"],[400,"CD"],[100,"C"],[95,"VC"],[90,"XC"],[50,"L"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],2:[[1e3,"M"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],3:[[1e3,"M"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],4:[[1e3,"M"],[999,"IM"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[499,"ID"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]]};let s=o,f="";for(const[i,h]of e[n])for(;s>=i;)f+=h,s-=i;return f}),l("BASE",2,3,(t,r,o)=>{const n=Math.trunc(a(t)),e=Math.trunc(a(r)),s=o===void 0||o===A?0:Math.trunc(a(o));if(n<0||n>=2**53||e<2||e>36||s<0||s>255)throw c.NUM;return n.toString(e).toUpperCase().padStart(s,"0")}),l("DECIMAL",2,2,(t,r)=>{const o=k(t).trim().toLowerCase(),n=Math.trunc(a(r));if(n<2||n>36||o.length>255)throw c.NUM;let e=0;for(const s of o){const f=parseInt(s,36);if(Number.isNaN(f)||f>=n)throw c.NUM;e=e*n+f}return L(e)}),l("RAND",0,0,()=>Math.random(),{volatile:!0}),l("RANDBETWEEN",2,2,(t,r)=>{const o=Math.ceil(a(t)),n=Math.floor(a(r));if(o>n)throw c.NUM;return o+Math.floor(Math.random()*(n-o+1))},{volatile:!0}),m("SUM",1,255,(t,r)=>{let o=0;const n={sum:0,count:0};for(const e of r)if(typeof e=="number")o+=e;else if(e instanceof p)t.sumRange(e,n);else if(e instanceof b)for(const s of e.refs)t.sumRange(s,n);else if(e instanceof D){for(const s of e.data)if(typeof s=="number")o+=s;else if(s instanceof V)throw s}else e!==null&&e!==A&&(o+=a(e));return L(o+n.sum)}),m("PRODUCT",1,255,(t,r)=>{let o=1,n=!1;for(const e of j(t,r))o*=e,n=!0;return n?L(o):0}),m("SUMSQ",1,255,(t,r)=>L(j(t,r).reduce((o,n)=>o+n*n,0)));function ft(t,r,o,n){return{row:t.r0+(o-r.r0),col:t.c0+(n-r.c0)}}function v(t,r,o){const[n,e]=o[0],s=n.rows,f=n.cols;for(const[U]of o)if(U.rows!==s||U.cols!==f)throw c.VALUE;let i=0,h=0;const w=(U,E)=>{for(let I=1;I<o.length;I++){const[C,d]=o[I];if(!d(t.read(C.sheet,C.r0+(U-n.r0),C.c0+(E-n.c0))))return!1}return!0},N=e(null),u=(U,E,I)=>{if(!(!e(U)||!w(E,I))&&(h++,r!==null)){const C=ft(r,n,E,I),d=t.read(r.sheet,C.row,C.col);if(d instanceof V)throw d;typeof d=="number"&&(i+=d)}};if(N){const U=Math.min(n.r1,Math.max(n.sheet.maxRow,n.r0)),E=Math.min(n.c1,Math.max(n.sheet.maxCol,n.c0));for(let I=n.c0;I<=E;I++)for(let C=n.r0;C<=U;C++)u(t.read(n.sheet,C,I),C,I)}else t.scan(n,u);return{sum:i,count:h}}const X=t=>{if(t instanceof V)throw t;if(!(t instanceof p))throw c.VALUE;return t},z=(t,r,o,n)=>{const e=t.scalar(r,o,n);return et(e)};m("SUMIF",2,3,(t,r,o,n)=>{const e=X(r[0]),s=r[2]===void 0||r[2]===A?e:X(r[2]);return v(t,s,[[e,z(t,r[1],o,n)]]).sum}),m("SUMIFS",3,255,(t,r,o,n)=>{if(r.length%2===0)throw c.VALUE;const e=[];for(let f=1;f<r.length;f+=2)e.push([X(r[f]),z(t,r[f+1],o,n)]);const s=X(r[0]);if(s.rows!==e[0][0].rows||s.cols!==e[0][0].cols)throw c.VALUE;return v(t,s,e).sum}),m("SUMPRODUCT",1,255,(t,r)=>{const o=r.map(i=>{if(i instanceof V)throw i;return t.toArr(i)}),{rows:n,cols:e}=o[0];for(const i of o)if(i.rows!==n||i.cols!==e)throw c.VALUE;let s=0;const f=n*e;for(let i=0;i<f;i++){let h=1;for(const w of o){const N=w.data[i];if(N instanceof V)throw N;h*=typeof N=="number"?N:0}s+=h}return L(s)},{array:!0});const it=(t,r,o)=>{const n=t.toArr(r),e=t.toArr(o);if(n.rows!==e.rows||n.cols!==e.cols)throw c.NA;return[n.data,e.data]},B=(t,r)=>m(t,2,2,(o,n)=>{const[e,s]=it(o,n[0],n[1]);let f=0;for(let i=0;i<e.length;i++){if(e[i]instanceof V)throw e[i];if(s[i]instanceof V)throw s[i];typeof e[i]=="number"&&typeof s[i]=="number"&&(f+=r(e[i],s[i]))}return L(f)},{array:!0});B("SUMX2MY2",(t,r)=>t*t-r*r),B("SUMX2PY2",(t,r)=>t*t+r*r),B("SUMXMY2",(t,r)=>(t-r)**2),m("SERIESSUM",4,4,(t,r,o,n)=>{const e=a(P(t,r[0],o,n)),s=a(P(t,r[1],o,n)),f=a(P(t,r[2],o,n));let i=0,h=0;for(const w of t.toArr(r[3]).data)i+=a(w)*e**(s+h*f),h++;return L(i)});function J(t,r,o,n,e){const{sheet:s}=r;t.scan(r,(f,i,h)=>{if(!(!o&&(s.rows.isHidden(i)||s.filteredRows?.has(i)))&&!t.isSubtotalCell(s,i,h)){if(f instanceof V){if(n)return;throw f}e.push(f)}})}function K(t,r,o){const n=r.length,e=()=>r.reduce((i,h)=>i+h,0),s=()=>e()/n,f=i=>{const h=s();return r.reduce((w,N)=>w+(N-h)**2,0)/(i?n:n-1)};switch(t){case 1:if(n===0)throw c.DIV0;return s();case 2:return n;case 3:return o.length;case 4:return n===0?0:Math.max(...r);case 5:return n===0?0:Math.min(...r);case 6:return n===0?0:r.reduce((i,h)=>i*h,1);case 7:if(n<2)throw c.DIV0;return Math.sqrt(f(!1));case 8:if(n===0)throw c.DIV0;return Math.sqrt(f(!0));case 9:return e();case 10:if(n<2)throw c.DIV0;return f(!1);case 11:if(n===0)throw c.DIV0;return f(!0);default:throw c.VALUE}}m("SUBTOTAL",2,255,(t,r,o,n)=>{const e=Math.trunc(a(t.scalar(r[0],o,n))),s=e>=1&&e<=11,f=e>100?e-100:e;if(f<1||f>11)throw c.VALUE;const i=[];for(let h=1;h<r.length;h++){const w=r[h]instanceof b?r[h].refs:[r[h]];for(const N of w){if(!(N instanceof p))throw c.VALUE;J(t,N,s,!1,i)}}return K(f,i.filter(h=>typeof h=="number"),i)});function y(t,r){const o=t.length;if(o===0||r<0||r>1)throw c.NUM;const n=r*(o-1),e=Math.floor(n),s=n-e;return s===0?t[e]:t[e]+s*(t[e+1]-t[e])}function q(t,r){const o=t.length,n=r*(o+1)-1;if(r<=0||r>=1||n<0||n>o-1)throw c.NUM;const e=Math.floor(n),s=n-e;return s===0?t[e]:t[e]+s*(t[e+1]-t[e])}m("AGGREGATE",3,255,(t,r,o,n)=>{const e=Math.trunc(a(t.scalar(r[0],o,n))),s=Math.trunc(a(t.scalar(r[1],o,n)));if(s<0||s>7)throw c.VALUE;const f=s===1||s===3||s===5||s===7,i=s===2||s===3||s===6||s===7,h=u=>{const U=[],E=u instanceof b?u.refs:[u];for(const I of E)if(I instanceof p)J(t,I,!f,i,U);else if(I instanceof D)for(const C of I.data)if(C instanceof V){if(!i)throw C}else C!==null&&U.push(C);else if(I instanceof V){if(!i)throw I}else I!==A&&I!==null&&I!==void 0&&U.push(a(I));return U};if(e>=1&&e<=13){for(let u=2;u<r.length;u++)if(r[u]instanceof V)throw r[u];for(let u=2;u<r.length;u++)if(!(r[u]instanceof p||r[u]instanceof b))throw c.VALUE}if(e>=1&&e<=11){const u=[];for(let U=2;U<r.length;U++)u.push(...h(r[U]));return K(e,u.filter(U=>typeof U=="number"),u)}const w=h(r[2]).filter(u=>typeof u=="number").sort((u,U)=>u-U),N=a(t.scalar(r[3],o,n));switch(e){case 12:return y(w,.5);case 13:return Z(w);case 14:{const u=Math.ceil(N);if(u<1||u>w.length)throw c.NUM;return w[w.length-u]}case 15:{const u=Math.ceil(N);if(u<1||u>w.length)throw c.NUM;return w[u-1]}case 16:return y(w,N);case 17:return y(w,N/4);case 18:return q(w,N);case 19:return q(w,N/4);default:throw c.VALUE}},{array:!0});function Z(t){const r=new Map;let o=null,n=1;for(const e of t){const s=(r.get(e)??0)+1;r.set(e,s),s>n&&(n=s,o=e)}if(o===null)throw c.NA;return o}function at(t){const r=t.length,o=t.map(e=>e.slice());let n=1;for(let e=0;e<r;e++){let s=e;for(let f=e+1;f<r;f++)Math.abs(o[f][e])>Math.abs(o[s][e])&&(s=f);if(o[s][e]===0)return 0;s!==e&&([o[s],o[e]]=[o[e],o[s]],n=-n),n*=o[e][e];for(let f=e+1;f<r;f++){const i=o[f][e]/o[e][e];for(let h=e;h<r;h++)o[f][h]-=i*o[e][h]}}return n}m("MDETERM",1,1,(t,r)=>{const o=T(t,r[0]);if(o.length!==o[0].length)throw c.VALUE;return at(o)},{array:!0}),m("MINVERSE",1,1,(t,r)=>{const o=T(t,r[0]),n=o.length;if(n!==o[0].length)throw c.VALUE;const e=o.map((s,f)=>[...s,...s.map((i,h)=>f===h?1:0)]);for(let s=0;s<n;s++){let f=s;for(let h=s+1;h<n;h++)Math.abs(e[h][s])>Math.abs(e[f][s])&&(f=h);if(Math.abs(e[f][s])<1e-300)throw c.NUM;[e[f],e[s]]=[e[s],e[f]];const i=e[s][s];for(let h=0;h<2*n;h++)e[s][h]/=i;for(let h=0;h<n;h++){if(h===s)continue;const w=e[h][s];for(let N=0;N<2*n;N++)e[h][N]-=w*e[s][N]}}return D.of2D(e.map(s=>s.slice(n)))},{array:!0}),m("MMULT",2,2,(t,r)=>{const o=T(t,r[0]),n=T(t,r[1]);if(o[0].length!==n.length)throw c.VALUE;const e=[];for(let s=0;s<o.length;s++){const f=[];for(let i=0;i<n[0].length;i++){let h=0;for(let w=0;w<n.length;w++)h+=o[s][w]*n[w][i];f.push(h)}e.push(f)}return D.of2D(e)},{array:!0}),l("MUNIT",1,1,t=>{const r=Math.trunc(a(t));if(r<1)throw c.VALUE;const o=new Array(r*r).fill(0);for(let n=0;n<r;n++)o[n*r+n]=1;return new D(r,r,o)});export{tt as bool,Z as modeOf,rt as opt,q as percentileExc,y as percentileInc,v as sumIfs};
|