@allyhub/sdk 0.9.0 → 0.11.0
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/CHANGELOG.md +16 -0
- package/dist/node_modules/@allyhub/audio/package.json +1 -1
- package/dist/node_modules/@allyhub/chart/dist/chart-xml.d.ts +3 -0
- package/dist/node_modules/@allyhub/chart/dist/chart-xml.js +2 -2
- package/dist/node_modules/@allyhub/chart/dist/chart.d.ts +22 -0
- package/dist/node_modules/@allyhub/chart/dist/chart.js +1 -1
- package/dist/node_modules/@allyhub/chart/package.json +1 -1
- package/dist/node_modules/@allyhub/diagnostics/dist/problem.d.ts +7 -0
- package/dist/node_modules/@allyhub/diagnostics/package.json +1 -1
- package/dist/node_modules/@allyhub/formats/dist/data/faces.d.ts +1 -1
- package/dist/node_modules/@allyhub/formats/dist/data/sheet.d.ts +87 -1
- package/dist/node_modules/@allyhub/formats/dist/data/sheet.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/data/slides.d.ts +89 -0
- package/dist/node_modules/@allyhub/formats/dist/data/slides.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/note-shape.d.ts +20 -0
- package/dist/node_modules/@allyhub/formats/dist/note-shape.js +1 -1
- package/dist/node_modules/@allyhub/formats/dist/schemas.d.ts +50 -0
- package/dist/node_modules/@allyhub/formats/package.json +1 -1
- package/dist/node_modules/@allyhub/image-draw/package.json +1 -1
- package/dist/node_modules/@allyhub/image-measure/package.json +1 -1
- package/dist/node_modules/@allyhub/image-sdk/dist/index.d.ts +1 -1
- package/dist/node_modules/@allyhub/image-sdk/package.json +1 -1
- package/dist/node_modules/@allyhub/measure/package.json +1 -1
- package/dist/node_modules/@allyhub/ooxml/dist/geometry/index.js +1 -1
- package/dist/node_modules/@allyhub/ooxml/package.json +1 -1
- package/dist/node_modules/@allyhub/piece/dist/layer.d.ts +2 -0
- package/dist/node_modules/@allyhub/piece/dist/layout.d.ts +16 -0
- package/dist/node_modules/@allyhub/piece/dist/page/index.d.ts +1 -1
- package/dist/node_modules/@allyhub/piece/dist/page/index.js +1 -1
- package/dist/node_modules/@allyhub/piece/dist/page/locate.d.ts +14 -1
- package/dist/node_modules/@allyhub/piece/dist/page/locate.js +1 -1
- package/dist/node_modules/@allyhub/piece/dist/stage.d.ts +18 -0
- package/dist/node_modules/@allyhub/piece/package.json +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/account.d.ts +140 -0
- package/dist/node_modules/@allyhub/protocol/dist/account.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/auth.d.ts +19 -1
- package/dist/node_modules/@allyhub/protocol/dist/auth.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/cloud.d.ts +157 -0
- package/dist/node_modules/@allyhub/protocol/dist/cloud.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/editor.d.ts +10 -2
- package/dist/node_modules/@allyhub/protocol/dist/editor.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/frame.d.ts +101 -186
- package/dist/node_modules/@allyhub/protocol/dist/frame.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/mail.d.ts +1 -0
- package/dist/node_modules/@allyhub/protocol/dist/mail.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/notes.d.ts +92 -94
- package/dist/node_modules/@allyhub/protocol/dist/notes.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/snapshots.d.ts +17 -0
- package/dist/node_modules/@allyhub/protocol/dist/snapshots.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/teams.d.ts +7 -0
- package/dist/node_modules/@allyhub/protocol/dist/teams.js +1 -1
- package/dist/node_modules/@allyhub/protocol/dist/tools.d.ts +15 -3
- package/dist/node_modules/@allyhub/protocol/dist/tools.js +1 -1
- package/dist/node_modules/@allyhub/protocol/package.json +1 -1
- package/dist/node_modules/@allyhub/runtimes/package.json +1 -1
- package/dist/node_modules/@allyhub/scenery/dist/index.d.ts +2 -2
- package/dist/node_modules/@allyhub/scenery/dist/index.js +1 -1
- package/dist/node_modules/@allyhub/scenery/dist/moire.d.ts +45 -5
- package/dist/node_modules/@allyhub/scenery/dist/moire.js +2 -2
- package/dist/node_modules/@allyhub/scenery/dist/phone.d.ts +4 -3
- package/dist/node_modules/@allyhub/scenery/dist/phone.js +1 -1
- package/dist/node_modules/@allyhub/scenery/package.json +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/coerce.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/engine.d.ts +70 -0
- package/dist/node_modules/@allyhub/sheet/dist/calc/engine.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.d.ts +6 -0
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/date.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/define.d.ts +3 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/define.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/info.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/kernels.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/lookup.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/maths.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/functions/text.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/indexes.d.ts +9 -0
- package/dist/node_modules/@allyhub/sheet/dist/calc/indexes.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/calc/types.d.ts +6 -0
- package/dist/node_modules/@allyhub/sheet/dist/calc/types.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/chart.d.ts +9 -0
- package/dist/node_modules/@allyhub/sheet/dist/chart.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/export.d.ts +0 -8
- package/dist/node_modules/@allyhub/sheet/dist/export.js +2 -4
- package/dist/node_modules/@allyhub/sheet/dist/formula/ast.d.ts +12 -1
- package/dist/node_modules/@allyhub/sheet/dist/formula/move.d.ts +5 -1
- package/dist/node_modules/@allyhub/sheet/dist/formula/move.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/formula/parse.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/formula/print.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/index.d.ts +3 -2
- package/dist/node_modules/@allyhub/sheet/dist/index.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/inspect.d.ts +12 -2
- package/dist/node_modules/@allyhub/sheet/dist/inspect.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/printed.d.ts +19 -0
- package/dist/node_modules/@allyhub/sheet/dist/printed.js +2 -0
- package/dist/node_modules/@allyhub/sheet/dist/values.d.ts +2 -0
- package/dist/node_modules/@allyhub/sheet/dist/values.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/workbook.d.ts +2 -0
- package/dist/node_modules/@allyhub/sheet/dist/workbook.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/write.js +15 -15
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.d.ts +6 -2
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/formula.js +2 -2
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.d.ts +0 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.d.ts +3 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.js +2 -2
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/sax.d.ts +7 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/sax.js +3 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/styles.d.ts +6 -0
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/styles.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.d.ts +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.js +1 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/xml.d.ts +12 -1
- package/dist/node_modules/@allyhub/sheet/dist/xlsx/xml.js +3 -2
- package/dist/node_modules/@allyhub/sheet/package.json +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/api.d.ts +57 -7
- package/dist/node_modules/@allyhub/slides-core/dist/api.js +2 -2
- package/dist/node_modules/@allyhub/slides-core/dist/capabilities.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/enums.d.ts +24 -0
- package/dist/node_modules/@allyhub/slides-core/dist/enums.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/fact.d.ts +15 -0
- package/dist/node_modules/@allyhub/slides-core/dist/measure.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/model.d.ts +6 -2
- package/dist/node_modules/@allyhub/slides-core/dist/resolve.d.ts +3 -1
- package/dist/node_modules/@allyhub/slides-core/dist/resolve.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/sdk.d.ts +1 -1
- package/dist/node_modules/@allyhub/slides-core/dist/sdk.js +1 -1
- package/dist/node_modules/@allyhub/slides-core/package.json +1 -1
- package/dist/node_modules/@allyhub/video-core/dist/piece.d.ts +2 -2
- package/dist/node_modules/@allyhub/video-core/dist/schema.d.ts +2 -2
- package/dist/node_modules/@allyhub/video-core/dist/schema.js +1 -1
- package/dist/node_modules/@allyhub/video-core/package.json +1 -1
- package/dist/node_modules/@allyhub/video-sdk/dist/phone.d.ts +12 -5
- package/dist/node_modules/@allyhub/video-sdk/dist/phone.js +1 -1
- package/dist/node_modules/@allyhub/video-sdk/package.json +1 -1
- package/docs/README.md +4 -0
- package/docs/canvas.md +28 -0
- package/docs/defaults.md +30 -4
- package/docs/docs.md +10 -10
- package/docs/image.md +3 -3
- package/docs/linkedin.md +96 -0
- package/docs/reports.md +89 -0
- package/docs/scenery.md +3 -4
- package/docs/sheets.md +23 -6
- package/docs/slides.md +50 -4
- package/docs/speech.md +1 -1
- package/docs/x.md +73 -0
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{MAX_COLUMNS as E,MAX_ROWS as k,overlaps as O}from"../address.js";import{CellError as m,ERR as h,Matrix as p,serialOfDate as _}from"../values.js";import{filteredRows as B}from"../filter.js";import{FormulaCell as y,SharedFormula as I}from"../workbook.js";import{analyse as z}from"./analyse.js";import{Graph as T}from"./graph.js";import{fromWire as G,matrixFromWire as D,matrixToWire as W,toWire as P}from"./remote.js";import{RangeCache as U}from"./indexes.js";import{binary as S,mapBoth as H,toNumber as q}from"./coerce.js";import{FUNCTIONS as v}from"./functions/index.js";import{fillsGroup as j,paramAt as X}from"./functions/define.js";import{Lambda as M,MISSING as F,Missing as x,Reference as u,Union as b}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const Y=new Set,Z=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,$=2e3,N=12,J=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),K=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;nowSerial;random;pass=0;stack=[];inCycle=new Set;namesAt=new Set;graph=new T({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,n)=>this.readsAt(t,e,s,n)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new U;results=new U;wb;constructor(t,e={}){this.wb=t;const{now:s,random:n}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>_(s??new Date),this.random=n??Math.random;for(const o of t.sheets)o.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??Y}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.run(t),this.applyFilters(t),this}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(n=>n.columns.size===0))continue;const s=B(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>Z.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const n=t.at(e,s);return n===void 0?this.ghost(t,e,s):typeof n!="object"||n instanceof m?n:(n.state===1?this.compute(n):n.state===2&&this.circular(n),n.value)}ghost(t,e,s){const n=t.spillAt(e,s);if(!n)return null;if(n.state===1)this.compute(n);else if(n.state===2)return this.circular(n),null;const{array:o}=n;return o&&t.spillAt(e,s)===n?o.at(e-n.row,s-n.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);s.some(n=>this.inCycle.has(n))||this.cycles.push(s);for(const n of s)this.inCycle.add(n)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:n,col:o}=e;this.forget(s,{top:n,left:o,bottom:n,right:o}),e.spill&&this.forget(s,e.spill)}this.computed=0;for(const e of this.order(t))e.state===1&&this.compute(e);this.spread()}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=N*2)break;if(t>=N)for(const{sheet:o,area:r}of e)for(const i of o.spillsIn(r))this.unsettled.add(i);this.build();const s=new Set,n=[...this.unsettled];for(const{sheet:o,area:r}of e)n.push(...this.readersIn(o,r),...o.blockedBy(r));for(;;){const o=n.pop();if(!o)break;s.has(o)||(s.add(o),n.push(...this.readers(o.sheet,o.row,o.col)))}if(s.size!==0){for(const o of s)o.state=1,this.inCycle.delete(o);this.cycles=this.cycles.filter(o=>!o.some(r=>s.has(r))),this.indexes.clear(),this.results.clear();for(const o of this.order([...s]))o.state===1&&this.compute(o)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:n,col:o}=t,r={top:n,left:o,bottom:n+e.rows-1,right:o+e.cols-1},i=s.spillsIn(r).filter(c=>c!==t);if(r.bottom>=k||r.right>=E||s.holds(r,t)||s.tables.some(c=>O(c.area,r))||s.merges.some(c=>O(c,r))||i.some(c=>K(c,t)))return s.blockSpill(t,r),!1;for(const c of i)this.evict(c,r);return s.placeSpill(t,r),!0}evict(t,e){const{sheet:s,spill:n}=t;n&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:n}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=z(this.wb,t)}readsOf(t){const e=[],s=V(t);for(const n of this.analysisOf(t.shared).reads){const o=n.kind==="ref"?this.resolveRef(n,s):this.structured(n,s);o instanceof u?e.push(o):o instanceof b&&e.push(...o.parts)}return e}readsAt(t,e,s,n){const o={sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col},r=[];for(const i of this.analysisOf(t).reads){const a=i.kind==="ref"?this.resolveRef(i,o):this.structured(i,o);a instanceof u?r.push(a):a instanceof b?r.push(...a.parts):r.push(void 0)}return r}readsOfFormula(t,e){const s=[],n={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const o of this.analysisOf(t).reads){const r=o.kind==="ref"?this.resolveRef(o,n):this.structured(o,n);r instanceof u?s.push(r):r instanceof b&&s.push(...r.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],n=new Set,o=[],r=i=>{i.mark=e,o.push({cell:i,reads:this.pending(i,n),next:0})};for(const i of t)if(i.mark!==e)for(r(i);;){const a=o[o.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),o.pop()):c.mark!==e&&c.state===1&&r(c)}return s}pending(t,e){const s=[];for(const n of this.readsOf(t)){const{sheet:o}=n;if(n.single){const i=o.at(n.top,n.left);i instanceof y&&s.push(i);continue}if(e.has(n.key))continue;e.add(n.key);const r=Math.min(n.right,o.columnsUsed-1);for(let i=n.left;i<=r;i++){if(!o.hasFormulas(i))continue;const[a,c]=o.span(i,n.top,n.bottom);for(let f=a;f<=c;f++){const d=o.at(f,i);d instanceof y&&s.push(d)}}}return s}compute(t){if(this.stack.length>=$){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=e?this.top(this.eval(e,V(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,n=t.spill,o=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof p){const i=tt(e);i.rows*i.cols<=1||this.spillOut(t,i)?(t.array=i,t.value=i.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const r=t.spill;(!J(o,t.array)||!Q(n,r))&&(n&&this.moved.push({sheet:s,area:n}),r&&this.moved.push({sheet:s,area:r}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof u?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof b?h.VALUE:t instanceof M?h.CALC:t instanceof x?null:t}matrix(t){const e=t.bottom===k-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===E-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,n=Math.max(0,e-t.top+1),o=Math.max(0,s-t.left+1),r=new Array(n*o);for(let i=0;i<n;i++)for(let a=0;a<o;a++)r[i*o+a]=this.read(t.sheet,t.top+i,t.left+a);return new p(n,o,r)}each(t,e){const{sheet:s}=t,n=Math.min(t.right,s.columnsUsed-1);for(let o=t.left;o<=n;o++){const[r,i]=s.span(o,t.top,t.bottom);for(let a=r;a<=i;a++){const c=s.at(a,o);if(c===void 0){s.spillAt(a,o)&&e(this.read(s,a,o),a,o);continue}e(typeof c=="object"&&!(c instanceof m)?this.read(s,a,o):c,a,o)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,n){const{node:o}=t;return o?this.top(this.eval(o,{sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col})):h.NAME}bound(t,e,s){const n=t.abs?t.index:t.index+e;return n<0||n>=s?void 0:n}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let n=0,o=E-1,r=0,i=k-1;if(t.col){const a=this.bound(t.col,e.dc,E),c=this.bound(t.col2??t.col,e.dc,E);if(a===void 0||c===void 0)return h.REF;n=Math.min(a,c),o=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,k),c=this.bound(t.row2??t.row,e.dr,k);if(a===void 0||c===void 0)return h.REF;r=Math.min(a,c),i=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,f]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new b(this.wb.sheets.slice(c,f+1).map(d=>new u(d,r,n,i,o)))}return new u(s,r,n,i,o)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?et(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const n=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const f=c.names.get(n);if(f!==void 0)return f}const o=s===void 0?e.sheet:this.wb.sheet(s);if(!o)return h.REF;const r=this.wb.name(t,o);if(r){if(!r.formula)return r.value??null;const{node:c}=r.formula;if(!c)return h.NAME;if(this.namesAt.has(n))return h.CYCLE;this.namesAt.add(n);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(n)}}const i=this.wb.table(t);if(i){const{top:c,left:f,bottom:d,right:g}=i.data;return new u(i.sheet,c,f,d,g)}const a=v.get(t.toUpperCase());return a&&a.kind!=="lazy"?st(a,t,e):h.NAME}intersect(t,e){return!(t instanceof u)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new u(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new u(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof p?new p(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const n=this.flat(e),o=this.flat(s);if(n instanceof p||o instanceof p){const r=(i,a)=>S(t,i,a);return H(n instanceof p?n:new p(1,1,[n]),o instanceof p?o:new p(1,1,[o]),r)}return S(t,n,o)}rangeOf(t,e){return t instanceof m?t:e instanceof m?e:!(t instanceof u)||!(e instanceof u)||t.sheet!==e.sheet?h.REF:new u(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof m)return t;if(e instanceof m)return e;if(!(t instanceof u)||!(e instanceof u))return h.REF;const s=Math.max(t.top,e.top),n=Math.max(t.left,e.left),o=Math.min(t.bottom,e.bottom),r=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>o||n>r?h.NULL:new u(t.sheet,s,n,o,r)}union(t,e){if(t instanceof m)return t;if(e instanceof m)return e;const s=o=>o instanceof u?[o]:o instanceof b?o.parts:[],n=[...s(t),...s(e)];return n.length===0?h.VALUE:new b(n)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return m.of(t.code);case"missing":return F;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,n=>S("*",n,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,n=>S("/",n,100));if(s instanceof u&&s.single){const n=s.sheet.at(s.top,s.left);if(n instanceof y){this.read(s.sheet,s.top,s.left);const{spill:o}=n;if(o)return new u(s.sheet,o.top,o.left,o.bottom,o.right);if(n.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),n=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,n):t.op===" "?this.intersection(s,n):t.op===","?this.union(s,n):this.operate(t.op,s,n)}case"array":return new p(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(n=>{const o=this.eval(n,e);return o instanceof p||o instanceof u||o instanceof b||o instanceof M||o instanceof x?h.VALUE:o})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof M?this.invoke(s,t.args,e):s instanceof m?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(n=>this.eval(n,s)))}apply(t,e){if(t.named!==void 0){const n=v.get(t.named);return!n||n.kind==="lazy"?h.NAME:e.length<n.min||e.length>n.max?h.VALUE:this.applyFunction(n,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=new Map;return t.params.forEach((n,o)=>{s.set(n,e[o]??F)}),this.eval(t.body,{...t.frame,scope:{names:s,...t.frame.scope?{parent:t.frame.scope}:{}}})}call(t,e,s){const n=v.get(t);if(!n){const o=this.nameValue(t,s);return o instanceof M?this.invoke(o,e,s):h.NAME}return e.length<n.min||e.length>n.max?h.VALUE:n.kind==="lazy"?n.call(e,{calc:this,frame:s}):this.applyFunction(n,e.map(o=>this.eval(o,s)),s)}applyFunction(t,e,s){const n={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return j(t.params,e.length)?this.over(e,r=>X(t.params,r)==="value",r=>t.call(r,n)):h.VALUE;const o=t.errors===!0;return this.over(e,()=>!0,r=>{const i=r.map(a=>a instanceof x?void 0:a);if(!o){for(const a of i)if(a instanceof m)return a}return t.call(...i)})}over(t,e,s){const n=[],o=new Map;for(const[f,d]of t.entries()){if(!e(f)||d instanceof x){n.push(d);continue}const g=this.flat(d);g instanceof p&&o.set(f,g),n.push(g)}if(o.size===0)return s(n);const r=[...o.values()],i=Math.max(...r.map(f=>f.rows)),a=Math.max(...r.map(f=>f.cols));if(i===0||a===0)return h.CALC;const c=new Array(i*a);for(let f=0;f<i;f++)for(let d=0;d<a;d++){const g=n.map((C,A)=>{const w=o.get(A);if(!w)return C;const L=w.rows===1?0:f,R=w.cols===1?0:d;return L<w.rows&&R<w.cols?w.at(L,R):h.NA});c[f*a+d]=this.element(s(g))}return new p(i,a,c)}element(t){const e=this.flat(t);return e instanceof p?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof x)return null;if(t instanceof u){const s=this.intersect(t,e);return s instanceof u?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof p?t.at(0,0):t instanceof b||t instanceof M?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const n=s.pop();if(!n)break;for(const o of this.readers(n.sheet,n.row,n.col))e.has(o)||(e.add(o),s.push(o))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,n){const o=typeof n=="string"&&n.length>1&&n.startsWith("=")?new y(new I(n,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:o??n??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:o,row:r,col:i,slot:a}of t){const c=o.at(r,i);c instanceof y&&o.clearSpill(c),o.replace(r,i,a),a instanceof y&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const n=new Set;for(const{sheet:o,row:r,col:i,slot:a}of t){const c=o.at(r,i);c instanceof y&&(n.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:o,area:c.spill}),o.clearSpill(c));const f=o.spillAt(r,i);f&&s.push(f),s.push(...o.blockedBy({top:r,left:i,bottom:r,right:i})),o.replace(r,i,a),this.forget(o,{top:r,left:i,bottom:r,right:i}),a instanceof y&&(this.addEdges(a),e.add(a))}n.size>0&&(this.cycles=this.cycles.filter(o=>!o.some(r=>n.has(r))));for(const{sheet:o,row:r,col:i}of t)s.push(...this.readers(o,r,i));for(s.push(...this.volatiles);;){const o=s.pop();if(!o)break;e.has(o)||(e.add(o),s.push(...this.readers(o.sheet,o.row,o.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const n=t.toLowerCase();this.mark(this.allCells().filter(o=>this.analysisOf(o.shared).names.has(n)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:P(e.value),...e.array?{array:W(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const n=this.wb.sheets[s.sheet],o=n?.at(s.row,s.col);!n||!(o instanceof y)||(n.clearSpill(o),o.value=G(s.value),o.array=s.array?D(s.array):void 0,s.spill&&n.placeSpill(o,s.spill),s.blocked&&n.blockSpill(o,s.blocked),o.state=0)}this.cycles=t.cycles.flatMap(s=>{const n=s.flatMap(o=>{const r=this.wb.sheets[o.sheet]?.at(o.row,o.col);return r instanceof y?[r]:[]});return n.length>0?[n]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:n}of t.hidden){const o=this.wb.sheets[s];if(!o)continue;const r=e.get(o),i=r?.size===n.length&&n.every(a=>r.has(a));this.hiddenBy.set(o,i?r:new Set(n))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return q(t)}}const Q=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function tt(l){return l.data.some(t=>t===null)?new p(l.rows,l.cols,l.data.map(t=>t??0)):l}function V(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function et(l,t,e){const{sheet:s}=l,n=l.area;let o=n.left,r=n.right;const[i,a]=t.columns;if(i!==void 0){const A=l.column(i),w=a===void 0?A:l.column(a);if(!A||!w)return h.REF;o=n.left+Math.min(A.index,w.index),r=n.left+Math.max(A.index,w.index)}if(t.thisRow)return e.sheet!==s||e.row<=n.top||e.row>n.bottom?h.VALUE:new u(s,e.row,o,e.row,r);const c=t.sections;if(c.includes("#Totals"))return h.REF;const f=c.includes("#All")||c.includes("#Headers"),d=c.length===0||c.includes("#All")||c.includes("#Data"),g=f?n.top:n.top+1,C=d?n.bottom:n.top;return new u(s,g,o,C,r)}function st(l,t,e){return new M([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
|
|
2
|
+
import{MAX_COLUMNS as v,MAX_ROWS as C,overlaps as N}from"../address.js";import{CellError as b,ERR as h,Matrix as m,serialOfDate as G}from"../values.js";import{filteredRows as W}from"../filter.js";import{FormulaCell as k,SharedFormula as P}from"../workbook.js";import{analyse as q}from"./analyse.js";import{Graph as H}from"./graph.js";import{fromWire as $,matrixFromWire as j,matrixToWire as K,toWire as X}from"./remote.js";import{RangeCache as U}from"./indexes.js";import{binary as O,mapBoth as Y,toNumber as Z}from"./coerce.js";import{FUNCTIONS as _}from"./functions/index.js";import{fillsGroup as J,paramAt as Q}from"./functions/define.js";import{Lambda as S,MISSING as T,Missing as L,Reference as d,Union as x,pushAll as M}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const tt=new Set,et=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,I=2e3,st=50,nt=1024;class V extends Error{cell;constructor(t){super("computed first"),this.cell=t}}const B=12;export const ITERATION_DEFAULTS={max_iterations:100,max_change:.001};function ot(l,t){return typeof l=="number"&&typeof t=="number"?Math.abs(t-l):l===t||l instanceof b&&t instanceof b&&l.code===t.code?0:1/0}const it=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),rt=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;iterated;nowSerial;random;pass=0;lambdaSlots=0;stack=[];inCycle=new Set;looping=new Set;tablesOf;inTables=!1;loop=new Set;around=new Set;namesAt=new Set;graph=new H({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,o)=>this.readsAt(t,e,s,o)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new U;results=new U;wb;constructor(t,e={}){this.wb=t;const{now:s,random:o}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>G(s??new Date),this.random=o??Math.random;for(const n of t.sheets)n.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??tt}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.looping.clear(),this.loop=new Set,this.around.clear(),this.tablesOf=void 0,this.iterated=void 0,this.run(t),this.applyFilters(t),this}iteration(){const t=this.wb.file.iteration;if(t!==void 0)return{max_iterations:(t===!0?void 0:t.max_iterations)??ITERATION_DEFAULTS.max_iterations,max_change:(t===!0?void 0:t.max_change)??ITERATION_DEFAULTS.max_change}}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(o=>o.columns.size===0))continue;const s=W(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>et.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const o=t.at(e,s);return o===void 0?this.ghost(t,e,s):typeof o!="object"||o instanceof b?o:(o.state===1?this.computeNow(o):o.state===2&&this.circular(o),o.value)}ghost(t,e,s){const o=t.spillAt(e,s);if(!o)return null;if(o.state===1)this.computeNow(o);else if(o.state===2)return this.circular(o),null;const{array:n}=o;return n&&t.spillAt(e,s)===o?n.at(e-o.row,s-o.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);if(this.iteration()){for(const o of s)this.looping.add(o);return}s.some(o=>this.inCycle.has(o))||this.cycles.push(s);for(const o of s)this.inCycle.add(o)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:o,col:n}=e;this.forget(s,{top:o,left:n,bottom:o,right:n}),e.spill&&this.forget(s,e.spill)}t.some(e=>this.loop.has(e))&&(this.iterated=void 0),this.computed=0;for(const e of this.order(t))e.state===1&&this.computeNow(e);this.spread(),this.iterate(),this.tables()}tableCells(){if(this.tablesOf)return this.tablesOf;const t=new Map;for(const e of this.allCells())if(R(e.shared.node)){const s=t.get(e.shared);s?s.push(e):t.set(e.shared,[e])}return this.tablesOf=t,t}tables(){if(this.replica||this.inTables)return;const t=this.tableCells();if(t.size!==0){this.inTables=!0;try{const e=[];for(const[n,i]of t)this.table(n,i),e.push(...i);const s=new Set(e),o=new Set;for(const n of e)for(const i of this.readers(n.sheet,n.row,n.col))s.has(i)||o.add(i);o.size>0&&this.mark([...o])}finally{this.inTables=!1}}}operations(t){const e=new Map,s=(n,i)=>{const r=n===void 0?void 0:this.eval(n,i);return r instanceof d&&r.single?r:void 0},o=t.map(n=>{const i=n.shared.node;if(i?.kind!=="call")return h.REF;const r=F(n),a=s(i.args[0],r);if(!a)return h.REF;const c=[];for(let f=1;f+1<i.args.length;f+=2){const u=s(i.args[f],r),w=s(i.args[f+1],r);if(!u||!w)return h.REF;const E=`${String(u.sheet.index)}:${String(u.top)}:${String(u.left)}`;e.has(E)||e.set(E,{sheet:u.sheet,row:u.top,col:u.left,slot:u.sheet.at(u.top,u.left)});const A=this.read(w.sheet,w.top,w.left);c.push({sheet:u.sheet,row:u.top,col:u.left,slot:A??void 0})}return this.change(c),this.read(a.sheet,a.top,a.left)});e.size>0&&this.change([...e.values()]),t.forEach((n,i)=>{n.value=o[i]??null,this.reports&&this.settled.add(n)})}table(t,e){const[s]=e,o=t.node;if(!s||o?.kind!=="call")return;if(o.args.length>2){this.operations(e);return}const{sheet:n}=s,i=t.row,r=t.col,a={sheet:n,row:i,col:r,dr:0,dc:0},c=p=>{if(p===void 0||p.kind==="missing")return null;const g=this.eval(p,a);return g instanceof d&&g.single?g:void 0},f=c(o.args[0]),u=c(o.args[1]),w=p=>{for(const g of e)g.value=p,this.reports&&this.settled.add(g)};if(f===void 0||u===void 0||f===null&&u===null||i===0||r===0){w(h.REF);return}const A=[f,u].filter(p=>p!==null).map(p=>({sheet:p.sheet,row:p.top,col:p.left,slot:p.sheet.at(p.top,p.left)})),y=e.map(p=>{const g=[];f&&g.push({sheet:f.sheet,row:f.top,col:f.left,slot:this.read(n,i-1,p.col)??void 0}),u&&g.push({sheet:u.sheet,row:u.top,col:u.left,slot:this.read(n,p.row,r-1)??void 0}),this.change(g);const[z,D]=f&&u?[i-1,r-1]:u?[i-1,p.col]:[p.row,r-1];return this.read(n,z,D)});this.change(A),e.forEach((p,g)=>{p.value=y[g]??null,this.reports&&this.settled.add(p)})}iterate(){const t=this.iteration();if(!t||this.looping.size===0){this.looping.clear();return}for(const i of this.looping)this.around.add(i);const e=this.downstream([...this.looping]),s=[...e];let o=1,n=1/0;for(;o<t.max_iterations;){const i=s.map(r=>r.value);this.looping.clear(),this.indexes.clear(),this.results.clear();for(const r of s)r.state=1;for(const r of this.order(s))r.state===1&&this.compute(r);if(o+=1,n=0,s.forEach((r,a)=>{n=Math.max(n,ot(i[a]??null,r.value))}),n<t.max_change)break}this.looping.clear(),this.spread(),this.looping.clear(),this.loop=e,this.iterated={cells:s.length,iterations:o,change:n}}iterates(t){return this.loop.has(t)}circularCells(){return[...this.around]}downstream(t){const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}return e}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=B*2)break;if(t>=B)for(const{sheet:n,area:i}of e)for(const r of n.spillsIn(i))this.unsettled.add(r);this.build();const s=new Set,o=[...this.unsettled];for(const{sheet:n,area:i}of e)M(o,this.readersIn(n,i)),M(o,n.blockedBy(i));for(;;){const n=o.pop();if(!n)break;s.has(n)||(s.add(n),M(o,this.readers(n.sheet,n.row,n.col)))}if(s.size!==0){for(const n of s)n.state=1,this.inCycle.delete(n);this.cycles=this.cycles.filter(n=>!n.some(i=>s.has(i))),this.indexes.clear(),this.results.clear();for(const n of this.order([...s]))n.state===1&&this.computeNow(n)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:o,col:n}=t,i={top:o,left:n,bottom:o+e.rows-1,right:n+e.cols-1},r=s.spillsIn(i).filter(c=>c!==t);if(i.bottom>=C||i.right>=v||s.holds(i,t)||s.tables.some(c=>N(c.area,i))||s.merges.some(c=>N(c,i))||r.some(c=>rt(c,t)))return s.blockSpill(t,i),!1;for(const c of r)this.evict(c,i);return s.placeSpill(t,i),!0}evict(t,e){const{sheet:s,spill:o}=t;o&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:o}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=q(this.wb,t)}readsOf(t){const e=[],s=F(t);for(const o of this.analysisOf(t.shared).reads){const n=o.kind==="ref"?this.resolveRef(o,s):this.structured(o,s);n instanceof d?e.push(n):n instanceof x&&e.push(...n.parts)}return e}readsAt(t,e,s,o){const n={sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col},i=[];for(const r of this.analysisOf(t).reads){const a=r.kind==="ref"?this.resolveRef(r,n):this.structured(r,n);a instanceof d?i.push(a):a instanceof x?i.push(...a.parts):i.push(void 0)}return i}readsOfFormula(t,e){const s=[],o={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const n of this.analysisOf(t).reads){const i=n.kind==="ref"?this.resolveRef(n,o):this.structured(n,o);i instanceof d?s.push(i):i instanceof x&&s.push(...i.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],o=new Set,n=[],i=r=>{r.mark=e,n.push({cell:r,reads:this.pending(r,o),next:0})};for(const r of t)if(r.mark!==e)for(i(r);;){const a=n[n.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),n.pop()):c.mark!==e&&c.state===1&&i(c)}return s}pending(t,e){const s=[];for(const o of this.readsOf(t)){const{sheet:n}=o;if(o.single){const r=n.at(o.top,o.left);r instanceof k&&s.push(r);continue}if(e.has(o.key))continue;e.add(o.key);const i=Math.min(o.right,n.columnsUsed-1);for(let r=o.left;r<=i;r++){if(!n.hasFormulas(r))continue;const[a,c]=n.span(r,o.top,o.bottom);for(let f=a;f<=c;f++){const u=n.at(f,r);u instanceof k&&s.push(u)}}}return s}computeNow(t){if(this.stack.length>0){if(this.stack.length>=st&&this.stack.length<I)throw new V(t);this.compute(t);return}const e=[t],s=new Set([t]);for(;e.length>0;){const o=e[e.length-1];if(o===void 0)break;if(o.state!==1){e.pop();continue}try{this.compute(o),e.pop()}catch(n){if(!(n instanceof V))throw n;for(const i of this.stack)i.state=1;if(this.stack.length=0,s.has(n.cell)){n.cell.value=h.CALC,n.cell.state=0;continue}s.add(n.cell),e.push(n.cell)}}}compute(t){if(this.stack.length>=I){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=R(e)?t.value:e?this.top(this.eval(e,F(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,o=t.spill,n=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof m){const r=ct(e);r.rows*r.cols<=1||this.spillOut(t,r)?(t.array=r,t.value=r.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const i=t.spill;(!it(n,t.array)||!at(o,i))&&(o&&this.moved.push({sheet:s,area:o}),i&&this.moved.push({sheet:s,area:i}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof d?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof x?h.VALUE:t instanceof S?h.CALC:t instanceof L?null:t}matrix(t){const e=t.bottom===C-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===v-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,o=Math.max(0,e-t.top+1),n=Math.max(0,s-t.left+1),i=new Array(o*n);for(let r=0;r<o;r++)for(let a=0;a<n;a++)i[r*n+a]=this.read(t.sheet,t.top+r,t.left+a);return new m(o,n,i)}each(t,e){const{sheet:s}=t,o=Math.min(t.right,s.columnsUsed-1);for(let n=t.left;n<=o;n++){const[i,r]=s.span(n,t.top,t.bottom);for(let a=i;a<=r;a++){const c=s.at(a,n);if(c===void 0){s.spillAt(a,n)&&e(this.read(s,a,n),a,n);continue}e(typeof c=="object"&&!(c instanceof b)?this.read(s,a,n):c,a,n)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,o){const{node:n}=t;return n?this.top(this.eval(n,{sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col})):h.NAME}bound(t,e,s){const o=t.abs?t.index:t.index+e;return o<0||o>=s?void 0:o}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let o=0,n=v-1,i=0,r=C-1;if(t.col){const a=this.bound(t.col,e.dc,v),c=this.bound(t.col2??t.col,e.dc,v);if(a===void 0||c===void 0)return h.REF;o=Math.min(a,c),n=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,C),c=this.bound(t.row2??t.row,e.dr,C);if(a===void 0||c===void 0)return h.REF;i=Math.min(a,c),r=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,f]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new x(this.wb.sheets.slice(c,f+1).map(u=>new d(u,i,o,r,n)))}return new d(s,i,o,r,n)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?lt(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const o=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const f=c.names.get(o);if(f!==void 0)return f}const n=s===void 0?e.sheet:this.wb.sheet(s);if(!n)return h.REF;const i=this.wb.name(t,n);if(i){if(!i.formula)return i.value??null;const{node:c}=i.formula;if(!c)return h.NAME;if(this.namesAt.has(o))return h.CYCLE;this.namesAt.add(o);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(o)}}const r=this.wb.table(t);if(r){const{top:c,left:f,bottom:u,right:w}=r.data;return new d(r.sheet,c,f,u,w)}const a=_.get(t.toUpperCase());return a&&a.kind!=="lazy"?ht(a,t,e):h.NAME}intersect(t,e){return!(t instanceof d)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new d(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new d(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof m?new m(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const o=this.flat(e),n=this.flat(s);if(o instanceof m||n instanceof m){const i=(r,a)=>O(t,r,a);return Y(o instanceof m?o:new m(1,1,[o]),n instanceof m?n:new m(1,1,[n]),i)}return O(t,o,n)}rangeOf(t,e){return t instanceof b?t:e instanceof b?e:!(t instanceof d)||!(e instanceof d)||t.sheet!==e.sheet?h.REF:new d(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof b)return t;if(e instanceof b)return e;if(!(t instanceof d)||!(e instanceof d))return h.REF;const s=Math.max(t.top,e.top),o=Math.max(t.left,e.left),n=Math.min(t.bottom,e.bottom),i=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>n||o>i?h.NULL:new d(t.sheet,s,o,n,i)}union(t,e){if(t instanceof b)return t;if(e instanceof b)return e;const s=n=>n instanceof d?[n]:n instanceof x?n.parts:[],o=[...s(t),...s(e)];return o.length===0?h.VALUE:new x(o)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return b.of(t.code);case"missing":return T;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,o=>O("*",o,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,o=>O("/",o,100));if(s instanceof d&&s.single){const o=s.sheet.at(s.top,s.left);if(o instanceof k){this.read(s.sheet,s.top,s.left);const{spill:n}=o;if(n)return new d(s.sheet,n.top,n.left,n.bottom,n.right);if(o.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),o=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,o):t.op===" "?this.intersection(s,o):t.op===","?this.union(s,o):this.operate(t.op,s,o)}case"array":return new m(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(o=>{const n=this.eval(o,e);return n instanceof m||n instanceof d||n instanceof x||n instanceof S||n instanceof L?h.VALUE:n})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof S?this.invoke(s,t.args,e):s instanceof b?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(o=>this.eval(o,s)))}apply(t,e){if(t.named!==void 0){const n=_.get(t.named);return!n||n.kind==="lazy"?h.NAME:e.length<n.min||e.length>n.max?h.VALUE:this.applyFunction(n,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=t.params.length+1;if(this.lambdaSlots+s>nt)return h.NUM;const o=new Map;t.params.forEach((n,i)=>{o.set(n,e[i]??T)}),this.lambdaSlots+=s;try{return this.eval(t.body,{...t.frame,scope:{names:o,...t.frame.scope?{parent:t.frame.scope}:{}}})}finally{this.lambdaSlots-=s}}call(t,e,s){const o=_.get(t);if(!o){const n=this.nameValue(t,s);return n instanceof S?this.invoke(n,e,s):h.NAME}return e.length<o.min||e.length>o.max?h.VALUE:o.kind==="lazy"?o.call(e,{calc:this,frame:s}):this.applyFunction(o,e.map(n=>this.eval(n,s)),s)}applyFunction(t,e,s){const o={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return J(t.params,e.length)?this.over(e,i=>Q(t.params,i)==="value",i=>t.call(i,o)):h.VALUE;const n=t.errors===!0;return this.over(e,()=>!0,i=>{const r=i.map(a=>a instanceof L?void 0:a);if(!n){for(const a of r)if(a instanceof b)return a}return t.call(...r)})}over(t,e,s){const o=[],n=new Map;for(const[f,u]of t.entries()){if(!e(f)||u instanceof L){o.push(u);continue}const w=this.flat(u);w instanceof m&&n.set(f,w),o.push(w)}if(n.size===0)return s(o);const i=[...n.values()],r=Math.max(...i.map(f=>f.rows)),a=Math.max(...i.map(f=>f.cols));if(r===0||a===0)return h.CALC;const c=new Array(r*a);for(let f=0;f<r;f++)for(let u=0;u<a;u++){const w=o.map((E,A)=>{const y=n.get(A);if(!y)return E;const p=y.rows===1?0:f,g=y.cols===1?0:u;return p<y.rows&&g<y.cols?y.at(p,g):h.NA});c[f*a+u]=this.element(s(w))}return new m(r,a,c)}element(t){const e=this.flat(t);return e instanceof m?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof L)return null;if(t instanceof d){const s=this.intersect(t,e);return s instanceof d?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof m?t.at(0,0):t instanceof x||t instanceof S?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,o){const n=typeof o=="string"&&o.length>1&&o.startsWith("=")?new k(new P(o,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:n??o??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);c instanceof k&&n.clearSpill(c),n.replace(i,r,a),a instanceof k&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const o=new Set;for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);(c instanceof k&&R(c.shared.node)||a instanceof k&&R(a.shared.node))&&(this.tablesOf=void 0),c instanceof k&&(o.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:n,area:c.spill}),n.clearSpill(c));const f=n.spillAt(i,r);f&&s.push(f),s.push(...n.blockedBy({top:i,left:r,bottom:i,right:r})),n.replace(i,r,a),this.forget(n,{top:i,left:r,bottom:i,right:r}),a instanceof k&&(this.addEdges(a),e.add(a))}o.size>0&&(this.cycles=this.cycles.filter(n=>!n.some(i=>o.has(i))));for(const{sheet:n,row:i,col:r}of t)M(s,this.readers(n,i,r));for(M(s,this.volatiles);;){const n=s.pop();if(!n)break;e.has(n)||(e.add(n),M(s,this.readers(n.sheet,n.row,n.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const o=t.toLowerCase();this.mark(this.allCells().filter(n=>this.analysisOf(n.shared).names.has(o)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:X(e.value),...e.array?{array:K(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const o=this.wb.sheets[s.sheet],n=o?.at(s.row,s.col);!o||!(n instanceof k)||(o.clearSpill(n),n.value=$(s.value),n.array=s.array?j(s.array):void 0,s.spill&&o.placeSpill(n,s.spill),s.blocked&&o.blockSpill(n,s.blocked),n.state=0)}this.cycles=t.cycles.flatMap(s=>{const o=s.flatMap(n=>{const i=this.wb.sheets[n.sheet]?.at(n.row,n.col);return i instanceof k?[i]:[]});return o.length>0?[o]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:o}of t.hidden){const n=this.wb.sheets[s];if(!n)continue;const i=e.get(n),r=i?.size===o.length&&o.every(a=>i.has(a));this.hiddenBy.set(n,r?i:new Set(o))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return Z(t)}}const at=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function ct(l){return l.data.some(t=>t===null)?new m(l.rows,l.cols,l.data.map(t=>t??0)):l}function R(l){return l?.kind==="call"&&l.name==="TABLE"}function F(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function lt(l,t,e){const{sheet:s}=l,o=l.area;let n=o.left,i=o.right;const[r,a]=t.columns;if(r!==void 0){const A=l.column(r),y=a===void 0?A:l.column(a);if(!A||!y)return h.REF;n=o.left+Math.min(A.index,y.index),i=o.left+Math.max(A.index,y.index)}if(t.thisRow)return e.sheet!==s||e.row<=o.top||e.row>o.bottom?h.VALUE:new d(s,e.row,n,e.row,i);const c=t.sections;if(c.includes("#Totals"))return h.REF;const f=c.includes("#All")||c.includes("#Headers"),u=c.length===0||c.includes("#All")||c.includes("#Data"),w=f?o.top:o.top+1,E=u?o.bottom:o.top;return new d(s,w,n,E,i)}function ht(l,t,e){return new S([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
|
|
@@ -10,6 +10,12 @@ export declare function indexKey(v: Scalar): string | undefined;
|
|
|
10
10
|
/** A wildcard pattern as a test: * any text, ? one character, ~ takes the next as it is. */
|
|
11
11
|
export declare function wildcard(pattern: string): (text: string) => boolean;
|
|
12
12
|
export declare const hasWildcard: (text: string) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The text a pattern matches when it holds no * or ? of its own, only
|
|
15
|
+
* ~-escapes ("~"&A1 in a MATCH): one text, found by an index, not tried
|
|
16
|
+
* against every cell.
|
|
17
|
+
*/
|
|
18
|
+
export declare function literalOf(pattern: string): string | undefined;
|
|
13
19
|
/** A criterion read from what a formula gives it. */
|
|
14
20
|
export declare function criterion(raw: Scalar): Criterion;
|
|
15
21
|
//# sourceMappingURL=criteria.d.ts.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{CellError as l,ERR as
|
|
2
|
+
import{CellError as l,ERR as g}from"../../values.js";import{compare as O,numberOfText as A,sameNumber as b}from"../coerce.js";import{Missing as C,Reference as T}from"../types.js";import{indexKeyOf as w}from"../indexes.js";import{cellsOf as h,raw as p}from"./define.js";export function indexKey(e){if(typeof e=="number")return`n${String(Number(e.toPrecision(15)))}`;if(typeof e=="string")return`t${e.toUpperCase()}`;if(typeof e=="boolean")return e?"bT":"bF"}const y=new Map,k=1e4;export function wildcard(e){const n=y.get(e);if(n)return n;y.size>=k&&y.clear();const t=V(e);return y.set(e,t),t}function V(e){let n="";for(let r=0;r<e.length;r++){const o=e.charAt(r);o==="~"&&/[*?~]/u.test(e.charAt(r+1))?(r+=1,n+=e.charAt(r).replace(/[*?]/u,"\\$&")):o==="*"?n+="[\\s\\S]*":o==="?"?n+="[\\s\\S]":n+=o.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}const t=new RegExp(`^${n}$`,"iu");return r=>t.test(r)}export const hasWildcard=e=>/[*?~]/u.test(e);export function literalOf(e){let n="";for(let t=0;t<e.length;t++){const r=e.charAt(t);if(r==="~"&&/[*?~]/u.test(e.charAt(t+1)))t+=1,n+=e.charAt(t);else{if(r==="*"||r==="?")return;n+=r}}return n}function I(e){return n=>{if(typeof n=="number")return b(n,e);if(typeof n!="string")return!1;const t=A(n);return t!==void 0&&b(t,e)}}function G(e,n){const t=r=>e===">"?r>0:e===">="?r>=0:e==="<"?r<0:r<=0;return r=>typeof r==typeof n&&t(O(r,n))}export function criterion(e){if(e===null)return{test:I(0),key:"n0"};if(typeof e=="number")return{test:I(e),key:`n${String(Number(e.toPrecision(15)))}`};if(typeof e=="boolean")return{test:s=>s===e,key:e?"bT":"bF"};if(e instanceof l)return{test:s=>s===e};const n=/^(<>|>=|<=|=|>|<)/u.exec(e),t=n?.[1]??"=",r=n?e.slice(t.length):e,o=s=>s===null;if(r==="")return t==="="?{test:n?o:s=>s===null||s===""}:t==="<>"?{test:s=>s!==null}:{test:()=>!1};const i=A(r),c=/^(?:TRUE|FALSE)$/iu.test(r)?r.toUpperCase()==="TRUE":void 0,f=i??c;if(t==="="||t==="<>"){let s,u;if(i!==void 0)s=I(i),u=indexKey(i);else if(c!==void 0)s=d=>d===c,u=indexKey(c);else if(r.startsWith("#"))s=d=>d instanceof l&&d.code===r.toUpperCase();else if(hasWildcard(r)){const d=wildcard(r);s=m=>typeof m=="string"&&d(m)}else{const d=r.toUpperCase();s=m=>typeof m=="string"&&m.toUpperCase()===d,u=indexKey(r)}return t==="="?u===void 0?{test:s}:{test:s,key:u}:{test:d=>!s(d)}}return{test:G(t,f??r)}}function P(e,n){const t=e.reference;if(!(t?.cols!==1||e.rows<64))return n.calc.indexes.ofRepeated(w("crit",t),[t],()=>{const r=new Map,o=(i,c)=>{if(i===void 0)return;const f=r.get(i);f?f.push(c):r.set(i,[c])};for(let i=0;i<e.rows;i++){const c=e.at(i,0);if(o(indexKey(c),i),typeof c=="string"){const f=A(c);f!==void 0&&o(indexKey(f),i)}}return r})}function E(e,n){const t=n.calc.scalar(e,n.frame);return criterion(t)}function x(e,n){const t=[];for(let o=0;o+1<e.length;o+=2){const i=h(e[o],n);if(i instanceof l)return i;const c=E(e[o+1],n);if(c instanceof l)return c;t.push({cells:i,crit:c})}const r=t[0]?.cells;return r&&t.some(o=>!M(o.cells,r))?g.VALUE:t}const M=(e,n)=>e.cols===n.cols&&(e.reference?.rows??e.rows)===(n.reference?.rows??n.rows);function F(e,n,t){const r=e[0];if(!r)return;let o,i;for(const f of e){if(f.crit.key===void 0)continue;const s=P(f.cells,n);if(!s)continue;const u=s.get(f.crit.key)??[];(o===void 0||u.length<o.length)&&(o=u,i=f)}if(o!==void 0){const f=e.filter(s=>s!==i);for(const s of o)f.every(u=>u.crit.test(u.cells.at(s,0)))&&t(s,0);return}const c=Math.max(...e.map(f=>f.cells.rows));for(let f=0;f<c;f++)for(let s=0;s<r.cells.cols;s++)e.every(u=>u.crit.test(u.cells.at(f,s)))&&t(f,s)}function j(e,n){return e instanceof T?(e.rows-n.rows)*e.cols:0}function R(e,n,t,r){let o;return F(n,t,(i,c)=>{if(o)return;const f=e.at(i,c);f instanceof l?o=f:typeof f=="number"&&r(f)}),o}function v(e,n){const t=h(e[0],n);if(t instanceof l)return t;const r=e[2];return r===void 0?t:h(r,n)}const $=e=>(n,t)=>{const r=h(n[0],t);if(r instanceof l)return r;const o=E(n[1],t);if(o instanceof l)return o;const i=v(n,t);if(i instanceof l)return i;const c=[];return R(i,[{cells:r,crit:o}],t,s=>{c.push(s)})??e(c)},S=e=>(n,t)=>{const r=h(n[0],t);if(r instanceof l)return r;const o=x(n.slice(1),t);if(o instanceof l)return o;const i=o[0]?.cells;if(i&&!M(r,i))return g.VALUE;const c=[];return R(r,o,t,s=>{c.push(s)})??e(c)},a=(e,n)=>(t,r)=>{const o=[],i=[e];for(const u of t)if(u instanceof T)o.push(u),i.push(`r${u.key}`);else if(u instanceof C)i.push("-");else if(u instanceof l)i.push(`e${u.code}`);else if(u===null||typeof u!="object")i.push(`${typeof u}:${String(u)}`);else return n(t,r);if(o.length===0)return n(t,r);const c=i.join("\0"),f=r.calc.results.get(c);if(f!==void 0)return f;const s=n(t,r);return(s===null||typeof s!="object"||s instanceof l)&&r.calc.results.set(c,o,s),s},U=e=>e.reduce((n,t)=>n+t,0),N=e=>e.length===0?g.DIV0:U(e)/e.length,L=e=>e.length===0?0:e.reduce((n,t)=>t<n?t:n),z=e=>e.length===0?0:e.reduce((n,t)=>t>n?t:n);p("SUMIF","rv?r",a("SUMIF",$(U))),p("AVERAGEIF","rv?r",a("AVERAGEIF",$(N))),p("SUMIFS","r(rv)",a("SUMIFS",S(U))),p("AVERAGEIFS","r(rv)",a("AVERAGEIFS",S(N))),p("MINIFS","r(rv)",a("MINIFS",S(L))),p("MAXIFS","r(rv)",a("MAXIFS",S(z))),p("COUNTIF","rv",a("COUNTIF",(e,n)=>{const t=h(e[0],n);if(t instanceof l)return t;const r=E(e[1],n);if(r instanceof l)return r;let o=0;return F([{cells:t,crit:r}],n,()=>{o+=1}),o+(r.test(null)?j(e[0],t):0)})),p("COUNTIFS","(rv)",a("COUNTIFS",(e,n)=>{const t=x(e,n);if(t instanceof l)return t;let r=0;return F(t,n,()=>{r+=1}),r}));
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{parseDate as B,parseTime as H,parseValue as
|
|
2
|
+
import{parseDate as B,parseTime as H,parseValue as L}from"numfmt";import{CellError as c,ERR as u,dateOf as h,serialOf as p}from"../../values.js";import{toBool as X,toNumber as m,toText as T}from"../coerce.js";import{Missing as v}from"../types.js";import{eachRow as g,number as S,numbersOf as j,raw as U,scalar as l}from"./define.js";const P=2958465,y=e=>{const n=m(e);return n instanceof c?n:n<0||n>=P+1?u.NUM:Math.floor(n)},Y=e=>e%4===0&&e%100!==0||e%400===0,A=(e,n)=>n===2?Y(e)?29:28:[4,6,9,11].includes(n)?30:31,I=e=>e.month===2&&e.day===A(e.year,2);l("DATE",3,3,S((e=0,n=1,r=1)=>{const t=Math.trunc(e);if(t<0||t>9999)return u.NUM;const o=p(t<1900?t+1900:t,Math.trunc(n),Math.trunc(r));return o<0?u.NUM:o})),l("TIME",3,3,S((e=0,n=0,r=0)=>{const t=Math.trunc(e)*3600+Math.trunc(n)*60+Math.trunc(r);return t<0?u.NUM:t%86400/86400})),l("YEAR",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).year}),l("MONTH",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).month}),l("DAY",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).day});const b=e=>{const n=m(e);return n instanceof c?n:n<0?u.NUM:Math.round((n-Math.floor(n))*86400)%86400};l("HOUR",1,1,e=>{const n=b(e);return n instanceof c?n:Math.floor(n/3600)}),l("MINUTE",1,1,e=>{const n=b(e);return n instanceof c?n:Math.floor(n/60)%60}),l("SECOND",1,1,e=>{const n=b(e);return n instanceof c?n:n%60}),l("DATEVALUE",1,1,e=>{const n=T(e);if(n instanceof c)return n;const r=B(n)??L(n);return typeof r?.value=="number"?Math.floor(r.value):u.VALUE}),l("TIMEVALUE",1,1,e=>{const n=T(e);if(n instanceof c)return n;const r=H(n)??L(n);return typeof r?.value!="number"?u.VALUE:r.value-Math.floor(r.value)});const D=e=>(e%7+6)%7;l("WEEKDAY",1,2,(e,n)=>{const r=y(e);if(r instanceof c)return r;const t=n==null?1:m(n);if(t instanceof c)return t;const o=D(r);return t===1?o+1:t===2?(o+6)%7+1:t===3?(o+6)%7:t>=11&&t<=17?(o+7-(t-10)%7)%7+1:u.NUM});const V=e=>{const n=(D(e)+6)%7,r=e-n+3,{year:t}=h(r);return Math.floor((r-p(t,1,1))/7)+1};l("ISOWEEKNUM",1,1,e=>{const n=y(e);return n instanceof c?n:V(n)}),l("WEEKNUM",1,2,(e,n)=>{const r=y(e);if(r instanceof c)return r;const t=n==null?1:m(n);if(t instanceof c)return t;if(t===21)return V(r);const o=t===1?0:t===2?1:t>=11&&t<=17?(t-10)%7:-1;if(o<0)return u.NUM;const a=p(h(r).year,1,1),s=(D(a)+7-o)%7;return Math.floor((r-a+s)/7)+1});const R=(e,n)=>{const{year:r,month:t,day:o}=h(e),a=r*12+t-1+n,s=Math.floor(a/12),f=a-s*12+1;return p(s,f,Math.min(o,A(s,f)))};l("EDATE",2,2,(e,n)=>{const r=y(e),t=m(n);if(r instanceof c)return r;if(t instanceof c)return t;const o=R(r,Math.trunc(t));return o<0?u.NUM:o}),l("EOMONTH",2,2,(e,n)=>{const r=y(e),t=m(n);if(r instanceof c)return r;if(t instanceof c)return t;const o=h(R(r,Math.trunc(t))),a=p(o.year,o.month,A(o.year,o.month));return a<0?u.NUM:a}),l("DAYS",2,2,(e,n)=>{const r=y(e),t=y(n);return r instanceof c?r:t instanceof c?t:r-t}),l("DATEDIF",3,3,(e,n,r)=>{const t=y(e),o=y(n),a=T(r);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;if(t>o)return u.NUM;const s=h(t),f=h(o),i=(f.year-s.year)*12+f.month-s.month-(f.day<s.day?1:0);switch(a.toUpperCase()){case"D":return o-t;case"M":return i;case"Y":return Math.floor(i/12);case"YM":return i%12;case"MD":{if(f.day>=s.day)return f.day-s.day;const d=f.month===1?12:f.month-1,N=f.month===1?f.year-1:f.year;return A(N,d)-s.day+f.day}case"YD":{const d=f.year-s.year-(i%12<0?1:0),N=Math.floor(i/12),M=R(t,N*12);return d>=0?o-M:u.NUM}default:return u.NUM}});function W(e,n,r){const t=h(e),o=h(n);let a=t.day,s=o.day,f=o.month,i=o.year;return r?(a=Math.min(a,30),s=Math.min(s,30)):(a===A(t.year,t.month)&&(a=30),s===31&&(a<30?(s=1,f+=1,f>12&&(f=1,i+=1)):s=30)),(i-t.year)*360+(f-t.month)*30+s-a}l("DAYS360",2,3,(e,n,r)=>{const t=y(e),o=y(n),a=r===void 0?!1:X(r);return t instanceof c?t:o instanceof c?o:a instanceof c?a:W(t,o,a)});export function yearFrac(e,n,r){const[t,o]=e<=n?[e,n]:[n,e],a=h(t),s=h(o),f=o-t;switch(r){case 0:{let i=a.day,d=s.day;return I(a)&&(I(s)&&(d=30),i=30),d===31&&i>=30&&(d=30),i===31&&(i=30),((s.year-a.year)*360+(s.month-a.month)*30+d-i)/360}case 2:return f/360;case 3:return f/365;case 4:return W(t,o,!0)/360;default:{if(a.year===s.year||s.year===a.year+1&&(a.month>s.month||a.month===s.month&&a.day>=s.day)){let M;if(a.year===s.year)M=Y(a.year);else{const E=w=>Y(w)?p(w,2,29):1/0;M=E(a.year)>=t&&E(a.year)<=o||E(s.year)>=t&&E(s.year)<=o}return f/(M?366:365)}const d=s.year-a.year+1,N=p(s.year+1,1,1)-p(a.year,1,1);return f/(N/d)}}}l("YEARFRAC",2,3,(e,n,r)=>{const t=y(e),o=y(n),a=r==null?0:m(r);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;const s=Math.trunc(a);return s<0||s>4?u.NUM:yearFrac(t,o,s)});function _(e){const n=(...t)=>[0,1,2,3,4,5,6].map(o=>t.includes(o));if(e==null)return n(0,6);if(typeof e=="string"){if(!/^[01]{7}$/u.test(e)||e==="1111111")return u.VALUE;const t=e.split("").map(o=>o==="1");return[t[6]??!1,...t.slice(0,6)]}const r=m(e);return r instanceof c?r:r>=1&&r<=7?n((r+5)%7,(r+6)%7):r>=11&&r<=17?n(r-11):u.NUM}function k(e,n){if(e===void 0||e instanceof v)return new Set;const r=j([e],n);return r instanceof c?r:new Set(r.map(t=>Math.floor(t)))}function $(e,n,r,t){const o=e<=n?1:-1;let a=0;for(let s=e;o>0?s<=n:s>=n;s+=o)r[D(s)]!==!0&&!t.has(s)&&(a+=o);return a}function q(e,n,r,t){if(r.every(Boolean))return u.VALUE;const o=n<0?-1:1;let a=e;for(let s=Math.abs(n);s>0;)a+=o,r[D(a)]!==!0&&!t.has(a)&&(s-=1);return a<0?u.NUM:a}const K=e=>(n,r)=>{const t=y(r.calc.scalar(n[0],r.frame)),o=y(r.calc.scalar(n[1],r.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=e?n[2]:void 0,s=_(a===void 0||a instanceof v?void 0:r.calc.scalar(a,r.frame));if(s instanceof c)return s;const f=k(n[e?3:2],r);return f instanceof c?f:$(t,o,s,f)},x=e=>(n,r)=>{const t=y(r.calc.scalar(n[0],r.frame)),o=m(r.calc.scalar(n[1],r.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=e?n[2]:void 0,s=_(a===void 0||a instanceof v?void 0:r.calc.scalar(a,r.frame));if(s instanceof c)return s;const f=k(n[e?3:2],r);return f instanceof c?f:q(t,Math.trunc(o),s,f)};U("NETWORKDAYS","vv?r",K(!1)),U("NETWORKDAYS.INTL","vv?vr",K(!0)),U("WORKDAY","vv?r",x(!1)),U("WORKDAY.INTL","vv?vr",x(!0)),U("NOW","",(e,n)=>n.calc.nowSerial(),{volatile:!0}),U("TODAY","",(e,n)=>Math.floor(n.calc.nowSerial()),{volatile:!0});function z(e,n,r){const t=r[0]??0;let o=0;return n.forEach((a,s)=>{o+=a/(1+e)**(((r[s]??t)-t)/365)}),o}const F=(e,n)=>{const r=[];return g([e],n,t=>r.push(t)),r},C=(e,n)=>{const r=F(e[0],n),t=F(e[1],n);if(r.length!==t.length)return u.NUM;const o=[],a=[];for(const[s,f]of r.entries()){const i=t[s]??null;if(f instanceof c)return f;if(i instanceof c)return i;if(typeof f=="number"){if(typeof i!="number")return u.VALUE;o.push(f),a.push(Math.floor(i))}}return o.length===0?u.NUM:{values:o,dates:a}};U("XNPV","vrr",(e,n)=>{const r=m(n.calc.scalar(e[0],n.frame));if(r instanceof c)return r;const t=C(e.slice(1),n);return t instanceof c?t:r<=0?u.NUM:z(r,t.values,t.dates)}),U("XIRR","rr?v",(e,n)=>{const r=C(e,n);if(r instanceof c)return r;const{values:t,dates:o}=r;if(!t.some(i=>i>0)||!t.some(i=>i<0))return u.NUM;const a=e[2]===void 0||e[2]instanceof v?.1:m(n.calc.scalar(e[2],n.frame));if(a instanceof c)return a;const s=o[0]??0;let f=a;for(let i=0;i<100;i++){let d=0,N=0;t.forEach((E,w)=>{const O=((o[w]??s)-s)/365;d+=E/(1+f)**O,N-=O*E/(1+f)**(O+1)});const M=f-d/N;if(!Number.isFinite(M))return u.NUM;if(Math.abs(M-f)<1e-10)return M;f=M<=-1?(f-1)/2:M}return u.NUM});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Node } from "../../formula/ast.js";
|
|
2
|
-
import { CellError, type Scalar } from "../../values.js";
|
|
2
|
+
import { CellError, Matrix, type Scalar } from "../../values.js";
|
|
3
3
|
import type { Calc } from "../engine.js";
|
|
4
4
|
import { Reference, type Frame, type Value } from "../types.js";
|
|
5
5
|
export interface Call {
|
|
@@ -75,6 +75,8 @@ export interface Cells {
|
|
|
75
75
|
at: (row: number, col: number) => Scalar;
|
|
76
76
|
/** Its reference, when it is one. */
|
|
77
77
|
reference?: Reference;
|
|
78
|
+
/** Its array, when it is one. */
|
|
79
|
+
array?: Matrix;
|
|
78
80
|
}
|
|
79
81
|
/** A range's cells, down to the last row its sheet uses. */
|
|
80
82
|
export declare function cellsOf(v: Value, c: Call): Cells | CellError;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{CellError as
|
|
2
|
+
import{CellError as a,ERR as l,Matrix as i}from"../../values.js";import{toBool as w,toNumber as c,toText as A}from"../coerce.js";import{Lambda as h,Missing as p,Reference as g,Union as x}from"../types.js";export const FUNCTIONS=new Map;const d=255;export const many=d;export function signature(e){const r=/^([vr?]*)(?:\(([vr]+)\))?$/u.exec(e),t=r?.[1];if(t===void 0||(t.match(/\?/gu)??[]).length>1)throw new Error(`"${e}" is not a signature.`);const o=m=>Array.from({length:m.length},(y,b)=>m.charAt(b)==="v"?"value":"whole"),n=o(t.replace("?","")),f=o(r?.[2]??""),s=t.indexOf("?"),u=s>=0?s:n.length+f.length;return{fixed:n,group:f,min:u,max:f.length>0?d:n.length}}export function paramAt(e,r){return r<e.fixed.length?e.fixed[r]??"whole":e.group.length===0?"whole":e.group[(r-e.fixed.length)%e.group.length]??"whole"}export function fillsGroup(e,r){return e.group.length===0||r<=e.fixed.length?!0:(r-e.fixed.length)%e.group.length===0}export function scalar(e,r,t,o,n={}){FUNCTIONS.set(e,{name:e,min:r,max:t,kind:"scalar",call:o,from:n.from??"allyhub",...n.errors===!0?{errors:!0}:{},...n.volatile===!0?{volatile:!0}:{}})}export function raw(e,r,t,o={}){const{min:n,max:f,fixed:s,group:u}=signature(r);FUNCTIONS.set(e,{name:e,min:n,max:f,kind:"raw",params:{fixed:s,group:u},call:t,from:o.from??"allyhub",...o.volatile===!0?{volatile:!0}:{}})}export function lazy(e,r,t,o){FUNCTIONS.set(e,{name:e,min:r,max:t,kind:"lazy",call:o,from:"allyhub"})}export function alias(e,r){const t=FUNCTIONS.get(r);t&&FUNCTIONS.set(e,{...t,name:e})}export function cellsOf(e,r){if(e instanceof a)return e;if(e instanceof x)return l.VALUE;if(e instanceof g){const o=Math.min(e.bottom,e.sheet.lastRow);return{rows:Math.max(0,o-e.top+1),cols:e.cols,at:(n,f)=>r.calc.read(e.sheet,e.top+n,e.left+f),reference:e}}if(e instanceof i)return{rows:e.rows,cols:e.cols,at:(o,n)=>e.at(o,n),array:e};if(e instanceof h)return l.VALUE;const t=e instanceof p?null:e;return{rows:1,cols:1,at:()=>t}}export function eachArg(e,r,t){for(const o of e)if(o instanceof g)r.calc.each(o,n=>{t(n,!1)});else if(o instanceof x)for(const n of o.parts)r.calc.each(n,f=>{t(f,!1)});else if(o instanceof i)for(const n of o.data)n!==null&&t(n,!1);else o instanceof p?t(null,!0):o instanceof h?t(l.VALUE,!0):t(o,!0)}export function eachRow(e,r,t){const o=n=>{for(const f of r.calc.matrix(n).data)t(f,!1)};for(const n of e)if(n instanceof g)o(n);else if(n instanceof x)n.parts.forEach(o);else if(n instanceof i)for(const f of n.data)t(f,!1);else n instanceof p?t(null,!0):n instanceof h?t(l.VALUE,!0):t(n,!0)}export function numbersOf(e,r){const t=[];let o;return eachArg(e,r,(n,f)=>{if(!o){if(n instanceof a)o=n;else if(typeof n=="number")t.push(n);else if(f){const s=c(n);s instanceof a?o=s:t.push(s)}}}),o??t}export function numbersOfA(e,r){const t=[];let o;return eachArg(e,r,(n,f)=>{if(!o)if(n instanceof a)o=n;else if(typeof n=="number")t.push(n);else if(typeof n=="boolean")t.push(n?1:0);else if(f){const s=c(n);s instanceof a?o=s:t.push(s)}else typeof n=="string"&&t.push(0)}),o??t}export const number=e=>(...r)=>{const t=[];for(const o of r){const n=c(o);if(n instanceof a)return n;t.push(n)}return e(...t)};export function textArg(e){return A(e)}export function boolArg(e,r){return e==null?r:w(e)}export function numberArg(e,r){return e===void 0?r:c(e)}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{cellName as N}from"../../address.js";import{CellError as s,ERR as n,Matrix as R}from"../../values.js";import{FormulaCell as
|
|
2
|
+
import{cellName as N}from"../../address.js";import{CellError as s,ERR as n,Matrix as R}from"../../values.js";import{FormulaCell as E}from"../../workbook.js";import{toNumber as L,toText as d}from"../coerce.js";import{Lambda as I,Reference as l,Union as y}from"../types.js";import{lazy as m,raw as i,scalar as c}from"./define.js";const f=(e,r)=>{c(e,1,1,t=>r(t),{errors:!0})};f("ISBLANK",e=>e==null),f("ISNUMBER",e=>typeof e=="number"),f("ISTEXT",e=>typeof e=="string"),f("ISNONTEXT",e=>typeof e!="string"),f("ISLOGICAL",e=>typeof e=="boolean"),f("ISERROR",e=>e instanceof s),f("ISERR",e=>e instanceof s&&e!==n.NA),f("ISNA",e=>e===n.NA);const p=(e,r)=>{const t=L(e);return t instanceof s?t:Math.abs(Math.trunc(t))%2===r};c("ISEVEN",1,1,e=>p(e,0)),c("ISODD",1,1,e=>p(e,1)),c("N",1,1,e=>typeof e=="number"?e:typeof e=="boolean"?e?1:0:e instanceof s?e:0,{errors:!0}),i("NA","",()=>n.NA),i("TYPE","r",(e,r)=>{const t=r.calc.flat(e[0]);return t instanceof R?64:typeof t=="number"||t===null?1:typeof t=="string"?2:typeof t=="boolean"?4:16});const S=[n.NULL,n.DIV0,n.VALUE,n.REF,n.NAME,n.NUM,n.NA];c("ERROR.TYPE",1,1,e=>{const r=e instanceof s?S.indexOf(e):-1;return r<0?n.NA:r+1},{errors:!0}),m("ISREF",1,1,(e,r)=>{const t=e[0];if(!t)return!1;const o=r.calc.eval(t,r.frame);return o instanceof l||o instanceof y});const A=e=>e instanceof l?e.sheet.at(e.top,e.left):void 0;m("ISFORMULA",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=r.calc.eval(t,r.frame);return o instanceof I?n.VALUE:A(o)instanceof E}),m("FORMULATEXT",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=A(r.calc.eval(t,r.frame));return o instanceof E?o.text:n.NA}),i("CELL","v?r",(e,r)=>{const t=d(r.calc.scalar(e[0],r.frame));if(t instanceof s)return t;const o=e[1],a=o instanceof l?o:new l(r.frame.sheet,r.frame.row,r.frame.col,r.frame.row,r.frame.col),u=r.calc.read(a.sheet,a.top,a.left);switch(t.toLowerCase()){case"row":return a.top+1;case"col":return a.left+1;case"address":return N({row:a.top,col:a.left}).replace(/^([A-Z]+)(\d+)$/u,"$$$1$$$2");case"contents":return u;case"type":return u===null?"b":typeof u=="string"?"l":"v";default:return n.VALUE}}),i("TABLE","?rrrrr",()=>n.NA);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import*as
|
|
2
|
+
import*as X from"@formulajs/formulajs";import{CellError as u,ERR as l,errorOf as k,numberText as g}from"../../values.js";import{Missing as y}from"../types.js";import{toBool as B,toNumber as N,toText as h}from"../coerce.js";import{alias as V,cellsOf as b,eachArg as x,numbersOf as H,numbersOfA as j,raw as D,scalar as d}from"./define.js";const w=X;function m(n){const t=n.split(".").reduce((r,e)=>r?.[e],w);if(typeof t!="function")throw new Error(`@formulajs/formulajs has no ${n}.`);return t}function v(n){return typeof n=="number"?Number.isFinite(n)?n:l.NUM:n instanceof Error?k(n.message)??l.VALUE:typeof n=="boolean"||typeof n=="string"?n:l.VALUE}const L=(n,t)=>{try{return v(n(...t))}catch{return l.VALUE}};function K(n,t){return H([n],t)}function Q(n,t){const r=b(n,t);if(r instanceof u)return r;const e=[];for(let f=0;f<r.rows;f++){const s=[];for(let E=0;E<r.cols;E++){const o=r.at(f,E);if(o instanceof u)return o;s.push(typeof o=="number"?o:0)}e.push(s)}return e}function c(n,t,r){const e=t.split("?")[0]?.length??0,f=t.replace("?",""),s=m(r);d(n,e,f.length,(...E)=>{const o=[];for(const[i,I]of E.entries()){if(I===void 0){o.push(void 0);continue}const R=f.charAt(i),a=R==="n"?N(I):R==="b"?B(I):h(I);if(a instanceof u)return a;o.push(a)}return L(s,o)},{from:"formulajs"})}function O(n,t,r){const e=t.replace("?",""),f=e.endsWith("r"),s=m(r),E=Array.from({length:t.length},(o,i)=>{const I=t.charAt(i);return I==="?"?"?":f&&i===t.length-1?"(r)":"lm".includes(I)?"r":"v"}).join("");D(n,E,(o,i)=>{const I=[];for(const[R,a]of o.entries()){const P=f&&R>=e.length-1?"r":e.charAt(R);if(P==="r"){const A=H(o.slice(R),i);if(A instanceof u)return A;I.push(A);break}if(a instanceof y){I.push(void 0);continue}let T;if(P==="l")T=K(a,i);else if(P==="m")T=Q(a,i);else{const A=i.calc.scalar(a,i.frame);T=P==="n"?N(A):P==="b"?B(A):h(A)}if(T instanceof u)return T;I.push(T)}return L(s,I)},{from:"formulajs"})}function M(n,t,r="",e=1){const f=m(t),s=r.length;D(n,`${"v".repeat(s)}rr`,(E,o)=>{const i=[];for(const A of E.slice(0,s)){const S=N(o.calc.scalar(A,o.frame));if(S instanceof u)return S;i.push(S)}const I=b(E[s],o),R=b(E[s+1],o);if(I instanceof u)return I;if(R instanceof u)return R;if(I.rows*I.cols!==R.rows*R.cols)return l.NA;const a=[],P=[];for(let A=0;A<I.rows;A++)for(let S=0;S<I.cols;S++){const p=I.at(A,S),U=R.rows===I.rows?R.at(A,S):R.at(S,A);if(p instanceof u)return p;if(U instanceof u)return U;typeof p=="number"&&typeof U=="number"&&(a.push(p),P.push(U))}if(a.length<e)return l.DIV0;const T=L(f,[...i,a,P]);return T===l.NUM?l.DIV0:T},{from:"formulajs"})}function C(n,t,r=!1,e=1,f=l.DIV0){const s=m(t);D(n,"(r)",(E,o)=>{const i=r?j(E,o):H(E,o);if(i instanceof u)return i;if(i.length<e)return f;const I=L(s,[i]);return I===l.NUM&&f===l.DIV0?l.DIV0:I},{from:"formulajs"})}function G(n){const t=/^([+-]?[\d.]+(?:e[+-]?\d+)?)?(?:([+-]?)([\d.]+(?:e[+-]?\d+)?)?([ij]))?$/iu.exec(n);if(!t)return n;const[,r,e,f,s]=t,E=r===void 0?"":g(Number(r));if(s===void 0)return E;const o=f===void 0?"":g(Number(f));return`${E}${e??""}${o==="1"?"":o}${s}`}function F(n,t){const r=m(n);d(n,t.length,t.length,(...e)=>{const f=[];for(const[E,o]of e.entries()){const i=t.charAt(E)==="n"?N(o):h(o);if(i instanceof u)return i;f.push(i)}const s=L(r,f);return typeof s=="string"?G(s):s},{from:"formulajs"})}for(const n of["ABS","ACOS","ACOSH","ACOT","ACOTH","ASIN","ASINH","ATAN","ATANH","COS","COSH","COT","COTH","CSC","CSCH","DEGREES","EVEN","EXP","INT","LN","LOG10","ODD","RADIANS","SEC","SECH","SIGN","SIN","SINH","SQRT","SQRTPI","TAN","TANH"])c(n,"n",n);for(const n of["COMBIN","COMBINA","MROUND","PERMUT","PERMUTATIONA","POWER"])c(n,"nn",n);c("TRUNC","n?n","TRUNC"),c("CEILING","nn","CEILING"),d("FLOOR",2,2,(n,t)=>{const r=N(n??0);if(r instanceof u)return r;const e=N(t??0);return e instanceof u?e:r===0&&e===0?0:L(m("FLOOR"),[r,e])}),c("CEILING.MATH","n?nn","CEILINGMATH"),c("FLOOR.MATH","n?nn","FLOORMATH"),c("CEILING.PRECISE","n?n","CEILINGPRECISE"),c("FLOOR.PRECISE","n?n","FLOORPRECISE"),V("ISO.CEILING","CEILING.PRECISE"),d("PI",0,0,()=>Math.PI),D("RAND","",(n,t)=>t.calc.random(),{volatile:!0}),D("RANDBETWEEN","vv",(n,t)=>{const r=N(t.calc.scalar(n[0],t.frame)),e=N(t.calc.scalar(n[1],t.frame));if(r instanceof u)return r;if(e instanceof u)return e;const f=Math.ceil(r),s=Math.floor(e);return f>s?l.NUM:f+Math.floor(t.calc.random()*(s-f+1))},{volatile:!0}),C("MULTINOMIAL","MULTINOMIAL"),C("AVEDEV","AVEDEV",!1,1,l.NUM),C("DEVSQ","DEVSQ",!1,1,l.NUM),C("MEDIAN","MEDIAN",!1,1,l.NUM),C("SKEW","SKEW",!1,3),C("SKEW.P","SKEW.P",!1,3),C("STDEV.S","STDEV.S",!1,2),C("STDEV.P","STDEV.P",!1,1),C("VAR.S","VAR.S",!1,2),C("VAR.P","VAR.P",!1,1),C("STDEVA","STDEVA",!0,2),C("STDEVPA","STDEVPA",!0,1),C("VARA","VARA",!0,2),C("VARPA","VARPA",!0,1);for(const[n,t]of[["STDEV","STDEV.S"],["STDEVP","STDEV.P"],["VAR","VAR.S"],["VARP","VAR.P"]])V(n,t);O("PERCENTILE.INC","ln","PERCENTILE.INC"),O("PERCENTILE.EXC","ln","PERCENTILE.EXC"),O("PERCENTRANK.INC","ln?n","PERCENTRANK.INC"),O("PERCENTRANK.EXC","ln?n","PERCENTRANK.EXC"),O("QUARTILE.EXC","ln","QUARTILE.EXC"),D("TRIMMEAN","rv",(n,t)=>{const r=H([n[0]],t);if(r instanceof u)return r;const e=N(t.calc.scalar(n[1],t.frame));if(e instanceof u)return e;if(e<0||e>=1||r.length===0)return l.NUM;const f=Math.floor(r.length*e/2),s=[...r].sort((E,o)=>E-o).slice(f,r.length-f);return s.reduce((E,o)=>E+o,0)/s.length}),V("PERCENTILE","PERCENTILE.INC"),V("PERCENTRANK","PERCENTRANK.INC"),M("CORREL","CORREL"),M("PEARSON","PEARSON"),M("RSQ","RSQ"),M("SLOPE","SLOPE"),M("INTERCEPT","INTERCEPT"),M("STEYX","STEYX","",3),M("COVARIANCE.P","COVARIANCE.P"),M("COVARIANCE.S","COVARIANCE.S","",2),M("FORECAST","FORECAST","n"),V("FORECAST.LINEAR","FORECAST"),V("COVAR","COVARIANCE.P"),c("PV","nnn?nn","PV"),c("NPER","nnn?nn","NPER"),c("RATE","nnn?nnn","RATE"),c("SYD","nnnn","SYD"),c("DDB","nnnn?n","DDB"),c("ISPMT","nnnn","ISPMT"),c("EFFECT","nn","EFFECT"),c("NOMINAL","nn","NOMINAL"),c("CUMIPMT","nnnnnn","CUMIPMT"),c("CUMPRINC","nnnnnn","CUMPRINC"),c("DOLLARDE","nn","DOLLARDE"),c("DOLLARFR","nn","DOLLARFR"),c("PDURATION","nnn","PDURATION"),c("RRI","nnn","RRI"),O("IRR","l?n","IRR"),O("MIRR","lnn","MIRR"),O("NPV","nr","NPV"),O("FVSCHEDULE","nl","FVSCHEDULE");for(const n of["BIN2DEC","HEX2DEC","OCT2DEC","IMABS","IMAGINARY","IMARGUMENT","IMREAL"])c(n,"t",n);for(const n of["IMCONJUGATE","IMCOS","IMCOSH","IMCOT","IMCSC","IMCSCH","IMEXP","IMLN","IMLOG10","IMLOG2","IMSEC","IMSECH","IMSIN","IMSINH","IMSQRT","IMTAN"])F(n,"t");for(const n of["BIN2OCT","HEX2BIN","HEX2OCT","OCT2BIN"])c(n,"t?n",n);for(const n of["DEC2BIN","DEC2OCT"])c(n,"n?n",n);for(const[n,t]of[["BIN2HEX","t"],["OCT2HEX","t"],["DEC2HEX","n"]]){const r=m(n);d(n,1,2,(e,f)=>{const s=t==="n"?N(e):h(e);if(s instanceof u)return s;const E=f===void 0?void 0:N(f);if(E instanceof u)return E;const o=L(r,[s,E]);return typeof o=="string"?o.toUpperCase():o},{from:"formulajs"})}F("IMDIV","tt"),F("IMSUB","tt"),F("IMPOWER","tn"),c("COMPLEX","nn?t","COMPLEX"),c("DECIMAL","tn","DECIMAL"),c("ARABIC","t","ARABIC");for(const n of["IMSUM","IMPRODUCT"]){const t=m(n);D(n,"(r)",(r,e)=>{const f=[];let s;if(x(r,e,o=>{if(s||o===null)return;const i=h(o);i instanceof u?s=i:f.push(i)}),s)return s;const E=L(t,f);return typeof E=="string"?G(E):E},{from:"formulajs"})}c("DELTA","n?n","DELTA"),c("GESTEP","n?n","GESTEP");
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{CellError as a,ERR as
|
|
2
|
+
import{CellError as a,ERR as p,Matrix as k}from"../../values.js";import{cellName as K,columnLetter as g,MAX_COLUMNS as W,MAX_ROWS as L}from"../../address.js";import{sheetPrefix as q}from"../../formula/print.js";import{parseFormula as G}from"../../formula/parse.js";import{compare as v,toBool as U,toNumber as M,toText as H}from"../coerce.js";import{Missing as O,Reference as x,Union as F}from"../types.js";import{cellsOf as R,lazy as J,raw as A,scalar as Q}from"./define.js";import{indexKeyOf as Y}from"../indexes.js";import{hasWildcard as Z,indexKey as C,literalOf as nn,wildcard as tn}from"./criteria.js";const I=(n,t)=>typeof n==typeof t&&!(n instanceof a)&&n!==null;function T(n){if(n.rows===1)return{length:n.cols,at:t=>n.at(0,t)};if(n.cols===1)return{length:n.rows,at:t=>n.at(t,0)}}const P=new WeakMap;function en(n,t){const e=P.get(n);if(e)return e;if(e===void 0){P.set(n,null);return}const o=t();return P.set(n,o),o}function V(n,t,e,o,r,s=0,c=1){const f=typeof n=="string"?n:void 0,i=r&&f!==void 0&&Z(f)?nn(f):f;if(f!==void 0&&i===void 0){const l=tn(f);for(let d=s;d>=0&&d<t.length;d+=c){const h=t.at(d);if(typeof h=="string"&&l(h))return d}return-1}const u=C(i??n);if(u===void 0)return-1;if(t.length>=64&&s===0&&c===1){const l=()=>{const w=new Map;for(let m=t.length-1;m>=0;m--){const E=C(t.at(m));E!==void 0&&w.set(E,[m])}return w},d=e.reference,h=d?o.calc.indexes.ofRepeated(Y("find",d),[d],l):e.array&&en(e.array,l);if(h)return h.get(u)?.[0]??-1}for(let l=s;l>=0&&l<t.length;l+=c)if(C(t.at(l))===u)return l;return-1}function N(n,t,e=!1){let o=0,r=t.length-1,s=-1;for(;o<=r;){const c=o+r>>1;let f=c;for(;f>=o&&!I(t.at(f),n);)f-=1;if(f<o){o=c+1;continue}const i=v(t.at(f),n);(e?i>=0:i<=0)?(s=f,o=c+1):r=f-1}return s}const y=(n,t)=>t.calc.scalar(n??null,t.frame),rn=(n,t,e)=>n===void 0?e:n instanceof O?!1:U(t.calc.scalar(n,t.frame));function X(n){return(t,e)=>{const o=y(t[0],e);if(o instanceof a)return o;const r=R(t[1],e);if(r instanceof a)return r;const s=M(e.calc.scalar(t[2],e.frame));if(s instanceof a)return s;const c=Math.trunc(s);if(c<1)return p.VALUE;if(c>(n?r.cols:r.rows))return p.REF;const f=rn(t[3],e,!0);if(f instanceof a)return f;const i={length:n?r.rows:r.cols,at:d=>n?r.at(d,0):r.at(0,d)},u={rows:n?r.rows:1,cols:n?1:r.cols,at:(d,h)=>r.at(d,h),...r.reference?{reference:new x(r.reference.sheet,r.reference.top,r.reference.left,n?r.reference.bottom:r.reference.top,n?r.reference.left:r.reference.right)}:{}},l=f?N(o,i):V(o,i,u,e,!0);return l<0?p.NA:n?r.at(l,c-1):r.at(c-1,l)}}A("VLOOKUP","vrv?v",X(!0)),A("HLOOKUP","vrv?v",X(!1)),A("MATCH","vr?v",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=T(o);if(!r)return p.NA;const s=n[2]===void 0?1:n[2]instanceof O?0:M(t.calc.scalar(n[2],t.frame));if(s instanceof a)return s;const c=s===0?V(e,r,o,t,!0):N(e,r,s<0);return c<0?p.NA:c+1}),A("LOOKUP","vr?r",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=o.rows>=o.cols,s={length:r?o.rows:o.cols,at:u=>r?o.at(u,0):o.at(0,u)},c=N(e,s);if(c<0)return p.NA;if(n[2]===void 0)return r?o.at(c,o.cols-1):o.at(o.rows-1,c);const f=R(n[2],t);if(f instanceof a)return f;const i=T(f);return i?i.at(c):p.NA});function D(n,t,e,o,r){const s=T(t);if(!s)return-1;const c=r===-1;if(Math.abs(r)===2){const l=N(n,s,r<0);if(l>=0&&v(s.at(l),n)===0)return l;if(o===0)return-1}const i=V(n,s,t,e,o===2,c?s.length-1:0,c?-1:1);if(i>=0||o===0||o===2)return i;let u=-1;for(let l=0;l<s.length;l++){const d=s.at(l);if(!I(d,n))continue;const h=v(d,n),w=u<0||(o<0?v(d,s.at(u))>0:v(d,s.at(u))<0);(o<0?h<0:h>0)&&w&&(u=l)}return u}const _=(n,t,e)=>{const o=(c,f)=>c===void 0||c instanceof O?f:M(e.calc.scalar(c,e.frame)),r=o(n,0);if(r instanceof a)return r;const s=o(t,1);return s instanceof a?s:[r,s]};A("XLOOKUP","vrr?rvv",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=R(n[2],t);if(r instanceof a)return r;const s=_(n[4],n[5],t);if(s instanceof a)return s;const c=D(e,o,t,s[0],s[1]);if(c<0){const i=n[3];return i===void 0||i instanceof O?p.NA:t.calc.flat(i)}if(o.cols===1){if(r.cols===1)return r.at(c,0);const i=[];for(let u=0;u<r.cols;u++)i.push(r.at(c,u));return new k(1,r.cols,i)}if(r.rows===1)return r.at(0,c);const f=[];for(let i=0;i<r.rows;i++)f.push(r.at(i,c));return new k(r.rows,1,f)}),A("XMATCH","vr?vv",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=_(n[2],n[3],t);if(r instanceof a)return r;const s=D(e,o,t,r[0],r[1]);return s<0?p.NA:s+1}),A("INDEX","rv?vv",(n,t)=>{let e=n[0];const o=w=>{if(w===void 0||w instanceof O)return 0;const m=M(t.calc.scalar(w,t.frame));return m instanceof a?m:Math.trunc(m)};if(e instanceof F){const w=o(n[3]??1);if(w instanceof a)return w;const m=e.parts[w-1];if(!m)return p.REF;e=m}if(e instanceof a)return e;const r=o(n[1]);if(r instanceof a)return r;const s=o(n[2]);if(s instanceof a)return s;if(r<0||s<0)return p.VALUE;if(e instanceof x){const w=n[2]===void 0&&e.rows===1,m=w?1:r,E=w?r:s;return m>e.rows||E>e.cols?p.REF:new x(e.sheet,m===0?e.top:e.top+m-1,E===0?e.left:e.left+E-1,m===0?e.bottom:e.top+m-1,E===0?e.right:e.left+E-1)}const c=R(e,t);if(c instanceof a)return c;const f=n[2]===void 0&&c.rows===1,i=f?1:r,u=f?r:s===0&&c.cols===1?1:s;if(i>c.rows||u>c.cols)return p.REF;if(i>0&&u>0)return c.at(i-1,u-1);const l=i===0?c.rows:1,d=u===0?c.cols:1,h=[];for(let w=0;w<l;w++)for(let m=0;m<d;m++)h.push(c.at(i===0?w:i-1,u===0?m:u-1));return new k(l,d,h)}),A("OFFSET","rvv?vv",(n,t)=>{const e=n[0];if(e instanceof a)return e;if(!(e instanceof x))return p.VALUE;const o=(b,B)=>{if(b===void 0||b instanceof O)return B;const S=M(t.calc.scalar(b,t.frame));return S instanceof a?S:Math.trunc(S)},r=o(n[1],0),s=o(n[2],0),c=o(n[3],e.rows),f=o(n[4],e.cols);for(const b of[r,s,c,f])if(b instanceof a)return b;const[i,u,l,d]=[r,s,c,f];if(l===0||d===0)return p.REF;const h=e.top+(i??0),w=e.left+(u??0),m=h+(l??1)-1,E=w+(d??1)-1;return Math.min(h,m)<0||Math.min(w,E)<0||Math.max(h,m)>=L||Math.max(w,E)>=W?p.REF:new x(e.sheet,Math.min(h,m),Math.min(w,E),Math.max(h,m),Math.max(w,E))},{volatile:!0});const on=/^(?:(.*)!)?R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/iu,$=(n,t)=>n===void 0?t:n.startsWith("[")?t+Number(n.slice(1,-1)):Number(n)-1;function sn(n,t,e){const o=on.exec(n.trim());if(!o)return;const[,r,s,c,f,i]=o,u={row:$(s,t),col:$(c,e)};if(u.row<0||u.col<0)return;const l=K(u),d=r===void 0?"":`${r}!`;if(f===void 0&&i===void 0)return`${d}${l}`;const h={row:$(f,t),col:$(i,e)};return h.row<0||h.col<0?void 0:`${d}${l}:${K(h)}`}A("INDIRECT","v?v",(n,t)=>{const e=H(t.calc.scalar(n[0],t.frame));if(e instanceof a)return e;const o=n[1]===void 0?!0:U(t.calc.scalar(n[1],t.frame));if(o instanceof a)return o;const r=o?e:sn(e,t.frame.row,t.frame.col);if(r===void 0)return p.REF;const s=G(r);if(!s.ok)return p.REF;const{node:c}=s;if(c.kind!=="ref"&&c.kind!=="structured"&&c.kind!=="name"&&!(c.kind==="binary"&&c.op===":"))return p.REF;const f=t.calc.eval(c,{...t.frame,dr:0,dc:0});return f instanceof x||f instanceof F?f:p.REF},{volatile:!0});const j=(n,t,e)=>{if(n===void 0||n instanceof O)return t+1;if(n instanceof a)return n;if(!(n instanceof x))return p.VALUE;const[o,r]=e(n);if(r<=1)return o+1;const s=Array.from({length:r},(c,f)=>o+f+1);return e===z?new k(r,1,s):new k(1,r,s)},z=n=>[n.top,n.bottom===L-1?Math.min(n.rows,n.sheet.lastRow-n.top+1):n.rows],cn=n=>[n.left,n.cols];A("ROW","?r",(n,t)=>j(n[0],t.frame.row,z)),A("COLUMN","?r",(n,t)=>j(n[0],t.frame.col,cn)),A("ROWS","r",(n,t)=>{const e=n[0];if(e instanceof x)return e.rows;const o=R(e,t);return o instanceof a?o:o.rows}),A("COLUMNS","r",(n,t)=>{const e=n[0];if(e instanceof x)return e.cols;const o=R(e,t);return o instanceof a?o:o.cols}),A("AREAS","r",n=>{const t=n[0];return t instanceof x?1:t instanceof F?t.parts.length:p.VALUE}),Q("ADDRESS",2,5,(n,t,e,o,r)=>{const s=M(n),c=M(t),f=e==null?1:M(e);for(const b of[s,c,f])if(b instanceof a)return b;const[i,u,l]=[s,c,f];if((i??0)<1||(u??0)<1||(i??0)>L||(u??0)>W)return p.VALUE;const d=o==null?!0:U(o);if(d instanceof a)return d;const h=l===1||l===2,w=l===1||l===3,m=d?`${w?"$":""}${g((u??1)-1)}${h?"$":""}${String(i)}`:`R${h?String(i):`[${String(i)}]`}C${w?String(u):`[${String(u)}]`}`;if(r==null)return m;const E=H(r);return E instanceof a?E:`${q(E)}${m}`}),A("TRANSPOSE","r",(n,t)=>{const e=R(n[0],t);if(e instanceof a)return e;const o=[];for(let r=0;r<e.cols;r++)for(let s=0;s<e.rows;s++)o.push(e.at(s,r));return new k(e.cols,e.rows,o)}),J("SHEET",0,1,(n,t)=>{const e=n[0];if(e===void 0)return t.frame.sheet.index+1;const o=t.calc.eval(e,t.frame);if(o instanceof x)return o.sheet.index+1;const r=t.calc.scalar(o,t.frame);if(typeof r!="string")return p.NA;const s=t.calc.wb.sheet(r);return s?s.index+1:p.NA}),A("SHEETS","?r",(n,t)=>t.calc.wb.sheets.length);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{CellError as a,ERR as l,Matrix as R}from"../../values.js";import{toBool as _,toNumber as A}from"../coerce.js";import{Reference as Q,Union as st}from"../types.js";import{FUNCTIONS as ot,alias as O,cellsOf as v,eachRow as ct,number as U,numbersOf as S,raw as N,scalar as T}from"./define.js";import{besselI as it,besselJ as ft,besselK as at,besselY as ut,betaI as q,gammaQ as lt,normalCdf as ht}from"./special.js";const P=2**48;function K(e,r){const[t="0",n="0"]=e.toExponential().split("e");return+`${t}e${String(Number(n)+r)}`}const Mt=e=>Number(e.toPrecision(15));function C(e,r,t){const n=Math.trunc(r),s=Math.abs(Mt(e)),o=K(s,n);if(o>=2**53)return e;const c=K(t(o),-n);return e<0?-c:c}T("ROUND",2,2,U((e=0,r=0)=>C(e,r,t=>Math.floor(t+.5)))),T("ROUNDUP",2,2,U((e=0,r=0)=>C(e,r,Math.ceil))),T("ROUNDDOWN",2,2,U((e=0,r=0)=>C(e,r,Math.floor))),T("MOD",2,2,U((e=0,r=0)=>{if(r===0)return l.DIV0;const t=e%r;return t!==0&&t<0!=r<0?t+r:t})),T("QUOTIENT",2,2,U((e=0,r=0)=>r===0?l.DIV0:Math.trunc(e/r))),T("POWER",2,2,U((e=0,r=0)=>{if(e===0&&r===0)return l.NUM;if(e===0&&r<0)return l.DIV0;const t=e**r;return Number.isFinite(t)?t:l.NUM})),T("LOG",1,2,(e,r)=>{const t=A(e),n=r==null?10:A(r);return t instanceof a?t:n instanceof a?n:t<=0||n<=0?l.NUM:n===1?l.DIV0:n===10?Math.log10(t):n===2?Math.log2(t):Math.log(t)/Math.log(n)});function X(e,r){let[t,n]=[e,r];for(;n!==0;)[t,n]=[n,t%n];return t}function H(e,r){const t=S(e,r);if(t instanceof a)return t;const n=t.map(Math.trunc);return n.some(s=>s<0||s>=2**53)?l.NUM:n}N("GCD","(r)",(e,r)=>{const t=H(e,r);return t instanceof a?t:t.reduce(X,0)}),N("LCM","(r)",(e,r)=>{const t=H(e,r);if(t instanceof a)return t;if(t.includes(0))return 0;const n=t.reduce((s,o)=>s*o/X(s,o),1);return Number.isFinite(n)?n:l.NUM}),T("BASE",2,3,U((e=0,r=0,t=Number.NaN)=>{const n=Math.trunc(e),s=Math.trunc(r),o=Number.isNaN(t)?0:Math.trunc(t);return n<0||n>=2**53||s<2||s>36||o<0||o>255?l.NUM:n.toString(s).toUpperCase().padStart(o,"0")}));const mt="MDCLXVI",Y=[1e3,500,100,50,10,5,1];function Nt(e,r){const t=f=>Y[f]??1,n=f=>mt.charAt(f),s=Y.length-1;let o=e,c="";for(let f=0;f<=s/2;f++){const i=2*f,u=Math.floor(o/t(i));if(u%5===4){const h=u===4?i-1:i-2;let M=i,m=0;for(;m<r&&M<s;)m+=1,t(h)-t(M+1)<=o?M+=1:m=r;c+=n(M)+n(h),o+=t(M)-t(h)}else u>4&&(c+=n(i-1)),c+=n(i).repeat(u%5),o%=t(i)}return c}T("ROMAN",1,2,(e,r)=>{const t=A(e);if(t instanceof a)return t;let n=0;if(typeof r=="boolean")n=r?0:4;else if(r!=null){const o=A(r);if(o instanceof a)return o;n=Math.trunc(o)}const s=Math.trunc(t);return s<0||s>3999||n<0||n>4?l.VALUE:Nt(s,n)});function B(e){return!Number.isInteger(e)||e<0||e>=P?l.NUM:BigInt(e)}for(const[e,r]of[["BITAND",(t,n)=>t&n],["BITOR",(t,n)=>t|n],["BITXOR",(t,n)=>t^n]])T(e,2,2,U((t=0,n=0)=>{const s=B(t),o=B(n);return s instanceof a?s:o instanceof a?o:Number(r(s,o))}));for(const[e,r]of[["BITLSHIFT",1],["BITRSHIFT",-1]])T(e,2,2,U((t=0,n=0)=>{const s=B(t);if(s instanceof a)return s;const o=Math.trunc(n)*r;if(Math.abs(o)>53)return l.NUM;const c=o>=0?s<<BigInt(o):s>>BigInt(-o);return c>=BigInt(P)?l.NUM:Number(c)}));for(const[e,r,t]of[["BESSELJ",ft,!1],["BESSELI",it,!1],["BESSELY",ut,!0],["BESSELK",at,!0]])T(e,2,2,U((n=0,s=0)=>{const o=Math.trunc(s);if(o<0||t&&n<=0)return l.NUM;const c=r(o,n);return Number.isFinite(c)?c:l.NUM}));T("SLN",3,3,U((e=0,r=0,t=0)=>t===0?l.DIV0:(e-r)/t)),N("GEOMEAN","(r)",(e,r)=>{const t=S(e,r);return t instanceof a?t:t.length===0||t.some(n=>n<=0)?l.NUM:Math.exp(t.reduce((n,s)=>n+Math.log(s),0)/t.length)}),N("HARMEAN","(r)",(e,r)=>{const t=S(e,r);return t instanceof a?t:t.length===0||t.some(n=>n<=0)?l.NUM:t.length/t.reduce((n,s)=>n+1/s,0)}),N("KURT","(r)",(e,r)=>{const t=S(e,r);if(t instanceof a)return t;const n=t.length;if(n<4)return l.DIV0;const s=t.reduce((f,i)=>f+i,0)/n,o=t.reduce((f,i)=>f+(i-s)**2,0)/(n-1);if(o===0)return l.DIV0;const c=t.reduce((f,i)=>f+(i-s)**4,0)/o**2;return n*(n+1)/((n-1)*(n-2)*(n-3))*c-3*(n-1)**2/((n-2)*(n-3))});function z(e,r){const t=[];let n;return ct(e,r,(s,o)=>{if(!n){if(s instanceof a)n=s;else if(typeof s=="number")t.push(s);else if(o&&s!==null){const c=A(s);c instanceof a?n=c:t.push(c)}}}),n??t}N("MODE.SNGL","(r)",(e,r)=>{const t=z(e,r);if(t instanceof a)return t;const n=new Map;for(const c of t)n.set(c,(n.get(c)??0)+1);let s,o=1;for(const[c,f]of n)f>o&&(s=c,o=f);return s??l.NA}),O("MODE","MODE.SNGL"),N("MODE.MULT","(r)",(e,r)=>{const t=z(e,r);if(t instanceof a)return t;const n=new Map;for(const c of t)n.set(c,(n.get(c)??0)+1);let s=0;for(const c of n.values())s=Math.max(s,c);if(s<2)return l.NA;const o=[...n].filter(([,c])=>c===s).map(([c])=>c);return new R(o.length,1,o)});function dt(e,r){const t=r*(e.length-1),n=Math.floor(t),s=e[n]??0,o=e[n+1]??s;return s+(t-n)*(o-s)}N("QUARTILE.INC","rv",(e,r)=>{const t=S([e[0]],r);if(t instanceof a)return t;const n=A(r.calc.scalar(e[1],r.frame));if(n instanceof a)return n;const s=Math.trunc(n);return t.length===0||s<0||s>4?l.NUM:dt([...t].sort((o,c)=>o-c),s/4)}),O("QUARTILE","QUARTILE.INC");function W(e,r){const t=A(r.calc.scalar(e[0],r.frame));if(t instanceof a)return t;const n=S([e[1]],r);if(n instanceof a)return n;const s=e[2]===void 0?0:A(r.calc.scalar(e[2],r.frame));if(s instanceof a)return s;const o=n.filter(c=>c===t).length;return o===0?l.NA:{before:n.filter(c=>s===0?c>t:c<t).length,ties:o}}N("RANK.EQ","vr?v",(e,r)=>{const t=W(e,r);return t instanceof a?t:t.before+1}),N("RANK.AVG","vr?v",(e,r)=>{const t=W(e,r);return t instanceof a?t:t.before+(t.ties+1)/2}),O("RANK","RANK.EQ");function L(e,r){const t=v(e,r);if(t instanceof a)return t;const n=[];for(let s=0;s<t.rows;s++){const o=[];for(let c=0;c<t.cols;c++){const f=t.at(s,c);if(f instanceof a)return f;if(typeof f!="number")return l.VALUE;o.push(f)}n.push(o)}return n}const F=e=>new R(e.length,e[0]?.length??0,e.flat());function pt(e,r){const t=r[0]?.length??0;return e.map(n=>Array.from({length:t},(s,o)=>n.reduce((c,f,i)=>c+f*(r[i]?.[o]??0),0)))}N("MMULT","rr",(e,r)=>{const t=L(e[0],r),n=L(e[1],r);return t instanceof a?t:n instanceof a?n:(t[0]?.length??0)!==n.length?l.VALUE:F(pt(t,n))});function V(e){const r=e.length,t=e.map((o,c)=>[...o,...Array.from({length:r},(f,i)=>c===i?1:0)]),n=(o,c)=>t[o]?.[c]??0;let s=1;for(let o=0;o<r;o++){let c=o;for(let i=o+1;i<r;i++)Math.abs(n(i,o))>Math.abs(n(c,o))&&(c=i);if(n(c,o)===0)return{determinant:0};if(c!==o){const[i=[],u=[]]=[t[o],t[c]];t[o]=u,t[c]=i,s=-s}const f=n(o,o);s*=f,t[o]=(t[o]??[]).map(i=>i/f);for(let i=0;i<r;i++){if(i===o)continue;const u=n(i,o),h=t[o]??[];t[i]=(t[i]??[]).map((M,m)=>M-u*(h[m]??0))}}return{determinant:s,inverse:t.map(o=>o.slice(r))}}const Z=e=>e.length>0&&e.every(r=>r.length===e.length);N("MDETERM","r",(e,r)=>{const t=L(e[0],r);return t instanceof a?t:Z(t)?V(t).determinant:l.VALUE}),N("MINVERSE","r",(e,r)=>{const t=L(e[0],r);if(t instanceof a)return t;if(!Z(t))return l.VALUE;const{inverse:n}=V(t);return n?F(n):l.NUM}),T("MUNIT",1,1,U((e=0)=>{const r=Math.trunc(e);return r<1||r>73?l.VALUE:F(Array.from({length:r},(t,n)=>Array.from({length:r},(s,o)=>n===o?1:0)))}));function G(e,r,t){const n=v(e,t),s=v(r,t);if(n instanceof a)return n;if(s instanceof a)return s;if(n.rows*n.cols!==s.rows*s.cols)return l.NA;const o=[],c=[];for(let f=0;f<n.rows;f++)for(let i=0;i<n.cols;i++){const u=n.at(f,i),h=s.rows===n.rows?s.at(f,i):s.at(i,f);if(u instanceof a)return u;if(h instanceof a)return h;typeof u=="number"&&typeof h=="number"&&(o.push(u),c.push(h))}return{xs:o,ys:c}}for(const[e,r]of[["SUMX2MY2",(t,n)=>t*t-n*n],["SUMX2PY2",(t,n)=>t*t+n*n],["SUMXMY2",(t,n)=>(t-n)**2]])N(e,"rr",(t,n)=>{const s=G(t[0],t[1],n);return s instanceof a?s:s.xs.length===0?l.DIV0:s.xs.reduce((o,c,f)=>o+r(c,s.ys[f]??0),0)});N("SERIESSUM","vvvr",(e,r)=>{const[t,n,s]=e.slice(0,3).map(c=>A(r.calc.scalar(c,r.frame)));if(t instanceof a)return t;if(n instanceof a)return n;if(s instanceof a)return s;const o=S([e[3]],r);return o instanceof a?o:o.reduce((c,f,i)=>c+f*(t??0)**((n??0)+i*(s??0)),0)}),N("FREQUENCY","rr",(e,r)=>{const t=S([e[0]],r),n=S([e[1]],r);if(t instanceof a)return t;if(n instanceof a)return n;const s=n.map((c,f)=>({b:c,i:f})).sort((c,f)=>c.b-f.b),o=new Array(n.length+1).fill(0);for(const c of t){const f=s.find(u=>c<=u.b),i=f?f.i:n.length;o[i]=(o[i]??0)+1}return new R(o.length,1,o)}),N("PROB","rrv?v",(e,r)=>{const t=G(e[0],e[1],r);if(t instanceof a)return t;const n=A(r.calc.scalar(e[2],r.frame));if(n instanceof a)return n;const s=e[3]===void 0?n:A(r.calc.scalar(e[3],r.frame));if(s instanceof a)return s;const o=t.ys.reduce((c,f)=>c+f,0);return t.ys.some(c=>c<=0||c>1)||Math.abs(o-1)>1e-12?l.NUM:t.xs.reduce((c,f,i)=>f>=n&&f<=s?c+(t.ys[i]??0):c,0)});const J=e=>e.reduce((r,t)=>r+t,0);function $(e,r,t){const n=r.map(u=>t?[...u,1]:[...u]),s=n[0]?.length??0,o=Array.from({length:s},(u,h)=>Array.from({length:s},(M,m)=>n.reduce((E,g)=>E+(g[h]??0)*(g[m]??0),0))),{inverse:c}=V(o);if(!c)return;const f=Array.from({length:s},(u,h)=>n.reduce((M,m,E)=>M+(m[h]??0)*(e[E]??0),0)),i=c.map(u=>u.reduce((h,M,m)=>h+M*(f[m]??0),0));return t?i:[...i,0]}function j(e,r,t){const n=L(e[0],r);if(n instanceof a)return n;const s=n.length===1,o=n.flat();if(t&&o.some(h=>h<=0))return l.NUM;const c=t?o.map(Math.log):o,f=e[1];if(f===void 0||!x(f))return{ys:c,xs:o.map((h,M)=>[M+1]),byRow:s};const i=L(f,r);if(i instanceof a)return i;const u=i[0]?.length??0;return s?u!==c.length?l.REF:{ys:c,xs:Array.from({length:u},(h,M)=>i.map(m=>m[M]??0)),byRow:s}:i.length!==c.length?l.REF:{ys:c,xs:i,byRow:s}}const x=e=>e instanceof Q||e instanceof R||typeof e=="number",tt=(e,r)=>e===void 0||!x(e)&&typeof e!="boolean"?!0:_(r.calc.scalar(e,r.frame));function nt(e){return(r,t)=>{const n=j(r,t,e);if(n instanceof a)return n;const s=tt(r[3],t);if(s instanceof a)return s;const o=$(n.ys,n.xs,s);if(!o)return l.NUM;const c=r[2];let f=n.xs;if(c!==void 0&&x(c)){const u=L(c,t);if(u instanceof a)return u;const h=n.xs[0]?.length??1,M=u[0]?.length??0;f=n.byRow||h===1&&u.length===1?Array.from({length:M},(m,E)=>u.map(g=>g[E]??0)):u}const i=f.map(u=>{const h=u.reduce((M,m,E)=>M+m*(o[E]??0),0)+(o[u.length]??0);return e?Math.exp(h):h});return n.byRow?new R(1,i.length,i):new R(i.length,1,i)}}N("TREND","r?rrv",nt(!1)),N("GROWTH","r?rrv",nt(!0));function Et(e,r,t,n){const s=r.map(p=>n?[...p,1]:[...p]),o=s[0]?.length??0,{inverse:c}=V(Array.from({length:o},(p,d)=>Array.from({length:o},(b,D)=>s.reduce((rt,k)=>rt+(k[d]??0)*(k[D]??0),0))));if(!c)return;const f=r.map(p=>p.reduce((d,b,D)=>d+b*(t[D]??0),0)+(t[p.length]??0)),i=e.reduce((p,d,b)=>p+(d-(f[b]??0))**2,0),u=n?J(e)/e.length:0,h=e.reduce((p,d)=>p+(d-u)**2,0),M=h-i,m=e.length-o,E=m>0?i/m:0,g=c.map((p,d)=>Math.sqrt(Math.max(0,(p[d]??0)*E))),I=r[0]?.length??0;return{errors:g,r2:h===0?1:M/h,sey:Math.sqrt(E),f:m>0&&i>0?M/I/(i/m):0,df:m,ssreg:M,ssresid:i}}function et(e){return(r,t)=>{const n=j(r,t,e);if(n instanceof a)return n;const s=tt(r[2],t);if(s instanceof a)return s;const o=$(n.ys,n.xs,s);if(!o)return l.NUM;const f=[...o.slice(0,-1).reverse(),o[o.length-1]??0],i=e?f.map(Math.exp):f,u=r[3],h=u===void 0||!gt(u)?!1:_(t.calc.scalar(u,t.frame));if(h instanceof a)return h;if(!h)return new R(1,i.length,i);const M=Et(n.ys,n.xs,o,s);if(!M)return l.NUM;const m=i.length,E=I=>[...I,...new Array(m-I.length).fill(l.NA)],g=s?[...M.errors.slice(0,-1).reverse(),M.errors[M.errors.length-1]??0]:[...[...M.errors].reverse(),l.NA];return new R(5,m,[...i,...g,...E([M.r2,M.sey]),...E([M.f,M.df]),...E([M.ssreg,M.ssresid])])}}const gt=e=>x(e)||typeof e=="boolean";N("LINEST","r?rvv",et(!1)),N("LOGEST","r?rvv",et(!0));const y=e=>J(e)/e.length,w=e=>{const r=y(e);return e.reduce((t,n)=>t+(n-r)**2,0)/(e.length-1)};N("CHISQ.TEST","rr",(e,r)=>{const t=L(e[0],r),n=L(e[1],r);if(t instanceof a)return t;if(n instanceof a)return n;const s=t.length,o=t[0]?.length??0;if(n.length!==s||(n[0]?.length??0)!==o)return l.NA;if(s*o<2)return l.NA;let c=0;for(let i=0;i<s;i++)for(let u=0;u<o;u++){const h=n[i]?.[u]??0;if(h<0)return l.NUM;if(h===0)return l.DIV0;c+=((t[i]?.[u]??0)-h)**2/h}const f=s>1&&o>1?(s-1)*(o-1):s>1?s-1:o-1;return lt(f/2,c/2)}),O("CHITEST","CHISQ.TEST"),N("T.TEST","rrvv",(e,r)=>{const t=A(r.calc.scalar(e[2],r.frame)),n=A(r.calc.scalar(e[3],r.frame));if(t instanceof a)return t;if(n instanceof a)return n;const s=Math.trunc(t),o=Math.trunc(n);if(s!==1&&s!==2||o<1||o>3)return l.NUM;let c,f;if(o===1){const u=G(e[0],e[1],r);if(u instanceof a)return u;const h=u.xs.map((M,m)=>M-(u.ys[m]??0));if(h.length<2)return l.DIV0;c=y(h)/Math.sqrt(w(h)/h.length),f=h.length-1}else{const u=S([e[0]],r),h=S([e[1]],r);if(u instanceof a)return u;if(h instanceof a)return h;if(u.length<2||h.length<2)return l.DIV0;const[M,m,E,g]=[w(u),w(h),u.length,h.length],I=Math.abs(y(u)-y(h));if(o===2){f=E+g-2;const p=((E-1)*M+(g-1)*m)/f;c=I/Math.sqrt(p*(1/E+1/g))}else{const p=M/E,d=m/g;c=I/Math.sqrt(p+d),f=(p+d)**2/(p**2/(E-1)+d**2/(g-1))}}return Number.isFinite(c)?q(f/2,.5,f/(f+c*c))*s/2:l.DIV0}),O("TTEST","T.TEST"),N("F.TEST","rr",(e,r)=>{const t=S([e[0]],r),n=S([e[1]],r);if(t instanceof a)return t;if(n instanceof a)return n;if(t.length<2||n.length<2)return l.DIV0;const[s,o]=[w(t),w(n)];if(s===0||o===0)return l.DIV0;const c=s/o,[f,i]=[t.length-1,n.length-1],u=q(i/2,f/2,i/(f*c+i));return 2*Math.min(u,1-u)}),O("FTEST","F.TEST"),N("Z.TEST","rv?v",(e,r)=>{const t=S([e[0]],r);if(t instanceof a)return t;const n=A(r.calc.scalar(e[1],r.frame));if(n instanceof a)return n;if(t.length===0)return l.NA;const s=e[2]===void 0?t.length<2?l.DIV0:Math.sqrt(w(t)):A(r.calc.scalar(e[2],r.frame));return s instanceof a?s:s===0?l.DIV0:1-ht((y(t)-n)/(s/Math.sqrt(t.length)))}),O("ZTEST","Z.TEST");const At={1:"AVERAGE",2:"COUNT",3:"COUNTA",4:"MAX",5:"MIN",6:"PRODUCT",7:"STDEV.S",8:"STDEV.P",9:"SUM",10:"VAR.S",11:"VAR.P",12:"MEDIAN",13:"MODE.SNGL",14:"LARGE",15:"SMALL",16:"PERCENTILE.INC",17:"QUARTILE.INC",18:"PERCENTILE.EXC",19:"QUARTILE.EXC"};N("AGGREGATE","vv(r)",(e,r)=>{const t=A(r.calc.scalar(e[0],r.frame)),n=A(r.calc.scalar(e[1],r.frame));if(t instanceof a)return t;if(n instanceof a)return n;const s=At[Math.trunc(t)],o=s===void 0?void 0:ot.get(s);if(o===void 0)return l.VALUE;const c=![2,3,6,7].includes(Math.trunc(n)),f=[1,3,5,7].includes(Math.trunc(n)),i=m=>{const E=m instanceof st?m.parts:[m],g=[];for(const p of E)if(p instanceof Q){const d=p;for(let b=d.top;b<=Math.min(d.bottom,d.sheet.lastRow);b++)if(!(f&&(d.sheet.spec.rows?.[String(b+1)]?.hidden===!0||r.calc.filtered(d.sheet).has(b))))for(let D=d.left;D<=d.right;D++)g.push(r.calc.read(d.sheet,b,D))}else{const d=r.calc.flat(p);d instanceof R?g.push(...d.data):g.push(d)}const I=c?g:g.filter(p=>!(p instanceof a));return new R(I.length,1,I)},u=Math.trunc(t)>=14,h=e.slice(2),M=u?[i(h[0]),...h.slice(1)]:h.map(i);return r.calc.applyFunction(o,M,r.frame)}),T("FACT",1,1,U(e=>{if(e<0)return l.NUM;let r=1;for(let t=2;t<=Math.trunc(e);t++)r*=t;return Number.isFinite(r)?r:l.NUM})),T("FACTDOUBLE",1,1,U(e=>{if(e<0)return l.NUM;let r=1;for(let t=Math.trunc(e);t>1;t-=2)r*=t;return Number.isFinite(r)?r:l.NUM})),T("ATAN2",2,2,U((e,r)=>e===0&&r===0?l.DIV0:Math.atan2(r,e)));for(const[e,r]of[["LARGE",!0],["SMALL",!1]])N(e,"rv",(t,n)=>{const s=S([t[0]],n);if(s instanceof a)return s;const o=A(t[1]);if(o instanceof a)return o;const c=Math.ceil(o);return c<1||c>s.length?l.NUM:(s.sort((f,i)=>r?i-f:f-i),s[c-1]??l.NUM)});
|
|
2
|
+
import{CellError as a,ERR as l,Matrix as I}from"../../values.js";import{toBool as Q,toNumber as A}from"../coerce.js";import{Reference as C,Union as ot,pushAll as ct}from"../types.js";import{indexKeyOf as it}from"../indexes.js";import{FUNCTIONS as ft,alias as L,cellsOf as v,eachArg as at,eachRow as ut,number as U,numbersOf as S,raw as d,scalar as T}from"./define.js";import{besselI as lt,besselJ as ht,besselK as mt,besselY as Mt,betaI as q,gammaQ as dt,normalCdf as Nt}from"./special.js";const P=2**48;function K(e,r){const[t="0",n="0"]=e.toExponential().split("e");return+`${t}e${String(Number(n)+r)}`}const pt=e=>Number(e.toPrecision(15));function k(e,r,t){const n=Math.trunc(r),s=Math.abs(pt(e)),o=K(s,n);if(o>=2**53)return e;const c=K(t(o),-n);return e<0?-c:c}T("ROUND",2,2,U((e=0,r=0)=>k(e,r,t=>Math.floor(t+.5)))),T("ROUNDUP",2,2,U((e=0,r=0)=>k(e,r,Math.ceil))),T("ROUNDDOWN",2,2,U((e=0,r=0)=>k(e,r,Math.floor))),T("MOD",2,2,U((e=0,r=0)=>{if(r===0)return l.DIV0;const t=e%r;return t!==0&&t<0!=r<0?t+r:t})),T("QUOTIENT",2,2,U((e=0,r=0)=>r===0?l.DIV0:Math.trunc(e/r))),T("POWER",2,2,U((e=0,r=0)=>{if(e===0&&r===0)return l.NUM;if(e===0&&r<0)return l.DIV0;const t=e**r;return Number.isFinite(t)?t:l.NUM})),T("LOG",1,2,(e,r)=>{const t=A(e),n=r==null?10:A(r);return t instanceof a?t:n instanceof a?n:t<=0||n<=0?l.NUM:n===1?l.DIV0:n===10?Math.log10(t):n===2?Math.log2(t):Math.log(t)/Math.log(n)});function X(e,r){let[t,n]=[e,r];for(;n!==0;)[t,n]=[n,t%n];return t}function H(e,r){const t=S(e,r);if(t instanceof a)return t;const n=t.map(Math.trunc);return n.some(s=>s<0||s>=2**53)?l.NUM:n}d("GCD","(r)",(e,r)=>{const t=H(e,r);return t instanceof a?t:t.reduce(X,0)}),d("LCM","(r)",(e,r)=>{const t=H(e,r);if(t instanceof a)return t;if(t.includes(0))return 0;const n=t.reduce((s,o)=>s*o/X(s,o),1);return Number.isFinite(n)?n:l.NUM}),T("BASE",2,3,U((e=0,r=0,t=Number.NaN)=>{const n=Math.trunc(e),s=Math.trunc(r),o=Number.isNaN(t)?0:Math.trunc(t);return n<0||n>=2**53||s<2||s>36||o<0||o>255?l.NUM:n.toString(s).toUpperCase().padStart(o,"0")}));const gt="MDCLXVI",Y=[1e3,500,100,50,10,5,1];function Et(e,r){const t=f=>Y[f]??1,n=f=>gt.charAt(f),s=Y.length-1;let o=e,c="";for(let f=0;f<=s/2;f++){const i=2*f,u=Math.floor(o/t(i));if(u%5===4){const h=u===4?i-1:i-2;let m=i,M=0;for(;M<r&&m<s;)M+=1,t(h)-t(m+1)<=o?m+=1:M=r;c+=n(m)+n(h),o+=t(m)-t(h)}else u>4&&(c+=n(i-1)),c+=n(i).repeat(u%5),o%=t(i)}return c}T("ROMAN",1,2,(e,r)=>{const t=A(e);if(t instanceof a)return t;let n=0;if(typeof r=="boolean")n=r?0:4;else if(r!=null){const o=A(r);if(o instanceof a)return o;n=Math.trunc(o)}const s=Math.trunc(t);return s<0||s>3999||n<0||n>4?l.VALUE:Et(s,n)});function B(e){return!Number.isInteger(e)||e<0||e>=P?l.NUM:BigInt(e)}for(const[e,r]of[["BITAND",(t,n)=>t&n],["BITOR",(t,n)=>t|n],["BITXOR",(t,n)=>t^n]])T(e,2,2,U((t=0,n=0)=>{const s=B(t),o=B(n);return s instanceof a?s:o instanceof a?o:Number(r(s,o))}));for(const[e,r]of[["BITLSHIFT",1],["BITRSHIFT",-1]])T(e,2,2,U((t=0,n=0)=>{const s=B(t);if(s instanceof a)return s;const o=Math.trunc(n)*r;if(Math.abs(o)>53)return l.NUM;const c=o>=0?s<<BigInt(o):s>>BigInt(-o);return c>=BigInt(P)?l.NUM:Number(c)}));for(const[e,r,t]of[["BESSELJ",ht,!1],["BESSELI",lt,!1],["BESSELY",Mt,!0],["BESSELK",mt,!0]])T(e,2,2,U((n=0,s=0)=>{const o=Math.trunc(s);if(o<0||t&&n<=0)return l.NUM;const c=r(o,n);return Number.isFinite(c)?c:l.NUM}));T("SLN",3,3,U((e=0,r=0,t=0)=>t===0?l.DIV0:(e-r)/t)),d("GEOMEAN","(r)",(e,r)=>{const t=S(e,r);return t instanceof a?t:t.length===0||t.some(n=>n<=0)?l.NUM:Math.exp(t.reduce((n,s)=>n+Math.log(s),0)/t.length)}),d("HARMEAN","(r)",(e,r)=>{const t=S(e,r);return t instanceof a?t:t.length===0||t.some(n=>n<=0)?l.NUM:t.length/t.reduce((n,s)=>n+1/s,0)}),d("KURT","(r)",(e,r)=>{const t=S(e,r);if(t instanceof a)return t;const n=t.length;if(n<4)return l.DIV0;const s=t.reduce((f,i)=>f+i,0)/n,o=t.reduce((f,i)=>f+(i-s)**2,0)/(n-1);if(o===0)return l.DIV0;const c=t.reduce((f,i)=>f+(i-s)**4,0)/o**2;return n*(n+1)/((n-1)*(n-2)*(n-3))*c-3*(n-1)**2/((n-2)*(n-3))});function W(e,r){const t=[];let n;return ut(e,r,(s,o)=>{if(!n){if(s instanceof a)n=s;else if(typeof s=="number")t.push(s);else if(o&&s!==null){const c=A(s);c instanceof a?n=c:t.push(c)}}}),n??t}d("MODE.SNGL","(r)",(e,r)=>{const t=W(e,r);if(t instanceof a)return t;const n=new Map;for(const c of t)n.set(c,(n.get(c)??0)+1);let s,o=1;for(const[c,f]of n)f>o&&(s=c,o=f);return s??l.NA}),L("MODE","MODE.SNGL"),d("MODE.MULT","(r)",(e,r)=>{const t=W(e,r);if(t instanceof a)return t;const n=new Map;for(const c of t)n.set(c,(n.get(c)??0)+1);let s=0;for(const c of n.values())s=Math.max(s,c);if(s<2)return l.NA;const o=[...n].filter(([,c])=>c===s).map(([c])=>c);return new I(o.length,1,o)});function At(e,r){const t=r*(e.length-1),n=Math.floor(t),s=e[n]??0,o=e[n+1]??s;return s+(t-n)*(o-s)}d("QUARTILE.INC","rv",(e,r)=>{const t=S([e[0]],r);if(t instanceof a)return t;const n=A(r.calc.scalar(e[1],r.frame));if(n instanceof a)return n;const s=Math.trunc(n);return t.length===0||s<0||s>4?l.NUM:At([...t].sort((o,c)=>o-c),s/4)}),L("QUARTILE","QUARTILE.INC");function z(e,r){const t=A(r.calc.scalar(e[0],r.frame));if(t instanceof a)return t;const n=S([e[1]],r);if(n instanceof a)return n;const s=e[2]===void 0?0:A(r.calc.scalar(e[2],r.frame));if(s instanceof a)return s;const o=n.filter(c=>c===t).length;return o===0?l.NA:{before:n.filter(c=>s===0?c>t:c<t).length,ties:o}}d("RANK.EQ","vr?v",(e,r)=>{const t=z(e,r);return t instanceof a?t:t.before+1}),d("RANK.AVG","vr?v",(e,r)=>{const t=z(e,r);return t instanceof a?t:t.before+(t.ties+1)/2}),L("RANK","RANK.EQ");function w(e,r){const t=v(e,r);if(t instanceof a)return t;const n=[];for(let s=0;s<t.rows;s++){const o=[];for(let c=0;c<t.cols;c++){const f=t.at(s,c);if(f instanceof a)return f;if(typeof f!="number")return l.VALUE;o.push(f)}n.push(o)}return n}const F=e=>new I(e.length,e[0]?.length??0,e.flat());function Tt(e,r){const t=r[0]?.length??0;return e.map(n=>Array.from({length:t},(s,o)=>n.reduce((c,f,i)=>c+f*(r[i]?.[o]??0),0)))}d("MMULT","rr",(e,r)=>{const t=w(e[0],r),n=w(e[1],r);return t instanceof a?t:n instanceof a?n:(t[0]?.length??0)!==n.length?l.VALUE:F(Tt(t,n))});function x(e){const r=e.length,t=e.map((o,c)=>[...o,...Array.from({length:r},(f,i)=>c===i?1:0)]),n=(o,c)=>t[o]?.[c]??0;let s=1;for(let o=0;o<r;o++){let c=o;for(let i=o+1;i<r;i++)Math.abs(n(i,o))>Math.abs(n(c,o))&&(c=i);if(n(c,o)===0)return{determinant:0};if(c!==o){const[i=[],u=[]]=[t[o],t[c]];t[o]=u,t[c]=i,s=-s}const f=n(o,o);s*=f,t[o]=(t[o]??[]).map(i=>i/f);for(let i=0;i<r;i++){if(i===o)continue;const u=n(i,o),h=t[o]??[];t[i]=(t[i]??[]).map((m,M)=>m-u*(h[M]??0))}}return{determinant:s,inverse:t.map(o=>o.slice(r))}}const Z=e=>e.length>0&&e.every(r=>r.length===e.length);d("MDETERM","r",(e,r)=>{const t=w(e[0],r);return t instanceof a?t:Z(t)?x(t).determinant:l.VALUE}),d("MINVERSE","r",(e,r)=>{const t=w(e[0],r);if(t instanceof a)return t;if(!Z(t))return l.VALUE;const{inverse:n}=x(t);return n?F(n):l.NUM}),T("MUNIT",1,1,U((e=0)=>{const r=Math.trunc(e);return r<1||r>73?l.VALUE:F(Array.from({length:r},(t,n)=>Array.from({length:r},(s,o)=>n===o?1:0)))}));function G(e,r,t){const n=v(e,t),s=v(r,t);if(n instanceof a)return n;if(s instanceof a)return s;if(n.rows*n.cols!==s.rows*s.cols)return l.NA;const o=[],c=[];for(let f=0;f<n.rows;f++)for(let i=0;i<n.cols;i++){const u=n.at(f,i),h=s.rows===n.rows?s.at(f,i):s.at(i,f);if(u instanceof a)return u;if(h instanceof a)return h;typeof u=="number"&&typeof h=="number"&&(o.push(u),c.push(h))}return{xs:o,ys:c}}for(const[e,r]of[["SUMX2MY2",(t,n)=>t*t-n*n],["SUMX2PY2",(t,n)=>t*t+n*n],["SUMXMY2",(t,n)=>(t-n)**2]])d(e,"rr",(t,n)=>{const s=G(t[0],t[1],n);return s instanceof a?s:s.xs.length===0?l.DIV0:s.xs.reduce((o,c,f)=>o+r(c,s.ys[f]??0),0)});d("SERIESSUM","vvvr",(e,r)=>{const[t,n,s]=e.slice(0,3).map(c=>A(r.calc.scalar(c,r.frame)));if(t instanceof a)return t;if(n instanceof a)return n;if(s instanceof a)return s;const o=S([e[3]],r);return o instanceof a?o:o.reduce((c,f,i)=>c+f*(t??0)**((n??0)+i*(s??0)),0)}),d("FREQUENCY","rr",(e,r)=>{const t=S([e[0]],r);if(t instanceof a)return t;const n=[];at([e[1]],r,c=>{typeof c=="number"&&n.push(c)});const s=n.map((c,f)=>({b:c,i:f})).sort((c,f)=>c.b-f.b),o=new Array(n.length+1).fill(0);for(const c of t){let f=0,i=s.length;for(;f<i;){const h=f+i>>1;c<=(s[h]?.b??1/0)?i=h:f=h+1}const u=s[f]?.i??n.length;o[u]=(o[u]??0)+1}return new I(o.length,1,o)}),d("PROB","rrv?v",(e,r)=>{const t=G(e[0],e[1],r);if(t instanceof a)return t;const n=A(r.calc.scalar(e[2],r.frame));if(n instanceof a)return n;const s=e[3]===void 0?n:A(r.calc.scalar(e[3],r.frame));if(s instanceof a)return s;const o=t.ys.reduce((c,f)=>c+f,0);return t.ys.some(c=>c<=0||c>1)||Math.abs(o-1)>1e-12?l.NUM:t.xs.reduce((c,f,i)=>f>=n&&f<=s?c+(t.ys[i]??0):c,0)});const J=e=>e.reduce((r,t)=>r+t,0);function $(e,r,t){const n=r.map(u=>t?[...u,1]:[...u]),s=n[0]?.length??0,o=Array.from({length:s},(u,h)=>Array.from({length:s},(m,M)=>n.reduce((g,E)=>g+(E[h]??0)*(E[M]??0),0))),{inverse:c}=x(o);if(!c)return;const f=Array.from({length:s},(u,h)=>n.reduce((m,M,g)=>m+(M[h]??0)*(e[g]??0),0)),i=c.map(u=>u.reduce((h,m,M)=>h+m*(f[M]??0),0));return t?i:[...i,0]}function j(e,r,t){const n=w(e[0],r);if(n instanceof a)return n;const s=n.length===1,o=n.flat();if(t&&o.some(h=>h<=0))return l.NUM;const c=t?o.map(Math.log):o,f=e[1];if(f===void 0||!V(f))return{ys:c,xs:o.map((h,m)=>[m+1]),byRow:s};const i=w(f,r);if(i instanceof a)return i;const u=i[0]?.length??0;return s?u!==c.length?l.REF:{ys:c,xs:Array.from({length:u},(h,m)=>i.map(M=>M[m]??0)),byRow:s}:i.length!==c.length?l.REF:{ys:c,xs:i,byRow:s}}const V=e=>e instanceof C||e instanceof I||typeof e=="number",tt=(e,r)=>e===void 0||!V(e)&&typeof e!="boolean"?!0:Q(r.calc.scalar(e,r.frame));function nt(e){return(r,t)=>{const n=j(r,t,e);if(n instanceof a)return n;const s=tt(r[3],t);if(s instanceof a)return s;const o=$(n.ys,n.xs,s);if(!o)return l.NUM;const c=r[2];let f=n.xs;if(c!==void 0&&V(c)){const u=w(c,t);if(u instanceof a)return u;const h=n.xs[0]?.length??1,m=u[0]?.length??0;f=n.byRow||h===1&&u.length===1?Array.from({length:m},(M,g)=>u.map(E=>E[g]??0)):u}const i=f.map(u=>{const h=u.reduce((m,M,g)=>m+M*(o[g]??0),0)+(o[u.length]??0);return e?Math.exp(h):h});return n.byRow?new I(1,i.length,i):new I(i.length,1,i)}}d("TREND","r?rrv",nt(!1)),d("GROWTH","r?rrv",nt(!0));function Ut(e,r,t,n){const s=r.map(p=>n?[...p,1]:[...p]),o=s[0]?.length??0,{inverse:c}=x(Array.from({length:o},(p,N)=>Array.from({length:o},(R,y)=>s.reduce((st,_)=>st+(_[N]??0)*(_[y]??0),0))));if(!c)return;const f=r.map(p=>p.reduce((N,R,y)=>N+R*(t[y]??0),0)+(t[p.length]??0)),i=e.reduce((p,N,R)=>p+(N-(f[R]??0))**2,0),u=n?J(e)/e.length:0,h=e.reduce((p,N)=>p+(N-u)**2,0),m=h-i,M=e.length-o,g=M>0?i/M:0,E=c.map((p,N)=>Math.sqrt(Math.max(0,(p[N]??0)*g))),b=r[0]?.length??0;return{errors:E,r2:h===0?1:m/h,sey:Math.sqrt(g),f:M>0&&i>0?m/b/(i/M):0,df:M,ssreg:m,ssresid:i}}function et(e){return(r,t)=>{const n=j(r,t,e);if(n instanceof a)return n;const s=tt(r[2],t);if(s instanceof a)return s;const o=$(n.ys,n.xs,s);if(!o)return l.NUM;const f=[...o.slice(0,-1).reverse(),o[o.length-1]??0],i=e?f.map(Math.exp):f,u=r[3],h=u===void 0||!St(u)?!1:Q(t.calc.scalar(u,t.frame));if(h instanceof a)return h;if(!h)return new I(1,i.length,i);const m=Ut(n.ys,n.xs,o,s);if(!m)return l.NUM;const M=i.length,g=b=>[...b,...new Array(M-b.length).fill(l.NA)],E=s?[...m.errors.slice(0,-1).reverse(),m.errors[m.errors.length-1]??0]:[...[...m.errors].reverse(),l.NA];return new I(5,M,[...i,...E,...g([m.r2,m.sey]),...g([m.f,m.df]),...g([m.ssreg,m.ssresid])])}}const St=e=>V(e)||typeof e=="boolean";d("LINEST","r?rvv",et(!1)),d("LOGEST","r?rvv",et(!0));const D=e=>J(e)/e.length,O=e=>{const r=D(e);return e.reduce((t,n)=>t+(n-r)**2,0)/(e.length-1)};d("CHISQ.TEST","rr",(e,r)=>{const t=w(e[0],r),n=w(e[1],r);if(t instanceof a)return t;if(n instanceof a)return n;const s=t.length,o=t[0]?.length??0;if(n.length!==s||(n[0]?.length??0)!==o)return l.NA;if(s*o<2)return l.NA;let c=0;for(let i=0;i<s;i++)for(let u=0;u<o;u++){const h=n[i]?.[u]??0;if(h<0)return l.NUM;if(h===0)return l.DIV0;c+=((t[i]?.[u]??0)-h)**2/h}const f=s>1&&o>1?(s-1)*(o-1):s>1?s-1:o-1;return dt(f/2,c/2)}),L("CHITEST","CHISQ.TEST"),d("T.TEST","rrvv",(e,r)=>{const t=A(r.calc.scalar(e[2],r.frame)),n=A(r.calc.scalar(e[3],r.frame));if(t instanceof a)return t;if(n instanceof a)return n;const s=Math.trunc(t),o=Math.trunc(n);if(s!==1&&s!==2||o<1||o>3)return l.NUM;let c,f;if(o===1){const u=G(e[0],e[1],r);if(u instanceof a)return u;const h=u.xs.map((m,M)=>m-(u.ys[M]??0));if(h.length<2)return l.DIV0;c=D(h)/Math.sqrt(O(h)/h.length),f=h.length-1}else{const u=S([e[0]],r),h=S([e[1]],r);if(u instanceof a)return u;if(h instanceof a)return h;if(u.length<2||h.length<2)return l.DIV0;const[m,M,g,E]=[O(u),O(h),u.length,h.length],b=Math.abs(D(u)-D(h));if(o===2){f=g+E-2;const p=((g-1)*m+(E-1)*M)/f;c=b/Math.sqrt(p*(1/g+1/E))}else{const p=m/g,N=M/E;c=b/Math.sqrt(p+N),f=(p+N)**2/(p**2/(g-1)+N**2/(E-1))}}return Number.isFinite(c)?q(f/2,.5,f/(f+c*c))*s/2:l.DIV0}),L("TTEST","T.TEST"),d("F.TEST","rr",(e,r)=>{const t=S([e[0]],r),n=S([e[1]],r);if(t instanceof a)return t;if(n instanceof a)return n;if(t.length<2||n.length<2)return l.DIV0;const[s,o]=[O(t),O(n)];if(s===0||o===0)return l.DIV0;const c=s/o,[f,i]=[t.length-1,n.length-1],u=q(i/2,f/2,i/(f*c+i));return 2*Math.min(u,1-u)}),L("FTEST","F.TEST"),d("Z.TEST","rv?v",(e,r)=>{const t=S([e[0]],r);if(t instanceof a)return t;const n=A(r.calc.scalar(e[1],r.frame));if(n instanceof a)return n;if(t.length===0)return l.NA;const s=e[2]===void 0?t.length<2?l.DIV0:Math.sqrt(O(t)):A(r.calc.scalar(e[2],r.frame));return s instanceof a?s:s===0?l.DIV0:1-Nt((D(t)-n)/(s/Math.sqrt(t.length)))}),L("ZTEST","Z.TEST");const It={1:"AVERAGE",2:"COUNT",3:"COUNTA",4:"MAX",5:"MIN",6:"PRODUCT",7:"STDEV.S",8:"STDEV.P",9:"SUM",10:"VAR.S",11:"VAR.P",12:"MEDIAN",13:"MODE.SNGL",14:"LARGE",15:"SMALL",16:"PERCENTILE.INC",17:"QUARTILE.INC",18:"PERCENTILE.EXC",19:"QUARTILE.EXC"};d("AGGREGATE","vv(r)",(e,r)=>{const t=A(r.calc.scalar(e[0],r.frame)),n=A(r.calc.scalar(e[1],r.frame));if(t instanceof a)return t;if(n instanceof a)return n;const s=It[Math.trunc(t)],o=s===void 0?void 0:ft.get(s);if(o===void 0)return l.VALUE;const c=![2,3,6,7].includes(Math.trunc(n)),f=[1,3,5,7].includes(Math.trunc(n)),i=M=>{const g=M instanceof ot?M.parts:[M],E=[];for(const p of g)if(p instanceof C){const N=p;for(let R=N.top;R<=Math.min(N.bottom,N.sheet.lastRow);R++)if(!(f&&(N.sheet.spec.rows?.[String(R+1)]?.hidden===!0||r.calc.filtered(N.sheet).has(R))))for(let y=N.left;y<=N.right;y++)E.push(r.calc.read(N.sheet,R,y))}else{const N=r.calc.flat(p);N instanceof I?ct(E,N.data):E.push(N)}const b=c?E:E.filter(p=>!(p instanceof a));return new I(b.length,1,b)},u=Math.trunc(t)>=14,h=e.slice(2),m=u?[i(h[0]),...h.slice(1)]:h.map(i);return r.calc.applyFunction(o,m,r.frame)}),T("FACT",1,1,U(e=>{if(e<0)return l.NUM;let r=1;for(let t=2;t<=Math.trunc(e);t++)r*=t;return Number.isFinite(r)?r:l.NUM})),T("FACTDOUBLE",1,1,U(e=>{if(e<0)return l.NUM;let r=1;for(let t=Math.trunc(e);t>1;t-=2)r*=t;return Number.isFinite(r)?r:l.NUM})),T("ATAN2",2,2,U((e,r)=>e===0&&r===0?l.DIV0:Math.atan2(r,e)));const rt={largest:new WeakMap,smallest:new WeakMap};function bt(e,r,t){const n=()=>{const f=S([e],t);return f instanceof a?f:f.sort((i,u)=>r?u-i:i-u)};if(e instanceof C)return t.calc.indexes.ofRepeated(it(r?"large":"small",e),[e],()=>{const i=n();return new Map(i instanceof a?[]:[["",i]])})?.get("")??n();if(!(e instanceof I))return n();const s=r?rt.largest:rt.smallest,o=s.get(e);if(o)return o;const c=n();return c instanceof a||s.set(e,o===null?c:null),c}for(const[e,r]of[["LARGE",!0],["SMALL",!1]])d(e,"rv",(t,n)=>{const s=bt(t[0],r,n);if(s instanceof a)return s;const o=A(t[1]);if(o instanceof a)return o;const c=Math.ceil(o);return c<1||c>s.length?l.NUM:s[c-1]??l.NUM});
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{format as $,isValidFormat as D}from"numfmt";import{CellError as i,ERR as
|
|
2
|
+
import{format as $,isValidFormat as D}from"numfmt";import{CellError as i,ERR as d}from"../../values.js";import{numberOfText as N,toBool as b,toNumber as p,toText as u}from"../coerce.js";import{Missing as F}from"../types.js";import{alias as H,cellsOf as S,eachRow as O,many as B,number as x,numberArg as V,raw as M,scalar as f}from"./define.js";import{literalOf as X,wildcard as j}from"./criteria.js";import{delimiters as k,eachLetter as G,lowerLetters as _,occurrences as J}from"./delimiters.js";const g=n=>r=>{const t=u(r);return t instanceof i?t:n(t)},T=(n,r)=>{if(n===void 0)return r;const t=p(n);return t instanceof i?t:t<0?d.VALUE:Math.trunc(t)},I=n=>G(n,r=>r.toUpperCase()),y=_;f("LEFT",1,2,(n,r)=>{const t=u(n),e=T(r,1);return t instanceof i?t:e instanceof i?e:t.slice(0,e)}),f("RIGHT",1,2,(n,r)=>{const t=u(n),e=T(r,1);return t instanceof i?t:e instanceof i?e:e===0?"":t.slice(-e)}),f("MID",3,3,(n,r,t)=>{const e=u(n),s=p(r),o=T(t,0);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(s<1)return d.VALUE;const c=Math.trunc(s)-1;return e.slice(c,c+o)}),f("LEN",1,1,g(n=>n.length)),f("LOWER",1,1,g(y)),f("UPPER",1,1,g(I)),f("PROPER",1,1,g(n=>y(n).replace(/(^|[^\p{L}])(\p{L})/gu,(r,t,e)=>`${t}${I(e)}`))),f("TRIM",1,1,g(n=>n.replace(/ +/gu," ").replace(/^ | $/gu,""))),f("CLEAN",1,1,g(n=>n.replace(/[\u0000-\u001f]/gu,""))),f("T",1,1,n=>typeof n=="string"?n:""),f("EXACT",2,2,(n,r)=>{const t=u(n),e=u(r);return t instanceof i?t:e instanceof i?e:t===e}),f("REPT",2,2,(n,r)=>{const t=u(n),e=T(r,0);return t instanceof i?t:e instanceof i?e:t.length*e>32767?d.VALUE:t.repeat(e)}),f("CHAR",1,1,x(n=>{const r=Math.trunc(n);return r<1||r>255?d.VALUE:W(r)})),f("UNICHAR",1,1,x(n=>{const r=Math.trunc(n);return r<1||r>1114111?d.VALUE:String.fromCodePoint(r)})),f("CODE",1,1,g(n=>n===""?d.VALUE:n.charCodeAt(0))),f("UNICODE",1,1,g(n=>n.codePointAt(0)??d.VALUE));const K="€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ";function W(n){return n>=128&&n<160?K.charAt(n-128):String.fromCharCode(n)}M("CONCAT","(r)",(n,r)=>{let t="",e;return O(n,r,s=>{if(e)return;const o=u(s);o instanceof i?e=o:t+=o}),e??t}),f("CONCATENATE",1,B,(...n)=>{let r="";for(const t of n){const e=u(t);if(e instanceof i)return e;r+=e}return r}),M("TEXTJOIN","rv(r)",(n,r)=>{const[t,e,...s]=n,o=S(t??"",r);if(o instanceof i)return o;const c=[];for(let h=0;h<o.rows;h++)for(let A=0;A<o.cols;A++){const m=u(o.at(h,A));if(m instanceof i)return m;c.push(m)}const l=b(r.calc.scalar(e??!0,r.frame));if(l instanceof i)return l;const E=[];let a;if(O(s,r,h=>{if(a)return;const A=u(h);A instanceof i?a=A:(!l||A!=="")&&E.push(A)}),a)return a;let L=E[0]??"";for(let h=1;h<E.length;h++)L+=`${c.length>0?c[(h-1)%c.length]??"":""}${E[h]??""}`;return L}),f("SUBSTITUTE",3,4,(n,r,t,e)=>{const s=u(n),o=u(r),c=u(t);if(s instanceof i)return s;if(o instanceof i)return o;if(c instanceof i)return c;if(o==="")return s;if(e===void 0)return s.split(o).join(c);const l=p(e);if(l instanceof i)return l;if(l<1)return d.VALUE;let E=-1;for(let a=0;a<Math.trunc(l);a++)if(E=s.indexOf(o,E+1),E<0)return s;return`${s.slice(0,E)}${c}${s.slice(E+o.length)}`}),f("REPLACE",4,4,(n,r,t,e)=>{const s=u(n),o=p(r),c=T(t,0),l=u(e);if(s instanceof i)return s;if(o instanceof i)return o;if(c instanceof i)return c;if(l instanceof i)return l;if(o<1)return d.VALUE;const E=Math.trunc(o)-1;return`${s.slice(0,E)}${l}${s.slice(E+c)}`}),f("FIND",2,3,(n,r,t)=>{const e=u(n),s=u(r),o=t===void 0?1:p(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(o<1||o>s.length+1)return d.VALUE;const c=s.indexOf(e,Math.trunc(o)-1);return c<0?d.VALUE:c+1}),f("SEARCH",2,3,(n,r,t)=>{const e=u(n),s=u(r),o=t===void 0?1:p(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(o<1||o>s.length+1)return d.VALUE;const c=Math.trunc(o)-1,l=X(e);if(l!==void 0){const a=s.toLowerCase(),L=l.toLowerCase();if(a.length===s.length&&L.length===l.length){const h=a.indexOf(L,c);return h<0?d.VALUE:h+1}}const E=j(`${e}*`);for(let a=c;a<=s.length;a++)if(E(s.slice(a)))return a+1;return d.VALUE}),f("VALUE",1,1,n=>typeof n=="number"?n:n==null?0:typeof n!="string"?d.VALUE:N(n)??d.VALUE),f("NUMBERVALUE",1,3,(n,r,t)=>{const e=u(n),s=u(r??"."),o=u(t??",");if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;const c=e.replace(/\s/gu,"").split(o).join("").replace(s,".");if(c==="")return 0;const l=/%*$/u.exec(c)?.[0].length??0,E=Number(c.slice(0,c.length-l));return Number.isNaN(E)?d.VALUE:E/100**l}),f("TEXT",2,2,(n,r)=>{const t=u(r);if(t instanceof i)return t;if(!D(t))return d.VALUE;if(typeof n=="boolean")return n?"TRUE":"FALSE";const e=typeof n=="string"?N(n)??n:n??0;if(e instanceof i)return e;try{return $(t,e)}catch{return d.VALUE}});function w(n,r,t){const e=t?"#,##0":"0",s=r>0?`.${"0".repeat(r)}`:"";return $(`${e}${s}`,n)}const v=(n,r)=>{const t=10**r;return Math.sign(n)*Math.round(Math.abs(n)*t+1e-9)/t};f("FIXED",1,3,(n,r,t)=>{const e=p(n),s=r==null?2:p(r),o=t===void 0?!1:b(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;const c=Math.trunc(s);return w(v(e,c),Math.max(0,c),!o)}),f("DOLLAR",1,2,(n,r)=>{const t=p(n),e=r==null?2:p(r);if(t instanceof i)return t;if(e instanceof i)return e;const s=Math.trunc(e),o=w(Math.abs(v(t,s)),Math.max(0,s),!0);return t<0?`($${o})`:`$${o}`});function Y(n,r,t,e,s){const o=J(n,r,e);return t>0?(s&&o.push({at:n.length,length:0}),o[t-1]):(s&&o.unshift({at:0,length:0}),o[o.length+t])}function P(n){return(r,t)=>{const[e,s,o,c,l,E]=r.map(U=>U instanceof F?void 0:U),a=u(e??null),L=k(s,t),h=V(o,1),A=V(c,0),m=V(l,0);if(a instanceof i)return a;if(L instanceof i)return L;if(h instanceof i)return h;if(A instanceof i)return A;if(m instanceof i)return m;const C=Math.trunc(h);if(C===0||Math.abs(C)>Math.max(a.length,1))return d.VALUE;if(L.length>0&&L.every(U=>U===""))return C>0===(n==="before")?"":a;const R=Y(a,L,C,A===1,m===1);return R?n==="before"?a.slice(0,R.at):a.slice(R.at+R.length):E===void 0?d.NA:E}}M("TEXTBEFORE","vr?vvvv",P("before")),M("TEXTAFTER","vr?vvvv",P("after")),f("HYPERLINK",1,2,(n,r)=>r===void 0?n??null:r),f("ENCODEURL",1,1,g(n=>encodeURIComponent(n)));for(const n of["LEN","LEFT","RIGHT","MID","FIND","SEARCH","REPLACE"])H(`${n}B`,n);
|
|
@@ -6,6 +6,15 @@ export declare class RangeCache<T> {
|
|
|
6
6
|
private readonly kept;
|
|
7
7
|
/** What `key` names, worked out over the ranges `over` read, once while they stay as they are. */
|
|
8
8
|
of(key: string, over: readonly Reference[], build: () => T): T;
|
|
9
|
+
/** Keys asked for once, whose value was not worked out. */
|
|
10
|
+
private readonly asked;
|
|
11
|
+
/**
|
|
12
|
+
* What `key` names, worked out the second time it is asked for: the
|
|
13
|
+
* first time, nothing, and the caller reads the range itself. Ranges
|
|
14
|
+
* asked for once each (COUNTIF($B$2:B2,B2) filled down 13,000 rows, a
|
|
15
|
+
* new range a row) would keep an index each, past memory.
|
|
16
|
+
*/
|
|
17
|
+
ofRepeated(key: string, over: readonly Reference[], build: () => T): T | undefined;
|
|
9
18
|
/** What `key` names, if it is kept. */
|
|
10
19
|
get(key: string): T | undefined;
|
|
11
20
|
/** Keeps what `key` names, worked out over the ranges `over` read. */
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{overlaps as
|
|
2
|
+
import{overlaps as n}from"../address.js";const h=s=>({top:s.top,left:s.left,bottom:s.bottom,right:s.right});export class RangeCache{kept=new Map;of(e,a,o){const t=this.kept.get(e);if(t)return t.value;const r=o();return this.kept.set(e,{over:a.map(i=>({sheet:i.sheet,area:h(i)})),value:r}),r}asked=new Set;ofRepeated(e,a,o){const t=this.kept.get(e);if(t)return t.value;if(!this.asked.has(e)){this.asked.add(e);return}return this.asked.delete(e),this.of(e,a,o)}get(e){return this.kept.get(e)?.value}set(e,a,o){this.kept.set(e,{over:a.map(t=>({sheet:t.sheet,area:h(t)})),value:o})}changed(e,a){if(this.kept.size!==0)for(const[o,t]of this.kept)t.over.some(r=>r.sheet===e&&n(r.area,a))&&this.kept.delete(o)}clear(){this.kept.clear(),this.asked.clear()}}export const indexKeyOf=(s,e)=>`${s}:${e.key}`;
|