@8sheets/data-grid 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/README.md +1 -1
  2. package/api/theme-core.js +2 -0
  3. package/calc/adjust.js +1 -1
  4. package/calc/compile.js +1 -1
  5. package/calc/engine.js +1 -1
  6. package/calc/functions/array.js +1 -1
  7. package/calc/functions/date.js +1 -1
  8. package/calc/functions/dbcs.js +1 -0
  9. package/calc/functions/financial.js +1 -1
  10. package/calc/functions/helpers.js +1 -1
  11. package/calc/functions/index.js +7 -1
  12. package/calc/functions/info.js +1 -1
  13. package/calc/functions/logical.js +1 -1
  14. package/calc/functions/lookup.js +1 -1
  15. package/calc/functions/math.js +1 -1
  16. package/calc/functions/stat.js +1 -1
  17. package/calc/functions/text.js +1 -1
  18. package/calc/parser.js +3 -3
  19. package/calc/values.js +1 -1
  20. package/calcx/culture/custom.js +1 -0
  21. package/calcx/culture/format-locale.js +1 -1
  22. package/calcx/culture/separators.js +1 -0
  23. package/calcx/functions/text.js +1 -1
  24. package/calcx/lang/convert.js +1 -0
  25. package/cells/axis-ops.js +1 -1
  26. package/cells/block.js +1 -0
  27. package/cells/book-css.js +2 -2
  28. package/cells/cell-defaults.js +1 -1
  29. package/cells/cf-engine.js +1 -1
  30. package/cells/consolidate-links.js +1 -1
  31. package/cells/find-engine.js +2 -2
  32. package/cells/format-panes.js +3 -3
  33. package/cells/format-tabs.js +5 -5
  34. package/cells/name-box.js +1 -1
  35. package/cells/outline-column.js +1 -1
  36. package/cells/panes.js +1 -1
  37. package/cells/selection-modes.js +1 -1
  38. package/cells/shift-check.js +1 -0
  39. package/cells/table-rows.js +1 -0
  40. package/cells/text-layout.js +9 -8
  41. package/cells/ui-kit.js +15 -0
  42. package/cells/view-options.js +1 -1
  43. package/celltypes/diagonal.js +1 -1
  44. package/celltypes/dropdowns.js +2 -2
  45. package/core/address.js +1 -1
  46. package/core/autofill.js +1 -0
  47. package/core/axis-styles.js +1 -0
  48. package/core/axis.js +1 -1
  49. package/core/cond-format.js +1 -1
  50. package/core/culture.js +1 -0
  51. package/core/default-names.js +1 -1
  52. package/core/formula-refs.js +1 -1
  53. package/core/input.js +1 -1
  54. package/core/merge.js +1 -0
  55. package/core/numfmt.js +1 -1
  56. package/core/region-input.js +1 -0
  57. package/core/row-fit.js +1 -0
  58. package/core/sheet.js +1 -1
  59. package/core/structure.js +1 -0
  60. package/core/styles.js +1 -1
  61. package/core/tables.js +2 -2
  62. package/core/whatif.js +1 -1
  63. package/core/workbook.js +1 -1
  64. package/datasheets/data-core.js +1 -1
  65. package/datasheets/gantt-paint.js +1 -1
  66. package/datasheets/path-binding.js +2 -2
  67. package/datasheets/rules.js +1 -1
  68. package/datasheets/view.js +2 -2
  69. package/i18n/dom.js +1 -1
  70. package/i18n/en/cells.js +1 -1
  71. package/i18n/en/common.js +1 -1
  72. package/i18n/en/format.js +1 -1
  73. package/i18n/en/shell.js +2 -1
  74. package/i18n/pl/cells.js +1 -1
  75. package/i18n/pl/common.js +1 -1
  76. package/i18n/pl/format.js +1 -1
  77. package/i18n/pl/shell.js +2 -1
  78. package/io/csv.js +8 -8
  79. package/io/plan.js +1 -1
  80. package/io/text-split.js +3 -3
  81. package/io/xlsx-write.js +2 -2
  82. package/objects/chart-model.js +1 -1
  83. package/package.json +1 -1
  84. package/pivot/engine.js +1 -1
  85. package/print/paginate.js +1 -1
  86. package/print/render-page.js +3 -3
  87. package/table-view/api.js +1 -1
  88. package/table-view/cell-editor.js +2 -1
  89. package/table-view/chart.js +1 -0
  90. package/table-view/column-drag.js +1 -1
  91. package/table-view/context-menu.js +1 -0
  92. package/table-view/editors.js +1 -0
  93. package/table-view/events.js +1 -1
  94. package/table-view/files.js +1 -1
  95. package/table-view/fill-handle.js +1 -0
  96. package/table-view/filter-builder.js +1 -0
  97. package/table-view/find.js +1 -0
  98. package/table-view/grid-keys.js +2 -2
  99. package/table-view/labels-de.js +1 -0
  100. package/table-view/labels-es.js +1 -0
  101. package/table-view/labels-fr.js +1 -0
  102. package/table-view/labels-ja.js +1 -0
  103. package/table-view/labels.js +1 -1
  104. package/table-view/model.js +1 -1
  105. package/table-view/mount.js +1 -1
  106. package/table-view/panel.js +1 -1
  107. package/table-view/pivot-spec.js +1 -0
  108. package/table-view/pivot.js +1 -1
  109. package/table-view/public.d.ts +277 -28
  110. package/table-view/reader.worker.js +1 -1
  111. package/table-view/remote.js +1 -1
  112. package/table-view/row-drag.js +1 -0
  113. package/table-view/sparkline.js +1 -0
  114. package/table-view/state-check.js +1 -0
  115. package/table-view/table-css.js +380 -0
  116. package/table-view/version.js +1 -1
  117. package/ui/dialogs.js +3 -3
  118. package/ui/icons.js +1 -1
  119. package/ui/menu-base.js +1 -1
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A data grid for the web in plain JavaScript modules with no runtime dependencies. Open a CSV or Excel file, or pass your records, and search, filter, sort, group, total, pivot and export them as CSV, Excel or PDF. Files are read in a Worker as a stream, the page keeps a bounded DOM, and a million records stay responsive. Server data plugs in through a query contract, so search, filters and totals cover the whole result.
4
4
 
5
- This is version 1.0.1. Components for [React](https://www.npmjs.com/package/@8sheets/data-grid-react), [Vue](https://www.npmjs.com/package/@8sheets/data-grid-vue) and [Angular](https://www.npmjs.com/package/@8sheets/data-grid-angular) build on it. For a full Excel-compatible workbook, see the Spreadsheet, [`@8sheets/core`](https://www.npmjs.com/package/@8sheets/core).
5
+ This is version 1.1.0. Components for [React](https://www.npmjs.com/package/@8sheets/data-grid-react), [Vue](https://www.npmjs.com/package/@8sheets/data-grid-vue) and [Angular](https://www.npmjs.com/package/@8sheets/data-grid-angular) build on it. For a full Excel-compatible workbook, see the Spreadsheet, [`@8sheets/core`](https://www.npmjs.com/package/@8sheets/core).
6
6
 
7
7
  ## Install
8
8
 
@@ -0,0 +1,2 @@
1
+ import{refLive as S}from"./style-refs.js";import{xmlLiteralText as w}from"../io/xml-escape.js";const p=["dk1","lt1","dk2","lt2","accent1","accent2","accent3","accent4","accent5","accent6","hlink","folHlink"],g={lt1:0,dk1:1,lt2:2,dk2:3,accent1:4,accent2:5,accent3:6,accent4:7,accent5:8,accent6:9,hlink:10,folHlink:11},N=Object.keys(g),O=[1,0,3,2,4,5,6,7,8,9,10,11],k="000000 FFFFFF 0E2841 E8E8E8 156082 E97132 196B24 0F9ED5 A02B93 4EA72E 467886 96607D".split(" "),u=()=>({name:"Office Theme",colors:Object.fromEntries(k.map((e,t)=>[p[t],e])),major:"Aptos Display",minor:"Aptos Narrow"});function R(e,t=u()){const o={...t.colors};for(const[l,a]of Object.entries(e.colors??{})){if(!p.includes(l))throw new TypeError(`Unknown theme color "${l}". Slots: ${p.join(", ")}`);const c=String(a).replace(/^#/,"").toUpperCase();if(!/^[0-9A-F]{6}$/.test(c))throw new TypeError(`Theme color ${l} must be six hex digits, not "${a}"`);o[l]=c}return{name:e.name??"Custom",colorName:e.colorName??e.name??t.colorName??t.name,fontName:e.fontName??e.name??t.fontName??t.name,colors:o,major:e.major??t.major,minor:e.minor??t.minor}}const C=w,I='<a:fmtScheme name="8sheets"><a:fillStyleLst><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:solidFill><a:schemeClr val="phClr"><a:tint val="85000"/></a:schemeClr></a:solidFill><a:solidFill><a:schemeClr val="phClr"><a:shade val="85000"/></a:schemeClr></a:solidFill></a:fillStyleLst><a:lnStyleLst><a:ln w="6350" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln><a:ln w="12700" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln><a:ln w="19050" cap="flat" cmpd="sng" algn="ctr"><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:prstDash val="solid"/></a:ln></a:lnStyleLst><a:effectStyleLst><a:effectStyle><a:effectLst/></a:effectStyle><a:effectStyle><a:effectLst/></a:effectStyle><a:effectStyle><a:effectLst/></a:effectStyle></a:effectStyleLst><a:bgFillStyleLst><a:solidFill><a:schemeClr val="phClr"/></a:solidFill><a:solidFill><a:schemeClr val="phClr"><a:tint val="95000"/></a:schemeClr></a:solidFill><a:solidFill><a:schemeClr val="phClr"><a:shade val="95000"/></a:schemeClr></a:solidFill></a:bgFillStyleLst></a:fmtScheme>';function _(e){const t=p.map(l=>{const a=e.colors[l];return l==="dk1"&&a==="000000"?'<a:dk1><a:sysClr val="windowText" lastClr="000000"/></a:dk1>':l==="lt1"&&a==="FFFFFF"?'<a:lt1><a:sysClr val="window" lastClr="FFFFFF"/></a:lt1>':`<a:${l}><a:srgbClr val="${a}"/></a:${l}>`}).join(""),o=l=>`<a:latin typeface="${C(l)}"/><a:ea typeface=""/><a:cs typeface=""/>`;return`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <a:theme xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" name="${C(e.name)}"><a:themeElements><a:clrScheme name="${C(e.colorName??e.name)}">${t}</a:clrScheme><a:fontScheme name="${C(e.fontName??e.name)}"><a:majorFont>${o(e.major)}</a:majorFont><a:minorFont>${o(e.minor)}</a:minorFont></a:fontScheme>${I}</a:themeElements><a:objectDefaults/><a:extraClrSchemeLst/></a:theme>`}const y=(e,t)=>new RegExp(`\\b${t}="([^"]*)"`).exec(e)?.[1];function H(e){const t=/<a:clrScheme\b[^>]*>([\s\S]*?)<\/a:clrScheme>/.exec(e);if(!t)throw new Error("This theme part has no color scheme");const o={};for(const a of p){const c=new RegExp(`<a:${a}>\\s*<a:(?:srgbClr val|sysClr[^>]*?lastClr)="([0-9A-Fa-f]{6})"`).exec(t[1]);o[a]=(c?.[1]??"000000").toUpperCase()}const l=a=>new RegExp(`<a:${a}>\\s*<a:latin\\s+typeface="([^"]*)"`).exec(e)?.[1]??"Calibri";return{name:y(/<a:theme\b[^>]*>/.exec(e)?.[0]??"","name")??"Imported",colorName:y(/<a:clrScheme\b[^>]*>/.exec(e)?.[0]??"","name"),fontName:y(/<a:fontScheme\b[^>]*>/.exec(e)?.[0]??"","name"),colors:o,major:l("majorFont"),minor:l("minorFont")}}function b(e){return N.map(t=>e.colors[t])}function U(e){const t=[];return e.forEach((o,l)=>{t[O[l]]=o}),t}function D(e,t){if(!t)return e.toUpperCase();const o=parseInt(e.slice(0,2),16)/255,l=parseInt(e.slice(2,4),16)/255,a=parseInt(e.slice(4,6),16)/255,c=Math.max(o,l,a),r=Math.min(o,l,a);let n=(c+r)/2;const i=c-r,f=i===0?0:i/(1-Math.abs(2*n-1));let s=0;i!==0&&(c===o?s=((l-a)/i+6)%6:c===l?s=(a-o)/i+2:s=(o-l)/i+4),n=t<0?n*(1+t):n*(1-t)+t;const m=(1-Math.abs(2*n-1))*f,h=m*(1-Math.abs(s%2-1)),v=n-m/2,[T,L,$]=s<1?[m,h,0]:s<2?[h,m,0]:s<3?[0,m,h]:s<4?[0,h,m]:s<5?[h,0,m]:[m,0,h],E=j=>Math.round((j+v)*255).toString(16).padStart(2,"0");return(E(T)+E(L)+E($)).toUpperCase()}function x(e,t=0,o=b(u())){const l=typeof e=="number"?e:g[e];if(l===void 0)throw new TypeError(`Unknown theme color "${e}"`);return{t:l,tint:t,hex:`#${D(o[l]??"000000",t)}`}}function z(e,t,o={}){const l=b(t),c=(o.previous??e.themeInfo??null)?.minor??null;for(const r of e.styles.list){let n=r.themeRefs;if(!n?.font&&c&&r.fontName===c&&(n={...n,font:"minor"},r.themeRefs=n),n){const i=(f,s=n)=>{const m=s[f];return s[f]=x(m.t,m.tint,l),s[f].hex};if(n=r.themeRefs={...n},n.color&&S(n.color,r.color??"#000000")){const f=i("color");r.color=f==="#000000"?null:f}if(S(n.fill,r.fill)&&(r.fill=i("fill")),n.patternFg&&r.pattern&&S(n.patternFg,r.pattern.fg)&&(r.pattern={...r.pattern,fg:i("patternFg")}),n.border&&r.borders){n.border={...n.border};const f={...r.borders};for(const s of["l","r","t","b","d"]){const m=n.border[s];m&&f[s]&&S(m,f[s].color)&&(f[s]={...f[s],color:i(s,n.border)})}r.borders=f}n.font==="major"?r.fontName=t.major:n.font==="minor"&&(r.fontName=t.minor)}}e.styles.index=null,e.styles.deriveCache?.clear(),e.theme=l,e.themeInfo=t,e.themeXml=_(t),e.themeChanged=!0;for(const r of e.sheets)r.cfEval?.invalidate(),r.revision++}const F=(e,t,o=0)=>({style:e,slot:t,tint:o}),d={Title:{fontSize:18,slot:"dk2",font:"major"},"Heading 1":{fontSize:15,bold:!0,slot:"dk2",bottom:F("thick","accent1")},"Heading 2":{fontSize:13,bold:!0,slot:"dk2",bottom:F("thick","accent1",.5)},"Heading 3":{bold:!0,slot:"dk2",bottom:F("medium","accent1",.4)},"Heading 4":{bold:!0,slot:"dk2"},Total:{bold:!0,slot:"dk1",top:F("thin","accent1"),bottom:F("double","accent1")}};for(let e=1;e<=6;e++){const t=`accent${e}`;d[`Accent${e}`]={fillSlot:t,slot:"lt1"},d[`20% - Accent${e}`]={fillSlot:t,fillTint:.8,slot:"dk1"},d[`40% - Accent${e}`]={fillSlot:t,fillTint:.6,slot:"dk1"},d[`60% - Accent${e}`]={fillSlot:t,fillTint:.4,slot:"lt1"}}const X=Object.keys(d);function B(e,t){const o=d[e];if(!o)return null;const l=t.theme?.length>=10?t.theme:b(u()),a={},c={};if(o.fontSize&&(c.fontSize=o.fontSize),o.bold&&(c.bold=!0),o.slot&&(a.color=x(o.slot,0,l),c.color=a.color.hex),o.fillSlot&&(a.fill=x(o.fillSlot,o.fillTint??0,l),c.fill=a.fill.hex),o.bottom||o.top){a.border={};const r=(n,i)=>n?(a.border[i]=x(n.slot,n.tint,l),{style:n.style,color:a.border[i].hex}):null;c.borders={l:null,r:null,t:r(o.top,"t"),b:r(o.bottom,"b")}}return o.font&&(a.font=o.font,c.fontName=o.font==="major"?t.themeInfo?.major??u().major:t.themeInfo?.minor??u().minor),c.themeRefs=a,c}import{refLive as q}from"./style-refs.js";export{p as COLOR_SLOTS,X as THEMED_CELL_STYLE_NAMES,g as THEME_INDEX,z as applyThemeToBook,R as buildTheme,u as defaultTheme,U as fileOrderToIndex,x as makeRef,H as parseThemeXml,q as refLive,b as themeArray,_ as themeXml,B as themedCellStylePatch,D as tintHex};
package/calc/adjust.js CHANGED
@@ -1 +1 @@
1
- import{colName as j,MAX_COLS as E,MAX_ROWS as S}from"../core/address.js";import{tokenize as O}from"./parser.js";function N(e,s,r){return e<s?e:r>0?e+r:e<s-r?null:e+r}function x(e,s,r,o,b){if(o>0){const R=e>=r?e+o:e,k=s>=r?Math.min(s+o,b-1):s;return R>=b?null:[R,k]}const $=r-o;if(e>=r&&s<$)return null;const y=e<r?e:e<$?r:e+o,u=s<r?s:s<$?r-1:s+o;return[y,u]}const M=(e,s,r,o)=>`${r?"$":""}${j(e)}${o?"$":""}${s+1}`;function X(e,s,r,o){const b=(i,h)=>!s.span||i>=s.span[0]&&h<=s.span[1];let $;try{$=O(e)}catch{return e}const y=o.toUpperCase(),u=s.axis==="row";let R="",k=0;const v=(i,h,d)=>{R+=e.slice(k,i)+d,k=h};for(let i=0;i<$.length;i++){const h=$[i];let d=r,c=null,A=i;h.t==="sheet"&&(c=h,d=h.v.book===null&&h.v.sheet2===null&&h.v.sheet.toUpperCase()===y,A=i+1);const l=$[A];if(l===void 0)break;if(l.t==="cell"){const t=l.v,m=$[A+1],f=$[A+2];if(m?.t==="op"&&m.v===":"&&f?.t==="cell"){const n=f.v,F=u?[Math.min(t.c,n.c),Math.max(t.c,n.c)]:[Math.min(t.r,n.r),Math.max(t.r,n.r)];if(d&&b(F[0],F[1])){const p=c?e.slice(c.s,c.e):"",a=u?x(Math.min(t.r,n.r),Math.max(t.r,n.r),s.at,s.n,S):x(Math.min(t.c,n.c),Math.max(t.c,n.c),s.at,s.n,E),C=c?c.s:l.s;if(a===null)v(C,f.e,"#REF!");else if(u){const w=t.r<=n.r;v(C,f.e,`${p}${M(t.c,w?a[0]:a[1],t.cAbs,t.rAbs)}:${M(n.c,w?a[1]:a[0],n.cAbs,n.rAbs)}`)}else{const w=t.c<=n.c;v(C,f.e,`${p}${M(w?a[0]:a[1],t.r,t.cAbs,t.rAbs)}:${M(w?a[1]:a[0],n.r,n.cAbs,n.rAbs)}`)}}i=A+2;continue}if(d&&b(u?t.c:t.r,u?t.c:t.r)){const n=N(u?t.r:t.c,s.at,s.n),F=u?S:E,p=c?c.s:l.s,a=c?e.slice(c.s,c.e):"";n===null||n>=F?v(p,l.e,"#REF!"):v(p,l.e,a+(u?M(t.c,n,t.cAbs,t.rAbs):M(n,t.r,t.cAbs,t.rAbs)))}i=A}else if(l.t==="rows"&&u&&d&&!s.span){const t=l.v,m=x(t.r0,t.r1,s.at,s.n,S),f=c?c.s:l.s,n=c?e.slice(c.s,c.e):"";m===null?v(f,l.e,"#REF!"):v(f,l.e,`${n}${t.a0?"$":""}${m[0]+1}:${t.a1?"$":""}${m[1]+1}`),i=A}else if(l.t==="cols"&&!u&&d&&!s.span){const t=l.v,m=x(t.c0,t.c1,s.at,s.n,E),f=c?c.s:l.s,n=c?e.slice(c.s,c.e):"";m===null?v(f,l.e,"#REF!"):v(f,l.e,`${n}${t.a0?"$":""}${j(m[0])}:${t.a1?"$":""}${j(m[1])}`),i=A}else c&&(i=A-1)}return R+e.slice(k)}function _(e,s){const r=s.axis==="row",o=r?x(e.r0,e.r1,s.at,s.n,S):x(e.c0,e.c1,s.at,s.n,E);return o===null?null:r?{...e,r0:o[0],r1:o[1]}:{...e,c0:o[0],c1:o[1]}}export{X as adjustFormula,_ as adjustRange,M as cellText};
1
+ import{colName as j,MAX_COLS as E,MAX_ROWS as S}from"../core/address.js";import{tokenize as O}from"./parser.js";function N(e,s,r){return e<s?e:r>0?e+r:e<s-r?null:e+r}function M(e,s,r,o,R){if(o>0){const k=e>=r?e+o:e,p=s>=r?Math.min(s+o,R-1):s;return k>=R?null:[k,p]}const $=r-o;if(e>=r&&s<$)return null;const y=e<r?e:e<$?r:e+o,u=s<r?s:s<$?r-1:s+o;return[y,u]}const b=(e,s,r,o)=>`${r?"$":""}${j(e)}${o?"$":""}${s+1}`;function X(e,s,r,o){const R=(i,h)=>!s.span||i>=s.span[0]&&h<=s.span[1];let $;try{$=O(e)}catch{return e}const y=o.toUpperCase(),u=s.axis==="row";let k="",p=0;const v=(i,h,d)=>{k+=e.slice(p,i)+d,p=h};for(let i=0;i<$.length;i++){const h=$[i];let d=r,c=null,A=i;h.t==="sheet"&&(c=h,d=h.v.book===null&&h.v.sheet2===null&&h.v.sheet.toUpperCase()===y,A=i+1);const l=$[A];if(l===void 0)break;if(l.t==="cell"){const t=l.v,m=$[A+1],a=$[A+2];if(m?.t==="op"&&m.v===":"&&a?.t==="cell"){const n=a.v,F=u?[Math.min(t.c,n.c),Math.max(t.c,n.c)]:[Math.min(t.r,n.r),Math.max(t.r,n.r)];if(d&&R(F[0],F[1])){const x=c?e.slice(c.s,c.e):"",f=u?M(Math.min(t.r,n.r),Math.max(t.r,n.r),s.at,s.n,S):M(Math.min(t.c,n.c),Math.max(t.c,n.c),s.at,s.n,E),C=c?c.s:l.s;if(f===null)v(C,a.e,`${x}#REF!`);else if(u){const w=t.r<=n.r;v(C,a.e,`${x}${b(t.c,w?f[0]:f[1],t.cAbs,t.rAbs)}:${b(n.c,w?f[1]:f[0],n.cAbs,n.rAbs)}`)}else{const w=t.c<=n.c;v(C,a.e,`${x}${b(w?f[0]:f[1],t.r,t.cAbs,t.rAbs)}:${b(w?f[1]:f[0],n.r,n.cAbs,n.rAbs)}`)}}i=A+2;continue}if(d&&R(u?t.c:t.r,u?t.c:t.r)){const n=N(u?t.r:t.c,s.at,s.n),F=u?S:E,x=c?c.s:l.s,f=c?e.slice(c.s,c.e):"";n===null||n>=F?v(x,l.e,`${f}#REF!`):v(x,l.e,f+(u?b(t.c,n,t.cAbs,t.rAbs):b(n,t.r,t.cAbs,t.rAbs)))}i=A}else if(l.t==="rows"&&u&&d&&!s.span){const t=l.v,m=M(t.r0,t.r1,s.at,s.n,S),a=c?c.s:l.s,n=c?e.slice(c.s,c.e):"";m===null?v(a,l.e,`${n}#REF!`):v(a,l.e,`${n}${t.a0?"$":""}${m[0]+1}:${t.a1?"$":""}${m[1]+1}`),i=A}else if(l.t==="cols"&&!u&&d&&!s.span){const t=l.v,m=M(t.c0,t.c1,s.at,s.n,E),a=c?c.s:l.s,n=c?e.slice(c.s,c.e):"";m===null?v(a,l.e,`${n}#REF!`):v(a,l.e,`${n}${t.a0?"$":""}${j(m[0])}:${t.a1?"$":""}${j(m[1])}`),i=A}else c&&(i=A-1)}return k+e.slice(p)}function _(e,s){const r=s.axis==="row",o=r?M(e.r0,e.r1,s.at,s.n,S):M(e.c0,e.c1,s.at,s.n,E);return o===null?null:r?{...e,r0:o[0],r1:o[1]}:{...e,c0:o[0],c1:o[1]}}export{X as adjustFormula,_ as adjustRange,b as cellText};
package/calc/compile.js CHANGED
@@ -1 +1 @@
1
- import{MAX_COLS as A,MAX_ROWS as d}from"../core/address.js";import{Arr as x,compareValues as C,ERR as m,Lambda as w,num as L,OMITTED as M,Ref as y,RefList as g,str as V,XlError as b}from"./values.js";import{FUNCS as E}from"./functions/registry.js";const R=5e-16;class N{constructor(e){this.cx=e,this.refs=[],this.names=[],this.tables=[],this.isVolatile=!1,this.mayArray=!1,this.external=!1,this.arrayCtx=!1,this.scopes=[],this.slots=0,this.rangeSlots=new Set}compileTop(e){const i=this.any(e),{slots:n,cx:a}=this;return n===0?i:(c,t,s)=>{const r=a.frame;a.frame=new Array(n);try{return i(c,t,s)}finally{a.frame=r}}}sheetOf(e){const{cx:i}=this;if(e==null)return t=>t;if(e.book!==null&&e.book!==void 0){const t=i.externalSheet?.(e.book,e.sheet)??null;return t===null&&(this.external=!0),()=>t}const n=e.sheet;let a=-1,c=null;return()=>(a!==i.sheetGen&&(c=i.sheetByName(n),a=i.sheetGen),c)}cellRefClosure(e){const i=this.sheetOf(e.sheet),{rAbs:n,cAbs:a,r:c,c:t}=e;return this.refs.push({sheet:e.sheet,rAbs0:n,r0:c,rAbs1:n,r1:c,cAbs0:a,c0:t,cAbs1:a,c1:t}),e.sheet?.sheet2?this.sheet3d(e.sheet,(s,r,o)=>{const h=n?c:r+c,u=a?t:o+t;return new y(s,h,u,h,u)}):(s,r,o)=>{const h=n?c:s+c,u=a?t:r+t,f=i(o);return f===null||h<0||u<0||h>=d||u>=A?m.REF:new y(f,h,u,h,u)}}sheet3d(e,i){const{cx:n}=this;return(a,c)=>{const t=n.sheetsBetween(e.sheet,e.sheet2);return t===null?m.REF:new g(t.map(s=>i(s,a,c)))}}rangeClosure(e){const{a:i,b:n}=e,a=this.sheetOf(e.sheet);this.refs.push({sheet:e.sheet,rAbs0:i.rAbs,r0:i.r,rAbs1:n.rAbs,r1:n.r,cAbs0:i.cAbs,c0:i.c,cAbs1:n.cAbs,c1:n.c});const c=(t,s,r)=>{const o=i.rAbs?i.r:s+i.r,h=i.cAbs?i.c:r+i.c,u=n.rAbs?n.r:s+n.r,f=n.cAbs?n.c:r+n.c;return o<0||h<0||u<0||f<0||o>=d||u>=d||h>=A||f>=A?m.REF:new y(t,Math.min(o,u),Math.min(h,f),Math.max(o,u),Math.max(h,f))};return e.sheet?.sheet2?this.sheet3d(e.sheet,c):(t,s,r)=>{const o=a(r);return o===null?m.REF:c(o,t,s)}}rowsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:e.a0,r0:e.r0,rAbs1:e.a1,r1:e.r1,cAbs0:!0,c0:0,cAbs1:!0,c1:A-1}),(n,a,c)=>{const t=e.a0?e.r0:n+e.r0,s=e.a1?e.r1:n+e.r1,r=i(c);return r===null||t<0||s<0||t>=d||s>=d?m.REF:new y(r,Math.min(t,s),0,Math.max(t,s),A-1)}}colsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:!0,r0:0,rAbs1:!0,r1:d-1,cAbs0:e.a0,c0:e.c0,cAbs1:e.a1,c1:e.c1}),(n,a,c)=>{const t=e.a0?e.c0:a+e.c0,s=e.a1?e.c1:a+e.c1,r=i(c);return r===null||t<0||s<0||t>=A||s>=A?m.REF:new y(r,0,Math.min(t,s),d-1,Math.max(t,s))}}any(e){const{cx:i}=this;switch(e.t){case"lit":{const n=e.v;return()=>n}case"omit":return()=>M;case"paren":return this.any(e.e);case"cell":return this.cellRefClosure(e);case"range":return this.rangeClosure(e);case"rows":return this.rowsClosure(e);case"cols":return this.colsClosure(e);case"array":{const n=x.of2D(e.rows);return()=>n}case"union":{const n=e.items.map(a=>this.any(a));return(a,c,t)=>{const s=[];for(const r of n){const o=r(a,c,t);if(o instanceof y)s.push(o);else if(o instanceof g)s.push(...o.refs);else return o instanceof b?o:m.VALUE}return new g(s)}}case"intersect":{const n=this.any(e.l),a=this.any(e.r);return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);if(r instanceof b)return r;if(o instanceof b)return o;if(!(r instanceof y)||!(o instanceof y)||r.sheet!==o.sheet)return m.VALUE;const h=Math.max(r.r0,o.r0),u=Math.min(r.r1,o.r1),f=Math.max(r.c0,o.c0),l=Math.min(r.c1,o.c1);return h>u||f>l?m.NULL:new y(r.sheet,h,f,u,l)}}case"rangeOp":{const n=this.any(e.l),a=this.any(e.r);return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);return r instanceof b?r:o instanceof b?o:!(r instanceof y)||!(o instanceof y)||r.sheet!==o.sheet?m.REF:new y(r.sheet,Math.min(r.r0,o.r0),Math.min(r.c0,o.c0),Math.max(r.r1,o.r1),Math.max(r.c1,o.c1))}}case"implicit":{const n=this.any(e.e);return(a,c,t)=>{const s=n(a,c,t);return s instanceof y?s.isCell?i.read(s.sheet,s.r0,s.c0):i.intersect(s,a,c):s instanceof x?s.data[0]:s}}case"spillRef":{const n=this.any(e.e);return(a,c,t)=>{const s=n(a,c,t);return s instanceof y?i.spillRange(s):m.REF}}case"name":return this.nameNode(e);case"table":return this.tables.push({name:e.name,spec:e.spec}),(n,a,c)=>i.resolveTable(e.name,e.spec,n,a,c);case"call":return this.call(e);case"apply":{const n=this.any(e.fn),a=e.args.map(c=>this.any(c));return(c,t,s)=>{const r=n(c,t,s);return r instanceof b?r:r instanceof w?i.invoke(r,a.map(o=>o(c,t,s)),c,t,s):m.VALUE}}default:return this.value(e)}}nameNode(e){const{cx:i}=this,n=this.lookupVar(e.name);if(n!==null){const s=n;return()=>i.frame[s]}this.names.push(e.name);const a=e.name,c=e.sheet?this.sheetOf(e.sheet):s=>s,t=e.sheet?void 0:E.get(a.toUpperCase());if(t!==void 0){const s=new w([],null,null);return s.native=t,s.label=a.toUpperCase(),(r,o,h)=>{const u=i.evalName(a,r,o,h);return u===m.NAME?s:u}}return(s,r,o)=>i.evalName(a,s,r,c(o)??o)}lookupVar(e){const i=e.toUpperCase();for(let n=this.scopes.length-1;n>=0;n--){const a=this.scopes[n].get(i);if(a!==void 0)return a}return null}deref(e){const{cx:i}=this;return this.arrayCtx?(n,a,c)=>{const t=e(n,a,c);return t instanceof y||t instanceof g?i.refToValue(t):t}:(n,a,c)=>{const t=e(n,a,c);return t instanceof y||t instanceof g?i.refAuto(t,n,a):t}}value(e){const{cx:i}=this;switch(e.t){case"lit":{const n=e.v;return()=>n}case"paren":return this.value(e.e);case"cell":{const{rAbs:n,cAbs:a,r:c,c:t}=e;if(this.refs.push({sheet:e.sheet,rAbs0:n,r0:c,rAbs1:n,r1:c,cAbs0:a,c0:t,cAbs1:a,c1:t}),e.sheet?.sheet2)return()=>m.VALUE;const s=e.sheet?this.sheetOf(e.sheet):null;return s===null?!n&&!a?(r,o,h)=>{const u=r+c,f=o+t;return u<0||f<0||u>=d||f>=A?m.REF:i.read(h,u,f)}:(r,o,h)=>{const u=n?c:r+c,f=a?t:o+t;return u<0||f<0||u>=d||f>=A?m.REF:i.read(h,u,f)}:(r,o,h)=>{const u=n?c:r+c,f=a?t:o+t,l=s(h);return l===null||u<0||f<0||u>=d||f>=A?m.REF:i.read(l,u,f)}}case"range":case"rows":case"cols":return this.arrayCtx||(this.mayArray=!0),this.deref(this.any(e));case"neg":{const n=this.value(e.e);return(a,c,t)=>{const s=n(a,c,t);return typeof s=="number"?-s:i.unary(s,r=>-r)}}case"pct":{const n=this.value(e.e);return(a,c,t)=>{const s=n(a,c,t);return typeof s=="number"?s/100:i.unary(s,r=>r/100)}}case"bin":return this.binary(e);default:return!this.arrayCtx&&this.mayYieldRange(e)&&(this.mayArray=!0),this.deref(this.any(e))}}mayYieldRange(e){switch(e.t){case"paren":return this.mayYieldRange(e.e);case"range":case"rows":case"cols":case"union":case"rangeOp":case"intersect":case"spillRef":case"apply":return!0;case"table":return!/^\[\[#This Row\],\[[^\]]+\]\]$/i.test(e.spec);case"name":{if(e.sheet)return!0;const i=this.lookupVar(e.name);return i!==null?this.rangeSlots.has(i):this.cx.nameMayYieldRange?.(e.name)??!0}case"call":{if(e.name==="LET"||e.name==="LAMBDA"||this.lookupVar(e.name)!==null)return!0;const i=E.get(e.name);return i===void 0||i.refResult?!0:i.passes?e.args.some((n,a)=>i.passes(a,e.args.length)&&this.mayYieldRange(n)):!1}default:return!1}}binary(e){const{cx:i}=this,n=this.value(e.l),a=this.value(e.r);switch(e.op){case"+":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);if(typeof r=="number"&&typeof o=="number"){const h=r+o;return Math.abs(h)<Math.abs(r)*R?0:h}return i.slowBin("+",r,o)};case"-":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);if(typeof r=="number"&&typeof o=="number"){const h=r-o;return Math.abs(h)<Math.abs(r)*R?0:h}return i.slowBin("-",r,o)};case"*":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);return typeof r=="number"&&typeof o=="number"?r*o:i.slowBin("*",r,o)};case"/":return(c,t,s)=>{const r=n(c,t,s),o=a(c,t,s);return typeof r=="number"&&typeof o=="number"&&o!==0?r/o:i.slowBin("/",r,o)};case"^":return(c,t,s)=>i.slowBin("^",n(c,t,s),a(c,t,s));case"&":return(c,t,s)=>i.slowBin("&",n(c,t,s),a(c,t,s));default:{const c=e.op;return(t,s,r)=>i.slowBin(c,n(t,s,r),a(t,s,r))}}}call(e){const{cx:i}=this,{name:n,args:a}=e;if(n==="LET")return this.compileLet(a);if(n==="LAMBDA")return this.compileLambda(a);const c=this.lookupVar(n);if(c!==null){const s=c,r=a.map(o=>this.any(o));return(o,h,u)=>{const f=i.frame[s];return f instanceof w?i.invoke(f,r.map(l=>l(o,h,u)),o,h,u):m.VALUE}}const t=E.get(n);if(t===void 0){this.names.push(n);const s=a.map(r=>this.any(r));return(r,o,h)=>{const u=i.evalName(n,r,o,h);return u instanceof w?i.invoke(u,s.map(f=>f(r,o,h)),r,o,h):m.NAME}}if((a.length<t.min||a.length>t.max)&&!(a.length===1&&a[0].t==="omit"&&t.min===0))return()=>m.VALUE;if(t.volatile&&(this.isVolatile=!0),t.notInExcel&&(this.notInExcel=!0),t.frozen&&(this.external=!0),t.kind==="lazy"||t.kind==="raw"){const s=this.arrayCtx;t.array&&(this.arrayCtx=!0);const r=a.map(h=>this.any(h));this.arrayCtx=s;const{fn:o}=t;return t.kind==="lazy"?(h,u,f)=>{try{return o(i,r,h,u,f)}catch(l){if(l instanceof b)return l;throw l}}:(h,u,f)=>{const l=new Array(r.length);for(let p=0;p<r.length;p++)l[p]=r[p](h,u,f);try{return o(i,l,h,u,f)}catch(p){if(p instanceof b)return p;throw p}}}return this.lifted(t,a)}lifted(e,i){const{cx:n}=this,{fn:a,errors:c}=e,t=i.map(r=>r.t==="omit"?()=>M:this.value(r)),s=t.length;if(s===1){const r=t[0];return(o,h,u)=>{const f=r(o,h,u);if(f instanceof x)return n.liftArr(a,[f],c);if(!c&&f instanceof b)return f;try{return a(f)}catch(l){if(l instanceof b)return l;throw l}}}if(s===2){const[r,o]=t;return(h,u,f)=>{const l=r(h,u,f),p=o(h,u,f);if(l instanceof x||p instanceof x)return n.liftArr(a,[l,p],c);if(!c){if(l instanceof b)return l;if(p instanceof b)return p}try{return a(l,p)}catch(v){if(v instanceof b)return v;throw v}}}return(r,o,h)=>{const u=new Array(s);let f=!1;for(let l=0;l<s;l++){const p=t[l](r,o,h);u[l]=p,p instanceof x&&(f=!0)}if(f)return n.liftArr(a,u,c);if(!c){for(let l=0;l<s;l++)if(u[l]instanceof b)return u[l]}try{return a(...u)}catch(l){if(l instanceof b)return l;throw l}}}compileLet(e){const{cx:i}=this;if(e.length<3||e.length%2===0)return()=>m.VALUE;const n=new Map;this.scopes.push(n);const a=[];for(let t=0;t+1<e.length;t+=2){const s=e[t];if(s.t!=="name")return this.scopes.pop(),()=>m.VALUE;const r=this.any(e[t+1]),o=this.slots++;this.mayYieldRange(e[t+1])&&this.rangeSlots.add(o),n.set(s.name.toUpperCase(),o),a.push(o,r)}const c=this.any(e[e.length-1]);return this.scopes.pop(),(t,s,r)=>{for(let o=0;o<a.length;o+=2)i.frame[a[o]]=a[o+1](t,s,r);return c(t,s,r)}}compileLambda(e){const{cx:i}=this;if(e.length===0)return()=>m.VALUE;const n=new Map;this.scopes.push(n);const a=[];for(let t=0;t<e.length-1;t++){const s=e[t];if(s.t!=="name")return this.scopes.pop(),()=>m.VALUE;const r=this.slots++;this.rangeSlots.add(r),n.set(s.name.toUpperCase(),r),a.push(r)}const c=this.any(e[e.length-1]);return this.scopes.pop(),()=>new w(a,c,i.frame)}}export{N as Compiler,C as compareValues,L as num,V as str};
1
+ import{MAX_COLS as d,MAX_ROWS as A}from"../core/address.js";import{Arr as x,compareValues as L,ERR as p,Lambda as w,num as k,OMITTED as M,Ref as m,RefList as g,REJECTED as R,str as U,XlError as b}from"./values.js";import{FUNCS as E}from"./functions/registry.js";const C=5e-16;class N{constructor(e){this.cx=e,this.refs=[],this.names=[],this.tables=[],this.isVolatile=!1,this.readsSheets=!1,this.mayArray=!1,this.external=!1,this.arrayCtx=!1,this.scopes=[],this.slots=0,this.rangeSlots=new Set}compileTop(e){const i=this.any(e),{slots:a,cx:c}=this;return a===0?i:(o,t,r)=>{const s=c.frame;c.frame=new Array(a);try{return i(o,t,r)}finally{c.frame=s}}}sheetOf(e){const{cx:i}=this;if(e==null)return t=>t;if(e.book!==null&&e.book!==void 0){const t=i.externalSheet?.(e.book,e.sheet)??null;return t===null&&(this.external=!0),()=>t}const a=e.sheet;let c=-1,o=null;return()=>(c!==i.sheetGen&&(o=i.sheetByName(a),c=i.sheetGen),o)}cellRefClosure(e){const i=this.sheetOf(e.sheet),{rAbs:a,cAbs:c,r:o,c:t}=e;return this.refs.push({sheet:e.sheet,rAbs0:a,r0:o,rAbs1:a,r1:o,cAbs0:c,c0:t,cAbs1:c,c1:t}),e.sheet?.sheet2?this.sheet3d(e.sheet,(r,s,n)=>{const u=a?o:s+o,h=c?t:n+t;return new m(r,u,h,u,h)}):(r,s,n)=>{const u=a?o:r+o,h=c?t:s+t,l=i(n);return l===null||u<0||h<0||u>=A||h>=d?p.REF:new m(l,u,h,u,h)}}sheet3d(e,i){const{cx:a}=this;return(c,o)=>{const t=a.sheetsBetween(e.sheet,e.sheet2);return t===null?p.REF:new g(t.map(r=>i(r,c,o)))}}rangeClosure(e){const{a:i,b:a}=e,c=this.sheetOf(e.sheet);this.refs.push({sheet:e.sheet,rAbs0:i.rAbs,r0:i.r,rAbs1:a.rAbs,r1:a.r,cAbs0:i.cAbs,c0:i.c,cAbs1:a.cAbs,c1:a.c});const o=(t,r,s)=>{const n=i.rAbs?i.r:r+i.r,u=i.cAbs?i.c:s+i.c,h=a.rAbs?a.r:r+a.r,l=a.cAbs?a.c:s+a.c;return n<0||u<0||h<0||l<0||n>=A||h>=A||u>=d||l>=d?p.REF:new m(t,Math.min(n,h),Math.min(u,l),Math.max(n,h),Math.max(u,l))};return e.sheet?.sheet2?this.sheet3d(e.sheet,o):(t,r,s)=>{const n=c(s);return n===null?p.REF:o(n,t,r)}}rowsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:e.a0,r0:e.r0,rAbs1:e.a1,r1:e.r1,cAbs0:!0,c0:0,cAbs1:!0,c1:d-1}),(a,c,o)=>{const t=e.a0?e.r0:a+e.r0,r=e.a1?e.r1:a+e.r1,s=i(o);return s===null||t<0||r<0||t>=A||r>=A?p.REF:new m(s,Math.min(t,r),0,Math.max(t,r),d-1)}}colsClosure(e){const i=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:!0,r0:0,rAbs1:!0,r1:A-1,cAbs0:e.a0,c0:e.c0,cAbs1:e.a1,c1:e.c1}),(a,c,o)=>{const t=e.a0?e.c0:c+e.c0,r=e.a1?e.c1:c+e.c1,s=i(o);return s===null||t<0||r<0||t>=d||r>=d?p.REF:new m(s,0,Math.min(t,r),A-1,Math.max(t,r))}}any(e){const{cx:i}=this;switch(e.t){case"lit":{const a=e.v;return()=>a}case"omit":return()=>M;case"paren":return this.any(e.e);case"cell":return this.cellRefClosure(e);case"range":return this.rangeClosure(e);case"rows":return this.rowsClosure(e);case"cols":return this.colsClosure(e);case"array":{const a=x.of2D(e.rows);return()=>a}case"union":{const a=e.items.map(c=>this.any(c));return(c,o,t)=>{const r=[];for(const s of a){const n=s(c,o,t);if(n instanceof m)r.push(n);else if(n instanceof g)r.push(...n.refs);else return n instanceof b?n:p.VALUE}return new g(r)}}case"intersect":{const a=this.any(e.l),c=this.any(e.r);return(o,t,r)=>{const s=a(o,t,r),n=c(o,t,r);if(s instanceof b)return s;if(n instanceof b)return n;if(!(s instanceof m)||!(n instanceof m)||s.sheet!==n.sheet)return p.VALUE;const u=Math.max(s.r0,n.r0),h=Math.min(s.r1,n.r1),l=Math.max(s.c0,n.c0),f=Math.min(s.c1,n.c1);return u>h||l>f?p.NULL:new m(s.sheet,u,l,h,f)}}case"rangeOp":{const a=this.any(e.l),c=this.any(e.r);return(o,t,r)=>{const s=a(o,t,r),n=c(o,t,r);return s instanceof b?s:n instanceof b?n:!(s instanceof m)||!(n instanceof m)||s.sheet!==n.sheet?p.REF:new m(s.sheet,Math.min(s.r0,n.r0),Math.min(s.c0,n.c0),Math.max(s.r1,n.r1),Math.max(s.c1,n.c1))}}case"implicit":{const a=this.any(e.e);return(c,o,t)=>{const r=a(c,o,t);return r instanceof m?r.isCell?i.read(r.sheet,r.r0,r.c0):i.intersect(r,c,o):r instanceof x?r.data[0]:r}}case"spillRef":{const a=this.any(e.e);return(c,o,t)=>{const r=a(c,o,t);return r instanceof m?i.spillRange(r):p.REF}}case"name":return this.nameNode(e);case"table":return this.tables.push({name:e.name,spec:e.spec}),(a,c,o)=>i.resolveTable(e.name,e.spec,a,c,o);case"call":return this.call(e);case"apply":{const a=this.any(e.fn),c=e.args.map(o=>this.any(o));return(o,t,r)=>{const s=a(o,t,r);return s instanceof b?s:s instanceof w?i.invoke(s,c.map(n=>n(o,t,r)),o,t,r):p.VALUE}}default:return this.value(e)}}nameNode(e){const{cx:i}=this,a=this.lookupVar(e.name);if(a!==null){const s=a;return()=>i.frame[s]}const c=i.tables?.get(e.name.toUpperCase());if(c&&(!e.sheet||e.sheet.book===null&&!e.sheet.sheet2&&e.sheet.sheet.toUpperCase()===c.sheet.name.toUpperCase()))return this.any({t:"table",name:e.name,spec:"[]"});this.names.push(e.name);const o=e.name,t=e.sheet?this.sheetOf(e.sheet):s=>s,r=e.sheet?void 0:E.get(o.toUpperCase());if(r!==void 0){const s=new w([],null,null);return s.native=r,s.label=o.toUpperCase(),(n,u,h)=>{const l=i.evalName(o,n,u,h);return l===p.NAME?s:l}}return(s,n,u)=>i.evalName(o,s,n,t(u)??u)}lookupVar(e){const i=e.toUpperCase();for(let a=this.scopes.length-1;a>=0;a--){const c=this.scopes[a].get(i);if(c!==void 0)return c}return null}deref(e){const{cx:i}=this;return this.arrayCtx?(a,c,o)=>{const t=e(a,c,o);return t instanceof m||t instanceof g?i.refToValue(t):t}:(a,c,o)=>{const t=e(a,c,o);return t instanceof m||t instanceof g?i.refAuto(t,a,c):t}}value(e){const{cx:i}=this;switch(e.t){case"lit":{const a=e.v;return()=>a}case"paren":return this.value(e.e);case"cell":{const{rAbs:a,cAbs:c,r:o,c:t}=e;if(this.refs.push({sheet:e.sheet,rAbs0:a,r0:o,rAbs1:a,r1:o,cAbs0:c,c0:t,cAbs1:c,c1:t}),e.sheet?.sheet2)return()=>p.VALUE;const r=e.sheet?this.sheetOf(e.sheet):null;return r===null?!a&&!c?(s,n,u)=>{const h=s+o,l=n+t;return h<0||l<0||h>=A||l>=d?p.REF:i.read(u,h,l)}:(s,n,u)=>{const h=a?o:s+o,l=c?t:n+t;return h<0||l<0||h>=A||l>=d?p.REF:i.read(u,h,l)}:(s,n,u)=>{const h=a?o:s+o,l=c?t:n+t,f=r(u);return f===null||h<0||l<0||h>=A||l>=d?p.REF:i.read(f,h,l)}}case"range":case"rows":case"cols":return this.arrayCtx||(this.mayArray=!0),this.deref(this.any(e));case"neg":{const a=this.value(e.e);return(c,o,t)=>{const r=a(c,o,t);return typeof r=="number"?-r:i.unary(r,s=>-s)}}case"pct":{const a=this.value(e.e);return(c,o,t)=>{const r=a(c,o,t);return typeof r=="number"?r/100:i.unary(r,s=>s/100)}}case"bin":return this.binary(e);default:return!this.arrayCtx&&this.mayYieldRange(e)&&(this.mayArray=!0),this.deref(this.any(e))}}mayYieldRange(e){switch(e.t){case"paren":return this.mayYieldRange(e.e);case"range":case"rows":case"cols":case"union":case"rangeOp":case"intersect":case"spillRef":case"apply":return!0;case"table":return!/^\[\[#This Row\],\[[^\]]+\]\]$/i.test(e.spec);case"name":{if(e.sheet)return!0;const i=this.lookupVar(e.name);return i!==null?this.rangeSlots.has(i):this.cx.nameMayYieldRange?.(e.name)??!0}case"call":{if(e.name==="LET"||e.name==="LAMBDA"||this.lookupVar(e.name)!==null)return!0;const i=E.get(e.name);return i===void 0||i.refResult?!0:i.passes?e.args.some((a,c)=>i.passes(c,e.args.length)&&this.mayYieldRange(a)):!1}default:return!1}}binary(e){const{cx:i}=this,a=this.value(e.l),c=this.value(e.r);switch(e.op){case"+":return(o,t,r)=>{const s=a(o,t,r),n=c(o,t,r);if(typeof s=="number"&&typeof n=="number"){const u=s+n;return Math.abs(u)<Math.abs(s)*C?0:u}return i.slowBin("+",s,n)};case"-":return(o,t,r)=>{const s=a(o,t,r),n=c(o,t,r);if(typeof s=="number"&&typeof n=="number"){const u=s-n;return Math.abs(u)<Math.abs(s)*C?0:u}return i.slowBin("-",s,n)};case"*":return(o,t,r)=>{const s=a(o,t,r),n=c(o,t,r);return typeof s=="number"&&typeof n=="number"?s*n:i.slowBin("*",s,n)};case"/":return(o,t,r)=>{const s=a(o,t,r),n=c(o,t,r);return typeof s=="number"&&typeof n=="number"&&n!==0?s/n:i.slowBin("/",s,n)};case"^":return(o,t,r)=>i.slowBin("^",a(o,t,r),c(o,t,r));case"&":return(o,t,r)=>i.slowBin("&",a(o,t,r),c(o,t,r));default:{const o=e.op;return(t,r,s)=>i.slowBin(o,a(t,r,s),c(t,r,s))}}}call(e){const{cx:i}=this,{name:a,args:c}=e;if(a==="LET")return this.compileLet(c);if(a==="LAMBDA")return this.compileLambda(c);const o=this.lookupVar(a);if(o!==null){const r=o,s=c.map(n=>this.any(n));return(n,u,h)=>{const l=i.frame[r];return l instanceof w?i.invoke(l,s.map(f=>f(n,u,h)),n,u,h):p.VALUE}}const t=E.get(a);if(t===void 0){this.names.push(a);const r=c.map(s=>this.any(s));return(s,n,u)=>{const h=i.evalName(a,s,n,u);return h instanceof w?i.invoke(h,r.map(l=>l(s,n,u)),s,n,u):p.NAME}}if((c.length<t.min||c.length>t.max)&&!(c.length===1&&c[0].t==="omit"&&t.min===0))return()=>p.VALUE;if(t.volatile&&(this.isVolatile=!0),t.readsSheets&&(this.readsSheets=!0),t.notInExcel&&(this.notInExcel=!0),t.frozen&&(this.external=!0),t.kind==="lazy"||t.kind==="raw"){const r=this.arrayCtx;t.array&&(this.arrayCtx=!0);const s=c.map(u=>this.any(u));this.arrayCtx=r;const{fn:n}=t;return t.kind==="lazy"?(u,h,l)=>{try{return n(i,s,u,h,l)}catch(f){if(f instanceof b)return f;throw f}}:(u,h,l)=>{const f=new Array(s.length);for(let y=0;y<s.length;y++)f[y]=s[y](u,h,l);try{return n(i,f,u,h,l)}catch(y){if(y instanceof b)return y;throw y}}}return this.lifted(t,c)}toolpakArg(e,i){const{cx:a}=this,c=t=>typeof t=="boolean"&&!i?R:t;if(e.t!=="range"&&e.t!=="rows"&&e.t!=="cols"&&e.t!=="union"&&e.t!=="call"&&e.t!=="name"){const t=this.value(e);return(r,s,n)=>c(t(r,s,n))}const o=this.any(e);return(t,r,s)=>{const n=o(t,r,s);return n instanceof g||!a.legacy&&n instanceof m&&(n.r0!==n.r1||n.c0!==n.c1)?R:c(n instanceof m?a.refAuto(n,t,r):n)}}lifted(e,i){const{cx:a}=this,{fn:c,errors:o}=e,t=i.map((s,n)=>s.t==="omit"?()=>M:e.toolpak?this.toolpakArg(s,e.toolpak.logical.includes(n)):this.value(s)),r=t.length;if(r===1){const s=t[0];return(n,u,h)=>{const l=s(n,u,h);if(l instanceof x)return a.liftArr(c,[l],o);if(!o&&l instanceof b)return l;try{return c(l)}catch(f){if(f instanceof b)return f;throw f}}}if(r===2){const[s,n]=t;return(u,h,l)=>{const f=s(u,h,l),y=n(u,h,l);if(f instanceof x||y instanceof x)return a.liftArr(c,[f,y],o);if(!o){if(f instanceof b)return f;if(y instanceof b)return y}try{return c(f,y)}catch(v){if(v instanceof b)return v;throw v}}}return(s,n,u)=>{const h=new Array(r);let l=!1;for(let f=0;f<r;f++){const y=t[f](s,n,u);h[f]=y,y instanceof x&&(l=!0)}if(l)return a.liftArr(c,h,o);if(!o){for(let f=0;f<r;f++)if(h[f]instanceof b)return h[f]}try{return c(...h)}catch(f){if(f instanceof b)return f;throw f}}}compileLet(e){const{cx:i}=this;if(e.length<3||e.length%2===0)return()=>p.VALUE;const a=new Map;this.scopes.push(a);const c=[];for(let t=0;t+1<e.length;t+=2){const r=e[t];if(r.t!=="name")return this.scopes.pop(),()=>p.VALUE;const s=this.any(e[t+1]),n=this.slots++;this.mayYieldRange(e[t+1])&&this.rangeSlots.add(n),a.set(r.name.toUpperCase(),n),c.push(n,s)}const o=this.any(e[e.length-1]);return this.scopes.pop(),(t,r,s)=>{for(let n=0;n<c.length;n+=2)i.frame[c[n]]=c[n+1](t,r,s);return o(t,r,s)}}compileLambda(e){const{cx:i}=this;if(e.length===0)return()=>p.VALUE;const a=new Map;this.scopes.push(a);const c=[];for(let t=0;t<e.length-1;t++){const r=e[t];if(r.t!=="name")return this.scopes.pop(),()=>p.VALUE;const s=this.slots++;this.rangeSlots.add(s),a.set(r.name.toUpperCase(),s),c.push(s)}const o=this.any(e[e.length-1]);return this.scopes.pop(),()=>new w(c,o,i.frame)}}export{N as Compiler,L as compareValues,k as num,U as str};
package/calc/engine.js CHANGED
@@ -1 +1 @@
1
- import{resolveStructuredSpec as yt}from"../slicers/structured.js";import{Arr as C,compareValues as v,ERR as k,ERR_BY_INDEX as Y,Lambda as kt,num as S,power as _t,Ref as q,RefList as $,RichValue as O,str as rt,XlError as x}from"./values.js";import{entityVm as at,richValueAt as gt}from"../cells/rich-values.js";import{Compiler as lt}from"./compile.js";import{FormulaSyntaxError as W,Parser as j,templateKey as bt,tokenize as z,toDisplayFormula as Mt}from"./parser.js";import{BOOL as G,CHUNK_BITS as E,CHUNK_MASK as T,CONTENT_MASK as ot,DIRTY as w,EMPTY as Q,EVALUATING as X,FORMULA as R,KIND_MASK as V,NUM as U,SPILL as H,STR as B,ERR as N}from"../core/sheet.js";import{colName as ct,MAX_COLS as M,MAX_ROWS as J,parseRangeRef as P}from"../core/address.js";import{CellSet as wt}from"./cell-set.js";import{isRowBelow as At}from"./fill-shape.js";import"./functions/index.js";const Z=Symbol("pending"),ht=200,Ct=4096,xt=64,tt=2147483647,D=4;class et{constructor(){this.n=0,this.data=new Int32Array(D*8)}push(t,e,s,n){if((this.n+1)*D>this.data.length){const a=new Int32Array(this.data.length*2);a.set(this.data),this.data=a}const i=this.n++*D,r=this.data;r[i]=t,r[i+1]=e,r[i+2]=s,r[i+3]=n}}function Tt(m,t){if(!m.valueMeta.has(t))return;const e=new Map(m.valueMeta);e.delete(t),m.valueMeta=e.size>0?e:null}function ft(m,t,e){if(m.valueMeta?.get(t)===e)return;const s=new Map(m.valueMeta??[]);s.set(t,e),m.valueMeta=s}function st(m,t){let e=m.get(t);return e===void 0&&(e=new wt,m.set(t,e)),e}class Rt{constructor(){this.buckets=[],this.wide=[],this.all=new et,this.allCols=[]}}class Bt{constructor(t){this.book=t,this.sheetGen=0,this.sheetMap=new Map,this.deps=new Map,this.templates=new Map,this.lastOwn={row:-2,col:-1,text:"",tpl:{cacheKey:void 0}},this.frame=null,this.depth=0,this.legacy=!1,this.pendingSheet=null,this.pendingRow=0,this.pendingCol=0,this.circular=null,this.evalStack=new Int32Array(3*256),this.evalTop=0,this.cycleLow=tt,this.iterate=null,this.cycleFound=[],this.cycleMembers=null,this.iterating=!1,this.sliceMs=10,this.syncLimit=4e3,this.externalLinks=null,this.names=new Map,this.nameTemplates=new Map,this.tables=new Map,this.volatile=new Map,this.visibilityReaders=new Map,this.styleReaders=new Map,this.wakePending=0,this.queue=new Int32Array(4096),this.qHead=0,this.qLen=0,this.sheetsByUid=new Map,this.running=!1,this.timer=0,this.manual=!1,this.graphReady=!0,this.pendingChanges=[],this.waiters=[],this.onChange=null,this.dataTables=[],this.tablesDirty=!1,this.inTables=!1,this.tablesForced=!1,this.onProgress=null,this.lookupCache=new Map,this.colRevisions=new Map,this.channel=null,this.sheetsChanged()}orderChanged(){this.sheetsChanged(),this.has3D&&this.rebuild()}sheetsChanged(){this.sheetGen++,this.sheetMap.clear(),this.sheetsByUid.clear();for(const t of this.book.sheets)this.sheetMap.set(t.name.toUpperCase(),t),this.sheetsByUid.set(t.uid,t),t.rows.watcher=this,t.cols.watcher=this}sheetByName(t){return this.sheetMap.get(t.toUpperCase())??null}externalSheet(t,e){return this.externalLinks?.sheet(t,e)??null}sheetsBetween(t,e){const{sheets:s}=this.book,n=s.findIndex(r=>r.name.toUpperCase()===t.toUpperCase()),i=s.findIndex(r=>r.name.toUpperCase()===e.toUpperCase());return n<0||i<0?null:s.slice(Math.min(n,i),Math.max(n,i)+1)}setNames(t){this.names.clear(),this.nameTemplates.clear();for(const e of t){const s=e.localSheetId!==void 0?`${e.localSheetId}!${e.name.toUpperCase()}`:e.name.toUpperCase();this.names.set(s,{text:e.text,name:e.name,hidden:e.hidden,comment:e.comment})}}setTables(t){this.tables.clear();for(const e of t)this.tables.set(e.name.toUpperCase(),e)}template(t,e,s){let n;try{n=z(t)}catch(a){if(a instanceof W)return this._broken(t);throw a}const i=bt(n,e,s);let r=this.templates.get(i);if(r===void 0){try{const a=new j(n,e,s).parse();r=this._compile(a)}catch(a){if(!(a instanceof W))throw a;r=this._broken(t)}r.cacheKey=i,this.templates.set(i,r)}return r}_ownTemplate(t,e,s){const n=this.lastOwn;let i;return n.row===e-1&&n.col===s&&this.templates.get(n.tpl.cacheKey)===n.tpl&&At(n.text,t)?i=n.tpl:i=this.template(t,e,s),n.row=e,n.col=s,n.text=t,n.tpl=i,i}_compile(t){const e=new lt(this);return{fn:e.compileTop(t),refs:e.refs,names:e.names,tables:e.tables,isVolatile:e.isVolatile,notInExcel:e.notInExcel===!0,mayArray:e.mayArray,external:e.external}}_broken(){return{invalid:!0,fn:()=>k.NAME,refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1,external:!0}}validate(t){it(new j(z(t),0,0).parse())}_templateFor(t,e,s,n){if(n.tpl!==void 0)return n.tpl;let i;return n.t==="dataTable"?i=this._dataTableTemplate():n.f!==void 0&&n.base===void 0?i=this._ownTemplate(n.f,e,s):n.f!==void 0?i=this.template(n.f,Math.floor(n.base/M),n.base%M):n.sf!==void 0?i=this.template(n.sf,Math.floor(n.base/M),n.base%M):i=this._broken(),n.tpl=i,i}_index(t){let e=this.deps.get(t);return e===void 0&&(e=new Rt,this.deps.set(t,e)),e}_addDep(t,e,s,n,i,r,a,o){const l=this._index(t),c=r.uid<<14|o;if(i-s>=xt){l.all.push(a,c,e,n),l.allCols.push(s,i);return}if(n-e>=Ct){for(let p=s;p<=i;p++){let f=l.wide[p];f===void 0&&(f=new et,l.wide[p]=f),f.push(a,c,e,n)}return}const u=e>>>E,d=n>>>E;for(let p=s;p<=i;p++){let f=l.buckets[p];f===void 0&&(f=[],l.buckets[p]=f);for(let h=u;h<=d;h++){let y=f[h];y===void 0&&(y=new et,f[h]=y),y.push(a,c,e,n)}}}register(t,e,s,n){const i=this._templateFor(t,e,s,n);if(i.external)return i;for(const r of i.refs)this._addRefDep(r,t,e,s);for(const r of i.names)this._addNameDeps(r,t,e,s);for(const r of i.tables)this._addTableDep(r,t,e,s);return i.isVolatile&&st(this.volatile,t).add(e,s),i.readsVisibility??=/SUBTOTAL|AGGREGATE|SPARK_/i.test(n.f??n.sf??""),i.readsVisibility&&st(this.visibilityReaders,t).add(e,s),i.readsStyles??=/SPARK_BLOCK/i.test(n.f??n.sf??""),i.readsStyles&&st(this.styleReaders,t).add(e,s),i}_addRefDep(t,e,s,n){const i=t.rAbs0?t.r0:s+t.r0,r=t.rAbs1?t.r1:s+t.r1,a=t.cAbs0?t.c0:n+t.c0,o=t.cAbs1?t.c1:n+t.c1,l=Math.max(0,Math.min(i,r)),c=Math.min(J-1,Math.max(i,r)),u=Math.max(0,Math.min(a,o)),d=Math.min(M-1,Math.max(a,o));if(l>c||u>d)return;if(t.sheet?.sheet2&&(t.sheet.book===null||t.sheet.book===void 0)){this.has3D=!0;for(const f of this.sheetsBetween(t.sheet.sheet,t.sheet.sheet2)??[])this._addDep(f,l,u,c,d,e,s,n);return}if(t.sheet?.book!==null&&t.sheet?.book!==void 0)return;const p=t.sheet?this.sheetByName(t.sheet.sheet):e;p&&this._addDep(p,l,u,c,d,e,s,n)}_addNameDeps(t,e,s,n){const i=this._nameTemplate(t,e);if(i!==null){for(const r of i.refs)this._addRefDep(r,e,s,n);for(const r of i.names)r.toUpperCase()!==t.toUpperCase()&&this._addNameDeps(r,e,s,n)}}_addTableDep(t,e,s,n){const i=this.resolveTable(t.name,t.spec,s,n,e);i instanceof q&&this._addDep(i.sheet,i.r0,i.c0,i.r1,i.c1,e,s,n)}rebuild(){this.has3D=!1,this.deps.clear(),this.volatile.clear(),this.visibilityReaders.clear(),this.styleReaders.clear(),this.lookupCache.clear(),this.qHead=0,this.qLen=0,this.cycleFound=[],this.cycleMembers=null,this.sheetGen++,this.buildGraph({fullCalc:!0})}buildGraph({fullCalc:t=!1,onDone:e}={}){this.graphReady=!1,this.dataTables=[],t&&(this.tablesDirty=!0);const s=this,n=this.book.sheets.map(c=>[c,c.formulas]);let i=0,r=0,a=0,o=0;const l=()=>{const c=performance.now()+s.sliceMs;let u=0;for(;i<n.length;i++,r=0,a=0,o=0){const[p,f]=n[i],{columns:h}=f;for(;r<h.length;r++,a=0,o=0){const y=h[r];if(y!==void 0)for(;a<y.length;a++,o=0){const _=y[a];if(_===void 0)continue;const{ids:g}=_,b=a<<E;for(;o<=T;o++){const L=g[o];if(L===0)continue;const A=f.infos[L],I=b+o;A.t==="dataTable"&&s.dataTables.push({sheet:p,row:I,col:r,info:A});const F=s.register(p,I,r,A);if(s._initFormula(p,I,r,A,F,t),++u%256===0&&performance.now()>c){o++,s.onProgress?.({preparing:!0}),s._later(l);return}}}}}s.graphReady=!0;const d=s.pendingChanges;s.pendingChanges=[];for(const[p,f,h,y]of d)s.cellsChanged(p,f,h,y);s._schedule(),e?.()};l()}_initFormula(t,e,s,n,i,r){const a=t._chunk(e,s);if(a===void 0)return;const o=e&T;if(n.t==="array"&&n.ref&&this._markArrayChildren(t,e,s,n),i.external)return;const l=a.kinds[o];(r||i.isVolatile||(l&V)===Q||i.notInExcel&&(l&V)===N)&&(a.kinds[o]=l|w,this._enqueue(t,e,s))}_markArrayChildren(t,e,s,n){const i=P(n.ref);if(i!==null){t.spills||(t.spills=new Map),t.spills.set(e*M+s,{...i,blocked:!1,fixed:!n.cm});for(let r=i.r0;r<=i.r1;r++)for(let a=i.c0;a<=i.c1;a++){if(r===e&&a===s)continue;const o=t._ensure(r,a);o.kinds[r&T]|=H}}}_enqueue(t,e,s){if(this.qLen+3>this.queue.length)if(this.qHead>this.queue.length>>>1)this.queue.copyWithin(0,this.qHead,this.qLen),this.qLen-=this.qHead,this.qHead=0;else{const n=new Int32Array(this.queue.length*2);n.set(this.queue),this.queue=n}this.queue[this.qLen++]=t.uid,this.queue[this.qLen++]=e,this.queue[this.qLen++]=s}_dirtyFrom(t,e,s,n,i){const r=[t,e,s,n,i];for(;r.length>0;){const a=r.pop(),o=r.pop(),l=r.pop(),c=r.pop(),u=r.pop(),d=this.deps.get(u);if(d===void 0)continue;const p=(y,_)=>{const g=this.sheetsByUid.get(_>>>14);if(g===void 0)return;const b=_&16383,L=g._chunk(y,b);if(L===void 0)return;const A=y&T,I=L.kinds[A];(I&R)===0||(I&w)!==0||(L.kinds[A]=I|w,this._enqueue(g,y,b),r.push(g,b,b,y,y))},f=o>>>E,h=a>>>E;for(let y=c;y<=l;y++){const _=d.buckets[y];if(_!==void 0)for(let b=f;b<=h;b++){const L=_[b];if(L===void 0)continue;const{data:A}=L,I=L.n*D;for(let F=0;F<I;F+=D)a>=A[F+2]&&o<=A[F+3]&&p(A[F],A[F+1])}const g=d.wide[y];if(g!==void 0){const{data:b}=g,L=g.n*D;for(let A=0;A<L;A+=D)a>=b[A+2]&&o<=b[A+3]&&p(b[A],b[A+1])}}if(d.all.n>0){const{data:y}=d.all;for(let _=0;_<d.all.n;_++){const g=_*D;a>=y[g+2]&&o<=y[g+3]&&l>=d.allCols[_*2]&&c<=d.allCols[_*2+1]&&p(y[g],y[g+1])}}}}cellsChanged(t,e,s,n){if(n===0)return;if(this.inTables||(this.tablesDirty=!0),!this.graphReady){this.pendingChanges.push([t,e.slice(0,n),s.slice(0,n),n]);return}this.lookupCache.clear();const i=new Map;for(let r=0;r<n;r++){const a=e[r],o=s[r],l=i.get(o);l===void 0?i.set(o,[a,a]):(a<l[0]&&(l[0]=a),a>l[1]&&(l[1]=a));const c=t._chunk(a,o);if(c===void 0)continue;const u=a&T,d=c.kinds[u];if(d&R){const p=t.formula(a,o);p&&(this.register(t,a,o,p),d&w||(c.kinds[u]=d|w,this._enqueue(t,a,o)))}t.spills&&this._spillTouched(t,a,o)}for(const[r,a]of i)this._dirtyFrom(t,r,r,a[0],a[1]);this._markVolatile(),this._schedule()}_spillTouched(t,e,s){const n=e*M+s;for(const[i,r]of t.spills){const a=Math.floor(i/M),o=i-a*M;if(i===n){(t.kind(a,o)&R)===0&&(this._clearSpill(t,r,a,o),t.spills.delete(i));continue}if(e>=r.r0&&e<=r.r1&&s>=r.c0&&s<=r.c1){const l=t._chunk(a,o);if(l===void 0)continue;const c=a&T,u=l.kinds[c];u&R&&!(u&w)&&(l.kinds[c]=u|w,this._enqueue(t,a,o),this._dirtyFrom(t,o,o,a,a))}}}_markVolatile(){for(const[t,e]of this.volatile)e.forEach((s,n)=>{const i=t._chunk(s,n);if(i===void 0)return;const r=s&T,a=i.kinds[r];if(!(a&R)){e.delete(s,n);return}a&w||(i.kinds[r]=a|w,this._enqueue(t,s,n),this._dirtyFrom(t,n,n,s,s))})}get pending(){return(this.qLen-this.qHead)/3}_later(t){if(this._next=t,typeof MessageChannel>"u"){setTimeout(()=>this._tick(),0);return}this.channel===null&&(this.channel=new MessageChannel,this.channel.port1.onmessage=()=>this._tick()),this.channel.port2.postMessage(0)}_tick(){const t=this._next;this._next=null,t?.(),this._next===null&&this.channel!==null&&(this.channel.port1.close(),this.channel=null)}_schedule(){if(this.manual||this.inTables||this.running||!this.graphReady)return;if(this.pending===0){this._finish();return}const t=this.pending;if(t<=this.syncLimit&&(this._slice(this.syncLimit===1/0?1/0:this.sliceMs),this.pending===0)){this._finish();return}this.running=!0,this._total=t;const e=()=>{this._slice(this.sliceMs),this.pending>0?(this.onProgress?.({remaining:this.pending,total:Math.max(this._total,this.pending)}),this._later(e)):(this.running=!1,this._finish())};this._later(e)}_slice(t){const e=t!==1/0;let s=e?performance.now():0;const n=s+t;let i=1,r=1;for(;this.qHead<this.qLen;){const a=this.queue[this.qHead++],o=this.queue[this.qHead++],l=this.queue[this.qHead++],c=this.sheetsByUid.get(a);if(c!==void 0&&(this._run(c,o,l),e&&--r===0)){const u=performance.now();if(u>n)break;i=u-s<.5?Math.min(i*2,16):1,r=i,s=u}}this.qHead>=this.qLen&&(this.qHead=0,this.qLen=0)}_finish(){for(;this.cycleFound.length>0;)this._iterateCycles();const t=this.tablesForced;this.tablesForced=!1,this.tablesDirty&&this.dataTables.length>0&&(!this.skipTables||t)&&(this.tablesDirty=!1,this._computeDataTables()),this.onChange?.();const e=this.waiters;this.waiters=[];for(const s of e)s()}_dataTableTemplate(){const t=this;return{fn:(s,n,i)=>t._plainValue(i,s,n),refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1}}_plainValue(t,e,s){const n=t.value(e,s);switch(t.kind(e,s)&V){case U:return n;case B:return this.book.strings.get(n);case G:return n!==0;case N:return Y[n];default:return null}}_writePlain(t,e,s,n){const i=t.kind(e,s)&R;n==null?t.setCell(e,s,i|U,0):typeof n=="number"?Number.isFinite(n)?t.setCell(e,s,i|U,n):t.setCell(e,s,i|N,k.NUM.index):typeof n=="boolean"?t.setCell(e,s,i|G,n?1:0):n instanceof x?t.setCell(e,s,i|N,n.index):t.setCell(e,s,i|B,this.book.strings.add(String(n)))}_setInputs(t,e){const s=new Uint32Array(e.length),n=new Uint32Array(e.length);e.forEach((i,r)=>{t.setCell(i.row,i.col,i.kind,i.value),s[r]=i.row,n[r]=i.col}),this.cellsChanged(t,s,n,e.length),this._slice(1/0)}evaluateWith(t,e,s){const n=e.map(({row:r,col:a})=>({row:r,col:a,kind:t.kind(r,a)&(V|R),value:t.value(r,a)})),i=this.inTables;this.inTables=!0;try{return this._setInputs(t,e.map(({row:r,col:a,text:o})=>{const l=Number(o);return o!==""&&Number.isFinite(l)?{row:r,col:a,kind:U,value:l}:{row:r,col:a,kind:B,value:this.book.strings.add(String(o))}})),s()}finally{this._setInputs(t,n),this.inTables=i}}_computeDataTables(){this.inTables=!0;const t=this.dataTables.filter(e=>e.sheet.formula(e.row,e.col)===e.info&&this.book.sheets.includes(e.sheet));this.dataTables=t;try{for(const e of t)this._computeDataTable(e)}finally{this.inTables=!1}}_computeDataTable({sheet:t,info:e}){const s=P(e.ref),n=P(e.r1??"");if(!s||!n)return;const i=e.r2?P(e.r2):null,r=e.dt2D==="1"||e.dt2D==="true",a=e.dtr==="1"||e.dtr==="true",o=(h,y)=>({kind:t.kind(h,y)&V,value:t.value(h,y)}),c=[n,i].filter(Boolean).map(h=>({row:h.r0,col:h.c0,kind:t.kind(h.r0,h.c0)&(V|R),value:t.value(h.r0,h.c0)})),u=[];for(let h=s.r0;h<=s.r1;h++)for(let y=s.c0;y<=s.c1;y++){let _,g;r?(_=[{row:n.r0,col:n.c0,...o(s.r0-1,y)},{row:i.r0,col:i.c0,...o(h,s.c0-1)}],g=[s.r0-1,s.c0-1]):a?(_=[{row:n.r0,col:n.c0,...o(s.r0-1,y)}],g=[h,s.c0-1]):(_=[{row:n.r0,col:n.c0,...o(h,s.c0-1)}],g=[s.r0-1,y]),this._setInputs(t,_);let b=this.read(t,g[0],g[1]);b instanceof C&&(b=b.data[0]),u.push(h,y,b)}this._setInputs(t,c);const d=u.length/3,p=new Uint32Array(d),f=new Uint32Array(d);for(let h=0;h<d;h++)p[h]=u[h*3],f[h]=u[h*3+1],this._writePlain(t,p[h],f[h],u[h*3+2]);this.cellsChanged(t,p,f,d),this._slice(1/0),t.revision++}dataTableAt(t,e,s){for(const n of this.dataTables){if(n.sheet!==t)continue;const i=P(n.info.ref);if(i&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1)return n}return null}calculateNow(){const{manual:t,skipTables:e}=this;this.manual=!1,this.skipTables=!1,this.tablesDirty=!0,this.tablesForced=!0,this._markVolatile(),this._markCycles(),this._schedule(),this.manual=t,this.skipTables=e}whenIdle(){return this.pending===0&&this.graphReady&&!this.running?Promise.resolve():new Promise(t=>{this.waiters.push(t),this._schedule()})}_iterateCycles(){const t=this.cycleFound;this.cycleFound=[];const e=new Map,s=(a,o,l)=>{let c=e.get(a.uid);c===void 0&&(c=new Map,e.set(a.uid,c)),c.set(o*M+l,[a,o,l])};for(let a=0;a<t.length;a+=3)s(t[a],t[a+1],t[a+2]);const n=[];for(const a of this.book.sheets){const o=e.get(a.uid);o&&n.push(...[...o.keys()].sort((l,c)=>l-c).map(l=>o.get(l)))}this.cycleMembers??=new Map;for(const a of n)this.cycleMembers.set(`${a[0].uid}:${a[1]}:${a[2]}`,a);const{count:i,delta:r}=this.iterate;for(let a=1;a<=i;a++){let o=0;for(const[l,c,u]of n){const d=l._chunk(c,u);if(d===void 0)continue;const p=c&T;if(!(d.kinds[p]&R))continue;const f=d.values[p];d.kinds[p]|=w,this.iterating=!0,this._run(l,c,u),this.iterating=!1;const h=Math.abs(d.values[p]-f);h>o&&(o=h)}if(this.onProgress?.({iteration:a,iterations:i}),o<r)break}for(const[a,o,l]of n)this._dirtyFrom(a,l,l,o,o);for(const[a,o,l]of n){const c=a._chunk(o,l);c!==void 0&&(c.kinds[o&T]&=~w)}this._slice(1/0)}_markCycles(){if(!(this.iterate===null||this.cycleMembers===null))for(const[t,e,s]of this.cycleMembers.values()){const n=t._chunk(e,s);n===void 0||!(n.kinds[e&T]&R)||(n.kinds[e&T]|=w,this._enqueue(t,e,s))}}_run(t,e,s){const n=[t,e,s];for(;n.length>0;){const i=n[n.length-1],r=n[n.length-2],a=n[n.length-3];try{this.depth=0,this.evalTop=0,this.cycleLow=tt,this._calc(a,r,i),n.length-=3}catch(o){if(o!==Z)throw o;n.push(this.pendingSheet,this.pendingRow,this.pendingCol)}}}_calc(t,e,s){const n=t._chunk(e,s);if(n===void 0)return;const i=e&T,r=n.kinds[i];if(!(r&w))return;if(r&X){this.circular={sheet:t,row:e,col:s},this.iterating||this._cycleHit(t.uid,e,s);return}if(this.depth>=ht)throw this.pendingSheet=t,this.pendingRow=e,this.pendingCol=s,Z;const a=t.formula(e,s);if(a===void 0||!(r&R)){n.kinds[i]=r&~w;return}const o=this._templateFor(t,e,s,a);if(o.external){n.kinds[i]=r&~w;return}n.kinds[i]=r|X;const l=this.evalTop;l+3>this.evalStack.length&&this._growEvalStack(),this.evalStack[l]=t.uid,this.evalStack[l+1]=e,this.evalStack[l+2]=s,this.evalTop=l+3;const c=this.cycleLow;this.cycleLow=tt;const u=this.legacy,d=this.frame,p=a.typed!==!0&&a.t!=="array";this.legacy=p,this.depth++;let f;try{f=o.fn(e,s,t)}catch(y){if(n.kinds[i]&=~X,this.depth--,this.legacy=u,this.frame=d,this.evalTop=l,this.cycleLow=c,y===Z)throw y;y instanceof x?f=y:(console.error("Formula evaluation failed",y),f=k.VALUE),this._store(t,n,i,e,s,f,a,p);return}this.depth--,this.legacy=u,this.frame=d,this.evalTop=l;const h=this.cycleLow;if(this.cycleLow=h<l&&h<c?h:c,h<=l){if(this.iterate!==null){n.kinds[i]=r&~(w|X),this.cycleFound.push(t,e,s);return}f=0}this._store(t,n,i,e,s,f,a,p)}_cycleHit(t,e,s){const n=this.evalStack;for(let i=this.evalTop-3;i>=0;i-=3)if(n[i]===t&&n[i+1]===e&&n[i+2]===s){i<this.cycleLow&&(this.cycleLow=i);return}}_growEvalStack(){const t=new Int32Array(this.evalStack.length*2);t.set(this.evalStack),this.evalStack=t}_store(t,e,s,n,i,r,a,o){let l=r;(l instanceof q||l instanceof $)&&(l=o?this.refAuto(l,n,i):this.refToValue(l)),l instanceof kt&&(l=k.CALC),l instanceof C?a.t==="array"&&!a.cm&&a.ref?l=this._fillFixedArray(t,n,i,a,l):o||l.rows===1&&l.cols===1?(l=l.data[0],t.spills&&this._releaseSpill(t,n,i)):l=this._spill(t,n,i,a,l):t.spills&&!(a.t==="array"&&!a.cm)&&this._releaseSpill(t,n,i),a.t==="array"&&!a.cm&&a.ref&&!(r instanceof C)&&(l=this._fillFixedArray(t,n,i,a,C.filled(1,1,l)));let c=R,u=0;typeof l=="number"?Number.isFinite(l)?(c|=U,u=l):(c|=N,u=k.NUM.index):typeof l=="string"?(c|=B,u=this.book.strings.add(l)):typeof l=="boolean"?(c|=G,u=l?1:0):l instanceof x?(c|=N,u=l.index):l instanceof O?(c|=N,u=k.VALUE.index):c|=U,l instanceof O?ft(t,n*M+i,at(this.book,l.index)):t.valueMeta!==null&&((e.kinds[s]&ot)!==c||e.values[s]!==u)&&!/IMAGE\(/i.test(a.f??a.sf??"")&&Tt(t,n*M+i),e.kinds[s]=c,e.values[s]=u,n>t.maxRow&&(t.maxRow=n),i>t.maxCol&&(t.maxCol=i)}_writeArrayCell(t,e,s,n){let i=H,r=0;typeof n=="number"?(i|=Number.isFinite(n)?U:N,r=Number.isFinite(n)?n:k.NUM.index):typeof n=="string"?(i|=B,r=this.book.strings.add(n)):typeof n=="boolean"?(i|=G,r=n?1:0):n instanceof x?(i|=N,r=n.index):n==null?i|=U:(i|=N,r=k.VALUE.index,n instanceof O&&ft(t,e*M+s,at(this.book,n.index))),t.setCell(e,s,i,r)}_fillFixedArray(t,e,s,n,i){const r=P(n.ref);if(r===null)return i.data[0];t.spills||(t.spills=new Map),t.spills.set(e*M+s,{...r,blocked:!1,fixed:!0});const a=(l,c)=>{const u=i.rows===1?0:l,d=i.cols===1?0:c;return u>=i.rows||d>=i.cols?k.NA:i.get(u,d)};for(let l=r.r0;l<=r.r1;l++)for(let c=r.c0;c<=r.c1;c++)l===e&&c===s||this._writeArrayCell(t,l,c,a(l-r.r0,c-r.c0));this._dirtyFrom(t,r.c0,r.c1,r.r0,r.r1);const o=a(e-r.r0,s-r.c0);return o===null?0:o}_spill(t,e,s,n,i){const r=e*M+s;t.spills||(t.spills=new Map);const a=t.spills.get(r),o=e+i.rows-1,l=s+i.cols-1,c={r0:e,c0:s,r1:o,c1:l,blocked:!1,fixed:!1},u=(f,h)=>a&&f>=a.r0&&f<=a.r1&&h>=a.c0&&h<=a.c1;let d=o>=J||l>=M;if(!d)t:for(let f=e;f<=o;f++)for(let h=s;h<=l;h++){if(f===e&&h===s)continue;const y=t.kind(f,h);if((y&ot)!==0&&!(y&H&&u(f,h))){d=!0;break t}if(t.merges.length>0&&t.mergeAt(f,h)){d=!0;break t}}if(d)return a&&!a.blocked&&this._clearSpill(t,a,e,s),c.blocked=!0,c.r1=Math.min(o,J-1),c.c1=Math.min(l,M-1),t.spills.set(r,c),k.SPILL;if(a&&!a.blocked){for(let f=a.r0;f<=a.r1;f++)for(let h=a.c0;h<=a.c1;h++)f>=c.r0&&f<=c.r1&&h>=c.c0&&h<=c.c1||(t.kind(f,h)&H)!==0&&t.setCell(f,h,Q,0);this._dirtyFrom(t,a.c0,a.c1,a.r0,a.r1)}t.spills.set(r,c);for(let f=e;f<=o;f++)for(let h=s;h<=l;h++)f===e&&h===s||this._writeArrayCell(t,f,h,i.get(f-e,h-s));n.t="array",n.ref=`${ut(s)}${e+1}:${ut(l)}${o+1}`,n.cm=1,delete n.raw,this._dirtyFrom(t,s,l,e,o);const p=i.data[0];return p===null?0:p}_clearSpill(t,e,s,n){for(let i=e.r0;i<=e.r1;i++)for(let r=e.c0;r<=e.c1;r++)i===s&&r===n||(t.kind(i,r)&H)!==0&&t.setCell(i,r,Q,0);this._dirtyFrom(t,e.c0,e.c1,e.r0,e.r1)}_releaseSpill(t,e,s){const n=e*M+s,i=t.spills.get(n);if(i===void 0)return;i.blocked||this._clearSpill(t,i,e,s),t.spills.delete(n);const r=t.formula(e,s);r&&r.typed&&(delete r.ref,delete r.cm,delete r.t)}spillMaster(t,e,s){if(!t.spills)return null;for(const[n,i]of t.spills)if(!i.blocked&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1){const r=Math.floor(n/M);return{row:r,col:n-r*M,rect:i}}return null}spillRange(t){const{sheet:e,r0:s,c0:n}=t;this.read(e,s,n);const i=e.spills?.get(s*M+n);return!i||i.blocked?k.REF:new q(e,i.r0,i.c0,i.r1,i.c1)}read(t,e,s){const n=t.columns[s];if(n===void 0)return null;const i=n[e>>>E];if(i===void 0)return null;const r=e&T;let a=i.kinds[r];if(a===0)return null;switch(a&w&&(this._calc(t,e,s),a=i.kinds[r]),a&V){case U:return i.values[r];case B:return this.book.strings.get(i.values[r]);case G:return i.values[r]!==0;case N:{if(t.valueMeta!==null&&this.book.richValues){const o=gt(this.book,t,e,s);if(o!==null)return o}return Y[i.values[r]]??k.NA}default:return null}}refToValue(t){return t instanceof $?t.refs.length===1?this.refToValue(t.refs[0]):k.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.toArr(t)}refAuto(t,e,s){return t instanceof $?t.refs.length===1?this.refAuto(t.refs[0],e,s):k.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.legacy?this.intersect(t,e,s):this.toArr(t)}intersect(t,e,s){return t.cols===1&&e>=t.r0&&e<=t.r1?this.read(t.sheet,e,t.c0):t.rows===1&&s>=t.c0&&s<=t.c1?this.read(t.sheet,t.r0,s):k.VALUE}trim(t){const{sheet:e}=t;return{r0:t.r0,c0:t.c0,r1:Math.min(t.r1,Math.max(e.maxRow,t.r0)),c1:Math.min(t.c1,Math.max(e.maxCol,t.c0))}}toArr(t){if(t instanceof C)return t;if(t instanceof q){const{r0:e,c0:s,r1:n,c1:i}=this.trim(t),r=n-e+1,a=i-s+1,o=new Array(r*a);let l=0;for(let c=e;c<=n;c++)for(let u=s;u<=i;u++)o[l++]=this.read(t.sheet,c,u);return new C(r,a,o)}if(t instanceof $){if(t.refs.length===1)return this.toArr(t.refs[0]);throw k.VALUE}if(t instanceof x)throw t;return new C(1,1,[t])}scalar(t,e,s){return t instanceof q?t.isCell?this.read(t.sheet,t.r0,t.c0):this.intersect(t,e,s):t instanceof $?k.VALUE:t instanceof C?t.data[0]:t}scan(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);for(let r=t.c0;r<=n;r++){const a=s.columns[r];if(a!==void 0)for(let o=t.r0>>>E;o<=i>>>E;o++){const l=a[o];if(l===void 0)continue;const c=o<<E,u=Math.max(t.r0-c,0),d=Math.min(i-c,T),{kinds:p}=l;for(let f=u;f<=d;f++){if(p[f]===0)continue;const h=this.read(s,c+f,r);h!==null&&e(h,c+f,r)}}}}sumRange(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);let{sum:r,count:a}=e;for(let o=t.c0;o<=n;o++){const l=s.columns[o];if(l!==void 0)for(let c=t.r0>>>E;c<=i>>>E;c++){const u=l[c];if(u===void 0)continue;const d=c<<E,p=Math.max(t.r0-d,0),f=Math.min(i-d,T),{kinds:h,values:y}=u;for(let _=p;_<=f;_++){let g=h[_];if(g===0)continue;g&w&&(this._calc(s,d+_,o),g=h[_]);const b=g&V;if(b===U)r+=y[_],a++;else if(b===N)throw Y[y[_]]}}}e.sum=r,e.count=a}unary(t,e){if(t instanceof x)return t;if(t instanceof C)return new C(t.rows,t.cols,t.data.map(s=>{try{return s instanceof x?s:e(S(s))}catch(n){return n}}));try{return e(S(t))}catch(s){if(s instanceof x)return s;throw s}}slowBin(t,e,s){if(e instanceof C||s instanceof C)return this._binArr(t,e,s);if(e instanceof x)return e;if(s instanceof x)return s;try{return dt(t,e,s)}catch(n){if(n instanceof x)return n;throw n}}_binArr(t,e,s){const n=e instanceof C?e:null,i=s instanceof C?s:null,r=Math.max(n?n.rows:1,i?i.rows:1),a=Math.max(n?n.cols:1,i?i.cols:1),o=new Array(r*a);for(let l=0;l<r;l++)for(let c=0;c<a;c++){const u=nt(e,l,c),d=nt(s,l,c);let p;if(u instanceof x)p=u;else if(d instanceof x)p=d;else try{p=dt(t,u,d)}catch(f){p=f}o[l*a+c]=p}return new C(r,a,o)}liftArr(t,e,s){let n=1,i=1;for(const l of e)l instanceof C&&(l.rows>n&&(n=l.rows),l.cols>i&&(i=l.cols));const r=new Array(n*i),a=e.length,o=new Array(a);for(let l=0;l<n;l++)for(let c=0;c<i;c++){let u=null;for(let d=0;d<a;d++){const p=nt(e[d],l,c);o[d]=p,!s&&u===null&&p instanceof x&&(u=p)}if(u!==null){r[l*i+c]=u;continue}try{r[l*i+c]=t(...o)}catch(d){if(d instanceof x)r[l*i+c]=d;else throw d}}return new C(n,i,r)}invoke(t,e,s,n,i){if(t.native)return this._invokeNative(t.native,e,s,n,i);const r=t.frame?t.frame.slice():[],{params:a}=t;if(e.length>a.length)return k.VALUE;for(let l=0;l<a.length;l++)r[a[l]]=l<e.length?e[l]:void 0;const o=this.frame;if(this.frame=r,this.depth++,this.depth>=ht*4)return this.depth--,this.frame=o,k.NUM;try{return t.body(s,n,i)}finally{this.depth--,this.frame=o}}_invokeNative(t,e,s,n,i){if(e.length<t.min||e.length>t.max)return k.VALUE;try{if(t.kind==="raw")return t.fn(this,e,s,n,i);if(t.kind==="lazy")return t.fn(this,e.map(a=>()=>a),s,n,i);const r=e.map(a=>a instanceof q||a instanceof $?this.refToValue(a):a);if(r.some(a=>a instanceof C))return this.liftArr(t.fn,r,t.errors);if(!t.errors){for(const a of r)if(a instanceof x)return a}return t.fn(...r)}catch(r){if(r instanceof x)return r;throw r}}_nameEntry(t,e){const s=t.toUpperCase();if(e){const i=this.book.sheets.indexOf(e),r=this.names.get(`${i}!${s}`);if(r)return{key:`${i}!${s}`,entry:r}}const n=this.names.get(s);return n?{key:s,entry:n}:null}_nameTemplate(t,e){const s=this._nameEntry(t,e);if(s===null)return null;let n=this.nameTemplates.get(s.key);if(n===void 0){try{n=this._compile(new j(z(s.entry.text),0,0).parse())}catch(i){if(!(i instanceof W))throw i;n=this._broken()}this.nameTemplates.set(s.key,n)}return n}nameMayYieldRange(t){const e=t.toUpperCase(),s=[];for(const[n,i]of this.names)(n===e||n.endsWith(`!${e}`))&&s.push(i);if(s.length===0||(this._rangeNames??=new Set,this._rangeNames.has(e)))return!0;this._rangeNames.add(e);try{return s.some(n=>{try{return new lt(this).mayYieldRange(new j(z(n.text),0,0).parse())}catch(i){if(i instanceof W)return!1;throw i}})}finally{this._rangeNames.delete(e)}}evalName(t,e,s,n){const i=this._nameTemplate(t,n);return i===null?k.NAME:i.fn(e,s,n)}tableSpecToA1(t,e,s){const n=mt(t,e,s);if(!(n instanceof q))return null;const i=/#This Row|^\[\s*@/i.test(e),r=(a,o)=>i?`${ct(o)}${a+1}`:`$${ct(o)}$${a+1}`;return n.isCell?r(n.r0,n.c0):`${r(n.r0,n.c0)}:${r(n.r1,n.c1)}`}resolveTable(t,e,s,n,i){let r=t?this.tables.get(t.toUpperCase()):null;if(!t){for(const a of this.tables.values())if(a.sheet===i&&s>=a.r0&&s<=a.r1&&n>=a.c0&&n<=a.c1){r=a;break}}return r?mt(r,e,s):k.REF}exactIndex(t){const e=`${t.sheet.uid}:${t.r0}:${t.c0}:${t.r1}:${t.c1}`;let s=this.lookupCache.get(e);if(s!==void 0)return s;s=new Map;const n=t.cols===1;return this.scan(t,(i,r,a)=>{const o=typeof i=="string"?`s${i.toLowerCase()}`:typeof i=="number"?i:`b${i}`;if(i instanceof x)return;const l=n?r-t.r0:a-t.c0,c=s.get(o);(c===void 0||l<c)&&s.set(o,l)}),this.lookupCache.set(e,s),s}visibilityChanged(){this._wakeReaders(this.visibilityReaders)}presentationChanged(){this._wakeLater(2)}axisHiddenChanged(){this._wakeLater(1)}axisResized(){this._wakeLater(2)}_wakeLater(t){(t===1?this.visibilityReaders:this.styleReaders).size!==0&&(this.wakePending===0&&queueMicrotask(()=>{const e=this.wakePending;this.wakePending=0,e&1&&this._wakeReaders(this.visibilityReaders),e&2&&this._wakeReaders(this.styleReaders)}),this.wakePending|=t)}_wakeReaders(t){if(!this.graphReady||t.size===0)return;let e=!1;for(const[s,n]of t)n.forEach((i,r)=>{const a=s._chunk(i,r);if(a===void 0)return;const o=i&T,l=a.kinds[o];if(!(l&R)){n.delete(i,r);return}l&w||(a.kinds[o]=l|w,this._enqueue(s,i,r)),this._dirtyFrom(s,r,r,i,i),e=!0});e&&this._schedule()}isSubtotalCell(t,e,s){if((t.kind(e,s)&R)===0)return!1;const n=t.formula(e,s),i=n?.f??n?.sf??"";return/^\s*(_xlfn\.)?(SUBTOTAL|AGGREGATE)\(/i.test(i)}displayFormula(t){return Mt(t)}}function ut(m){let t="";for(let e=m+1;e>0;e=Math.floor((e-1)/26))t=String.fromCharCode(65+(e-1)%26)+t;return t}function nt(m,t,e){if(!(m instanceof C))return m;const s=m.rows===1?0:t,n=m.cols===1?0:e;return s>=m.rows||n>=m.cols?k.NA:m.data[s*m.cols+n]}function dt(m,t,e){if((t instanceof O||e instanceof O)&&m!=="="&&m!=="<>"&&m!=="&")throw k.VALUE;switch(m){case"+":{const s=S(t),n=s+S(e);return Math.abs(n)<Math.abs(s)*pt?0:K(n)}case"-":{const s=S(t),n=s-S(e);return Math.abs(n)<Math.abs(s)*pt?0:K(n)}case"*":return K(S(t)*S(e));case"/":{const s=S(t),n=S(e);if(n===0)throw k.DIV0;return K(s/n)}case"^":{const s=S(t),n=S(e);if(s===0&&n===0)throw k.NUM;if(s===0&&n<0)throw k.DIV0;const i=_t(s,n);if(Number.isNaN(i))throw k.NUM;return K(i)}case"&":return rt(t)+rt(e);case"=":return v(t,e)===0;case"<>":return v(t,e)!==0;case"<":return v(t,e)<0;case">":return v(t,e)>0;case"<=":return v(t,e)<=0;case">=":return v(t,e)>=0;default:throw k.VALUE}}const pt=5e-16;function K(m){if(!Number.isFinite(m))throw k.NUM;return m}function mt(m,t,e){const s=yt(m,t,e);return s==="REF"?k.REF:s==="VALUE"?k.VALUE:new q(m.sheet,s.top,s.left,s.bottom,s.right)}const Lt={COUNTIF:[0],COUNTIFS:["even",0],SUMIF:[0,2],SUMIFS:[0,"odd",1],AVERAGEIF:[0,2],AVERAGEIFS:[0,"odd",1],MAXIFS:[0,"odd",1],MINIFS:[0,"odd",1],COUNTBLANK:[0],RANK:[1],"RANK.EQ":[1],"RANK.AVG":[1]};function St(m,t){for(let e=0;e<m.length;e++){const s=m[e];if(s===t||s==="odd"&&t>=m[e+1]&&t%2===1||s==="even"&&t>=m[e+1]&&t%2===0)return!0}return!1}function it(m){if(!(m===null||typeof m!="object")){if(Array.isArray(m)){for(const t of m)it(t);return}if(m.t==="call"){const t=Lt[String(m.name).replace(/^_xl(?:fn|ws)\./i,"").toUpperCase()];t&&m.args.forEach((e,s)=>{if(e?.t==="array"&&St(t,s))throw new Error(`${m.name} takes a range here`)})}for(const t of Object.values(m))t!==null&&typeof t=="object"&&it(t)}}export{Bt as Engine};
1
+ import{resolveStructuredSpec as kt}from"../slicers/structured.js";import{Arr as A,compareValues as v,ERR as k,ERR_BY_INDEX as Q,Lambda as _t,num as S,power as gt,Ref as q,RefList as $,RichValue as O,str as rt,XlError as C}from"./values.js";import{entityVm as at,richValueAt as bt}from"../cells/rich-values.js";import{Compiler as lt}from"./compile.js";import{formulaEntryLimit as wt,FormulaSyntaxError as P,Parser as z,templateKey as Mt,tokenize as X,toDisplayFormula as At}from"./parser.js";import{BOOL as H,CHUNK_BITS as L,CHUNK_MASK as x,CONTENT_MASK as ot,DIRTY as M,EMPTY as J,EVALUATING as Y,FORMULA as T,KIND_MASK as V,NUM as F,SPILL as K,STR as B,ERR as E}from"../core/sheet.js";import{colName as ct,MAX_COLS as w,MAX_ROWS as Z,parseRangeRef as G}from"../core/address.js";import{CellSet as Ct}from"./cell-set.js";import{isRowBelow as Rt}from"./fill-shape.js";import{FUNCS as xt}from"./functions/registry.js";import"./functions/index.js";const Tt=new Set(["range","rows","cols","rangeOp","intersect","union"]),tt=Symbol("pending"),ht=200,St=4096,Lt=64,et=2147483647,D=4;class st{constructor(){this.n=0,this.data=new Int32Array(D*8)}push(t,e,s,n){if((this.n+1)*D>this.data.length){const r=new Int32Array(this.data.length*2);r.set(this.data),this.data=r}const i=this.n++*D,a=this.data;a[i]=t,a[i+1]=e,a[i+2]=s,a[i+3]=n}}function Et(m,t){if(!m.valueMeta.has(t))return;const e=new Map(m.valueMeta);e.delete(t),m.valueMeta=e.size>0?e:null}function ft(m,t,e){if(m.valueMeta?.get(t)===e)return;const s=new Map(m.valueMeta??[]);s.set(t,e),m.valueMeta=s}function W(m,t){let e=m.get(t);return e===void 0&&(e=new Ct,m.set(t,e)),e}class Nt{constructor(){this.buckets=[],this.wide=[],this.all=new st,this.allCols=[]}}class Kt{constructor(t){this.book=t,this.sheetGen=0,this.sheetMap=new Map,this.deps=new Map,this.templates=new Map,this.lastOwn={row:-2,col:-1,text:"",tpl:{cacheKey:void 0}},this.frame=null,this.depth=0,this.legacy=!1,this.pendingSheet=null,this.pendingRow=0,this.pendingCol=0,this.circular=null,this.evalStack=new Int32Array(3*256),this.evalTop=0,this.cycleLow=et,this.iterate=null,this.cycleFound=[],this.cycleMembers=null,this.iterating=!1,this.sliceMs=10,this.syncLimit=4e3,this.externalLinks=null,this.names=new Map,this.nameTemplates=new Map,this.tables=new Map,this.volatile=new Map,this.visibilityReaders=new Map,this.sheetReaders=new Map,this.styleReaders=new Map,this.wakePending=0,this.queue=new Int32Array(4096),this.qHead=0,this.qLen=0,this.sheetsByUid=new Map,this.running=!1,this.timer=0,this.manual=!1,this.graphReady=!0,this.pendingChanges=[],this.waiters=[],this.onChange=null,this.dataTables=[],this.tablesDirty=!1,this.inTables=!1,this.tablesForced=!1,this.onProgress=null,this.lookupCache=new Map,this.colRevisions=new Map,this.channel=null,this.sheetsChanged()}orderChanged(){this.sheetsChanged(),this.has3D&&this.rebuild()}sheetsChanged(){this.sheetGen++,this.sheetMap.clear(),this.sheetsByUid.clear();for(const t of this.book.sheets)this.sheetMap.set(t.name.toUpperCase(),t),this.sheetsByUid.set(t.uid,t),t.rows.watcher=this,t.cols.watcher=this;this._wakeReaders(this.sheetReaders)}sheetByName(t){return this.sheetMap.get(t.toUpperCase())??null}externalSheet(t,e){return this.externalLinks?.sheet(t,e)??null}sheetsBetween(t,e){const{sheets:s}=this.book,n=s.findIndex(a=>a.name.toUpperCase()===t.toUpperCase()),i=s.findIndex(a=>a.name.toUpperCase()===e.toUpperCase());return n<0||i<0?null:s.slice(Math.min(n,i),Math.max(n,i)+1)}setNames(t){this.names.clear(),this.nameTemplates.clear();for(const e of t){const s=e.localSheetId!==void 0?`${e.localSheetId}!${e.name.toUpperCase()}`:e.name.toUpperCase();this.names.set(s,{text:e.text,name:e.name,hidden:e.hidden,comment:e.comment})}}setTables(t){this.tables.clear();for(const e of t)this.tables.set(e.name.toUpperCase(),e)}template(t,e,s){let n;try{n=X(t)}catch(r){if(r instanceof P)return this._broken(t);throw r}const i=Mt(n,e,s);let a=this.templates.get(i);if(a===void 0){try{const r=new z(n,e,s).parse();a=this._compile(r)}catch(r){if(!(r instanceof P))throw r;a=this._broken(t)}a.cacheKey=i,this.templates.set(i,a)}return a}_ownTemplate(t,e,s){const n=this.lastOwn;let i;return n.row===e-1&&n.col===s&&this.templates.get(n.tpl.cacheKey)===n.tpl&&Rt(n.text,t)?i=n.tpl:i=this.template(t,e,s),n.row=e,n.col=s,n.text=t,n.tpl=i,i}_compile(t){const e=new lt(this),s=e.compileTop(t);for(;t.t==="paren";)t=t.e;return{fn:s,refs:e.refs,names:e.names,tables:e.tables,isVolatile:e.isVolatile,readsSheets:e.readsSheets,notInExcel:e.notInExcel===!0,mayArray:e.mayArray,rangeResult:Tt.has(t.t)||t.t==="call"&&xt.get(t.name)?.refResult===!0,external:e.external}}_broken(){return{invalid:!0,fn:()=>k.NAME,refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1,external:!0}}validate(t){const e=wt(t);if(e)throw new P(e);it(new z(X(t),0,0).parse())}_templateFor(t,e,s,n){if(n.tpl!==void 0)return n.tpl;let i;return n.t==="dataTable"?i=this._dataTableTemplate():n.f!==void 0&&n.base===void 0?i=this._ownTemplate(n.f,e,s):n.f!==void 0?i=this.template(n.f,Math.floor(n.base/w),n.base%w):n.sf!==void 0?i=this.template(n.sf,Math.floor(n.base/w),n.base%w):i=this._broken(),n.tpl=i,i}_index(t){let e=this.deps.get(t);return e===void 0&&(e=new Nt,this.deps.set(t,e)),e}_addDep(t,e,s,n,i,a,r,o){const l=this._index(t),c=a.uid<<14|o;if(i-s>=Lt){l.all.push(r,c,e,n),l.allCols.push(s,i);return}if(n-e>=St){for(let p=s;p<=i;p++){let f=l.wide[p];f===void 0&&(f=new st,l.wide[p]=f),f.push(r,c,e,n)}return}const u=e>>>L,d=n>>>L;for(let p=s;p<=i;p++){let f=l.buckets[p];f===void 0&&(f=[],l.buckets[p]=f);for(let h=u;h<=d;h++){let y=f[h];y===void 0&&(y=new st,f[h]=y),y.push(r,c,e,n)}}}register(t,e,s,n){const i=this._templateFor(t,e,s,n);if(i.external)return i;for(const a of i.refs)this._addRefDep(a,t,e,s);for(const a of i.names)this._addNameDeps(a,t,e,s);for(const a of i.tables)this._addTableDep(a,t,e,s);return i.readsSheets&&W(this.sheetReaders,t).add(e,s),i.isVolatile&&W(this.volatile,t).add(e,s),i.readsVisibility??=/SUBTOTAL|AGGREGATE|SPARK_/i.test(n.f??n.sf??""),i.readsVisibility&&W(this.visibilityReaders,t).add(e,s),i.readsStyles??=/SPARK_BLOCK/i.test(n.f??n.sf??""),i.readsStyles&&W(this.styleReaders,t).add(e,s),i}_addRefDep(t,e,s,n){const i=t.rAbs0?t.r0:s+t.r0,a=t.rAbs1?t.r1:s+t.r1,r=t.cAbs0?t.c0:n+t.c0,o=t.cAbs1?t.c1:n+t.c1,l=Math.max(0,Math.min(i,a)),c=Math.min(Z-1,Math.max(i,a)),u=Math.max(0,Math.min(r,o)),d=Math.min(w-1,Math.max(r,o));if(l>c||u>d)return;if(t.sheet?.sheet2&&(t.sheet.book===null||t.sheet.book===void 0)){this.has3D=!0;for(const f of this.sheetsBetween(t.sheet.sheet,t.sheet.sheet2)??[])this._addDep(f,l,u,c,d,e,s,n);return}if(t.sheet?.book!==null&&t.sheet?.book!==void 0)return;const p=t.sheet?this.sheetByName(t.sheet.sheet):e;p&&this._addDep(p,l,u,c,d,e,s,n)}_addNameDeps(t,e,s,n){const i=this._nameTemplate(t,e);if(i!==null){i.readsSheets&&W(this.sheetReaders,e).add(s,n);for(const a of i.refs)this._addRefDep(a,e,s,n);for(const a of i.tables)this._addTableDep(a,e,s,n);for(const a of i.names)a.toUpperCase()!==t.toUpperCase()&&this._addNameDeps(a,e,s,n)}}_addTableDep(t,e,s,n){const i=this.resolveTable(t.name,t.spec,s,n,e);i instanceof q&&this._addDep(i.sheet,i.r0,i.c0,i.r1,i.c1,e,s,n)}rebuild(){this.has3D=!1,this.deps.clear(),this.volatile.clear(),this.visibilityReaders.clear(),this.styleReaders.clear(),this.sheetReaders.clear(),this.lookupCache.clear(),this.qHead=0,this.qLen=0,this.cycleFound=[],this.cycleMembers=null,this.sheetGen++,this.buildGraph({fullCalc:!0})}buildGraph({fullCalc:t=!1,onDone:e}={}){this.graphReady=!1,this.dataTables=[],t&&(this.tablesDirty=!0);const s=this,n=this.sheetGen,i=this.book.sheets.map(u=>[u,u.formulas]);let a=0,r=0,o=0,l=0;const c=()=>{const u=performance.now()+s.sliceMs;let d=0;for(;a<i.length;a++,r=0,o=0,l=0){const[f,h]=i[a],{columns:y}=h;for(;r<y.length;r++,o=0,l=0){const _=y[r];if(_!==void 0)for(;o<_.length;o++,l=0){const g=_[o];if(g===void 0)continue;const{ids:b}=g,N=o<<L;for(;l<=x;l++){const R=b[l];if(R===0)continue;const U=h.infos[R],I=N+l;U.t==="dataTable"&&s.dataTables.push({sheet:f,row:I,col:r,info:U});const yt=s.register(f,I,r,U);if(s._initFormula(f,I,r,U,yt,t),++d%256===0&&performance.now()>u){l++,s.onProgress?.({preparing:!0}),s._later(c);return}}}}}s.graphReady=!0,s.sheetGen!==n&&s._wakeReaders(s.sheetReaders);const p=s.pendingChanges;s.pendingChanges=[];for(const[f,h,y,_]of p)s.cellsChanged(f,h,y,_);s._schedule(),e?.()};c()}_initFormula(t,e,s,n,i,a){const r=t._chunk(e,s);if(r===void 0)return;const o=e&x;if(n.t==="array"&&n.ref&&this._markArrayChildren(t,e,s,n),i.external)return;const l=r.kinds[o];(a||i.isVolatile||(l&V)===J||i.notInExcel&&(l&V)===E)&&(r.kinds[o]=l|M,this._enqueue(t,e,s))}_markArrayChildren(t,e,s,n){const i=G(n.ref);if(i!==null){t.spills||(t.spills=new Map),t.spills.set(e*w+s,{...i,blocked:!1,fixed:!n.cm});for(let a=i.r0;a<=i.r1;a++)for(let r=i.c0;r<=i.c1;r++){if(a===e&&r===s)continue;const o=t._ensure(a,r);o.kinds[a&x]|=K}}}_enqueue(t,e,s){if(this.qLen+3>this.queue.length)if(this.qHead>this.queue.length>>>1)this.queue.copyWithin(0,this.qHead,this.qLen),this.qLen-=this.qHead,this.qHead=0;else{const n=new Int32Array(this.queue.length*2);n.set(this.queue),this.queue=n}this.queue[this.qLen++]=t.uid,this.queue[this.qLen++]=e,this.queue[this.qLen++]=s}_dirtyFrom(t,e,s,n,i){const a=[t,e,s,n,i];for(;a.length>0;){const r=a.pop(),o=a.pop(),l=a.pop(),c=a.pop(),u=a.pop(),d=this.deps.get(u);if(d===void 0)continue;const p=(y,_)=>{const g=this.sheetsByUid.get(_>>>14);if(g===void 0)return;const b=_&16383,N=g._chunk(y,b);if(N===void 0)return;const R=y&x,U=N.kinds[R];(U&T)===0||(U&M)!==0||(N.kinds[R]=U|M,this._enqueue(g,y,b),a.push(g,b,b,y,y))},f=o>>>L,h=r>>>L;for(let y=c;y<=l;y++){const _=d.buckets[y];if(_!==void 0)for(let b=f;b<=h;b++){const N=_[b];if(N===void 0)continue;const{data:R}=N,U=N.n*D;for(let I=0;I<U;I+=D)r>=R[I+2]&&o<=R[I+3]&&p(R[I],R[I+1])}const g=d.wide[y];if(g!==void 0){const{data:b}=g,N=g.n*D;for(let R=0;R<N;R+=D)r>=b[R+2]&&o<=b[R+3]&&p(b[R],b[R+1])}}if(d.all.n>0){const{data:y}=d.all;for(let _=0;_<d.all.n;_++){const g=_*D;r>=y[g+2]&&o<=y[g+3]&&l>=d.allCols[_*2]&&c<=d.allCols[_*2+1]&&p(y[g],y[g+1])}}}}cellsChanged(t,e,s,n){if(n===0)return;if(this.inTables||(this.tablesDirty=!0),!this.graphReady){this.pendingChanges.push([t,e.slice(0,n),s.slice(0,n),n]);return}this.lookupCache.clear();const i=new Map;for(let a=0;a<n;a++){const r=e[a],o=s[a],l=i.get(o);l===void 0?i.set(o,[r,r]):(r<l[0]&&(l[0]=r),r>l[1]&&(l[1]=r));const c=t._chunk(r,o);if(c===void 0)continue;const u=r&x,d=c.kinds[u];if(d&T){const p=t.formula(r,o);p&&(this.register(t,r,o,p),d&M||(c.kinds[u]=d|M,this._enqueue(t,r,o)))}t.spills&&this._spillTouched(t,r,o)}for(const[a,r]of i)this._dirtyFrom(t,a,a,r[0],r[1]);this._markVolatile(),this._schedule()}_spillTouched(t,e,s){const n=e*w+s;for(const[i,a]of t.spills){const r=Math.floor(i/w),o=i-r*w;if(i===n){(t.kind(r,o)&T)===0&&(this._clearSpill(t,a,r,o),t.spills.delete(i));continue}if(e>=a.r0&&e<=a.r1&&s>=a.c0&&s<=a.c1){const l=t._chunk(r,o);if(l===void 0)continue;const c=r&x,u=l.kinds[c];u&T&&!(u&M)&&(l.kinds[c]=u|M,this._enqueue(t,r,o),this._dirtyFrom(t,o,o,r,r))}}}_markVolatile(){for(const[t,e]of this.volatile)e.forEach((s,n)=>{const i=t._chunk(s,n);if(i===void 0)return;const a=s&x,r=i.kinds[a];if(!(r&T)){e.delete(s,n);return}r&M||(i.kinds[a]=r|M,this._enqueue(t,s,n),this._dirtyFrom(t,n,n,s,s))})}get pending(){return(this.qLen-this.qHead)/3}_later(t){if(this._next=t,typeof MessageChannel>"u"){setTimeout(()=>this._tick(),0);return}this.channel===null&&(this.channel=new MessageChannel,this.channel.port1.onmessage=()=>this._tick()),this.channel.port2.postMessage(0)}_tick(){const t=this._next;this._next=null,t?.(),this._next===null&&this.channel!==null&&(this.channel.port1.close(),this.channel=null)}_schedule(){if(this.manual||this.inTables||this.running||!this.graphReady)return;if(this.pending===0){this._finish();return}const t=this.pending;if(t<=this.syncLimit&&(this._slice(this.syncLimit===1/0?1/0:this.sliceMs),this.pending===0)){this._finish();return}this.running=!0,this._total=t;const e=()=>{this._slice(this.sliceMs),this.pending>0?(this.onProgress?.({remaining:this.pending,total:Math.max(this._total,this.pending)}),this._later(e)):(this.running=!1,this._finish())};this._later(e)}_slice(t){const e=t!==1/0;let s=e?performance.now():0;const n=s+t;let i=1,a=1;for(;this.qHead<this.qLen;){const r=this.queue[this.qHead++],o=this.queue[this.qHead++],l=this.queue[this.qHead++],c=this.sheetsByUid.get(r);if(c!==void 0&&(this._run(c,o,l),e&&--a===0)){const u=performance.now();if(u>n)break;i=u-s<.5?Math.min(i*2,16):1,a=i,s=u}}this.qHead>=this.qLen&&(this.qHead=0,this.qLen=0)}_finish(){for(;this.cycleFound.length>0;)this._iterateCycles();const t=this.tablesForced;this.tablesForced=!1,this.tablesDirty&&this.dataTables.length>0&&(!this.skipTables||t)&&(this.tablesDirty=!1,this._computeDataTables()),this.onChange?.();const e=this.waiters;this.waiters=[];for(const s of e)s()}_dataTableTemplate(){const t=this;return{fn:(s,n,i)=>t._plainValue(i,s,n),refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1}}_plainValue(t,e,s){const n=t.value(e,s);switch(t.kind(e,s)&V){case F:return n;case B:return this.book.strings.get(n);case H:return n!==0;case E:return Q[n];default:return null}}_writePlain(t,e,s,n){const i=t.kind(e,s)&T;n==null?t.setCell(e,s,i|F,0):typeof n=="number"?Number.isFinite(n)?t.setCell(e,s,i|F,n):t.setCell(e,s,i|E,k.NUM.index):typeof n=="boolean"?t.setCell(e,s,i|H,n?1:0):n instanceof C?t.setCell(e,s,i|E,n.index):t.setCell(e,s,i|B,this.book.strings.add(String(n)))}_setInputs(t,e){const s=new Uint32Array(e.length),n=new Uint32Array(e.length);e.forEach((i,a)=>{t.setCell(i.row,i.col,i.kind,i.value),s[a]=i.row,n[a]=i.col}),this.cellsChanged(t,s,n,e.length),this._slice(1/0)}evaluateWith(t,e,s){const n=e.map(({row:a,col:r})=>({row:a,col:r,kind:t.kind(a,r)&(V|T),value:t.value(a,r)})),i=this.inTables;this.inTables=!0;try{return this._setInputs(t,e.map(({row:a,col:r,text:o})=>{const l=Number(o);return o!==""&&Number.isFinite(l)?{row:a,col:r,kind:F,value:l}:{row:a,col:r,kind:B,value:this.book.strings.add(String(o))}})),s()}finally{this._setInputs(t,n),this.inTables=i}}_computeDataTables(){this.inTables=!0;const t=this.dataTables.filter(e=>e.sheet.formula(e.row,e.col)===e.info&&this.book.sheets.includes(e.sheet));this.dataTables=t;try{for(const e of t)this._computeDataTable(e)}finally{this.inTables=!1}}_computeDataTable({sheet:t,info:e}){const s=G(e.ref),n=G(e.r1??"");if(!s||!n)return;const i=e.r2?G(e.r2):null,a=e.dt2D==="1"||e.dt2D==="true",r=e.dtr==="1"||e.dtr==="true",o=(h,y)=>({kind:t.kind(h,y)&V,value:t.value(h,y)}),c=[n,i].filter(Boolean).map(h=>({row:h.r0,col:h.c0,kind:t.kind(h.r0,h.c0)&(V|T),value:t.value(h.r0,h.c0)})),u=[];for(let h=s.r0;h<=s.r1;h++)for(let y=s.c0;y<=s.c1;y++){let _,g;a?(_=[{row:n.r0,col:n.c0,...o(s.r0-1,y)},{row:i.r0,col:i.c0,...o(h,s.c0-1)}],g=[s.r0-1,s.c0-1]):r?(_=[{row:n.r0,col:n.c0,...o(s.r0-1,y)}],g=[h,s.c0-1]):(_=[{row:n.r0,col:n.c0,...o(h,s.c0-1)}],g=[s.r0-1,y]),this._setInputs(t,_);let b=this.read(t,g[0],g[1]);b instanceof A&&(b=b.data[0]),u.push(h,y,b)}this._setInputs(t,c);const d=u.length/3,p=new Uint32Array(d),f=new Uint32Array(d);for(let h=0;h<d;h++)p[h]=u[h*3],f[h]=u[h*3+1],this._writePlain(t,p[h],f[h],u[h*3+2]);this.cellsChanged(t,p,f,d),this._slice(1/0),t.revision++}dataTableAt(t,e,s){for(const n of this.dataTables){if(n.sheet!==t)continue;const i=G(n.info.ref);if(i&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1)return n}return null}calculateNow(){const{manual:t,skipTables:e}=this;this.manual=!1,this.skipTables=!1,this.tablesDirty=!0,this.tablesForced=!0,this._markVolatile(),this._markCycles(),this._schedule(),this.manual=t,this.skipTables=e}whenIdle(){return this.pending===0&&this.graphReady&&!this.running?Promise.resolve():new Promise(t=>{this.waiters.push(t),this._schedule()})}_iterateCycles(){const t=this.cycleFound;this.cycleFound=[];const e=new Map,s=(r,o,l)=>{let c=e.get(r.uid);c===void 0&&(c=new Map,e.set(r.uid,c)),c.set(o*w+l,[r,o,l])};for(let r=0;r<t.length;r+=3)s(t[r],t[r+1],t[r+2]);const n=[];for(const r of this.book.sheets){const o=e.get(r.uid);o&&n.push(...[...o.keys()].sort((l,c)=>l-c).map(l=>o.get(l)))}this.cycleMembers??=new Map;for(const r of n)this.cycleMembers.set(`${r[0].uid}:${r[1]}:${r[2]}`,r);const{count:i,delta:a}=this.iterate;for(let r=1;r<=i;r++){let o=0;for(const[l,c,u]of n){const d=l._chunk(c,u);if(d===void 0)continue;const p=c&x;if(!(d.kinds[p]&T))continue;const f=d.values[p];d.kinds[p]|=M,this.iterating=!0,this._run(l,c,u),this.iterating=!1;const h=Math.abs(d.values[p]-f);h>o&&(o=h)}if(this.onProgress?.({iteration:r,iterations:i}),o<a)break}for(const[r,o,l]of n)this._dirtyFrom(r,l,l,o,o);for(const[r,o,l]of n){const c=r._chunk(o,l);c!==void 0&&(c.kinds[o&x]&=~M)}this._slice(1/0)}_markCycles(){if(!(this.iterate===null||this.cycleMembers===null))for(const[t,e,s]of this.cycleMembers.values()){const n=t._chunk(e,s);n===void 0||!(n.kinds[e&x]&T)||(n.kinds[e&x]|=M,this._enqueue(t,e,s))}}_run(t,e,s){const n=[t,e,s];for(;n.length>0;){const i=n[n.length-1],a=n[n.length-2],r=n[n.length-3];try{this.depth=0,this.evalTop=0,this.cycleLow=et,this._calc(r,a,i),n.length-=3}catch(o){if(o!==tt)throw o;n.push(this.pendingSheet,this.pendingRow,this.pendingCol)}}}_calc(t,e,s){const n=t._chunk(e,s);if(n===void 0)return;const i=e&x,a=n.kinds[i];if(!(a&M))return;if(a&Y){this.circular={sheet:t,row:e,col:s},this.iterating||this._cycleHit(t.uid,e,s);return}if(this.depth>=ht)throw this.pendingSheet=t,this.pendingRow=e,this.pendingCol=s,tt;const r=t.formula(e,s);if(r===void 0||!(a&T)){n.kinds[i]=a&~M;return}const o=this._templateFor(t,e,s,r);if(o.external){n.kinds[i]=a&~M;return}n.kinds[i]=a|Y;const l=this.evalTop;l+3>this.evalStack.length&&this._growEvalStack(),this.evalStack[l]=t.uid,this.evalStack[l+1]=e,this.evalStack[l+2]=s,this.evalTop=l+3;const c=this.cycleLow;this.cycleLow=et;const u=this.legacy,d=this.frame,p=r.typed!==!0&&r.t!=="array";this.legacy=p,this.depth++;let f;try{f=o.fn(e,s,t),(f instanceof q||f instanceof $)&&(f=this.refAuto(f,e,s))}catch(y){if(n.kinds[i]&=~Y,this.depth--,this.legacy=u,this.frame=d,this.evalTop=l,this.cycleLow=c,y===tt)throw y;y instanceof C?f=y:(console.error("Formula evaluation failed",y),f=k.VALUE),this._store(t,n,i,e,s,f,r,p);return}this.depth--,this.legacy=u,this.frame=d,this.evalTop=l;const h=this.cycleLow;if(this.cycleLow=h<l&&h<c?h:c,h<=l){if(this.iterate!==null){n.kinds[i]=a&~(M|Y),this.cycleFound.push(t,e,s);return}f=0}this._store(t,n,i,e,s,f,r,p)}_cycleHit(t,e,s){const n=this.evalStack;for(let i=this.evalTop-3;i>=0;i-=3)if(n[i]===t&&n[i+1]===e&&n[i+2]===s){i<this.cycleLow&&(this.cycleLow=i);return}}_growEvalStack(){const t=new Int32Array(this.evalStack.length*2);t.set(this.evalStack),this.evalStack=t}_store(t,e,s,n,i,a,r,o){let l=a;l instanceof _t&&(l=k.CALC),l instanceof A?r.t==="array"&&!r.cm&&r.ref?l=this._fillFixedArray(t,n,i,r,l):o||l.rows===1&&l.cols===1?(l=l.data[0],t.spills&&this._releaseSpill(t,n,i)):l=this._spill(t,n,i,r,l):t.spills&&!(r.t==="array"&&!r.cm)&&this._releaseSpill(t,n,i),r.t==="array"&&!r.cm&&r.ref&&!(a instanceof A)&&(l=this._fillFixedArray(t,n,i,r,A.filled(1,1,l)));let c=T,u=0;typeof l=="number"?Number.isFinite(l)?(c|=F,u=l):(c|=E,u=k.NUM.index):typeof l=="string"?(c|=B,u=this.book.strings.add(l)):typeof l=="boolean"?(c|=H,u=l?1:0):l instanceof C?(c|=E,u=l.index):l instanceof O?(c|=E,u=k.VALUE.index):c|=F,l instanceof O?ft(t,n*w+i,at(this.book,l.index)):t.valueMeta!==null&&((e.kinds[s]&ot)!==c||e.values[s]!==u)&&!/IMAGE\(/i.test(r.f??r.sf??"")&&Et(t,n*w+i),e.kinds[s]=c,e.values[s]=u,n>t.maxRow&&(t.maxRow=n),i>t.maxCol&&(t.maxCol=i)}_writeArrayCell(t,e,s,n){let i=K,a=0;typeof n=="number"?(i|=Number.isFinite(n)?F:E,a=Number.isFinite(n)?n:k.NUM.index):typeof n=="string"?(i|=B,a=this.book.strings.add(n)):typeof n=="boolean"?(i|=H,a=n?1:0):n instanceof C?(i|=E,a=n.index):n==null?i|=F:(i|=E,a=k.VALUE.index,n instanceof O&&ft(t,e*w+s,at(this.book,n.index))),t.setCell(e,s,i,a)}_fillFixedArray(t,e,s,n,i){const a=G(n.ref);if(a===null)return i.data[0];t.spills||(t.spills=new Map),t.spills.set(e*w+s,{...a,blocked:!1,fixed:!0});const r=(l,c)=>{const u=i.rows===1?0:l,d=i.cols===1?0:c;return u>=i.rows||d>=i.cols?k.NA:i.get(u,d)};for(let l=a.r0;l<=a.r1;l++)for(let c=a.c0;c<=a.c1;c++)l===e&&c===s||this._writeArrayCell(t,l,c,r(l-a.r0,c-a.c0));this._dirtyFrom(t,a.c0,a.c1,a.r0,a.r1);const o=r(e-a.r0,s-a.c0);return o===null?0:o}_spill(t,e,s,n,i){const a=e*w+s;t.spills||(t.spills=new Map);const r=t.spills.get(a),o=e+i.rows-1,l=s+i.cols-1,c={r0:e,c0:s,r1:o,c1:l,blocked:!1,fixed:!1},u=(f,h)=>r&&f>=r.r0&&f<=r.r1&&h>=r.c0&&h<=r.c1;let d=o>=Z||l>=w;if(!d)t:for(let f=e;f<=o;f++)for(let h=s;h<=l;h++){if(f===e&&h===s)continue;const y=t.kind(f,h);if((y&ot)!==0&&!(y&K&&u(f,h))){d=!0;break t}if(t.merges.length>0&&t.mergeAt(f,h)){d=!0;break t}}if(d)return r&&!r.blocked&&this._clearSpill(t,r,e,s),c.blocked=!0,c.r1=Math.min(o,Z-1),c.c1=Math.min(l,w-1),t.spills.set(a,c),k.SPILL;if(r&&!r.blocked){for(let f=r.r0;f<=r.r1;f++)for(let h=r.c0;h<=r.c1;h++)f>=c.r0&&f<=c.r1&&h>=c.c0&&h<=c.c1||(t.kind(f,h)&K)!==0&&t.setCell(f,h,J,0);this._dirtyFrom(t,r.c0,r.c1,r.r0,r.r1)}t.spills.set(a,c);for(let f=e;f<=o;f++)for(let h=s;h<=l;h++)f===e&&h===s||this._writeArrayCell(t,f,h,i.get(f-e,h-s));n.t="array",n.ref=`${ut(s)}${e+1}:${ut(l)}${o+1}`,n.cm=1,delete n.raw,this._dirtyFrom(t,s,l,e,o);const p=i.data[0];return p===null?0:p}_clearSpill(t,e,s,n){for(let i=e.r0;i<=e.r1;i++)for(let a=e.c0;a<=e.c1;a++)i===s&&a===n||(t.kind(i,a)&K)!==0&&t.setCell(i,a,J,0);this._dirtyFrom(t,e.c0,e.c1,e.r0,e.r1)}_releaseSpill(t,e,s){const n=e*w+s,i=t.spills.get(n);if(i===void 0)return;i.blocked||this._clearSpill(t,i,e,s),t.spills.delete(n);const a=t.formula(e,s);a&&a.typed&&(delete a.ref,delete a.cm,delete a.t)}spillMaster(t,e,s){if(!t.spills)return null;for(const[n,i]of t.spills)if(!i.blocked&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1){const a=Math.floor(n/w);return{row:a,col:n-a*w,rect:i}}return null}spillRange(t){const{sheet:e,r0:s,c0:n}=t;this.read(e,s,n);const i=e.spills?.get(s*w+n);return!i||i.blocked?k.REF:new q(e,i.r0,i.c0,i.r1,i.c1)}read(t,e,s){const n=t.columns[s];if(n===void 0)return null;const i=n[e>>>L];if(i===void 0)return null;const a=e&x;let r=i.kinds[a];if(r===0)return null;switch(r&M&&(this._calc(t,e,s),r=i.kinds[a]),r&V){case F:return i.values[a];case B:return this.book.strings.get(i.values[a]);case H:return i.values[a]!==0;case E:{if(t.valueMeta!==null&&this.book.richValues){const o=bt(this.book,t,e,s);if(o!==null)return o}return Q[i.values[a]]??k.NA}default:return null}}refToValue(t){return t instanceof $?t.refs.length===1?this.refToValue(t.refs[0]):k.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.toArr(t)}refAuto(t,e,s){return t instanceof $?t.refs.length===1?this.refAuto(t.refs[0],e,s):k.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.legacy?this.intersect(t,e,s):this.toArr(t)}intersect(t,e,s){return t.cols===1&&e>=t.r0&&e<=t.r1?this.read(t.sheet,e,t.c0):t.rows===1&&s>=t.c0&&s<=t.c1?this.read(t.sheet,t.r0,s):k.VALUE}trim(t){const{sheet:e}=t;return{r0:t.r0,c0:t.c0,r1:Math.min(t.r1,Math.max(e.maxRow,t.r0)),c1:Math.min(t.c1,Math.max(e.maxCol,t.c0))}}toArr(t){if(t instanceof A)return t;if(t instanceof q){const{r0:e,c0:s,r1:n,c1:i}=this.trim(t),a=n-e+1,r=i-s+1,o=new Array(a*r);let l=0;for(let c=e;c<=n;c++)for(let u=s;u<=i;u++)o[l++]=this.read(t.sheet,c,u);return new A(a,r,o)}if(t instanceof $){if(t.refs.length===1)return this.toArr(t.refs[0]);throw k.VALUE}if(t instanceof C)throw t;return new A(1,1,[t])}scalar(t,e,s){return t instanceof q?t.isCell?this.read(t.sheet,t.r0,t.c0):this.intersect(t,e,s):t instanceof $?k.VALUE:t instanceof A?t.data[0]:t}scan(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);for(let a=t.c0;a<=n;a++){const r=s.columns[a];if(r!==void 0)for(let o=t.r0>>>L;o<=i>>>L;o++){const l=r[o];if(l===void 0)continue;const c=o<<L,u=Math.max(t.r0-c,0),d=Math.min(i-c,x),{kinds:p}=l;for(let f=u;f<=d;f++){if(p[f]===0)continue;const h=this.read(s,c+f,a);h!==null&&e(h,c+f,a)}}}}sumRange(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);let{sum:a,count:r}=e;for(let o=t.c0;o<=n;o++){const l=s.columns[o];if(l!==void 0)for(let c=t.r0>>>L;c<=i>>>L;c++){const u=l[c];if(u===void 0)continue;const d=c<<L,p=Math.max(t.r0-d,0),f=Math.min(i-d,x),{kinds:h,values:y}=u;for(let _=p;_<=f;_++){let g=h[_];if(g===0)continue;g&M&&(this._calc(s,d+_,o),g=h[_]);const b=g&V;if(b===F)a+=y[_],r++;else if(b===E)throw Q[y[_]]}}}e.sum=a,e.count=r}unary(t,e){if(t instanceof C)return t;if(t instanceof A)return new A(t.rows,t.cols,t.data.map(s=>{try{return s instanceof C?s:e(S(s))}catch(n){return n}}));try{return e(S(t))}catch(s){if(s instanceof C)return s;throw s}}slowBin(t,e,s){if(e instanceof A||s instanceof A)return this._binArr(t,e,s);if(e instanceof C)return e;if(s instanceof C)return s;try{return dt(t,e,s)}catch(n){if(n instanceof C)return n;throw n}}_binArr(t,e,s){const n=e instanceof A?e:null,i=s instanceof A?s:null,a=Math.max(n?n.rows:1,i?i.rows:1),r=Math.max(n?n.cols:1,i?i.cols:1),o=new Array(a*r);for(let l=0;l<a;l++)for(let c=0;c<r;c++){const u=nt(e,l,c),d=nt(s,l,c);let p;if(u instanceof C)p=u;else if(d instanceof C)p=d;else try{p=dt(t,u,d)}catch(f){p=f}o[l*r+c]=p}return new A(a,r,o)}liftArr(t,e,s){let n=1,i=1;for(const l of e)l instanceof A&&(l.rows>n&&(n=l.rows),l.cols>i&&(i=l.cols));const a=new Array(n*i),r=e.length,o=new Array(r);for(let l=0;l<n;l++)for(let c=0;c<i;c++){let u=null;for(let d=0;d<r;d++){const p=nt(e[d],l,c);o[d]=p,!s&&u===null&&p instanceof C&&(u=p)}if(u!==null){a[l*i+c]=u;continue}try{a[l*i+c]=t(...o)}catch(d){if(d instanceof C)a[l*i+c]=d;else throw d}}return new A(n,i,a)}invoke(t,e,s,n,i){if(t.native)return this._invokeNative(t.native,e,s,n,i);const a=t.frame?t.frame.slice():[],{params:r}=t;if(e.length>r.length)return k.VALUE;for(let l=0;l<r.length;l++)a[r[l]]=l<e.length?e[l]:void 0;const o=this.frame;if(this.frame=a,this.depth++,this.depth>=ht*4)return this.depth--,this.frame=o,k.NUM;try{return t.body(s,n,i)}finally{this.depth--,this.frame=o}}_invokeNative(t,e,s,n,i){if(e.length<t.min||e.length>t.max)return k.VALUE;try{if(t.kind==="raw")return t.fn(this,e,s,n,i);if(t.kind==="lazy")return t.fn(this,e.map(r=>()=>r),s,n,i);const a=e.map(r=>r instanceof q||r instanceof $?this.refToValue(r):r);if(a.some(r=>r instanceof A))return this.liftArr(t.fn,a,t.errors);if(!t.errors){for(const r of a)if(r instanceof C)return r}return t.fn(...a)}catch(a){if(a instanceof C)return a;throw a}}_nameEntry(t,e){const s=t.toUpperCase();if(e){const i=this.book.sheets.indexOf(e),a=this.names.get(`${i}!${s}`);if(a)return{key:`${i}!${s}`,entry:a}}const n=this.names.get(s);return n?{key:s,entry:n}:null}_nameTemplate(t,e){const s=this._nameEntry(t,e);if(s===null)return null;let n=this.nameTemplates.get(s.key);if(n===void 0){try{n=this._compile(new z(X(s.entry.text),0,0).parse())}catch(i){if(!(i instanceof P))throw i;n=this._broken()}this.nameTemplates.set(s.key,n)}return n}nameMayYieldRange(t){const e=t.toUpperCase(),s=[];for(const[n,i]of this.names)(n===e||n.endsWith(`!${e}`))&&s.push(i);if(s.length===0||(this._rangeNames??=new Set,this._rangeNames.has(e)))return!0;this._rangeNames.add(e);try{return s.some(n=>{try{return new lt(this).mayYieldRange(new z(X(n.text),0,0).parse())}catch(i){if(i instanceof P)return!1;throw i}})}finally{this._rangeNames.delete(e)}}evalName(t,e,s,n){const i=this._nameTemplate(t,n);return i===null?k.NAME:i.fn(e,s,n)}tableSpecToA1(t,e,s){const n=mt(t,e,s);if(!(n instanceof q))return null;const i=/#This Row|^\[\s*@/i.test(e),a=(r,o)=>i?`${ct(o)}${r+1}`:`$${ct(o)}$${r+1}`;return n.isCell?a(n.r0,n.c0):`${a(n.r0,n.c0)}:${a(n.r1,n.c1)}`}resolveTable(t,e,s,n,i){let a=t?this.tables.get(t.toUpperCase()):null;if(!t){for(const r of this.tables.values())if(r.sheet===i&&s>=r.r0&&s<=r.r1&&n>=r.c0&&n<=r.c1){a=r;break}}return a?mt(a,e,s):k.REF}exactIndex(t){const e=`${t.sheet.uid}:${t.r0}:${t.c0}:${t.r1}:${t.c1}`;let s=this.lookupCache.get(e);if(s!==void 0)return s;s=new Map;const n=t.cols===1;return this.scan(t,(i,a,r)=>{const o=typeof i=="string"?`s${i.toLowerCase()}`:typeof i=="number"?i:`b${i}`;if(i instanceof C)return;const l=n?a-t.r0:r-t.c0,c=s.get(o);(c===void 0||l<c)&&s.set(o,l)}),this.lookupCache.set(e,s),s}visibilityChanged(){this._wakeReaders(this.visibilityReaders)}presentationChanged(){this._wakeLater(2)}axisHiddenChanged(){this._wakeLater(1)}axisResized(){this._wakeLater(2)}_wakeLater(t){(t===1?this.visibilityReaders:this.styleReaders).size!==0&&(this.wakePending===0&&queueMicrotask(()=>{const e=this.wakePending;this.wakePending=0,e&1&&this._wakeReaders(this.visibilityReaders),e&2&&this._wakeReaders(this.styleReaders)}),this.wakePending|=t)}_wakeReaders(t){if(!this.graphReady||t.size===0)return;let e=!1;for(const[s,n]of t)n.forEach((i,a)=>{const r=s._chunk(i,a);if(r===void 0)return;const o=i&x,l=r.kinds[o];if(!(l&T)){n.delete(i,a);return}l&M||(r.kinds[o]=l|M,this._enqueue(s,i,a)),this._dirtyFrom(s,a,a,i,i),e=!0});e&&this._schedule()}isSubtotalCell(t,e,s){if((t.kind(e,s)&T)===0)return!1;const n=t.formula(e,s),i=n?.f??n?.sf??"";return/^\s*(_xlfn\.)?(SUBTOTAL|AGGREGATE)\(/i.test(i)}displayFormula(t){return At(t)}}function ut(m){let t="";for(let e=m+1;e>0;e=Math.floor((e-1)/26))t=String.fromCharCode(65+(e-1)%26)+t;return t}function nt(m,t,e){if(!(m instanceof A))return m;const s=m.rows===1?0:t,n=m.cols===1?0:e;return s>=m.rows||n>=m.cols?k.NA:m.data[s*m.cols+n]}function dt(m,t,e){if((t instanceof O||e instanceof O)&&m!=="="&&m!=="<>"&&m!=="&")throw k.VALUE;switch(m){case"+":{const s=S(t),n=s+S(e);return Math.abs(n)<Math.abs(s)*pt?0:j(n)}case"-":{const s=S(t),n=s-S(e);return Math.abs(n)<Math.abs(s)*pt?0:j(n)}case"*":return j(S(t)*S(e));case"/":{const s=S(t),n=S(e);if(n===0)throw k.DIV0;return j(s/n)}case"^":{const s=S(t),n=S(e);if(s===0&&n===0)throw k.NUM;if(s===0&&n<0)throw k.DIV0;const i=gt(s,n);if(Number.isNaN(i))throw k.NUM;return j(i)}case"&":return rt(t)+rt(e);case"=":return v(t,e)===0;case"<>":return v(t,e)!==0;case"<":return v(t,e)<0;case">":return v(t,e)>0;case"<=":return v(t,e)<=0;case">=":return v(t,e)>=0;default:throw k.VALUE}}const pt=5e-16;function j(m){if(!Number.isFinite(m))throw k.NUM;return m}function mt(m,t,e){const s=kt(m,t,e);return s==="REF"?k.REF:s==="VALUE"?k.VALUE:new q(m.sheet,s.top,s.left,s.bottom,s.right)}const Ut={COUNTIF:[0],COUNTIFS:["even",0],SUMIF:[0,2],SUMIFS:[0,"odd",1],AVERAGEIF:[0,2],AVERAGEIFS:[0,"odd",1],MAXIFS:[0,"odd",1],MINIFS:[0,"odd",1],COUNTBLANK:[0],RANK:[1],"RANK.EQ":[1],"RANK.AVG":[1]};function It(m,t){for(let e=0;e<m.length;e++){const s=m[e];if(s===t||s==="odd"&&t>=m[e+1]&&t%2===1||s==="even"&&t>=m[e+1]&&t%2===0)return!0}return!1}function it(m){if(!(m===null||typeof m!="object")){if(Array.isArray(m)){for(const t of m)it(t);return}if(m.t==="call"){const t=Ut[String(m.name).replace(/^_xl(?:fn|ws)\./i,"").toUpperCase()];t&&m.args.forEach((e,s)=>{if(e?.t==="array"&&It(t,s))throw new Error(`${m.name} takes a range here`)})}for(const t of Object.values(m))t!==null&&typeof t=="object"&&it(t)}}export{Kt as Engine};
@@ -1 +1 @@
1
- import{setCategory as j}from"./registry.js";import{lift as _,raw as m}from"./registry.js";import{Arr as p,bool as D,compareValues as G,ERR as A,num as E,OMITTED as L,Ref as N,str as K,XlError as M}from"../values.js";j("Lookup & Reference");const C=r=>r===void 0||r===L,R=(r,t,s,c,o)=>C(t)?o:Math.trunc(E(r.scalar(t,s,c))),U=(r,t,s,c,o)=>C(t)?o:D(r.scalar(t,s,c)),O=r=>Array.from({length:r.rows},(t,s)=>r.data.slice(s*r.cols,(s+1)*r.cols)),T=r=>Array.from({length:r.cols},(t,s)=>Array.from({length:r.rows},(c,o)=>r.get(o,s))),g=r=>r===null?0:r;_("SEQUENCE",1,4,(r,t,s,c)=>{const o=Math.trunc(E(r)),n=t===void 0||t===L?1:Math.trunc(E(t)),e=s===void 0||s===L?1:E(s),a=c===void 0||c===L?1:E(c);if(o<0||n<0)throw A.VALUE;if(o===0||n===0)throw A.CALC;if(o*n>1<<27)throw A.NUM;const f=new Array(o*n);for(let l=0;l<f.length;l++)f[l]=e+l*a;return new p(o,n,f)}),_("RANDARRAY",0,5,(r,t,s,c,o)=>{const n=r===void 0||r===L?1:Math.trunc(E(r)),e=t===void 0||t===L?1:Math.trunc(E(t)),a=s===void 0||s===L?0:E(s),f=c===void 0||c===L?1:E(c),l=o===void 0||o===L?!1:D(o);if(n<0||e<0||a>f)throw A.VALUE;if(n===0||e===0)throw A.CALC;const i=new Array(n*e);for(let d=0;d<i.length;d++)i[d]=l?Math.ceil(a)+Math.floor(Math.random()*(Math.floor(f)-Math.ceil(a)+1)):a+Math.random()*(f-a);return new p(n,e,i)},{volatile:!0}),m("FILTER",2,3,(r,t,s,c)=>{const o=r.toArr(t[0]),n=r.toArr(t[1]),e=n.cols===1&&n.rows===o.rows,a=n.rows===1&&n.cols===o.cols;if(!e&&!a)throw A.VALUE;const f=n.data.map(u=>{if(u instanceof M)throw u;return u===null?!1:D(u)}),l=e?O(o).filter((u,h)=>f[h]):null,i=e?null:T(o).filter((u,h)=>f[h]);if(e?l.length===0:i.length===0){if(C(t[2]))throw A.CALC;return t[2]}if(e)return p.of2D(l.map(u=>u.map(g)));const w=new p(o.rows,i.length,new Array(o.rows*i.length));return i.forEach((u,h)=>u.forEach((y,V)=>{w.data[V*i.length+h]=g(y)})),w},{array:!0,passes:r=>r===2});function b(r,t){const s=r[0].length,c=Array.from({length:s},(o,n)=>n);return c.sort((o,n)=>{for(let e=0;e<r.length;e++){const a=r[e][o],f=r[e][n],l=a==null||a instanceof M,i=f==null||f instanceof M;if(l||i){if(l&&i)continue;return l?1:-1}const d=G(a,f);if(d!==0)return d*t[e]}return o-n}),c}m("SORT",1,4,(r,t,s,c)=>{const o=r.toArr(t[0]),n=U(r,t[3],s,c,!1),e=C(t[1])?[1]:r.toArr(t[1]).data.map(h=>Math.trunc(E(h))),a=C(t[2])?e.map(()=>1):r.toArr(t[2]).data.map(h=>E(h));if(a.length!==e.length||a.some(h=>h!==1&&h!==-1))throw A.VALUE;const f=n?T(o):O(o),l=n?o.rows:o.cols;for(const h of e)if(h<1||h>l)throw A.VALUE;const i=e.map(h=>f.map(y=>y[h-1])),w=b(i,a).map(h=>f[h]);if(!n)return p.of2D(w.map(h=>h.map(g)));const u=new Array(o.rows*o.cols);return w.forEach((h,y)=>h.forEach((V,W)=>{u[W*o.cols+y]=g(V)})),new p(o.rows,o.cols,u)},{array:!0}),m("SORTBY",2,255,(r,t,s,c)=>{const o=r.toArr(t[0]),n=[],e=[];let a=1;for(;a<t.length;){const w=r.toArr(t[a]),u=a+1<t.length&&!C(t[a+1])&&!(t[a+1]instanceof N)&&!(t[a+1]instanceof p);let h=1,y=1;if(u?(h=E(r.scalar(t[a+1],s,c)),y=2):a+1<t.length&&C(t[a+1])&&(y=2),h!==1&&h!==-1)throw A.VALUE;n.push(w),e.push(h),a+=y}const f=n[0].cols===1;for(const w of n)if(f?w.rows!==o.rows||w.cols!==1:w.cols!==o.cols||w.rows!==1)throw A.VALUE;const l=b(n.map(w=>w.data),e);if(f){const w=O(o);return p.of2D(l.map(u=>w[u].map(g)))}const i=T(o),d=new Array(o.rows*o.cols);return l.forEach((w,u)=>i[w].forEach((h,y)=>{d[y*o.cols+u]=g(h)})),new p(o.rows,o.cols,d)},{array:!0});const k=r=>r.map(t=>t===null?"\0":typeof t=="string"?`s${t.toLowerCase()}`:t instanceof M?`e${t.code}`:`${typeof t}${t}`).join("");m("UNIQUE",1,3,(r,t,s,c)=>{const o=r.toArr(t[0]),n=U(r,t[1],s,c,!1),e=U(r,t[2],s,c,!1),a=n?T(o):O(o),f=new Map;for(const w of a){const u=k(w);f.set(u,(f.get(u)??0)+1)}const l=new Set,i=[];for(const w of a){const u=k(w);(e?f.get(u)!==1:l.has(u))||(l.add(u),i.push(w))}if(i.length===0)throw A.CALC;if(!n)return p.of2D(i.map(w=>w.map(g)));const d=new Array(o.rows*i.length);return i.forEach((w,u)=>w.forEach((h,y)=>{d[y*i.length+u]=g(h)})),new p(o.rows,i.length,d)},{array:!0}),m("TRANSPOSE",1,1,(r,t)=>{const s=r.toArr(t[0]),c=new Array(s.data.length);for(let o=0;o<s.rows;o++)for(let n=0;n<s.cols;n++)c[n*s.rows+o]=g(s.get(o,n));return new p(s.cols,s.rows,c)},{array:!0});function I(r,t,s){const c=t.map(f=>r.toArr(f));if(!s){const f=Math.max(...c.map(i=>i.cols)),l=[];for(const i of c)for(let d=0;d<i.rows;d++){const w=new Array(f);for(let u=0;u<f;u++)w[u]=u<i.cols?g(i.get(d,u)):A.NA;l.push(w)}return p.of2D(l)}const o=Math.max(...c.map(f=>f.rows)),n=c.reduce((f,l)=>f+l.cols,0),e=new Array(o*n);let a=0;for(const f of c){for(let l=0;l<f.cols;l++)for(let i=0;i<o;i++)e[i*n+a+l]=i<f.rows?g(f.get(i,l)):A.NA;a+=f.cols}return new p(o,n,e)}m("VSTACK",1,254,(r,t)=>I(r,t,!1),{array:!0}),m("HSTACK",1,254,(r,t)=>I(r,t,!0),{array:!0});function H(r,t,s,c,o){const n=s-t+1,e=o-c+1,a=new Array(n*e);for(let f=0;f<n;f++)for(let l=0;l<e;l++)a[f*e+l]=g(r.get(t+f,c+l));return new p(n,e,a)}m("TAKE",2,3,(r,t,s,c)=>{const o=r.toArr(t[0]),n=R(r,t[1],s,c,o.rows),e=R(r,t[2],s,c,o.cols);if(n===0||e===0)throw A.CALC;const a=Math.min(Math.abs(n),o.rows),f=Math.min(Math.abs(e),o.cols),l=n<0?o.rows-a:0,i=e<0?o.cols-f:0;return H(o,l,l+a-1,i,i+f-1)},{array:!0}),m("DROP",2,3,(r,t,s,c)=>{const o=r.toArr(t[0]),n=R(r,t[1],s,c,0),e=R(r,t[2],s,c,0),a=o.rows-Math.abs(n),f=o.cols-Math.abs(e);if(a<=0||f<=0)throw A.CALC;const l=n>0?n:0,i=e>0?e:0;return H(o,l,l+a-1,i,i+f-1)},{array:!0}),m("EXPAND",2,4,(r,t,s,c)=>{const o=r.toArr(t[0]),n=R(r,t[1],s,c,o.rows),e=R(r,t[2],s,c,o.cols),a=C(t[3])?A.NA:r.scalar(t[3],s,c);if(n<o.rows||e<o.cols)throw A.VALUE;const f=new Array(n*e);for(let l=0;l<n;l++)for(let i=0;i<e;i++)f[l*e+i]=l<o.rows&&i<o.cols?g(o.get(l,i)):a;return new p(n,e,f)},{array:!0});function F(r,t,s,c,o){const n=r.toArr(t[0]),e=o?O(n):T(n),a=[];for(let l=1;l<t.length;l++)for(const i of r.toArr(t[l]).data){const d=Math.trunc(E(i)),w=d<0?e.length+d:d-1;if(d===0||w<0||w>=e.length)throw A.VALUE;a.push(e[w])}if(o)return p.of2D(a.map(l=>l.map(g)));const f=new Array(n.rows*a.length);return a.forEach((l,i)=>l.forEach((d,w)=>{f[w*a.length+i]=g(d)})),new p(n.rows,a.length,f)}m("CHOOSEROWS",2,255,(r,t,s,c)=>F(r,t,s,c,!0),{array:!0}),m("CHOOSECOLS",2,255,(r,t,s,c)=>F(r,t,s,c,!1),{array:!0});function P(r,t,s,c){const o=r.toArr(t[0]),n=R(r,t[1],s,c,0),e=U(r,t[2],s,c,!1);if(n<0||n>3)throw A.VALUE;const a=[],f=l=>{(n===1||n===3)&&l===null||(n===2||n===3)&&l instanceof M||a.push(g(l))};if(e)for(let l=0;l<o.cols;l++)for(let i=0;i<o.rows;i++)f(o.get(i,l));else for(const l of o.data)f(l);if(a.length===0)throw A.CALC;return a}m("TOCOL",1,3,(r,t,s,c)=>p.column(P(r,t,s,c)),{array:!0}),m("TOROW",1,3,(r,t,s,c)=>p.row(P(r,t,s,c)),{array:!0});function $(r,t,s,c,o){const n=r.toArr(t[0]);if(n.rows>1&&n.cols>1)throw A.VALUE;const e=R(r,t[1],s,c,0);if(e<1)throw A.NUM;const a=C(t[2])?A.NA:r.scalar(t[2],s,c),f=n.data.length,l=Math.ceil(f/e),i=o?l:e,d=o?e:l,w=new Array(i*d);for(let u=0;u<i*d;u++){const h=u<f?g(n.data[u]):a;o?w[u]=h:w[u%e*d+Math.floor(u/e)]=h}return new p(i,d,w)}m("WRAPROWS",2,3,(r,t,s,c)=>$(r,t,s,c,!0),{array:!0}),m("WRAPCOLS",2,3,(r,t,s,c)=>$(r,t,s,c,!1),{array:!0});function S(r,t,s,c){if(!(t instanceof N))return t;const o=r.trim(t);let{r0:n,c0:e}=t,a=s&2?o.r1:t.r1,f=c&2?o.c1:t.c1;s&2&&o.r1<n&&(a=n),c&2&&o.c1<e&&(f=e);const l=(u,h)=>t.sheet.kind(u,h)!==0,i=u=>{for(let h=e;h<=f;h++)if(l(u,h))return!0;return!1},d=u=>{for(let h=n;h<=a;h++)if(l(h,u))return!0;return!1};if(s&1)for(;n<a&&!i(n);)n++;if(s&2)for(;a>n&&!i(a);)a--;if(c&1)for(;e<f&&!d(e);)e++;if(c&2)for(;f>e&&!d(f);)f--;if(s?n===a&&!i(n):c?e===f&&!d(e):!1)throw A.REF;return new N(t.sheet,n,e,a,f)}m("TRIMRANGE",1,3,(r,t,s,c)=>{const o=n=>{const e=t[n]===void 0||t[n]===null?3:R(r,t[n],s,c,3);if(e<0||e>3)throw A.VALUE;return e};return S(r,t[0],o(1),o(2))},{refResult:!0}),m("_TRO_ALL",1,1,(r,t)=>S(r,t[0],3,3),{refResult:!0,category:"Hidden"}),m("_TRO_LEADING",1,1,(r,t)=>S(r,t[0],1,1),{refResult:!0,category:"Hidden"}),m("_TRO_TRAILING",1,1,(r,t)=>S(r,t[0],2,2),{refResult:!0,category:"Hidden"}),m("ARRAYTOTEXT",1,2,(r,t,s,c)=>{const o=r.toArr(t[0]),n=R(r,t[1],s,c,0)===1,e=a=>a===null?"":typeof a=="string"?n?`"${a.replace(/"/g,'""')}"`:a:a instanceof M?a.code:K(a);return n?`{${O(o).map(a=>a.map(e).join(",")).join(";")}}`:o.data.map(e).join(", ")},{array:!0}),m("PERCENTOF",2,2,(r,t)=>{const s=o=>{let n=0;for(const e of r.toArr(o).data){if(e instanceof M)throw e;typeof e=="number"&&(n+=e)}return n},c=s(t[1]);if(c===0)throw A.DIV0;return s(t[0])/c},{array:!0}),m("SINGLE",1,1,(r,t,s,c)=>{const o=r.scalar(t[0],s,c);return o===null?0:o}),m("ANCHORARRAY",1,1,(r,t)=>t[0]instanceof N?r.spillRange(t[0]):A.REF,{refResult:!0});
1
+ import{setCategory as X}from"./registry.js";import{lift as D,raw as m}from"./registry.js";import{Arr as p,bool as _,compareValues as j,ERR as d,num as y,OMITTED as R,Ref as T,str as G,XlError as M}from"../values.js";import{MAX_COLS as K,MAX_ROWS as Y}from"../../core/address.js";X("Lookup & Reference");const C=o=>o===void 0||o===R,L=(o,t,s,c,r)=>C(t)?r:Math.trunc(y(o.scalar(t,s,c))),S=(o,t,s,c,r)=>C(t)?r:_(o.scalar(t,s,c)),O=o=>Array.from({length:o.rows},(t,s)=>o.data.slice(s*o.cols,(s+1)*o.cols)),N=o=>Array.from({length:o.cols},(t,s)=>Array.from({length:o.rows},(c,r)=>o.get(r,s))),g=o=>o===null?0:o;D("SEQUENCE",1,4,(o,t,s,c)=>{const r=Math.trunc(y(o)),n=t===void 0||t===R?1:Math.trunc(y(t)),e=s===void 0||s===R?1:y(s),a=c===void 0||c===R?1:y(c);if(r<0||n<0)throw d.VALUE;if(r===0||n===0)throw d.CALC;if(r*n>1<<27)throw d.NUM;const f=new Array(r*n);for(let l=0;l<f.length;l++)f[l]=e+l*a;return new p(r,n,f)}),D("RANDARRAY",0,5,(o,t,s,c,r)=>{const n=o===void 0||o===R?1:Math.trunc(y(o)),e=t===void 0||t===R?1:Math.trunc(y(t)),a=s===void 0||s===R?0:y(s),f=c===void 0||c===R?1:y(c),l=r===void 0||r===R?!1:_(r);if(n<0||e<0||a>f)throw d.VALUE;if(n===0||e===0)throw d.CALC;const i=new Array(n*e);for(let A=0;A<i.length;A++)i[A]=l?Math.ceil(a)+Math.floor(Math.random()*(Math.floor(f)-Math.ceil(a)+1)):a+Math.random()*(f-a);return new p(n,e,i)},{volatile:!0}),m("FILTER",2,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=o.toArr(t[1]),e=n.cols===1&&n.rows===r.rows,a=n.rows===1&&n.cols===r.cols;if(!e&&!a)throw d.VALUE;const f=n.data.map(u=>{if(u instanceof M)throw u;return u===null?!1:_(u)}),l=e?O(r).filter((u,h)=>f[h]):null,i=e?null:N(r).filter((u,h)=>f[h]);if(e?l.length===0:i.length===0){if(C(t[2]))throw d.CALC;return t[2]}if(e)return p.of2D(l.map(u=>u.map(g)));const w=new p(r.rows,i.length,new Array(r.rows*i.length));return i.forEach((u,h)=>u.forEach((E,V)=>{w.data[V*i.length+h]=g(E)})),w},{array:!0,passes:o=>o===2});function b(o,t){const s=o[0].length,c=Array.from({length:s},(r,n)=>n);return c.sort((r,n)=>{for(let e=0;e<o.length;e++){const a=o[e][r],f=o[e][n],l=a==null||a instanceof M,i=f==null||f instanceof M;if(l||i){if(l&&i)continue;return l?1:-1}const A=j(a,f);if(A!==0)return A*t[e]}return r-n}),c}m("SORT",1,4,(o,t,s,c)=>{const r=o.toArr(t[0]),n=S(o,t[3],s,c,!1),e=C(t[1])?[1]:o.toArr(t[1]).data.map(h=>Math.trunc(y(h))),a=C(t[2])?e.map(()=>1):o.toArr(t[2]).data.map(h=>y(h));if(a.length!==e.length||a.some(h=>h!==1&&h!==-1))throw d.VALUE;const f=n?N(r):O(r),l=n?r.rows:r.cols;for(const h of e)if(h<1||h>l)throw d.VALUE;const i=e.map(h=>f.map(E=>E[h-1])),w=b(i,a).map(h=>f[h]);if(!n)return p.of2D(w.map(h=>h.map(g)));const u=new Array(r.rows*r.cols);return w.forEach((h,E)=>h.forEach((V,$)=>{u[$*r.cols+E]=g(V)})),new p(r.rows,r.cols,u)},{array:!0}),m("SORTBY",2,255,(o,t,s,c)=>{const r=o.toArr(t[0]),n=[],e=[];let a=1;for(;a<t.length;){const w=o.toArr(t[a]),u=a+1<t.length&&!C(t[a+1])&&!(t[a+1]instanceof T)&&!(t[a+1]instanceof p);let h=1,E=1;if(u?(h=y(o.scalar(t[a+1],s,c)),E=2):a+1<t.length&&C(t[a+1])&&(E=2),h!==1&&h!==-1)throw d.VALUE;n.push(w),e.push(h),a+=E}const f=n[0].cols===1;for(const w of n)if(f?w.rows!==r.rows||w.cols!==1:w.cols!==r.cols||w.rows!==1)throw d.VALUE;const l=b(n.map(w=>w.data),e);if(f){const w=O(r);return p.of2D(l.map(u=>w[u].map(g)))}const i=N(r),A=new Array(r.rows*r.cols);return l.forEach((w,u)=>i[w].forEach((h,E)=>{A[E*r.cols+u]=g(h)})),new p(r.rows,r.cols,A)},{array:!0});const k=o=>o.map(t=>t===null?"\0":typeof t=="string"?`s${t.toLowerCase()}`:t instanceof M?`e${t.code}`:`${typeof t}${t}`).join("");m("UNIQUE",1,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=S(o,t[1],s,c,!1),e=S(o,t[2],s,c,!1),a=n?N(r):O(r),f=new Map;for(const w of a){const u=k(w);f.set(u,(f.get(u)??0)+1)}const l=new Set,i=[];for(const w of a){const u=k(w);(e?f.get(u)!==1:l.has(u))||(l.add(u),i.push(w))}if(i.length===0)throw d.CALC;if(!n)return p.of2D(i.map(w=>w.map(g)));const A=new Array(r.rows*i.length);return i.forEach((w,u)=>w.forEach((h,E)=>{A[E*i.length+u]=g(h)})),new p(r.rows,i.length,A)},{array:!0}),m("TRANSPOSE",1,1,(o,t)=>{const s=o.toArr(t[0]),c=new Array(s.data.length);for(let r=0;r<s.rows;r++)for(let n=0;n<s.cols;n++)c[n*s.rows+r]=g(s.get(r,n));return new p(s.cols,s.rows,c)},{array:!0});function I(o,t,s){const c=t.map(f=>o.toArr(f));if(!s){const f=Math.max(...c.map(i=>i.cols)),l=[];for(const i of c)for(let A=0;A<i.rows;A++){const w=new Array(f);for(let u=0;u<f;u++)w[u]=u<i.cols?g(i.get(A,u)):d.NA;l.push(w)}return p.of2D(l)}const r=Math.max(...c.map(f=>f.rows)),n=c.reduce((f,l)=>f+l.cols,0),e=new Array(r*n);let a=0;for(const f of c){for(let l=0;l<f.cols;l++)for(let i=0;i<r;i++)e[i*n+a+l]=i<f.rows?g(f.get(i,l)):d.NA;a+=f.cols}return new p(r,n,e)}m("VSTACK",1,254,(o,t)=>I(o,t,!1),{array:!0}),m("HSTACK",1,254,(o,t)=>I(o,t,!0),{array:!0});function H(o,t,s,c,r){const n=s-t+1,e=r-c+1,a=new Array(n*e);for(let f=0;f<n;f++)for(let l=0;l<e;l++)a[f*e+l]=g(o.get(t+f,c+l));return new p(n,e,a)}m("TAKE",2,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,r.rows),e=L(o,t[2],s,c,r.cols);if(n===0||e===0)throw d.CALC;const a=Math.min(Math.abs(n),r.rows),f=Math.min(Math.abs(e),r.cols),l=n<0?r.rows-a:0,i=e<0?r.cols-f:0;return H(r,l,l+a-1,i,i+f-1)},{array:!0}),m("DROP",2,3,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,0),e=L(o,t[2],s,c,0),a=r.rows-Math.abs(n),f=r.cols-Math.abs(e);if(a<=0||f<=0)throw d.CALC;const l=n>0?n:0,i=e>0?e:0;return H(r,l,l+a-1,i,i+f-1)},{array:!0}),m("EXPAND",2,4,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,r.rows),e=L(o,t[2],s,c,r.cols),a=C(t[3])?d.NA:o.scalar(t[3],s,c);if(n<r.rows||e<r.cols||n>Y||e>K)throw d.VALUE;const f=new Array(n*e);for(let l=0;l<n;l++)for(let i=0;i<e;i++)f[l*e+i]=l<r.rows&&i<r.cols?g(r.get(l,i)):a;return new p(n,e,f)},{array:!0});function F(o,t,s,c,r){const n=o.toArr(t[0]),e=r?O(n):N(n),a=[];for(let l=1;l<t.length;l++)for(const i of o.toArr(t[l]).data){const A=Math.trunc(y(i)),w=A<0?e.length+A:A-1;if(A===0||w<0||w>=e.length)throw d.VALUE;a.push(e[w])}if(r)return p.of2D(a.map(l=>l.map(g)));const f=new Array(n.rows*a.length);return a.forEach((l,i)=>l.forEach((A,w)=>{f[w*a.length+i]=g(A)})),new p(n.rows,a.length,f)}m("CHOOSEROWS",2,255,(o,t,s,c)=>F(o,t,s,c,!0),{array:!0}),m("CHOOSECOLS",2,255,(o,t,s,c)=>F(o,t,s,c,!1),{array:!0});function P(o,t,s,c){const r=o.toArr(t[0]),n=L(o,t[1],s,c,0),e=S(o,t[2],s,c,!1);if(n<0||n>3)throw d.VALUE;const a=[],f=l=>{(n===1||n===3)&&l===null||(n===2||n===3)&&l instanceof M||a.push(g(l))};if(e)for(let l=0;l<r.cols;l++)for(let i=0;i<r.rows;i++)f(r.get(i,l));else for(const l of r.data)f(l);if(a.length===0)throw d.CALC;return a}m("TOCOL",1,3,(o,t,s,c)=>p.column(P(o,t,s,c)),{array:!0}),m("TOROW",1,3,(o,t,s,c)=>p.row(P(o,t,s,c)),{array:!0});function W(o,t,s,c,r){const n=o.toArr(t[0]);if(n.rows>1&&n.cols>1)throw d.VALUE;const e=L(o,t[1],s,c,0);if(e<1)throw d.NUM;const a=C(t[2])?d.NA:o.scalar(t[2],s,c),f=n.data.length,l=Math.ceil(f/e),i=r?l:e,A=r?e:l,w=new Array(i*A);for(let u=0;u<i*A;u++){const h=u<f?g(n.data[u]):a;r?w[u]=h:w[u%e*A+Math.floor(u/e)]=h}return new p(i,A,w)}m("WRAPROWS",2,3,(o,t,s,c)=>W(o,t,s,c,!0),{array:!0}),m("WRAPCOLS",2,3,(o,t,s,c)=>W(o,t,s,c,!1),{array:!0});function U(o,t,s,c){if(!(t instanceof T))return t;const r=o.trim(t);let{r0:n,c0:e}=t,a=s&2?r.r1:t.r1,f=c&2?r.c1:t.c1;s&2&&r.r1<n&&(a=n),c&2&&r.c1<e&&(f=e);const l=(u,h)=>t.sheet.kind(u,h)!==0,i=u=>{for(let h=e;h<=f;h++)if(l(u,h))return!0;return!1},A=u=>{for(let h=n;h<=a;h++)if(l(h,u))return!0;return!1};if(s&1)for(;n<a&&!i(n);)n++;if(s&2)for(;a>n&&!i(a);)a--;if(c&1)for(;e<f&&!A(e);)e++;if(c&2)for(;f>e&&!A(f);)f--;if(s?n===a&&!i(n):c?e===f&&!A(e):!1)throw d.REF;return new T(t.sheet,n,e,a,f)}m("TRIMRANGE",1,3,(o,t,s,c)=>{const r=n=>{const e=t[n]===void 0||t[n]===null?3:L(o,t[n],s,c,3);if(e<0||e>3)throw d.VALUE;return e};return U(o,t[0],r(1),r(2))},{refResult:!0}),m("_TRO_ALL",1,1,(o,t)=>U(o,t[0],3,3),{refResult:!0,category:"Hidden"}),m("_TRO_LEADING",1,1,(o,t)=>U(o,t[0],1,1),{refResult:!0,category:"Hidden"}),m("_TRO_TRAILING",1,1,(o,t)=>U(o,t[0],2,2),{refResult:!0,category:"Hidden"}),m("ARRAYTOTEXT",1,2,(o,t,s,c)=>{const r=o.toArr(t[0]),n=L(o,t[1],s,c,0)===1,e=a=>a===null?"":typeof a=="string"?n?`"${a.replace(/"/g,'""')}"`:a:a instanceof M?a.code:G(a);return n?`{${O(r).map(a=>a.map(e).join(",")).join(";")}}`:r.data.map(e).join(", ")},{array:!0}),m("PERCENTOF",2,2,(o,t)=>{const s=r=>{if(!(r instanceof T)&&!(r instanceof p)){if(r instanceof M)throw r;return r===null||r===R?0:y(r)}let n=0;for(const e of o.toArr(r).data){if(e instanceof M)throw e;typeof e=="number"&&(n+=e)}return n},c=s(t[1]);if(c===0)throw d.DIV0;return s(t[0])/c},{array:!0}),m("SINGLE",1,1,(o,t,s,c)=>{const r=o.scalar(t[0],s,c);return r===null?0:r}),m("ANCHORARRAY",1,1,(o,t)=>t[0]instanceof T?o.spillRange(t[0]):d.REF,{refResult:!0});
@@ -1 +1 @@
1
- import{setCategory as g}from"./registry.js";import{alias as k,lift as i,raw as p}from"./registry.js";import{Arr as I,ERR as f,num as u,OMITTED as C,Ref as K,RefList as F,str as H,XlError as O}from"../values.js";import{partsToSerial as M,serialToParts as h}from"../../core/numfmt.js";import{eachValue as j}from"./helpers.js";import{parseInput as $}from"../../core/input.js";import{NUM as z}from"../../core/sheet.js";import{days360 as B,yearFrac as X}from"./financial.js";g("Date & Time");const y=t=>t===void 0||t===C,D=2958465;function c(t){const e=Math.floor(u(t));if(e<0||e>D)throw f.NUM;return e}function Z(t,e,n){let r=t;if(r>=0&&r<1900&&(r+=1900),r<0||r>9999)throw f.NUM;const o=r*12+(e-1),s=Math.floor(o/12),a=o-s*12+1,l=M(s,a,1)+n-1;if(l<0||l>D)throw f.NUM;return l}i("DATE",3,3,(t,e,n)=>Z(Math.trunc(u(t)),Math.trunc(u(e)),Math.trunc(u(n)))),i("YEAR",1,1,t=>h(c(t)).year),i("MONTH",1,1,t=>h(c(t)).month),i("DAY",1,1,t=>h(c(t)).day);const T=t=>{const e=u(t);if(e<0)throw f.NUM;return Math.round((e-Math.floor(e))*86400)};i("HOUR",1,1,t=>Math.floor(T(t)/3600)%24),i("MINUTE",1,1,t=>Math.floor(T(t)%3600/60)),i("SECOND",1,1,t=>T(t)%60),i("TIME",3,3,(t,e,n)=>{const r=Math.trunc(u(t))*3600+Math.trunc(u(e))*60+Math.trunc(u(n));if(r<0)throw f.NUM;return r%86400/86400});const b=()=>{const t=new Date;return{whole:M(t.getFullYear(),t.getMonth()+1,t.getDate()),frac:(t.getHours()*3600+t.getMinutes()*60+t.getSeconds()+t.getMilliseconds()/1e3)/86400}};i("NOW",0,0,()=>{const t=b();return t.whole+t.frac},{volatile:!0}),i("TODAY",0,0,()=>b().whole,{volatile:!0});const R={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12};function L(t){const e=t.trim(),n=$(e);if(n.kind===z&&n.format&&/[ymd]/i.test(n.format))return n.value;let r=/^(\d{1,2})[-\s]([A-Za-z]{3,})[-\s,]*(\d{2,4})?$/.exec(e);if(r){const o=R[r[2].slice(0,3).toLowerCase()];if(o){const s=r[3]?Number(r[3])<100?2e3+Number(r[3]):Number(r[3]):new Date().getFullYear();return M(s,o,Number(r[1]))}}if(r=/^([A-Za-z]{3,})\s+(\d{1,2})(?:,?\s*(\d{2,4}))?$/.exec(e),r){const o=R[r[1].slice(0,3).toLowerCase()];if(o){const s=r[3]?Number(r[3])<100?2e3+Number(r[3]):Number(r[3]):new Date().getFullYear();return M(s,o,Number(r[2]))}}return null}i("DATEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const e=t.split(/\s+\d{1,2}:\d{2}/)[0],n=L(e);if(n===null)throw f.VALUE;return Math.floor(n)}),i("TIMEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const e=/(\d{1,2}):(\d{2})(?::(\d{2}(?:\.\d+)?))?\s*(am|pm|a|p)?/i.exec(t);if(!e)throw f.VALUE;let n=Number(e[1]);if(e[4]){const r=e[4][0].toLowerCase()==="p";if(n>12)throw f.VALUE;n=n%12+(r?12:0)}return(n*3600+Number(e[2])*60+Number(e[3]??0))/86400%1}),i("DAYS",2,2,(t,e)=>c(t)-c(e)),i("DAYS360",2,3,(t,e,n)=>B(c(t),c(e),y(n)?!1:!!u(n)));function q(t,e){const n=h(t),r=n.year*12+(n.month-1)+e,o=Math.floor(r/12),s=r-o*12+1;if(o<1900||o>9999)throw f.NUM;const a=E(o,s);return M(o,s,Math.min(n.day,a))}function E(t,e){return e===2?t===1900||t%4===0&&(t%100!==0||t%400===0)?29:28:[31,28,31,30,31,30,31,31,30,31,30,31][e-1]}i("EDATE",2,2,(t,e)=>q(c(t),Math.trunc(u(e)))),i("EOMONTH",2,2,(t,e)=>{const n=h(c(t)),r=n.year*12+(n.month-1)+Math.trunc(u(e)),o=Math.floor(r/12),s=r-o*12+1;if(o<1900||o>9999)throw f.NUM;return M(o,s,E(o,s))}),i("WEEKDAY",1,2,(t,e)=>{const n=c(t),r=y(e)?1:Math.trunc(u(e)),o=h(n).weekday,s={1:0,2:1,3:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[r];if(s===void 0)throw f.NUM;const a=(o-s+7)%7;return r===3?a:a+1}),i("ISOWEEKNUM",1,1,t=>S(c(t)));function S(t){const e=(h(t).weekday+6)%7,n=t-e+3,r=h(n).year;return Math.floor((n-M(r,1,1))/7)+1}i("WEEKNUM",1,2,(t,e)=>{const n=c(t),r=y(e)?1:Math.trunc(u(e));if(r===21)return S(n);const o={1:0,2:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[r];if(o===void 0)throw f.NUM;const s=M(h(n).year,1,1),a=(h(s).weekday-o+7)%7;return Math.floor((n-s+a)/7)+1});function A(t){if(y(t)||t===null)return[!1,!1,!1,!1,!1,!0,!0];if(typeof t=="string"){if(!/^[01]{7}$/.test(t))throw f.VALUE;return[...t].map(r=>r==="1")}const e=Math.trunc(u(t)),n=new Array(7).fill(!1);if(e>=1&&e<=7)n[(e+4)%7]=!0,n[(e+5)%7]=!0;else if(e>=11&&e<=17)n[e-11]=!0;else throw f.NUM;return n}function U(t,e){const n=new Set;return y(e)||e===null||j(t,e,r=>{if(r instanceof O)throw r;if(typeof r=="number")n.add(Math.floor(r));else if(typeof r=="string"){const o=L(r);if(o===null)throw f.VALUE;n.add(Math.floor(o))}}),n}const Y=(t,e)=>e[(h(t).weekday+6)%7];function V(t,e,n,r){const o=e>=t?1:-1,s=Math.min(t,e),a=Math.max(t,e),l=n.filter(d=>!d).length,w=a-s+1;let m=Math.floor(w/7)*l;for(let d=s+Math.floor(w/7)*7;d<=a;d++)Y(d,n)||m++;for(const d of r)d>=s&&d<=a&&!Y(d,n)&&m--;return m*o}const N=(t,e,n,r)=>{const o=t.scalar(e,n,r);if(o instanceof O)throw o;if(typeof o=="string"){const s=L(o);if(s===null)throw f.VALUE;return Math.floor(s)}return c(o)};p("NETWORKDAYS",2,3,(t,e,n,r)=>V(N(t,e[0],n,r),N(t,e[1],n,r),A(void 0),U(t,e[2]))),p("NETWORKDAYS.INTL",2,4,(t,e,n,r)=>V(N(t,e[0],n,r),N(t,e[1],n,r),A(y(e[2])?void 0:t.scalar(e[2],n,r)),U(t,e[3])));function W(t,e,n,r){const o=Math.trunc(e),s=o<0?-1:1;let a=Math.abs(o),l=t;if(n.every(Boolean))throw f.VALUE;for(;a>0;){if(l+=s,l<0||l>D)throw f.NUM;!Y(l,n)&&!r.has(l)&&a--}return l}p("WORKDAY",2,3,(t,e,n,r)=>W(N(t,e[0],n,r),u(t.scalar(e[1],n,r)),A(void 0),U(t,e[2]))),p("WORKDAY.INTL",2,4,(t,e,n,r)=>W(N(t,e[0],n,r),u(t.scalar(e[1],n,r)),A(y(e[2])?void 0:t.scalar(e[2],n,r)),U(t,e[3]))),i("YEARFRAC",2,3,(t,e,n)=>{const r=y(n)?0:Math.trunc(u(n));if(r<0||r>4)throw f.NUM;return X(c(t),c(e),r)}),i("DATEDIF",3,3,(t,e,n)=>{const r=c(t),o=c(e);if(r>o)throw f.NUM;const s=h(r),a=h(o),l=H(n).toUpperCase();let w=(a.year-s.year)*12+a.month-s.month;switch(a.day<s.day&&w--,l){case"D":return o-r;case"M":return w;case"Y":return Math.floor(w/12);case"YM":return w%12;case"YD":{let m=a.year;return(a.month<s.month||a.month===s.month&&a.day<s.day)&&m--,o-M(m,s.month,Math.min(s.day,E(m,s.month)))}case"MD":{if(a.day>=s.day)return a.day-s.day;const m=a.month===1?12:a.month-1,d=a.month===1?a.year-1:a.year;return E(d,m)-s.day+a.day}default:throw f.NUM}});export{I as Arr,K as Ref,F as RefList,k as alias};
1
+ import{setCategory as W}from"./registry.js";import{alias as g,lift as i,raw as N}from"./registry.js";import{Arr as I,ERR as f,num as u,OMITTED as C,Ref as K,RefList as F,str as H,XlError as j}from"../values.js";import{partsToSerial as M,serialToParts as h}from"../../core/numfmt.js";import{eachValue as $,toolpakScalar as D}from"./helpers.js";import{parseInput as z}from"../../core/input.js";import{NUM as B}from"../../core/sheet.js";import{days360 as X,yearFrac as Z}from"./financial.js";W("Date & Time");const y=t=>t===void 0||t===C,T=2958465;function c(t){const e=Math.floor(u(t));if(e<0||e>T)throw f.NUM;return e}function q(t,e,r){let o=t;if(o>=0&&o<1900&&(o+=1900),o<0||o>9999)throw f.NUM;const n=o*12+(e-1),s=Math.floor(n/12),a=n-s*12+1,l=M(s,a,1)+r-1;if(l<0||l>T)throw f.NUM;return l}i("DATE",3,3,(t,e,r)=>q(Math.trunc(u(t)),Math.trunc(u(e)),Math.trunc(u(r)))),i("YEAR",1,1,t=>h(c(t)).year),i("MONTH",1,1,t=>h(c(t)).month),i("DAY",1,1,t=>h(c(t)).day);const L=t=>{const e=u(t);if(e<0)throw f.NUM;return Math.round((e-Math.floor(e))*86400)};i("HOUR",1,1,t=>Math.floor(L(t)/3600)%24),i("MINUTE",1,1,t=>Math.floor(L(t)%3600/60)),i("SECOND",1,1,t=>L(t)%60),i("TIME",3,3,(t,e,r)=>{const o=Math.trunc(u(t))*3600+Math.trunc(u(e))*60+Math.trunc(u(r));if(o<0)throw f.NUM;return o%86400/86400});const S=()=>{const t=new Date;return{whole:M(t.getFullYear(),t.getMonth()+1,t.getDate()),frac:(t.getHours()*3600+t.getMinutes()*60+t.getSeconds()+t.getMilliseconds()/1e3)/86400}};i("NOW",0,0,()=>{const t=S();return t.whole+t.frac},{volatile:!0}),i("TODAY",0,0,()=>S().whole,{volatile:!0});const b={jan:1,feb:2,mar:3,apr:4,may:5,jun:6,jul:7,aug:8,sep:9,oct:10,nov:11,dec:12};function Y(t){const e=t.trim(),r=z(e);if(r.kind===B&&r.format&&/[ymd]/i.test(r.format))return r.value;let o=/^(\d{1,2})[-\s]([A-Za-z]{3,})[-\s,]*(\d{2,4})?$/.exec(e);if(o){const n=b[o[2].slice(0,3).toLowerCase()];if(n){const s=o[3]?Number(o[3])<100?2e3+Number(o[3]):Number(o[3]):new Date().getFullYear();return M(s,n,Number(o[1]))}}if(o=/^([A-Za-z]{3,})\s+(\d{1,2})(?:,?\s*(\d{2,4}))?$/.exec(e),o){const n=b[o[1].slice(0,3).toLowerCase()];if(n){const s=o[3]?Number(o[3])<100?2e3+Number(o[3]):Number(o[3]):new Date().getFullYear();return M(s,n,Number(o[2]))}}return null}i("DATEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const e=t.split(/\s+\d{1,2}:\d{2}/)[0],r=Y(e);if(r===null)throw f.VALUE;return Math.floor(r)}),i("TIMEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const e=/(\d{1,2}):(\d{2})(?::(\d{2}(?:\.\d+)?))?\s*(am|pm|a|p)?/i.exec(t);if(!e)throw f.VALUE;let r=Number(e[1]);if(e[4]){const o=e[4][0].toLowerCase()==="p";if(r>12)throw f.VALUE;r=r%12+(o?12:0)}return(r*3600+Number(e[2])*60+Number(e[3]??0))/86400%1}),i("DAYS",2,2,(t,e)=>c(t)-c(e)),i("DAYS360",2,3,(t,e,r)=>X(c(t),c(e),y(r)?!1:!!u(r)));function P(t,e){const r=h(t),o=r.year*12+(r.month-1)+e,n=Math.floor(o/12),s=o-n*12+1;if(n<1900||n>9999)throw f.NUM;const a=E(n,s);return M(n,s,Math.min(r.day,a))}function E(t,e){return e===2?t===1900||t%4===0&&(t%100!==0||t%400===0)?29:28:[31,28,31,30,31,30,31,31,30,31,30,31][e-1]}i("EDATE",2,2,(t,e)=>P(c(t),Math.trunc(u(e)))),i("EOMONTH",2,2,(t,e)=>{const r=h(c(t)),o=r.year*12+(r.month-1)+Math.trunc(u(e)),n=Math.floor(o/12),s=o-n*12+1;if(n<1900||n>9999)throw f.NUM;return M(n,s,E(n,s))}),i("WEEKDAY",1,2,(t,e)=>{const r=c(t),o=y(e)?1:Math.trunc(u(e)),n=h(r).weekday,s={1:0,2:1,3:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[o];if(s===void 0)throw f.NUM;const a=(n-s+7)%7;return o===3?a:a+1}),i("ISOWEEKNUM",1,1,t=>R(c(t)));function R(t){const e=(h(t).weekday+6)%7,r=t-e+3,o=h(r).year;return Math.floor((r-M(o,1,1))/7)+1}i("WEEKNUM",1,2,(t,e)=>{const r=c(t),o=y(e)?1:Math.trunc(u(e));if(o===21)return R(r);const n={1:0,2:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[o];if(n===void 0)throw f.NUM;const s=M(h(r).year,1,1),a=(h(s).weekday-n+7)%7;return Math.floor((r-s+a)/7)+1});function A(t){if(y(t)||t===null)return[!1,!1,!1,!1,!1,!0,!0];if(typeof t=="string"){if(!/^[01]{7}$/.test(t))throw f.VALUE;return[...t].map(o=>o==="1")}const e=Math.trunc(u(t)),r=new Array(7).fill(!1);if(e>=1&&e<=7)r[(e+4)%7]=!0,r[(e+5)%7]=!0;else if(e>=11&&e<=17)r[e-11]=!0;else throw f.NUM;return r}function U(t,e){const r=new Set;return y(e)||e===null||$(t,e,o=>{if(o instanceof j)throw o;if(typeof o=="number")r.add(Math.floor(o));else if(typeof o=="string"){const n=Y(o);if(n===null)throw f.VALUE;r.add(Math.floor(n))}}),r}const O=(t,e)=>e[(h(t).weekday+6)%7];function V(t,e,r,o){const n=e>=t?1:-1,s=Math.min(t,e),a=Math.max(t,e),l=r.filter(d=>!d).length,w=a-s+1;let m=Math.floor(w/7)*l;for(let d=s+Math.floor(w/7)*7;d<=a;d++)O(d,r)||m++;for(const d of o)d>=s&&d<=a&&!O(d,r)&&m--;return m*n}const p=(t,e,r,o)=>{const n=D(t,e,r,o);if(typeof n=="string"){const s=Y(n);if(s===null)throw f.VALUE;return Math.floor(s)}return c(n)};N("NETWORKDAYS",2,3,(t,e,r,o)=>V(p(t,e[0],r,o),p(t,e[1],r,o),A(void 0),U(t,e[2]))),N("NETWORKDAYS.INTL",2,4,(t,e,r,o)=>V(p(t,e[0],r,o),p(t,e[1],r,o),A(y(e[2])?void 0:t.scalar(e[2],r,o)),U(t,e[3])));function k(t,e,r,o){const n=Math.trunc(e),s=n<0?-1:1;let a=Math.abs(n),l=t;if(r.every(Boolean))throw f.VALUE;for(;a>0;){if(l+=s,l<0||l>T)throw f.NUM;!O(l,r)&&!o.has(l)&&a--}return l}N("WORKDAY",2,3,(t,e,r,o)=>k(p(t,e[0],r,o),u(D(t,e[1],r,o)),A(void 0),U(t,e[2]))),N("WORKDAY.INTL",2,4,(t,e,r,o)=>k(p(t,e[0],r,o),u(D(t,e[1],r,o)),A(y(e[2])?void 0:t.scalar(e[2],r,o)),U(t,e[3]))),i("YEARFRAC",2,3,(t,e,r)=>{const o=y(r)?0:Math.trunc(u(r));if(o<0||o>4)throw f.NUM;return Z(c(t),c(e),o)}),i("DATEDIF",3,3,(t,e,r)=>{const o=c(t),n=c(e);if(o>n)throw f.NUM;const s=h(o),a=h(n),l=H(r).toUpperCase();let w=(a.year-s.year)*12+a.month-s.month;switch(a.day<s.day&&w--,l){case"D":return n-o;case"M":return w;case"Y":return Math.floor(w/12);case"YM":return w%12;case"YD":{let m=a.year;return(a.month<s.month||a.month===s.month&&a.day<s.day)&&m--,n-M(m,s.month,Math.min(s.day,E(m,s.month)))}case"MD":{if(a.day>=s.day)return a.day-s.day;const m=a.month===1?12:a.month-1,d=a.month===1?a.year-1:a.year;return E(d,m)-s.day+a.day}default:throw f.NUM}});export{I as Arr,K as Ref,F as RefList,g as alias};
@@ -0,0 +1 @@
1
+ let a=null;const A=()=>a;function p(t){const e=String(t??"").slice(0,2).toLowerCase();a=e==="ja"||e==="zh"||e==="ko"?e:null}const g=65377,u=65439,c=new Map,f=new Map;for(let t=g;t<=u;t++){const e=String.fromCharCode(t),n=t===65438?"゛":t===65439?"゜":e.normalize("NFKC");c.set(e,n),f.set(n,e)}const h=/[。-゚][゙゚]?/g,s=/[、。「」゛゜ァ-ー]/g;function C(t){return t.replace(/[!-~]/g,e=>String.fromCharCode(e.charCodeAt(0)+65248)).replace(/ /g," ").replace(h,e=>{if(e.length===2){const n=(c.get(e[0])+(e[1]==="゙"?"゙":"゚")).normalize("NFC");if(n.length===1)return n}return Array.from(e,n=>c.get(n)).join("")})}function L(t){return t.replace(/[!-~]/g,e=>String.fromCharCode(e.charCodeAt(0)-65248)).replace(/ /g," ").replace(s,e=>{const n=f.get(e);if(n)return n;const[r,l]=e.normalize("NFD"),i=f.get(r);return!i||l!=="゙"&&l!=="゚"?e:i+(l==="゙"?"゙":"゚")})}const o=t=>t<128||a==="ja"&&t>=g&&t<=u?1:2;function x(t){let e=0;for(let n=0;n<t.length;n++)e+=o(t.charCodeAt(n));return e}function F(t,e){let n=0,r=0;for(;r<t.length&&n<e;)n+=o(t.charCodeAt(r++));return r}function d(t,e){let n=0,r=0;for(;r<t.length&&n+o(t.charCodeAt(r))<=e;)n+=o(t.charCodeAt(r++));return r}export{x as byteLength,A as dbcsLanguage,p as setDbcsLanguage,C as toFullWidth,L as toHalfWidth,F as unitAtByte,d as unitBeforeByte};
@@ -1 +1 @@
1
- import{setCategory as Ot}from"./registry.js";import{lift as Nt,raw as B}from"./registry.js";import{Arr as vt,bool as mt,checked as X,ERR as N,num as h,opt as R,Ref as Ut,RefList as Ft,XlError as Vt}from"../values.js";import{scalarOf as dt}from"./helpers.js";import{partsToSerial as rt,serialToParts as x}from"../../core/numfmt.js";Ot("Financial");const kt=2958465,pt=1e-7,yt=200,y=(t,n,s,o)=>Nt(t,n,s,(...e)=>X(o(...e))),p=t=>{if(!t)throw N.NUM},m=t=>{if(typeof t=="boolean")throw N.VALUE;return h(t)},G=t=>h(R(t,0))!==0?1:0;function D(t){const n=Math.trunc(m(t));if(n<0||n>kt)throw N.VALUE;return n}function F(t){const n=Math.trunc(m(R(t,0)));return p(n>=0&&n<=4),n}function z(t){const n=Math.trunc(m(t));return p(n===1||n===2||n===4),n}const J=t=>t%4===0&&(t%100!==0||t%400===0),ct=(t,n)=>n===2?J(t)?29:28:n===4||n===6||n===9||n===11?30:31;function Z(t){const n=x(t);return n.day>=ct(n.year,n.month)}function P(t,n,s){const o=x(t),e=o.year*12+o.month-1+n,r=Math.floor(e/12),a=e-r*12+1,c=ct(r,a);return rt(r,a,s?c:Math.min(o.day,c))}function $(t,n,s=!1){const o=x(t),e=x(n);let r=o.day,a=e.day;if(s)r===31&&(r=30),a===31&&(a=30);else{const c=f=>f.month===2&&f.day>=ct(f.year,2);c(o)&&(c(e)&&(a=30),r=30),a===31&&r>=30&&(a=30),r===31&&(r=30)}return(e.year-o.year)*360+(e.month-o.month)*30+a-r}const V=(t,n,s)=>s===0?$(t,n):s===4?$(t,n,!0):n-t;function St(t,n){const s=x(t),o=x(n);if(s.year===o.year)return(n-t)/(J(s.year)?366:365);if(o.year===s.year+1&&(s.month>o.month||s.month===o.month&&s.day>=o.day)){const r=J(s.year)&&t<=rt(s.year,2,29)||J(o.year)&&n>=rt(o.year,2,29);return(n-t)/(r?366:365)}let e=0;for(let r=s.year;r<=o.year;r++)e+=J(r)?366:365;return(n-t)/(e/(o.year-s.year+1))}function _(t,n,s=0){const o=Math.trunc(Math.min(t,n)),e=Math.trunc(Math.max(t,n));switch(s){case 1:return St(o,e);case 2:return(e-o)/360;case 3:return(e-o)/365;case 4:return $(o,e,!0)/360;default:return $(o,e)/360}}function at(t,n,s,o){const e=Z(n),r=12/s;let a=1;for(;P(n,-a*r,e)>t;)a++;const c=P(n,-a*r,e),f=P(n,(1-a)*r,e),i=V(c,t,o),u=o===1?f-c:(o===3?365:360)/s,l=o===0||o===4?u-i:f-t;return{n:a,pcd:c,ncd:f,a:i,e:u,dsc:l,f:s}}function tt(t,n,s,o){const e=D(t),r=D(n),a=z(s),c=F(o);return p(e<r),at(e,r,a,c)}function nt(t,n,s=-1){const o=.001*Math.max(1,Math.abs(t(n))),e=u=>Math.abs(t(u))<=o;let r=n;for(let u=0;u<yt;u++){const l=t(r);if(l===0)return r;const M=Math.min(1e-6*(1+Math.abs(r)),(r-s)/2),d=r-l*2*M/(t(r+M)-t(r-M));if(!Number.isFinite(d)||d<=s)break;if(Math.abs(d-r)<pt&&e(d))return d;r=d}let a=n,c=n+.01*(1+Math.abs(n)),f=t(a),i=t(c);for(let u=0;u<yt;u++){if(i===0)return c;let l=c-i*(c-a)/(i-f);if(l<=s&&(l=(c+s)/2),!Number.isFinite(l))break;if(Math.abs(l-c)<pt&&e(l))return l;a=c,f=i,c=l,i=t(c)}throw N.NUM}const K=(t,n)=>(1+t)**n,ot=(t,n)=>t===0?n:(t>-1?Math.expm1(n*Math.log1p(t)):K(t,n)-1)/t,ft=(t,n,s,o,e)=>-(o*K(t,n)+s*(1+t*e)*ot(t,n)),Yt=(t,n,s,o,e)=>-(o+s*(1+t*e)*ot(t,n))/K(t,n),et=(t,n,s,o,e)=>-(o+s*K(t,n))/((1+t*e)*ot(t,n));function st(t,n,s,o,e,r){const a=et(t,s,o,e,r);return n===1?r?0:-o*t:(r?ft(t,n-2,a,o,1)-a:ft(t,n-1,a,o,0))*t}function It(t,n,s,o,e,r){const a=[h(t),h(n),h(s),h(o),h(R(e,0)),G(r)];return p(a[1]>=1&&a[1]<a[2]+1),a}y("PMT",3,5,(t,n,s,o,e)=>et(h(t),h(n),h(s),h(R(o,0)),G(e))),y("PV",3,5,(t,n,s,o,e)=>Yt(h(t),h(n),h(s),h(R(o,0)),G(e))),y("FV",3,5,(t,n,s,o,e)=>ft(h(t),h(n),h(s),h(R(o,0)),G(e))),y("IPMT",4,6,(...t)=>st(...It(...t))),y("PPMT",4,6,(...t)=>{const[n,s,o,e,r,a]=It(...t);return et(n,o,e,r,a)-st(n,s,o,e,r,a)}),y("NPER",3,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=h(R(o,0)),i=G(e);if(r===0){if(a===0)throw N.DIV0;return-(c+f)/a}const u=a*(1+r*i);return Math.log((u-f*r)/(u+c*r))/Math.log1p(r)}),y("RATE",3,6,(t,n,s,o,e,r)=>{const a=h(t),c=h(n),f=h(s),i=h(R(o,0)),u=G(e),l=h(R(r,.1));p(a>0&&l>-1);const M=nt(d=>f*K(d,a)+c*(1+d*u)*ot(d,a)+i,l);return p(M>-1+1e-6),M}),y("ISPMT",4,4,(t,n,s,o)=>{const e=h(s);if(e===0)throw N.DIV0;return h(o)*h(t)*(h(n)/e-1)}),y("PDURATION",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return p(o>0&&e>0&&r>0),(Math.log(r)-Math.log(e))/Math.log1p(o)}),y("RRI",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return p(o>0&&e!==0&&e*r>=0),(r/e)**(1/o)-1});function Dt(t,n,s,o,e,r,a){const c=m(t),f=Math.trunc(m(n)),i=m(s),u=Math.trunc(m(o)),l=Math.trunc(m(e)),M=Math.trunc(m(r));p(c>0&&f>0&&i>0&&u>=1&&l>=u&&l<=f&&(M===0||M===1));let d=0;for(let I=u;I<=l;I++)d+=a(c,I,f,i,M);return d}y("CUMIPMT",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>st(n,s,o,e,0,r))),y("CUMPRINC",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>et(n,o,e,0,r)-st(n,s,o,e,0,r)));function q(t,n){const s=[],o=e=>{for(const r of t.toArr(e).data)if(typeof r=="number")s.push(r);else if(r instanceof Vt)throw r};return n instanceof Ft?n.refs.forEach(o):n instanceof Ut||n instanceof vt?o(n):s.push(h(n)),s}const H=(t,n,s,o)=>h(dt(t,n,s,o)),it=t=>t.some(n=>n>0)&&t.some(n=>n<0),bt=99;function Et(t,n,s){const o=q(t,n),e=q(t,s).map(D);return p(o.length===e.length&&o.length>0),p(e.every(r=>r>=e[0])),[o,e.map(r=>(r-e[0])/365)]}function Q(t,n,s){let o=0;for(let e=0;e<t.length;e++)o+=t[e]/(1+s)**n[e];return o}B("NPV",2,255,(t,n,s,o)=>{const e=H(t,n[0],s,o);if(e===-1)throw N.DIV0;let r=0,a=1;for(let c=1;c<n.length;c++)for(const f of q(t,n[c]))r+=f/(1+e)**a++;return X(r)},{array:!0}),B("IRR",1,2,(t,n,s,o)=>{const e=q(t,n[0]),r=H(t,R(n[1],.1),s,o);return p(r>-1&&it(e)),nt(a=>{let c=0;for(let f=0;f<e.length;f++)c+=e[f]/(1+a)**f;return c},r)},{array:!0}),B("MIRR",3,3,(t,n,s,o)=>{const e=q(t,n[0]),r=H(t,n[1],s,o),a=H(t,n[2],s,o);if(r===-1||a===-1||!it(e))throw N.DIV0;const c=e.length-1;let f=0,i=0;return e.forEach((u,l)=>{u>0?f+=u*(1+a)**(c-l):i+=u/(1+r)**l}),X((f/-i)**(1/c)-1)},{array:!0}),B("XNPV",3,3,(t,n,s,o)=>{const e=H(t,n[0],s,o),[r,a]=Et(t,n[1],n[2]);return p(e>=0),X(Q(r,a,e))},{array:!0});function Bt(t,n,s){const o=t[0],e=u=>u*o>=0,r=s===0?1e-5:s,a=Q(t,n,0)*o>0;let c=0,f=r,i=0;if(a){p(Math.abs(r)<1);const u=-Math.abs(r);e(Q(t,n,u))?[c,f]=[-1,u]:[c,f]=[u,0]}else for(p(r>0),e(Q(t,n,f))||(i=1);!e(Q(t,n,f));)c=f,f*=2,i++,p(i<=bt);for(;++i<=bt;){const u=(c+f)/2,l=Q(t,n,u);if(e(l)?f=u:c=u,f-c<=1e-8*(Math.abs(u)+.5)&&(!a||Math.abs(l)<=2e-8*Math.abs(o)))return u}throw N.NUM}B("XIRR",2,3,(t,n,s,o)=>{const[e,r]=Et(t,n[0],n[1]),a=H(t,R(n[2],.1),s,o);return p(a>-1&&it(e)),X(Bt(e,r,a))},{array:!0}),B("FVSCHEDULE",2,2,(t,n,s,o)=>{let e=m(dt(t,n[0],s,o));for(const r of q(t,n[1]))e*=1+r;return X(e)},{array:!0}),y("SLN",3,3,(t,n,s)=>{const o=h(s);if(o===0)throw N.DIV0;return(h(t)-h(n))/o}),y("SYD",4,4,(t,n,s,o)=>{const[e,r]=[h(s),h(o)];return p(e>0&&r>0&&r<=e),(h(t)-h(n))*(e-r+1)*2/(e*(e+1))}),y("DB",4,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=Math.trunc(h(o)),i=Math.trunc(h(R(e,12)));if(p(r>=0&&a>=0&&c>0&&f>=1&&i>=1&&i<=12),p(f<=(i===12?c:c+1)),a>=r)return 0;const u=Math.round((1-(a/r)**(1/c))*1e3)/1e3;let l=r*u*i/12;if(f===1)return l;let M=0;for(let d=2;d<=Math.min(f,c);d++)M=(r-l)*u,l+=M;return f>c?(r-l)*u*(12-i)/12:M});function ut(t,n,s,o,e){let r=e/s,a;r>=1?(r=1,a=o===1?t:0):a=t*(1-r)**(o-1);const c=t*(1-r)**o;return Math.max(a-Math.max(c,n),0)}y("DDB",4,5,(t,n,s,o,e)=>{const[r,a,c,f]=[h(t),h(n),h(s),h(o)],i=h(R(e,2));return p(r>=0&&a>=0&&c>0&&f>0&&f<=c&&i>0),ut(r,a,c,f,i)});function Rt(t,n,s,o,e,r){const a=Math.ceil(e);let c=t-n,f=0,i=!1,u=0;for(let l=1;l<=a;l++){let M=f;if(!i){const d=ut(t,n,s,l,r);f=c/(o-(l-1)),i=f>d,M=i?f:d,i||(c-=d)}u+=l===a?M*(e+1-a):M}return u}y("VDB",5,7,(t,n,s,o,e,r,a)=>{let c=h(t);const f=h(n),i=h(s);let u=h(o),l=h(e);const M=h(R(r,2)),d=mt(R(a,!1));if(p(c>=0&&f>=0&&i>0&&u>=0&&l>=u&&l<=i&&M>=0),d){const E=Math.floor(u),b=Math.ceil(l);let g=0;for(let A=E+1;A<=b;A++){let C=ut(c,f,i,A,M);A===E+1?C*=Math.min(l,E+1)-u:A===b&&(C*=l+1-b),g+=C}return g}let I=i;return u!==Math.floor(u)&&M>1&&u>=i/2&&(l-=u-i/2,u=i/2,I+=1),c-=Rt(c,f,i,I,u,M),Rt(c,f,i,i-u,l-u,M)});function At(t,n,s,o,e,r,a){const c={cost:m(t),bought:D(n),firstEnd:D(s),salvage:m(o),per:Math.trunc(m(e)),rate:m(r),basis:F(a)};return p(c.basis!==2&&c.cost>=0&&c.salvage>=0&&c.salvage<=c.cost),p(c.per>=0&&c.rate>0&&c.bought<=c.firstEnd),c.frac=_(c.bought,c.firstEnd,c.basis),c}y("AMORLINC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=n*e,c=r*a,f=Math.trunc((n-s-c)/a);return o===0?c:o<=f?a:o===f+1?n-s-a*f-c:0}),y("AMORDEGRC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=1/e,c=(M,d)=>a>=M&&a<=d;p(c(3,4)||c(5,6)||a>6);const f=e*(c(3,4)?1.5:c(5,6)?2:2.5);let i=Math.round(r*f*n),u=n-i,l=u-s;for(let M=0;M<o;M++){if(i=Math.round(f*u),l-=i,l<0)return o-M===1?Math.round(u*.5):0;u-=i}return i}),y("EFFECT",2,2,(t,n)=>{const s=m(t),o=Math.trunc(m(n));return p(s>0&&o>=1),(1+s/o)**o-1}),y("NOMINAL",2,2,(t,n)=>{const s=m(t),o=Math.trunc(m(n));return p(s>0&&o>=1),o*((1+s)**(1/o)-1)});function Ct(t,n,s){const o=m(t),e=Math.trunc(m(n));if(p(e>=0),e===0)throw N.DIV0;const r=Math.trunc(o);return r+s(o-r,e,10**Math.ceil(Math.log10(e)))}y("DOLLARDE",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*e/o)),y("DOLLARFR",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*o/e));function lt(t,n,s,o){const e=D(t),r=D(n),a=m(s);return p(e<r&&r<=P(e,12,!1)&&a>0),o(a,r-e)}y("TBILLEQ",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>365*o/(360-o*e))),y("TBILLPRICE",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>100*(1-o*e/360))),y("TBILLYIELD",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>(100-o)/o*360/e)),y("ACCRINT",6,8,(t,n,s,o,e,r,a,c)=>{const f=D(t),i=D(n),u=D(s),l=m(o),M=m(R(e,1e3)),d=z(r),I=F(a),E=mt(R(c,!0));p(l>0&&M>0&&f<u);const b=!E&&u>i?i:f,g=12/d,A=Z(i);let C=0;for(;P(i,C*g,A)>b;)C--;for(;P(i,(C+1)*g,A)<=b;)C++;let U=0;for(;P(i,C*g,A)<u;C++){const L=P(i,C*g,A),v=P(i,(C+1)*g,A),T=I===1?v-L:(I===3?365:360)/d;U+=V(Math.max(L,b),Math.min(v,u),I)/T}return M*l*U/d}),y("ACCRINTM",3,5,(t,n,s,o,e)=>{const r=D(t),a=D(n),c=m(s),f=m(R(o,1e3)),i=F(e);return p(c>0&&f>0&&r<a),f*c*_(r,a,i)});const j=(t,n)=>y(t,3,4,(s,o,e,r)=>n(tt(s,o,e,r)));j("COUPDAYBS",t=>t.a),j("COUPDAYS",t=>t.e),j("COUPDAYSNC",t=>t.dsc),j("COUPNCD",t=>t.ncd),j("COUPNUM",t=>t.n),j("COUPPCD",t=>t.pcd);const W=(t,n)=>y(t,4,5,(s,o,e,r,a)=>{const c=D(s),f=D(o),i=m(e),u=m(r),l=F(a);return p(c<f&&i>0&&u>0),n(i,u,_(c,f,l))});W("DISC",(t,n,s)=>(n-t)/n/s),W("INTRATE",(t,n,s)=>(n-t)/t/s),W("RECEIVED",(t,n,s)=>{const o=1-n*s;return p(o>0),t/o}),W("PRICEDISC",(t,n,s)=>n*(1-t*s)),W("YIELDDISC",(t,n,s)=>(n/t-1)/s);const gt=(t,n,s)=>y(t,5,6,(o,e,r,a,c,f)=>{const i=D(o),u=D(e),l=D(r),M=m(a),d=m(c),I=F(f);return p(i<u&&l<i&&M>=0&&n(d)),s(M,d,_(l,u,I),_(l,i,I),_(i,u,I))});gt("PRICEMAT",t=>t>=0,(t,n,s,o,e)=>(100+s*t*100)/(1+e*n)-o*t*100),gt("YIELDMAT",t=>t>0,(t,n,s,o,e)=>{const r=n/100+o*t;return(1+s*t-r)/r/e});function Pt(t,n,s,o){const e=100*n/t.f,r=t.dsc/t.e;if(t.n===1)return(o+e)/(1+s/t.f*r)-e*t.a/t.e;const a=1+s/t.f;let c=o/a**(t.n-1+r);for(let f=0;f<t.n;f++)c+=e/a**(f+r);return c-e*t.a/t.e}y("PRICE",6,7,(t,n,s,o,e,r,a)=>{const c=m(s),f=m(o),i=m(e),u=tt(t,n,r,a);return p(c>=0&&f>=0&&i>0),Pt(u,c,f,i)}),y("YIELD",6,7,(t,n,s,o,e,r,a)=>{const c=m(s),f=m(o),i=m(e),u=tt(t,n,r,a);if(p(c>=0&&f>0&&i>0),u.n===1){const l=c/u.f,M=f/100+u.a/u.e*l;return(i/100+l-M)/M*(u.f*u.e/u.dsc)}return nt(l=>Pt(u,c,l,i)-f,.1,-u.f)});function Lt(t,n,s,o,e,r){const a=m(s),c=m(o),f=tt(t,n,e,r);p(a>=0&&c>=0);const i=1+c/f.f,u=100*a/f.f,l=f.dsc/f.e;let M=0,d=0;for(let I=0;I<f.n;I++){const E=I+l,b=(I===f.n-1?u+100:u)/i**E;M+=E*b,d+=b}return[M/d/f.f,i]}y("DURATION",5,6,(...t)=>Lt(...t)[0]),y("MDURATION",5,6,(...t)=>{const[n,s]=Lt(...t);return n/s});function Tt(t,n,s,o,e,r,a,c){const f=D(t),i=D(n),u=D(s),l=D(o),M=m(e),d=m(r),I=z(a),E=F(c);p(i>l&&l>f&&f>u&&M>=0&&d>0);const b=at(f,l,I,E),g=b.n===1?V(f,l,E):b.dsc,A=at(l,i,I,E).n,C=Z(l),U=12/I;let L=0,v=0;for(let k=0,w=l;;k++){const O=P(l,-(k+1)*U,C),S=E===1?w-O:b.e;L+=(O>u?S:V(u,w,E))/S;const ht=Math.max(u,O),Mt=Math.min(f,w);if(Mt>ht&&(v+=V(ht,Mt,E)/S),O<=u)break;w=O}const T=100*M/I,Y=b.n-1+g/b.e;return k=>{const w=1+k/I;let O=d/w**(A+Y)+T*L/w**Y;for(let S=1;S<=A;S++)O+=T/w**(S+Y);return O-T*v}}y("ODDFPRICE",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=m(r),u=Tt(t,n,s,o,e,a,c,f);return p(i>=0),u(i)}),y("ODDFYIELD",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=m(r),u=Tt(t,n,s,o,e,a,c,f);return p(i>0),nt(l=>u(l)-i,.1,-z(c))});function wt(t,n,s,o,e,r,a){const c=D(t),f=D(n),i=D(s),u=m(o),l=m(e),M=z(r),d=F(a);p(f>c&&c>i&&u>=0&&l>0);const I=12/M,E=Z(i);let b=0,g=0,A=0;for(let U=0;;U++){const L=P(i,U*I,E),v=P(i,(U+1)*I,E),T=d===0||d===4?360/M:v-L,Y=v>=f,k=Y?V(L,f,d):T;b+=k/T,v<c?g+=k/T:L<c&&(g+=V(L,c,d)/T);const w=Math.max(c,L),O=Math.min(f,v);if(O>w&&(A+=V(w,O,d)/T),Y)break}const C=100*u/M;return{income:l+b*C,interest:g*C,toGo:A,f:M}}y("ODDLPRICE",7,8,(t,n,s,o,e,r,a,c)=>{const f=m(e),i=wt(t,n,s,o,r,a,c);return p(f>=0),i.income/(1+i.toGo*f/i.f)-i.interest}),y("ODDLYIELD",7,8,(t,n,s,o,e,r,a,c)=>{const f=m(e),i=wt(t,n,s,o,r,a,c);p(f>0);const u=f+i.interest;return(i.income-u)/u*(i.f/i.toGo)});export{$ as days360,_ as yearFrac};
1
+ import{setCategory as Ot}from"./registry.js";import{lift as Nt,raw as B}from"./registry.js";import{Arr as vt,bool as mt,checked as X,ERR as N,num as h,opt as R,Ref as Ut,RefList as Ft,XlError as Vt}from"../values.js";import{scalarOf as kt,toolpakScalar as dt}from"./helpers.js";import{partsToSerial as rt,serialToParts as x}from"../../core/numfmt.js";Ot("Financial");const St=2958465,pt=1e-7,yt=200,y=(t,n,s,o)=>Nt(t,n,s,(...e)=>X(o(...e))),m=t=>{if(!t)throw N.NUM},d=t=>{if(typeof t=="boolean")throw N.VALUE;return h(t)},G=t=>h(R(t,0))!==0?1:0;function D(t){const n=Math.trunc(d(t));if(n<0||n>St)throw N.VALUE;return n}function F(t){const n=Math.trunc(d(R(t,0)));return m(n>=0&&n<=4),n}function j(t){const n=Math.trunc(d(t));return m(n===1||n===2||n===4),n}const z=t=>t%4===0&&(t%100!==0||t%400===0),ct=(t,n)=>n===2?z(t)?29:28:n===4||n===6||n===9||n===11?30:31;function Z(t){const n=x(t);return n.day>=ct(n.year,n.month)}function P(t,n,s){const o=x(t),e=o.year*12+o.month-1+n,r=Math.floor(e/12),a=e-r*12+1,c=ct(r,a);return rt(r,a,s?c:Math.min(o.day,c))}function $(t,n,s=!1){const o=x(t),e=x(n);let r=o.day,a=e.day;if(s)r===31&&(r=30),a===31&&(a=30);else{const c=f=>f.month===2&&f.day>=ct(f.year,2);c(o)&&(c(e)&&(a=30),r=30),a===31&&r>=30&&(a=30),r===31&&(r=30)}return(e.year-o.year)*360+(e.month-o.month)*30+a-r}const V=(t,n,s)=>s===0?$(t,n):s===4?$(t,n,!0):n-t;function Yt(t,n){const s=x(t),o=x(n);if(s.year===o.year)return(n-t)/(z(s.year)?366:365);if(o.year===s.year+1&&(s.month>o.month||s.month===o.month&&s.day>=o.day)){const r=z(s.year)&&t<=rt(s.year,2,29)||z(o.year)&&n>=rt(o.year,2,29);return(n-t)/(r?366:365)}let e=0;for(let r=s.year;r<=o.year;r++)e+=z(r)?366:365;return(n-t)/(e/(o.year-s.year+1))}function _(t,n,s=0){const o=Math.trunc(Math.min(t,n)),e=Math.trunc(Math.max(t,n));switch(s){case 1:return Yt(o,e);case 2:return(e-o)/360;case 3:return(e-o)/365;case 4:return $(o,e,!0)/360;default:return $(o,e)/360}}function at(t,n,s,o){const e=Z(n),r=12/s;let a=1;for(;P(n,-a*r,e)>t;)a++;const c=P(n,-a*r,e),f=P(n,(1-a)*r,e),i=V(c,t,o),u=o===1?f-c:(o===3?365:360)/s,l=o===0||o===4?u-i:f-t;return{n:a,pcd:c,ncd:f,a:i,e:u,dsc:l,f:s}}function tt(t,n,s,o,e=!1){const r=D(t),a=D(n);e&&m(r<a);const c=j(s),f=F(o);return m(r<a),at(r,a,c,f)}function nt(t,n,s=-1){const o=.001*Math.max(1,Math.abs(t(n))),e=u=>Math.abs(t(u))<=o;let r=n;for(let u=0;u<yt;u++){const l=t(r);if(l===0)return r;const M=Math.min(1e-6*(1+Math.abs(r)),(r-s)/2),p=r-l*2*M/(t(r+M)-t(r-M));if(!Number.isFinite(p)||p<=s)break;if(Math.abs(p-r)<pt&&e(p))return p;r=p}let a=n,c=n+.01*(1+Math.abs(n)),f=t(a),i=t(c);for(let u=0;u<yt;u++){if(i===0)return c;let l=c-i*(c-a)/(i-f);if(l<=s&&(l=(c+s)/2),!Number.isFinite(l))break;if(Math.abs(l-c)<pt&&e(l))return l;a=c,f=i,c=l,i=t(c)}throw N.NUM}const J=(t,n)=>(1+t)**n,ot=(t,n)=>t===0?n:(t>-1?Math.expm1(n*Math.log1p(t)):J(t,n)-1)/t,ft=(t,n,s,o,e)=>-(o*J(t,n)+s*(1+t*e)*ot(t,n)),Bt=(t,n,s,o,e)=>-(o+s*(1+t*e)*ot(t,n))/J(t,n),et=(t,n,s,o,e)=>-(o+s*J(t,n))/((1+t*e)*ot(t,n));function st(t,n,s,o,e,r){const a=et(t,s,o,e,r);return n===1?r?0:-o*t:(r?ft(t,n-2,a,o,1)-a:ft(t,n-1,a,o,0))*t}function It(t,n,s,o,e,r){const a=[h(t),h(n),h(s),h(o),h(R(e,0)),G(r)];return m(a[1]>=1&&a[1]<a[2]+1),a}y("PMT",3,5,(t,n,s,o,e)=>et(h(t),h(n),h(s),h(R(o,0)),G(e))),y("PV",3,5,(t,n,s,o,e)=>Bt(h(t),h(n),h(s),h(R(o,0)),G(e))),y("FV",3,5,(t,n,s,o,e)=>ft(h(t),h(n),h(s),h(R(o,0)),G(e))),y("IPMT",4,6,(...t)=>st(...It(...t))),y("PPMT",4,6,(...t)=>{const[n,s,o,e,r,a]=It(...t);return et(n,o,e,r,a)-st(n,s,o,e,r,a)}),y("NPER",3,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=h(R(o,0)),i=G(e);if(r===0){if(a===0)throw N.DIV0;return-(c+f)/a}const u=a*(1+r*i);return Math.log((u-f*r)/(u+c*r))/Math.log1p(r)}),y("RATE",3,6,(t,n,s,o,e,r)=>{const a=h(t),c=h(n),f=h(s),i=h(R(o,0)),u=G(e),l=h(R(r,.1));m(a>0&&l>-1);const M=nt(p=>f*J(p,a)+c*(1+p*u)*ot(p,a)+i,l);return m(M>-1+1e-6),M}),y("ISPMT",4,4,(t,n,s,o)=>{const e=h(s);if(e===0)throw N.DIV0;return h(o)*h(t)*(h(n)/e-1)}),y("PDURATION",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return m(o>0&&e>0&&r>0),(Math.log(r)-Math.log(e))/Math.log1p(o)}),y("RRI",3,3,(t,n,s)=>{const[o,e,r]=[h(t),h(n),h(s)];return m(o>0&&e!==0&&e*r>=0),(r/e)**(1/o)-1});function Dt(t,n,s,o,e,r,a){const c=d(t),f=Math.trunc(d(n)),i=d(s),u=Math.trunc(d(o)),l=Math.trunc(d(e)),M=Math.trunc(d(r));m(c>0&&f>0&&i>0&&u>=1&&l>=u&&l<=f&&(M===0||M===1));let p=0;for(let I=u;I<=l;I++)p+=a(c,I,f,i,M);return p}y("CUMIPMT",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>st(n,s,o,e,0,r))),y("CUMPRINC",6,6,(...t)=>Dt(...t,(n,s,o,e,r)=>et(n,o,e,0,r)-st(n,s,o,e,0,r)));function q(t,n){const s=[],o=e=>{for(const r of t.toArr(e).data)if(typeof r=="number")s.push(r);else if(r instanceof Vt)throw r};return n instanceof Ft?n.refs.forEach(o):n instanceof Ut||n instanceof vt?o(n):s.push(h(n)),s}const K=(t,n,s,o)=>h(kt(t,n,s,o)),it=t=>t.some(n=>n>0)&&t.some(n=>n<0),bt=99;function Et(t,n,s){const o=q(t,n),e=q(t,s).map(D);return m(o.length===e.length&&o.length>0),m(e.every(r=>r>=e[0])),[o,e.map(r=>(r-e[0])/365)]}function H(t,n,s){let o=0;for(let e=0;e<t.length;e++)o+=t[e]/(1+s)**n[e];return o}B("NPV",2,255,(t,n,s,o)=>{const e=K(t,n[0],s,o);if(e===-1)throw N.DIV0;let r=0,a=1;for(let c=1;c<n.length;c++)for(const f of q(t,n[c]))r+=f/(1+e)**a++;return X(r)},{array:!0}),B("IRR",1,2,(t,n,s,o)=>{const e=q(t,n[0]),r=K(t,R(n[1],.1),s,o);return m(r>-1&&it(e)),nt(a=>{let c=0;for(let f=0;f<e.length;f++)c+=e[f]/(1+a)**f;return c},r)},{array:!0}),B("MIRR",3,3,(t,n,s,o)=>{const e=q(t,n[0]),r=K(t,n[1],s,o),a=K(t,n[2],s,o);if(r===-1||a===-1||!it(e))throw N.DIV0;const c=e.length-1;let f=0,i=0;return e.forEach((u,l)=>{u>0?f+=u*(1+a)**(c-l):i+=u/(1+r)**l}),X((f/-i)**(1/c)-1)},{array:!0}),B("XNPV",3,3,(t,n,s,o)=>{const e=h(dt(t,n[0],s,o)),[r,a]=Et(t,n[1],n[2]);return m(e>=0),X(H(r,a,e))},{array:!0});function Xt(t,n,s){const o=t[0],e=u=>u*o>=0,r=s===0?1e-5:s,a=H(t,n,0)*o>0;let c=0,f=r,i=0;if(a){m(Math.abs(r)<1);const u=-Math.abs(r);e(H(t,n,u))?[c,f]=[-1,u]:[c,f]=[u,0]}else for(m(r>0),e(H(t,n,f))||(i=1);!e(H(t,n,f));)c=f,f*=2,i++,m(i<=bt);for(;++i<=bt;){const u=(c+f)/2,l=H(t,n,u);if(e(l)?f=u:c=u,f-c<=1e-8*(Math.abs(u)+.5)&&(!a||Math.abs(l)<=2e-8*Math.abs(o)))return u}throw N.NUM}B("XIRR",2,3,(t,n,s,o)=>{const[e,r]=Et(t,n[0],n[1]),a=K(t,R(n[2],.1),s,o);return m(a>-1&&it(e)),X(Xt(e,r,a))},{array:!0}),B("FVSCHEDULE",2,2,(t,n,s,o)=>{let e=h(dt(t,n[0],s,o));for(const r of q(t,n[1]))e*=1+r;return X(e)},{array:!0}),y("SLN",3,3,(t,n,s)=>{const o=h(s);if(o===0)throw N.DIV0;return(h(t)-h(n))/o}),y("SYD",4,4,(t,n,s,o)=>{const[e,r]=[h(s),h(o)];return m(e>0&&r>0&&r<=e),(h(t)-h(n))*(e-r+1)*2/(e*(e+1))}),y("DB",4,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),f=Math.trunc(h(o)),i=Math.trunc(h(R(e,12)));if(m(r>=0&&a>=0&&c>0&&f>=1&&i>=1&&i<=12),m(f<=(i===12?c:c+1)),r===0)return 0;const u=Math.round((1-(a/r)**(1/c))*1e3)/1e3;let l=r*u*i/12;if(f===1)return l;let M=0;for(let p=2;p<=Math.min(f,c);p++)M=(r-l)*u,l+=M;return f>c?(r-l)*u*(12-i)/12:M});function ut(t,n,s,o,e){let r=e/s,a;r>=1?(r=1,a=o===1?t:0):a=t*(1-r)**(o-1);const c=t*(1-r)**o;return Math.max(a-Math.max(c,n),0)}y("DDB",4,5,(t,n,s,o,e)=>{const[r,a,c,f]=[h(t),h(n),h(s),h(o)],i=h(R(e,2));return m(r>=0&&a>=0&&c>0&&f>0&&f<=c&&i>0),ut(r,a,c,f,i)});function Rt(t,n,s,o,e,r){const a=Math.ceil(e);let c=t-n,f=0,i=!1,u=0;for(let l=1;l<=a;l++){let M=f;if(!i){const p=ut(t,n,s,l,r);f=c/(o-(l-1)),i=f>p,M=i?f:p,i||(c-=p)}u+=l===a?M*(e+1-a):M}return u}y("VDB",5,7,(t,n,s,o,e,r,a)=>{let c=h(t);const f=h(n),i=h(s);let u=h(o),l=h(e);const M=h(R(r,2)),p=mt(R(a,!1));if(m(c>=0&&f>=0&&i>0&&u>=0&&l>=u&&l<=i&&M>=0),p){const E=Math.floor(u),b=Math.ceil(l);let g=0;for(let A=E+1;A<=b;A++){let C=ut(c,f,i,A,M);A===E+1?C*=Math.min(l,E+1)-u:A===b&&(C*=l+1-b),g+=C}return g}let I=i;return u!==Math.floor(u)&&M>1&&u>=i/2&&(l-=u-i/2,u=i/2,I+=1),c-=Rt(c,f,i,I,u,M),Rt(c,f,i,i-u,l-u,M)});function At(t,n,s,o,e,r,a){const c={cost:d(t),bought:D(n),firstEnd:D(s),salvage:d(o),per:Math.trunc(d(e)),rate:d(r),basis:F(a)};return m(c.basis!==2&&c.cost>=0&&c.salvage>=0&&c.salvage<=c.cost),m(c.per>=0&&c.rate>0&&c.bought<=c.firstEnd),c.frac=_(c.bought,c.firstEnd,c.basis),c}y("AMORLINC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=n*e,c=r*a,f=Math.trunc((n-s-c)/a);return o===0?c:o<=f?a:o===f+1?n-s-a*f-c:0}),y("AMORDEGRC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=At(...t),a=1/e,c=(M,p)=>a>=M&&a<=p;m(c(3,4)||c(5,6)||a>6);const f=e*(c(3,4)?1.5:c(5,6)?2:2.5);let i=Math.round(r*f*n),u=n-i,l=u-s;for(let M=0;M<o;M++){if(i=Math.round(f*u),l-=i,l<0)return o-M===1?Math.round(u*.5):0;u-=i}return i}),y("EFFECT",2,2,(t,n)=>{const s=d(t),o=Math.trunc(d(n));return m(s>0&&o>=1),(1+s/o)**o-1}),y("NOMINAL",2,2,(t,n)=>{const s=d(t),o=Math.trunc(d(n));return m(s>0&&o>=1),o*((1+s)**(1/o)-1)});function Ct(t,n,s){const o=d(t),e=Math.trunc(d(n));if(m(e>=0),e===0)throw N.DIV0;const r=Math.trunc(o);return r+s(o-r,e,10**Math.ceil(Math.log10(e)))}y("DOLLARDE",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*e/o)),y("DOLLARFR",2,2,(t,n)=>Ct(t,n,(s,o,e)=>s*o/e));function lt(t,n,s,o){const e=D(t),r=D(n),a=d(s);return m(e<r&&r<=P(e,12,!1)&&a>0),o(a,r-e)}y("TBILLEQ",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>365*o/(360-o*e))),y("TBILLPRICE",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>100*(1-o*e/360))),y("TBILLYIELD",3,3,(t,n,s)=>lt(t,n,s,(o,e)=>(100-o)/o*360/e)),y("ACCRINT",6,8,(t,n,s,o,e,r,a,c)=>{const f=D(t),i=D(n),u=D(s),l=d(o),M=d(R(e,1e3)),p=j(r),I=F(a),E=mt(R(c,!0));m(l>0&&M>0&&f<u);const b=!E&&u>i?i:f,g=12/p,A=Z(i);let C=0;for(;P(i,C*g,A)>b;)C--;for(;P(i,(C+1)*g,A)<=b;)C++;let U=0;for(;P(i,C*g,A)<u;C++){const L=P(i,C*g,A),v=P(i,(C+1)*g,A),T=I===1?v-L:(I===3?365:360)/p;U+=V(Math.max(L,b),Math.min(v,u),I)/T}return M*l*U/p}),y("ACCRINTM",4,5,(t,n,s,o,e)=>{const r=D(t),a=D(n),c=d(s),f=d(R(o,1e3)),i=F(e);return m(c>0&&f>0&&r<a),f*c*_(r,a,i)});const Q=(t,n)=>y(t,3,4,(s,o,e,r)=>n(tt(s,o,e,r,!0)));Q("COUPDAYBS",t=>t.a),Q("COUPDAYS",t=>t.e),Q("COUPDAYSNC",t=>t.dsc),Q("COUPNCD",t=>t.ncd),Q("COUPNUM",t=>t.n),Q("COUPPCD",t=>t.pcd);const W=(t,n)=>y(t,4,5,(s,o,e,r,a)=>{const c=D(s),f=D(o),i=d(e),u=d(r),l=F(a);return m(c<f&&i>0&&u>0),n(i,u,_(c,f,l))});W("DISC",(t,n,s)=>(n-t)/n/s),W("INTRATE",(t,n,s)=>(n-t)/t/s),W("RECEIVED",(t,n,s)=>{const o=1-n*s;return m(o>0),t/o}),W("PRICEDISC",(t,n,s)=>n*(1-t*s)),W("YIELDDISC",(t,n,s)=>(n/t-1)/s);const gt=(t,n,s)=>y(t,5,6,(o,e,r,a,c,f)=>{const i=D(o),u=D(e),l=D(r),M=d(a),p=d(c),I=F(f);return m(i<u&&l<i&&M>=0&&n(p)),s(M,p,_(l,u,I),_(l,i,I),_(i,u,I))});gt("PRICEMAT",t=>t>=0,(t,n,s,o,e)=>(100+s*t*100)/(1+e*n)-o*t*100),gt("YIELDMAT",t=>t>0,(t,n,s,o,e)=>{const r=n/100+o*t;return(1+s*t-r)/r/e});function Pt(t,n,s,o){const e=100*n/t.f,r=t.dsc/t.e;if(t.n===1)return(o+e)/(1+s/t.f*r)-e*t.a/t.e;const a=1+s/t.f;let c=o/a**(t.n-1+r);for(let f=0;f<t.n;f++)c+=e/a**(f+r);return c-e*t.a/t.e}y("PRICE",6,7,(t,n,s,o,e,r,a)=>{const c=d(s),f=d(o),i=d(e),u=tt(t,n,r,a);return m(c>=0&&f>=0&&i>0),Pt(u,c,f,i)}),y("YIELD",6,7,(t,n,s,o,e,r,a)=>{const c=d(s),f=d(o),i=d(e),u=tt(t,n,r,a);if(m(c>=0&&f>0&&i>0),u.n===1){const l=c/u.f,M=f/100+u.a/u.e*l;return(i/100+l-M)/M*(u.f*u.e/u.dsc)}return nt(l=>Pt(u,c,l,i)-f,.1,-u.f)});function Lt(t,n,s,o,e,r){const a=d(s),c=d(o),f=tt(t,n,e,r);m(a>=0&&c>=0);const i=1+c/f.f,u=100*a/f.f,l=f.dsc/f.e;let M=0,p=0;for(let I=0;I<f.n;I++){const E=I+l,b=(I===f.n-1?u+100:u)/i**E;M+=E*b,p+=b}return[M/p/f.f,i]}y("DURATION",5,6,(...t)=>Lt(...t)[0]),y("MDURATION",5,6,(...t)=>{const[n,s]=Lt(...t);return n/s});function Tt(t,n,s,o,e,r,a,c){const f=D(t),i=D(n),u=D(s),l=D(o),M=d(e),p=d(r),I=j(a),E=F(c);m(i>l&&l>f&&f>u&&M>=0&&p>0);const b=at(f,l,I,E),g=b.n===1?V(f,l,E):b.dsc,A=at(l,i,I,E).n,C=Z(l),U=12/I;let L=0,v=0;for(let k=0,w=l;;k++){const O=P(l,-(k+1)*U,C),S=E===1?w-O:b.e;L+=(O>u?S:V(u,w,E))/S;const ht=Math.max(u,O),Mt=Math.min(f,w);if(Mt>ht&&(v+=V(ht,Mt,E)/S),O<=u)break;w=O}const T=100*M/I,Y=b.n-1+g/b.e;return k=>{const w=1+k/I;let O=p/w**(A+Y)+T*L/w**Y;for(let S=1;S<=A;S++)O+=T/w**(S+Y);return O-T*v}}y("ODDFPRICE",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=d(r),u=Tt(t,n,s,o,e,a,c,f);return m(i>=0),u(i)}),y("ODDFYIELD",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=d(r),u=Tt(t,n,s,o,e,a,c,f);return m(i>0),nt(l=>u(l)-i,.1,-j(c))});function wt(t,n,s,o,e,r,a){const c=D(t),f=D(n),i=D(s),u=d(o),l=d(e),M=j(r),p=F(a);m(f>c&&c>i&&u>=0&&l>0);const I=12/M,E=Z(i);let b=0,g=0,A=0;for(let U=0;;U++){const L=P(i,U*I,E),v=P(i,(U+1)*I,E),T=p===0||p===4?360/M:v-L,Y=v>=f,k=Y?V(L,f,p):T;b+=k/T,v<c?g+=k/T:L<c&&(g+=V(L,c,p)/T);const w=Math.max(c,L),O=Math.min(f,v);if(O>w&&(A+=V(w,O,p)/T),Y)break}const C=100*u/M;return{income:l+b*C,interest:g*C,toGo:A,f:M}}y("ODDLPRICE",7,8,(t,n,s,o,e,r,a,c)=>{const f=d(e),i=wt(t,n,s,o,r,a,c);return m(f>=0),i.income/(1+i.toGo*f/i.f)-i.interest}),y("ODDLYIELD",7,8,(t,n,s,o,e,r,a,c)=>{const f=d(e),i=wt(t,n,s,o,r,a,c);m(f>0);const u=f+i.interest;return(i.income-u)/u*(i.f/i.toGo)});export{$ as days360,_ as yearFrac};
@@ -1 +1 @@
1
- import{Arr as a,bool as A,compareText as R,ERR as h,num as b,OMITTED as l,Ref as y,RefList as m,RichValue as x,str as L,textToNumber as w,XlError as c}from"../values.js";function d(n,r,o){if(r instanceof y)n.scan(r,o);else if(r instanceof m)for(const t of r.refs)n.scan(t,o);else if(r instanceof a)for(const t of r.data)o(t);else r!==l&&r!==void 0&&o(r)}function T(n,r){const o=[];for(const t of r)t instanceof y||t instanceof m||t instanceof a?d(n,t,s=>{if(typeof s=="number")o.push(s);else{if(s instanceof c)throw s;if(s instanceof x)throw h.VALUE}}):t!==l&&t!==void 0&&t!==null?o.push(b(t)):(t===l||t===null)&&o.push(0);return o}function U(n,r){const o=[];for(const t of r)if(t instanceof y||t instanceof m||t instanceof a){const s=!(t instanceof a);d(n,t,f=>{if(typeof f=="number")o.push(f);else if(typeof f=="boolean"&&s)o.push(f?1:0);else if(typeof f=="string")o.push(0);else{if(f instanceof c)throw f;if(f instanceof x)throw h.VALUE}})}else t!==l&&t!==void 0&&t!==null&&o.push(b(t));return o}function g(n,r){return n.toArr(r).data}function C(n,r,o){const t=g(n,r),s=g(n,o);if(t.length!==s.length)throw h.NA;const f=[],u=[];for(let e=0;e<t.length;e++){if(t[e]instanceof c)throw t[e];if(s[e]instanceof c)throw s[e];typeof t[e]=="number"&&typeof s[e]=="number"&&(f.push(t[e]),u.push(s[e]))}return[f,u]}function S(n,r,o,t){const s=n.scalar(r,o,t);if(s instanceof c)throw s;return s}function E(n){let r="^";for(let o=0;o<n.length;o++){const t=n[o];t==="~"&&(n[o+1]==="*"||n[o+1]==="?"||n[o+1]==="~")?r+=n[++o].replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):t==="*"?r+="[\\s\\S]*":t==="?"?r+="[\\s\\S]":r+=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}return new RegExp(`${r}$`,"i")}const $=n=>/[*?~]/.test(n);function V(n){if(n instanceof c)return e=>e===n;if(typeof n=="number")return e=>e===n||typeof e=="string"&&w(e)===n;if(typeof n=="boolean")return e=>e===n;if(n==null||n===l)return e=>e===0||e===null;const r=String(n),o=/^(<=|>=|<>|<|>|=)([\s\S]*)$/.exec(r),t=o?o[1]:"=",s=o?o[2]:r,f=w(s);if(f!==null)switch(t){case"=":return e=>e===f||typeof e=="string"&&w(e)===f;case"<>":return e=>!(e===f||typeof e=="string"&&w(e)===f);case"<":return e=>typeof e=="number"&&e<f;case"<=":return e=>typeof e=="number"&&e<=f;case">":return e=>typeof e=="number"&&e>f;default:return e=>typeof e=="number"&&e>=f}const u=s.toUpperCase();if(u==="TRUE"||u==="FALSE"){const e=u==="TRUE";return t==="<>"?i=>i!==e:i=>i===e}if(t==="="||t==="<>"){if(s==="")return t==="="?i=>i===null:i=>i!==null;if($(s)){const i=E(s);return t==="="?p=>typeof p=="string"&&i.test(p):p=>!(typeof p=="string"&&i.test(p))}const e=s.toLowerCase();return t==="="?i=>typeof i=="string"&&i.toLowerCase()===e:i=>!(typeof i=="string"&&i.toLowerCase()===e)}return e=>{if(typeof e!="string")return!1;const i=R(e,s);return t==="<"?i<0:t==="<="?i<=0:t===">"?i>0:i>=0}}function O(n,r){const o=n.toArr(r),t=[];for(let s=0;s<o.rows;s++){const f=[];for(let u=0;u<o.cols;u++){const e=o.get(s,u);if(typeof e!="number")throw e instanceof c?e:h.VALUE;f.push(e)}t.push(f)}return t}export{a as Arr,h as ERR,l as OMITTED,y as Ref,m as RefList,c as XlError,A as bool,T as collectNumbers,U as collectNumbersA,d as eachValue,g as flat,$ as hasWildcards,V as makeCriteria,O as matrixOf,b as num,C as pairedNumbers,S as scalarOf,L as str,E as wildcardRegex};
1
+ import{Arr as h,bool as A,compareText as L,ERR as l,num as b,OMITTED as a,Ref as y,RefList as m,RichValue as x,str as R,textToNumber as w,XlError as c}from"../values.js";function d(n,o,r){if(o instanceof y)n.scan(o,r);else if(o instanceof m)for(const t of o.refs)n.scan(t,r);else if(o instanceof h)for(const t of o.data)r(t);else o!==a&&o!==void 0&&r(o)}function N(n,o){const r=[];for(const t of o)t instanceof y||t instanceof m||t instanceof h?d(n,t,s=>{if(typeof s=="number")r.push(s);else{if(s instanceof c)throw s;if(s instanceof x)throw l.VALUE}}):t!==a&&t!==void 0&&t!==null?r.push(b(t)):(t===a||t===null)&&r.push(0);return r}function S(n,o){const r=[];for(const t of o)if(t instanceof y||t instanceof m||t instanceof h){const s=!(t instanceof h);d(n,t,f=>{if(typeof f=="number")r.push(f);else if(typeof f=="boolean"&&s)r.push(f?1:0);else if(typeof f=="string")r.push(0);else{if(f instanceof c)throw f;if(f instanceof x)throw l.VALUE}})}else t!==a&&t!==void 0&&t!==null&&r.push(b(t));return r}function g(n,o){return n.toArr(o).data}function T(n,o,r){const t=g(n,o),s=g(n,r);if(t.length!==s.length)throw l.NA;const f=[],u=[];for(let e=0;e<t.length;e++){if(t[e]instanceof c)throw t[e];if(s[e]instanceof c)throw s[e];typeof t[e]=="number"&&typeof s[e]=="number"&&(f.push(t[e]),u.push(s[e]))}return[f,u]}function U(n,o,r,t){const s=n.scalar(o,r,t);if(s instanceof c)throw s;return s}function C(n,o,r,t){if(o instanceof m||!n.legacy&&o instanceof y&&(o.r0!==o.r1||o.c0!==o.c1))throw l.VALUE;const s=U(n,o,r,t);if(typeof s=="boolean")throw l.VALUE;return s}function E(n){let o="^";for(let r=0;r<n.length;r++){const t=n[r];t==="~"&&(n[r+1]==="*"||n[r+1]==="?"||n[r+1]==="~")?o+=n[++r].replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):t==="*"?o+="[\\s\\S]*":t==="?"?o+="[\\s\\S]":o+=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}return new RegExp(`${o}$`,"i")}const V=n=>/[*?~]/.test(n);function O(n){if(n instanceof c)return e=>e===n;if(typeof n=="number")return e=>e===n||typeof e=="string"&&w(e)===n;if(typeof n=="boolean")return e=>e===n;if(n==null||n===a)return e=>e===0||e===null;const o=String(n),r=/^(<=|>=|<>|<|>|=)([\s\S]*)$/.exec(o),t=r?r[1]:"=",s=r?r[2]:o,f=w(s);if(f!==null)switch(t){case"=":return e=>e===f||typeof e=="string"&&w(e)===f;case"<>":return e=>!(e===f||typeof e=="string"&&w(e)===f);case"<":return e=>typeof e=="number"&&e<f;case"<=":return e=>typeof e=="number"&&e<=f;case">":return e=>typeof e=="number"&&e>f;default:return e=>typeof e=="number"&&e>=f}const u=s.toUpperCase();if(u==="TRUE"||u==="FALSE"){const e=u==="TRUE";return t==="<>"?i=>i!==e:i=>i===e}if(t==="="||t==="<>"){if(s==="")return t==="="?i=>i===null:i=>i!==null;if(V(s)){const i=E(s);return t==="="?p=>typeof p=="string"&&i.test(p):p=>!(typeof p=="string"&&i.test(p))}const e=s.toLowerCase();return t==="="?i=>typeof i=="string"&&i.toLowerCase()===e:i=>!(typeof i=="string"&&i.toLowerCase()===e)}return e=>{if(typeof e!="string")return!1;const i=L(e,s);return t==="<"?i<0:t==="<="?i<=0:t===">"?i>0:i>=0}}function k(n,o){const r=n.toArr(o),t=[];for(let s=0;s<r.rows;s++){const f=[];for(let u=0;u<r.cols;u++){const e=r.get(s,u);if(typeof e!="number")throw e instanceof c?e:l.VALUE;f.push(e)}t.push(f)}return t}export{h as Arr,l as ERR,a as OMITTED,y as Ref,m as RefList,c as XlError,A as bool,N as collectNumbers,S as collectNumbersA,d as eachValue,g as flat,V as hasWildcards,O as makeCriteria,k as matrixOf,b as num,T as pairedNumbers,U as scalarOf,R as str,C as toolpakScalar,E as wildcardRegex};
@@ -1 +1,7 @@
1
- import"./math.js";import"./logical.js";import"./lookup.js";import"./array.js";import"./text.js";import"./date.js";import"./info.js";import"./stat.js";import"./financial.js";import"./engineering.js";import"./database.js";import"../../celltypes/barcode.js";import"../../calcx/functions/index.js";import"../../sparklines/functions.js";
1
+ import"./math.js";import"./logical.js";import"./lookup.js";import"./array.js";import"./text.js";import"./date.js";import"./info.js";import"./stat.js";import"./financial.js";import"./engineering.js";import"./database.js";import"../../celltypes/barcode.js";import"../../calcx/functions/index.js";import"../../sparklines/functions.js";import{FUNCS as C}from"./registry.js";const D=`ACCRINT ACCRINTM AMORDEGRC AMORLINC BESSELI BESSELJ BESSELK BESSELY BIN2DEC BIN2HEX BIN2OCT COMPLEX
2
+ CONVERT COUPDAYBS COUPDAYS COUPDAYSNC COUPNCD COUPNUM COUPPCD CUMIPMT CUMPRINC DEC2BIN DEC2HEX DEC2OCT DELTA DISC
3
+ DOLLARDE DOLLARFR DURATION EDATE EFFECT EOMONTH ERF ERF.PRECISE ERFC ERFC.PRECISE FACTDOUBLE GESTEP HEX2BIN HEX2DEC
4
+ HEX2OCT IMABS IMAGINARY IMARGUMENT IMCONJUGATE IMCOS IMCOSH IMCOT IMCSC IMCSCH IMDIV IMEXP IMLN IMLOG10 IMLOG2
5
+ IMPOWER IMREAL IMSEC IMSECH IMSIN IMSINH IMSQRT IMSUB IMTAN INTRATE ISEVEN ISODD MDURATION MROUND NOMINAL OCT2BIN
6
+ OCT2DEC OCT2HEX PRICE PRICEDISC PRICEMAT QUOTIENT RECEIVED SQRTPI TBILLEQ TBILLPRICE TBILLYIELD WEEKNUM YEARFRAC YIELD
7
+ YIELDDISC YIELDMAT ODDFPRICE ODDFYIELD ODDLPRICE ODDLYIELD`.split(/\s+/),O={ACCRINT:[7]};for(const E of D){const I=C.get(E);I?.kind==="lift"&&(I.toolpak={logical:O[E]??[]})}
@@ -1 +1 @@
1
- import{setCategory as I}from"./registry.js";import{lift as m,raw as f}from"./registry.js";import{Arr as R,ERR as o,Lambda as S,num as y,OMITTED as A,Ref as i,RefList as p,RichValue as L,XlError as E}from"../values.js";import{CONTENT_MASK as w,FORMULA as g}from"../../core/sheet.js";import{cellRef as b,colName as T,sheetPrefix as O}from"../../core/address.js";import{newBookName as k}from"../../core/default-names.js";import{str as d}from"../values.js";I("Information");const s=(e,t)=>m(e,1,1,t,{errors:!0});s("ISERROR",e=>e instanceof E),s("ISERR",e=>e instanceof E&&e!==o.NA),s("ISNA",e=>e===o.NA),s("ISNUMBER",e=>typeof e=="number"),s("ISTEXT",e=>typeof e=="string"),s("ISNONTEXT",e=>typeof e!="string"),s("ISLOGICAL",e=>typeof e=="boolean"),s("ISBLANK",e=>e==null),m("ISEVEN",1,1,e=>Math.trunc(y(e))%2===0),m("ISODD",1,1,e=>Math.trunc(y(e))%2!==0),f("ISREF",1,1,(e,t)=>t[0]instanceof i||t[0]instanceof p),f("ISFORMULA",1,1,(e,t)=>{const r=t[0];if(!(r instanceof i))throw o.VALUE;return(r.sheet.kind(r.r0,r.c0)&g)!==0}),m("N",1,1,e=>{if(typeof e=="number")return e;if(typeof e=="boolean")return e?1:0;if(e instanceof L)throw o.VALUE;return 0}),m("NA",0,0,()=>o.NA),s("ERROR.TYPE",e=>{if(!(e instanceof E))throw o.NA;return{"#NULL!":1,"#DIV/0!":2,"#VALUE!":3,"#REF!":4,"#NAME?":5,"#NUM!":6,"#N/A":7,"#GETTING_DATA":8,"#SPILL!":9,"#FIELD!":13,"#CALC!":14}[e.code]}),f("TYPE",1,1,(e,t)=>{const r=t[0]instanceof i&&t[0].isCell?e.read(t[0].sheet,t[0].r0,t[0].c0):t[0];return r instanceof i||r instanceof p||r instanceof R?64:r===null||typeof r=="number"?1:typeof r=="string"?2:typeof r=="boolean"?4:r instanceof E?16:r instanceof S||r instanceof L?128:1}),f("SHEET",0,1,(e,t,r,u,c)=>{if(t.length===0||t[0]===A)return e.book.sheets.indexOf(c)+1;if(t[0]instanceof i)return e.book.sheets.indexOf(t[0].sheet)+1;if(typeof t[0]=="string"){const h=e.sheetByName(t[0]);if(!h)throw o.NA;return e.book.sheets.indexOf(h)+1}throw o.VALUE}),f("SHEETS",0,1,(e,t)=>{if(t.length===0||t[0]===A)return e.book.sheets.length;if(t[0]instanceof p)return new Set(t[0].refs.map(r=>r.sheet)).size;if(t[0]instanceof i)return 1;throw o.REF}),f("INFO",1,1,(e,t,r,u)=>{switch(d(e.scalar(t[0],r,u)).toLowerCase()){case"numfile":return e.book.sheets.length;case"recalc":return e.manual?"Manual":"Automatic";case"release":return"16.0";case"system":return"mac";case"origin":return"$A:$A$1";case"directory":return"";default:throw o.VALUE}}),f("CELL",1,2,(e,t,r,u,c)=>{const h=d(e.scalar(t[0],r,u)).toLowerCase(),n=t[1]===void 0||t[1]===A?new i(c,r,u,r,u):t[1];if(!(n instanceof i))throw o.VALUE;const a=e.read(n.sheet,n.r0,n.c0),l=e.book.styles.get(n.sheet.style(n.r0,n.c0));switch(h){case"address":{const N=`$${T(n.c0)}$${n.r0+1}`;return n.sheet===c?N:`${O(n.sheet.name,{book:e.book.fileName??k()})}${N}`}case"col":return n.c0+1;case"row":return n.r0+1;case"contents":return a===null?0:a;case"filename":return"";case"type":return a===null?"b":typeof a=="string"?"l":"v";case"width":return Math.round(n.sheet.cols.sizeOf(n.c0)/6);case"color":return/\[red\]/i.test(l.numFmt)?1:0;case"format":return F(l.numFmt);case"parentheses":return/\(/.test(l.numFmt)?1:0;case"prefix":return typeof a=="string"?l.hAlign==="right"?'"':l.hAlign==="center"?"^":"'":"";case"protect":return 1;default:throw o.VALUE}});function F(e){return e==="General"?"G":e==="0"?"F0":e==="#,##0"?",0":e==="0.00"?"F2":e==="#,##0.00"?",2":/%/.test(e)?`P${(/\.(0+)/.exec(e)?.[1]??"").length}`:/E\+/i.test(e)?"S2":/[ymd]/i.test(e)?"D1":"G"}export{w as CONTENT_MASK,b as cellRef};
1
+ import{setCategory as S}from"./registry.js";import{lift as h,raw as f}from"./registry.js";import{Arr as I,ERR as o,Lambda as R,num as N,OMITTED as A,Ref as i,RefList as d,RichValue as y,XlError as E}from"../values.js";import{CONTENT_MASK as w,FORMULA as g}from"../../core/sheet.js";import{cellRef as b,colName as T,sheetPrefix as O}from"../../core/address.js";import{newBookName as k}from"../../core/default-names.js";import{str as L}from"../values.js";S("Information");const s=(e,t)=>h(e,1,1,t,{errors:!0});s("ISERROR",e=>e instanceof E),s("ISERR",e=>e instanceof E&&e!==o.NA),s("ISNA",e=>e===o.NA),s("ISNUMBER",e=>typeof e=="number"),s("ISTEXT",e=>typeof e=="string"),s("ISNONTEXT",e=>typeof e!="string"),s("ISLOGICAL",e=>typeof e=="boolean"),s("ISBLANK",e=>e==null),h("ISEVEN",1,1,e=>Math.trunc(N(e))%2===0),h("ISODD",1,1,e=>Math.trunc(N(e))%2!==0),f("ISREF",1,1,(e,t)=>t[0]instanceof i||t[0]instanceof d),f("ISFORMULA",1,1,(e,t)=>{const r=t[0];if(!(r instanceof i))throw o.VALUE;return(r.sheet.kind(r.r0,r.c0)&g)!==0}),h("N",1,1,e=>{if(typeof e=="number")return e;if(typeof e=="boolean")return e?1:0;if(e instanceof y)throw o.VALUE;return 0}),h("NA",0,0,()=>o.NA),s("ERROR.TYPE",e=>{if(!(e instanceof E))throw o.NA;return{"#NULL!":1,"#DIV/0!":2,"#VALUE!":3,"#REF!":4,"#NAME?":5,"#NUM!":6,"#N/A":7,"#GETTING_DATA":8,"#SPILL!":9,"#FIELD!":13,"#CALC!":14}[e.code]}),f("TYPE",1,1,(e,t)=>{const r=t[0]instanceof i&&t[0].isCell?e.read(t[0].sheet,t[0].r0,t[0].c0):t[0];return r instanceof i||r instanceof d||r instanceof I?64:r===null||typeof r=="number"?1:typeof r=="string"?2:typeof r=="boolean"?4:r instanceof E?16:r instanceof R||r instanceof y?128:1}),f("SHEET",0,1,(e,t,r,u,c)=>{if(t.length===0||t[0]===A)return e.book.sheets.indexOf(c)+1;if(t[0]instanceof i)return e.book.sheets.indexOf(t[0].sheet)+1;if(typeof t[0]=="string"){const m=e.sheetByName(t[0]);if(!m)throw o.NA;return e.book.sheets.indexOf(m)+1}throw o.VALUE},{readsSheets:!0}),f("SHEETS",0,1,(e,t)=>{if(t.length===0||t[0]===A)return e.book.sheets.length;if(t[0]instanceof d)return new Set(t[0].refs.map(r=>r.sheet)).size;if(t[0]instanceof i)return 1;throw o.REF},{readsSheets:!0}),f("INFO",1,1,(e,t,r,u)=>{switch(L(e.scalar(t[0],r,u)).toLowerCase()){case"numfile":return e.book.sheets.length;case"recalc":return e.manual?"Manual":"Automatic";case"release":return"16.0";case"system":return"mac";case"origin":return"$A:$A$1";case"directory":return"";default:throw o.VALUE}},{readsSheets:!0}),f("CELL",1,2,(e,t,r,u,c)=>{const m=L(e.scalar(t[0],r,u)).toLowerCase(),n=t[1]===void 0||t[1]===A?new i(c,r,u,r,u):t[1];if(!(n instanceof i))throw o.VALUE;const a=e.read(n.sheet,n.r0,n.c0),l=e.book.styles.get(n.sheet.style(n.r0,n.c0));switch(m){case"address":{const p=`$${T(n.c0)}$${n.r0+1}`;return n.sheet===c?p:`${O(n.sheet.name,{book:e.book.fileName??k()})}${p}`}case"col":return n.c0+1;case"row":return n.r0+1;case"contents":return a===null?0:a;case"filename":return"";case"type":return a===null?"b":typeof a=="string"?"l":"v";case"width":return Math.round(n.sheet.cols.sizeOf(n.c0)/6);case"color":return/\[red\]/i.test(l.numFmt)?1:0;case"format":return F(l.numFmt);case"parentheses":return/\(/.test(l.numFmt)?1:0;case"prefix":return typeof a=="string"?l.hAlign==="right"?'"':l.hAlign==="center"?"^":"'":"";case"protect":return 1;default:throw o.VALUE}});function F(e){return e==="General"?"G":e==="0"?"F0":e==="#,##0"?",0":e==="0.00"?"F2":e==="#,##0.00"?",2":/%/.test(e)?`P${(/\.(0+)/.exec(e)?.[1]??"").length}`:/E\+/i.test(e)?"S2":/[ymd]/i.test(e)?"D1":"G"}export{w as CONTENT_MASK,b as cellRef};
@@ -1 +1 @@
1
- import{setCategory as D}from"./registry.js";import{lazy as N,lift as U,raw as h}from"./registry.js";import{Arr as l,bool as T,ERR as d,Lambda as B,OMITTED as R,Ref as b,RefList as F,XlError as m}from"../values.js";import{compareValues as Y}from"../values.js";import{eachValue as W}from"./helpers.js";D("Logical");for(const n of["LET","LAMBDA"])N(n,0,255,()=>{throw new Error(`${n} compiles directly`)});U("TRUE",0,0,()=>!0),U("FALSE",0,0,()=>!1),U("NOT",1,1,n=>!T(n));function k(n,t,r){let o=!1;for(const s of t)if(s instanceof b||s instanceof F||s instanceof l)W(n,s,e=>{if(e instanceof m)throw e;(typeof e=="number"||typeof e=="boolean")&&(o=!0,r(typeof e=="number"?e!==0:e))});else{if(s instanceof m)throw s;typeof s=="string"&&!/^(TRUE|FALSE)$/i.test(s)||(s!==R&&s!==null?(o=!0,r(T(s))):s===R&&(o=!0,r(!1)))}if(!o)throw d.VALUE}h("AND",1,255,(n,t)=>{let r=!0;return k(n,t,o=>{o||(r=!1)}),r},{array:!0}),h("OR",1,255,(n,t)=>{let r=!1;return k(n,t,o=>{o&&(r=!0)}),r},{array:!0}),h("XOR",1,255,(n,t)=>{let r=!1;return k(n,t,o=>{o&&(r=!r)}),r},{array:!0});const p=(n,t,r,o)=>t instanceof b?t.isCell?n.read(t.sheet,t.r0,t.c0):n.refAuto(t,r,o):t instanceof F?d.VALUE:t;function V(n){if(n instanceof m)throw n;return T(n)}function X(n,t,r,o,s,e,f){const a=r?r(s,e,f):!0,i=o?o(s,e,f):!1,c=p(n,a,s,e),u=p(n,i,s,e),M=Math.max(t.rows,c instanceof l?c.rows:1,u instanceof l?u.rows:1),L=Math.max(t.cols,c instanceof l?c.cols:1,u instanceof l?u.cols:1),g=(A,y,O)=>{if(!(A instanceof l))return A;const w=A.rows===1?0:y,S=A.cols===1?0:O;return w<A.rows&&S<A.cols?A.get(w,S):d.NA},C=new Array(M*L);for(let A=0;A<M;A++)for(let y=0;y<L;y++){const O=g(t,A,y);let w;if(O instanceof m)w=O;else try{w=T(O)?g(c,A,y):g(u,A,y)}catch(S){w=S}C[A*L+y]=w===R?0:w}return new l(M,L,C)}N("IF",2,3,(n,t,r,o,s)=>{const e=p(n,t[0](r,o,s),r,o);if(e instanceof l)return X(n,e,t[1],t[2],r,o,s);if(V(e)){const a=t[1](r,o,s);return a===R?0:a}if(t.length<3)return!1;const f=t[2](r,o,s);return f===R?0:f},{passes:n=>n>0}),N("IFS",2,254,(n,t,r,o,s)=>{if(t.length%2!==0)throw d.NA;for(let e=0;e<t.length;e+=2){const f=p(n,t[e](r,o,s),r,o),a=f instanceof l?f.data[0]:f;if(V(a))return t[e+1](r,o,s)}throw d.NA},{passes:n=>n%2===1}),N("IFERROR",2,2,(n,t,r,o,s)=>{let e=p(n,t[0](r,o,s),r,o);if(e instanceof l){const f=p(n,t[1](r,o,s),r,o);return new l(e.rows,e.cols,e.data.map(a=>a instanceof m?f instanceof l?f.data[0]:f:a))}return e instanceof m&&(e=t[1](r,o,s)),e===R?"":e},{passes:()=>!0}),N("IFNA",2,2,(n,t,r,o,s)=>{const e=p(n,t[0](r,o,s),r,o);if(e instanceof l){const f=p(n,t[1](r,o,s),r,o);return new l(e.rows,e.cols,e.data.map(a=>a===d.NA?f instanceof l?f.data[0]:f:a))}return e===d.NA?t[1](r,o,s):e},{passes:()=>!0}),N("SWITCH",3,255,(n,t,r,o,s)=>{const e=i=>{const c=p(n,i(r,o,s),r,o);return c instanceof l?c.data[0]:c},f=e(t[0]);if(f instanceof m)throw f;let a=1;for(;a+1<t.length;a+=2){const i=e(t[a]);if(i instanceof m)throw i;if(typeof i==typeof f&&Y(i,f)===0)return t[a+1](r,o,s)}if(a<t.length)return t[a](r,o,s);throw d.NA},{passes:(n,t)=>n>1&&(n%2===0||n===t-1)});const I=n=>{if(n instanceof m)throw n;if(!(n instanceof B))throw d.VALUE;return n},E=(n,t,r,o)=>{const s=t instanceof b?n.refAuto(t,r,o):t;return s instanceof l?s.data[0]:s};h("MAP",2,255,(n,t,r,o,s)=>{const e=I(t[t.length-1]),f=t.slice(0,-1).map(u=>n.toArr(u)),{rows:a,cols:i}=f[0],c=new Array(a*i);for(let u=0;u<c.length;u++){const M=n.invoke(e,f.map(L=>L.data[u]??d.NA),r,o,s);c[u]=E(n,M,r,o)}return new l(a,i,c)}),h("REDUCE",2,3,(n,t,r,o,s)=>{const e=t.length===3,f=I(t[t.length-1]),a=n.toArr(t[e?1:0]).data;let i=e?t[0]:a[0];for(let c=e?0:1;c<a.length;c++)i=n.invoke(f,[i,a[c]],r,o,s);return i}),h("SCAN",2,3,(n,t,r,o,s)=>{const e=t.length===3,f=I(t[t.length-1]),a=n.toArr(t[e?1:0]);let i=e?E(n,t[0],r,o):a.data[0];const c=new Array(a.data.length);for(let u=0;u<a.data.length;u++){if(u===0&&!e){c[0]=i;continue}i=E(n,n.invoke(f,[i,a.data[u]],r,o,s),r,o),c[u]=i}return new l(a.rows,a.cols,c)}),h("BYROW",2,2,(n,t,r,o,s)=>{const e=n.toArr(t[0]),f=I(t[1]),a=new Array(e.rows);for(let i=0;i<e.rows;i++){const c=new l(1,e.cols,e.data.slice(i*e.cols,(i+1)*e.cols));a[i]=E(n,n.invoke(f,[c],r,o,s),r,o)}return new l(e.rows,1,a)}),h("BYCOL",2,2,(n,t,r,o,s)=>{const e=n.toArr(t[0]),f=I(t[1]),a=new Array(e.cols);for(let i=0;i<e.cols;i++){const c=new Array(e.rows);for(let u=0;u<e.rows;u++)c[u]=e.get(u,i);a[i]=E(n,n.invoke(f,[new l(e.rows,1,c)],r,o,s),r,o)}return new l(1,e.cols,a)}),h("MAKEARRAY",3,3,(n,t,r,o,s)=>{const e=Math.trunc(E(n,t[0],r,o)),f=Math.trunc(E(n,t[1],r,o)),a=I(t[2]);if(!(e>=1)||!(f>=1))throw d.VALUE;const i=new Array(e*f);for(let c=0;c<e;c++)for(let u=0;u<f;u++)i[c*f+u]=E(n,n.invoke(a,[c+1,u+1],r,o,s),r,o);return new l(e,f,i)}),h("ISOMITTED",1,1,(n,t)=>t[0]===void 0||t[0]===R);
1
+ import{setCategory as B}from"./registry.js";import{lazy as N,lift as b,raw as m}from"./registry.js";import{Arr as l,bool as T,ERR as d,isOmitted as V,Lambda as Y,OMITTED as R,Ref as k,RefList as g,XlError as p}from"../values.js";import{compareValues as W}from"../values.js";import{eachValue as X}from"./helpers.js";B("Logical");for(const n of["LET","LAMBDA"])N(n,0,255,()=>{throw new Error(`${n} compiles directly`)});b("TRUE",0,0,()=>!0),b("FALSE",0,0,()=>!1),b("NOT",1,1,n=>!T(n));function C(n,t,r){let o=!1;for(const s of t)if(s instanceof k||s instanceof g||s instanceof l)X(n,s,e=>{if(e instanceof p)throw e;(typeof e=="number"||typeof e=="boolean")&&(o=!0,r(typeof e=="number"?e!==0:e))});else{if(s instanceof p)throw s;typeof s=="string"&&!/^(TRUE|FALSE)$/i.test(s)||(s!==R&&s!==null?(o=!0,r(T(s))):s===R&&(o=!0,r(!1)))}if(!o)throw d.VALUE}m("AND",1,255,(n,t)=>{let r=!0;return C(n,t,o=>{o||(r=!1)}),r},{array:!0}),m("OR",1,255,(n,t)=>{let r=!1;return C(n,t,o=>{o&&(r=!0)}),r},{array:!0}),m("XOR",1,255,(n,t)=>{let r=!1;return C(n,t,o=>{o&&(r=!r)}),r},{array:!0});const y=(n,t,r,o)=>t instanceof k?t.isCell?n.read(t.sheet,t.r0,t.c0):n.refAuto(t,r,o):t instanceof g?d.VALUE:t;function D(n){if(n instanceof p)throw n;return T(n)}function $(n,t,r,o,s,e,f){const a=r?r(s,e,f):!0,i=o?o(s,e,f):!1,c=y(n,a,s,e),u=y(n,i,s,e),O=Math.max(t.rows,c instanceof l?c.rows:1,u instanceof l?u.rows:1),L=Math.max(t.cols,c instanceof l?c.cols:1,u instanceof l?u.cols:1),U=(A,h,M)=>{if(!(A instanceof l))return A;const w=A.rows===1?0:h,S=A.cols===1?0:M;return w<A.rows&&S<A.cols?A.get(w,S):d.NA},F=new Array(O*L);for(let A=0;A<O;A++)for(let h=0;h<L;h++){const M=U(t,A,h);let w;if(M instanceof p)w=M;else try{w=T(M)?U(c,A,h):U(u,A,h)}catch(S){w=S}F[A*L+h]=w===R?0:w}return new l(O,L,F)}N("IF",2,3,(n,t,r,o,s)=>{const e=y(n,t[0](r,o,s),r,o);if(e instanceof l)return $(n,e,t[1],t[2],r,o,s);if(D(e)){const a=t[1](r,o,s);return a===R?0:a}if(t.length<3)return!1;const f=t[2](r,o,s);return f===R?0:f},{passes:n=>n>0}),N("IFS",2,254,(n,t,r,o,s)=>{if(t.length%2!==0)throw d.NA;for(let e=0;e<t.length;e+=2){const f=y(n,t[e](r,o,s),r,o),a=f instanceof l?f.data[0]:f;if(D(a))return t[e+1](r,o,s)}throw d.NA},{passes:n=>n%2===1}),N("IFERROR",2,2,(n,t,r,o,s)=>{let e=y(n,t[0](r,o,s),r,o);if(e instanceof l){const f=y(n,t[1](r,o,s),r,o);return new l(e.rows,e.cols,e.data.map(a=>a instanceof p?f instanceof l?f.data[0]:f:a))}return e instanceof p&&(e=t[1](r,o,s)),e===R?"":e},{passes:()=>!0}),N("IFNA",2,2,(n,t,r,o,s)=>{const e=y(n,t[0](r,o,s),r,o);if(e instanceof l){const f=y(n,t[1](r,o,s),r,o);return new l(e.rows,e.cols,e.data.map(a=>a===d.NA?f instanceof l?f.data[0]:f:a))}return e===d.NA?t[1](r,o,s):e},{passes:()=>!0}),N("SWITCH",3,255,(n,t,r,o,s)=>{const e=i=>{const c=y(n,i(r,o,s),r,o);return c instanceof l?c.data[0]:c},f=e(t[0]);if(f instanceof p)throw f;let a=1;for(;a+1<t.length;a+=2){const i=e(t[a]);if(i instanceof p)throw i;if(typeof i==typeof f&&W(i,f)===0)return t[a+1](r,o,s)}if(a<t.length)return t[a](r,o,s);throw d.NA},{passes:(n,t)=>n>1&&(n%2===0||n===t-1)});const I=n=>{if(n instanceof p)throw n;if(!(n instanceof Y))throw d.VALUE;return n},E=(n,t,r,o)=>{const s=t instanceof k?n.refAuto(t,r,o):t;return s instanceof l?s.data[0]:s};m("MAP",2,255,(n,t,r,o,s)=>{const e=I(t[t.length-1]),f=t.slice(0,-1).map(u=>n.toArr(u)),{rows:a,cols:i}=f[0],c=new Array(a*i);for(let u=0;u<c.length;u++){const O=n.invoke(e,f.map(L=>L.data[u]??d.NA),r,o,s);c[u]=E(n,O,r,o)}return new l(a,i,c)}),m("REDUCE",3,3,(n,t,r,o,s)=>{const e=!V(t[0]),f=I(t[2]),a=n.toArr(t[1]).data;let i=e?t[0]:a[0];for(let c=e?0:1;c<a.length;c++)i=n.invoke(f,[i,a[c]],r,o,s);return i}),m("SCAN",3,3,(n,t,r,o,s)=>{const e=!V(t[0]),f=I(t[2]),a=n.toArr(t[1]);let i=e?E(n,t[0],r,o):a.data[0];const c=new Array(a.data.length);for(let u=0;u<a.data.length;u++){if(u===0&&!e){c[0]=i;continue}i=E(n,n.invoke(f,[i,a.data[u]],r,o,s),r,o),c[u]=i}return new l(a.rows,a.cols,c)}),m("BYROW",2,2,(n,t,r,o,s)=>{const e=n.toArr(t[0]),f=I(t[1]),a=new Array(e.rows);for(let i=0;i<e.rows;i++){const c=new l(1,e.cols,e.data.slice(i*e.cols,(i+1)*e.cols));a[i]=E(n,n.invoke(f,[c],r,o,s),r,o)}return new l(e.rows,1,a)}),m("BYCOL",2,2,(n,t,r,o,s)=>{const e=n.toArr(t[0]),f=I(t[1]),a=new Array(e.cols);for(let i=0;i<e.cols;i++){const c=new Array(e.rows);for(let u=0;u<e.rows;u++)c[u]=e.get(u,i);a[i]=E(n,n.invoke(f,[new l(e.rows,1,c)],r,o,s),r,o)}return new l(1,e.cols,a)}),m("MAKEARRAY",3,3,(n,t,r,o,s)=>{const e=Math.trunc(E(n,t[0],r,o)),f=Math.trunc(E(n,t[1],r,o)),a=I(t[2]);if(!(e>=1)||!(f>=1))throw d.VALUE;const i=new Array(e*f);for(let c=0;c<e;c++)for(let u=0;u<f;u++)i[c*f+u]=E(n,n.invoke(a,[c+1,u+1],r,o,s),r,o);return new l(e,f,i)}),m("ISOMITTED",1,1,(n,t)=>t[0]===void 0||t[0]===R);