@allyhub/sdk 0.16.0 → 0.17.1

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/node_modules/@allyhub/audio/dist/index.d.ts +1 -0
  3. package/dist/node_modules/@allyhub/audio/dist/index.js +1 -1
  4. package/dist/node_modules/@allyhub/audio/dist/place.d.ts +26 -0
  5. package/dist/node_modules/@allyhub/audio/dist/place.js +2 -0
  6. package/dist/node_modules/@allyhub/audio/package.json +1 -1
  7. package/dist/node_modules/@allyhub/chart/package.json +1 -1
  8. package/dist/node_modules/@allyhub/diagnostics/package.json +1 -1
  9. package/dist/node_modules/@allyhub/formats/dist/data/slides.d.ts +15 -3
  10. package/dist/node_modules/@allyhub/formats/dist/data/slides.js +1 -1
  11. package/dist/node_modules/@allyhub/formats/dist/items.d.ts +40 -7
  12. package/dist/node_modules/@allyhub/formats/dist/items.js +1 -1
  13. package/dist/node_modules/@allyhub/formats/dist/schemas.d.ts +4 -0
  14. package/dist/node_modules/@allyhub/formats/package.json +1 -1
  15. package/dist/node_modules/@allyhub/image-draw/dist/stage.js +1 -1
  16. package/dist/node_modules/@allyhub/image-draw/package.json +1 -1
  17. package/dist/node_modules/@allyhub/image-measure/package.json +1 -1
  18. package/dist/node_modules/@allyhub/image-sdk/package.json +1 -1
  19. package/dist/node_modules/@allyhub/measure/package.json +1 -1
  20. package/dist/node_modules/@allyhub/ooxml/package.json +1 -1
  21. package/dist/node_modules/@allyhub/piece/dist/layer.d.ts +15 -0
  22. package/dist/node_modules/@allyhub/piece/dist/page/index.d.ts +1 -0
  23. package/dist/node_modules/@allyhub/piece/dist/page/index.js +1 -1
  24. package/dist/node_modules/@allyhub/piece/dist/page/runtimes.d.ts +9 -0
  25. package/dist/node_modules/@allyhub/piece/dist/page/runtimes.js +2 -0
  26. package/dist/node_modules/@allyhub/piece/dist/source.d.ts +51 -10
  27. package/dist/node_modules/@allyhub/piece/dist/source.js +1 -1
  28. package/dist/node_modules/@allyhub/piece/package.json +1 -1
  29. package/dist/node_modules/@allyhub/protocol/dist/account.d.ts +0 -6
  30. package/dist/node_modules/@allyhub/protocol/dist/account.js +1 -1
  31. package/dist/node_modules/@allyhub/protocol/dist/events.d.ts +39 -0
  32. package/dist/node_modules/@allyhub/protocol/dist/events.js +1 -1
  33. package/dist/node_modules/@allyhub/protocol/dist/frame.d.ts +3 -0
  34. package/dist/node_modules/@allyhub/protocol/dist/frame.js +1 -1
  35. package/dist/node_modules/@allyhub/protocol/dist/kinds.d.ts +30 -0
  36. package/dist/node_modules/@allyhub/protocol/dist/kinds.js +1 -1
  37. package/dist/node_modules/@allyhub/protocol/dist/tools.d.ts +6 -0
  38. package/dist/node_modules/@allyhub/protocol/dist/tools.js +1 -1
  39. package/dist/node_modules/@allyhub/protocol/package.json +1 -1
  40. package/dist/node_modules/@allyhub/runtimes/package.json +1 -1
  41. package/dist/node_modules/@allyhub/scenery/package.json +1 -1
  42. package/dist/node_modules/@allyhub/sheet/dist/calc/coerce.js +1 -1
  43. package/dist/node_modules/@allyhub/sheet/dist/calc/engine.d.ts +12 -0
  44. package/dist/node_modules/@allyhub/sheet/dist/calc/engine.js +1 -1
  45. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.js +1 -1
  46. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/date.js +1 -1
  47. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/dynamic.js +1 -1
  48. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/info.js +1 -1
  49. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/logical.js +1 -1
  50. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/lookup.js +1 -1
  51. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/text.js +1 -1
  52. package/dist/node_modules/@allyhub/sheet/dist/formula/parse.js +1 -1
  53. package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.d.ts +1 -1
  54. package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.js +1 -1
  55. package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.js +1 -1
  56. package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.js +2 -2
  57. package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.js +1 -1
  58. package/dist/node_modules/@allyhub/sheet/package.json +1 -1
  59. package/dist/node_modules/@allyhub/slides-core/dist/animation.d.ts +10 -0
  60. package/dist/node_modules/@allyhub/slides-core/dist/animation.js +1 -1
  61. package/dist/node_modules/@allyhub/slides-core/dist/api.js +1 -1
  62. package/dist/node_modules/@allyhub/slides-core/dist/components.js +1 -1
  63. package/dist/node_modules/@allyhub/slides-core/dist/model.d.ts +3 -1
  64. package/dist/node_modules/@allyhub/slides-core/dist/resolve.d.ts +5 -0
  65. package/dist/node_modules/@allyhub/slides-core/dist/resolve.js +1 -1
  66. package/dist/node_modules/@allyhub/slides-core/package.json +1 -1
  67. package/dist/node_modules/@allyhub/video-core/package.json +1 -1
  68. package/dist/node_modules/@allyhub/video-sdk/dist/free.d.ts +1 -1
  69. package/dist/node_modules/@allyhub/video-sdk/package.json +1 -1
  70. package/docs/docs.md +11 -0
  71. package/docs/layers.md +2 -0
  72. package/docs/slides.md +1 -1
  73. 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 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)}
2
+ import{MAX_COLUMNS as C,MAX_ROWS as L,overlaps as B}from"../address.js";import{CellError as b,ERR as h,Matrix as w,serialOfDate as H}from"../values.js";import{filteredRows as $}from"../filter.js";import{FormulaCell as k,SharedFormula as j}from"../workbook.js";import{analyse as K}from"./analyse.js";import{Graph as X}from"./graph.js";import{fromWire as Y,matrixFromWire as Z,matrixToWire as J,toWire as Q}from"./remote.js";import{RangeCache as z}from"./indexes.js";import{binary as F,mapBoth as tt,toNumber as et}from"./coerce.js";import{FUNCTIONS as U}from"./functions/index.js";import{fillsGroup as st,paramAt as nt}from"./functions/define.js";import{Lambda as E,MISSING as D,Missing as O,Reference as p,Union as A,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 ot=new Set,it=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,G=2e3,rt=50,at=1024;class W extends Error{cell;constructor(t){super("computed first"),this.cell=t}}const P=12;export const ITERATION_DEFAULTS={max_iterations:100,max_change:.001};function ct(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 lt=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),ht=(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;waiting=new Set;work=[];unwound=new Map;counted=new Set;looping=new Set;tablesOf;inTables=!1;loop=new Set;around=new Set;namesAt=new Set;graph=new X({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 z;results=new z;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=()=>H(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)??ot}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=$(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>it.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.waiting.has(o)?this.computeNow(o):o.state!==0&&this.circular(o),o.value)}ghost(t,e,s){const o=t.spillAt(e,s);if(!o)return null;if(o.state===1&&!this.waiting.has(o))this.computeNow(o);else if(o.state!==0)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);let s=e>=0?this.stack.slice(e):[t],o=!1;if(e<0&&this.waiting.has(t)){s=[];for(let n=this.work.lastIndexOf(t);n<this.work.length-1;n++){const i=this.work[n];if(i!==void 0){if(this.counted.has(i)){o=!0;continue}this.counted.add(i),s.push(...this.unwound.get(i)??[i])}}s.push(...this.stack)}if(this.iteration()){for(const n of s)this.looping.add(n);return}!o&&!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: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 p&&r.single?r:void 0},o=t.map(n=>{const i=n.shared.node;if(i?.kind!=="call")return h.REF;const r=T(n),a=s(i.args[0],r);if(!a)return h.REF;const c=[];for(let u=1;u+1<i.args.length;u+=2){const f=s(i.args[u],r),g=s(i.args[u+1],r);if(!f||!g)return h.REF;const S=`${String(f.sheet.index)}:${String(f.top)}:${String(f.left)}`;e.has(S)||e.set(S,{sheet:f.sheet,row:f.top,col:f.left,slot:f.sheet.at(f.top,f.left)});const x=this.read(g.sheet,g.top,g.left);c.push({sheet:f.sheet,row:f.top,col:f.left,slot:x??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=d=>{if(d===void 0||d.kind==="missing")return null;const m=this.eval(d,a);return m instanceof p&&m.single?m:void 0},u=c(o.args[0]),f=c(o.args[1]),g=d=>{for(const m of e)m.value=d,this.reports&&this.settled.add(m)};if(u===void 0||f===void 0||u===null&&f===null||i===0||r===0){g(h.REF);return}const x=[u,f].filter(d=>d!==null).map(d=>({sheet:d.sheet,row:d.top,col:d.left,slot:d.sheet.at(d.top,d.left)})),y=d=>{const m=[];u&&m.push({sheet:u.sheet,row:u.top,col:u.left,slot:this.read(n,i-1,d.col)??void 0}),f&&m.push({sheet:f.sheet,row:f.top,col:f.left,slot:this.read(n,d.row,r-1)??void 0}),this.change(m)},N=d=>{const[m,v]=u&&f?[i-1,r-1]:f?[i-1,d.col]:[d.row,r-1];return this.read(n,m,v)},_=d=>u&&f?void 0:f?d.row:d.col,q=e.map((d,m)=>({cell:d,i:m})).sort((d,m)=>f&&!u?d.cell.row-m.cell.row||d.cell.col-m.cell.col:d.cell.col-m.cell.col||d.cell.row-m.cell.row),I=[];let V;for(const{cell:d,i:m}of q){const v=_(d);(v===void 0||v!==V)&&(y(d),V=v),I[m]=N(d)}this.change(x),e.forEach((d,m)=>{d.value=I[m]??null,this.reports&&this.settled.add(d)})}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.computeNow(r);if(o+=1,n=0,s.forEach((r,a)=>{n=Math.max(n,ct(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>=P*2)break;if(t>=P)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>=L||i.right>=C||s.holds(i,t)||s.tables.some(c=>B(c.area,i))||s.merges.some(c=>B(c,i))||r.some(c=>ht(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??=K(this.wb,t)}readsOf(t){const e=[],s=T(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 p?e.push(n):n instanceof A&&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 p?i.push(a):a instanceof A?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 p?s.push(i):i instanceof A&&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 u=a;u<=c;u++){const f=n.at(u,r);f instanceof k&&s.push(f)}}}return s}computeNow(t){if(this.stack.length>0){if(this.stack.length>=rt&&this.stack.length<G)throw new W(t);this.compute(t);return}const e=[t],s=new Set([t]);this.work=e;try{for(;e.length>0;){const o=e[e.length-1];if(o===void 0)break;if(this.waiting.delete(o),o.state!==1){e.pop();continue}try{this.compute(o),e.pop()}catch(n){if(!(n instanceof W))throw n;this.unwound.set(o,[...this.stack]),this.counted.delete(o);for(const i of this.stack)i.state=1;if(this.stack.length=0,this.waiting.add(o),s.has(n.cell)){n.cell.value=h.CALC,n.cell.state=0;continue}s.add(n.cell),e.push(n.cell)}}}finally{this.waiting.clear(),this.unwound.clear(),this.counted.clear(),this.work=[]}}compute(t){if(this.stack.length>=G){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,T(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 w){const r=ut(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;(!lt(n,t.array)||!ft(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 p?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof A?h.VALUE:t instanceof E?h.CALC:t instanceof O?null:t}matrix(t){const e=t.bottom===L-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===C-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 w(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=C-1,i=0,r=L-1;if(t.col){const a=this.bound(t.col,e.dc,C),c=this.bound(t.col2??t.col,e.dc,C);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,L),c=this.bound(t.row2??t.row,e.dr,L);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,u]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new A(this.wb.sheets.slice(c,u+1).map(f=>new p(f,i,o,r,n)))}return new p(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?dt(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 u=c.names.get(o);if(u!==void 0)return u}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:u,bottom:f,right:g}=r.data;return new p(r.sheet,c,u,f,g)}const a=U.get(t.toUpperCase());return a&&a.kind!=="lazy"?pt(a,t,e):h.NAME}intersect(t,e){return!(t instanceof p)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new p(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new p(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof w?new w(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 w||n instanceof w){const i=(r,a)=>F(t,r,a);return tt(o instanceof w?o:new w(1,1,[o]),n instanceof w?n:new w(1,1,[n]),i)}return F(t,o,n)}rangeOf(t,e){return t instanceof b?t:e instanceof b?e:!(t instanceof p)||!(e instanceof p)||t.sheet!==e.sheet?h.REF:new p(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 p)||!(e instanceof p))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 p(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 p?[n]:n instanceof A?n.parts:[],o=[...s(t),...s(e)];return o.length===0?h.VALUE:new A(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 D;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=>F("*",o,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,o=>F("/",o,100));if(s instanceof p&&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 p(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 w(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(o=>{const n=this.eval(o,e);return n instanceof w||n instanceof p||n instanceof A||n instanceof E||n instanceof O?h.VALUE:n})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof E?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 i=U.get(t.named);return!i||i.kind==="lazy"?h.NAME:e.length<i.min||e.length>i.max?h.VALUE:this.applyFunction(i,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=t.params.length+1;if(this.lambdaSlots+s>at)return h.NUM;const o=new Map;t.params.forEach((i,r)=>{o.set(i,e[r]??D)}),this.lambdaSlots+=s;const n=this.stack.length;try{return this.eval(t.body,{...t.frame,scope:{names:o,...t.frame.scope?{parent:t.frame.scope}:{}}})}catch(i){if(!(i instanceof RangeError)||!i.message.includes("call stack"))throw i;for(const r of this.stack.splice(n))r.state=1;return h.NUM}finally{this.lambdaSlots-=s}}call(t,e,s){const o=U.get(t);if(!o){const n=this.nameValue(t,s);return n instanceof E?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 st(t.params,e.length)?this.over(e,i=>nt(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 O?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[u,f]of t.entries()){if(!e(u)||f instanceof O){o.push(f);continue}const g=this.flat(f);g instanceof w&&n.set(u,g),o.push(g)}if(n.size===0)return s(o);const i=[...n.values()],r=Math.max(...i.map(u=>u.rows)),a=Math.max(...i.map(u=>u.cols));if(r===0||a===0)return h.CALC;const c=new Array(r*a);for(let u=0;u<r;u++)for(let f=0;f<a;f++){const g=o.map((S,x)=>{const y=n.get(x);if(!y)return S;const N=y.rows===1?0:u,_=y.cols===1?0:f;return N<y.rows&&_<y.cols?y.at(N,_):h.NA});c[u*a+f]=this.element(s(g))}return new w(r,a,c)}element(t){const e=this.flat(t);return e instanceof w?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof O)return null;if(t instanceof p){const s=this.intersect(t,e);return s instanceof p?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof w?t.at(0,0):t instanceof A||t instanceof E?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 j(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 u=n.spillAt(i,r);u&&s.push(u),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)||this.inTables&&R(n.shared.node)||(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:Q(e.value),...e.array?{array:J(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=Y(s.value),n.array=s.array?Z(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 et(t)}}const ft=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function ut(l){return l.data.some(t=>t===null)?new w(l.rows,l.cols,l.data.map(t=>t??0)):l}function R(l){return l?.kind==="call"&&l.name==="TABLE"}function T(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function dt(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 x=l.column(r),y=a===void 0?x:l.column(a);if(!x||!y)return h.REF;n=o.left+Math.min(x.index,y.index),i=o.left+Math.max(x.index,y.index)}if(t.thisRow)return e.sheet!==s||e.row<=o.top||e.row>o.bottom?h.VALUE:new p(s,e.row,n,e.row,i);const c=t.sections;if(c.includes("#Totals"))return h.REF;const u=c.includes("#All")||c.includes("#Headers"),f=c.length===0||c.includes("#All")||c.includes("#Data"),g=u?o.top:o.top+1,S=f?o.bottom:o.top;return new p(s,g,n,S,i)}function pt(l,t,e){return new E([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
@@ -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 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}));
2
+ import{CellError as l,ERR as g,errorOf as O}from"../../values.js";import{compare as C,numberOfText as A,sameNumber as b}from"../coerce.js";import{Missing as w,Reference as T}from"../types.js";import{indexKeyOf as k}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,V=1e4;export function wildcard(e){const n=y.get(e);if(n)return n;y.size>=V&&y.clear();const t=G(e);return y.set(e,t),t}function G(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 P(e,n){const t=r=>e===">"?r>0:e===">="?r>=0:e==="<"?r<0:r<=0;return r=>typeof r==typeof n&&t(C(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(O(r)!==void 0)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:P(t,f??r)}}function j(e,n){const t=e.reference;if(!(t?.cols!==1||e.rows<64))return n.calc.indexes.ofRepeated(k("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=j(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 v(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 L(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=L(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 w)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,z=e=>e.length===0?0:e.reduce((n,t)=>t<n?t:n),K=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(z))),p("MAXIFS","r(rv)",a("MAXIFS",S(K))),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)?v(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 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});
2
+ import{parseDate as H,parseTime as X,parseValue as L}from"numfmt";import{CellError as c,ERR as u,dateOf as h,serialOf as p}from"../../values.js";import{toBool as g,toNumber as m,toText as T}from"../coerce.js";import{Missing as v}from"../types.js";import{eachRow as j,number as S,numbersOf as P,raw as U,scalar as l}from"./define.js";const I=2958465,y=e=>{const n=m(e);return n instanceof c?n:n<0||n>=I+1?u.NUM:Math.floor(n)},b=e=>e%4===0&&e%100!==0||e%400===0,A=(e,n)=>n===2?b(e)?29:28:[4,6,9,11].includes(n)?30:31,V=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 Number.isFinite(o)&&o>=0&&o<=I?o:u.NUM})),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 Y=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=Y(e);return n instanceof c?n:Math.floor(n/3600)}),l("MINUTE",1,1,e=>{const n=Y(e);return n instanceof c?n:Math.floor(n/60)%60}),l("SECOND",1,1,e=>{const n=Y(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=H(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=X(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 W=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:W(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 W(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 _(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:g(r);return t instanceof c?t:o instanceof c?o:a instanceof c?a:_(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 V(a)&&(V(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 _(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=b(a.year);else{const E=w=>b(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 k(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=P([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 x=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=k(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)},F=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=k(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",x(!1)),U("NETWORKDAYS.INTL","vv?vr",x(!0)),U("WORKDAY","vv?r",F(!1)),U("WORKDAY.INTL","vv?vr",F(!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 C=(e,n)=>{const r=[];return j([e],n,t=>r.push(t)),r},B=(e,n)=>{const r=C(e[0],n),t=C(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=B(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=B(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,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 P,MAX_ROWS as B}from"../../address.js";import{compareForSort as I}from"../../sort.js";import{CellError as u,ERR as a,Matrix as m}from"../../values.js";import{toBool as Y,toNumber as x,toText as D}from"../coerce.js";import{Lambda as O,Missing as V}from"../types.js";import{cellsOf as w,raw as p}from"./define.js";import{delimiters as S,splitAt as R}from"./delimiters.js";const K=5e6;export const rowsOfCells=n=>Array.from({length:n.rows},(e,r)=>Array.from({length:n.cols},(t,o)=>n.at(r,o))),matrixOf=n=>{const e=n[0]?.length??0;return new m(n.length,e,n.flat())},transposed=n=>{const e=n[0]?.length??0;return Array.from({length:e},(r,t)=>n.map(o=>o[t]??null))};function E(n,e){if(!(n===void 0||n instanceof V))return e.calc.scalar(n,e.frame)}export function numberOf(n,e,r){const t=E(n,e);return t==null?r:x(t)}export function boolOf(n,e,r){const t=E(n,e);return t==null?r:Y(t)}export function indexes(n,e,r){if(n===void 0||n instanceof V)return[r];const t=w(n,e);if(t instanceof u)return t;const o=[];for(let s=0;s<t.rows;s++)for(let c=0;c<t.cols;c++){const i=x(t.at(s,c));if(i instanceof u)return i;o.push(Math.trunc(i))}return o}const U=n=>n===null||n==="";export function compareBy(n,e,r){if(U(n)||U(e))return U(n)&&U(e)?0:U(n)?1:-1;const t=I(n,e);return r?-t:t}export function included(n){return n instanceof u?n:n===null?!1:typeof n=="boolean"?n:typeof n=="number"?n!==0:a.VALUE}p("FILTER","rr?r",(n,e)=>{const r=w(n[0],e),t=w(n[1],e);if(r instanceof u)return r;if(t instanceof u)return t;if(t.rows>1&&t.cols>1)return a.VALUE;const o=t.cols===1&&t.rows===r.rows?!0:t.rows===1&&t.cols===r.cols?!1:void 0;if(o===void 0)return a.VALUE;const s=rowsOfCells(r),c=o?s:transposed(s),i=[];for(let f=0;f<c.length;f++){const l=included(o?t.at(f,0):t.at(0,f));if(l instanceof u)return l;l&&i.push(c[f]??[])}return i.length===0?n[2]===void 0||n[2]instanceof V?a.CALC:n[2]:matrixOf(o?i:transposed(i))}),p("SORT","r?rrr",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=indexes(n[1],e,1);if(t instanceof u)return t;const o=indexes(n[2],e,1);if(o instanceof u)return o;const s=boolOf(n[3],e,!1);if(s instanceof u)return s;const c=rowsOfCells(r),i=s?transposed(c):c,f=i[0]?.length??0;if(o.length!==1&&o.length!==t.length)return a.VALUE;if(t.some(h=>h<1||h>f))return a.VALUE;if(o.some(h=>h!==1&&h!==-1))return a.VALUE;const l=i.map((h,A)=>({line:h,index:A})).sort((h,A)=>{for(const[L,d]of t.entries()){const y=o[o.length===1?0:L]??1,C=compareBy(h.line[d-1]??null,A.line[d-1]??null,y<0);if(C!==0)return C}return h.index-A.index}).map(h=>h.line);return matrixOf(s?transposed(l):l)}),p("SORTBY","r(r)",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=[];let o;for(let f=1;f<n.length;f+=2){const l=w(n[f],e);if(l instanceof u)return l;if(l.rows>1&&l.cols>1)return a.VALUE;const h=l.cols===1&&l.rows===r.rows,A=l.rows===1&&l.cols===r.cols;if(!h&&!A)return a.VALUE;const L=o??h;if(L?!h:!A)return a.VALUE;o=L;const d=numberOf(n[f+1],e,1);if(d instanceof u)return d;if(d!==1&&d!==-1)return a.VALUE;t.push({values:rowsOfCells(l).flat(),descending:d<0})}const s=rowsOfCells(r),i=(o===!1?transposed(s):s).map((f,l)=>({line:f,index:l})).sort((f,l)=>{for(const{values:h,descending:A}of t){const L=compareBy(h[f.index]??null,h[l.index]??null,A);if(L!==0)return L}return f.index-l.index}).map(f=>f.line);return matrixOf(o===!1?transposed(i):i)});export const keyOf=n=>n instanceof u?`e${n.code}`:typeof n=="string"?`s${n.toLowerCase()}`:`${typeof n}${String(n)}`;p("UNIQUE","r?rr",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=boolOf(n[1],e,!1);if(t instanceof u)return t;const o=boolOf(n[2],e,!1);if(o instanceof u)return o;const s=rowsOfCells(r),c=t?transposed(s):s,i=new Map;for(const l of c){const h=l.map(keyOf).join("\0"),A=i.get(h);A?A.count+=1:i.set(h,{line:l,count:1})}const f=[...i.values()].filter(l=>!o||l.count===1).map(l=>l.line);return f.length===0?a.CALC:matrixOf(t?transposed(f):f)});function M(n,e){if(n instanceof u)return n;if(e instanceof u)return e;const r=Math.trunc(n),t=Math.trunc(e);return r<0||t<0?a.VALUE:r===0||t===0?a.CALC:r>B||t>P||r*t>K?a.VALUE:{rows:r,cols:t}}p("SEQUENCE","r?rrr",(n,e)=>{const r=M(numberOf(n[0],e,1),numberOf(n[1],e,1));if(r instanceof u)return r;const t=numberOf(n[2],e,1);if(t instanceof u)return t;const o=numberOf(n[3],e,1);if(o instanceof u)return o;const s=new Array(r.rows*r.cols);for(let c=0;c<s.length;c++)s[c]=t+c*o;return new m(r.rows,r.cols,s)}),p("RANDARRAY","?rrrrr",(n,e)=>{const r=M(numberOf(n[0],e,1),numberOf(n[1],e,1));if(r instanceof u)return r;const t=numberOf(n[2],e,0);if(t instanceof u)return t;const o=numberOf(n[3],e,1);if(o instanceof u)return o;const s=boolOf(n[4],e,!1);if(s instanceof u)return s;if(t>o)return a.VALUE;if(s&&(!Number.isInteger(t)||!Number.isInteger(o)))return a.VALUE;const c=new Array(r.rows*r.cols);for(let i=0;i<c.length;i++){const f=e.calc.random();c[i]=s?t+Math.floor(f*(o-t+1)):t+f*(o-t)}return new m(r.rows,r.cols,c)},{volatile:!0}),p("TEXTSPLIT","vr?rvvv",(n,e)=>{const r=E(n[0],e);if(r instanceof u)return r;const t=D(r??"");if(t instanceof u)return t;const o=S(n[1],e);if(o instanceof u)return o;const s=S(n[2],e);if(s instanceof u)return s;if(o.length===0&&s.length===0)return a.VALUE;if(o.includes("")||s.includes(""))return a.VALUE;const c=boolOf(n[3],e,!1);if(c instanceof u)return c;const i=numberOf(n[4],e,0);if(i instanceof u)return i;const f=E(n[5],e)??a.NA,l=i===1,A=(s.length===0?[t]:R(t,s,l)).filter(d=>!(c&&d==="")).map(d=>{const y=o.length===0?[d]:R(d,o,l);return c?y.filter(C=>C!==""):y}),L=Math.max(0,...A.map(d=>d.length));return A.length===0||L===0?a.CALC:matrixOf(A.map(d=>Array.from({length:L},(y,C)=>d[C]??f)))});export const oneOf=(n,e)=>{const r=e.calc.flat(n);return r instanceof m?r.at(0,0):r};p("MAP","r(r)",(n,e)=>{const r=n[n.length-1];if(!(r instanceof O))return a.VALUE;const t=[];for(const f of n.slice(0,-1)){const l=w(f,e);if(l instanceof u)return l;t.push(l)}const o=t[0];if(!o)return a.VALUE;const{rows:s,cols:c}=o;if(t.some(f=>f.rows!==s||f.cols!==c))return a.VALUE;const i=new Array(s*c);for(let f=0;f<s;f++)for(let l=0;l<c;l++)i[f*c+l]=oneOf(e.calc.apply(r,t.map(h=>h.at(f,l))),e);return new m(s,c,i)});function N(n,e){const r=n.length===3,t=n[n.length-1];if(!(t instanceof O))return a.VALUE;const o=w(n[r?1:0],e);return o instanceof u?o:{start:r?E(n[0],e)??null:o.at(0,0),cells:o,fn:t}}p("REDUCE","rr?r",(n,e)=>{const r=N(n,e);if(r instanceof u)return r;const{cells:t,fn:o}=r,s=n.length===3?0:1;let c=r.start;for(let i=s;i<t.rows*t.cols;i++)c=oneOf(e.calc.apply(o,[c,t.at(Math.floor(i/t.cols),i%t.cols)]),e);return c}),p("SCAN","rr?r",(n,e)=>{const r=N(n,e);if(r instanceof u)return r;const{cells:t,fn:o}=r,s=t.rows*t.cols,c=n.length===3?0:1,i=new Array(s);let f=r.start;c===1&&(i[0]=f);for(let l=c;l<s;l++)f=oneOf(e.calc.apply(o,[f,t.at(Math.floor(l/t.cols),l%t.cols)]),e),i[l]=f;return new m(t.rows,t.cols,i)});const T=n=>(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=e[1];if(!(o instanceof O))return a.VALUE;const s=rowsOfCells(t),i=(n?s:transposed(s)).map(f=>oneOf(r.calc.apply(o,[n?new m(1,f.length,f):new m(f.length,1,f)]),r));return n?new m(i.length,1,i):new m(1,i.length,i)};p("BYROW","rr",T(!0)),p("BYCOL","rr",T(!1)),p("MAKEARRAY","rrr",(n,e)=>{const r=M(numberOf(n[0],e,1),numberOf(n[1],e,1));if(r instanceof u)return r;const t=n[2];if(!(t instanceof O))return a.VALUE;const o=new Array(r.rows*r.cols);for(let s=0;s<r.rows;s++)for(let c=0;c<r.cols;c++)o[s*r.cols+c]=oneOf(e.calc.apply(t,[s+1,c+1]),e);return new m(r.rows,r.cols,o)});function b(n){return(e,r)=>{const t=[];for(const c of e){const i=w(c,r);if(i instanceof u)return i;const f=rowsOfCells(i);t.push(n?f:transposed(f))}const o=Math.max(0,...t.map(c=>c[0]?.length??0)),s=t.flatMap(c=>c.map(i=>Array.from({length:o},(f,l)=>l<i.length?i[l]??null:a.NA)));return s.length===0?a.CALC:matrixOf(n?s:transposed(s))}}p("VSTACK","(r)",b(!0)),p("HSTACK","(r)",b(!1));function _(n,e,r){const t=Math.min(n,Math.abs(e));return r?e>=0?[0,t]:[n-t,n]:e>=0?[t,n]:[0,n-t]}function k(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=A=>A===void 0||A instanceof V,s=numberOf(e[1],r,n?t.rows:0);if(s instanceof u)return s;const c=numberOf(e[2],r,n?t.cols:0);if(c instanceof u)return c;if(n&&!o(e[1])&&Math.trunc(s)===0)return a.CALC;if(n&&!o(e[2])&&Math.trunc(c)===0)return a.CALC;const[i,f]=_(t.rows,Math.trunc(s),n),[l,h]=_(t.cols,Math.trunc(c),n);return f<=i||h<=l?a.CALC:matrixOf(rowsOfCells(t).slice(i,f).map(A=>A.slice(l,h)))}}p("TAKE","rr?r",k(!0)),p("DROP","rr?r",k(!1));function z(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=numberOf(e[1],r,0);if(o instanceof u)return o;const s=boolOf(e[2],r,!1);if(s instanceof u)return s;if(![0,1,2,3].includes(o))return a.VALUE;const c=rowsOfCells(t),f=(s?transposed(c):c).flat().filter(l=>!((o===1||o===3)&&l===null||(o===2||o===3)&&l instanceof u));return f.length===0?a.CALC:n?new m(f.length,1,f):new m(1,f.length,f)}}p("TOCOL","r?rr",z(!0)),p("TOROW","r?rr",z(!1));function g(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=rowsOfCells(t),s=n?o:transposed(o),c=[];for(const i of e.slice(1)){const f=indexes(i,r,1);if(f instanceof u)return f;for(const l of f){const h=s[l>0?l-1:s.length+l];if(l===0||!h)return a.VALUE;c.push(h)}}return matrixOf(n?c:transposed(c))}}p("CHOOSEROWS","r(r)",g(!0)),p("CHOOSECOLS","r(r)",g(!1)),p("EXPAND","rr?rr",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=M(numberOf(n[1],e,r.rows),numberOf(n[2],e,r.cols));if(t instanceof u)return t;if(t.rows<r.rows||t.cols<r.cols)return a.VALUE;const o=E(n[3],e)??a.NA,s=new Array(t.rows*t.cols);for(let c=0;c<t.rows;c++)for(let i=0;i<t.cols;i++)s[c*t.cols+i]=c<r.rows&&i<r.cols?r.at(c,i):o;return new m(t.rows,t.cols,s)});function W(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;if(t.rows>1&&t.cols>1)return a.VALUE;const o=numberOf(e[1],r,1);if(o instanceof u)return o;const s=Math.trunc(o);if(s<1)return a.NUM;const c=E(e[2],r)??a.NA,i=rowsOfCells(t).flat(),f=[];for(let l=0;l<i.length;l+=s)f.push(Array.from({length:s},(h,A)=>l+A<i.length?i[l+A]??null:c));return matrixOf(n?f:transposed(f))}}p("WRAPROWS","rr?r",W(!0)),p("WRAPCOLS","rr?r",W(!1));
2
+ import{MAX_COLUMNS as B,MAX_ROWS as I}from"../../address.js";import{compareForSort as Y}from"../../sort.js";import{CellError as u,ERR as a,Matrix as L}from"../../values.js";import{toBool as D,toNumber as x,toText as K}from"../coerce.js";import{Lambda as O,Missing as V}from"../types.js";import{cellsOf as m,raw as A}from"./define.js";import{delimiters as S,splitAt as R}from"./delimiters.js";const N=5e6;export const rowsOfCells=t=>Array.from({length:t.rows},(e,r)=>Array.from({length:t.cols},(n,o)=>t.at(r,o))),matrixOf=t=>{const e=t[0]?.length??0;return new L(t.length,e,t.flat())},transposed=t=>{const e=t[0]?.length??0;return Array.from({length:e},(r,n)=>t.map(o=>o[n]??null))};function y(t,e){if(!(t===void 0||t instanceof V))return e.calc.scalar(t,e.frame)}export function numberOf(t,e,r){const n=y(t,e);return n==null?r:x(n)}export function boolOf(t,e,r){const n=y(t,e);return n==null?r:D(n)}export function indexes(t,e,r){if(t===void 0||t instanceof V)return[r];const n=m(t,e);if(n instanceof u)return n;const o=[];for(let s=0;s<n.rows;s++)for(let c=0;c<n.cols;c++){const i=x(n.at(s,c));if(i instanceof u)return i;o.push(Math.trunc(i))}return o}const U=t=>t===null||t==="";export function compareBy(t,e,r){if(U(t)||U(e))return U(t)&&U(e)?0:U(t)?1:-1;const n=Y(t,e);return r?-n:n}export function included(t){return t instanceof u?t:t===null?!1:typeof t=="boolean"?t:typeof t=="number"?t!==0:a.VALUE}A("FILTER","rr?r",(t,e)=>{const r=m(t[0],e),n=m(t[1],e);if(r instanceof u)return r;if(n instanceof u)return n;if(n.rows>1&&n.cols>1)return a.VALUE;const o=n.cols===1&&n.rows===r.rows?!0:n.rows===1&&n.cols===r.cols?!1:void 0;if(o===void 0)return a.VALUE;const s=rowsOfCells(r),c=o?s:transposed(s),i=[];for(let f=0;f<c.length;f++){const l=included(o?n.at(f,0):n.at(0,f));if(l instanceof u)return l;l&&i.push(c[f]??[])}return i.length===0?t[2]===void 0||t[2]instanceof V?a.CALC:t[2]:matrixOf(o?i:transposed(i))}),A("SORT","r?rrr",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=indexes(t[1],e,1);if(n instanceof u)return n;const o=indexes(t[2],e,1);if(o instanceof u)return o;const s=boolOf(t[3],e,!1);if(s instanceof u)return s;const c=rowsOfCells(r),i=s?transposed(c):c,f=i[0]?.length??0;if(o.length!==1&&o.length!==n.length)return a.VALUE;if(n.some(h=>h<1||h>f))return a.VALUE;if(o.some(h=>h!==1&&h!==-1))return a.VALUE;const l=i.map((h,p)=>({line:h,index:p})).sort((h,p)=>{for(const[w,d]of n.entries()){const C=o[o.length===1?0:w]??1,E=compareBy(h.line[d-1]??null,p.line[d-1]??null,C<0);if(E!==0)return E}return h.index-p.index}).map(h=>h.line);return matrixOf(s?transposed(l):l)}),A("SORTBY","r(r)",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=[];let o;for(let f=1;f<t.length;f+=2){const l=m(t[f],e);if(l instanceof u)return l;if(l.rows>1&&l.cols>1)return a.VALUE;const h=l.cols===1&&l.rows===r.rows,p=l.rows===1&&l.cols===r.cols;if(!h&&!p)return a.VALUE;const w=o??h;if(w?!h:!p)return a.VALUE;o=w;const d=numberOf(t[f+1],e,1);if(d instanceof u)return d;if(d!==1&&d!==-1)return a.VALUE;n.push({values:rowsOfCells(l).flat(),descending:d<0})}const s=rowsOfCells(r),i=(o===!1?transposed(s):s).map((f,l)=>({line:f,index:l})).sort((f,l)=>{for(const{values:h,descending:p}of n){const w=compareBy(h[f.index]??null,h[l.index]??null,p);if(w!==0)return w}return f.index-l.index}).map(f=>f.line);return matrixOf(o===!1?transposed(i):i)});export const keyOf=t=>t instanceof u?`e${t.code}`:typeof t=="string"?`s${t.toLowerCase()}`:`${typeof t}${String(t)}`;A("UNIQUE","r?rr",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=boolOf(t[1],e,!1);if(n instanceof u)return n;const o=boolOf(t[2],e,!1);if(o instanceof u)return o;const s=rowsOfCells(r),c=(r.reference?.rows??r.rows)-r.rows,i=w=>new Array(w).fill(null);if(c>0&&n&&(s.length+c)*r.cols<=N)for(let w=0;w<c;w++)s.push(i(r.cols));const f=n?transposed(s):s,l=new Map,h=(w,d)=>{const C=w.map(keyOf).join("\0"),E=l.get(C);E?E.count+=d:l.set(C,{line:w,count:d})};for(const w of f)h(w,1);c>0&&!n&&h(i(r.cols),c);const p=[...l.values()].filter(w=>!o||w.count===1).map(w=>w.line);return p.length===0?a.CALC:matrixOf(n?transposed(p):p)});function M(t,e){if(t instanceof u)return t;if(e instanceof u)return e;const r=Math.trunc(t),n=Math.trunc(e);return r<0||n<0?a.VALUE:r===0||n===0?a.CALC:r>I||n>B||r*n>N?a.VALUE:{rows:r,cols:n}}A("SEQUENCE","r?rrr",(t,e)=>{const r=M(numberOf(t[0],e,1),numberOf(t[1],e,1));if(r instanceof u)return r;const n=numberOf(t[2],e,1);if(n instanceof u)return n;const o=numberOf(t[3],e,1);if(o instanceof u)return o;const s=new Array(r.rows*r.cols);for(let c=0;c<s.length;c++)s[c]=n+c*o;return new L(r.rows,r.cols,s)}),A("RANDARRAY","?rrrrr",(t,e)=>{const r=M(numberOf(t[0],e,1),numberOf(t[1],e,1));if(r instanceof u)return r;const n=numberOf(t[2],e,0);if(n instanceof u)return n;const o=numberOf(t[3],e,1);if(o instanceof u)return o;const s=boolOf(t[4],e,!1);if(s instanceof u)return s;if(n>o)return a.VALUE;if(s&&(!Number.isInteger(n)||!Number.isInteger(o)))return a.VALUE;const c=new Array(r.rows*r.cols);for(let i=0;i<c.length;i++){const f=e.calc.random();c[i]=s?n+Math.floor(f*(o-n+1)):n+f*(o-n)}return new L(r.rows,r.cols,c)},{volatile:!0}),A("TEXTSPLIT","vr?rvvv",(t,e)=>{const r=y(t[0],e);if(r instanceof u)return r;const n=K(r??"");if(n instanceof u)return n;const o=S(t[1],e);if(o instanceof u)return o;const s=S(t[2],e);if(s instanceof u)return s;if(o.length===0&&s.length===0)return a.VALUE;if(o.includes("")||s.includes(""))return a.VALUE;const c=boolOf(t[3],e,!1);if(c instanceof u)return c;const i=numberOf(t[4],e,0);if(i instanceof u)return i;const f=y(t[5],e)??a.NA,l=i===1,p=(s.length===0?[n]:R(n,s,l)).filter(d=>!(c&&d==="")).map(d=>{const C=o.length===0?[d]:R(d,o,l);return c?C.filter(E=>E!==""):C}),w=Math.max(0,...p.map(d=>d.length));return p.length===0||w===0?a.CALC:matrixOf(p.map(d=>Array.from({length:w},(C,E)=>d[E]??f)))});export const oneOf=(t,e)=>{const r=e.calc.flat(t);return r instanceof L?r.at(0,0):r};A("MAP","r(r)",(t,e)=>{const r=t[t.length-1];if(!(r instanceof O))return a.VALUE;const n=[];for(const f of t.slice(0,-1)){const l=m(f,e);if(l instanceof u)return l;n.push(l)}const o=n[0];if(!o)return a.VALUE;const{rows:s,cols:c}=o;if(n.some(f=>f.rows!==s||f.cols!==c))return a.VALUE;const i=new Array(s*c);for(let f=0;f<s;f++)for(let l=0;l<c;l++)i[f*c+l]=oneOf(e.calc.apply(r,n.map(h=>h.at(f,l))),e);return new L(s,c,i)});function T(t,e){const r=t.length===3,n=t[t.length-1];if(!(n instanceof O))return a.VALUE;const o=m(t[r?1:0],e);return o instanceof u?o:{start:r?y(t[0],e)??null:o.at(0,0),cells:o,fn:n}}A("REDUCE","rr?r",(t,e)=>{const r=T(t,e);if(r instanceof u)return r;const{cells:n,fn:o}=r,s=t.length===3?0:1;let c=r.start;for(let i=s;i<n.rows*n.cols;i++)c=oneOf(e.calc.apply(o,[c,n.at(Math.floor(i/n.cols),i%n.cols)]),e);return c}),A("SCAN","rr?r",(t,e)=>{const r=T(t,e);if(r instanceof u)return r;const{cells:n,fn:o}=r,s=n.rows*n.cols,c=t.length===3?0:1,i=new Array(s);let f=r.start;c===1&&(i[0]=f);for(let l=c;l<s;l++)f=oneOf(e.calc.apply(o,[f,n.at(Math.floor(l/n.cols),l%n.cols)]),e),i[l]=f;return new L(n.rows,n.cols,i)});const b=t=>(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=e[1];if(!(o instanceof O))return a.VALUE;const s=rowsOfCells(n),i=(t?s:transposed(s)).map(f=>oneOf(r.calc.apply(o,[t?new L(1,f.length,f):new L(f.length,1,f)]),r));return t?new L(i.length,1,i):new L(1,i.length,i)};A("BYROW","rr",b(!0)),A("BYCOL","rr",b(!1)),A("MAKEARRAY","rrr",(t,e)=>{const r=M(numberOf(t[0],e,1),numberOf(t[1],e,1));if(r instanceof u)return r;const n=t[2];if(!(n instanceof O))return a.VALUE;const o=new Array(r.rows*r.cols);for(let s=0;s<r.rows;s++)for(let c=0;c<r.cols;c++)o[s*r.cols+c]=oneOf(e.calc.apply(n,[s+1,c+1]),e);return new L(r.rows,r.cols,o)});function _(t){return(e,r)=>{const n=[];for(const c of e){const i=m(c,r);if(i instanceof u)return i;const f=rowsOfCells(i);n.push(t?f:transposed(f))}const o=Math.max(0,...n.map(c=>c[0]?.length??0)),s=n.flatMap(c=>c.map(i=>Array.from({length:o},(f,l)=>l<i.length?i[l]??null:a.NA)));return s.length===0?a.CALC:matrixOf(t?s:transposed(s))}}A("VSTACK","(r)",_(!0)),A("HSTACK","(r)",_(!1));function g(t,e,r){const n=Math.min(t,Math.abs(e));return r?e>=0?[0,n]:[t-n,t]:e>=0?[n,t]:[0,t-n]}function k(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=p=>p===void 0||p instanceof V,s=numberOf(e[1],r,t?n.rows:0);if(s instanceof u)return s;const c=numberOf(e[2],r,t?n.cols:0);if(c instanceof u)return c;if(t&&!o(e[1])&&Math.trunc(s)===0)return a.CALC;if(t&&!o(e[2])&&Math.trunc(c)===0)return a.CALC;const[i,f]=g(n.rows,Math.trunc(s),t),[l,h]=g(n.cols,Math.trunc(c),t);return f<=i||h<=l?a.CALC:matrixOf(rowsOfCells(n).slice(i,f).map(p=>p.slice(l,h)))}}A("TAKE","rr?r",k(!0)),A("DROP","rr?r",k(!1));function z(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=numberOf(e[1],r,0);if(o instanceof u)return o;const s=boolOf(e[2],r,!1);if(s instanceof u)return s;if(![0,1,2,3].includes(o))return a.VALUE;const c=rowsOfCells(n),f=(s?transposed(c):c).flat().filter(l=>!((o===1||o===3)&&l===null||(o===2||o===3)&&l instanceof u));return f.length===0?a.CALC:t?new L(f.length,1,f):new L(1,f.length,f)}}A("TOCOL","r?rr",z(!0)),A("TOROW","r?rr",z(!1));function W(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=rowsOfCells(n),s=t?o:transposed(o),c=[];for(const i of e.slice(1)){const f=indexes(i,r,1);if(f instanceof u)return f;for(const l of f){const h=s[l>0?l-1:s.length+l];if(l===0||!h)return a.VALUE;c.push(h)}}return matrixOf(t?c:transposed(c))}}A("CHOOSEROWS","r(r)",W(!0)),A("CHOOSECOLS","r(r)",W(!1)),A("EXPAND","rr?rr",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=M(numberOf(t[1],e,r.rows),numberOf(t[2],e,r.cols));if(n instanceof u)return n;if(n.rows<r.rows||n.cols<r.cols)return a.VALUE;const o=y(t[3],e)??a.NA,s=new Array(n.rows*n.cols);for(let c=0;c<n.rows;c++)for(let i=0;i<n.cols;i++)s[c*n.cols+i]=c<r.rows&&i<r.cols?r.at(c,i):o;return new L(n.rows,n.cols,s)});function P(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;if(n.rows>1&&n.cols>1)return a.VALUE;const o=numberOf(e[1],r,1);if(o instanceof u)return o;const s=Math.trunc(o);if(s<1)return a.NUM;const c=y(e[2],r)??a.NA,i=rowsOfCells(n).flat(),f=[];for(let l=0;l<i.length;l+=s)f.push(Array.from({length:s},(h,p)=>l+p<i.length?i[l+p]??null:c));return matrixOf(t?f:transposed(f))}}A("WRAPROWS","rr?r",P(!0)),A("WRAPCOLS","rr?r",P(!1));
@@ -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 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);
2
+ import{cellName as R}from"../../address.js";import{CellError as s,ERR as n,Matrix as L}from"../../values.js";import{FormulaCell as p}from"../../workbook.js";import{toNumber as d,toText as I}from"../coerce.js";import{Lambda as y,Reference as l,Union as S}from"../types.js";import{lazy as u,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 A=(e,r)=>{const t=d(e);return t instanceof s?t:Math.abs(Math.trunc(t))%2===r};c("ISEVEN",1,1,e=>A(e,0)),c("ISODD",1,1,e=>A(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 L?64:typeof t=="number"||t===null?1:typeof t=="string"?2:typeof t=="boolean"?4:16});const b=[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?b.indexOf(e):-1;return r<0?n.NA:r+1},{errors:!0}),u("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 S});const N=e=>e instanceof l?e.sheet.at(e.top,e.left):void 0;u("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 y?n.VALUE:N(o)instanceof p}),u("FORMULATEXT",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=N(r.calc.eval(t,r.frame));return o instanceof p?o.text:n.NA}),i("CELL","v?r",(e,r)=>{const t=I(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),m=()=>r.calc.read(a.sheet,a.top,a.left);switch(t.toLowerCase()){case"filename":return`[${r.calc.wb.title??"sheet"}.xlsx]${a.sheet.name}`;case"row":return a.top+1;case"col":return a.left+1;case"address":return R({row:a.top,col:a.left}).replace(/^([A-Z]+)(\d+)$/u,"$$$1$$$2");case"contents":return m();case"type":{const E=m();return E===null?"b":typeof E=="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{CellError as s,ERR as i,Matrix as A}from"../../values.js";import{compare as N,toBool as p,toNumber as U}from"../coerce.js";import{Lambda as R,Missing as b}from"../types.js";import{eachArg as O,lazy as m,many as d,raw as E,scalar as k}from"./define.js";const l=(t,n)=>t?n.calc.eval(t,n.frame):!1;function V(t,n,e){if(!(t instanceof A))return t;const r=t.rows===1?0:n,o=t.cols===1?0:e;return r<t.rows&&o<t.cols?t.at(r,o):i.NA}function L(t,n,e){const r=[t,...n].filter(c=>c instanceof A),o=Math.max(...r.map(c=>c.rows)),f=Math.max(...r.map(c=>c.cols)),a=new Array(o*f);for(let c=0;c<o;c++)for(let u=0;u<f;u++){const h=e(V(t,c,u));a[c*f+u]=h instanceof s?h:V(n[h]??i.VALUE,c,u)}return new A(o,f,a)}m("IF",1,3,(t,n)=>{const e=n.calc.flat(l(t[0],n)),r=f=>{const a=t[f];if(a===void 0)return f===1;const c=n.calc.eval(a,n.frame);return c instanceof b?0:c};if(e instanceof A){const f=a=>a<t.length?n.calc.flat(r(a)):!1;return L(e,[f(1),f(2)],a=>{const c=p(a);return c instanceof s?c:c?0:1})}const o=p(e);return o instanceof s?o:r(o?1:2)}),m("IFS",2,d,(t,n)=>{for(let e=0;e+1<t.length;e+=2){const r=p(n.calc.scalar(l(t[e],n),n.frame));if(r instanceof s)return r;if(r)return l(t[e+1],n)}return i.NA});const M=t=>(n,e)=>{const r=e.calc.flat(l(n[0],e)),o=a=>a instanceof s&&(t===void 0||a===t),f=()=>n[1]?e.calc.flat(e.calc.eval(n[1],e.frame)):"";return r instanceof A?L(r,[r,f()],a=>o(a)?1:0):o(r)?f():r};m("IFERROR",2,2,M()),m("IFNA",2,2,M(i.NA)),m("CHOOSE",2,255,(t,n)=>{const e=n.calc.flat(l(t[0],n));if(e instanceof A){const a=t.slice(1).map(c=>n.calc.flat(n.calc.eval(c,n.frame)));return L(e,a,c=>{const u=U(c);if(u instanceof s)return u;const h=Math.trunc(u)-1;return h<0||h>=a.length?i.VALUE:h})}const r=U(e);if(r instanceof s)return r;const o=Math.trunc(r),f=t[o];return o<1||f===void 0?i.VALUE:n.calc.eval(f,n.frame)}),m("SWITCH",3,d,(t,n)=>{const e=n.calc.scalar(l(t[0],n),n.frame);if(e instanceof s)return e;let r=1;for(;r+1<t.length;r+=2){const o=n.calc.scalar(l(t[r],n),n.frame);if(o instanceof s)return o;if(N(e,o)===0)return l(t[r+1],n)}return r<t.length?l(t[r],n):i.NA});function w(t,n){const e=[];let r;return O(t,n,(o,f)=>{if(r)return;if(o instanceof s){r=o;return}if(typeof o=="string"&&!f||o===null)return;const a=p(o);a instanceof s?r=a:e.push(a)}),r||(e.length===0?i.VALUE:e)}E("AND","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.every(Boolean)}),E("OR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.some(Boolean)}),E("XOR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.filter(Boolean).length%2===1}),k("NOT",1,1,t=>{const n=p(t);return n instanceof s?n:!n}),E("TRUE","",()=>!0),E("FALSE","",()=>!1);const y=(t,n)=>n?{names:t,parent:n}:{names:t};m("LET",3,d,(t,n)=>{if(t.length%2===0)return i.VALUE;const e=new Map,r={...n.frame,scope:y(e,n.frame.scope)};for(let o=0;o+1<t.length;o+=2){const f=t[o];if(f?.kind!=="name")return i.VALUE;e.set(f.name.toLowerCase(),l(t[o+1],{...n,frame:r}))}return l(t[t.length-1],{...n,frame:r})}),m("LAMBDA",1,254,(t,n)=>{const e=[];for(const o of t.slice(0,-1)){if(o.kind!=="name")return i.VALUE;e.push(o.name.toLowerCase())}const r=t[t.length-1];return r?new R(e,r,n.frame):i.VALUE});
2
+ import{CellError as s,ERR as i,Matrix as A}from"../../values.js";import{compare as N,toBool as p,toNumber as U}from"../coerce.js";import{Lambda as R,Missing as b}from"../types.js";import{eachArg as O,lazy as m,many as d,raw as E,scalar as k}from"./define.js";const l=(t,n)=>t?n.calc.eval(t,n.frame):!1;function V(t,n,e){if(!(t instanceof A))return t;const r=t.rows===1?0:n,o=t.cols===1?0:e;return r<t.rows&&o<t.cols?t.at(r,o):i.NA}function L(t,n,e){const r=[t,...n].filter(c=>c instanceof A),o=Math.max(...r.map(c=>c.rows)),f=Math.max(...r.map(c=>c.cols)),a=new Array(o*f);for(let c=0;c<o;c++)for(let u=0;u<f;u++){const h=e(V(t,c,u));a[c*f+u]=h instanceof s?h:V(n[h]??i.VALUE,c,u)}return new A(o,f,a)}m("IF",1,3,(t,n)=>{const e=n.calc.flat(l(t[0],n)),r=f=>{const a=t[f];if(a===void 0)return f===1;const c=n.calc.eval(a,n.frame);return c instanceof b?null:c};if(e instanceof A){const f=a=>a<t.length?n.calc.flat(r(a))??0:!1;return L(e,[f(1),f(2)],a=>{const c=p(a);return c instanceof s?c:c?0:1})}const o=p(e);return o instanceof s?o:r(o?1:2)}),m("IFS",2,d,(t,n)=>{for(let e=0;e+1<t.length;e+=2){const r=p(n.calc.scalar(l(t[e],n),n.frame));if(r instanceof s)return r;if(r)return l(t[e+1],n)}return i.NA});const M=t=>(n,e)=>{const r=e.calc.flat(l(n[0],e)),o=a=>a instanceof s&&(t===void 0||a===t),f=()=>n[1]?e.calc.flat(e.calc.eval(n[1],e.frame)):"";return r instanceof A?L(r,[r,f()],a=>o(a)?1:0):o(r)?f():r};m("IFERROR",2,2,M()),m("IFNA",2,2,M(i.NA)),m("CHOOSE",2,255,(t,n)=>{const e=n.calc.flat(l(t[0],n));if(e instanceof A){const a=t.slice(1).map(c=>n.calc.flat(n.calc.eval(c,n.frame)));return L(e,a,c=>{const u=U(c);if(u instanceof s)return u;const h=Math.trunc(u)-1;return h<0||h>=a.length?i.VALUE:h})}const r=U(e);if(r instanceof s)return r;const o=Math.trunc(r),f=t[o];return o<1||f===void 0?i.VALUE:n.calc.eval(f,n.frame)}),m("SWITCH",3,d,(t,n)=>{const e=n.calc.scalar(l(t[0],n),n.frame);if(e instanceof s)return e;let r=1;for(;r+1<t.length;r+=2){const o=n.calc.scalar(l(t[r],n),n.frame);if(o instanceof s)return o;if(N(e,o)===0)return l(t[r+1],n)}return r<t.length?l(t[r],n):i.NA});function w(t,n){const e=[];let r;return O(t,n,(o,f)=>{if(r)return;if(o instanceof s){r=o;return}if(typeof o=="string"&&!f||o===null)return;const a=p(o);a instanceof s?r=a:e.push(a)}),r||(e.length===0?i.VALUE:e)}E("AND","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.every(Boolean)}),E("OR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.some(Boolean)}),E("XOR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.filter(Boolean).length%2===1}),k("NOT",1,1,t=>{const n=p(t);return n instanceof s?n:!n}),E("TRUE","",()=>!0),E("FALSE","",()=>!1);const y=(t,n)=>n?{names:t,parent:n}:{names:t};m("LET",3,d,(t,n)=>{if(t.length%2===0)return i.VALUE;const e=new Map,r={...n.frame,scope:y(e,n.frame.scope)};for(let o=0;o+1<t.length;o+=2){const f=t[o];if(f?.kind!=="name")return i.VALUE;e.set(f.name.toLowerCase(),l(t[o+1],{...n,frame:r}))}return l(t[t.length-1],{...n,frame:r})}),m("LAMBDA",1,254,(t,n)=>{const e=[];for(const o of t.slice(0,-1)){if(o.kind!=="name")return i.VALUE;e.push(o.name.toLowerCase())}const r=t[t.length-1];return r?new R(e,r,n.frame):i.VALUE});
@@ -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 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);
2
+ import{CellError as u,ERR as p,Matrix as M}from"../../values.js";import{cellName as K,columnLetter as q,MAX_COLUMNS as W,MAX_ROWS as L}from"../../address.js";import{sheetPrefix as G}from"../../formula/print.js";import{parseFormula as J}from"../../formula/parse.js";import{compare as y,toBool as U,toNumber as O,toText as H}from"../coerce.js";import{Missing as k,Reference as x,Union as F}from"../types.js";import{cellsOf as R,lazy as Q,raw as A,scalar as Y}from"./define.js";import{indexKeyOf as Z}from"../indexes.js";import{hasWildcard as g,indexKey as C,literalOf as nn,wildcard as tn}from"./criteria.js";const I=(n,t)=>typeof n==typeof t&&!(n instanceof u)&&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 V=new WeakMap;function en(n,t){const e=V.get(n);if(e)return e;if(e===void 0){V.set(n,null);return}const o=t();return V.set(n,o),o}function P(n,t,e,o,r,s=0,c=1){const f=typeof n=="string"?n:void 0,i=r&&f!==void 0&&g(f)?nn(f):f;if(f!==void 0&&i===void 0){const l=tn(f);for(let w=s;w>=0&&w<t.length;w+=c){const h=t.at(w);if(typeof h=="string"&&l(h))return w}return-1}const a=C(i??n);if(a===void 0)return-1;if(t.length>=64&&s===0&&c===1){const l=()=>{const d=new Map;for(let m=t.length-1;m>=0;m--){const E=C(t.at(m));E!==void 0&&d.set(E,[m])}return d},w=e.reference,h=w?o.calc.indexes.ofRepeated(Z("find",w),[w],l):e.array&&en(e.array,l);if(h)return h.get(a)?.[0]??-1}for(let l=s;l>=0&&l<t.length;l+=c)if(C(t.at(l))===a)return l;return-1}function $(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=y(t.at(f),n);(e?i>=0:i<=0)?(s=f,o=c+1):r=f-1}return s}const N=(n,t)=>t.calc.scalar(n??null,t.frame),rn=(n,t,e)=>n===void 0?e:n instanceof k?!1:U(t.calc.scalar(n,t.frame));function X(n){return(t,e)=>{const o=N(t[0],e);if(o instanceof u)return o;const r=R(t[1],e);if(r instanceof u)return r;const s=O(e.calc.scalar(t[2],e.frame));if(s instanceof u)return s;const c=Math.trunc(s);if(c<1)return p.VALUE;const f=r.reference,i=f?f.bottom-f.top+1:r.rows;if(c>(n?r.cols:i))return p.REF;const a=rn(t[3],e,!0);if(a instanceof u)return a;const l={length:n?r.rows:r.cols,at:d=>n?r.at(d,0):r.at(0,d)},w={rows:n?r.rows:1,cols:n?1:r.cols,at:(d,m)=>r.at(d,m),...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)}:{}},h=a?$(o,l):P(o,l,w,e,!0);return h<0?p.NA:n?r.at(h,c-1):r.at(c-1,h)}}A("VLOOKUP","vrv?v",X(!0)),A("HLOOKUP","vrv?v",X(!1)),A("MATCH","vr?v",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=T(o);if(!r)return p.NA;const s=n[2]===void 0?1:n[2]instanceof k?0:O(t.calc.scalar(n[2],t.frame));if(s instanceof u)return s;const c=s===0?P(e,r,o,t,!0):$(e,r,s<0);return c<0?p.NA:c+1}),A("LOOKUP","vr?r",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=o.rows>=o.cols,s={length:r?o.rows:o.cols,at:a=>r?o.at(a,0):o.at(0,a)},c=$(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 u)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,s,r<0);if(l>=0&&y(s.at(l),n)===0)return l;if(o===0)return-1}const i=P(n,s,t,e,o===2,c?s.length-1:0,c?-1:1);if(i>=0||o===0||o===2)return i;let a=-1;for(let l=0;l<s.length;l++){const w=s.at(l);if(!I(w,n))continue;const h=y(w,n),d=a<0||(o<0?y(w,s.at(a))>0:y(w,s.at(a))<0);(o<0?h<0:h>0)&&d&&(a=l)}return a}const _=(n,t,e)=>{const o=(c,f)=>c===void 0||c instanceof k?f:O(e.calc.scalar(c,e.frame)),r=o(n,0);if(r instanceof u)return r;const s=o(t,1);return s instanceof u?s:[r,s]};A("XLOOKUP","vrr?rvv",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=R(n[2],t);if(r instanceof u)return r;const s=_(n[4],n[5],t);if(s instanceof u)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 k?p.NA:t.calc.flat(i)}if(o.cols===1){if(r.cols===1)return r.at(c,0);const i=[];for(let a=0;a<r.cols;a++)i.push(r.at(c,a));return new M(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 M(r.rows,1,f)}),A("XMATCH","vr?vv",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=_(n[2],n[3],t);if(r instanceof u)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=d=>{if(d===void 0||d instanceof k)return 0;const m=O(t.calc.scalar(d,t.frame));return m instanceof u?m:Math.trunc(m)};if(e instanceof F){const d=o(n[3]??1);if(d instanceof u)return d;const m=e.parts[d-1];if(!m)return p.REF;e=m}if(e instanceof u)return e;const r=o(n[1]);if(r instanceof u)return r;const s=o(n[2]);if(s instanceof u)return s;if(r<0||s<0)return p.VALUE;if(e instanceof x){const d=n[2]===void 0&&e.rows===1,m=d?1:r,E=d?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 u)return c;const f=n[2]===void 0&&c.rows===1,i=f?1:r,a=f?r:s===0&&c.cols===1?1:s;if(i>c.rows||a>c.cols)return p.REF;if(i>0&&a>0)return c.at(i-1,a-1);const l=i===0?c.rows:1,w=a===0?c.cols:1,h=[];for(let d=0;d<l;d++)for(let m=0;m<w;m++)h.push(c.at(i===0?d:i-1,a===0?m:a-1));return new M(l,w,h)}),A("OFFSET","rvv?vv",(n,t)=>{const e=n[0];if(e instanceof u)return e;if(!(e instanceof x))return p.VALUE;const o=(b,B)=>{if(b===void 0||b instanceof k)return B;const S=O(t.calc.scalar(b,t.frame));return S instanceof u?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 u)return b;const[i,a,l,w]=[r,s,c,f];if(l===0||w===0)return p.REF;const h=e.top+(i??0),d=e.left+(a??0),m=h+(l??1)-1,E=d+(w??1)-1;return Math.min(h,m)<0||Math.min(d,E)<0||Math.max(h,m)>=L||Math.max(d,E)>=W?p.REF:new x(e.sheet,Math.min(h,m),Math.min(d,E),Math.max(h,m),Math.max(d,E))},{volatile:!0});const on=/^(?:(.*)!)?R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/iu,v=(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,a={row:v(s,t),col:v(c,e)};if(a.row<0||a.col<0)return;const l=K(a),w=r===void 0?"":`${r}!`;if(f===void 0&&i===void 0)return`${w}${l}`;const h={row:v(f,t),col:v(i,e)};return h.row<0||h.col<0?void 0:`${w}${l}:${K(h)}`}A("INDIRECT","v?v",(n,t)=>{const e=H(t.calc.scalar(n[0],t.frame));if(e instanceof u)return e;const o=n[1]===void 0?!0:U(t.calc.scalar(n[1],t.frame));if(o instanceof u)return o;const r=o?e:sn(e,t.frame.row,t.frame.col);if(r===void 0)return p.REF;const s=J(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 k)return t+1;if(n instanceof u)return n;if(n instanceof M)return new M(n.rows,n.cols,n.data.map(c=>c instanceof u?c:p.VALUE));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 M(r,1,s):new M(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 u?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 u?o:o.cols}),A("AREAS","r",n=>{const t=n[0];return t instanceof x?1:t instanceof F?t.parts.length:p.VALUE}),Y("ADDRESS",2,5,(n,t,e,o,r)=>{const s=O(n),c=O(t),f=e==null?1:O(e);for(const b of[s,c,f])if(b instanceof u)return b;const[i,a,l]=[s,c,f];if((i??0)<1||(a??0)<1||(i??0)>L||(a??0)>W)return p.VALUE;const w=o==null?!0:U(o);if(w instanceof u)return w;const h=l===1||l===2,d=l===1||l===3,m=w?`${d?"$":""}${q((a??1)-1)}${h?"$":""}${String(i)}`:`R${h?String(i):`[${String(i)}]`}C${d?String(a):`[${String(a)}]`}`;if(r==null)return m;const E=H(r);return E instanceof u?E:`${G(E)}${m}`}),A("TRANSPOSE","r",(n,t)=>{const e=R(n[0],t);if(e instanceof u)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 M(e.cols,e.rows,o)}),Q("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{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);
2
+ import{format as $,isValidFormat as F}from"numfmt";import{CellError as i,ERR as E}from"../../values.js";import{numberOfText as N,toBool as b,toNumber as p,toText as u}from"../coerce.js";import{Missing as O}from"../types.js";import{alias as H,cellsOf as S,eachRow as x,many as B,number as I,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?E.VALUE:Math.trunc(t)},y=n=>G(n,r=>r.toUpperCase()),w=_;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 E.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(w)),f("UPPER",1,1,g(y)),f("PROPER",1,1,g(n=>w(n).replace(/(^|[^\p{L}])(\p{L})/gu,(r,t,e)=>`${t}${y(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?E.VALUE:t.repeat(e)}),f("CHAR",1,1,I(n=>{const r=Math.trunc(n);return r<1||r>255?E.VALUE:W(r)})),f("UNICHAR",1,1,I(n=>{const r=Math.trunc(n);return r<1||r>1114111?E.VALUE:String.fromCodePoint(r)})),f("CODE",1,1,g(n=>n===""?E.VALUE:n.charCodeAt(0))),f("UNICODE",1,1,g(n=>n.codePointAt(0)??E.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 x(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===void 0||e instanceof O?!0:e,r.frame));if(l instanceof i)return l;const d=[];let a;if(x(s,r,h=>{if(a)return;const A=u(h);A instanceof i?a=A:(!l||A!=="")&&d.push(A)}),a)return a;let L=d[0]??"";for(let h=1;h<d.length;h++)L+=`${c.length>0?c[(h-1)%c.length]??"":""}${d[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 E.VALUE;let d=-1;for(let a=0;a<Math.trunc(l);a++)if(d=s.indexOf(o,d+1),d<0)return s;return`${s.slice(0,d)}${c}${s.slice(d+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 E.VALUE;const d=Math.trunc(o)-1;return`${s.slice(0,d)}${l}${s.slice(d+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 E.VALUE;const c=s.indexOf(e,Math.trunc(o)-1);return c<0?E.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 E.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?E.VALUE:h+1}}const d=j(`${e}*`);for(let a=c;a<=s.length;a++)if(d(s.slice(a)))return a+1;return E.VALUE}),f("VALUE",1,1,n=>typeof n=="number"?n:n==null?0:typeof n!="string"?E.VALUE:N(n)??E.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,d=Number(c.slice(0,c.length-l));return Number.isNaN(d)?E.VALUE:d/100**l}),f("TEXT",2,2,(n,r)=>{const t=u(r);if(t instanceof i)return t;if(!F(t))return E.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 E.VALUE}});function v(n,r,t){const e=t?"#,##0":"0",s=r>0?`.${"0".repeat(r)}`:"";return $(`${e}${s}`,n)}const P=(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 v(P(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=v(Math.abs(P(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 D(n){return(r,t)=>{const[e,s,o,c,l,d]=r.map(U=>U instanceof O?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 E.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):d===void 0?E.NA:d}}M("TEXTBEFORE","vr?vvvv",D("before")),M("TEXTAFTER","vr?vvvv",D("after")),f("HYPERLINK",1,2,(n,r)=>r===void 0?n??"":r),f("ENCODEURL",1,1,g(n=>encodeURIComponent(n)));for(const n of["LEN","LEFT","RIGHT","MID","FIND","SEARCH","REPLACE"])H(`${n}B`,n);
@@ -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 N,MAX_ROWS as d,columnIndex as p}from"../address.js";import{errorOf as R}from"../values.js";import{TABLE_SECTIONS as C}from"./ast.js";class g extends Error{at;constructor(t,s){super(t),this.at=s}}const E=new Set(["=","<>","<","<=",">",">="]),w={"=":1,"<>":1,"<":1,"<=":1,">":1,">=":1,"&":2,"+":3,"-":3,"*":4,"/":4,"^":5},x=i=>i>="0"&&i<="9",m=i=>/[\p{L}_\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",A=i=>/[\p{L}\p{N}\p{M}_.?\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",u=(i,t)=>({index:i,abs:t});function f(i,t){const s={};return t.before&&(s.before=t.before),t.inner&&(s.inner=t.inner),t.after&&(s.after=t.after),Object.keys(s).length===0?i:{...i,space:{...i.space,...s}}}class L{at=0;s;constructor(t){this.s=t}blanks(){return this.space()!==""}space(){const t=this.at;for(;/\s/u.test(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}peek(t){return this.s.startsWith(t,this.at)}eat(t){return this.peek(t)?(this.at+=t.length,!0):!1}stop(t,s=this.at){throw new g(t,s)}parse(){this.eat("="),this.s.slice(this.at).trim()===""&&this.stop("a formula with nothing in it",this.s.length);const t=this.expression(0);return this.blanks(),this.at<this.s.length&&this.stop(`"${this.s.charAt(this.at)}" where an operator should be`),t}operator(){const t=this.at,s=this.space(),e=this.s.slice(this.at,this.at+2),n=this.s.charAt(this.at);return this.at=t,E.has(e)?{op:e,inner:s}:w[n]===void 0?void 0:{op:n,inner:s}}expression(t){let s=this.prefix();for(;;){const e=this.operator();if(e===void 0)return s;const{op:n,inner:r}=e,h=w[n]??0;if(h<=t)return s;this.at+=r.length+n.length;const a=this.expression(h);s=f({kind:"binary",op:n,left:s,right:a},{inner:r})}}prefix(){const t=this.at,s=this.space();for(const e of["-","+","@"])if(this.eat(e))return f({kind:"unary",op:e,arg:this.prefix()},{before:s});return this.at=t,this.postfix()}postfix(){let t=this.references();for(;;){const s=this.at;if(this.blanks(),this.eat("%")){t={kind:"postfix",op:"%",arg:t};continue}if(this.at=s,this.peek("#")&&!$(this.s,this.at)){this.at+=1,t={kind:"postfix",op:"#",arg:t};continue}if(this.peek("(")&&(t.kind==="call"||t.kind==="paren")){const{args:e}=this.call("");t={kind:"invoke",callee:t,args:e};continue}return t}}references(){let t=this.primary();for(;;){if(this.peek(":")){this.at+=1,this.blanks();const e=this.primary();t={kind:"binary",op:":",left:t,right:e};continue}const s=this.at;if(S(t)&&this.blanks()&&this.startsReference()){const e=this.primary();t={kind:"binary",op:" ",left:t,right:e};continue}return this.at=s,t}}startsReference(){const t=this.s.charAt(this.at);return t==="$"||t==="'"||t==="("||t==="["||m(t)}primary(){const t=this.space();return f(this.bare(),{before:t})}bare(){const t=this.s.charAt(this.at);if(t===""&&this.stop("the formula ends where a value should be"),t==="(")return this.parenthesis();if(t==="{")return this.arrayConstant();if(t==='"')return{kind:"string",value:this.text()};if(x(t)||t==="."){const s=k(this.s,this.at);return s?(this.at=s.end,{kind:"ref",...s.ref}):this.number()}return t==="#"?this.errorLiteral(void 0):t==="["?this.bracket(void 0):t==="$"||t==="'"||m(t)?this.word():this.stop(`"${t}" where a value should be`)}parenthesis(){const t=this.at;this.at+=1;let s=this.expression(0),e=this.space();for(;this.eat(",");){const n=this.expression(0);s=f({kind:"binary",op:",",left:s,right:n},{inner:e}),e=this.space()}return this.eat(")")||this.stop("a ( that is never closed",t),f({kind:"paren",arg:s},{inner:e})}number(){const t=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/u.exec(this.s.slice(this.at));return t?(this.at+=t[0].length,{kind:"number",value:Number(t[0]),raw:t[0]}):this.stop("a number that is not one")}text(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop('a " that is never closed',t);const e=this.s.charAt(this.at);if(e==='"'){if(this.s.charAt(this.at+1)!=='"')return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}errorLiteral(t){const s=$(this.s,this.at);return s===void 0?this.stop("a # that names no error"):(this.at+=s.length,{kind:"error",code:s,...t===void 0?{}:{sheet:t}})}arrayConstant(){const t=this.at;this.at+=1;const s=[[]];for(;;)if(this.blanks(),s[s.length-1]?.push(this.arrayItem()),this.blanks(),!this.eat(",")){if(this.eat(";")){s.push([]);continue}if(this.eat("}"))break;this.stop("a { that is never closed",t)}const e=s[0]?.length;return s.some(n=>n.length!==e)&&this.stop("an array whose rows are not all as long",t),{kind:"array",rows:s}}arrayItem(){const t=this.s.charAt(this.at);if(t==='"')return{kind:"string",value:this.text()};if(t==="#")return this.errorLiteral(void 0);if(t==="-"||t==="+")return this.at+=1,{kind:"unary",op:t,arg:this.number()};if(x(t)||t===".")return this.number();const s=this.name(),e=s.toUpperCase();return e==="TRUE"||e==="FALSE"?{kind:"boolean",value:e==="TRUE"}:this.stop(`${s} in an array: an array holds only constants`)}name(){const t=this.at;for(;this.at<this.s.length&&A(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}quoted(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop("a ' that is never closed",t);const e=this.s.charAt(this.at);if(e==="'"){if(this.s.charAt(this.at+1)!=="'")return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}word(){const t=this.sheetPrefix();if(t)return this.afterSheet(t.first,t.last);const s=k(this.s,this.at);if(s)return this.at=s.end,{kind:"ref",...s.ref};const e=y(this.s,this.at);if(e)return this.at=e.end,{kind:"ref",...e.ref};if(this.peek("$"))return this.stop('a "$" that is not part of a reference');const n=this.at,r=this.name();if(r==="")return this.stop(`"${this.s.charAt(n)}" in a name`);if(this.peek("("))return this.call(r);if(this.peek("["))return this.bracket(r);const h=r.toUpperCase();return h==="TRUE"||h==="FALSE"?{kind:"boolean",value:h==="TRUE"}:{kind:"name",name:r}}sheetPrefix(){const t=this.at;if(this.peek("'")){const e=this.quoted();if(!this.eat("!")){this.at=t;return}const n=e.indexOf(":");return n>0?{first:e.slice(0,n),last:e.slice(n+1)}:{first:e}}const s=this.name();if(s!==""&&this.eat("!"))return{first:s};if(s!==""&&this.eat(":")){const e=this.name();if(e!==""&&this.eat("!"))return{first:s,last:e}}this.at=t}afterSheet(t,s){const e={sheet:t,...s===void 0?{}:{lastSheet:s}};if(this.peek("#"))return this.errorLiteral(t);const n=k(this.s,this.at);if(n)return this.at=n.end,{kind:"ref",...e,...n.ref};const r=y(this.s,this.at);if(r)return this.at=r.end,{kind:"ref",...e,...r.ref};const h=this.name();return h===""?this.stop(`nothing after ${t}!`):{kind:"name",name:h,sheet:t}}call(t){const s=this.at;this.at+=1;const e=[],n=a=>f({kind:"call",name:t.toUpperCase(),written:t,args:e},{inner:a}),r=this.at,h=this.space();if(this.eat(")"))return n(h);for(this.at=r;;){const a=this.at,c=this.space();let o;this.peek(",")||this.peek(")")?o=f({kind:"missing"},{before:c}):(this.at=a,o=this.expression(0));const b=this.space();if(this.eat(",")){e.push(f(o,{after:b}));continue}if(e.push(o),this.eat(")"))return n(b);this.stop(`a ( of ${t.toUpperCase()} that is never closed`,s)}}columnName(){let t="";for(;;){this.at>=this.s.length&&this.stop("a [ that is never closed");const s=this.s.charAt(this.at);if(s==="]")return t;if(s==="["&&this.stop("a [ inside a column's name"),s==="'"){this.at+=1,t+=this.s.charAt(this.at),this.at+=1;continue}t+=s,this.at+=1}}bracket(t){const s=this.at;this.at+=1;const e={kind:"structured",...t===void 0?{}:{table:t},sections:[],columns:[],thisRow:!1};if(this.eat("]"))return e;if(!(this.peek("[")||this.peek("#")||this.peek("@"))){const r=this.columnName();return this.at+=1,t===void 0&&/[!']/u.test(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),t===void 0&&m(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),e.columns=[r.trim()],e}return this.eat("@")?(e.thisRow=!0,this.eat("]")||(this.peek("[")?this.items(e):e.columns=[this.columnName().trim()],this.eat("]")||this.stop("a [ that is never closed",s)),e):(this.items(e),this.eat("]")||this.stop("a [ that is never closed",s),e)}items(t){for(;;){if(this.blanks(),this.peek("#"))this.section(t);else if(this.eat("["))if(this.peek("#"))this.section(t),this.eat("]")||this.stop("a [ that is never closed");else{const s=this.columnName().trim();this.at+=1,t.columns.push(s),this.eat(":")&&(this.eat("[")||this.stop("a column's name after :"),t.columns.push(this.columnName().trim()),this.at+=1)}else this.stop("a column or a section of a table");if(this.blanks(),!this.eat(","))return}}section(t){const s=this.s.slice(this.at).toLowerCase(),e=C.find(n=>s.startsWith(n.toLowerCase()));e===void 0&&this.stop("a section of a table that is not one"),this.at+=e.length,e==="#This Row"?t.thisRow=!0:t.sections.push(e)}}const S=i=>i.kind==="ref"||i.kind==="name"||i.kind==="structured"||i.kind==="call"||i.kind==="paren"||i.kind==="binary"&&(i.op===":"||i.op===" ");function $(i,t){const s=/^#[A-Za-z0-9_/]+[!?]?/u.exec(i.slice(t));return s?R(s[0])?.code:void 0}const l=(i,t)=>{const s=i.charAt(t);return s!==""&&(A(s)||s==="("||s==="!"||s==="[")};function y(i,t){const s=i.slice(t),e=/^(\$?)([A-Za-z]{1,3}):(\$?)([A-Za-z]{1,3})/u.exec(s);if(e){const a=t+e[0].length,c=p(e[2]??""),o=p(e[4]??"");if(c!==void 0&&o!==void 0&&!l(i,a))return{ref:{col:u(c,e[1]==="$"),col2:u(o,e[3]==="$"),range:!0},end:a}}const n=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(s);if(!n)return;const r=v(n),h=t+n[0].length;if(!(!r||l(i,h))){if(i.charAt(h)===":"){const a=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(i.slice(h+1));if(a){const c=v(a),o=h+1+a[0].length;if(c&&!l(i,o))return{ref:{...r,col2:c.col,row2:c.row,range:!0},end:o}}}return{ref:{...r,range:!1},end:h}}}function v(i){const t=p(i[2]??""),s=Number(i[4])-1;if(!(t===void 0||t>=N||s>=d))return{col:u(t,i[1]==="$"),row:u(s,i[3]==="$")}}function k(i,t){const s=/^(\$?)([1-9]\d*):(\$?)([1-9]\d*)/u.exec(i.slice(t));if(!s)return;const e=t+s[0].length,n=Number(s[2])-1,r=Number(s[4])-1;if(!(n>=d||r>=d||l(i,e)))return{ref:{row:u(n,s[1]==="$"),row2:u(r,s[3]==="$"),range:!0},end:e}}export function parseFormula(i){try{return{ok:!0,node:new L(i).parse()}}catch(t){if(t instanceof g)return{ok:!1,error:{message:t.message,at:t.at}};throw t}}
2
+ import{MAX_COLUMNS as L,MAX_ROWS as p,columnIndex as m}from"../address.js";import{errorOf as R}from"../values.js";import{TABLE_SECTIONS as E}from"./ast.js";class x extends Error{at;constructor(t,s){super(t),this.at=s}}const S=new Set(["=","<>","<","<=",">",">="]),A={"=":1,"<>":1,"<":1,"<=":1,">":1,">=":1,"&":2,"+":3,"-":3,"*":4,"/":4,"^":5},$=i=>i>="0"&&i<="9",k=i=>/[\p{L}_\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",y=i=>/[\p{L}\p{N}\p{M}_.?\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",l=(i,t)=>({index:i,abs:t});function f(i,t){const s={};return t.before&&(s.before=t.before),t.inner&&(s.inner=t.inner),t.after&&(s.after=t.after),Object.keys(s).length===0?i:{...i,space:{...i.space,...s}}}class O{at=0;s;locals=[];constructor(t){this.s=t}blanks(){return this.space()!==""}space(){const t=this.at;for(;/\s/u.test(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}peek(t){return this.s.startsWith(t,this.at)}eat(t){return this.peek(t)?(this.at+=t.length,!0):!1}stop(t,s=this.at){throw new x(t,s)}parse(){this.eat("="),this.s.slice(this.at).trim()===""&&this.stop("a formula with nothing in it",this.s.length);const t=this.expression(0);return this.blanks(),this.at<this.s.length&&this.stop(`"${this.s.charAt(this.at)}" where an operator should be`),t}operator(){const t=this.at,s=this.space(),e=this.s.slice(this.at,this.at+2),n=this.s.charAt(this.at);return this.at=t,S.has(e)?{op:e,inner:s}:A[n]===void 0?void 0:{op:n,inner:s}}expression(t){let s=this.prefix();for(;;){const e=this.operator();if(e===void 0)return s;const{op:n,inner:r}=e,h=A[n]??0;if(h<=t)return s;this.at+=r.length+n.length;const a=this.expression(h);s=f({kind:"binary",op:n,left:s,right:a},{inner:r})}}prefix(){const t=this.at,s=this.space();for(const e of["-","+","@"])if(this.eat(e))return f({kind:"unary",op:e,arg:this.prefix()},{before:s});return this.at=t,this.postfix()}postfix(){let t=this.references();for(;;){const s=this.at;if(this.blanks(),this.eat("%")){t={kind:"postfix",op:"%",arg:t};continue}if(this.at=s,this.peek("#")&&!b(this.s,this.at)){this.at+=1,t={kind:"postfix",op:"#",arg:t};continue}if(this.peek("(")&&(t.kind==="call"||t.kind==="paren")){const{args:e}=this.call("");t={kind:"invoke",callee:t,args:e};continue}return t}}spilled(){const t=this.primary();return!this.peek("#")||b(this.s,this.at)?t:(this.at+=1,{kind:"postfix",op:"#",arg:t})}references(){let t=this.spilled();for(;;){if(this.peek(":")){this.at+=1,this.blanks();const e=this.spilled();t={kind:"binary",op:":",left:t,right:e};continue}const s=this.at;if(T(t)&&this.blanks()&&this.startsReference()){const e=this.primary();t={kind:"binary",op:" ",left:t,right:e};continue}return this.at=s,t}}startsReference(){const t=this.s.charAt(this.at);return t==="$"||t==="'"||t==="("||t==="["||k(t)}primary(){const t=this.space();return f(this.bare(),{before:t})}bare(){const t=this.s.charAt(this.at);if(t===""&&this.stop("the formula ends where a value should be"),t==="(")return this.parenthesis();if(t==="{")return this.arrayConstant();if(t==='"')return{kind:"string",value:this.text()};if($(t)||t==="."){const s=g(this.s,this.at);return s?(this.at=s.end,{kind:"ref",...s.ref}):this.number()}return t==="#"?this.errorLiteral(void 0):t==="["?this.bracket(void 0):t==="$"||t==="'"||k(t)?this.word():this.stop(`"${t}" where a value should be`)}parenthesis(){const t=this.at;this.at+=1;let s=this.expression(0),e=this.space();for(;this.eat(",");){const n=this.expression(0);s=f({kind:"binary",op:",",left:s,right:n},{inner:e}),e=this.space()}return this.eat(")")||this.stop("a ( that is never closed",t),f({kind:"paren",arg:s},{inner:e})}number(){const t=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/u.exec(this.s.slice(this.at));return t?(this.at+=t[0].length,{kind:"number",value:Number(t[0]),raw:t[0]}):this.stop("a number that is not one")}text(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop('a " that is never closed',t);const e=this.s.charAt(this.at);if(e==='"'){if(this.s.charAt(this.at+1)!=='"')return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}errorLiteral(t){const s=b(this.s,this.at);return s===void 0?this.stop("a # that names no error"):(this.at+=s.length,{kind:"error",code:s,...t===void 0?{}:{sheet:t}})}arrayConstant(){const t=this.at;this.at+=1;const s=[[]];for(;;)if(this.blanks(),s[s.length-1]?.push(this.arrayItem()),this.blanks(),!this.eat(",")){if(this.eat(";")){s.push([]);continue}if(this.eat("}"))break;this.stop("a { that is never closed",t)}const e=s[0]?.length;return s.some(n=>n.length!==e)&&this.stop("an array whose rows are not all as long",t),{kind:"array",rows:s}}arrayItem(){const t=this.s.charAt(this.at);if(t==='"')return{kind:"string",value:this.text()};if(t==="#")return this.errorLiteral(void 0);if(t==="-"||t==="+")return this.at+=1,{kind:"unary",op:t,arg:this.number()};if($(t)||t===".")return this.number();const s=this.name(),e=s.toUpperCase();return e==="TRUE"||e==="FALSE"?{kind:"boolean",value:e==="TRUE"}:this.stop(`${s} in an array: an array holds only constants`)}name(){const t=this.at;for(;this.at<this.s.length&&y(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}quoted(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop("a ' that is never closed",t);const e=this.s.charAt(this.at);if(e==="'"){if(this.s.charAt(this.at+1)!=="'")return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}bound(t){const s=t.toLowerCase();return this.locals.some(e=>e.has(s))}word(){const t=this.sheetPrefix();if(t)return this.afterSheet(t.first,t.last);const s=g(this.s,this.at);if(s)return this.at=s.end,{kind:"ref",...s.ref};const e=/^([A-Za-z]{1,3}):([A-Za-z]{1,3})/u.exec(this.s.slice(this.at)),r=e!==null&&(this.bound(e[1]??"")||this.bound(e[2]??""))?void 0:N(this.s,this.at);if(r)return this.at=r.end,{kind:"ref",...r.ref};if(this.peek("$"))return this.stop('a "$" that is not part of a reference');const h=this.at,a=this.name();if(a==="")return this.stop(`"${this.s.charAt(h)}" in a name`);if(this.peek("("))return this.call(a);if(this.peek("["))return this.bracket(a);const o=a.toUpperCase();return o==="TRUE"||o==="FALSE"?{kind:"boolean",value:o==="TRUE"}:{kind:"name",name:a}}sheetPrefix(){const t=this.at;if(this.peek("'")){const e=this.quoted();if(!this.eat("!")){this.at=t;return}const n=e.indexOf(":");return n>0?{first:e.slice(0,n),last:e.slice(n+1)}:{first:e}}const s=this.name();if(s!==""&&this.eat("!"))return{first:s};if(s!==""&&this.eat(":")){const e=this.name();if(e!==""&&this.eat("!"))return{first:s,last:e}}this.at=t}afterSheet(t,s){const e={sheet:t,...s===void 0?{}:{lastSheet:s}};if(this.peek("#"))return this.errorLiteral(t);const n=g(this.s,this.at);if(n)return this.at=n.end,{kind:"ref",...e,...n.ref};const r=N(this.s,this.at);if(r)return this.at=r.end,{kind:"ref",...e,...r.ref};const h=this.name();return h===""?this.stop(`nothing after ${t}!`):{kind:"name",name:h,sheet:t}}call(t){const s=this.at;this.at+=1;const e=[],n=c=>f({kind:"call",name:t.toUpperCase(),written:t,args:e},{inner:c}),r=this.at,h=this.space();if(this.eat(")"))return n(h);this.at=r;const a=U.test(t)?new Set:void 0;a&&this.locals.push(a);const o=c=>(a&&this.locals.pop(),n(c));for(;;){const c=this.at,C=this.space();let u;this.peek(",")||this.peek(")")?u=f({kind:"missing"},{before:C}):(this.at=c,u=this.expression(0));const w=this.space();if(a&&u.kind==="name"&&u.sheet===void 0&&a.add(u.name.replace(/^_xlpm\./iu,"").toLowerCase()),this.eat(",")){e.push(f(u,{after:w}));continue}if(e.push(u),this.eat(")"))return o(w);this.stop(`a ( of ${t.toUpperCase()} that is never closed`,s)}}columnName(){let t="";for(;;){this.at>=this.s.length&&this.stop("a [ that is never closed");const s=this.s.charAt(this.at);if(s==="]")return t;if(s==="["&&this.stop("a [ inside a column's name"),s==="'"){this.at+=1,t+=this.s.charAt(this.at),this.at+=1;continue}t+=s,this.at+=1}}bracket(t){const s=this.at;this.at+=1;const e={kind:"structured",...t===void 0?{}:{table:t},sections:[],columns:[],thisRow:!1};if(this.eat("]"))return e;if(!(this.peek("[")||this.peek("#")||this.peek("@"))){const r=this.columnName();return this.at+=1,t===void 0&&/[!']/u.test(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),t===void 0&&k(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),e.columns=[r.trim()],e}return this.eat("@")?(e.thisRow=!0,this.eat("]")||(this.peek("[")?this.items(e):e.columns=[this.columnName().trim()],this.eat("]")||this.stop("a [ that is never closed",s)),e):(this.items(e),this.eat("]")||this.stop("a [ that is never closed",s),e)}items(t){for(;;){if(this.blanks(),this.peek("#"))this.section(t);else if(this.eat("["))if(this.peek("#"))this.section(t),this.eat("]")||this.stop("a [ that is never closed");else{const s=this.columnName().trim();this.at+=1,t.columns.push(s),this.eat(":")&&(this.eat("[")||this.stop("a column's name after :"),t.columns.push(this.columnName().trim()),this.at+=1)}else this.stop("a column or a section of a table");if(this.blanks(),!this.eat(","))return}}section(t){const s=this.s.slice(this.at).toLowerCase(),e=E.find(n=>s.startsWith(n.toLowerCase()));e===void 0&&this.stop("a section of a table that is not one"),this.at+=e.length,e==="#This Row"?t.thisRow=!0:t.sections.push(e)}}const U=/^(?:_xlfn\.)?(?:LET|LAMBDA)$/iu,T=i=>i.kind==="ref"||i.kind==="name"||i.kind==="structured"||i.kind==="call"||i.kind==="paren"||i.kind==="binary"&&(i.op===":"||i.op===" ");function b(i,t){const s=/^#[A-Za-z0-9_/]+[!?]?/u.exec(i.slice(t));return s?R(s[0])?.code:void 0}const d=(i,t)=>{const s=i.charAt(t);return s!==""&&(y(s)||s==="("||s==="!"||s==="[")};function N(i,t){const s=i.slice(t),e=/^(\$?)([A-Za-z]{1,3}):(\$?)([A-Za-z]{1,3})/u.exec(s);if(e){const a=t+e[0].length,o=m(e[2]??""),c=m(e[4]??"");if(o!==void 0&&c!==void 0&&!d(i,a))return{ref:{col:l(o,e[1]==="$"),col2:l(c,e[3]==="$"),range:!0},end:a}}const n=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(s);if(!n)return;const r=v(n),h=t+n[0].length;if(!(!r||d(i,h))){if(i.charAt(h)===":"){const a=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(i.slice(h+1));if(a){const o=v(a),c=h+1+a[0].length;if(o&&!d(i,c))return{ref:{...r,col2:o.col,row2:o.row,range:!0},end:c}}}return{ref:{...r,range:!1},end:h}}}function v(i){const t=m(i[2]??""),s=Number(i[4])-1;if(!(t===void 0||t>=L||s>=p))return{col:l(t,i[1]==="$"),row:l(s,i[3]==="$")}}function g(i,t){const s=/^(\$?)([1-9]\d*):(\$?)([1-9]\d*)/u.exec(i.slice(t));if(!s)return;const e=t+s[0].length,n=Number(s[2])-1,r=Number(s[4])-1;if(!(n>=p||r>=p||d(i,e)))return{ref:{row:l(n,s[1]==="$"),row2:l(r,s[3]==="$"),range:!0},end:e}}export function parseFormula(i){try{return{ok:!0,node:new O(i).parse()}}catch(t){if(t instanceof x)return{ok:!1,error:{message:t.message,at:t.at}};throw t}}
@@ -29,7 +29,7 @@ export type ReadChart = SheetChart;
29
29
  * whose numbers are in no cell of the workbook is left out, and `said`
30
30
  * says so: written again, its numbers would be in the chart alone.
31
31
  */
32
- export declare function readChart(root: Tag | undefined, said?: string[], has?: (sheet: string) => boolean): ReadChart | string;
32
+ export declare function readChart(root: Tag | undefined, said?: string[], has?: (sheet: string) => boolean, defined?: (name: string) => boolean): ReadChart | string;
33
33
  export interface Anchored {
34
34
  /** Its relationship in the drawing's part. */
35
35
  id: string;
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{CHART_DEFAULTS as ge,chartSpaceXml as Pe}from"@allyhub/chart";import{columnLetter as we}from"../address.js";import{Reference as ye,Union as Se}from"../calc/types.js";import{CellError as Me}from"../values.js";import{categoryNumbers as Fe,chartDef as Re,chartSize as De}from"../chart.js";import{parseFormula as Ne}from"../formula/parse.js";import{sheetPrefix as be}from"../formula/print.js";import{HEAD as _e,attr as b,el as d,flag as ve,numberAttr as E,tag as r,tags as O,textOf as P}from"./xml.js";const je="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing",Ee="http://schemas.openxmlformats.org/drawingml/2006/main",$e="http://schemas.openxmlformats.org/drawingml/2006/chart",Oe="http://schemas.openxmlformats.org/officeDocument/2006/relationships",v=9525,Le=(e,n)=>`${be(e.sheet.name)}$${we(e.left)}$${String(e.top+1)}${e.single?"":`:$${we(e.right)}$${String(n+1)}`}`;function L(e,n,t){if(!t?.node)return;const o=e.eval(t.node,{sheet:n,row:0,col:0,dr:0,dc:0}),s=l=>Le(l,Math.min(l.bottom,Math.max(l.top,l.sheet.lastRow)));if(o instanceof ye)return s(o);if(o instanceof Se)return`(${o.parts.map(s).join(",")})`;const a=t.node;if(a.kind==="name"&&a.sheet!==void 0&&!(o instanceof Me&&o.code==="#NAME?"))return`${be(a.sheet)}${a.name}`}function ze(e,n,t){if(!t?.node)return!1;const o=e.eval(t.node,{sheet:n,row:0,col:0,dr:0,dc:0});return o instanceof ye&&typeof e.value(o.sheet,o.top,o.left)=="number"}export function chartPart(e,n,t){const o=Re(e,n,t),s=t.series.map((c,f)=>({s:c,i:f,values:L(e,n,c.values)})).filter(c=>c.values!==void 0),a={...o,series:s.flatMap(c=>o.series[c.i]??[])},l=ze(e,n,t.categories),h=l?Fe(e,n,t):void 0,m={name:c=>L(e,n,s[c]?.s.name),categories:L(e,n,t.categories),numeric:l,...h===void 0?{}:{categoryNumbers:h.values,categoryFormat:h.format},values:c=>s[c]?.values??"",x:c=>L(e,n,s[c]?.s.x??t.categories)},y=L(e,n,t.title);return Pe(a,{refs:m,...y===void 0?{}:{titleRef:y}})}const Ge=(e,n,t)=>d("xdr:oneCellAnchor",{},d("xdr:from",{},`<xdr:col>${String(e.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round(e.x*v))}</xdr:colOff>`,`<xdr:row>${String(e.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((e.y??0)*v))}</xdr:rowOff>`),d("xdr:ext",{cx:Math.round(e.width*v),cy:Math.round(e.height*v)}),d("xdr:pic",{},d("xdr:nvPicPr",{},d("xdr:cNvPr",{id:n,name:e.name,descr:e.path}),d("xdr:cNvPicPr",{},d("a:picLocks",{noChangeAspect:!0}))),d("xdr:blipFill",{},d("a:blip",{"xmlns:r":Oe,"r:embed":`rId${String(t)}`}),d("a:stretch",{},d("a:fillRect"))),d("xdr:spPr",{},d("a:xfrm",{},d("a:off",{x:0,y:0}),d("a:ext",{cx:Math.round(e.width*v),cy:Math.round(e.height*v)})),d("a:prstGeom",{prst:"rect"},d("a:avLst")))),d("xdr:clientData"));export function drawingPart(e,n=[]){const t=(o,s)=>{const{width:a,height:l}=De(o.spec);return d("xdr:oneCellAnchor",{},d("xdr:from",{},`<xdr:col>${String(o.at.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round((o.spec.x??0)*v))}</xdr:colOff>`,`<xdr:row>${String(o.at.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((o.spec.y??0)*v))}</xdr:rowOff>`),d("xdr:ext",{cx:Math.round(a*v),cy:Math.round(l*v)}),d("xdr:graphicFrame",{macro:""},d("xdr:nvGraphicFramePr",{},d("xdr:cNvPr",{id:s+2,name:`Chart ${String(s+1)}`}),d("xdr:cNvGraphicFramePr")),d("xdr:xfrm",{},d("a:off",{x:0,y:0}),d("a:ext",{cx:0,cy:0})),d("a:graphic",{},d("a:graphicData",{uri:$e},d("c:chart",{"xmlns:c":$e,"xmlns:r":Oe,"r:id":`rId${String(s+1)}`})))),d("xdr:clientData"))};return _e+d("xdr:wsDr",{"xmlns:xdr":je,"xmlns:a":Ee},...e.map(t),...n.map((o,s)=>Ge(o,e.length+s+2,e.length+s+1)))}function ne(e){if(Array.isArray(e))return e.map(ne).join("");if(typeof e!="object"||e===null)return"";const n=e,t=[];for(const[o,s]of Object.entries(n))o==="t"?t.push(P(s)):typeof s=="object"&&t.push(ne(s));return t.join("")}const te=e=>{const n=r(e?.solidFill),t=b(r(n?.srgbClr),"val");return t!==void 0?`#${t.toUpperCase()}`:Ie[b(r(n?.schemeClr),"val")??""]},Ie={accent1:"#4472C4",accent2:"#ED7D31",accent3:"#A5A5A5",accent4:"#FFC000",accent5:"#5B9BD5",accent6:"#70AD47"},A=(e,n)=>b(r(e?.[n]),"val"),M=(e,n)=>E(r(e?.[n]),"val"),z=(e,n)=>ve(r(e?.[n]),"val"),Te=e=>/^"(?:[^"]|"")*"$/u.test(e)?e.slice(1,-1).replaceAll('""','"'):void 0,Ae=e=>Te(e)??`=${e}`;function Ce(e){if(!e)return;const n=r(e.tx),t=r(n?.strRef);if(t)return Ae(P(t.f));const o=ne(n?.rich);return o===""?void 0:o}function re(e){if(!e)return;const n=r(e.scaling),t=r(e.numFmt),o=b(t,"formatCode"),s={},a=Ce(r(e.title));a!==void 0&&(s.title=a);const l=M(n,"min"),h=M(n,"max"),m=M(e,"majorUnit");return l!==void 0&&(s.min=l),h!==void 0&&(s.max=h),m!==void 0&&m>0&&(s.major=m),z(e,"delete")===!0&&(s.visible=!1),A(n,"orientation")==="maxMin"&&(s.reverse=!0),o!==void 0&&o!=="General"&&ve(t,"sourceLinked")!==!0&&(s.format=o),s}const G=e=>{const n=r(e?.numRef)??r(e?.strRef),t=n?P(n.f):"";return t===""?void 0:t};function We(e,n){const t=G(r(e[n?"yVal":"val"]));if(t===void 0)return"its numbers are written in the chart";const o={values:t},s=r(e.tx),a=G(s);if(a!==void 0)o.name=Ae(a);else{const u=P(s?.v);u!==""&&(o.name=u)}const l=r(e.spPr),h=r(l?.ln),m=r(e.marker),y=n||m!==void 0,c=te(l)??(y?te(h)??te(r(m?.spPr)):void 0);c!==void 0&&(o.color=c);const f=E(h,"w");if(y&&f!==void 0&&h?.noFill===void 0){const u=Math.max(.25,Number((f/12700).toFixed(2)));u!==ge.lineWidth&&(o.line_width=u)}z(e,"smooth")===!0&&(o.smooth=!0);const p=M(m,"size");p!==void 0&&p!==ge.marker&&(o.marker_size=p);const $=n?G(r(e.xVal)):void 0;$!==void 0&&(o.x=$);const g=n?void 0:G(r(e.cat));return{series:o,...g===void 0?{}:{categories:g}}}function oe(e,n){const t=O(n.ser),o=a=>r(r(a.spPr)?.ln)?.noFill!==void 0,s=a=>A(r(a.marker),"symbol")==="none";switch(e){case"barChart":return Ue(n);case"lineChart":return t.every(s)?"line":"line-markers";case"areaChart":return Be(n);case"pieChart":return"pie";case"doughnutChart":return"doughnut";case"scatterChart":return t.every(o)?"scatter":t.some(a=>z(a,"smooth")===!0)?"scatter-smooth":"scatter-lines";default:return}}function Ue(e){const n=A(e,"barDir")==="bar"?"bar":"column",t=A(e,"grouping");return t==="stacked"?`${n}-stacked`:t==="percentStacked"?`${n}-stacked-100`:`${n}-clustered`}function Be(e){const n=A(e,"grouping");return n==="stacked"?"area-stacked":n==="percentStacked"?"area-stacked-100":"area"}function I(e,n){if(e===void 0)return;const t=e.replace(/^=/u,"");if(/(?:^|[(,])'?\[\d+\]/u.test(t))return"it reads another workbook";if(e.includes("#REF!"))return"its cells were deleted (#REF!)";const o=/^(?:'((?:[^']|'')+)'|([^!'(),]+))!/u.exec(t),s=o?.[1]?.replaceAll("''","'")??o?.[2];if(s!==void 0&&!n(s))return`it reads a sheet the workbook does not have (${s})`;const a=Ne(`=${t}`);if(!a.ok||!T(a.node))return`it names no cells (${t})`}function T(e){switch(e.kind){case"ref":case"name":case"structured":return!0;case"paren":return T(e.arg);case"binary":return(e.op===","||e.op===":")&&T(e.left)&&T(e.right);default:return!1}}export function readChart(e,n=[],t=()=>!0){const o=r(e?.chartSpace),s=r(o?.chart),a=r(s?.plotArea);if(!s||!a)return"it has no plot";const l=Object.entries(a).filter(([i])=>i.endsWith("Chart")).flatMap(([i,x])=>O(x).map(k=>({kind:i,plot:k}))),[h]=l;if(!h)return"it has no plot";const m=O(a.valAx),y=i=>O(i.axId).map(x=>b(x,"val")),c=i=>{const x=m.find(k=>y(i).includes(b(r(k.axId),"val")));return x!==void 0&&A(x,"axPos")==="r"},f=l.length>1,p=l.map(({kind:i,plot:x})=>({kind:i,plot:x,type:oe(i,x),secondary:f&&c(x)}));if(f&&p.some(i=>i.type===void 0||!(i.type.startsWith("column")||i.type.startsWith("line")||i.type.startsWith("area"))))return"it combines several kinds of chart, not columns, lines and areas";const $=p.find(i=>!i.secondary)??p[0]??h,{kind:g,plot:u}=$,F=g==="scatterChart",S=[];let R,D;const N=p.flatMap(i=>O(i.plot.ser).map(x=>({s:x,of:i}))),U=N.filter(({s:i})=>i.trendline!==void 0).length;U>0&&n.push(U===1?"trendline":`trendlines of ${String(U)} series`);const B=N.filter(({s:i})=>i.errBars!==void 0).length;B>0&&n.push(B===1?"error bars":`error bars of ${String(B)} series`);for(const[i,{s:x,of:k}]of N.entries()){const w=We(x,F);if(typeof w=="string")return w;if(f){const ee=k.type;ee!==void 0&&ee!==oe(g,u)&&(w.series.type=ee),k.secondary&&(w.series.secondary=!0)}const Z=I(w.series.values,t);if(Z!==void 0){n.push(`series ${String(i+1)}, as ${Z}`),D=Z;continue}w.series.name?.startsWith("=")===!0&&I(w.series.name,t)!==void 0&&delete w.series.name,I(w.series.x,t)!==void 0&&delete w.series.x,S.push(w.series),I(w.categories,t)===void 0&&(R??=w.categories)}if(S.length===0)return D===void 0?"it has no series":`every series ${D.replace(/^it /u,"")}`;const j=oe(g,u);if(!j)return`a sheet has no ${g.replace(/Chart$/u,"")} chart`;const se=Ce(r(s.title)),ie=r(s.legend),V=A(ie,"legendPos"),ce=r(u.dLbls),C=i=>z(ce,i)===!0,ke=C("showVal")||C("showPercent")||C("showCatName")||C("showSerName"),ae=A(ce,"dLblPos"),H=M(u,"gapWidth"),de=M(u,"overlap"),X=M(u,"holeSize"),fe=z(u,"varyColors"),ue=j==="pie"||j==="doughnut",le=i=>y(u).includes(b(r(i.axId),"val")),me=[...O(a.catAx),...O(a.dateAx)],_={value:f?m.find(le)??m[0]:m.find((i,x,k)=>F?x===k.length-1:!0),category:F?m[0]:f?me.find(le)??me[0]:r(a.catAx)??r(a.dateAx),second:p.some(i=>i.secondary)?m.find(i=>A(i,"axPos")==="r"):void 0},q=re(_.value),J=re(_.category);q&&_.value?.majorGridlines===void 0&&(q.gridlines=!1),J&&_.category?.majorGridlines!==void 0&&(J.gridlines=!0);const K=E(r(r(r(r(o?.txPr)?.p)?.pPr)?.defRPr),"sz"),Q=i=>i&&Object.keys(i).length>0?i:void 0,he=Q(q),pe=Q(J),Y=re(_.second);Y&&_.second?.majorGridlines!==void 0&&(Y.gridlines=!0);const xe=Q(Y);return{type:j,at:"A1",...se===void 0?{}:{title:se},...R===void 0?{}:{categories:R},series:ue?S.slice(0,1):S,...ie?{legend:V==="b"?"bottom":V==="t"?"top":V==="l"?"left":"right"}:{},...ke?{labels:{...C("showVal")?{value:!0}:{},...C("showPercent")?{percentage:!0}:{},...C("showCatName")?{category:!0}:{},...C("showSerName")?{series:!0}:{},...ae===void 0?{}:{position:ae}}}:{},...H===void 0||H===150?{}:{gap_width:H},...de===void 0||j.includes("stacked")?{}:{overlap:de},...X===void 0||X===50?{}:{hole_size:X},...fe===!0&&g==="barChart"?{vary_by_categories:!0}:{},...fe===!1&&ue?{vary_by_categories:!1}:{},...he?{value_axis:he}:{},...pe?{category_axis:pe}:{},...xe?{secondary_axis:xe}:{},...K===void 0||K===900?{}:{size:K/100}}}const W=e=>Math.round(e/v);export function chartAnchors(e,n,t){const o=r(e?.wsDr),s=[],a=[];let l=0;const h=c=>({col:Number(P(c?.col)),row:Number(P(c?.row)),colOff:W(Number(P(c?.colOff))),rowOff:W(Number(P(c?.rowOff)))}),m=c=>b(r(r(r(r(c.graphicFrame)?.graphic)?.graphicData)?.chart),"id"),y=(c,f,p,$)=>{const g={at:{row:f.row,col:f.col},...f.colOff>0?{x:f.colOff}:{},...f.rowOff>0?{y:f.rowOff}:{},width:p,height:$},u=r(c.pic),F=b(r(r(u?.blipFill)?.blip),"embed");if(u&&F!==void 0){const R=r(r(u.nvPicPr)?.cNvPr),D=b(R,"name"),N=b(R,"descr");a.push({id:F,...g,...D===void 0?{}:{name:D},...N===void 0?{}:{descr:N}});return}const S=m(c);if(S===void 0){l+=1;return}s.push({id:S,...g})};for(const c of O(o?.oneCellAnchor)){const f=r(c.ext);y(c,h(r(c.from)),W(E(f,"cx")??0),W(E(f,"cy")??0))}for(const c of O(o?.twoCellAnchor)){const f=h(r(c.from)),p=h(r(c.to));let $=p.colOff-f.colOff;for(let u=f.col;u<p.col;u++)$+=n(u);let g=p.rowOff-f.rowOff;for(let u=f.row;u<p.row;u++)g+=t(u);y(c,f,$,g)}return{anchors:s,pictures:a,others:l}}
2
+ import{CHART_DEFAULTS as we,chartSpaceXml as Se}from"@allyhub/chart";import{columnLetter as ye}from"../address.js";import{Reference as be,Union as Me}from"../calc/types.js";import{CellError as Fe}from"../values.js";import{categoryNumbers as Re,chartDef as De,chartSize as Ne}from"../chart.js";import{parseFormula as _e}from"../formula/parse.js";import{sheetPrefix as ve}from"../formula/print.js";import{HEAD as je,attr as v,el as a,flag as $e,numberAttr as E,tag as o,tags as A,textOf as S}from"./xml.js";const Ee="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing",Le="http://schemas.openxmlformats.org/drawingml/2006/main",Oe="http://schemas.openxmlformats.org/drawingml/2006/chart",Ae="http://schemas.openxmlformats.org/officeDocument/2006/relationships",$=9525,ze=(e,t)=>`${ve(e.sheet.name)}$${ye(e.left)}$${String(e.top+1)}${e.single?"":`:$${ye(e.right)}$${String(t+1)}`}`;function L(e,t,r){if(!r?.node)return;const n=e.eval(r.node,{sheet:t,row:0,col:0,dr:0,dc:0}),s=f=>ze(f,Math.min(f.bottom,Math.max(f.top,f.sheet.lastRow)));if(n instanceof be)return s(n);if(n instanceof Me)return`(${n.parts.map(s).join(",")})`;const d=r.node;if(d.kind==="name"&&d.sheet!==void 0&&!(n instanceof Fe&&n.code==="#NAME?"))return`${ve(d.sheet)}${d.name}`}function Ge(e,t,r){if(!r?.node)return!1;const n=e.eval(r.node,{sheet:t,row:0,col:0,dr:0,dc:0});return n instanceof be&&typeof e.value(n.sheet,n.top,n.left)=="number"}export function chartPart(e,t,r){const n=De(e,t,r),s=r.series.map((c,l)=>({s:c,i:l,values:L(e,t,c.values)})).filter(c=>c.values!==void 0),d={...n,series:s.flatMap(c=>n.series[c.i]??[])},f=Ge(e,t,r.categories),m=f?Re(e,t,r):void 0,g={name:c=>L(e,t,s[c]?.s.name),categories:L(e,t,r.categories),numeric:f,...m===void 0?{}:{categoryNumbers:m.values,categoryFormat:m.format},values:c=>s[c]?.values??"",x:c=>L(e,t,s[c]?.s.x??r.categories)},x=L(e,t,r.title);return Se(d,{refs:g,...x===void 0?{}:{titleRef:x}})}const Ie=(e,t,r)=>a("xdr:oneCellAnchor",{},a("xdr:from",{},`<xdr:col>${String(e.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round(e.x*$))}</xdr:colOff>`,`<xdr:row>${String(e.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((e.y??0)*$))}</xdr:rowOff>`),a("xdr:ext",{cx:Math.round(e.width*$),cy:Math.round(e.height*$)}),a("xdr:pic",{},a("xdr:nvPicPr",{},a("xdr:cNvPr",{id:t,name:e.name,descr:e.path}),a("xdr:cNvPicPr",{},a("a:picLocks",{noChangeAspect:!0}))),a("xdr:blipFill",{},a("a:blip",{"xmlns:r":Ae,"r:embed":`rId${String(r)}`}),a("a:stretch",{},a("a:fillRect"))),a("xdr:spPr",{},a("a:xfrm",{},a("a:off",{x:0,y:0}),a("a:ext",{cx:Math.round(e.width*$),cy:Math.round(e.height*$)})),a("a:prstGeom",{prst:"rect"},a("a:avLst")))),a("xdr:clientData"));export function drawingPart(e,t=[]){const r=(n,s)=>{const{width:d,height:f}=Ne(n.spec);return a("xdr:oneCellAnchor",{},a("xdr:from",{},`<xdr:col>${String(n.at.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round((n.spec.x??0)*$))}</xdr:colOff>`,`<xdr:row>${String(n.at.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((n.spec.y??0)*$))}</xdr:rowOff>`),a("xdr:ext",{cx:Math.round(d*$),cy:Math.round(f*$)}),a("xdr:graphicFrame",{macro:""},a("xdr:nvGraphicFramePr",{},a("xdr:cNvPr",{id:s+2,name:`Chart ${String(s+1)}`}),a("xdr:cNvGraphicFramePr")),a("xdr:xfrm",{},a("a:off",{x:0,y:0}),a("a:ext",{cx:0,cy:0})),a("a:graphic",{},a("a:graphicData",{uri:Oe},a("c:chart",{"xmlns:c":Oe,"xmlns:r":Ae,"r:id":`rId${String(s+1)}`})))),a("xdr:clientData"))};return je+a("xdr:wsDr",{"xmlns:xdr":Ee,"xmlns:a":Le},...e.map(r),...t.map((n,s)=>Ie(n,e.length+s+2,e.length+s+1)))}function ne(e){if(Array.isArray(e))return e.map(ne).join("");if(typeof e!="object"||e===null)return"";const t=e,r=[];for(const[n,s]of Object.entries(t))n==="t"?r.push(S(s)):typeof s=="object"&&r.push(ne(s));return r.join("")}const re=e=>{const t=o(e?.solidFill),r=v(o(t?.srgbClr),"val");return r!==void 0?`#${r.toUpperCase()}`:Te[v(o(t?.schemeClr),"val")??""]},Te={accent1:"#4472C4",accent2:"#ED7D31",accent3:"#A5A5A5",accent4:"#FFC000",accent5:"#5B9BD5",accent6:"#70AD47"},C=(e,t)=>v(o(e?.[t]),"val"),F=(e,t)=>E(o(e?.[t]),"val"),z=(e,t)=>$e(o(e?.[t]),"val"),We=e=>/^"(?:[^"]|"")*"$/u.test(e)?e.slice(1,-1).replaceAll('""','"'):void 0,Ce=e=>We(e)??`=${e}`;function ke(e){if(!e)return;const t=o(e.tx),r=o(t?.strRef);if(r)return Ce(S(r.f));const n=ne(t?.rich);return n===""?void 0:n}function oe(e){if(!e)return;const t=o(e.scaling),r=o(e.numFmt),n=v(r,"formatCode"),s={},d=ke(o(e.title));d!==void 0&&(s.title=d);const f=F(t,"min"),m=F(t,"max"),g=F(e,"majorUnit");return f!==void 0&&(s.min=f),m!==void 0&&(s.max=m),g!==void 0&&g>0&&(s.major=g),z(e,"delete")===!0&&(s.visible=!1),C(t,"orientation")==="maxMin"&&(s.reverse=!0),n!==void 0&&n!=="General"&&$e(r,"sourceLinked")!==!0&&(s.format=n),s}const G=e=>{const t=o(e?.numRef)??o(e?.strRef),r=t?S(t.f):"";return r===""?void 0:r};function Ue(e,t){const r=G(o(e[t?"yVal":"val"]));if(r===void 0)return"its numbers are written in the chart";const n={values:r},s=o(e.tx),d=G(s);if(d!==void 0)n.name=Ce(d);else{const u=S(s?.v);u!==""&&(n.name=u)}const f=o(e.spPr),m=o(f?.ln),g=o(e.marker),x=t||g!==void 0,c=re(f)??(x?re(m)??re(o(g?.spPr)):void 0);c!==void 0&&(n.color=c);const l=E(m,"w");if(x&&l!==void 0&&m?.noFill===void 0){const u=Math.max(.25,Number((l/12700).toFixed(2)));u!==we.lineWidth&&(n.line_width=u)}z(e,"smooth")===!0&&(n.smooth=!0);const h=F(g,"size");h!==void 0&&h!==we.marker&&(n.marker_size=h);const w=t?G(o(e.xVal)):void 0;w!==void 0&&(n.x=w);const O=t?void 0:G(o(e.cat));return{series:n,...O===void 0?{}:{categories:O}}}function se(e,t){const r=A(t.ser),n=d=>o(o(d.spPr)?.ln)?.noFill!==void 0,s=d=>C(o(d.marker),"symbol")==="none";switch(e){case"barChart":return Be(t);case"lineChart":return r.every(s)?"line":"line-markers";case"areaChart":return Ve(t);case"pieChart":return"pie";case"doughnutChart":return"doughnut";case"scatterChart":return r.every(n)?"scatter":r.some(d=>z(d,"smooth")===!0)?"scatter-smooth":"scatter-lines";default:return}}function Be(e){const t=C(e,"barDir")==="bar"?"bar":"column",r=C(e,"grouping");return r==="stacked"?`${t}-stacked`:r==="percentStacked"?`${t}-stacked-100`:`${t}-clustered`}function Ve(e){const t=C(e,"grouping");return t==="stacked"?"area-stacked":t==="percentStacked"?"area-stacked-100":"area"}function I(e,t,r){if(e===void 0)return;const n=e.replace(/^=/u,"");if(/(?:^|[(,])'?\[\d+\]/u.test(n))return"it reads another workbook";if(e.includes("#REF!"))return"its cells were deleted (#REF!)";const s=/^(?:'((?:[^']|'')+)'|([^!'(),]+))!/u.exec(n),d=s?.[1]?.replaceAll("''","'")??s?.[2];if(d!==void 0&&!t(d))return`it reads a sheet the workbook does not have (${d})`;const f=_e(`=${n}`);if(!f.ok||!T(f.node,r))return`it names no cells (${n})`}function T(e,t){switch(e.kind){case"ref":case"structured":return!0;case"name":return e.sheet!==void 0||t(e.name);case"paren":return T(e.arg,t);case"binary":return(e.op===","||e.op===":")&&T(e.left,t)&&T(e.right,t);default:return!1}}export function readChart(e,t=[],r=()=>!0,n=()=>!0){const s=o(e?.chartSpace),d=o(s?.chart),f=o(d?.plotArea);if(!d||!f)return"it has no plot";const m=Object.entries(f).filter(([i])=>i.endsWith("Chart")).flatMap(([i,p])=>A(p).map(P=>({kind:i,plot:P}))),[g]=m;if(!g)return"it has no plot";const x=A(f.valAx),c=i=>A(i.axId).map(p=>v(p,"val")),l=i=>{const p=x.find(P=>c(i).includes(v(o(P.axId),"val")));return p!==void 0&&C(p,"axPos")==="r"},h=m.length>1,w=m.map(({kind:i,plot:p})=>({kind:i,plot:p,type:se(i,p),secondary:h&&l(p)}));if(h&&w.some(i=>i.type===void 0||!(i.type.startsWith("column")||i.type.startsWith("line")||i.type.startsWith("area"))))return"it combines several kinds of chart, not columns, lines and areas";const O=w.find(i=>!i.secondary)??w[0]??g,{kind:u,plot:b}=O,R=u==="scatterChart",M=[];let D,N;const U=w.flatMap(i=>A(i.plot.ser).map(p=>({s:p,of:i}))),B=U.filter(({s:i})=>i.trendline!==void 0).length;B>0&&t.push(B===1?"trendline":`trendlines of ${String(B)} series`);const V=U.filter(({s:i})=>i.errBars!==void 0).length;V>0&&t.push(V===1?"error bars":`error bars of ${String(V)} series`);for(const[i,{s:p,of:P}]of U.entries()){const y=Ue(p,R);if(typeof y=="string")return y;if(h){const te=P.type;te!==void 0&&te!==se(u,b)&&(y.series.type=te),P.secondary&&(y.series.secondary=!0)}const ee=I(y.series.values,r,n);if(ee!==void 0){t.push(`series ${String(i+1)}, as ${ee}`),N=ee;continue}y.series.name?.startsWith("=")===!0&&I(y.series.name,r,n)!==void 0&&delete y.series.name,I(y.series.x,r,n)!==void 0&&delete y.series.x,M.push(y.series),I(y.categories,r,n)===void 0&&(D??=y.categories)}if(M.length===0)return N===void 0?"it has no series":`every series ${N.replace(/^it /u,"")}`;const j=se(u,b);if(!j)return`a sheet has no ${u.replace(/Chart$/u,"")} chart`;const ie=ke(o(d.title)),ce=o(d.legend),H=C(ce,"legendPos"),ae=o(b.dLbls),k=i=>z(ae,i)===!0,Pe=k("showVal")||k("showPercent")||k("showCatName")||k("showSerName"),de=C(ae,"dLblPos"),X=F(b,"gapWidth"),fe=F(b,"overlap"),q=F(b,"holeSize"),ue=z(b,"varyColors"),le=j==="pie"||j==="doughnut",me=i=>c(b).includes(v(o(i.axId),"val")),he=[...A(f.catAx),...A(f.dateAx)],_={value:h?x.find(me)??x[0]:x.find((i,p,P)=>R?p===P.length-1:!0),category:R?x[0]:h?he.find(me)??he[0]:o(f.catAx)??o(f.dateAx),second:w.some(i=>i.secondary)?x.find(i=>C(i,"axPos")==="r"):void 0},J=oe(_.value),K=oe(_.category);J&&_.value?.majorGridlines===void 0&&(J.gridlines=!1),K&&_.category?.majorGridlines!==void 0&&(K.gridlines=!0);const Q=E(o(o(o(o(s?.txPr)?.p)?.pPr)?.defRPr),"sz"),Y=i=>i&&Object.keys(i).length>0?i:void 0,pe=Y(J),xe=Y(K),Z=oe(_.second);Z&&_.second?.majorGridlines!==void 0&&(Z.gridlines=!0);const ge=Y(Z);return{type:j,at:"A1",...ie===void 0?{}:{title:ie},...D===void 0?{}:{categories:D},series:le?M.slice(0,1):M,...ce?{legend:H==="b"?"bottom":H==="t"?"top":H==="l"?"left":"right"}:{},...Pe?{labels:{...k("showVal")?{value:!0}:{},...k("showPercent")?{percentage:!0}:{},...k("showCatName")?{category:!0}:{},...k("showSerName")?{series:!0}:{},...de===void 0?{}:{position:de}}}:{},...X===void 0||X===150?{}:{gap_width:X},...fe===void 0||j.includes("stacked")?{}:{overlap:fe},...q===void 0||q===50?{}:{hole_size:q},...ue===!0&&u==="barChart"?{vary_by_categories:!0}:{},...ue===!1&&le?{vary_by_categories:!1}:{},...pe?{value_axis:pe}:{},...xe?{category_axis:xe}:{},...ge?{secondary_axis:ge}:{},...Q===void 0||Q===900?{}:{size:Q/100}}}const W=e=>Math.round(e/$);export function chartAnchors(e,t,r){const n=o(e?.wsDr),s=[],d=[];let f=0;const m=c=>({col:Number(S(c?.col)),row:Number(S(c?.row)),colOff:W(Number(S(c?.colOff))),rowOff:W(Number(S(c?.rowOff)))}),g=c=>v(o(o(o(o(c.graphicFrame)?.graphic)?.graphicData)?.chart),"id"),x=(c,l,h,w)=>{const O={at:{row:l.row,col:l.col},...l.colOff>0?{x:l.colOff}:{},...l.rowOff>0?{y:l.rowOff}:{},width:h,height:w},u=o(c.pic),b=v(o(o(u?.blipFill)?.blip),"embed");if(u&&b!==void 0){const M=o(o(u.nvPicPr)?.cNvPr),D=v(M,"name"),N=v(M,"descr");d.push({id:b,...O,...D===void 0?{}:{name:D},...N===void 0?{}:{descr:N}});return}const R=g(c);if(R===void 0){f+=1;return}s.push({id:R,...O})};for(const c of A(n?.oneCellAnchor)){const l=o(c.ext);x(c,m(o(c.from)),W(E(l,"cx")??0),W(E(l,"cy")??0))}for(const c of A(n?.twoCellAnchor)){const l=m(o(c.from)),h=m(o(c.to));let w=h.colOff-l.colOff;for(let u=l.col;u<h.col;u++)w+=t(u);let O=h.rowOff-l.rowOff;for(let u=l.row;u<h.row;u++)O+=r(u);x(c,l,w,O)}return{anchors:s,pictures:d,others:f}}