@8sheets/data-grid 1.1.2 → 1.2.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 (226) hide show
  1. package/README.md +1 -1
  2. package/a11y/cell-alt-text.js +1 -1
  3. package/api/serialize.js +1 -1
  4. package/api/style-refs.js +1 -1
  5. package/calc/adjust.js +1 -1
  6. package/calc/compile.js +1 -1
  7. package/calc/engine.js +1 -1
  8. package/calc/functions/array.js +1 -1
  9. package/calc/functions/database.js +1 -1
  10. package/calc/functions/date.js +1 -1
  11. package/calc/functions/engineering.js +1 -1
  12. package/calc/functions/financial.js +1 -1
  13. package/calc/functions/helpers.js +1 -1
  14. package/calc/functions/index.js +3 -2
  15. package/calc/functions/info.js +1 -1
  16. package/calc/functions/logical.js +1 -1
  17. package/calc/functions/lookup.js +1 -1
  18. package/calc/functions/math.js +1 -1
  19. package/calc/functions/stat.js +1 -1
  20. package/calc/functions/text.js +1 -1
  21. package/calc/parser.js +2 -2
  22. package/calc/shown-refs.js +1 -0
  23. package/calc/values.js +1 -1
  24. package/calcx/calc-control.js +1 -0
  25. package/calcx/external-read.js +1 -1
  26. package/calcx/external.js +1 -1
  27. package/cells/axis-ops.js +1 -1
  28. package/cells/cell-styles-xml.js +1 -1
  29. package/cells/cf-engine.js +1 -1
  30. package/cells/cf-file.js +1 -1
  31. package/cells/docprops-xml.js +2 -2
  32. package/cells/filter-eval.js +1 -0
  33. package/cells/filter-file.js +1 -1
  34. package/cells/find-engine.js +1 -1
  35. package/cells/goto.js +1 -1
  36. package/cells/name-box.js +1 -1
  37. package/cells/note-box.js +3 -0
  38. package/cells/options.js +1 -1
  39. package/cells/outline-column.js +1 -1
  40. package/cells/protected-menu.js +1 -0
  41. package/cells/rich-data-xml.js +1 -1
  42. package/cells/sheet-views-file.js +2 -0
  43. package/cells/shift-check.js +1 -1
  44. package/cells/sort-compare.js +1 -0
  45. package/cells/ui-kit.js +1 -1
  46. package/cells/undo-group.js +1 -1
  47. package/celltypes/dropdowns.js +2 -2
  48. package/core/address.js +1 -1
  49. package/core/autofill.js +1 -1
  50. package/core/axis.js +1 -1
  51. package/core/cond-format.js +1 -1
  52. package/core/culture.js +5 -1
  53. package/core/custom-views.js +1 -1
  54. package/core/data-tools.js +1 -0
  55. package/core/date-codes.js +1 -0
  56. package/core/default-names.js +1 -1
  57. package/core/formula-refs.js +1 -1
  58. package/core/input.js +1 -1
  59. package/core/numfmt.js +1 -1
  60. package/core/precision.js +1 -0
  61. package/core/region-input.js +1 -1
  62. package/core/rule-refs.js +1 -0
  63. package/core/shared-view.js +1 -0
  64. package/core/sheet.js +1 -1
  65. package/core/sparklines.js +1 -1
  66. package/core/sparse.js +1 -1
  67. package/core/spills.js +1 -0
  68. package/core/strings.js +1 -1
  69. package/core/structure.js +1 -1
  70. package/core/table-columns.js +1 -0
  71. package/core/tables.js +2 -2
  72. package/core/validation-ranges.js +1 -1
  73. package/core/whatif.js +1 -1
  74. package/core/workbook.js +1 -1
  75. package/datasheets/formula.js +1 -1
  76. package/datasheets/path-binding.js +1 -1
  77. package/datasheets/persist-writer.js +1 -1
  78. package/datasheets/report.js +1 -1
  79. package/extras/getdata/persist.js +2 -2
  80. package/extras/getdata/rows.js +1 -1
  81. package/extras/getdata/shared-steps.js +1 -0
  82. package/extras/getdata/steps-formula.js +1 -0
  83. package/extras/getdata/steps-more.js +1 -0
  84. package/extras/getdata/steps-tables.js +1 -0
  85. package/extras/getdata/steps.js +1 -1
  86. package/i18n/de/a11y.js +1 -0
  87. package/i18n/de/cells.js +2 -0
  88. package/i18n/de/charts.js +1 -0
  89. package/i18n/de/common.js +1 -0
  90. package/i18n/de/datasheets.js +1 -0
  91. package/i18n/de/format.js +1 -0
  92. package/i18n/de/misc.js +1 -0
  93. package/i18n/de/pivot.js +1 -0
  94. package/i18n/de/proofing.js +1 -0
  95. package/i18n/de/ribbon.js +1 -0
  96. package/i18n/de/shapes.js +1 -0
  97. package/i18n/de/shell.js +3 -0
  98. package/i18n/de/touch.js +1 -0
  99. package/i18n/de.js +1 -0
  100. package/i18n/en/cells.js +2 -1
  101. package/i18n/en/charts.js +1 -1
  102. package/i18n/en/format.js +1 -1
  103. package/i18n/en/misc.js +1 -1
  104. package/i18n/en/pivot.js +1 -1
  105. package/i18n/en/proofing.js +1 -1
  106. package/i18n/en/ribbon.js +1 -1
  107. package/i18n/en/shapes.js +1 -1
  108. package/i18n/en/shell.js +3 -2
  109. package/i18n/en/touch.js +1 -1
  110. package/i18n/es/a11y.js +1 -0
  111. package/i18n/es/cells.js +2 -0
  112. package/i18n/es/charts.js +1 -0
  113. package/i18n/es/common.js +1 -0
  114. package/i18n/es/datasheets.js +1 -0
  115. package/i18n/es/format.js +1 -0
  116. package/i18n/es/misc.js +1 -0
  117. package/i18n/es/pivot.js +1 -0
  118. package/i18n/es/proofing.js +1 -0
  119. package/i18n/es/ribbon.js +1 -0
  120. package/i18n/es/shapes.js +1 -0
  121. package/i18n/es/shell.js +3 -0
  122. package/i18n/es/touch.js +1 -0
  123. package/i18n/es.js +1 -0
  124. package/i18n/fr/a11y.js +1 -0
  125. package/i18n/fr/cells.js +2 -0
  126. package/i18n/fr/charts.js +1 -0
  127. package/i18n/fr/common.js +1 -0
  128. package/i18n/fr/datasheets.js +1 -0
  129. package/i18n/fr/format.js +1 -0
  130. package/i18n/fr/misc.js +1 -0
  131. package/i18n/fr/pivot.js +1 -0
  132. package/i18n/fr/proofing.js +1 -0
  133. package/i18n/fr/ribbon.js +1 -0
  134. package/i18n/fr/shapes.js +1 -0
  135. package/i18n/fr/shell.js +3 -0
  136. package/i18n/fr/touch.js +1 -0
  137. package/i18n/fr.js +1 -0
  138. package/i18n/index.js +1 -1
  139. package/i18n/languages.js +1 -1
  140. package/i18n/pl/cells.js +2 -1
  141. package/i18n/pl/charts.js +1 -1
  142. package/i18n/pl/format.js +1 -1
  143. package/i18n/pl/misc.js +1 -1
  144. package/i18n/pl/pivot.js +1 -1
  145. package/i18n/pl/proofing.js +1 -1
  146. package/i18n/pl/ribbon.js +1 -1
  147. package/i18n/pl/shapes.js +1 -1
  148. package/i18n/pl/shell.js +3 -2
  149. package/i18n/pl/touch.js +1 -1
  150. package/io/blob-sink.js +1 -0
  151. package/io/canonical-xml.js +1 -0
  152. package/io/client.js +1 -1
  153. package/io/crypto/cfb-build.js +1 -0
  154. package/io/crypto/cfb-read.js +1 -1
  155. package/io/crypto/cfb-write.js +1 -1
  156. package/io/crypto/errors.js +1 -1
  157. package/io/csv.js +8 -8
  158. package/io/foreign-sheets.js +2 -2
  159. package/io/io.worker.js +1 -1
  160. package/io/open-package.js +1 -0
  161. package/io/package-paths.js +1 -1
  162. package/io/plan.js +1 -1
  163. package/io/text-split.js +3 -3
  164. package/io/xls/chart-sheet.js +1 -0
  165. package/io/xls/chart.js +1 -0
  166. package/io/xls/crypt.js +1 -0
  167. package/io/xls/dml-color.js +1 -0
  168. package/io/xls/drawing.js +1 -0
  169. package/io/xls/formula.js +1 -0
  170. package/io/xls/geometry.js +1 -0
  171. package/io/xls/index.js +1 -0
  172. package/io/xls/pictures.js +1 -0
  173. package/io/xls/pivot.js +1 -0
  174. package/io/xls/props.js +1 -0
  175. package/io/xls/records.js +1 -0
  176. package/io/xls/shapes.js +1 -0
  177. package/io/xls/sheet-ext.js +1 -0
  178. package/io/xls/sheet.js +1 -0
  179. package/io/xls/styles.js +1 -0
  180. package/io/xls/vba.js +1 -0
  181. package/io/xls/workbook.js +1 -0
  182. package/io/xlsb/filter.js +1 -0
  183. package/io/xlsb/formula.js +2 -0
  184. package/io/xlsb/functions.js +1 -0
  185. package/io/xlsb/index.js +1 -0
  186. package/io/xlsb/parts.js +1 -0
  187. package/io/xlsb/pivot.js +1 -0
  188. package/io/xlsb/records.js +1 -0
  189. package/io/xlsb/sheet-ext.js +1 -0
  190. package/io/xlsb/sheet.js +1 -0
  191. package/io/xlsb/slicer.js +1 -0
  192. package/io/xlsb/styles.js +1 -0
  193. package/io/xlsb/workbook.js +1 -0
  194. package/io/xlsb/xml.js +2 -0
  195. package/io/xlsx-read.js +1 -1
  196. package/io/xlsx-styles.js +2 -2
  197. package/io/xlsx-tail.js +1 -1
  198. package/io/xlsx-write.js +2 -2
  199. package/io/xml-escape.js +1 -1
  200. package/io/xml-scan.js +2 -2
  201. package/io/zip-reader.js +1 -1
  202. package/objects/chart-model.js +1 -1
  203. package/objects/drawing-xml.js +1 -1
  204. package/package.json +1 -1
  205. package/pivot/calc-items.js +1 -0
  206. package/pivot/engine.js +1 -1
  207. package/pivot/xml.js +2 -2
  208. package/print/note-pages.js +2 -2
  209. package/print/page-setup.js +1 -1
  210. package/print/paginate.js +1 -1
  211. package/print/pdf-crypt.js +1 -0
  212. package/print/pdf-tags.js +1 -0
  213. package/print/pdf.js +24 -13
  214. package/print/render-page.js +3 -3
  215. package/print/shown-notes.js +1 -0
  216. package/shapes/form-xml.js +1 -1
  217. package/slicers/custom-styles.js +1 -1
  218. package/slicers/format.js +1 -1
  219. package/slicers/structured.js +1 -1
  220. package/slicers/write.js +1 -1
  221. package/table-view/files.js +1 -1
  222. package/table-view/mount.js +1 -1
  223. package/table-view/version.js +1 -1
  224. package/ui/icons.js +1 -1
  225. package/ui/menu-base.js +1 -1
  226. package/ui/protection-ui.js +1 -1
@@ -1 +1 @@
1
- import{setCategory as Nt}from"./registry.js";import{lift as f,raw as gt}from"./registry.js";import{checked as T,ERR as u,num as dt,numberToText as wt,OMITTED as P,opt as O,str as R,XlError as yt}from"../values.js";import{eachValue as Tt}from"./helpers.js";Nt("Engineering");const M=t=>{if(typeof t=="boolean")throw u.VALUE;return dt(t)},B=t=>Number(t.toPrecision(15)),$={2:10,8:30,16:40},Rt={2:/^[01]*$/,8:/^[0-7]*$/,16:/^[0-9a-fA-F]*$/};function St(t,e){if(typeof t=="boolean")throw u.VALUE;const n=R(t);if(n.length>10||!Rt[e].test(n))throw u.NUM;if(n==="")return 0;const o=parseInt(n,e);return o>=2**($[e]-1)?o-2**$[e]:o}function Lt(t,e,n){const o=n===void 0||n===P?null:Math.trunc(M(n)),r=$[e];if(t<-(2**(r-1))||t>=2**(r-1))throw u.NUM;if(t<0)return(t+2**r).toString(e).toUpperCase();const s=t.toString(e).toUpperCase();if(o===null)return s;if(o<1||o>10||s.length>o)throw u.NUM;return s.padStart(o,"0")}const Q=[["BIN",2],["OCT",8],["DEC",10],["HEX",16]];for(const[t,e]of Q){const n=e===10?o=>Math.trunc(M(o)):o=>St(o,e);for(const[o,r]of Q)t!==o&&(r===10?f(`${t}2DEC`,1,1,n):f(`${t}2${o}`,1,2,(s,c)=>Lt(n(s),r,c)))}const v=2**48-1,q=t=>{const e=M(t);if(!Number.isInteger(e)||e<0||e>v)throw u.NUM;return e},z=(t,e)=>f(t,2,2,(n,o)=>{const r=q(n),s=q(o);return Number(e(BigInt(r),BigInt(s)))});z("BITAND",(t,e)=>t&e),z("BITOR",(t,e)=>t|e),z("BITXOR",(t,e)=>t^e);function x(t,e,n){const o=q(t),r=M(e);if(!Number.isInteger(r)||Math.abs(r)>53)throw u.NUM;const s=r*n;if(s<=0)return Math.floor(o/2**-s);const c=o*2**s;if(c>v)throw u.NUM;return c}f("BITLSHIFT",2,2,(t,e)=>x(t,e,1)),f("BITRSHIFT",2,2,(t,e)=>x(t,e,-1)),f("DELTA",1,2,(t,e)=>M(t)===M(O(e,0))?1:0),f("GESTEP",1,2,(t,e)=>M(t)>=M(O(e,0))?1:0);function tt(t){let e=t;for(let n=200;n>=1;n--)e=t+n/2/e;return Math.exp(-t*t)/(Math.sqrt(Math.PI)*e)}function S(t){const e=Math.abs(t);if(e>=6)return Math.sign(t);if(e>=2.5)return Math.sign(t)*(1-tt(e));let n=e,o=e;for(let r=1;n>o*1e-17;r++)n*=2*e*e/(2*r+1),o+=n;return Math.sign(t)*2*Math.exp(-e*e)*o/Math.sqrt(Math.PI)}function _(t){return t<0?2-_(-t):t<1?1-S(t):t>=27?0:tt(t)}f("ERF",1,2,(t,e)=>{const n=S(M(t));return e===void 0||e===P?n:S(M(e))-n}),f("ERF.PRECISE",1,1,t=>S(M(t))),f("ERFC",1,1,t=>_(M(t))),f("ERFC.PRECISE",1,1,t=>_(M(t)));const Ut=40,et=1e10,N=1e-10,nt=2**27,Ct=709.7,At=22250738585072014e-324,k=t=>Math.abs(t)<At?0:t;function F(t){const e=Math.abs(t);if(e<3.75){const o=t/3.75,r=o*o;return 1+r*(3.5156229+r*(3.0899424+r*(1.2067492+r*(.2659732+r*(.0360768+r*.0045813)))))}const n=3.75/e;return Math.exp(e)/Math.sqrt(e)*(.39894228+n*(.01328592+n*(.00225319+n*(-.00157565+n*(.00916281+n*(-.02057706+n*(.02635537+n*(-.01647633+n*.00392377))))))))}function ot(t){const e=Math.abs(t);let n;if(e<3.75){const o=t/3.75,r=o*o;n=e*(.5+r*(.87890594+r*(.51498869+r*(.15084934+r*(.02658733+r*(.00301532+r*32411e-8))))))}else{const o=3.75/e,r=.02282967+o*(-.02895312+o*(.01787654-o*.00420059));n=.39894228+o*(-.03988024+o*(-.00362018+o*(.00163801+o*(-.01031555+o*r)))),n*=Math.exp(e)/Math.sqrt(e)}return t<0?-n:n}function rt(t){if(t<=2){const n=t*t/4;return-Math.log(t/2)*F(t)+(-.57721566+n*(.4227842+n*(.23069756+n*(.0348859+n*(.00262698+n*(1075e-7+n*74e-7))))))}const e=2/t;return k(Math.exp(-t)/Math.sqrt(t))*(1.25331414+e*(-.07832358+e*(.02189568+e*(-.01062446+e*(.00587872+e*(-.0025154+e*53208e-8))))))}function st(t){if(t<=2){const n=t*t/4;return Math.log(t/2)*ot(t)+1/t*(1+n*(.15443144+n*(-.67278579+n*(-.18156897+n*(-.01919402+n*(-.00110404+n*-4686e-8))))))}const e=2/t;return k(Math.exp(-t)/Math.sqrt(t))*(1.25331414+e*(.23498619+e*(-.0365562+e*(.01504268+e*(-.00780353+e*(.00325614+e*-68245e-8))))))}function H(t){const e=Math.abs(t);if(e<8){const i=t*t;return(57568490574+i*(-13362590354+i*(6516196407e-1+i*(-1121442418e-2+i*(77392.33017+i*-184.9052456)))))/(57568490411+i*(1029532985+i*(9494680718e-3+i*(59272.64853+i*(267.8532712+i*1)))))}const n=8/e,o=n*n,r=e-.785398164,s=1+o*(-.001098628267+o*(2734510407e-14+o*(-2073370639e-15+o*2093887211e-16))),c=-.01562499995+o*(.0001430488765+o*(-6911147651e-15+o*(7621095161e-16-o*934935152e-16)));return Math.sqrt(.636619772/e)*(Math.cos(r)*s-n*Math.sin(r)*c)}function G(t){const e=Math.abs(t);if(e<8){const a=t*t;return t*(72362614232+a*(-7895059235+a*(2423968531e-1+a*(-2972611439e-3+a*(15704.4826+a*-30.16036606)))))/(144725228442+a*(2300535178+a*(1858330474e-2+a*(99447.43394+a*(376.9991397+a*1)))))}const n=8/e,o=n*n,r=e-2.356194491,s=1+o*(.00183105+o*(-3516396496e-14+o*(-3516396496e-14+o*(2457520174e-15+o*-240337019e-15)))),c=.04687499995+o*(-.0002002690873+o*(8449199096e-15+o*(-88228987e-14+o*105787412e-15))),i=Math.sqrt(.636619772/e)*(Math.cos(r)*s-n*Math.sin(r)*c);return t<0?-i:i}function ct(t){if(t<8){const c=t*t;return(-2957821389+c*(7062834065+c*(-5123598036e-1+c*(1087988129e-2+c*(-86327.92757+c*228.4622733)))))/(40076544269+c*(7452499648e-1+c*(7189466438e-3+c*(47447.2647+c*(226.1030244+c*1)))))+.636619772*H(t)*Math.log(t)}const e=8/t,n=e*e,o=t-.785398164,r=1+n*(-.001098628627+n*(2734510407e-14+n*(-2073370639e-15+n*2093887211e-16))),s=-.01562499995+n*(.0001430488765+n*(-6911147651e-15+n*(7621095161e-16+n*-934945152e-16)));return Math.sqrt(.636619772/t)*(Math.sin(o)*r+e*Math.cos(o)*s)}function it(t){if(t<8){const c=t*t;return t*(-4900604943e3+c*(127527439e4+c*(-51534381390+c*(7349264551e-1+c*(-4237922726e-3+c*8511.937935)))))/(249958057e5+c*(424441966400+c*(3733650367+c*(2245904002e-2+c*(102042.605+c*(354.9632885+c))))))+.636619772*(G(t)*Math.log(t)-1/t)}const e=8/t,n=e*e,o=t-2.356194491,r=1+n*(.00183105+n*(-3516396496e-14+n*(2457520174e-15+n*-240337019e-15))),s=.04687499995+n*(-.0002002690873+n*(8449199096e-15+n*(-88228987e-14+n*105787412e-15)));return Math.sqrt(.636619772/t)*(Math.sin(o)*r+e*Math.cos(o)*s)}const at=t=>t+Math.trunc(Math.sqrt(Ut*t)),ut=(t,e)=>e>1e6&&2*t<e;function bt(t,e){if(Math.abs(t)>Ct)throw u.NUM;if(e===0)return F(t);if(e===1)return ot(t);if(t===0||ut(Math.abs(t),e))return 0;const n=2/Math.abs(t);let o=0,r=1,s=0;for(let c=2*at(e);c>0;c--){const i=o+c*n*r;o=r,r=i,Math.abs(r)>et&&(s*=N,r*=N,o*=N),c===e&&(s=o)}return s*=F(t)/r,t<0&&e%2===1?-s:s}function Pt(t,e){if(Math.abs(t)>=nt)throw u.NUM;if(e===0)return H(t);if(e===1)return G(t);const n=Math.abs(t);if(n===0||ut(n,e))return 0;const o=2/n;let r;if(n>e){let s=H(n),c=G(n);for(let i=1;i<e;i++){const a=i*o*c-s;s=c,c=a}r=c}else{let s=!1,c=0,i=0,a=1;r=0;for(let g=2*Math.trunc(at(e)/2);g>0;g--){const b=g*o*a-i;i=a,a=b,Math.abs(a)>et&&(a*=N,i*=N,r*=N,c*=N),s&&(c+=a),s=!s,g===e&&(r=i)}r/=2*c-a}return t<0&&e%2===1?-r:r}function Ot(t,e){if(t<=0||t>=nt)throw u.NUM;if(e===0)return ct(t);if(e===1)return it(t);const n=2/t;let o=ct(t),r=it(t);for(let s=1;s<e&&Number.isFinite(r);s++){const c=s*n*r-o;o=r,r=c}return r}function Bt(t,e){if(t<=0)throw u.NUM;if(e===0)return rt(t);if(e===1)return st(t);const n=2/t;let o=rt(t),r=st(t);for(let s=1;s<e&&Number.isFinite(r);s++){if(o===0&&r===0)return 0;const c=o+k(s*n*r);o=r,r=c}return r}const L=(t,e)=>f(t,2,2,(n,o)=>{const r=M(n),s=Math.trunc(M(o));if(s<0)throw u.NUM;return T(e(r,s))});L("BESSELI",bt),L("BESSELJ",Pt),L("BESSELK",Bt),L("BESSELY",Ot);const V=String.raw`(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?`,$t=new RegExp(`^[+-]?${V}$`),qt=new RegExp(`^(?:([+-]?${V})(?=[+-]))?([+-]?)(${V})?([ij])$`);function I(t){if(t instanceof yt)throw t;if(typeof t=="boolean")throw u.VALUE;if(t==null||t===P)return{z:[0,0],suffix:null};if(typeof t=="number")return{z:[t,0],suffix:null};const e=R(t);let n=0,o=0,r=null;if($t.test(e))n=Number(e);else{const s=qt.exec(e);if(s===null)throw u.NUM;s[1]!==void 0&&(n=Number(s[1])),o=(s[2]==="-"?-1:1)*(s[3]===void 0?1:Number(s[3])),[,,,,r]=s}if(!Number.isFinite(n)||!Number.isFinite(o))throw u.NUM;return{z:[n,o],suffix:r}}function D(t){const e=wt(t),n=/^(-?)(\d)(?:\.(\d+))?E-(\d+)$/.exec(e);if(!n)return e;const o=`0.${"0".repeat(Number(n[4])-1)}${n[2]}${n[3]??""}`;return o.length<=21?n[1]+o:e}function d([t,e],n){const o=B(T(t)),r=B(T(e));if(r===0)return D(o);const s=r===1?"":r===-1?"-":D(r);return o===0?`${s}${n}`:`${D(o)}${r>0?"+":""}${s}${n}`}function Y(t,e){if(t!==null&&e!==null&&t!==e)throw u.VALUE;return t??e}const zt=([t,e],[n,o])=>[t+n,e+o],_t=([t,e],[n,o])=>[t*n-e*o,t*o+e*n];function kt([t,e],[n,o]){const r=n*n+o*o;if(r===0)throw u.NUM;return[(t*n+e*o)/r,(e*n-t*o)/r]}const m=t=>(Math.exp(t)-Math.exp(-t))/2,p=t=>(Math.exp(t)+Math.exp(-t))/2,Ft=([t,e])=>[Math.sin(t)*p(e),Math.cos(t)*m(e)],Ht=([t,e])=>[Math.cos(t)*p(e),-Math.sin(t)*m(e)],Gt=([t,e])=>{const n=Math.sin(e);return[m(t)*Math.cos(e),(Math.exp(t)*n+Math.exp(-t)*n)/2]},Vt=([t,e])=>[p(t)*Math.cos(e),m(t)*Math.sin(e)];function j([t,e]){if(t===0&&e===0)throw u.NUM;return[Math.log(Math.hypot(t,e)),Math.atan2(e,t)]}const Dt=[1,0];f("COMPLEX",2,3,(t,e,n)=>{const o=M(t),r=M(e),s=O(n,"i");if(s!=="i"&&s!=="j")throw u.VALUE;return d([o,r],s)});const U=(t,e)=>f(t,1,1,n=>T(e(I(n).z)));U("IMABS",([t,e])=>Math.hypot(t,e)),U("IMAGINARY",([,t])=>t),U("IMREAL",([t])=>t),U("IMARGUMENT",([t,e])=>{if(t===0&&e===0)throw u.DIV0;return Math.atan2(e,t)});const h=(t,e)=>f(t,1,1,n=>{const{z:o,suffix:r}=I(n);return d(e(o),r??"i")});h("IMCONJUGATE",([t,e])=>[t,-e]),h("IMCOS",Ht),h("IMCOSH",Vt),h("IMCOT",([t,e])=>{const n=p(2*e)-Math.cos(2*t);return[Math.sin(2*t)/n,-m(2*e)/n]}),h("IMCSC",([t,e])=>{const n=(p(2*e)-Math.cos(2*t))/2;return[Math.sin(t)*(p(e)/n),-Math.cos(t)*(m(e)/n)]}),h("IMCSCH",([t,e])=>{const n=Math.sin(e),o=m(t),r=o*o+n*n;return[o*Math.cos(e)/r,-p(t)*n/r]}),h("IMEXP",([t,e])=>[Math.exp(t)*Math.cos(e),Math.exp(t)*Math.sin(e)]),h("IMLN",j),h("IMLOG10",t=>j(t).map(e=>e/Math.LN10)),h("IMLOG2",t=>j(t).map(e=>e/Math.LN2)),h("IMSEC",([t,e])=>{const n=(p(2*e)+Math.cos(2*t))/2;return[Math.cos(t)*p(e)/n,Math.sin(t)*m(e)/n]}),h("IMSECH",([t,e])=>{const n=Math.cos(e),o=m(t),r=o*o+n*n;return[p(t)*n/r,-o*Math.sin(e)/r]}),h("IMSIN",Ft),h("IMSINH",Gt),h("IMSQRT",([t,e])=>{const n=Math.sqrt(Math.hypot(t,e)),o=Math.atan2(e,t)/2;return[n*Math.cos(o),n*Math.sin(o)]}),h("IMTAN",([t,e])=>{const n=Math.cos(2*t)+p(2*e);return[Math.sin(2*t)/n,m(2*e)/n]}),f("IMDIV",2,2,(t,e)=>{const n=I(t),o=I(e);return d(kt(n.z,o.z),Y(n.suffix,o.suffix)??"i")}),f("IMSUB",2,2,(t,e)=>{const n=I(t),o=I(e);return d([n.z[0]-o.z[0],n.z[1]-o.z[1]],Y(n.suffix,o.suffix)??"i")}),f("IMPOWER",2,2,(t,e)=>{const{z:[n,o],suffix:r}=I(t),s=M(e);if(n===0&&o===0){if(s<=0)throw u.NUM;return"0"}const c=Math.hypot(n,o)**s,i=Math.atan2(o,n)*s;return d([c*Math.cos(i),c*Math.sin(i)],r??"i")});const ft=(t,e,n,o)=>gt(t,1,255,(r,s)=>{let c=e,i=null,a=!1;for(const g of s)Tt(r,g,b=>{const Z=I(b);i=Y(i,Z.suffix),c=n(c,Z.z),a=!0});return a?d(c,i??"i"):o});ft("IMSUM",[0,0],zt,"0"),ft("IMPRODUCT",Dt,_t,"0");const X=new Map;function l(t,e){for(const[n,o,r=1]of e)for(const s of o.split(" ")){const c=s.endsWith("*");X.set(c?s.slice(0,-1):s,{group:t,factor:n,prefix:c,power:r})}}const w=.0254,y=.3048,J=.9144,C=1609.344,A=1852,K=9460730472580800,W=w/72,E=231*w**3;l("mass",[[1,"g*"],[14593.9029372064,"sg"],[453.59237,"lbm"],[1660538782e-33,"u*"],[28.349523125,"ozm"],[.06479891,"grain"],[45359.237,"cwt shweight"],[50802.34544,"uk_cwt lcwt hweight"],[6350.29318,"stone"],[907184.74,"ton"],[10160469088e-4,"uk_ton LTON brton"]]),l("distance",[[1,"m*"],[C,"mi"],[A,"Nmi"],[w,"in"],[y,"ft"],[J,"yd"],[1e-10,"ang*"],[1.143,"ell"],[K,"ly*"],[0x6da012f93c9a8c,"parsec pc"],[W,"Picapt Pica"],[w/6,"pica"],[5280*1200/3937,"survey_mi"]]),l("time",[[31557600,"yr"],[86400,"day d"],[3600,"hr"],[60,"mn min"],[1,"sec* s*"]]),l("pressure",[[1,"Pa* p*"],[101325,"atm* at*"],[133.322,"mmHg*"],[6894.75729316836,"psi*"],[101325/760,"Torr*"]]),l("force",[[1,"N*"],[1e-5,"dyn* dy*"],[4.4482216152605,"lbf"],[.00980665,"pond*"]]),l("energy",[[1,"J*"],[1e-7,"e*"],[4.184,"c*"],[4.1868,"cal*"],[1602176487e-28,"eV* ev*"],[268451953769617e-8,"HPh hh"],[3600,"Wh* wh*"],[1.3558179483314,"flb"],[1055.05585262,"BTU btu"]]),l("power",[[745.69987158227,"HP h"],[735.49875,"PS"],[1,"W* w*"]]),l("magnetism",[[1,"T*"],[1e-4,"ga*"]]),l("volume",[[E/768,"tsp"],[5e-6,"tspm"],[E/256,"tbs"],[E/128,"oz"],[E/16,"cup"],[E/8,"pt us_pt"],[.00056826125,"uk_pt"],[E/4,"qt"],[.0011365225,"uk_qt"],[E,"gal"],[.00454609,"uk_gal"],[.001,"l* L* lt*"],[1e-30,"ang3* ang^3*",3],[42*E,"barrel"],[.03523907016688,"bushel"],[y**3,"ft3 ft^3"],[w**3,"in3 in^3"],[K**3,"ly3 ly^3"],[1,"m3* m^3*",3],[C**3,"mi3 mi^3"],[J**3,"yd3 yd^3"],[A**3,"Nmi3 Nmi^3"],[W**3,"Picapt3 Picapt^3 Pica3 Pica^3"],[100*y**3,"GRT regton"],[40*y**3,"MTON"]]),l("area",[[4046.8564224,"uk_acre"],[43560*(1200/3937)**2,"us_acre"],[1e-20,"ang2* ang^2*",2],[100,"ar*"],[y**2,"ft2 ft^2"],[1e4,"ha"],[w**2,"in2 in^2"],[K**2,"ly2 ly^2"],[1,"m2* m^2*",2],[2500,"Morgen"],[C**2,"mi2 mi^2"],[A**2,"Nmi2 Nmi^2"],[W**2,"Picapt2 Picapt^2 Pica2 Pica^2"],[J**2,"yd2 yd^2"]]),l("information",[[1,"bit*"],[8,"byte*"]]),l("speed",[[6080*y/3600,"admkn"],[A/3600,"kn"],[1/3600,"m/h* m/hr*"],[1,"m/s* m/sec*"],[C/3600,"mph"]]);const Yt=new Map([["Y",1e24],["Z",1e21],["E",1e18],["P",1e15],["T",1e12],["G",1e9],["M",1e6],["k",1e3],["h",100],["da",10],["e",10],["d",.1],["c",.01],["m",.001],["u",1e-6],["n",1e-9],["p",1e-12],["f",1e-15],["a",1e-18],["z",1e-21],["y",1e-24]]),Mt=new Map([["Yi",2**80],["Zi",2**70],["Ei",2**60],["Pi",2**50],["Ti",2**40],["Gi",2**30],["Mi",2**20],["ki",2**10]]),ht=[1,0],lt=[1.8,32],pt=[1,273.15],mt=new Map([["C",ht],["cel",ht],["F",lt],["fah",lt],["K",pt],["kel",pt],["Rank",[1.8,491.67]],["Reau",[.8,0]]]),It=t=>Math.abs(t)<1e-12?0:t;function jt(t,[e,n],[o,r]){const s=o/e;return It(t*s+It(r-n*s))}function Et(t){if(mt.has(t))return{group:"temperature",temperature:mt.get(t)};const e=X.get(t);if(e!==void 0)return e;for(const n of[2,1]){const o=X.get(t.slice(n));if(o===void 0||!o.prefix)continue;const r=t.slice(0,n),s=Yt.get(r);if(s!==void 0)return{group:o.group,factor:o.factor*s**o.power};if(o.group==="information"&&Mt.has(r))return{group:o.group,factor:o.factor*Mt.get(r)}}return null}f("CONVERT",3,3,(t,e,n)=>{const o=M(t),r=R(e),s=R(n),c=Et(r),i=Et(s);if(c===null||i===null||c.group!==i.group)throw u.NA;if(r===s)return o;const a=c.group==="temperature"?jt(o,c.temperature,i.temperature):o*c.factor/i.factor;return B(T(a))});
1
+ import{setCategory as gt}from"./registry.js";import{lift as f,raw as dt}from"./registry.js";import{checked as T,ERR as u,num as b,numberToText as wt,OMITTED as O,opt as B,str as R,XlError as yt}from"../values.js";import{eachValue as Tt}from"./helpers.js";gt("Engineering");const M=t=>{if(typeof t=="boolean")throw u.VALUE;return b(t)},$=t=>Number(t.toPrecision(15)),q={2:10,8:30,16:40},Rt={2:/^[01]*$/,8:/^[0-7]*$/,16:/^[0-9a-fA-F]*$/};function St(t,e){if(typeof t=="boolean")throw u.VALUE;const n=R(t);if(n.length>10||!Rt[e].test(n))throw u.NUM;if(n==="")return 0;const o=parseInt(n,e);return o>=2**(q[e]-1)?o-2**q[e]:o}function Lt(t,e,n){const o=n===void 0||n===O?null:Math.trunc(M(n)),r=q[e];if(t<-(2**(r-1))||t>=2**(r-1))throw u.NUM;if(t<0)return(t+2**r).toString(e).toUpperCase();const s=t.toString(e).toUpperCase();if(o===null)return s;if(o<1||o>10||s.length>o)throw u.NUM;return s.padStart(o,"0")}const v=[["BIN",2],["OCT",8],["DEC",10],["HEX",16]];for(const[t,e]of v){const n=e===10?o=>Math.trunc(M(o)):o=>St(o,e);for(const[o,r]of v)t!==o&&(r===10?f(`${t}2DEC`,1,1,n):f(`${t}2${o}`,1,2,(s,c)=>Lt(n(s),r,c)))}const x=2**48-1,z=t=>{const e=b(t);if(!Number.isInteger(e)||e<0||e>x)throw u.NUM;return e},_=(t,e)=>f(t,2,2,(n,o)=>{const r=z(n),s=z(o);return Number(e(BigInt(r),BigInt(s)))});_("BITAND",(t,e)=>t&e),_("BITOR",(t,e)=>t|e),_("BITXOR",(t,e)=>t^e);function tt(t,e,n){const o=z(t),r=Math.trunc(b(e));if(Math.abs(r)>53)throw u.NUM;const s=r*n;if(s<=0)return Math.floor(o/2**-s);const c=o*2**s;if(c>x)throw u.NUM;return c}f("BITLSHIFT",2,2,(t,e)=>tt(t,e,1)),f("BITRSHIFT",2,2,(t,e)=>tt(t,e,-1)),f("DELTA",1,2,(t,e)=>M(t)===M(B(e,0))?1:0),f("GESTEP",1,2,(t,e)=>M(t)>=M(B(e,0))?1:0);function et(t){let e=t;for(let n=200;n>=1;n--)e=t+n/2/e;return Math.exp(-t*t)/(Math.sqrt(Math.PI)*e)}function S(t){const e=Math.abs(t);if(e>=6)return Math.sign(t);if(e>=2.5)return Math.sign(t)*(1-et(e));let n=e,o=e;for(let r=1;n>o*1e-17;r++)n*=2*e*e/(2*r+1),o+=n;return Math.sign(t)*2*Math.exp(-e*e)*o/Math.sqrt(Math.PI)}function k(t){return t<0?2-k(-t):t<1?1-S(t):t>=27?0:et(t)}f("ERF",1,2,(t,e)=>{const n=S(M(t));return e===void 0||e===O?n:S(M(e))-n}),f("ERF.PRECISE",1,1,t=>S(M(t))),f("ERFC",1,1,t=>k(M(t))),f("ERFC.PRECISE",1,1,t=>k(M(t)));const Ut=40,nt=1e10,N=1e-10,ot=2**27,Ct=709.7,At=22250738585072014e-324,F=t=>Math.abs(t)<At?0:t;function H(t){const e=Math.abs(t);if(e<3.75){const o=t/3.75,r=o*o;return 1+r*(3.5156229+r*(3.0899424+r*(1.2067492+r*(.2659732+r*(.0360768+r*.0045813)))))}const n=3.75/e;return Math.exp(e)/Math.sqrt(e)*(.39894228+n*(.01328592+n*(.00225319+n*(-.00157565+n*(.00916281+n*(-.02057706+n*(.02635537+n*(-.01647633+n*.00392377))))))))}function rt(t){const e=Math.abs(t);let n;if(e<3.75){const o=t/3.75,r=o*o;n=e*(.5+r*(.87890594+r*(.51498869+r*(.15084934+r*(.02658733+r*(.00301532+r*32411e-8))))))}else{const o=3.75/e,r=.02282967+o*(-.02895312+o*(.01787654-o*.00420059));n=.39894228+o*(-.03988024+o*(-.00362018+o*(.00163801+o*(-.01031555+o*r)))),n*=Math.exp(e)/Math.sqrt(e)}return t<0?-n:n}function st(t){if(t<=2){const n=t*t/4;return-Math.log(t/2)*H(t)+(-.57721566+n*(.4227842+n*(.23069756+n*(.0348859+n*(.00262698+n*(1075e-7+n*74e-7))))))}const e=2/t;return F(Math.exp(-t)/Math.sqrt(t))*(1.25331414+e*(-.07832358+e*(.02189568+e*(-.01062446+e*(.00587872+e*(-.0025154+e*53208e-8))))))}function ct(t){if(t<=2){const n=t*t/4;return Math.log(t/2)*rt(t)+1/t*(1+n*(.15443144+n*(-.67278579+n*(-.18156897+n*(-.01919402+n*(-.00110404+n*-4686e-8))))))}const e=2/t;return F(Math.exp(-t)/Math.sqrt(t))*(1.25331414+e*(.23498619+e*(-.0365562+e*(.01504268+e*(-.00780353+e*(.00325614+e*-68245e-8))))))}function G(t){const e=Math.abs(t);if(e<8){const i=t*t;return(57568490574+i*(-13362590354+i*(6516196407e-1+i*(-1121442418e-2+i*(77392.33017+i*-184.9052456)))))/(57568490411+i*(1029532985+i*(9494680718e-3+i*(59272.64853+i*(267.8532712+i*1)))))}const n=8/e,o=n*n,r=e-.785398164,s=1+o*(-.001098628267+o*(2734510407e-14+o*(-2073370639e-15+o*2093887211e-16))),c=-.01562499995+o*(.0001430488765+o*(-6911147651e-15+o*(7621095161e-16-o*934935152e-16)));return Math.sqrt(.636619772/e)*(Math.cos(r)*s-n*Math.sin(r)*c)}function V(t){const e=Math.abs(t);if(e<8){const a=t*t;return t*(72362614232+a*(-7895059235+a*(2423968531e-1+a*(-2972611439e-3+a*(15704.4826+a*-30.16036606)))))/(144725228442+a*(2300535178+a*(1858330474e-2+a*(99447.43394+a*(376.9991397+a*1)))))}const n=8/e,o=n*n,r=e-2.356194491,s=1+o*(.00183105+o*(-3516396496e-14+o*(-3516396496e-14+o*(2457520174e-15+o*-240337019e-15)))),c=.04687499995+o*(-.0002002690873+o*(8449199096e-15+o*(-88228987e-14+o*105787412e-15))),i=Math.sqrt(.636619772/e)*(Math.cos(r)*s-n*Math.sin(r)*c);return t<0?-i:i}function it(t){if(t<8){const c=t*t;return(-2957821389+c*(7062834065+c*(-5123598036e-1+c*(1087988129e-2+c*(-86327.92757+c*228.4622733)))))/(40076544269+c*(7452499648e-1+c*(7189466438e-3+c*(47447.2647+c*(226.1030244+c*1)))))+.636619772*G(t)*Math.log(t)}const e=8/t,n=e*e,o=t-.785398164,r=1+n*(-.001098628627+n*(2734510407e-14+n*(-2073370639e-15+n*2093887211e-16))),s=-.01562499995+n*(.0001430488765+n*(-6911147651e-15+n*(7621095161e-16+n*-934945152e-16)));return Math.sqrt(.636619772/t)*(Math.sin(o)*r+e*Math.cos(o)*s)}function at(t){if(t<8){const c=t*t;return t*(-4900604943e3+c*(127527439e4+c*(-51534381390+c*(7349264551e-1+c*(-4237922726e-3+c*8511.937935)))))/(249958057e5+c*(424441966400+c*(3733650367+c*(2245904002e-2+c*(102042.605+c*(354.9632885+c))))))+.636619772*(V(t)*Math.log(t)-1/t)}const e=8/t,n=e*e,o=t-2.356194491,r=1+n*(.00183105+n*(-3516396496e-14+n*(2457520174e-15+n*-240337019e-15))),s=.04687499995+n*(-.0002002690873+n*(8449199096e-15+n*(-88228987e-14+n*105787412e-15)));return Math.sqrt(.636619772/t)*(Math.sin(o)*r+e*Math.cos(o)*s)}const ut=t=>t+Math.trunc(Math.sqrt(Ut*t)),ft=(t,e)=>e>1e6&&2*t<e;function Pt(t,e){if(Math.abs(t)>Ct)throw u.NUM;if(e===0)return H(t);if(e===1)return rt(t);if(t===0||ft(Math.abs(t),e))return 0;const n=2/Math.abs(t);let o=0,r=1,s=0;for(let c=2*ut(e);c>0;c--){const i=o+c*n*r;o=r,r=i,Math.abs(r)>nt&&(s*=N,r*=N,o*=N),c===e&&(s=o)}return s*=H(t)/r,t<0&&e%2===1?-s:s}function bt(t,e){if(Math.abs(t)>=ot)throw u.NUM;if(e===0)return G(t);if(e===1)return V(t);const n=Math.abs(t);if(n===0||ft(n,e))return 0;const o=2/n;let r;if(n>e){let s=G(n),c=V(n);for(let i=1;i<e;i++){const a=i*o*c-s;s=c,c=a}r=c}else{let s=!1,c=0,i=0,a=1;r=0;for(let g=2*Math.trunc(ut(e)/2);g>0;g--){const P=g*o*a-i;i=a,a=P,Math.abs(a)>nt&&(a*=N,i*=N,r*=N,c*=N),s&&(c+=a),s=!s,g===e&&(r=i)}r/=2*c-a}return t<0&&e%2===1?-r:r}function Ot(t,e){if(t<=0||t>=ot)throw u.NUM;if(e===0)return it(t);if(e===1)return at(t);const n=2/t;let o=it(t),r=at(t);for(let s=1;s<e&&Number.isFinite(r);s++){const c=s*n*r-o;o=r,r=c}return r}function Bt(t,e){if(t<=0)throw u.NUM;if(e===0)return st(t);if(e===1)return ct(t);const n=2/t;let o=st(t),r=ct(t);for(let s=1;s<e&&Number.isFinite(r);s++){if(o===0&&r===0)return 0;const c=o+F(s*n*r);o=r,r=c}return r}const L=(t,e)=>f(t,2,2,(n,o)=>{const r=M(n),s=Math.trunc(M(o));if(M(o)<0)throw u.NUM;return T(e(r,s))});L("BESSELI",Pt),L("BESSELJ",bt),L("BESSELK",Bt),L("BESSELY",Ot);const D=String.raw`(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?`,$t=new RegExp(`^[+-]?${D}$`),qt=new RegExp(`^(?:([+-]?${D})(?=[+-]))?([+-]?)(${D})?([ij])$`);function I(t){if(t instanceof yt)throw t;if(typeof t=="boolean")throw u.VALUE;if(t==null||t===O)return{z:[0,0],suffix:null};if(typeof t=="number")return{z:[t,0],suffix:null};const e=R(t);let n=0,o=0,r=null;if($t.test(e))n=Number(e);else{const s=qt.exec(e);if(s===null)throw u.NUM;s[1]!==void 0&&(n=Number(s[1])),o=(s[2]==="-"?-1:1)*(s[3]===void 0?1:Number(s[3])),[,,,,r]=s}if(!Number.isFinite(n)||!Number.isFinite(o))throw u.NUM;return{z:[n,o],suffix:r}}function Y(t){const e=wt(t),n=/^(-?)(\d)(?:\.(\d+))?E-(\d+)$/.exec(e);if(!n)return e;const o=`0.${"0".repeat(Number(n[4])-1)}${n[2]}${n[3]??""}`;return o.length<=21?n[1]+o:e}function d([t,e],n){const o=$(T(t)),r=$(T(e));if(r===0)return Y(o);const s=r===1?"":r===-1?"-":Y(r);return o===0?`${s}${n}`:`${Y(o)}${r>0?"+":""}${s}${n}`}function j(t,e){if(t!==null&&e!==null&&t!==e)throw u.VALUE;return t??e}const zt=([t,e],[n,o])=>[t+n,e+o],_t=([t,e],[n,o])=>[t*n-e*o,t*o+e*n];function kt([t,e],[n,o]){const r=n*n+o*o;if(r===0)throw u.NUM;return[(t*n+e*o)/r,(e*n-t*o)/r]}const m=t=>(Math.exp(t)-Math.exp(-t))/2,p=t=>(Math.exp(t)+Math.exp(-t))/2,Ft=([t,e])=>[Math.sin(t)*p(e),Math.cos(t)*m(e)],Ht=([t,e])=>[Math.cos(t)*p(e),-Math.sin(t)*m(e)],Gt=([t,e])=>{const n=Math.sin(e);return[m(t)*Math.cos(e),(Math.exp(t)*n+Math.exp(-t)*n)/2]},Vt=([t,e])=>[p(t)*Math.cos(e),m(t)*Math.sin(e)];function X([t,e]){if(t===0&&e===0)throw u.NUM;return[Math.log(Math.hypot(t,e)),Math.atan2(e,t)]}const Dt=[1,0];f("COMPLEX",2,3,(t,e,n)=>{const o=M(t),r=M(e),s=B(n,"i");if(s!=="i"&&s!=="j")throw u.VALUE;return d([o,r],s)});const U=(t,e)=>f(t,1,1,n=>T(e(I(n).z)));U("IMABS",([t,e])=>Math.hypot(t,e)),U("IMAGINARY",([,t])=>t),U("IMREAL",([t])=>t),U("IMARGUMENT",([t,e])=>{if(t===0&&e===0)throw u.DIV0;return Math.atan2(e,t)});const h=(t,e)=>f(t,1,1,n=>{const{z:o,suffix:r}=I(n);return d(e(o),r??"i")});h("IMCONJUGATE",([t,e])=>[t,-e]),h("IMCOS",Ht),h("IMCOSH",Vt),h("IMCOT",([t,e])=>{const n=p(2*e)-Math.cos(2*t);return[Math.sin(2*t)/n,-m(2*e)/n]}),h("IMCSC",([t,e])=>{const n=(p(2*e)-Math.cos(2*t))/2;return[Math.sin(t)*(p(e)/n),-Math.cos(t)*(m(e)/n)]}),h("IMCSCH",([t,e])=>{const n=Math.sin(e),o=m(t),r=o*o+n*n;return[o*Math.cos(e)/r,-p(t)*n/r]}),h("IMEXP",([t,e])=>[Math.exp(t)*Math.cos(e),Math.exp(t)*Math.sin(e)]),h("IMLN",X),h("IMLOG10",t=>X(t).map(e=>e/Math.LN10)),h("IMLOG2",t=>X(t).map(e=>e/Math.LN2)),h("IMSEC",([t,e])=>{const n=(p(2*e)+Math.cos(2*t))/2;return[Math.cos(t)*p(e)/n,Math.sin(t)*m(e)/n]}),h("IMSECH",([t,e])=>{const n=Math.cos(e),o=m(t),r=o*o+n*n;return[p(t)*n/r,-o*Math.sin(e)/r]}),h("IMSIN",Ft),h("IMSINH",Gt),h("IMSQRT",([t,e])=>{const n=Math.sqrt(Math.hypot(t,e)),o=Math.atan2(e,t)/2;return[n*Math.cos(o),n*Math.sin(o)]}),h("IMTAN",([t,e])=>{const n=Math.cos(2*t)+p(2*e);return[Math.sin(2*t)/n,m(2*e)/n]}),f("IMDIV",2,2,(t,e)=>{const n=I(t),o=I(e);return d(kt(n.z,o.z),j(n.suffix,o.suffix)??"i")}),f("IMSUB",2,2,(t,e)=>{const n=I(t),o=I(e);return d([n.z[0]-o.z[0],n.z[1]-o.z[1]],j(n.suffix,o.suffix)??"i")}),f("IMPOWER",2,2,(t,e)=>{const{z:[n,o],suffix:r}=I(t),s=M(e);if(n===0&&o===0){if(s<=0)throw u.NUM;return"0"}const c=Math.hypot(n,o)**s,i=Math.atan2(o,n)*s;return d([c*Math.cos(i),c*Math.sin(i)],r??"i")});const Mt=(t,e,n,o)=>dt(t,1,255,(r,s)=>{let c=e,i=null,a=!1;for(const g of s)Tt(r,g,P=>{const Q=I(P);i=j(i,Q.suffix),c=n(c,Q.z),a=!0});return a?d(c,i??"i"):o});Mt("IMSUM",[0,0],zt,"0"),Mt("IMPRODUCT",Dt,_t,"0");const J=new Map;function l(t,e){for(const[n,o,r=1]of e)for(const s of o.split(" ")){const c=s.endsWith("*");J.set(c?s.slice(0,-1):s,{group:t,factor:n,prefix:c,power:r})}}const w=.0254,y=.3048,K=.9144,C=1609.344,A=1852,W=9460730472580800,Z=w/72,E=231*w**3;l("mass",[[1,"g*"],[14593.9029372064,"sg"],[453.59237,"lbm"],[1660538782e-33,"u*"],[28.349523125,"ozm"],[.06479891,"grain"],[45359.237,"cwt shweight"],[50802.34544,"uk_cwt lcwt hweight"],[6350.29318,"stone"],[907184.74,"ton"],[10160469088e-4,"uk_ton LTON brton"]]),l("distance",[[1,"m*"],[C,"mi"],[A,"Nmi"],[w,"in"],[y,"ft"],[K,"yd"],[1e-10,"ang*"],[1.143,"ell"],[W,"ly*"],[0x6da012f93c9a8c,"parsec pc"],[Z,"Picapt Pica"],[w/6,"pica"],[5280*1200/3937,"survey_mi"]]),l("time",[[31557600,"yr"],[86400,"day d"],[3600,"hr"],[60,"mn min"],[1,"sec* s*"]]),l("pressure",[[1,"Pa* p*"],[101325,"atm* at*"],[133.322,"mmHg*"],[6894.75729316836,"psi*"],[101325/760,"Torr*"]]),l("force",[[1,"N*"],[1e-5,"dyn* dy*"],[4.4482216152605,"lbf"],[.00980665,"pond*"]]),l("energy",[[1,"J*"],[1e-7,"e*"],[4.184,"c*"],[4.1868,"cal*"],[1602176487e-28,"eV* ev*"],[268451953769617e-8,"HPh hh"],[3600,"Wh* wh*"],[1.3558179483314,"flb"],[1055.05585262,"BTU btu"]]),l("power",[[745.69987158227,"HP h"],[735.49875,"PS"],[1,"W* w*"]]),l("magnetism",[[1,"T*"],[1e-4,"ga*"]]),l("volume",[[E/768,"tsp"],[5e-6,"tspm"],[E/256,"tbs"],[E/128,"oz"],[E/16,"cup"],[E/8,"pt us_pt"],[.00056826125,"uk_pt"],[E/4,"qt"],[.0011365225,"uk_qt"],[E,"gal"],[.00454609,"uk_gal"],[.001,"l* L* lt*"],[1e-30,"ang3* ang^3*",3],[42*E,"barrel"],[.03523907016688,"bushel"],[y**3,"ft3 ft^3"],[w**3,"in3 in^3"],[W**3,"ly3 ly^3"],[1,"m3* m^3*",3],[C**3,"mi3 mi^3"],[K**3,"yd3 yd^3"],[A**3,"Nmi3 Nmi^3"],[Z**3,"Picapt3 Picapt^3 Pica3 Pica^3"],[100*y**3,"GRT regton"],[40*y**3,"MTON"]]),l("area",[[4046.8564224,"uk_acre"],[43560*(1200/3937)**2,"us_acre"],[1e-20,"ang2* ang^2*",2],[100,"ar*"],[y**2,"ft2 ft^2"],[1e4,"ha"],[w**2,"in2 in^2"],[W**2,"ly2 ly^2"],[1,"m2* m^2*",2],[2500,"Morgen"],[C**2,"mi2 mi^2"],[A**2,"Nmi2 Nmi^2"],[Z**2,"Picapt2 Picapt^2 Pica2 Pica^2"],[K**2,"yd2 yd^2"]]),l("information",[[1,"bit*"],[8,"byte*"]]),l("speed",[[6080*y/3600,"admkn"],[A/3600,"kn"],[1/3600,"m/h* m/hr*"],[1,"m/s* m/sec*"],[C/3600,"mph"]]);const Yt=new Map([["Y",1e24],["Z",1e21],["E",1e18],["P",1e15],["T",1e12],["G",1e9],["M",1e6],["k",1e3],["h",100],["da",10],["e",10],["d",.1],["c",.01],["m",.001],["u",1e-6],["n",1e-9],["p",1e-12],["f",1e-15],["a",1e-18],["z",1e-21],["y",1e-24]]),ht=new Map([["Yi",2**80],["Zi",2**70],["Ei",2**60],["Pi",2**50],["Ti",2**40],["Gi",2**30],["Mi",2**20],["ki",2**10]]),lt=[1,0],pt=[1.8,32],mt=[1,273.15],It=new Map([["C",lt],["cel",lt],["F",pt],["fah",pt],["K",mt],["kel",mt],["Rank",[1.8,491.67]],["Reau",[.8,0]]]),Et=t=>Math.abs(t)<1e-12?0:t;function jt(t,[e,n],[o,r]){const s=o/e;return Et(t*s+Et(r-n*s))}function Nt(t){if(It.has(t))return{group:"temperature",temperature:It.get(t)};const e=J.get(t);if(e!==void 0)return e;for(const n of[2,1]){const o=J.get(t.slice(n));if(o===void 0||!o.prefix)continue;const r=t.slice(0,n),s=Yt.get(r);if(s!==void 0)return{group:o.group,factor:o.factor*s**o.power};if(o.group==="information"&&ht.has(r))return{group:o.group,factor:o.factor*ht.get(r)}}return null}f("CONVERT",3,3,(t,e,n)=>{const o=M(t),r=R(e),s=R(n),c=Nt(r),i=Nt(s);if(c===null||i===null||c.group!==i.group)throw u.NA;if(r===s)return o;const a=c.group==="temperature"?jt(o,c.temperature,i.temperature):o*c.factor/i.factor;return $(T(a))});
@@ -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 E,Ref as Ut,RefList as Ft,XlError as Vt}from"../values.js";import{liftReferenceArgs as kt,scalarOf as St,toolpakScalar as dt}from"./helpers.js";import{partsToSerial as rt,serialToParts as x}from"../../core/numfmt.js";Ot("Financial");const Yt=2958465,pt=1e-7,yt=200,y=(t,n,s,o)=>Nt(t,n,s,(...e)=>X(o(...e))),d=t=>{if(!t)throw N.NUM},p=t=>{if(typeof t=="boolean")throw N.VALUE;return h(t)},G=t=>h(E(t,0))!==0?1:0;function D(t){const n=Math.trunc(p(t));if(n<0||n>Yt)throw N.VALUE;return n}function F(t){const n=Math.trunc(p(E(t,0)));return d(n>=0&&n<=4),n}function j(t){const n=Math.trunc(p(t));return d(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 Bt(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 Bt(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&&d(r<a);const c=j(s),f=F(o);return d(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),m=r-l*2*M/(t(r+M)-t(r-M));if(!Number.isFinite(m)||m<=s)break;if(Math.abs(m-r)<pt&&e(m))return m;r=m}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)=>{const r=J(t,n),a=Math.abs(t)<1e-6?ot(t,n):(r-1)/t;return-(o*r+s*(1+t*e)*a)},Xt=(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(E(e,0)),G(r)];return d(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(E(o,0)),G(e))),y("PV",3,5,(t,n,s,o,e)=>Xt(h(t),h(n),h(s),h(E(o,0)),G(e))),y("FV",3,5,(t,n,s,o,e)=>ft(h(t),h(n),h(s),h(E(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(E(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(E(o,0)),u=G(e),l=h(E(r,.1));d(a>0&&l>-1);const M=nt(m=>f*J(m,a)+c*(1+m*u)*ot(m,a)+i,l);return d(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 d(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 d(o>0&&e!==0&&e*r>=0),(r/e)**(1/o)-1});function Dt(t,n,s,o,e,r,a){const c=p(t),f=Math.trunc(p(n)),i=p(s),u=Math.trunc(p(o)),l=Math.trunc(p(e)),M=Math.trunc(p(r));d(c>0&&f>0&&i>0&&u>=1&&l>=u&&l<=f&&(M===0||M===1));let m=0;for(let I=u;I<=l;I++)m+=a(c,I,f,i,M);return m}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(St(t,n,s,o)),it=t=>t.some(n=>n>0)&&t.some(n=>n<0),Rt=99;function bt(t,n,s){const o=q(t,n),e=q(t,s).map(D);return d(o.length===e.length&&o.length>0),d(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}function xt(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)}B("NPV",2,255,(t,n,s,o)=>kt(n,e=>xt(t,e,s,o)),{array:!0}),B("IRR",1,2,(t,n,s,o)=>{const e=q(t,n[0]),r=K(t,E(n[1],.1),s,o);return d(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]=bt(t,n[1],n[2]);return d(e>=0),X(H(r,a,e))},{array:!0});function Gt(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){d(Math.abs(r)<1);const u=-Math.abs(r);e(H(t,n,u))?[c,f]=[-1,u]:[c,f]=[u,0]}else for(d(r>0),e(H(t,n,f))||(i=1);!e(H(t,n,f));)c=f,f*=2,i++,d(i<=Rt);for(;++i<=Rt;){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]=bt(t,n[0],n[1]),a=K(t,E(n[2],.1),s,o);return d(a>-1&&it(e)),X(Gt(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 d(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(E(e,12)));if(d(r>=0&&a>=0&&c>0&&f>=1&&i>=1&&i<=12),d(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 m=2;m<=Math.min(f,c);m++)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(E(e,2));return d(r>=0&&a>=0&&c>0&&f>0&&f<=c&&i>0),ut(r,a,c,f,i)});function Et(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 m=ut(t,n,s,l,r);f=c/(o-(l-1)),i=f>m,M=i?f:m,i||(c-=m)}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(E(r,2)),m=mt(E(a,!1));if(d(c>=0&&f>=0&&i>0&&u>=0&&l>=u&&l<=i&&M>=0),m){const b=Math.floor(u),R=Math.ceil(l);let g=0;for(let A=b+1;A<=R;A++){let C=ut(c,f,i,A,M);A===b+1?C*=Math.min(l,b+1)-u:A===R&&(C*=l+1-R),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-=Et(c,f,i,I,u,M),Et(c,f,i,i-u,l-u,M)});function At(t,n,s,o,e,r,a){const c={cost:p(t),bought:D(n),firstEnd:D(s),salvage:p(o),per:Math.trunc(p(e)),rate:p(r),basis:F(a)};return d(c.basis!==2&&c.cost>=0&&c.salvage>=0&&c.salvage<=c.cost),d(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,m)=>a>=M&&a<=m;d(c(3,4)||c(5,6)||a>6);const f=e*(c(3,4)?1.5:c(5,6)?2:2.5),i=Math.ceil(a)-1;if(o>i)return 0;let u=Math.round(r*f*n),l=n-u;for(let M=1;M<=o;M++){if(l<s)return 0;u=(M===i?1:M===i-1?.5:f)*l,l-=u}return Math.round(u)}),y("EFFECT",2,2,(t,n)=>{const s=p(t),o=Math.trunc(p(n));return d(s>0&&o>=1),(1+s/o)**o-1}),y("NOMINAL",2,2,(t,n)=>{const s=p(t),o=Math.trunc(p(n));return d(s>0&&o>=1),o*((1+s)**(1/o)-1)});function Ct(t,n,s){const o=p(t),e=Math.trunc(p(n));if(d(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=p(s);return d(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=p(o),M=p(E(e,1e3)),m=j(r),I=F(a),b=mt(E(c,!0));d(l>0&&M>0&&f<u);const R=!b&&u>i?i:f,g=12/m,A=Z(i);let C=0;for(;P(i,C*g,A)>R;)C--;for(;P(i,(C+1)*g,A)<=R;)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)/m;U+=V(Math.max(L,R),Math.min(v,u),I)/T}return M*l*U/m}),y("ACCRINTM",4,5,(t,n,s,o,e)=>{const r=D(t),a=D(n),c=p(s),f=p(E(o,1e3)),i=F(e);return d(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=p(e),u=p(r),l=F(a);return d(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 d(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=p(a),m=p(c),I=F(f);return d(i<u&&l<i&&M>=0&&n(m)),s(M,m,_(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=p(s),f=p(o),i=p(e),u=tt(t,n,r,a);return d(c>=0&&f>=0&&i>0),Pt(u,c,f,i)}),y("YIELD",6,7,(t,n,s,o,e,r,a)=>{const c=p(s),f=p(o),i=p(e),u=tt(t,n,r,a);if(d(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=p(s),c=p(o),f=tt(t,n,e,r);d(a>=0&&c>=0);const i=1+c/f.f,u=100*a/f.f,l=f.dsc/f.e;let M=0,m=0;for(let I=0;I<f.n;I++){const b=I+l,R=(I===f.n-1?u+100:u)/i**b;M+=b*R,m+=R}return[M/m/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=p(e),m=p(r),I=j(a),b=F(c);d(i>l&&l>f&&f>u&&M>=0&&m>0);const R=at(f,l,I,b),g=R.n===1?V(f,l,b):R.dsc,A=at(l,i,I,b).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=b===1?w-O:R.e;L+=(O>u?S:V(u,w,b))/S;const ht=Math.max(u,O),Mt=Math.min(f,w);if(Mt>ht&&(v+=V(ht,Mt,b)/S),O<=u)break;w=O}const T=100*M/I,Y=R.n-1+g/R.e;return k=>{const w=1+k/I;let O=m/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=p(r),u=Tt(t,n,s,o,e,a,c,f);return d(i>=0),u(i)}),y("ODDFYIELD",8,9,(t,n,s,o,e,r,a,c,f)=>{const i=p(r),u=Tt(t,n,s,o,e,a,c,f);return d(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=p(o),l=p(e),M=j(r),m=F(a);d(f>c&&c>i&&u>=0&&l>0);const I=12/M,b=Z(i);let R=0,g=0,A=0;for(let U=0;;U++){const L=P(i,U*I,b),v=P(i,(U+1)*I,b),T=m===0||m===4?360/M:v-L,Y=v>=f,k=Y?V(L,f,m):T;R+=k/T,v<c?g+=k/T:L<c&&(g+=V(L,c,m)/T);const w=Math.max(c,L),O=Math.min(f,v);if(O>w&&(A+=V(w,O,m)/T),Y)break}const C=100*u/M;return{income:l+R*C,interest:g*C,toGo:A,f:M}}y("ODDLPRICE",7,8,(t,n,s,o,e,r,a,c)=>{const f=p(e),i=wt(t,n,s,o,r,a,c);return d(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=p(e),i=wt(t,n,s,o,r,a,c);d(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 Ft}from"./registry.js";import{lift as kt,raw as B}from"./registry.js";import{Arr as pt,bool as dt,checked as x,ERR as A,num as h,OMITTED as rt,opt as L,Ref as vt,RefList as St,XlError as Yt}from"../values.js";import{liftReferenceArgs as Bt,scalarOf as xt,toolpakScalar as yt}from"./helpers.js";import{partsToSerial as ct,serialToParts as X}from"../../core/numfmt.js";Ft("Financial");const It=2958465,bt=1e-7,Dt=200,I=(t,n,s,o)=>kt(t,n,s,(...e)=>x(o(...e))),p=t=>{if(!t)throw A.NUM},y=t=>{if(typeof t=="boolean")throw A.VALUE;return h(t)},at=(t,n)=>t===void 0?n:t===rt?0:t,G=t=>h(L(t,0))!==0?1:0;function D(t){const n=Math.trunc(y(t));if(n<0||n>It)throw A.NUM;return n}function v(t){const n=Math.trunc(y(L(t,0)));return p(n>=0&&n<=4),n}function j(t){const n=Math.trunc(y(t));return p(n===1||n===2||n===4),n}const z=t=>t%4===0&&(t%100!==0||t%400===0),it=(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>=it(n.year,n.month)}function C(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=it(r,a);return ct(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=i=>i.month===2&&i.day>=it(i.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 F=(t,n,s)=>s===0?$(t,n):s===4?$(t,n,!0):n-t;function Xt(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<=ct(s.year,2,29)||z(o.year)&&n>=ct(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 q(t,n,s=0){const o=Math.trunc(Math.min(t,n)),e=Math.trunc(Math.max(t,n));switch(s){case 1:return Xt(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 ft(t,n,s,o){const e=Z(n),r=12/s;let a=1;for(;C(n,-a*r,e)>t;)a++;const c=C(n,-a*r,e),i=C(n,(1-a)*r,e),f=F(c,t,o),u=o===1?i-c:(o===3?365:360)/s,l=o===0||o===4?u-f:i-t;return{n:a,pcd:c,ncd:i,a:f,e:u,dsc:l,part:(u-f)/u,f:s,basis:o}}function tt(t,n,s,o,e=!1){const r=D(t),a=D(n);e&&p(r<a);const c=j(s),i=v(o);return p(r<a),ft(r,a,c,i)}function nt(t,n,s=-1,o=()=>0,e=!1){const r=.001*Math.max(1,Math.abs(t(n))),a=m=>Math.abs(t(m))<=Math.max(r,1e-9*o(m));let c=n;for(let m=0;m<Dt;m++){const M=t(c);if(M===0)return c;const d=Math.min(1e-6*(1+Math.abs(c)),(c-s)/2),b=c-M*2*d/(t(c+d)-t(c-d));if(!Number.isFinite(b)||b<=s)break;if(Math.abs(b-c)<bt&&a(b))return b;c=b}if(e)throw A.NUM;let i=n,f=n+.01*(1+Math.abs(n)),u=t(i),l=t(f);for(let m=0;m<Dt;m++){if(l===0)return f;let M=f-l*(f-i)/(l-u);if(M<=s&&(M=(f+s)/2),!Number.isFinite(M))break;if(Math.abs(M-f)<bt&&a(M))return M;i=f,u=l,f=M,l=t(f)}throw A.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,ut=(t,n,s,o,e)=>{const r=J(t,n),a=Math.abs(t)<1e-6?ot(t,n):(r-1)/t;return-(o*r+s*(1+t*e)*a)},Gt=(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?ut(t,n-2,a,o,1)-a:ut(t,n-1,a,o,0))*t}function Et(t,n,s,o,e,r){const a=[h(t),h(n),h(s),h(o),h(L(e,0)),G(r)];return p(a[1]>=1&&a[1]<a[2]+1),a}I("PMT",3,5,(t,n,s,o,e)=>et(h(t),h(n),h(s),h(L(o,0)),G(e))),I("PV",3,5,(t,n,s,o,e)=>Gt(h(t),h(n),h(s),h(L(o,0)),G(e))),I("FV",3,5,(t,n,s,o,e)=>ut(h(t),h(n),h(s),h(L(o,0)),G(e))),I("IPMT",4,6,(...t)=>st(...Et(...t))),I("PPMT",4,6,(...t)=>{const[n,s,o,e,r,a]=Et(...t);return et(n,o,e,r,a)-st(n,s,o,e,r,a)}),I("NPER",3,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),i=h(L(o,0)),f=G(e);if(r===0){if(a===0)throw A.DIV0;return-(c+i)/a}const u=a*(1+r*f);return Math.log((u-i*r)/(u+c*r))/Math.log1p(r)}),I("RATE",3,6,(t,n,s,o,e,r)=>{const a=h(t),c=h(n),i=h(s),f=h(L(o,0)),u=G(e),l=h(L(r,.1));if(l<=-1)throw A.VALUE;p(a>0);const m=b=>i*J(b,a),M=b=>c*(1+b*u)*ot(b,a),d=nt(b=>m(b)+M(b)+f,l,-1,b=>Math.abs(m(b))+Math.abs(M(b))+Math.abs(f),!0);return p(d>-1+1e-6),d}),I("ISPMT",4,4,(t,n,s,o)=>{const e=h(s);if(e===0)throw A.DIV0;return h(o)*h(t)*(h(n)/e-1)}),I("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)}),I("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 Rt(t,n,s,o,e,r,a){const c=y(t),i=Math.trunc(y(n)),f=y(s),u=Math.trunc(y(o)),l=Math.trunc(y(e)),m=y(r);p(c>0&&i>0&&f>0&&u>=1&&l>=u&&l<=i&&(m===0||m===1));let M=0;for(let d=u;d<=l;d++)M+=a(c,d,i,f,m);return M}I("CUMIPMT",6,6,(...t)=>Rt(...t,(n,s,o,e,r)=>st(n,s,o,e,0,r))),I("CUMPRINC",6,6,(...t)=>Rt(...t,(n,s,o,e,r)=>et(n,o,e,0,r)-st(n,s,o,e,0,r)));function _(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 Yt)throw r};return n instanceof St?n.refs.forEach(o):n instanceof vt||n instanceof pt?o(n):s.push(h(n)),s}const K=(t,n,s,o)=>h(xt(t,n,s,o)),At=t=>t.some(n=>n>0)&&t.some(n=>n<0),wt=99;function Lt(t,n,s){for(const r of[n,s])if(t.toArr(r).data.some(a=>typeof a=="string"||typeof a=="boolean"))throw A.VALUE;const o=_(t,n),e=_(t,s).map(Math.trunc);return p(e.every(r=>r>=0&&r<=It)),p(o.length===e.length&&o.length>0),p(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}function qt(t,n,s,o){const e=K(t,n[0],s,o);if(e===-1)throw A.DIV0;let r=0,a=1;for(let c=1;c<n.length;c++)for(const i of _(t,n[c]))r+=i/(1+e)**a++;return x(r)}B("NPV",2,255,(t,n,s,o)=>Bt(n,e=>qt(t,e,s,o)),{array:!0}),B("IRR",1,2,(t,n,s,o)=>{if(n[0]===rt)throw A.VALUE;const e=_(t,n[0]),r=K(t,L(n[1],.1),s,o);return p(r>-1&&At(e)),nt(a=>{let c=0;for(let i=0;i<e.length;i++)c+=e[i]/(1+a)**i;return c},r)},{array:!0}),B("MIRR",3,3,(t,n,s,o)=>{if(n[0]===rt)throw A.VALUE;const e=_(t,n[0]),r=K(t,n[1],s,o),a=K(t,n[2],s,o);if(r===-1||a===-1||!e.some(u=>u<0))throw A.DIV0;const c=e.length-1;let i=0,f=0;return e.forEach((u,l)=>{u>0?i+=u*(1+a)**(c-l):f+=u/(1+r)**l}),x((i/-f)**(1/c)-1)},{array:!0}),B("XNPV",3,3,(t,n,s,o)=>{const e=h(yt(t,n[0],s,o)),[r,a]=Lt(t,n[1],n[2]);return p(e>=0),x(H(r,a,e))},{array:!0});function _t(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,i=r,f=0;if(a){p(Math.abs(r)<1);const u=-Math.abs(r);e(H(t,n,u))?[c,i]=[-1,u]:[c,i]=[u,0]}else for(p(r>0),e(H(t,n,i))||(f=1);!e(H(t,n,i));)c=i,i*=2,f++,p(f<=wt);for(;++f<=wt;){const u=(c+i)/2,l=H(t,n,u);if(e(l)?i=u:c=u,i-c<=1e-8*(Math.abs(u)+.5)&&(!a||Math.abs(l)<=2e-8*Math.abs(o)))return u}throw A.NUM}B("XIRR",2,3,(t,n,s,o)=>{const e=K(t,L(n[2],.1),s,o),[r,a]=Lt(t,n[0],n[1]);return p(e>-1&&At(r)),x(_t(r,a,e))},{array:!0}),B("FVSCHEDULE",2,2,(t,n,s,o)=>{let e=h(yt(t,n[0],s,o));if(n[1]instanceof pt&&n[1].data.some(r=>typeof r=="string"))throw A.VALUE;for(const r of _(t,n[1]))e*=1+r;return x(e)},{array:!0}),I("SLN",3,3,(t,n,s)=>{const o=h(s);if(o===0)throw A.DIV0;return(h(t)-h(n))/o}),I("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))}),I("DB",4,5,(t,n,s,o,e)=>{const r=h(t),a=h(n),c=h(s),i=Math.trunc(h(o)),f=Math.trunc(h(at(e,12)));if(p(r>=0&&a>=0&&c>0&&i>=1&&f>=1&&f<=12),p(i<=(f===12?c:c+1)),r===0)return 0;const u=Math.round((1-(a/r)**(1/c))*1e3)/1e3;let l=r*u*f/12;if(i===1)return l;let m=0;for(let M=2;M<=Math.min(i,c);M++)m=(r-l)*u,l+=m;return i>c?(r-l)*u*(12-f)/12:m});function Ct(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)}I("DDB",4,5,(t,n,s,o,e)=>{const[r,a,c,i]=[h(t),h(n),h(s),h(o)],f=h(at(e,2));return p(r>=0&&a>=0&&c>0&&i>0&&i<=c&&f>0),Ct(r,a,c,i,f)});function lt(t,n,s,o,e,r){const a=Math.min(r/s,1);let c=t,i=0,f=null;for(let u=0;u<e;u++){if(f===null){const l=c*a,m=(c-n)/(s-o-u);if(m>l)f=m;else{const M=Math.min(l,c-n);c-=M,i+=M*Math.min(1,e-u);continue}}c-=f,i+=f*Math.min(1,e-u)}return i}function Ht(t,n,s,o,e){const r=o-Math.floor(o);if(r===0)return t-lt(t,n,s,0,o,e);const a=Math.max(0,Math.min(t*Math.min(e/s,1)*r,t-n)),c=Math.max(0,(t-n)/s*r),i=t-Math.max(a,c);return i-lt(i,n,s,r,Math.floor(o),e)}I("VDB",5,7,(t,n,s,o,e,r,a)=>{const c=h(t),i=h(n),f=h(s),u=h(o),l=h(e),m=h(at(r,2)),M=dt(L(a,!1));if(p(l<=f),f===0)throw A.DIV0;if(p(c>=0&&i>=0&&f>0&&u>=0&&l>=u&&l<=f&&m>=0),M){const d=Math.floor(u),b=Math.ceil(l);let E=0;for(let w=d+1;w<=b;w++){let P=Ct(c,i,f,w,m);w===d+1?P*=Math.min(l,d+1)-u:w===b&&(P*=l+1-b),E+=P}return E}return lt(Ht(c,i,f,u,m),i,f,u,l-u,m)});function Pt(t,n,s,o,e,r,a){const c={cost:y(t),bought:D(n),firstEnd:D(s),salvage:y(o),per:y(e),rate:y(r),basis:v(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=q(c.bought,c.firstEnd,c.basis),c}I("AMORLINC",6,7,(...t)=>{const{cost:n,salvage:s,per:o,rate:e,frac:r}=Pt(...t),a=n*e,c=Math.min(r*a,n-s),i=Math.trunc((n-s-c)/a);return o===0?c:o<=i?a:Math.ceil(o)===i+1?n-s-a*i-c:0}),I("AMORDEGRC",6,7,(...t)=>{const{cost:n,salvage:s,rate:o,frac:e,...r}=Pt(...t),a=Math.trunc(r.per),c=1/o,i=(M,d)=>c>=M&&c<=d;p(i(3,4)||i(5,6)||c>6);const f=o*(i(3,4)?1.5:i(5,6)?2:2.5),u=Math.ceil(c)-1;if(a>u)return 0;let l=Math.min(Math.round(e*f*n),n-s),m=n-l;for(let M=1;M<=a;M++){if(m<s)return 0;l=(M===u?1:M===u-1?.5:f)*m,m-=l}return Math.round(l)}),I("EFFECT",2,2,(t,n)=>{const s=y(t),o=Math.trunc(y(n));return p(s>0&&o>=1),(1+s/o)**o-1}),I("NOMINAL",2,2,(t,n)=>{const s=y(t),o=Math.trunc(y(n));return p(s>0&&o>=1),o*((1+s)**(1/o)-1)});function Tt(t,n,s){const o=y(t),e=Math.trunc(y(n));if(p(e>=0),e===0)throw A.DIV0;const r=Math.trunc(o);return r+s(o-r,e,10**Math.ceil(Math.log10(e)))}I("DOLLARDE",2,2,(t,n)=>Tt(t,n,(s,o,e)=>s*e/o)),I("DOLLARFR",2,2,(t,n)=>Tt(t,n,(s,o,e)=>s*o/e));function ht(t,n,s,o){const e=D(t),r=D(n),a=y(s);return p(e<r&&r<=C(e,12,!1)&&a>0),o(a,r-e)}I("TBILLEQ",3,3,(t,n,s)=>ht(t,n,s,(o,e)=>{if(e<=182)return 365*o/(360-o*e);const r=1-o*e/360,a=e/365;return(2*Math.sqrt(a*a-(2*a-1)*(1-1/r))-2*a)/(2*a-1)})),I("TBILLPRICE",3,3,(t,n,s)=>ht(t,n,s,(o,e)=>100*(1-o*e/360))),I("TBILLYIELD",3,3,(t,n,s)=>ht(t,n,s,(o,e)=>(100-o)/o*360/e)),I("ACCRINT",6,8,(t,n,s,o,e,r,a,c)=>{const i=D(t),f=D(n),u=D(s),l=y(o),m=y(L(e,1e3)),M=j(r),d=v(a),b=dt(L(c,!0));p(l>0&&m>0&&i<u);const E=12/M,w=Z(f),P=(R,N)=>d===1?N-R:(d===3?365:360)/M,S=P(C(f,-E,w),f),k=m*l/M;if(!b){let R=0;for(;C(i,(R+2)*E,!1)<=f;)R++;return k*F(C(i,R*E,!1),u,d)/S}let T=0,V=Math.max(f,i);for(let R=0;C(f,R*E,w)>i;R--){const N=C(f,R*E,w),g=C(f,(R-1)*E,w);if(g>=u){V=Math.max(g,i);continue}T+=g>=i?1:F(i,N,d)/P(g,N)}return T+=F(V,u,d)/S,k*T}),I("ACCRINTM",4,5,(t,n,s,o,e)=>{const r=D(t),a=D(n),c=y(s),i=y(L(o,1e3)),f=v(e);return p(c>0&&i>0&&r<a),i*c*q(r,a,f)});const Q=(t,n)=>I(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=>Math.max(0,t.pcd));const W=(t,n)=>I(t,4,5,(s,o,e,r,a)=>{const c=D(s),i=D(o),f=y(e),u=y(r),l=v(a);return p(c<i&&f>0&&u>0),n(f,u,q(c,i,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)=>I(t,5,6,(o,e,r,a,c,i)=>{const f=D(o),u=D(e),l=D(r),m=y(a),M=y(c),d=v(i);return p(f<u&&l<f&&m>=0&&n(M)),s(m,M,q(l,u,d),q(l,f,d),q(f,u,d))});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 Nt(t,n,s,o){const e=100*n/t.f,{part:r}=t;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 i=0;i<t.n;i++)c+=e/a**(i+r);return c-e*t.a/t.e}I("PRICE",6,7,(t,n,s,o,e,r,a)=>{const c=y(s),i=y(o),f=y(e),u=tt(t,n,r,a);return p(c>=0&&i>=0&&f>0),Nt(u,c,i,f)}),I("YIELD",6,7,(t,n,s,o,e,r,a)=>{const c=y(s),i=y(o),f=y(e),u=tt(t,n,r,a);if(p(c>=0&&i>0&&f>0),u.n===1){const l=c/u.f,m=u.basis===2||u.basis===3,M=m?u.ncd-u.pcd:u.e,d=i/100+u.a/M*l;return(f/100+l-d)/d*(u.f*M/(m?u.dsc:M-u.a))}return nt(l=>Nt(u,c,l,f)-i,.1,-u.f)});function Ot(t,n,s,o,e,r){const a=y(s),c=y(o),i=tt(t,n,e,r);p(a>=0&&c>=0);const f=1+c/i.f,u=100*a/i.f,{part:l}=i;let m=0,M=0;for(let d=0;d<i.n;d++){const b=d+l,E=(d===i.n-1?u+100:u)/f**b;m+=b*E,M+=E}return[m/M/i.f,f]}I("DURATION",5,6,(...t)=>Ot(...t)[0]),I("MDURATION",5,6,(...t)=>{const[n,s]=Ot(...t);return n/s});function Ut(t,n,s,o,e,r,a,c){const i=D(t),f=D(n),u=D(s),l=D(o),m=y(e),M=y(r),d=j(a),b=v(c);p(f>l&&l>i&&i>u&&m>=0&&M>0);const E=ft(i,l,d,b),w=E.n===1?F(i,l,b):E.dsc,P=ft(l,f,d,b).n,S=Z(l),k=12/d;let T=0,V=0;for(let g=0,O=l;;g++){const U=C(l,-(g+1)*k,S),Y=b===1?O-U:E.e;T+=(U>u?Y:F(u,O,b))/Y;const Mt=Math.max(u,U),mt=Math.min(i,O);if(mt>Mt&&(V+=F(Mt,mt,b)/Y),U<=u)break;O=U}const R=100*m/d,N=E.n-1+w/E.e;return g=>{const O=1+g/d;let U=M/O**(P+N)+R*T/O**N;for(let Y=1;Y<=P;Y++)U+=R/O**(Y+N);return U-R*V}}I("ODDFPRICE",8,9,(t,n,s,o,e,r,a,c,i)=>{const f=y(r),u=Ut(t,n,s,o,e,a,c,i);return p(f>=0),u(f)}),I("ODDFYIELD",8,9,(t,n,s,o,e,r,a,c,i)=>{const f=y(r),u=Ut(t,n,s,o,e,a,c,i);return p(f>0),nt(l=>u(l)-f,.1,-j(c))});function Vt(t,n,s,o,e,r,a){const c=D(t),i=D(n),f=D(s),u=y(o),l=y(e),m=j(r),M=v(a);p(i>c&&c>f&&u>=0&&l>0);const d=12/m,b=Z(f);let E=0,w=0,P=0;for(let k=0;;k++){const T=C(f,k*d,b),V=C(f,(k+1)*d,b),R=M===0||M===4?360/m:V-T,N=V>=i,g=N?F(T,i,M):R;E+=g/R,V<c?w+=g/R:T<c&&(w+=F(T,c,M)/R);const O=Math.max(c,T),U=Math.min(i,V);if(U>O&&(P+=F(O,U,M)/R),N)break}const S=100*u/m;return{income:l+E*S,interest:w*S,toGo:P,f:m}}I("ODDLPRICE",7,8,(t,n,s,o,e,r,a,c)=>{const i=y(e),f=Vt(t,n,s,o,r,a,c);return p(i>=0),f.income/(1+f.toGo*i/f.f)-f.interest}),I("ODDLYIELD",7,8,(t,n,s,o,e,r,a,c)=>{const i=y(e),f=Vt(t,n,s,o,r,a,c);p(i>0);const u=i+f.interest;return(f.income-u)/u*(f.f/f.toGo)});export{$ as days360,q as yearFrac};
@@ -1 +1 @@
1
- import{Arr as p,bool as V,compareText as $,compareValues as m,ERR as a,num as b,OMITTED as h,Ref as w,RefArray as x,RefList as y,RichValue as R,str as g,textToNumber as L,XlError as u}from"../values.js";function d(n,r,o){if(r instanceof w)n.scan(r,o);else if(r instanceof y)for(const e of r.refs)n.scan(e,o);else if(r instanceof x)d(n,r.data[0],o);else if(r instanceof p)for(const e of r.data)o(e);else r!==h&&r!==void 0&&o(r)}function M(n,r){let o=0,e=0;for(const i of n)i instanceof x&&(o=Math.max(o,i.rows),e=Math.max(e,i.cols));if(o===0)return r(n);const s=new Array(o*e);for(let i=0;i<o;i++)for(let c=0;c<e;c++){const t=n.map(f=>{if(!(f instanceof x))return f;const l=f.rows===1?0:i,E=f.cols===1?0:c;return l<f.rows&&E<f.cols?f.get(l,E):a.NA});try{s[i*e+c]=r(t)}catch(f){if(!(f instanceof u))throw f;s[i*e+c]=f}}return new p(o,e,s)}function O(n,r){const o=[];for(const e of r)e instanceof w||e instanceof y||e instanceof p?d(n,e,s=>{if(typeof s=="number")o.push(s);else{if(s instanceof u)throw s;if(s instanceof R)throw a.VALUE}}):e!==h&&e!==void 0&&e!==null?o.push(b(e)):(e===h||e===null)&&o.push(0);return o}function k(n,r){const o=[];for(const e of r)if(e instanceof w||e instanceof y||e instanceof p){const s=!(e instanceof p);d(n,e,i=>{if(typeof i=="number")o.push(i);else if(typeof i=="boolean"&&s)o.push(i?1:0);else if(typeof i=="string")o.push(0);else{if(i instanceof u)throw i;if(i instanceof R)throw a.VALUE}})}else e!==h&&e!==void 0&&e!==null&&o.push(b(e));return o}function N(n,r){return n.toArr(r).data}function D(n,r,o){const e=N(n,r),s=N(n,o);if(e.length!==s.length)throw a.NA;const i=[],c=[];for(let t=0;t<e.length;t++){if(e[t]instanceof u)throw e[t];if(s[t]instanceof u)throw s[t];typeof e[t]=="number"&&typeof s[t]=="number"&&(i.push(e[t]),c.push(s[t]))}return[i,c]}function S(n,r,o,e){const s=n.scalar(r,o,e);if(s instanceof u)throw s;return s}function F(n,r,o,e){if(r instanceof y||!n.legacy&&r instanceof w&&(r.r0!==r.r1||r.c0!==r.c1))throw a.VALUE;const s=S(n,r,o,e);if(typeof s=="boolean")throw a.VALUE;return s}function U(n,r=!1){let o="^";for(let e=0;e<n.length;e++){const s=n[e];s==="~"&&(r||n[e+1]==="*"||n[e+1]==="?"||n[e+1]==="~")?o+=(n[++e]??"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):s==="*"?o+="[\\s\\S]*":s==="?"?o+="[\\s\\S]":o+=s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}return new RegExp(`${o}$`,"i")}const T=n=>/[*?~]/.test(n);function A(n,r){const o=typeof n=="string"?L(n):n;return typeof o=="number"&&m(o,r)===0}function I(n){if(n instanceof u)return t=>t===n;if(typeof n=="number")return t=>A(t,n);if(typeof n=="boolean")return t=>t===n;if(n==null||n===h)return t=>t===0||t===null;const r=String(n),o=/^(<=|>=|<>|<|>|=)([\s\S]*)$/.exec(r),e=o?o[1]:"=",s=o?o[2]:r,i=L(s);if(i!==null)switch(e){case"=":return t=>A(t,i);case"<>":return t=>!A(t,i);case"<":return t=>typeof t=="number"&&m(t,i)<0;case"<=":return t=>typeof t=="number"&&m(t,i)<=0;case">":return t=>typeof t=="number"&&m(t,i)>0;default:return t=>typeof t=="number"&&m(t,i)>=0}const c=s.toUpperCase();if(c==="TRUE"||c==="FALSE"){const t=c==="TRUE";return e==="<>"?f=>f!==t:f=>f===t}if(e==="="||e==="<>"){if(s==="")return e==="="?f=>f===null:f=>f!==null;if(T(s)){const f=U(s);return e==="="?l=>typeof l=="string"&&f.test(l):l=>!(typeof l=="string"&&f.test(l))}const t=s.toLowerCase();return e==="="?f=>typeof f=="string"&&f.toLowerCase()===t:f=>!(typeof f=="string"&&f.toLowerCase()===t)}return t=>{if(typeof t!="string")return!1;const f=$(t,s);return e==="<"?f<0:e==="<="?f<=0:e===">"?f>0:f>=0}}function W(n,r){const o=n.toArr(r),e=[];for(let s=0;s<o.rows;s++){const i=[];for(let c=0;c<o.cols;c++){const t=o.get(s,c);if(typeof t!="number")throw t instanceof u?t:a.VALUE;i.push(t)}e.push(i)}return e}export{p as Arr,a as ERR,h as OMITTED,w as Ref,y as RefList,u as XlError,V as bool,O as collectNumbers,k as collectNumbersA,d as eachValue,N as flat,T as hasWildcards,M as liftReferenceArgs,I as makeCriteria,W as matrixOf,b as num,D as pairedNumbers,S as scalarOf,g as str,F as toolpakScalar,U as wildcardRegex};
1
+ import{Arr as p,bool as $,compareText as C,compareValues as w,ERR as u,num as b,OMITTED as h,Ref as m,RefArray as d,RefList as y,RichValue as L,str as S,textToNumber as R,XlError as l}from"../values.js";function x(t,r,s){if(r instanceof m)t.scan(r,s);else if(r instanceof y)for(const e of r.refs)t.scan(e,s);else if(r instanceof d)x(t,r.data[0],s);else if(r instanceof p)for(const e of r.data)s(e);else r!==h&&r!==void 0&&s(r)}function O(t,r){let s=0,e=0;for(const i of t)i instanceof d&&(s=Math.max(s,i.rows),e=Math.max(e,i.cols));if(s===0)return r(t);const o=new Array(s*e);for(let i=0;i<s;i++)for(let c=0;c<e;c++){const n=t.map(f=>{if(!(f instanceof d))return f;const a=f.rows===1?0:i,E=f.cols===1?0:c;return a<f.rows&&E<f.cols?f.get(a,E):u.NA});try{o[i*e+c]=r(n)}catch(f){if(!(f instanceof l))throw f;o[i*e+c]=f}}return new p(s,e,o)}function g(t,r){const s=[];for(const e of r)e instanceof m||e instanceof y||e instanceof p?x(t,e,o=>{if(typeof o=="number")s.push(o);else{if(o instanceof l)throw o;if(o instanceof L)throw u.VALUE}}):e!==h&&e!==void 0&&e!==null?s.push(b(e)):(e===h||e===null)&&s.push(0);return s}function B(t,r,s=!1){const e=[];for(const o of r)if(o instanceof m||o instanceof y||o instanceof p){const i=!(o instanceof p);x(t,o,c=>{if(typeof c=="number")e.push(c);else if(typeof c=="boolean"&&i)e.push(c?1:0);else if(typeof c=="string")(i||s)&&e.push(0);else{if(c instanceof l)throw c;if(c instanceof L)throw u.VALUE}})}else o===h?e.push(0):o!=null&&e.push(b(o));return e}function U(t,r){if(r instanceof m&&r.isCell&&t.read(r.sheet,r.r0,r.c0)===null)throw u.VALUE}function V(t,r){return t.toArr(r).data}function D(t,r,s){U(t,r),U(t,s);const e=V(t,r),o=V(t,s);if(e.length!==o.length)throw u.NA;const i=[],c=[];for(let n=0;n<e.length;n++){if(e[n]instanceof l)throw e[n];if(o[n]instanceof l)throw o[n];typeof e[n]=="number"&&typeof o[n]=="number"&&(i.push(e[n]),c.push(o[n]))}return[i,c]}function T(t,r,s,e){const o=t.scalar(r,s,e);if(o instanceof l)throw o;return o}function F(t,r,s,e){if(r instanceof y||!t.legacy&&r instanceof m&&(r.r0!==r.r1||r.c0!==r.c1))throw u.VALUE;const o=T(t,r,s,e);if(typeof o=="boolean")throw u.VALUE;return o}function N(t,r=!1){let s="^";for(let e=0;e<t.length;e++){const o=t[e];o==="~"&&(r||t[e+1]==="*"||t[e+1]==="?"||t[e+1]==="~")?s+=(t[++e]??"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):o==="*"?s+="[\\s\\S]*":o==="?"?s+="[\\s\\S]":s+=o.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}return new RegExp(`${s}$`,"i")}const k=t=>/[*?~]/.test(t);function A(t,r){const s=typeof t=="string"?R(t):t;return typeof s=="number"&&w(s,r)===0}function I(t){if(t instanceof l)return n=>n===t;if(typeof t=="number")return n=>A(n,t);if(typeof t=="boolean")return n=>n===t;if(t==null||t===h)return n=>n===0||n===null;const r=String(t),s=/^(<=|>=|<>|<|>|=)([\s\S]*)$/.exec(r),e=s?s[1]:"=",o=s?s[2]:r,i=R(o);if(i!==null)switch(e){case"=":return n=>A(n,i);case"<>":return n=>!A(n,i);case"<":return n=>typeof n=="number"&&w(n,i)<0;case"<=":return n=>typeof n=="number"&&w(n,i)<=0;case">":return n=>typeof n=="number"&&w(n,i)>0;default:return n=>typeof n=="number"&&w(n,i)>=0}const c=o.toUpperCase();if(c==="TRUE"||c==="FALSE"){const n=c==="TRUE";return e==="<>"?f=>f!==n:f=>f===n}if(e==="="||e==="<>"){if(o==="")return e==="="?f=>f===null:f=>f!==null;if(k(o)){const f=N(o);return e==="="?a=>typeof a=="string"&&f.test(a):a=>!(typeof a=="string"&&f.test(a))}const n=o.toLowerCase();return e==="="?f=>typeof f=="string"&&f.toLowerCase()===n:f=>!(typeof f=="string"&&f.toLowerCase()===n)}return n=>{if(typeof n!="string")return!1;const f=C(n,o);return e==="<"?f<0:e==="<="?f<=0:e===">"?f>0:f>=0}}function W(t,r){const s=t.toArr(r),e=[];for(let o=0;o<s.rows;o++){const i=[];for(let c=0;c<s.cols;c++){const n=s.get(o,c);if(typeof n!="number")throw n instanceof l?n:u.VALUE;i.push(n)}e.push(i)}return e}export{p as Arr,u as ERR,h as OMITTED,m as Ref,y as RefList,l as XlError,$ as bool,g as collectNumbers,B as collectNumbersA,x as eachValue,V as flat,k as hasWildcards,O as liftReferenceArgs,I as makeCriteria,W as matrixOf,b as num,D as pairedNumbers,T as scalarOf,S as str,F as toolpakScalar,N as wildcardRegex};
@@ -1,7 +1,8 @@
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
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 O}from"./registry.js";const C={SUMIF:[1],SUMIFS:E=>E>=2&&E%2===0,VLOOKUP:[0,2,3],HLOOKUP:[0,2,3],LOOKUP:[0],XLOOKUP:[0,4,5],XMATCH:[0,2,3],ADDRESS:[0,1,2,3,4],INDIRECT:[0,1],RANK:[2],"RANK.EQ":[2],"RANK.AVG":[2],TRIMMEAN:[1],PROB:[2,3],"T.TEST":[2,3],TTEST:[2,3],"Z.TEST":[2],ZTEST:[2],TEXTJOIN:[1],TEXTBEFORE:[0,2,3,4],TEXTAFTER:[0,2,3,4],TEXTSPLIT:[0],REGEXEXTRACT:[0,1,2,3],ARRAYTOTEXT:[1],NPV:[0],IRR:[1],MIRR:[1,2],WRAPROWS:[1],WRAPCOLS:[1],EXPAND:[1,2],HYPERLINK:[0],SUBTOTAL:[0]};for(const E of["DSUM","DAVERAGE","DCOUNT","DCOUNTA","DMAX","DMIN","DPRODUCT","DSTDEV","DSTDEVP","DVAR","DVARP","DGET"])C[E]=[1];const A={SERIESSUM:[0,1,2],XNPV:[0],XIRR:[2],FVSCHEDULE:[0],NETWORKDAYS:[0,1],WORKDAY:[0,1],"NETWORKDAYS.INTL":[0,1],"WORKDAY.INTL":[0,1],T:[0],AGGREGATE:[0,1,3]};for(const[E,I]of[...Object.entries(C),...Object.entries(A)]){const R=O.get(E);!R||R.kind!=="raw"||(R.scalars=typeof I=="function"?I:T=>I.includes(T),R.scalarRanges=E in C)}for(const E of["VLOOKUP","HLOOKUP"])O.get(E).shapeFrom=0;for(const E of["NETWORKDAYS.INTL","WORKDAY.INTL"]){const I=O.get(E),R=I.scalars;I.scalars=T=>R(T)||T===2,I.rangeAt=T=>T===2}const D=`ACCRINT ACCRINTM AMORDEGRC AMORLINC BESSELI BESSELJ BESSELK BESSELY BIN2DEC BIN2HEX BIN2OCT COMPLEX
2
2
  CONVERT COUPDAYBS COUPDAYS COUPDAYSNC COUPNCD COUPNUM COUPPCD CUMIPMT CUMPRINC DEC2BIN DEC2HEX DEC2OCT DELTA DISC
3
3
  DOLLARDE DOLLARFR DURATION EDATE EFFECT EOMONTH ERF ERF.PRECISE ERFC ERFC.PRECISE FACTDOUBLE GESTEP HEX2BIN HEX2DEC
4
4
  HEX2OCT IMABS IMAGINARY IMARGUMENT IMCONJUGATE IMCOS IMCOSH IMCOT IMCSC IMCSCH IMDIV IMEXP IMLN IMLOG10 IMLOG2
5
5
  IMPOWER IMREAL IMSEC IMSECH IMSIN IMSINH IMSQRT IMSUB IMTAN INTRATE ISEVEN ISODD MDURATION MROUND NOMINAL OCT2BIN
6
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]??[]})}
7
+ YIELDDISC YIELDMAT ODDFPRICE ODDFYIELD ODDLPRICE ODDLYIELD`.split(/\s+/),S={ACCRINT:[7]},N=`SERIESSUM GCD LCM MULTINOMIAL XNPV XIRR FVSCHEDULE NETWORKDAYS NETWORKDAYS.INTL WORKDAY WORKDAY.INTL
8
+ IMSUM IMPRODUCT`.split(/\s+/);for(const E of[...D,...N]){const I=O.get(E);I&&(I.required=E==="ERF"?2:I.min)}for(const E of D){const I=O.get(E);I?.kind==="lift"&&(I.toolpak={logical:S[E]??[]})}
@@ -1 +1 @@
1
- import{setCategory as D}from"./registry.js";import{lift as l,raw as m}from"./registry.js";import{Arr as S,ERR as o,Lambda as w,num as p,OMITTED as d,Ref as i,RefList as E,RichValue as N,XlError as y}from"../values.js";import{CONTENT_MASK as I,FORMULA as R}from"../../core/sheet.js";import{cellRef as b,colName as g,sheetPrefix as k}from"../../core/address.js";import{newBookName as T}from"../../core/default-names.js";import{str as L}from"../values.js";D("Information");const s=(e,t)=>l(e,1,1,t,{errors:!0});s("ISERROR",e=>e instanceof y),s("ISERR",e=>e instanceof y&&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),l("ISEVEN",1,1,e=>Math.trunc(p(e))%2===0),l("ISODD",1,1,e=>Math.trunc(p(e))%2!==0),m("ISREF",1,1,(e,t)=>t[0]instanceof i||t[0]instanceof E),m("ISFORMULA",1,1,(e,t)=>{const r=t[0];if(!(r instanceof i))throw o.VALUE;return(r.sheet.kind(r.r0,r.c0)&R)!==0}),l("N",1,1,e=>{if(typeof e=="number")return e;if(typeof e=="boolean")return e?1:0;if(e instanceof N)throw o.VALUE;return 0}),l("NA",0,0,()=>o.NA),s("ERROR.TYPE",e=>{if(!(e instanceof y))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]}),m("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 E||r instanceof S?64:r===null||typeof r=="number"?1:typeof r=="string"?2:typeof r=="boolean"?4:r instanceof y?16:r instanceof w||r instanceof N?128:1}),m("SHEET",0,1,(e,t,r,f,a)=>{if(t.length===0||t[0]===d)return e.book.sheets.indexOf(a)+1;if(t[0]instanceof i)return e.book.sheets.indexOf(t[0].sheet)+1;if(typeof t[0]=="string"){const u=e.sheetByName(t[0]);if(!u)throw o.NA;return e.book.sheets.indexOf(u)+1}throw o.VALUE},{readsSheets:!0}),m("SHEETS",0,1,(e,t)=>{if(t.length===0||t[0]===d)return e.book.sheets.length;if(t[0]instanceof E)return new Set(t[0].refs.map(r=>r.sheet)).size;if(t[0]instanceof i)return 1;throw o.REF},{readsSheets:!0}),m("INFO",1,1,(e,t,r,f)=>{switch(L(e.scalar(t[0],r,f)).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}),m("CELL",1,2,(e,t,r,f,a)=>{const u=L(e.scalar(t[0],r,f)).toLowerCase(),n=t[1]===void 0||t[1]===d?new i(a,r,f,r,f):t[1];if(n instanceof y)throw n;if(!(n instanceof i))throw o.VALUE;if(u==="filename")return e.book.fileName?`[${e.book.fileName}]${n.sheet.name}`:"";const c=e.read(n.sheet,n.r0,n.c0),h=e.book.styles.get(n.sheet.style(n.r0,n.c0));switch(u){case"address":{const A=`$${g(n.c0)}$${n.r0+1}`;return n.sheet===a?A:`${k(n.sheet.name,{book:e.book.fileName??T()})}${A}`}case"col":return n.c0+1;case"row":return n.r0+1;case"contents":return c===null?0:c;case"type":return c===null?"b":typeof c=="string"?"l":"v";case"width":return Math.max(0,Math.round((n.sheet.cols.sizeOf(n.c0)-5)/6));case"color":return/\[red\]/i.test(h.numFmt)?1:0;case"format":return O(h.numFmt);case"parentheses":return/\(/.test(h.numFmt)?1:0;case"prefix":return typeof c=="string"?h.hAlign==="right"?'"':h.hAlign==="center"?"^":"'":"";case"protect":return 1;default:throw o.VALUE}});const F=new Map([["d-mmm-yy","D1"],["dd-mmm-yy","D1"],["d-mmm","D2"],["dd-mmm","D2"],["mmm-yy","D3"],["m/d/yy","D4"],["m/d/yyyy","D4"],["mm/dd/yy","D4"],["m/d/yy h:mm","D4"],["m/d/yyyy h:mm","D4"],["mm/dd","D5"],["h:mm:ss am/pm","D6"],["h:mm am/pm","D7"],["h:mm:ss","D8"],["h:mm","D9"]]);function O(e){if(e==="General")return"G";if(e==="0")return"F0";if(e==="#,##0")return",0";if(e==="0.00")return"F2";if(e==="#,##0.00")return",2";if(/%/.test(e))return`P${(/\.(0+)/.exec(e)?.[1]??"").length}`;if(/E\+/i.test(e))return"S2";const t=F.get(e.toLowerCase());return t||(/[ymd]/i.test(e)?"D1":"G")}export{I as CONTENT_MASK,b as cellRef};
1
+ import{setCategory as D}from"./registry.js";import{lift as y,raw as m}from"./registry.js";import{Arr as N,ERR as o,Lambda as S,num as p,OMITTED as d,Ref as i,RefList as A,RichValue as w,XlError as f}from"../values.js";import{CONTENT_MASK as I,FORMULA as R}from"../../core/sheet.js";import{cellRef as b,colName as g,sheetPrefix as k}from"../../core/address.js";import{newBookName as T}from"../../core/default-names.js";import{str as L}from"../values.js";D("Information");const s=(e,t)=>y(e,1,1,t,{errors:!0});s("ISERROR",e=>e instanceof f),s("ISERR",e=>e instanceof f&&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),y("ISEVEN",1,1,e=>Math.trunc(p(e))%2===0),y("ISODD",1,1,e=>Math.trunc(p(e))%2!==0),m("ISREF",1,1,(e,t)=>t[0]instanceof i||t[0]instanceof A),m("ISFORMULA",1,1,(e,t)=>{const r=t[0];if(r instanceof f)throw r;if(!(r instanceof i))throw o.VALUE;return(r.sheet.kind(r.r0,r.c0)&R)!==0}),y("N",1,1,e=>{if(typeof e=="number")return e;if(typeof e=="boolean")return e?1:0;if(e instanceof w)throw o.VALUE;return 0}),y("NA",0,0,()=>o.NA),s("ERROR.TYPE",e=>{if(!(e instanceof f))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]}),m("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 A||r instanceof N?64:r===null||typeof r=="number"?1:typeof r=="string"?2:typeof r=="boolean"?4:r instanceof f?16:r instanceof S||r instanceof w?128:1}),m("SHEET",0,1,(e,t,r,u,a)=>{if(t.length===0||t[0]===d)return e.book.sheets.indexOf(a)+1;if(t[0]instanceof f)throw t[0];if(t[0]instanceof N)throw o.NA;if(t[0]instanceof i)return e.book.sheets.indexOf(t[0].sheet)+1;if(typeof t[0]=="string"){const c=e.sheetByName(t[0]);if(!c)throw o.NA;return e.book.sheets.indexOf(c)+1}throw o.VALUE},{readsSheets:!0}),m("SHEETS",0,1,(e,t)=>{if(t.length===0||t[0]===d)return e.book.sheets.length;if(t[0]instanceof A)return new Set(t[0].refs.map(r=>r.sheet)).size;if(t[0]instanceof i)return 1;throw t[0]instanceof f?t[0]:o.NA},{readsSheets:!0}),m("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}),m("CELL",1,2,(e,t,r,u,a)=>{const c=L(e.scalar(t[0],r,u)).toLowerCase(),n=t[1]===void 0||t[1]===d?new i(a,r,u,r,u):t[1];if(n instanceof f)throw n;if(!(n instanceof i))throw o.VALUE;if(c==="filename")return e.book.fileName?`[${e.book.fileName}]${n.sheet.name}`:"";const h=e.read(n.sheet,n.r0,n.c0),l=e.book.styles.get(n.sheet.style(n.r0,n.c0));switch(c){case"address":{const E=`$${g(n.c0)}$${n.r0+1}`;return n.sheet===a?E:`${k(n.sheet.name,{book:e.book.fileName??T()})}${E}`}case"col":return n.c0+1;case"row":return n.r0+1;case"contents":return h===null?0:h;case"type":return h===null?"b":typeof h=="string"?"l":"v";case"width":return Math.max(0,Math.round((n.sheet.cols.sizeOf(n.c0)-5)/6));case"color":return/\[red\]/i.test(l.numFmt)?1:0;case"format":return C(l.numFmt);case"parentheses":return/\(/.test(l.numFmt)?1:0;case"prefix":return typeof h=="string"?l.hAlign==="right"?'"':l.hAlign==="center"?"^":"'":"";case"protect":return 1;default:throw o.VALUE}});const O=new Map([["d-mmm-yy","D1"],["dd-mmm-yy","D1"],["d-mmm","D2"],["dd-mmm","D2"],["mmm-yy","D3"],["m/d/yy","D4"],["m/d/yyyy","D4"],["mm/dd/yy","D4"],["m/d/yy h:mm","D4"],["m/d/yyyy h:mm","D4"],["mm/dd","D5"],["h:mm:ss am/pm","D6"],["h:mm am/pm","D7"],["h:mm:ss","D8"],["h:mm","D9"]]);function C(e){if(e==="General")return"G";if(e==="0")return"F0";if(e==="#,##0")return",0";if(e==="0.00")return"F2";if(e==="#,##0.00")return",2";if(/%/.test(e))return`P${(/\.(0+)/.exec(e)?.[1]??"").length}`;if(/E\+/i.test(e))return"S2";const t=O.get(e.toLowerCase());return t||(/[ymd]/i.test(e)?"D1":"G")}export{I as CONTENT_MASK,b as cellRef};
@@ -1 +1 @@
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}),m("OR",1,255,(n,t)=>{let r=!1;return C(n,t,o=>{o&&(r=!0)}),r}),m("XOR",1,255,(n,t)=>{let r=!1;return C(n,t,o=>{o&&(r=!r)}),r},{array:!0});const h=(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=h(n,a,s,e),u=h(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,w,M)=>{if(!(A instanceof l))return A;const y=A.rows===1?0:w,S=A.cols===1?0:M;return y<A.rows&&S<A.cols?A.get(y,S):d.NA},F=new Array(O*L);for(let A=0;A<O;A++)for(let w=0;w<L;w++){const M=U(t,A,w);let y;if(M instanceof p)y=M;else try{y=T(M)?U(c,A,w):U(u,A,w)}catch(S){y=S}F[A*L+w]=y===R?0:y}return new l(O,L,F)}N("IF",2,3,(n,t,r,o,s)=>{const e=h(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=h(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=h(n,t[0](r,o,s),r,o);if(e instanceof l){const f=h(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=h(n,t[0](r,o,s),r,o);if(e instanceof l){const f=h(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=h(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},{refResult:!0,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);
1
+ import{setCategory as Y}from"./registry.js";import{lazy as g,lift as U,raw as p}from"./registry.js";import{Arr as l,bool as S,ERR as h,isOmitted as D,Lambda as W,OMITTED as E,Ref as C,RefList as B,XlError as m}from"../values.js";import{compareValues as X}from"../values.js";import{eachValue as $}from"./helpers.js";Y("Logical");for(const t of["LET","LAMBDA"])g(t,0,255,()=>{throw new Error(`${t} compiles directly`)});U("TRUE",0,0,()=>!0),U("FALSE",0,0,()=>!1),U("NOT",1,1,t=>!S(t));function F(t,n,r){let o=!1;for(const s of n)if(s instanceof C||s instanceof B||s instanceof l)$(t,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!==E&&s!==null?(o=!0,r(S(s))):s===E&&(o=!0,r(!1)))}if(!o)throw h.VALUE}p("AND",1,255,(t,n)=>{let r=!0;return F(t,n,o=>{o||(r=!1)}),r}),p("OR",1,255,(t,n)=>{let r=!1;return F(t,n,o=>{o&&(r=!0)}),r}),p("XOR",1,255,(t,n)=>{let r=!1;return F(t,n,o=>{o&&(r=!r)}),r},{array:!0});const d=(t,n,r,o)=>n instanceof C?n.isCell?t.read(n.sheet,n.r0,n.c0):t.refAuto(n,r,o):n instanceof B?h.VALUE:n;function V(t){if(t instanceof m)throw t;return S(t)}function z(t,n,r,o,s,e,c){const i=r?r(s,e,c):!0,a=o?o(s,e,c):!1,f=d(t,i,s,e),u=d(t,a,s,e),w=Math.max(n.rows,f instanceof l?f.rows:1,u instanceof l?u.rows:1),y=Math.max(n.cols,f instanceof l?f.cols:1,u instanceof l?u.cols:1),T=(A,R,I)=>{if(!(A instanceof l))return A;const N=A.rows===1?0:R,O=A.cols===1?0:I;return N<A.rows&&O<A.cols?A.get(N,O):h.NA},b=new Array(w*y);for(let A=0;A<w;A++)for(let R=0;R<y;R++){const I=T(n,A,R);let N;if(I instanceof m)N=I;else try{N=S(I)?T(f,A,R):T(u,A,R)}catch(O){N=O}b[A*y+R]=N===E?0:N}return new l(w,y,b)}g("IF",2,3,(t,n,r,o,s)=>{const e=d(t,n[0](r,o,s),r,o);if(e instanceof l)return z(t,e,n[1],n[2],r,o,s);if(V(e)){const i=n[1](r,o,s);return i===E?0:i}if(n.length<3)return!1;const c=n[2](r,o,s);return c===E?0:c},{passes:t=>t>0});const H=t=>t===E?0:t;function k(t,n){const r=Math.max(...t.map(e=>e instanceof l?e.rows:1)),o=Math.max(...t.map(e=>e instanceof l?e.cols:1)),s=new Array(r*o);for(let e=0;e<r;e++)for(let c=0;c<o;c++){const i=t.map(f=>{if(!(f instanceof l))return f;const u=f.rows===1?0:e,w=f.cols===1?0:c;return u<f.rows&&w<f.cols?f.get(u,w):h.NA});let a;try{a=n(i)}catch(f){if(!(f instanceof m))throw f;a=f}s[e*o+c]=a===E||a===null?0:a}return new l(r,o,s)}function K(t){for(let n=0;n<t.length;n+=2)if(V(t[n]))return t[n+1];throw h.NA}g("IFS",2,254,(t,n,r,o,s)=>{if(n.length%2!==0)throw h.NA;const e=[];for(let c=0;c<n.length;c+=2){const i=d(t,n[c](r,o,s),r,o);if(i instanceof l){e[c]=i;for(let a=c+1;a<n.length;a++)e[a]=d(t,n[a](r,o,s),r,o);return k(e.slice(c),K)}if(V(i))return H(n[c+1](r,o,s))}throw h.NA},{passes:t=>t%2===1}),g("IFERROR",2,2,(t,n,r,o,s)=>{let e=d(t,n[0](r,o,s),r,o);if(e instanceof l){const c=d(t,n[1](r,o,s),r,o);return k([e,c],([i,a])=>i instanceof m?a:i)}return e instanceof m&&(e=n[1](r,o,s)),e===E?0:e},{passes:()=>!0}),g("IFNA",2,2,(t,n,r,o,s)=>{const e=d(t,n[0](r,o,s),r,o);if(e instanceof l){const c=d(t,n[1](r,o,s),r,o);return k([e,c],([i,a])=>i===h.NA?a:i)}return e===h.NA?n[1](r,o,s):e},{passes:()=>!0});const P=(t,n)=>typeof t==typeof n&&X(t,n)===0;function j(t){if(t[0]instanceof m)throw t[0];let n=1;for(;n+1<t.length;n+=2){if(t[n]instanceof m)throw t[n];if(P(t[n],t[0]))return t[n+1]}if(n<t.length)return t[n];throw h.NA}g("SWITCH",3,255,(t,n,r,o,s)=>{const e=u=>d(t,u(r,o,s),r,o),c=u=>k(n.map((w,y)=>y in u?u[y]:e(w)),j),i=e(n[0]);if(i instanceof l)return c([i]);if(i instanceof m)throw i;const a=[i];let f=1;for(;f+1<n.length;f+=2){const u=e(n[f]);if(u instanceof l)return a[f]=u,c(a);if(u instanceof m)throw u;if(P(u,i))return n[f+1](r,o,s)}if(f<n.length)return n[f](r,o,s);throw h.NA},{refResult:!0,passes:(t,n)=>t>1&&(t%2===0||t===n-1)});const M=t=>{if(t instanceof m)throw t;if(!(t instanceof W))throw h.VALUE;return t},L=(t,n,r,o)=>{const s=n instanceof C?t.refAuto(n,r,o):n;return s instanceof l?s.data[0]:s};p("MAP",2,255,(t,n,r,o,s)=>{const e=M(n[n.length-1]),c=n.slice(0,-1).map(u=>t.toArr(u)),{rows:i,cols:a}=c[0],f=new Array(i*a);for(let u=0;u<f.length;u++){const w=t.invoke(e,c.map(y=>y.data[u]??h.NA),r,o,s);f[u]=L(t,w,r,o)}return new l(i,a,f)}),p("REDUCE",3,3,(t,n,r,o,s)=>{const e=!D(n[0]),c=M(n[2]),i=t.toArr(n[1]).data;let a=e?n[0]:i[0];for(let f=e?0:1;f<i.length;f++)a=t.invoke(c,[a,i[f]],r,o,s);return a}),p("SCAN",3,3,(t,n,r,o,s)=>{const e=!D(n[0]),c=M(n[2]),i=t.toArr(n[1]);let a=e?L(t,n[0],r,o):i.data[0];const f=new Array(i.data.length);for(let u=0;u<i.data.length;u++){if(u===0&&!e){f[0]=a;continue}a=L(t,t.invoke(c,[a,i.data[u]],r,o,s),r,o),f[u]=a}return new l(i.rows,i.cols,f)}),p("BYROW",2,2,(t,n,r,o,s)=>{const e=t.toArr(n[0]),c=M(n[1]),i=new Array(e.rows);for(let a=0;a<e.rows;a++){const f=new l(1,e.cols,e.data.slice(a*e.cols,(a+1)*e.cols));i[a]=L(t,t.invoke(c,[f],r,o,s),r,o)}return new l(e.rows,1,i)}),p("BYCOL",2,2,(t,n,r,o,s)=>{const e=t.toArr(n[0]),c=M(n[1]),i=new Array(e.cols);for(let a=0;a<e.cols;a++){const f=new Array(e.rows);for(let u=0;u<e.rows;u++)f[u]=e.get(u,a);i[a]=L(t,t.invoke(c,[new l(e.rows,1,f)],r,o,s),r,o)}return new l(1,e.cols,i)}),p("MAKEARRAY",3,3,(t,n,r,o,s)=>{const e=Math.trunc(L(t,n[0],r,o)),c=Math.trunc(L(t,n[1],r,o)),i=M(n[2]);if(!(e>=1)||!(c>=1))throw h.VALUE;const a=new Array(e*c);for(let f=0;f<e;f++)for(let u=0;u<c;u++)a[f*c+u]=L(t,t.invoke(i,[f+1,u+1],r,o,s),r,o);return new l(e,c,a)}),p("ISOMITTED",1,1,(t,n)=>n[0]===void 0||n[0]===E);
@@ -1 +1 @@
1
- import{setCategory as Y}from"./registry.js";import{lazy as q,raw as M}from"./registry.js";import{Arr as p,bool as J,compareValues as b,ERR as A,num as E,OMITTED as Q,Ref as L,RefArray as Z,RefList as T,str as I,XlError as N}from"../values.js";import{FormulaSyntaxError as v,tokenize as K}from"../parser.js";import{hasWildcards as x,scalarOf as F,wildcardRegex as tt}from"./helpers.js";import{colName as nt,MAX_COLS as P,MAX_ROWS as D,parseRangeRef as rt,sheetPrefix as z}from"../../core/address.js";import{FORMULA as et,SPILL as st}from"../../core/sheet.js";Y("Lookup & Reference");const ot=64,R=t=>t===void 0||t===Q;function U(t,n){if(n instanceof N)throw n;if(n instanceof L){const r=t.trim(n),i=n.cols===1,s=n.rows===1,c=i?n.rows:s?n.cols:n.rows*n.cols;if(i)return{ref:n,length:c,usedLength:r.r1-n.r0+1,at:o=>t.read(n.sheet,n.r0+o,n.c0)};if(s)return{ref:n,length:c,usedLength:r.c1-n.c0+1,at:o=>t.read(n.sheet,n.r0,n.c0+o)};const e=t.toArr(n);return{length:e.data.length,usedLength:e.data.length,at:o=>e.data[o]}}return n instanceof p?{length:n.data.length,usedLength:n.data.length,at:r=>n.data[r]}:{length:1,usedLength:1,at:()=>n}}const C=(t,n)=>typeof t==typeof n;function V(t,n,r,i,s=!1){if(r==null&&(r=0),typeof r=="string"&&i&&x(r)){const c=tt(r);if(s)for(let e=n.usedLength-1;e>=0;e--){const o=n.at(e);if(typeof o=="string"&&c.test(o))return e}else for(let e=0;e<n.usedLength;e++){const o=n.at(e);if(typeof o=="string"&&c.test(o))return e}return-1}if(!s&&n.ref&&n.usedLength>ot){const c=t.exactIndex(n.ref),e=typeof r=="string"?`s${r.toLowerCase()}`:typeof r=="number"?r:`b${r}`,o=c.get(e);return o===void 0?-1:o}if(s){for(let c=n.usedLength-1;c>=0;c--){const e=n.at(c);if(e!==null&&C(e,r)&&b(e,r)===0)return c}return-1}for(let c=0;c<n.usedLength;c++){const e=n.at(c);if(e!==null&&C(e,r)&&b(e,r)===0)return c}return-1}function H(t,n,r=b){let i=0,s=t.usedLength-1,c=-1;for(;i<=s;){let e=i+s>>>1,o=e;for(;o<=s;){const d=t.at(o);if(d!==null&&C(d,n))break;o++}if(o>s){s=e-1;continue}e=o;const h=r(t.at(e),n);if(h===0){let d=e;for(i=e+1;i<=s;){const f=i+s>>>1,u=t.at(f);u!==null&&C(u,n)&&r(u,n)===0?(d=f,i=f+1):s=f-1}return d}h<0?(c=e,i=e+1):s=e-1}return c}function ct(t,n){let r=-1;for(let i=0;i<t.usedLength;i++){const s=t.at(i);if(s===null||!C(s,n))continue;const c=b(s,n);if(c===0)return i;if(c<0)break;r=i}return r}M("MATCH",2,3,(t,n,r,i,s,c)=>S(t,n,[0,2],e=>{const o=F(t,e[0],r,i),h=e[2]===void 0?1:E(t.scalar(e[2],r,i));if(!(e[1]instanceof L||e[1]instanceof p||e[1]instanceof T))throw E(F(t,e[1],r,i)),A.NA;const d=e[1]instanceof L&&!(e[1].rows===1||e[1].cols===1)?null:e[1];if(d===null)throw A.NA;if(d instanceof p&&d.rows>1&&d.cols>1)throw A.NA;const f=U(t,d);let u;if(h===0?u=V(t,f,o,!0):h>0?u=H(f,o===null?0:o):u=ct(f,o===null?0:o),u<0)throw A.NA;return u+1},c),{array:!0});function it(t,n){return typeof t=="number"&&typeof n=="number"?t===n?0:t<n?-1:1:b(t,n)}function W(t,n,r,i,s,c,e,o){const h=Math.trunc(E(t.scalar(i,c,e)));if(typeof s=="string")throw A.VALUE;const d=s===void 0||J(s),f=n===null?0:n;let u,a,w;if(r instanceof L)w=o?r.c1-r.c0+1:r.r1-r.r0+1,u=U(t,o?new L(r.sheet,r.r0,r.c0,r.r1,r.c0):new L(r.sheet,r.r0,r.c0,r.r0,r.c1)),a=m=>o?t.read(r.sheet,r.r0+m,r.c0+h-1):t.read(r.sheet,r.r0+h-1,r.c0+m);else{if(!(r instanceof p))throw E(r),A.NA;const m=r,y=o?m.rows:m.cols;w=o?m.cols:m.rows,u={length:y,usedLength:y,at:O=>o?m.get(O,0):m.get(0,O)},a=O=>o?m.get(O,h-1):m.get(h-1,O)}const l=d?H(u,f,it):V(t,u,f,!0);if(l<0)throw A.NA;if(h<1)throw A.VALUE;if(h>w)throw A.REF;const g=a(l);return g===null?0:g}const _=t=>t instanceof L&&(t.r0!==t.r1||t.c0!==t.c1)||t instanceof p&&t.data.length>1;function S(t,n,r,i,s=!1,c=!1){if(t.legacy&&!s)return i(n);const e=r.map(f=>_(n[f])?t.toArr(n[f]):null);if(e.every(f=>f===null))return i(n);const o=Math.max(...e.map(f=>f?.rows??1)),h=Math.max(...e.map(f=>f?.cols??1)),d=new Array(o*h);for(let f=0;f<o;f++)for(let u=0;u<h;u++){const a=n.slice();let w=!1;e.forEach((g,m)=>{g&&(g.rows!==1&&f>=g.rows||g.cols!==1&&u>=g.cols?w=!0:a[r[m]]=g.get(g.rows===1?0:f,g.cols===1?0:u))});let l;if(w)l=A.NA;else try{l=i(a)}catch(g){if(!(g instanceof N))throw g;l=g}l instanceof L&&!c?l=t.read(l.sheet,l.r0,l.c0):l instanceof p&&(l=l.data[0]),d[f*h+u]=l===null?0:l}return c?new Z(o,h,d):new p(o,h,d)}function B(t,n,r,i,s){if(n[1]instanceof N)throw n[1];const c=n[1];if(t.legacy||!_(n[0])&&!_(n[3])){const w=R(n[3])?n[3]:F(t,n[3],r,i);return W(t,F(t,n[0],r,i),c,n[2],w,r,i,s)}const e=t.toArr(n[0]),o=R(n[3])?null:t.toArr(n[3]),h=Math.max(e.rows,o?.rows??1),d=Math.max(e.cols,o?.cols??1),f=(w,l,g)=>w.get(w.rows===1?0:l,w.cols===1?0:g),u=(w,l,g)=>(w.rows===1||l<w.rows)&&(w.cols===1||g<w.cols),a=new Array(h*d);for(let w=0;w<h;w++)for(let l=0;l<d;l++){if(!u(e,w,l)||o&&!u(o,w,l)){a[w*d+l]=A.NA;continue}try{a[w*d+l]=W(t,f(e,w,l),c,n[2],o?f(o,w,l):n[3],r,i,s)}catch(g){if(!(g instanceof N))throw g;a[w*d+l]=g}}return new p(h,d,a)}M("VLOOKUP",3,4,(t,n,r,i)=>B(t,n,r,i,!0),{array:!0}),M("HLOOKUP",3,4,(t,n,r,i)=>B(t,n,r,i,!1),{array:!0}),M("LOOKUP",2,3,(t,n,r,i)=>{const s=F(t,n[0],r,i),c=s===null?0:s;let e=n[1],o=R(n[2])?null:n[2];if(o===null){const u=t.toArr(e);u.rows>=u.cols?(e=new p(u.rows,1,Array.from({length:u.rows},(a,w)=>u.get(w,0))),o=new p(u.rows,1,Array.from({length:u.rows},(a,w)=>u.get(w,u.cols-1)))):(e=new p(1,u.cols,Array.from({length:u.cols},(a,w)=>u.get(0,w))),o=new p(1,u.cols,Array.from({length:u.cols},(a,w)=>u.get(u.rows-1,w))))}const h=U(t,e),d=H(h,c);if(d<0)throw A.NA;const f=U(t,o).at(d);return f===null?0:f},{array:!0});function j(t,n,r,i,s){const c=r===null?0:r;if(s===2||s===-2){const f=s===2;let u=0,a=n.usedLength-1,w=-1,l=-1,g=-1;for(;u<=a;){const m=u+a>>>1,y=b(n.at(m),c);if(y===0){w=m;break}(f?y<0:y>0)?(f?l=m:g=m,u=m+1):(f?g=m:l=m,a=m-1)}return w>=0?w:i===-1?l:i===1?g:-1}const e=s===-1;if(i===0)return V(t,n,c,!1,e);if(i===2)return V(t,n,c,!0,e);let o=-1,h=null;const d=n.usedLength;for(let f=0;f<d;f++){const u=e?d-1-f:f,a=n.at(u);if(a===null||!C(a,c))continue;const w=b(a,c);if(w===0)return u;(i===-1&&w<0&&(o<0||b(a,h)>0)||i===1&&w>0&&(o<0||b(a,h)<0))&&(o=u,h=a)}return o}function ft(t,n){const r=t.length;for(let i=0;i<r;i++){const s=n<0?r-1-i:i;if(t.at(s)===null)return s}return-1}M("XMATCH",2,4,(t,n,r,i)=>{const s=F(t,n[0],r,i),c=R(n[2])?0:Math.trunc(E(t.scalar(n[2],r,i))),e=R(n[3])?1:Math.trunc(E(t.scalar(n[3],r,i)));if(![0,-1,1,2].includes(c)||![1,-1,2,-2].includes(e))throw A.VALUE;const o=j(t,U(t,n[1]),s,c,e);if(o<0)throw A.NA;return o+1},{array:!0});const X=t=>t?.t==="cell"||t?.t==="range"&&t.a.c===t.b.c&&t.a.cAbs===t.b.cAbs||t?.t==="cols"&&t.c0===t.c1&&t.a0===t.a1,k=t=>t?.t==="cell"||t?.t==="range"&&t.a.r===t.b.r&&t.a.rAbs===t.b.rAbs||t?.t==="rows"&&t.r0===t.r1&&t.a0===t.a1;M("XLOOKUP",3,6,(t,n,r,i)=>S(t,n,[0],s=>{const c=F(t,s[0],r,i),e=R(s[4])?0:Math.trunc(E(t.scalar(s[4],r,i))),o=R(s[5])?1:Math.trunc(E(t.scalar(s[5],r,i)));if(![0,-1,1,2].includes(e)||![1,-1,2,-2].includes(o))throw A.VALUE;if(s[1]instanceof N)throw s[1];const h=s[1]instanceof L?s[1].rows:t.toArr(s[1]).rows,d=s[1]instanceof L?s[1].cols:t.toArr(s[1]).cols;if(h>1&&d>1)throw A.VALUE;if(s[2]instanceof N)throw s[2];const f=s[2]instanceof L?s[2].rows:t.toArr(s[2]).rows,u=s[2]instanceof L?s[2].cols:t.toArr(s[2]).cols;if(h*d>1&&(d===1?f!==h:u!==d))throw A.VALUE;const a=R(s[0])?ft(U(t,s[1]),o):j(t,U(t,s[1]),c,e,o);if(a<0){if(R(s[3]))throw A.NA;return s[3]}const w=d===1,l=s[2];if(l instanceof L)return w?new L(l.sheet,l.r0+a,l.c0,l.r0+a,l.c1):new L(l.sheet,l.r0,l.c0+a,l.r1,l.c0+a);const g=t.toArr(l);return w?g.cols===1?g.get(a,0):new p(1,g.cols,g.data.slice(a*g.cols,(a+1)*g.cols)):g.rows===1?g.get(0,a):new p(g.rows,1,Array.from({length:g.rows},(m,y)=>g.get(y,a)))}),{array:!0,refResult:([,t,n,r])=>r&&r.t!=="omit"&&r.t!=="lit"&&r.t!=="cell"?!0:X(t)?!X(n):!(k(t)&&k(n))});const G=t=>t?.t==="lit"&&typeof t.v=="number"&&Math.trunc(t.v)>0||t?.t==="call"&&(t.name==="MATCH"||t.name==="XMATCH");M("INDEX",2,4,(t,n,r,i)=>S(t,n,[1,2],s=>{let c=s[0];if(c instanceof N)throw c;const e=R(s[1])?0:Math.trunc(E(t.scalar(s[1],r,i))),o=R(s[2])?0:Math.trunc(E(t.scalar(s[2],r,i)));if(c instanceof T){const u=R(s[3])?1:Math.trunc(E(t.scalar(s[3],r,i)));if(u<1||u>c.refs.length)throw A.REF;c=c.refs[u-1]}if(e<0||o<0)throw A.VALUE;if(c instanceof L){let u=e,a=o;if((s.length===2||R(s[2]))&&c.rows===1&&c.cols>1&&(a=u,u=0),u>c.rows||a>c.cols)throw A.REF;const w=u===0?c.r0:c.r0+u-1,l=u===0?c.r1:w,g=a===0?c.c0:c.c0+a-1,m=a===0?c.c1:g;return new L(c.sheet,w,g,l,m)}const h=t.toArr(c);let d=e,f=o;if(s.length===2&&h.rows===1&&h.cols>1&&(f=d,d=0,f===0))return h;if(s.length===2&&h.cols===1&&(f=1),d>h.rows||f>h.cols)throw A.REF;return d===0&&f===0?h:d===0?new p(h.rows,1,Array.from({length:h.rows},(u,a)=>h.get(a,f-1))):f===0?new p(1,h.cols,h.data.slice((d-1)*h.cols,d*h.cols)):h.get(d-1,f-1)}),{array:t=>t===0,refResult:([t,n,r])=>!(G(n)&&(G(r)||(r===void 0||r.t==="omit")&&(X(t)||k(t))))}),M("OFFSET",3,5,(t,n,r,i,s,c)=>S(t,n,[1,2,3,4],e=>{const o=e[0];if(o instanceof N)throw o;if(!(o instanceof L))throw A.VALUE;const h=Math.trunc(E(t.scalar(e[1],r,i))),d=Math.trunc(E(t.scalar(e[2],r,i))),f=R(e[3])?o.rows:Math.trunc(E(t.scalar(e[3],r,i))),u=R(e[4])?o.cols:Math.trunc(E(t.scalar(e[4],r,i)));if(f===0||u===0)throw A.REF;const a=o.r0+h+(f<0?f+1:0),w=o.c0+d+(u<0?u+1:0),l=a+Math.abs(f)-1,g=w+Math.abs(u)-1;if(a<0||w<0||l>=D||g>=P)throw A.REF;return new L(o.sheet,a,w,l,g)},c,!0),{volatile:!0,refResult:!0}),M("INDIRECT",1,2,(t,n,r,i,s)=>{const c=I(F(t,n[0],r,i)).trim(),e=R(n[1])?!0:!!F(t,n[1],r,i);if(e&&c.includes("["))try{const a=K(c);if(a.length===1&&a[0].t==="table"&&a[0].v.name){const{name:w,spec:l}=a[0].v;return t.resolveTable(w,l,r,i,s)}}catch(a){if(!(a instanceof v))throw a}let o=s,h=c;const d=c.lastIndexOf("!");if(d>=0){if(o=t.sheetByName(c.slice(0,d).replace(/^'|'$/g,"").replace(/''/g,"'")),o===null)throw A.REF;h=c.slice(d+1)}if(!e){const a=/^R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/i.exec(h);if(!a)throw A.REF;const w=(O,$)=>O===void 0?$:O[0]==="["?$+Number(O.slice(1,-1)):Number(O)-1,l=w(a[1],r),g=w(a[2],i),m=a[3]===void 0&&a[4]===void 0?l:w(a[3],r),y=a[3]===void 0&&a[4]===void 0?g:w(a[4],i);if(l<0||g<0||m>=D||y>=P)throw A.REF;return new L(o,Math.min(l,m),Math.min(g,y),Math.max(l,m),Math.max(g,y))}const f=rt(h.replace(/\$/g,""));if(f!==null)return new L(o,f.r0,f.c0,f.r1,f.c1);const u=t.evalName(h,r,i,o);if(u instanceof L)return u;throw A.REF},{volatile:!0,refResult:!0}),M("ROW",0,1,(t,n,r,i,s,c)=>{if(n.length===0||R(n[0]))return r+1;const e=n[0];if(e instanceof N)throw e;if(!(e instanceof L))throw A.VALUE;return e.rows===1||t.legacy&&!c?e.r0+1:new p(e.rows,1,Array.from({length:e.rows},(o,h)=>e.r0+h+1))},{refResult:([t])=>t!==void 0&&t.t!=="omit"&&t.t!=="cell"}),M("COLUMN",0,1,(t,n,r,i,s,c)=>{if(n.length===0||R(n[0]))return i+1;const e=n[0];if(e instanceof N)throw e;if(!(e instanceof L))throw A.VALUE;return e.cols===1||t.legacy&&!c?e.c0+1:new p(1,e.cols,Array.from({length:e.cols},(o,h)=>e.c0+h+1))}),M("ROWS",1,1,(t,n)=>{const r=n[0];if(r instanceof N)throw r;if(r instanceof L||r instanceof p)return r.rows;if(r instanceof T)throw A.REF;return 1}),M("COLUMNS",1,1,(t,n)=>{const r=n[0];if(r instanceof N)throw r;if(r instanceof L||r instanceof p)return r.cols;if(r instanceof T)throw A.REF;return 1}),M("AREAS",1,1,(t,n)=>{if(n[0]instanceof N)throw n[0];if(n[0]instanceof T)return n[0].refs.length;if(n[0]instanceof L)return 1;throw A.VALUE}),M("ADDRESS",2,5,(t,n,r,i)=>{const s=Math.trunc(E(t.scalar(n[0],r,i))),c=Math.trunc(E(t.scalar(n[1],r,i))),e=R(n[2])?1:Math.trunc(E(t.scalar(n[2],r,i))),o=R(n[3])?!0:!!t.scalar(n[3],r,i),h=R(n[4])?null:I(t.scalar(n[4],r,i));if(s<1||c<1||s>D||c>P||e<1||e>4)throw A.VALUE;const d=e===1||e===2,f=e===1||e===3,u=o?`${f?"$":""}${nt(c-1)}${d?"$":""}${s}`:`R${d?s:`[${s}]`}C${f?c:`[${c}]`}`;if(h===null)return u;if(h==="")return`!${u}`;const a=/^\[([^\]]*)\](.*)$/.exec(h);return`${a?z(a[2],{book:a[1]}):z(h)}${u}`}),q("CHOOSE",2,255,(t,n,r,i,s)=>{const c=w=>{if(w instanceof N)throw w;const l=Math.trunc(E(w));if(l<1||l>=n.length)throw A.VALUE;return l},e=n[0](r,i,s);if(!(e instanceof p)&&(t.legacy||!_(e)))return n[c(t.scalar(e,r,i))](r,i,s);const o=t.toArr(e),h=new Map;let{rows:d,cols:f}=o;const u=o.data.map(w=>{try{const l=c(w);h.has(l)||h.set(l,t.toArr(n[l](r,i,s)));const g=h.get(l);return d=Math.max(d,g.rows),f=Math.max(f,g.cols),g}catch(l){if(!(l instanceof N))throw l;return l}}),a=new Array(d*f);for(let w=0;w<d;w++)for(let l=0;l<f;l++){const g=o.rows===1?0:w,m=o.cols===1?0:l,y=g<o.rows&&m<o.cols?u[g*o.cols+m]:A.NA;if(y instanceof N)a[w*f+l]=y;else{const O=y.rows===1?0:w,$=y.cols===1?0:l;a[w*f+l]=O<y.rows&&$<y.cols?y.get(O,$):A.NA}}return new p(d,f,a)},{passes:t=>t>0}),M("FORMULATEXT",1,1,(t,n)=>{const r=n[0];if(!(r instanceof L))throw A.NA;const{sheet:i}=r;let s=r.r0,c=r.c0;const e=i.kind(s,c);if(e&st){const h=t.spillMaster(i,s,c);if(!h?.rect.fixed)throw A.NA;({row:s,col:c}=h)}else if((e&et)===0)throw A.NA;if(i.protection&&t.book.styles.get(i.style(s,c)).hiddenFormula)throw A.NA;let o=t.book.editText(i,s,c);if(o.includes("[]")){const h=K(o);for(let d=h.length-1;d>=0;d--){const f=h[d];f.t==="table"&&f.v.name&&f.v.spec==="[]"&&(o=o.slice(0,f.e-2)+o.slice(f.e))}}return o}),M("HYPERLINK",1,2,(t,n,r,i)=>{if(R(n[1])){const c=t.scalar(n[0],r,i);return c instanceof N?c:c===null?"":I(c)}const s=t.scalar(n[1],r,i);return s===null?0:s}),M("GETPIVOTDATA",2,255,()=>{throw A.REF});
1
+ import{setCategory as Q}from"./registry.js";import{lazy as Z,raw as M}from"./registry.js";import{Arr as E,bool as W,compareValues as U,ERR as A,num as p,OMITTED as b,Ref as m,RefArray as x,RefList as C,str as P,XlError as y}from"../values.js";import{FormulaSyntaxError as v,tokenize as tt}from"../parser.js";import{shownReferences as nt}from"../shown-refs.js";import{hasWildcards as rt,scalarOf as F,wildcardRegex as et}from"./helpers.js";import{colName as st,MAX_COLS as H,MAX_ROWS as X,parseRangeRef as ot,sheetPrefix as j}from"../../core/address.js";import{FORMULA as it,SPILL as ft}from"../../core/sheet.js";Q("Lookup & Reference");const ct=64,N=t=>t===void 0||t===b;function $(t,n){if(n instanceof y)throw n;if(n instanceof m){const r=t.trim(n),i=n.cols===1,s=n.rows===1,f=i?n.rows:s?n.cols:n.rows*n.cols;if(i)return{ref:n,length:f,usedLength:r.r1-n.r0+1,at:o=>t.read(n.sheet,n.r0+o,n.c0)};if(s)return{ref:n,length:f,usedLength:r.c1-n.c0+1,at:o=>t.read(n.sheet,n.r0,n.c0+o)};const e=t.toArr(n);return{length:e.data.length,usedLength:e.data.length,at:o=>e.data[o]}}return n instanceof E?{length:n.data.length,usedLength:n.data.length,at:r=>n.data[r]}:{length:1,usedLength:1,at:()=>n}}const T=(t,n)=>typeof t==typeof n;function _(t,n,r,i,s=!1){if(r==null&&(r=0),typeof r=="string"&&i&&rt(r)){const f=et(r);if(s)for(let e=n.usedLength-1;e>=0;e--){const o=n.at(e);if(typeof o=="string"&&f.test(o))return e}else for(let e=0;e<n.usedLength;e++){const o=n.at(e);if(typeof o=="string"&&f.test(o))return e}return-1}if(!s&&n.ref&&n.usedLength>ct){const f=t.exactIndex(n.ref),e=typeof r=="string"?`s${r.toLowerCase()}`:typeof r=="number"?r:`b${r}`,o=f.get(e);return o===void 0?-1:o}if(s){for(let f=n.usedLength-1;f>=0;f--){const e=n.at(f);if(e!==null&&T(e,r)&&U(e,r)===0)return f}return-1}for(let f=0;f<n.usedLength;f++){const e=n.at(f);if(e!==null&&T(e,r)&&U(e,r)===0)return f}return-1}function k(t,n,r=U){let i=0,s=t.usedLength-1,f=-1;for(;i<=s;){let e=i+s>>>1,o=e;for(;o<=s;){const d=t.at(o);if(d!==null&&T(d,n))break;o++}if(o>s){s=e-1;continue}e=o;const w=r(t.at(e),n);if(w===0){let d=e;for(i=e+1;i<=s;){const a=i+s>>>1,c=t.at(a);c!==null&&T(c,n)&&r(c,n)===0?(d=a,i=a+1):s=a-1}return d}w<0?(f=e,i=e+1):s=e-1}return f}function lt(t,n){let r=-1;for(let i=0;i<t.usedLength;i++){const s=t.at(i);if(s===null||!T(s,n))continue;const f=U(s,n);if(f===0)return i;if(f<0)break;r=i}return r}M("MATCH",2,3,(t,n,r,i,s,f)=>S(t,n,[0,2],e=>{const o=e[0]===b?0:F(t,e[0],r,i),w=e[2]===void 0?1:p(t.scalar(e[2],r,i));if(!(e[1]instanceof m||e[1]instanceof E||e[1]instanceof C))throw p(F(t,e[1],r,i)),A.NA;const d=e[1]instanceof m&&!(e[1].rows===1||e[1].cols===1)?null:e[1];if(d===null)throw A.NA;if(d instanceof E&&d.rows>1&&d.cols>1)throw A.NA;const a=$(t,d);let c;if(w===0?c=_(t,a,o,!0):w>0?c=k(a,o===null?0:o):c=lt(a,o===null?0:o),c<0)throw A.NA;return c+1},f),{array:!0});function ut(t,n){return typeof t=="number"&&typeof n=="number"?t===n?0:t<n?-1:1:U(t,n)}function B(t,n,r,i,s,f,e,o){const w=Math.trunc(p(t.scalar(i,f,e)));if(typeof s=="string")throw A.VALUE;const d=s===void 0||W(s),a=n===null?0:n;let c,u,h;if(r instanceof m)h=o?r.c1-r.c0+1:r.r1-r.r0+1,c=$(t,o?new m(r.sheet,r.r0,r.c0,r.r1,r.c0):new m(r.sheet,r.r0,r.c0,r.r0,r.c1)),u=L=>o?t.read(r.sheet,r.r0+L,r.c0+w-1):t.read(r.sheet,r.r0+w-1,r.c0+L);else{if(!(r instanceof E))throw p(r),A.NA;const L=r,R=o?L.rows:L.cols;h=o?L.cols:L.rows,c={length:R,usedLength:R,at:O=>o?L.get(O,0):L.get(0,O)},u=O=>o?L.get(O,w-1):L.get(w-1,O)}const l=d?k(c,a,ut):_(t,c,a,!0);if(l<0)throw A.NA;if(w<1)throw A.VALUE;if(w>h)throw A.REF;const g=u(l);return g===null?0:g}const I=t=>t instanceof m&&(t.r0!==t.r1||t.c0!==t.c1)||t instanceof E&&t.data.length>1;function S(t,n,r,i,s=!1,f=!1){if(t.legacy&&!s)return i(n);const e=r.map(a=>I(n[a])?t.toArr(n[a]):null);if(e.every(a=>a===null))return i(n);const o=Math.max(...e.map(a=>a?.rows??1)),w=Math.max(...e.map(a=>a?.cols??1)),d=new Array(o*w);for(let a=0;a<o;a++)for(let c=0;c<w;c++){const u=n.slice();let h=!1;e.forEach((g,L)=>{g&&(g.rows!==1&&a>=g.rows||g.cols!==1&&c>=g.cols?h=!0:u[r[L]]=g.get(g.rows===1?0:a,g.cols===1?0:c))});let l;if(h)l=A.NA;else try{l=i(u)}catch(g){if(!(g instanceof y))throw g;l=g}l instanceof m&&!f?l=t.read(l.sheet,l.r0,l.c0):l instanceof E&&(l=l.data[0]),d[a*w+c]=l===null?0:l}return f?new x(o,w,d):new E(o,w,d)}function G(t,n,r,i,s){if(n[1]instanceof y)throw n[1];const f=n[1];if(t.legacy||!I(n[0])&&!I(n[3])){const h=N(n[3])?n[3]:F(t,n[3],r,i);return B(t,n[0]===b?0:F(t,n[0],r,i),f,n[2],h,r,i,s)}const e=t.toArr(n[0]),o=N(n[3])?null:t.toArr(n[3]),w=Math.max(e.rows,o?.rows??1),d=Math.max(e.cols,o?.cols??1),a=(h,l,g)=>h.get(h.rows===1?0:l,h.cols===1?0:g),c=(h,l,g)=>(h.rows===1||l<h.rows)&&(h.cols===1||g<h.cols),u=new Array(w*d);for(let h=0;h<w;h++)for(let l=0;l<d;l++){if(!c(e,h,l)||o&&!c(o,h,l)){u[h*d+l]=A.NA;continue}try{u[h*d+l]=B(t,a(e,h,l),f,n[2],o?a(o,h,l):n[3],r,i,s)}catch(g){if(!(g instanceof y))throw g;u[h*d+l]=g}}return new E(w,d,u)}M("VLOOKUP",3,4,(t,n,r,i)=>G(t,n,r,i,!0),{array:!0}),M("HLOOKUP",3,4,(t,n,r,i)=>G(t,n,r,i,!1),{array:!0}),M("LOOKUP",2,3,(t,n,r,i)=>{const s=n[0]===b?0:F(t,n[0],r,i),f=s===null?0:s;if(n[1]===b||n[2]===b)throw A.VALUE;let e=n[1],o=n[2]===void 0?null:n[2];if(o instanceof m||o instanceof E){if(o.rows>1&&o.cols>1)throw A.NA;const{rows:c,cols:u}=e instanceof m||e instanceof E?e:{rows:1,cols:1};c>1&&u>1&&(e instanceof m?e=c>=u?new m(e.sheet,e.r0,e.c0,e.r1,e.c0):new m(e.sheet,e.r0,e.c0,e.r0,e.c1):e=c>=u?E.column(Array.from({length:c},(h,l)=>e.get(l,0))):E.row(e.data.slice(0,u)))}if(o===null){const c=t.toArr(e);c.rows>=c.cols?(e=new E(c.rows,1,Array.from({length:c.rows},(u,h)=>c.get(h,0))),o=new E(c.rows,1,Array.from({length:c.rows},(u,h)=>c.get(h,c.cols-1)))):(e=new E(1,c.cols,Array.from({length:c.cols},(u,h)=>c.get(0,h))),o=new E(1,c.cols,Array.from({length:c.cols},(u,h)=>c.get(c.rows-1,h))))}const w=$(t,e),d=k(w,f);if(d<0)throw A.NA;let a;if(o instanceof m){const c=o.rows>1;a=t.read(o.sheet,o.r0+(c?d:0),o.c0+(c?0:d))}else{const c=o instanceof E?o.data:[o];if(d>=c.length)throw A.NA;a=c[d]}return a===null?0:a},{array:!0});const at={number:0,string:1,boolean:2},Y=t=>t===null?4:t instanceof y?3:at[typeof t]??1;function D(t,n){const r=Y(t),i=Y(n);return r!==i?r-i:r===4?0:r===3?t.index-n.index:U(t,n)}function q(t,n,r,i,s){const f=r===void 0||r===b?null:r;if(i===2&&Math.abs(s)===2)throw A.VALUE;if(s===2||s===-2){const a=s===2;let c=0,u=n.usedLength-1,h=-1,l=-1,g=-1;for(;c<=u;){const L=c+u>>>1,R=D(n.at(L),f);if(R===0){h=L;break}(a?R<0:R>0)?(a?l=L:g=L,c=L+1):(a?g=L:l=L,u=L-1)}return h>=0?h:i===-1?l:i===1?g:-1}const e=s===-1;if(f===null&&(i===0||i===2))return ht(n,s);if(i===0)return _(t,n,f,!1,e);if(i===2)return _(t,n,f,!0,e);let o=-1,w=null;const d=n.length>n.usedLength?n.usedLength+1:n.length;for(let a=0;a<d;a++){const c=e?d-1-a:a,u=n.at(c),h=D(u,f);if(h===0)return c;(i===-1&&h<0&&(o<0||D(u,w)>0)||i===1&&h>0&&(o<0||D(u,w)<0))&&(o=c,w=u)}return o}function ht(t,n){const r=t.length;for(let i=0;i<r;i++){const s=n<0?r-1-i:i;if(t.at(s)===null)return s}return-1}M("XMATCH",2,4,(t,n,r,i)=>{const s=n[0]===b?null:F(t,n[0],r,i);if(n[1]===b)throw A.VALUE;const f=N(n[2])?0:Math.trunc(p(t.scalar(n[2],r,i))),e=N(n[3])?1:Math.trunc(p(t.scalar(n[3],r,i)));if(![0,-1,1,2].includes(f)||![1,-1,2,-2].includes(e))throw A.VALUE;const o=q(t,$(t,n[1]),s,f,e);if(o<0)throw A.NA;return o+1},{array:!0});const K=t=>t?.t==="cell"||t?.t==="range"&&t.a.c===t.b.c&&t.a.cAbs===t.b.cAbs||t?.t==="cols"&&t.c0===t.c1&&t.a0===t.a1,z=t=>t?.t==="cell"||t?.t==="range"&&t.a.r===t.b.r&&t.a.rAbs===t.b.rAbs||t?.t==="rows"&&t.r0===t.r1&&t.a0===t.a1;M("XLOOKUP",3,6,(t,n,r,i)=>S(t,n,[0],s=>{const f=s[0]===b?null:F(t,s[0],r,i),e=N(s[4])?0:Math.trunc(p(t.scalar(s[4],r,i))),o=N(s[5])?1:Math.trunc(p(t.scalar(s[5],r,i)));if(![0,-1,1,2].includes(e)||![1,-1,2,-2].includes(o))throw A.VALUE;if(s[1]instanceof y)throw s[1];const w=s[1]instanceof m?s[1].rows:t.toArr(s[1]).rows,d=s[1]instanceof m?s[1].cols:t.toArr(s[1]).cols;if(w>1&&d>1)throw A.VALUE;if(s[2]instanceof y)throw s[2];const a=s[2]instanceof m?s[2].rows:t.toArr(s[2]).rows,c=s[2]instanceof m?s[2].cols:t.toArr(s[2]).cols;if(w*d>1&&(d===1?a!==w:c!==d))throw A.VALUE;const u=q(t,$(t,s[1]),f,e,o);if(u<0){if(N(s[3]))throw A.NA;return s[3]}const h=d===1,l=s[2];if(l instanceof m)return h?new m(l.sheet,l.r0+u,l.c0,l.r0+u,l.c1):new m(l.sheet,l.r0,l.c0+u,l.r1,l.c0+u);const g=t.toArr(l);return h?g.cols===1?g.get(u,0):new E(1,g.cols,g.data.slice(u*g.cols,(u+1)*g.cols)):g.rows===1?g.get(0,u):new E(g.rows,1,Array.from({length:g.rows},(L,R)=>g.get(R,u)))}),{array:!0,refResult:([,t,n,r])=>r&&r.t!=="omit"&&r.t!=="lit"&&r.t!=="cell"?!0:K(t)?!K(n):!(z(t)&&z(n))});const J=t=>t?.t==="lit"&&typeof t.v=="number"&&Math.trunc(t.v)>0||t?.t==="call"&&(t.name==="MATCH"||t.name==="XMATCH");M("INDEX",2,4,(t,n,r,i)=>S(t,n,[1,2],s=>{let f=s[0];if(f instanceof y)throw f;const e=N(s[1])?0:Math.trunc(p(t.scalar(s[1],r,i))),o=N(s[2])?0:Math.trunc(p(t.scalar(s[2],r,i)));if(f instanceof C){const c=N(s[3])?1:Math.trunc(p(t.scalar(s[3],r,i)));if(c<1||c>f.refs.length)throw A.REF;f=f.refs[c-1]}else if(f instanceof m&&!N(s[3])&&Math.trunc(p(t.scalar(s[3],r,i)))!==1)throw A.REF;if(e<0||o<0)throw A.VALUE;if(f instanceof m){let c=e,u=o;if(s.length===2&&f.rows>1&&f.cols>1)throw A.REF;if((s.length===2||N(s[2]))&&f.rows===1&&f.cols>1&&(u=c,c=0),c>f.rows||u>f.cols)throw A.REF;const h=c===0?f.r0:f.r0+c-1,l=c===0?f.r1:h,g=u===0?f.c0:f.c0+u-1,L=u===0?f.c1:g;return new m(f.sheet,h,g,l,L)}const w=t.toArr(f);let d=e,a=o;if(s.length===2&&w.rows===1&&w.cols>1&&(a=d,d=0,a===0))return w;if(s.length===2&&w.cols===1&&(a=1),d>w.rows||a>w.cols)throw A.REF;return d===0&&a===0?w:d===0?new E(w.rows,1,Array.from({length:w.rows},(c,u)=>w.get(u,a-1))):a===0?new E(1,w.cols,w.data.slice((d-1)*w.cols,d*w.cols)):w.get(d-1,a-1)}),{array:t=>t===0,refResult:([t,n,r])=>!(J(n)&&(J(r)||(r===void 0||r.t==="omit")&&(K(t)||z(t))))}),M("OFFSET",3,5,(t,n,r,i,s,f)=>S(t,n,[1,2,3,4],e=>{const o=e[0];if(o instanceof y)throw o;if(!(o instanceof m))throw A.VALUE;const w=Math.trunc(p(t.scalar(e[1],r,i))),d=Math.trunc(p(t.scalar(e[2],r,i))),a=N(e[3])?o.rows:Math.trunc(p(t.scalar(e[3],r,i))),c=N(e[4])?o.cols:Math.trunc(p(t.scalar(e[4],r,i)));if(a===0||c===0)throw A.REF;const u=o.r0+w+(a<0?a+1:0),h=o.c0+d+(c<0?c+1:0),l=u+Math.abs(a)-1,g=h+Math.abs(c)-1;if(u<0||h<0||l>=X||g>=H)throw A.REF;return new m(o.sheet,u,h,l,g)},f,!0),{volatile:!0,refResult:!0}),M("INDIRECT",1,2,(t,n,r,i,s)=>{const f=P(F(t,n[0],r,i)).trim(),e=n[1]===void 0||n[1]!==b&&!!F(t,n[1],r,i);if(e&&f.includes("["))try{const u=tt(f);if(u.length===1&&u[0].t==="table"&&u[0].v.name){const{name:h,spec:l}=u[0].v;return t.resolveTable(h,l,r,i,s)}}catch(u){if(!(u instanceof v))throw u}let o=s,w=f;const d=f.lastIndexOf("!");if(d>=0){if(o=t.sheetByName(f.slice(0,d).replace(/^'|'$/g,"").replace(/''/g,"'")),o===null)throw A.REF;w=f.slice(d+1)}if(!e){const u=/^R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/i.exec(w);if(!u)throw A.REF;const h=(O,V)=>O===void 0?V:O[0]==="["?V+Number(O.slice(1,-1)):Number(O)-1,l=h(u[1],r),g=h(u[2],i),L=u[3]===void 0&&u[4]===void 0?l:h(u[3],r),R=u[3]===void 0&&u[4]===void 0?g:h(u[4],i);if(l<0||g<0||L>=X||R>=H)throw A.REF;return new m(o,Math.min(l,L),Math.min(g,R),Math.max(l,L),Math.max(g,R))}const a=ot(w.replace(/\$/g,""));if(a!==null)return new m(o,a.r0,a.c0,a.r1,a.c1);const c=t.evalName(w,r,i,o);if(c instanceof m)return c;throw A.REF},{volatile:!0,refResult:!0}),M("ROW",0,1,(t,n,r,i,s,f)=>{if(n.length===0||N(n[0]))return r+1;const e=n[0];if(e instanceof y)throw e;if(!(e instanceof m))throw A.VALUE;return e.rows===1||t.legacy&&!f?e.r0+1:new E(e.rows,1,Array.from({length:e.rows},(o,w)=>e.r0+w+1))},{refResult:([t])=>t!==void 0&&t.t!=="omit"&&t.t!=="cell"}),M("COLUMN",0,1,(t,n,r,i,s,f)=>{if(n.length===0||N(n[0]))return i+1;const e=n[0];if(e instanceof y)throw e;if(!(e instanceof m))throw A.VALUE;return e.cols===1||t.legacy&&!f?e.c0+1:new E(1,e.cols,Array.from({length:e.cols},(o,w)=>e.c0+w+1))}),M("ROWS",1,1,(t,n)=>{const r=n[0];if(r instanceof y)throw r;if(r instanceof m||r instanceof E)return r.rows;if(r instanceof C)throw A.REF;return 1}),M("COLUMNS",1,1,(t,n)=>{const r=n[0];if(r instanceof y)throw r;if(r instanceof m||r instanceof E)return r.cols;if(r instanceof C)throw A.REF;return 1}),M("AREAS",1,1,(t,n)=>{if(n[0]instanceof y)throw n[0];if(n[0]instanceof C)return n[0].refs.length;if(n[0]instanceof m)return 1;throw A.VALUE}),M("ADDRESS",2,5,(t,n,r,i)=>{const s=Math.trunc(p(t.scalar(n[0],r,i))),f=Math.trunc(p(t.scalar(n[1],r,i))),e=N(n[2])?1:Math.trunc(p(t.scalar(n[2],r,i))),o=N(n[3])?!0:W(t.scalar(n[3],r,i)),w=N(n[4])?null:P(t.scalar(n[4],r,i));if(s<1||f<1||s>X||f>H||e<1||e>4)throw A.VALUE;const d=e===1||e===2,a=e===1||e===3,c=o?`${a?"$":""}${st(f-1)}${d?"$":""}${s}`:`R${d?s:`[${s}]`}C${a?f:`[${f}]`}`;if(w===null)return c;if(w==="")return`!${c}`;const u=/^\[([^\]]*)\](.*)$/.exec(w);return`${u?j(u[2],{book:u[1]}):j(w)}${c}`}),Z("CHOOSE",2,255,(t,n,r,i,s)=>{const f=h=>{if(h instanceof y)throw h;const l=Math.trunc(p(h));if(l<1||l>=n.length)throw A.VALUE;return l},e=n[0](r,i,s);if(!(e instanceof E)&&(t.legacy||!I(e)))return n[f(t.scalar(e,r,i))](r,i,s);const o=t.toArr(e),w=new Map;let{rows:d,cols:a}=o;const c=o.data.map(h=>{try{const l=f(h);w.has(l)||w.set(l,t.toArr(n[l](r,i,s)));const g=w.get(l);return d=Math.max(d,g.rows),a=Math.max(a,g.cols),g}catch(l){if(!(l instanceof y))throw l;return l}}),u=new Array(d*a);for(let h=0;h<d;h++)for(let l=0;l<a;l++){const g=o.rows===1?0:h,L=o.cols===1?0:l,R=g<o.rows&&L<o.cols?c[g*o.cols+L]:A.NA;if(R instanceof y)u[h*a+l]=R;else{const O=R.rows===1?0:h,V=R.cols===1?0:l;u[h*a+l]=O<R.rows&&V<R.cols?R.get(O,V):A.NA}}return new E(d,a,u)},{passes:t=>t>0}),M("FORMULATEXT",1,1,(t,n)=>{const r=n[0];if(r instanceof y)throw r;if(!(r instanceof m))throw A.NA;const{sheet:i}=r;let s=r.r0,f=r.c0;const e=i.kind(s,f);if(e&ft){const w=t.spillMaster(i,s,f);if(!w?.rect.fixed)throw A.NA;({row:s,col:f}=w)}else if((e&it)===0)throw A.NA;if(i.protection&&t.book.styles.get(i.style(s,f)).hiddenFormula)throw A.NA;let o=t.book.editText(i,s,f);return o=nt(t.book,i,s,f,o),o}),M("HYPERLINK",1,2,(t,n,r,i)=>{if(N(n[1])){const e=t.scalar(n[0],r,i);return e instanceof y?e:e===null?"":P(e)}const s=t.scalar(n[0],r,i);if(s instanceof y)return s;const f=t.scalar(n[1],r,i);return f===null?0:f}),M("GETPIVOTDATA",2,255,()=>{throw A.REF});
@@ -1 +1 @@
1
- import{setCategory as $}from"./registry.js";import{alias as x,lift as w,raw as A}from"./registry.js";import{Arr as E,bool as tt,checked as L,ERR as f,int as O,num as c,OMITTED as d,opt as rt,power as nt,Ref as g,RefList as b,str as k,XlError as V}from"../values.js";import{collectNumbers as Y,eachValue as et,liftReferenceArgs as ot,makeCriteria as st,matrixOf as T,toolpakScalar as P}from"./helpers.js";$("Math & Trig");const N=(t,r)=>w(t,1,1,n=>L(r(c(n)))),ft=2**27,p=t=>r=>{if(Math.abs(r)>=ft)throw f.NUM;return t(r)},j=t=>r=>{if(!Number.isFinite(r*r))throw f.NUM;return t(r)},Q=t=>r=>{if(!Number.isFinite(Math.exp(Math.abs(r))))throw f.NUM;return t(r)};N("ABS",Math.abs),N("ACOS",t=>{if(t<-1||t>1)throw f.NUM;return Math.acos(t)}),N("ACOSH",j(t=>{if(t<1)throw f.NUM;return Math.acosh(t)})),N("ACOT",t=>Math.PI/2-Math.atan(t)),N("ACOTH",t=>{if(Math.abs(t)<=1)throw f.NUM;return .5*Math.log((t+1)/(t-1))}),N("ASIN",t=>{if(t<-1||t>1)throw f.NUM;return Math.asin(t)}),N("ASINH",j(Math.asinh)),N("ATAN",Math.atan),N("ATANH",t=>{if(t<=-1||t>=1)throw f.NUM;return Math.atanh(t)}),N("COS",p(Math.cos)),N("COSH",Q(Math.cosh)),N("COT",p(t=>{if(t===0)throw f.DIV0;return 1/Math.tan(t)})),N("COTH",t=>{if(t===0)throw f.DIV0;return 1/Math.tanh(t)}),N("CSC",p(t=>{if(t===0)throw f.DIV0;return 1/Math.sin(t)})),N("CSCH",t=>{if(t===0)throw f.DIV0;return 1/Math.sinh(t)}),N("DEGREES",t=>t*180/Math.PI),N("EXP",Math.exp),N("INT",Math.floor),N("LN",t=>{if(t<=0)throw f.NUM;return Math.log(t)}),N("LOG10",t=>{if(t<=0)throw f.NUM;return Math.log10(t)}),N("RADIANS",t=>t*Math.PI/180),N("SEC",p(t=>1/Math.cos(t))),N("SECH",t=>1/Math.cosh(t)),N("SIGN",Math.sign),N("SIN",p(Math.sin)),N("SINH",Q(Math.sinh)),N("SQRT",t=>{if(t<0)throw f.NUM;return Math.sqrt(t)}),N("SQRTPI",t=>{if(t<0)throw f.NUM;return Math.sqrt(t*Math.PI)}),N("TAN",p(Math.tan)),N("TANH",Math.tanh),w("ATAN2",2,2,(t,r)=>{const n=c(t),e=c(r);if(n===0&&e===0)throw f.DIV0;return Math.atan2(e,n)}),w("LOG",1,2,(t,r)=>{const n=c(t),e=r===void 0||r===d?10:c(r);if(n<=0||e<=0)throw f.NUM;if(e===1)throw f.DIV0;return e===10?Math.log10(n):Math.log(n)/Math.log(e)}),w("PI",0,0,()=>Math.PI),w("POWER",2,2,(t,r)=>{const n=c(t),e=c(r);if(n===0&&e===0)throw f.NUM;if(n===0&&e<0)throw f.DIV0;return L(nt(n,e))}),w("MOD",2,2,(t,r)=>{const n=c(t),e=c(r);if(e===0)throw f.DIV0;if(Math.abs(n/e)>=2**40)throw f.NUM;return L(n-e*Math.floor(n/e))}),w("QUOTIENT",2,2,(t,r)=>{const n=c(r);if(n===0)throw f.DIV0;return Math.trunc(c(t)/n)});function z(t,r){if(!Number.isFinite(t))throw f.NUM;r>15&&(r=15);const n=10**Math.abs(r),e=r>=0?Math.abs(t)*n:Math.abs(t)/n,o=Number(e.toPrecision(15)),s=Math.floor(o+.5),i=r>=0?s/n:s*n;return t<0?-i:i}function F(t,r,n){r>15&&(r=15);const e=10**Math.abs(r),o=r>=0?Math.abs(t)*e:Math.abs(t)/e,s=Number(o.toPrecision(15)),i=n?Math.ceil(s):Math.floor(s),a=r>=0?i/e:i*e;return t<0?-a:a}w("ROUND",2,2,(t,r)=>z(c(t),O(r))),w("ROUNDUP",2,2,(t,r)=>F(c(t),O(r),!0)),w("ROUNDDOWN",2,2,(t,r)=>F(c(t),O(r),!1)),w("TRUNC",1,2,(t,r)=>F(c(t),r===void 0||r===d?0:O(r),!1)),w("EVEN",1,1,t=>{const r=c(t),n=Math.ceil(Number((Math.abs(r)/2).toPrecision(15)))*2;return r<0?-n:n}),w("ODD",1,1,t=>{const r=Math.abs(c(t));let n=Math.ceil(Number(r.toPrecision(15)));return n%2===0&&(n+=1),c(t)<0?-n:n}),w("MROUND",2,2,(t,r)=>{const n=c(t),e=c(r);if(e===0)return 0;if(n*e<0)throw f.NUM;return z(n/e,0)*e});function S(t,r){const n=Number((t/r).toPrecision(15));return Math.ceil(n)*r}function R(t,r){const n=Number((t/r).toPrecision(15));return Math.floor(n)*r}w("CEILING",2,2,(t,r)=>{const n=c(t),e=c(r);if(e===0)return 0;if(n>0&&e<0)throw f.NUM;return n<0&&e<0?-S(-n,-e):S(n,e)}),w("FLOOR",2,2,(t,r)=>{const n=c(t),e=c(r);if(e===0){if(n===0)return 0;throw f.DIV0}if(n>0&&e<0)throw f.NUM;return n<0&&e<0?-R(-n,-e):R(n,e)}),w("CEILING.MATH",1,3,(t,r,n)=>{const e=c(t),o=Math.abs(r===void 0||r===d?1:c(r))||0;if(o===0)return 0;const s=n===void 0||n===d?0:c(n);return e<0&&s!==0?-S(-e,o):S(e,o)}),w("FLOOR.MATH",1,3,(t,r,n)=>{const e=c(t),o=Math.abs(r===void 0||r===d?1:c(r))||0;if(o===0)return 0;const s=n===void 0||n===d?0:c(n);return e<0&&s!==0?-R(-e,o):R(e,o)}),w("CEILING.PRECISE",1,2,(t,r)=>{const n=Math.abs(r===void 0||r===d?1:c(r));return n===0?0:S(c(t),n)}),x("ISO.CEILING","CEILING.PRECISE"),w("FLOOR.PRECISE",1,2,(t,r)=>{const n=Math.abs(r===void 0||r===d?1:c(r));return n===0?0:R(c(t),n)});function it(t){if(t<0)throw f.NUM;if(t>170)throw f.NUM;let r=1;for(let n=2;n<=t;n++)r*=n;return r}w("FACT",1,1,t=>it(Math.floor(c(t)))),w("FACTDOUBLE",1,1,t=>{const r=Math.floor(c(t));if(r<-1)throw f.NUM;let n=1;for(let e=r;e>1;e-=2)if(n*=e,!Number.isFinite(n))throw f.NUM;return L(n)});function H(t,r){if(r<0||t<0||r>t)throw f.NUM;const n=Math.min(r,t-r);let e=1;for(let o=1;o<=n;o++)e=e*(t-n+o)/o;return L(Math.round(e))}w("COMBIN",2,2,(t,r)=>H(Math.trunc(c(t)),Math.trunc(c(r)))),w("COMBINA",2,2,(t,r)=>{const n=Math.trunc(c(t)),e=Math.trunc(c(r));if(n<0||e<0)throw f.NUM;return n===0&&e===0?1:n===0?0:H(n+e-1,e)});function W(t,r){for(;r!==0;)[t,r]=[r,t%r];return t}const v=(t,r)=>{const n=[];for(const e of r)if(e instanceof g||e instanceof b||e instanceof E)et(t,e,o=>{if(o instanceof V)throw o;if(typeof o=="number"){if(o<0)throw f.NUM;n.push(Math.trunc(o))}else if(typeof o=="string")throw f.VALUE});else{const o=Math.trunc(c(e));if(o<0)throw f.NUM;n.push(o)}return n};A("GCD",1,255,(t,r)=>v(t,r).reduce(W,0)),A("LCM",1,255,(t,r)=>{let n=1;for(const e of v(t,r)){if(e===0)return 0;n=n/W(n,e)*e}return L(n)}),A("MULTINOMIAL",1,255,(t,r)=>{const n=v(t,r);let e=0,o=1;for(const s of n)e+=s,o*=H(e,s);return L(o)}),w("ARABIC",1,1,t=>{const r=k(t).trim().toUpperCase();if(r==="")return 0;const n=r[0]==="-",e=n?r.slice(1):r,o={M:1e3,D:500,C:100,L:50,X:10,V:5,I:1};let s=0;for(let i=0;i<e.length;i++){const a=o[e[i]];if(a===void 0)throw f.VALUE;const h=o[e[i+1]]??0;s+=a<h?-a:a}return n?-s:s}),w("ROMAN",1,2,(t,r)=>{const n=Math.trunc(c(t)),e=r===void 0||r===d?0:typeof r=="boolean"?r?0:4:Math.trunc(c(r));if(n<0||n>3999)throw f.VALUE;if(e<0||e>4)throw f.VALUE;if(n===0)return"";const o={0:[[1e3,"M"],[900,"CM"],[500,"D"],[400,"CD"],[100,"C"],[90,"XC"],[50,"L"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],1:[[1e3,"M"],[950,"LM"],[900,"CM"],[500,"D"],[450,"LD"],[400,"CD"],[100,"C"],[95,"VC"],[90,"XC"],[50,"L"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],2:[[1e3,"M"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],3:[[1e3,"M"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],4:[[1e3,"M"],[999,"IM"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[499,"ID"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]]};let s=n,i="";for(const[a,h]of o[e])for(;s>=a;)i+=h,s-=a;return i}),w("BASE",2,3,(t,r,n)=>{const e=Math.trunc(c(t)),o=Math.trunc(c(r)),s=n===void 0||n===d?0:Math.trunc(c(n));if(e<0||e>=2**53||o<2||o>36||s<0||s>255)throw f.NUM;return e.toString(o).toUpperCase().padStart(s,"0")}),w("DECIMAL",2,2,(t,r)=>{const n=k(t).trim().toLowerCase(),e=Math.trunc(c(r));if(e<2||e>36||n.length>255)throw f.NUM;let o=0;for(const s of n){const i=parseInt(s,36);if(Number.isNaN(i)||i>=e)throw f.NUM;o=o*e+i}return L(o)}),w("RAND",0,0,()=>Math.random(),{volatile:!0}),w("RANDBETWEEN",2,2,(t,r)=>{const n=Math.ceil(c(t)),e=Math.floor(c(r));if(n>e)throw f.NUM;return n+Math.floor(Math.random()*(e-n+1))},{volatile:!0}),A("SUM",1,255,(t,r)=>{let n=0;const e={sum:0,count:0};for(const o of r)if(typeof o=="number")n+=o;else if(o instanceof g)t.sumRange(o,e);else if(o instanceof b)for(const s of o.refs)t.sumRange(s,e);else if(o instanceof E){for(const s of o.data)if(typeof s=="number")n+=s;else if(s instanceof V)throw s}else o!==null&&o!==d&&(n+=c(o));return L(n+e.sum)}),A("PRODUCT",1,255,(t,r)=>{let n=1,e=!1;for(const o of Y(t,r))n*=o,e=!0;return e?L(n):0}),A("SUMSQ",1,255,(t,r)=>L(Y(t,r).reduce((n,e)=>n+e*e,0)));function ct(t,r,n,e){return{row:t.r0+(n-r.r0),col:t.c0+(e-r.c0)}}function G(t,r,n){const[e,o]=n[0],s=e.rows,i=e.cols;for(const[U]of n)if(U.rows!==s||U.cols!==i)throw f.VALUE;let a=0,h=0;const u=(U,m)=>{for(let I=1;I<n.length;I++){const[C,D]=n[I];if(!D(t.read(C.sheet,C.r0+(U-e.r0),C.c0+(m-e.c0))))return!1}return!0},M=o(null),l=(U,m,I)=>{if(!(!o(U)||!u(m,I))&&(h++,r!==null)){const C=ct(r,e,m,I),D=t.read(r.sheet,C.row,C.col);if(D instanceof V)throw D;typeof D=="number"&&(a+=D)}};if(M){const U=Math.min(e.r1,Math.max(e.sheet.maxRow,e.r0)),m=Math.min(e.c1,Math.max(e.sheet.maxCol,e.c0));for(let I=e.c0;I<=m;I++)for(let C=e.r0;C<=U;C++)l(t.read(e.sheet,C,I),C,I)}else t.scan(e,l);return{sum:a,count:h}}const X=t=>{if(t instanceof V)throw t;if(!(t instanceof g))throw f.VALUE;return t},_=(t,r,n,e)=>{const o=t.scalar(r,n,e);return st(o)};A("SUMIF",2,3,(t,r,n,e)=>{const o=X(r[0]),s=r[2]===void 0||r[2]===d?o:X(r[2]);return G(t,s,[[o,_(t,r[1],n,e)]]).sum}),A("SUMIFS",3,255,(t,r,n,e)=>{if(r.length%2===0)throw f.VALUE;const o=[];for(let i=1;i<r.length;i+=2)o.push([X(r[i]),_(t,r[i+1],n,e)]);const s=X(r[0]);if(s.rows!==o[0][0].rows||s.cols!==o[0][0].cols)throw f.VALUE;return G(t,s,o).sum}),A("SUMPRODUCT",1,255,(t,r)=>{const n=r.map(a=>{if(a instanceof V)throw a;return t.toArr(a)}),{rows:e,cols:o}=n[0];for(const a of n)if(a.rows!==e||a.cols!==o)throw f.VALUE;let s=0;const i=e*o;for(let a=0;a<i;a++){let h=1;for(const u of n){const M=u.data[a];if(M instanceof V)throw M;h*=typeof M=="number"?M:0}s+=h}return L(s)},{array:!0});const B=(t,r)=>A(t,2,2,(n,e)=>{const o=n.toArr(e[0]).data,s=n.toArr(e[1]).data;if(o.length!==s.length)throw f.NA;let i=0,a=!1,h=!1;for(let u=0;u<o.length;u++){if(o[u]instanceof V)throw o[u];if(s[u]instanceof V)throw s[u];const M=typeof o[u]=="number",l=typeof s[u]=="number";a||=M,h||=l,M&&l&&(i+=r(o[u],s[u]))}if(!a||!h)throw f.DIV0;return L(i)},{array:!0});B("SUMX2MY2",(t,r)=>t*t-r*r),B("SUMX2PY2",(t,r)=>t*t+r*r),B("SUMXMY2",(t,r)=>(t-r)**2),A("SERIESSUM",4,4,(t,r,n,e)=>{const o=c(P(t,r[0],n,e)),s=c(P(t,r[1],n,e)),i=c(P(t,r[2],n,e));let a=0,h=0;for(const u of t.toArr(r[3]).data)a+=c(u)*o**(s+h*i),h++;return L(a)});function J(t,r,n,e,o,s,i=!0){const{sheet:a}=r;t.scan(r,(h,u,M)=>{if(!(!e&&a.filteredRows?.has(u)||!n&&a.rows.isHidden(u))&&!(i&&t.isSubtotalCell(a,u,M))){if(h instanceof V){if(o==="ignore")return;if(o==="throw")throw h}s.push(h)}})}function at(t,r){if(!r.filteredRows?.size)return!1;if(r.autoFilter?.columns.size||r.advancedFilter)return!0;for(const n of t.tables.values())if(n.sheet===r&&n.filter?.columns.size)return!0;return!1}function K(t,r,n){const e=r.length,o=()=>r.reduce((a,h)=>a+h,0),s=()=>o()/e,i=a=>{const h=o(),u=r.reduce((m,I)=>m+I*I,0),M=u-h*h/e,l=a?e:e-1;if(M>u*1e-8)return M/l;const U=h/e;return r.reduce((m,I)=>m+(I-U)**2,0)/l};switch(t){case 1:if(e===0)throw f.DIV0;return s();case 2:return e;case 3:return n.length;case 4:return e===0?0:Math.max(...r);case 5:return e===0?0:Math.min(...r);case 6:return e===0?0:r.reduce((a,h)=>a*h,1);case 7:if(e<2)throw f.DIV0;return Math.sqrt(i(!1));case 8:if(e===0)throw f.DIV0;return Math.sqrt(i(!0));case 9:return o();case 10:if(e<2)throw f.DIV0;return i(!1);case 11:if(e===0)throw f.DIV0;return i(!0);default:throw f.VALUE}}A("SUBTOTAL",2,255,(t,r,n,e)=>ot(r,o=>{const s=Math.trunc(c(t.scalar(o[0],n,e))),i=s>=1&&s<=11,a=s>100?s-100:s;if(a<1||a>11)throw f.VALUE;const h=a===2?"ignore":a===3?"count":"throw",u=[];for(let M=1;M<o.length;M++){const l=o[M]instanceof b?o[M].refs:[o[M]];for(const U of l){if(U instanceof V)throw U;if(!(U instanceof g))throw f.VALUE;J(t,U,i&&!at(t,U.sheet),!1,h,u)}}return K(a,u.filter(M=>typeof M=="number"),u)}));function y(t,r){const n=t.length;if(n===0||r<0||r>1)throw f.NUM;const e=r*(n-1),o=Math.floor(e),s=e-o;return s===0?t[o]:t[o]+s*(t[o+1]-t[o])}function q(t,r){const n=t.length,e=r*(n+1)-1;if(r<=0||r>=1||e<0||e>n-1)throw f.NUM;const o=Math.floor(e),s=e-o;return s===0?t[o]:t[o]+s*(t[o+1]-t[o])}A("AGGREGATE",3,255,(t,r,n,e)=>{const o=Math.trunc(c(t.scalar(r[0],n,e))),s=Math.trunc(c(t.scalar(r[1],n,e)));if(s<0||s>7)throw f.VALUE;const i=s===1||s===3||s===5||s===7,a=s===2||s===3||s===6||s===7,h=l=>{const U=[],m=l instanceof b?l.refs:[l];for(const I of m)if(I instanceof g)J(t,I,!i,!i,a?"ignore":"throw",U,s<4);else if(I instanceof E)for(const C of I.data)if(C instanceof V){if(!a)throw C}else C!==null&&U.push(C);else if(I instanceof V){if(!a)throw I}else I!==d&&I!==null&&I!==void 0&&U.push(c(I));return U};if(o>=1&&o<=13){for(let l=2;l<r.length;l++)if(r[l]instanceof V)throw r[l];for(let l=2;l<r.length;l++)if(!(r[l]instanceof g||r[l]instanceof b))throw f.VALUE}if(o>=1&&o<=13){const l=[];for(let m=2;m<r.length;m++)l.push(...h(r[m]));const U=l.filter(m=>typeof m=="number");return o===12?y(U.sort((m,I)=>m-I),.5):o===13?Z(U):K(o,U,l)}const u=h(r[2]).filter(l=>typeof l=="number").sort((l,U)=>l-U),M=c(t.scalar(r[3],n,e));switch(o){case 14:{const l=Math.ceil(M);if(l<1||l>u.length)throw f.NUM;return u[u.length-l]}case 15:{const l=Math.ceil(M);if(l<1||l>u.length)throw f.NUM;return u[l-1]}case 16:return y(u,M);case 17:return y(u,M/4);case 18:return q(u,M);case 19:return q(u,M/4);default:throw f.VALUE}},{array:!0});function Z(t){const r=new Map;for(const o of t)r.set(o,(r.get(o)??0)+1);let n=null,e=1;for(const[o,s]of r)s>e&&(n=o,e=s);if(n===null)throw f.NA;return n}function ht(t){const r=t.length,n=t.map(o=>o.slice());let e=1;for(let o=0;o<r;o++){let s=o;for(let i=o+1;i<r;i++)Math.abs(n[i][o])>Math.abs(n[s][o])&&(s=i);if(n[s][o]===0)return 0;s!==o&&([n[s],n[o]]=[n[o],n[s]],e=-e),e*=n[o][o];for(let i=o+1;i<r;i++){const a=n[i][o]/n[o][o];for(let h=o;h<r;h++)n[i][h]-=a*n[o][h]}}return e}A("MDETERM",1,1,(t,r)=>{const n=T(t,r[0]);if(n.length!==n[0].length)throw f.VALUE;return ht(n)},{array:!0}),A("MINVERSE",1,1,(t,r)=>{const n=T(t,r[0]),e=n.length;if(e!==n[0].length)throw f.VALUE;const o=n.map((s,i)=>[...s,...s.map((a,h)=>i===h?1:0)]);for(let s=0;s<e;s++){let i=s;for(let h=s+1;h<e;h++)Math.abs(o[h][s])>Math.abs(o[i][s])&&(i=h);if(Math.abs(o[i][s])<1e-300)throw f.NUM;[o[i],o[s]]=[o[s],o[i]];const a=o[s][s];for(let h=0;h<2*e;h++)o[s][h]/=a;for(let h=0;h<e;h++){if(h===s)continue;const u=o[h][s];for(let M=0;M<2*e;M++)o[h][M]-=u*o[s][M]}}return E.of2D(o.map(s=>s.slice(e)))},{array:!0}),A("MMULT",2,2,(t,r)=>{const n=T(t,r[0]),e=T(t,r[1]);if(n[0].length!==e.length)throw f.VALUE;const o=[];for(let s=0;s<n.length;s++){const i=[];for(let a=0;a<e[0].length;a++){let h=0;for(let u=0;u<e.length;u++)h+=n[s][u]*e[u][a];i.push(h)}o.push(i)}return E.of2D(o)},{array:!0}),w("MUNIT",1,1,t=>{const r=Math.trunc(c(t));if(r<1)throw f.VALUE;const n=new Array(r*r).fill(0);for(let e=0;e<r;e++)n[e*r+e]=1;return new E(r,r,n)});export{tt as bool,Z as modeOf,rt as opt,q as percentileExc,y as percentileInc,G as sumIfs};
1
+ import{setCategory as x}from"./registry.js";import{alias as tt,lift as N,raw as A}from"./registry.js";import{Arr as p,bool as rt,checked as d,ERR as f,int as O,num as a,OMITTED as L,opt as nt,power as et,Ref as E,RefList as b,str as Y,XlError as V}from"../values.js";import{rowsShown as j}from"../../core/shared-view.js";import{collectNumbers as z,eachValue as ot,liftReferenceArgs as st,makeCriteria as ft,matrixOf as T,toolpakScalar as P}from"./helpers.js";x("Math & Trig");const U=(t,r)=>N(t,1,1,n=>d(r(a(n)))),it=2**27,D=t=>r=>{if(Math.abs(r)>=it)throw f.NUM;return t(r)},Q=t=>r=>{if(!Number.isFinite(r*r))throw f.NUM;return t(r)},W=t=>r=>{if(!Number.isFinite(Math.exp(Math.abs(r))))throw f.NUM;return t(r)};U("ABS",Math.abs),U("ACOS",t=>{if(t<-1||t>1)throw f.NUM;return Math.acos(t)}),U("ACOSH",Q(t=>{if(t<1)throw f.NUM;return Math.acosh(t)})),U("ACOT",t=>Math.PI/2-Math.atan(t)),U("ACOTH",t=>{if(Math.abs(t)<=1)throw f.NUM;return .5*Math.log((t+1)/(t-1))}),U("ASIN",t=>{if(t<-1||t>1)throw f.NUM;return Math.asin(t)}),U("ASINH",Q(Math.asinh)),U("ATAN",Math.atan),U("ATANH",t=>{if(t<=-1||t>=1)throw f.NUM;return Math.atanh(t)}),U("COS",D(Math.cos)),U("COSH",W(Math.cosh)),U("COT",D(t=>{if(t===0)throw f.DIV0;return 1/Math.tan(t)})),U("COTH",t=>{if(t===0)throw f.DIV0;return 1/Math.tanh(t)}),U("CSC",D(t=>{if(t===0)throw f.DIV0;return 1/Math.sin(t)})),U("CSCH",t=>{if(t===0)throw f.DIV0;return 1/Math.sinh(t)}),U("DEGREES",t=>t*180/Math.PI),U("EXP",Math.exp),U("INT",t=>Number.isInteger(t)?t:Math.floor(Number(t.toPrecision(15)))),U("LN",t=>{if(t<=0)throw f.NUM;return Math.log(t)}),U("LOG10",t=>{if(t<=0)throw f.NUM;return Math.log10(t)}),U("RADIANS",t=>t*Math.PI/180),U("SEC",D(t=>1/Math.cos(t))),U("SECH",t=>1/Math.cosh(t)),U("SIGN",Math.sign),U("SIN",D(Math.sin)),U("SINH",W(Math.sinh)),U("SQRT",t=>{if(t<0)throw f.NUM;return Math.sqrt(t)}),U("SQRTPI",t=>{if(t<0)throw f.NUM;return Math.sqrt(t*Math.PI)}),U("TAN",D(Math.tan)),U("TANH",Math.tanh),N("ATAN2",2,2,(t,r)=>{const n=a(t),e=a(r);if(n===0&&e===0)throw f.DIV0;return Math.atan2(e,n)}),N("LOG",1,2,(t,r)=>{const n=a(t),e=r===void 0?10:r===L?0:a(r);if(n<=0||e<=0)throw f.NUM;if(e===1)throw f.DIV0;return e===10?Math.log10(n):Math.log(n)/Math.log(e)}),N("PI",0,0,()=>Math.PI),N("POWER",2,2,(t,r)=>{const n=a(t),e=a(r);if(n===0&&e===0)throw f.NUM;if(n===0&&e<0)throw f.DIV0;return d(et(n,e))}),N("MOD",2,2,(t,r)=>{const n=a(t),e=a(r);if(e===0)throw f.DIV0;if(Math.abs(n/e)>=2**40)throw f.NUM;return d(n-e*Math.floor(n/e))}),N("QUOTIENT",2,2,(t,r)=>{const n=a(t),e=a(r);if(e===0)throw f.DIV0;return Math.trunc(n/e)});function _(t,r){if(!Number.isFinite(t))throw f.NUM;r>15&&(r=15);const n=10**Math.abs(r),e=r>=0?Math.abs(t)*n:Math.abs(t)/n,o=Number(e.toPrecision(15)),s=Math.floor(o+.5),i=r>=0?s/n:s*n;return t<0?-i:i}function F(t,r,n){r>15&&(r=15);const e=10**Math.abs(r),o=Number(Math.abs(t).toPrecision(15)),s=r>=0?o*e:o/e,i=Number(s.toPrecision(15)),c=n?Math.ceil(i):Math.floor(i),h=r>=0?c/e:c*e;return t<0?-h:h}N("ROUND",2,2,(t,r)=>_(a(t),O(r))),N("ROUNDUP",2,2,(t,r)=>F(a(t),O(r),!0)),N("ROUNDDOWN",2,2,(t,r)=>F(a(t),O(r),!1)),N("TRUNC",1,2,(t,r)=>F(a(t),r===void 0||r===L?0:O(r),!1)),N("EVEN",1,1,t=>{const r=a(t),n=Math.ceil(Number((Math.abs(r)/2).toPrecision(15)))*2;return r<0?-n:n}),N("ODD",1,1,t=>{const r=Math.abs(a(t));let n=Math.ceil(Number(r.toPrecision(15)));return n%2===0&&(n+=1),a(t)<0?-n:n}),N("MROUND",2,2,(t,r)=>{const n=a(t),e=a(r);if(e===0)return 0;if(n*e<0)throw f.NUM;return _(n/e,0)*e});function S(t,r){const n=Number((t/r).toPrecision(15));return Math.ceil(n)*r}function R(t,r){const n=Number((t/r).toPrecision(15));return Math.floor(n)*r}N("CEILING",2,2,(t,r)=>{const n=a(t),e=a(r);if(e===0)return 0;if(n>0&&e<0)throw f.NUM;return n<0&&e<0?-S(-n,-e):S(n,e)}),N("FLOOR",2,2,(t,r)=>{const n=a(t),e=a(r);if(e===0){if(n===0)return 0;throw f.DIV0}if(n>0&&e<0)throw f.NUM;return n<0&&e<0?-R(-n,-e):R(n,e)}),N("CEILING.MATH",1,3,(t,r,n)=>{const e=a(t),o=Math.abs(r===void 0||r===L?1:a(r))||0;if(o===0)return 0;const s=n===void 0||n===L?0:a(n);return e<0&&s!==0?-S(-e,o):S(e,o)}),N("FLOOR.MATH",1,3,(t,r,n)=>{const e=a(t),o=Math.abs(r===void 0||r===L?1:a(r))||0;if(o===0)return 0;const s=n===void 0||n===L?0:a(n);return e<0&&s!==0?-R(-e,o):R(e,o)}),N("CEILING.PRECISE",1,2,(t,r)=>{const n=Math.abs(r===void 0||r===L?1:a(r));return n===0?0:S(a(t),n)}),tt("ISO.CEILING","CEILING.PRECISE"),N("FLOOR.PRECISE",1,2,(t,r)=>{const n=Math.abs(r===void 0||r===L?1:a(r));return n===0?0:R(a(t),n)});function ct(t){if(t<0)throw f.NUM;if(t>170)throw f.NUM;let r=1;for(let n=2;n<=t;n++)r*=n;return r}N("FACT",1,1,t=>ct(Math.floor(a(t)))),N("FACTDOUBLE",1,1,t=>{const r=Math.floor(a(t));if(r<-1)throw f.NUM;let n=1;for(let e=r;e>1;e-=2)if(n*=e,!Number.isFinite(n))throw f.NUM;return d(n)});function H(t,r){if(r<0||t<0||r>t)throw f.NUM;const n=Math.min(r,t-r);let e=1;for(let o=1;o<=n;o++)e=e*(t-n+o)/o;return d(Math.round(e))}N("COMBIN",2,2,(t,r)=>H(Math.trunc(a(t)),Math.trunc(a(r)))),N("COMBINA",2,2,(t,r)=>{const n=Math.trunc(a(t)),e=Math.trunc(a(r));if(n<0||e<0)throw f.NUM;if(n===0&&e===0)return 1;if(n===0)throw f.NUM;return H(n+e-1,e)});function $(t,r){for(;r!==0;)[t,r]=[r,t%r];return t}const v=(t,r)=>{const n=[];for(const e of r)if(e instanceof E||e instanceof b||e instanceof p)ot(t,e,o=>{if(o instanceof V)throw o;if(typeof o=="number"){if(o<0)throw f.NUM;n.push(Math.trunc(o))}else if(typeof o=="string"||typeof o=="boolean")throw f.VALUE});else{if(e===L)continue;{if(typeof e=="boolean")throw f.VALUE;const o=Math.trunc(a(e));if(o<0)throw f.NUM;n.push(o)}}return n};A("GCD",1,255,(t,r)=>v(t,r).reduce($,0)),A("LCM",1,255,(t,r)=>{let n=1;for(const e of v(t,r)){if(e===0)return 0;if(n=n/$(n,e)*e,n>=2**53)throw f.NUM}return n}),A("MULTINOMIAL",1,255,(t,r)=>{const n=v(t,r);if(n.length===0)throw f.VALUE;if(n.reduce((s,i)=>s+i,0)>170)throw f.NUM;let e=0,o=1;for(const s of n)e+=s,o*=H(e,s);return d(o)}),N("ARABIC",1,1,t=>{const r=Y(t).trim().toUpperCase();if(r==="")return 0;const n=r[0]==="-",e=n?r.slice(1):r,o={M:1e3,D:500,C:100,L:50,X:10,V:5,I:1};let s=0;for(let i=0;i<e.length;i++){const c=o[e[i]];if(c===void 0)throw f.VALUE;const h=o[e[i+1]]??0;s+=c<h?-c:c}return n?-s:s}),N("ROMAN",1,2,(t,r)=>{const n=Math.trunc(a(t)),e=r===void 0||r===L?0:typeof r=="boolean"?r?0:4:Math.trunc(a(r));if(n<0||n>3999)throw f.VALUE;if(e<0||e>4)throw f.VALUE;if(n===0)return"";const o={0:[[1e3,"M"],[900,"CM"],[500,"D"],[400,"CD"],[100,"C"],[90,"XC"],[50,"L"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],1:[[1e3,"M"],[950,"LM"],[900,"CM"],[500,"D"],[450,"LD"],[400,"CD"],[100,"C"],[95,"VC"],[90,"XC"],[50,"L"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],2:[[1e3,"M"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],3:[[1e3,"M"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]],4:[[1e3,"M"],[999,"IM"],[995,"VM"],[990,"XM"],[950,"LM"],[900,"CM"],[500,"D"],[499,"ID"],[495,"VD"],[490,"XD"],[450,"LD"],[400,"CD"],[100,"C"],[99,"IC"],[95,"VC"],[90,"XC"],[50,"L"],[49,"IL"],[45,"VL"],[40,"XL"],[10,"X"],[9,"IX"],[5,"V"],[4,"IV"],[1,"I"]]};let s=n,i="";for(const[c,h]of o[e])for(;s>=c;)i+=h,s-=c;return i}),N("BASE",2,3,(t,r,n)=>{const e=Math.trunc(a(t)),o=Math.trunc(a(r)),s=n===void 0||n===L?0:Math.trunc(a(n));if(e<0||e>=2**53||o<2||o>36||s<0||s>255)throw f.NUM;return e.toString(o).toUpperCase().padStart(s,"0")}),N("DECIMAL",2,2,(t,r)=>{const n=Math.trunc(a(r)),e=Y(t).toLowerCase().replace(n===2?/b$/:/$^/,"");if(n<2||n>36||e.length>255)throw f.NUM;let o=0;for(const s of e){const i=parseInt(s,36);if(Number.isNaN(i)||i>=n)throw f.NUM;o=o*n+i}return d(o)}),N("RAND",0,0,()=>Math.random(),{volatile:!0}),N("RANDBETWEEN",2,2,(t,r)=>{const n=Math.ceil(a(t)),e=Math.floor(a(r));if(n>e)throw f.NUM;return n+Math.floor(Math.random()*(e-n+1))},{volatile:!0}),A("SUM",1,255,(t,r)=>{let n=0;const e={sum:0,count:0};for(const o of r)if(typeof o=="number")n+=o;else if(o instanceof E)t.sumRange(o,e);else if(o instanceof b)for(const s of o.refs)t.sumRange(s,e);else if(o instanceof p){for(const s of o.data)if(typeof s=="number")n+=s;else if(s instanceof E)t.sumRange(s,e);else if(s instanceof V)throw s}else o!==null&&o!==L&&(n+=a(o));return d(n+e.sum)}),A("PRODUCT",1,255,(t,r)=>{let n=1,e=!1;for(const o of z(t,r.filter(s=>s!==L)))n*=o,e=!0;return e?d(n):0}),A("SUMSQ",1,255,(t,r)=>d(z(t,r).reduce((n,e)=>n+e*e,0)));function at(t,r,n,e){return{row:t.r0+(n-r.r0),col:t.c0+(e-r.c0)}}function G(t,r,n){const[e,o]=n[0],s=e.rows,i=e.cols;for(const[M]of n)if(M.rows!==s||M.cols!==i)throw f.VALUE;let c=0,h=0;const u=(M,m)=>{for(let I=1;I<n.length;I++){const[C,g]=n[I];if(!g(t.read(C.sheet,C.r0+(M-e.r0),C.c0+(m-e.c0))))return!1}return!0},w=o(null),l=(M,m,I)=>{if(!(!o(M)||!u(m,I))&&(h++,r!==null)){const C=at(r,e,m,I),g=t.read(r.sheet,C.row,C.col);if(g instanceof V)throw g;typeof g=="number"&&(c+=g)}};if(w){const M=Math.min(e.r1,Math.max(e.sheet.maxRow,e.r0)),m=Math.min(e.c1,Math.max(e.sheet.maxCol,e.c0));for(let I=e.c0;I<=m;I++)for(let C=e.r0;C<=M;C++)l(t.read(e.sheet,C,I),C,I)}else t.scan(e,l);return{sum:c,count:h}}const X=t=>{if(t instanceof V)throw t;if(!(t instanceof E))throw f.VALUE;return t},k=(t,r,n,e)=>{const o=t.scalar(r,n,e);return ft(o)};A("SUMIF",2,3,(t,r,n,e)=>{const o=X(r[0]),s=r[2]===void 0||r[2]===L?o:X(r[2]);return G(t,s,[[o,k(t,r[1],n,e)]]).sum}),A("SUMIFS",3,255,(t,r,n,e)=>{if(r.length%2===0)throw f.VALUE;const o=[];for(let i=1;i<r.length;i+=2)o.push([X(r[i]),k(t,r[i+1],n,e)]);const s=X(r[0]);if(s.rows!==o[0][0].rows||s.cols!==o[0][0].cols)throw f.VALUE;return G(t,s,o).sum}),A("SUMPRODUCT",1,255,(t,r)=>{const n=r.map(c=>{if(c instanceof V)throw c;if(c instanceof E&&c.isCell&&t.read(c.sheet,c.r0,c.c0)===null)throw f.VALUE;return t.toArr(c)}),{rows:e,cols:o}=n[0];for(const c of n)if(c.rows!==e||c.cols!==o)throw f.VALUE;let s=0;const i=e*o;for(let c=0;c<i;c++){let h=1;for(const u of n){const w=u.data[c];if(w instanceof V)throw w;h*=typeof w=="number"?w:0}s+=h}return d(s)},{array:!0});const B=(t,r)=>A(t,2,2,(n,e)=>{for(const u of e)if(u instanceof E&&u.isCell&&n.read(u.sheet,u.r0,u.c0)===null)throw f.VALUE;const o=n.toArr(e[0]).data,s=n.toArr(e[1]).data;if(o.length!==s.length)throw f.NA;let i=0,c=!1,h=!1;for(let u=0;u<o.length;u++){if(o[u]instanceof V)throw o[u];if(s[u]instanceof V)throw s[u];const w=typeof o[u]=="number",l=typeof s[u]=="number";c||=w,h||=l,w&&l&&(i+=r(o[u],s[u]))}if(!c||!h)throw f.DIV0;return d(i)},{array:!0});B("SUMX2MY2",(t,r)=>t*t-r*r),B("SUMX2PY2",(t,r)=>t*t+r*r),B("SUMXMY2",(t,r)=>(t-r)**2),A("SERIESSUM",4,4,(t,r,n,e)=>{const o=a(P(t,r[0],n,e)),s=a(P(t,r[1],n,e)),i=a(P(t,r[2],n,e));let c=0,h=0;for(const u of t.toArr(r[3]).data){if(typeof u=="boolean")throw f.VALUE;if(o===0&&s+h*i===0)throw f.NUM;c+=a(u)*o**(s+h*i),h++}return d(c)});function J(t,r,n,e,o,s,i=!0){const{sheet:c}=r,{filtered:h,isHidden:u}=j(t,c);t.scan(r,(w,l,M)=>{if(!(!e&&h?.has(l)||!n&&u(l))&&!(i&&t.isSubtotalCell(c,l,M))){if(w instanceof V){if(o==="ignore")return;if(o==="throw")throw w}s.push(w)}})}function ht(t,r){const{filtered:n,autoFilter:e,tables:o}=j(t,r);if(!n?.size)return!1;if(e?.columns.size||r.advancedFilter)return!0;for(const s of t.tables.values())if(s.sheet===r&&(o?o.get(s):s.filter)?.columns.size)return!0;return!1}function K(t,r,n){const e=r.length,o=()=>r.reduce((c,h)=>c+h,0),s=()=>o()/e,i=c=>{const h=o(),u=r.reduce((m,I)=>m+I*I,0),w=u-h*h/e,l=c?e:e-1;if(w>u*1e-8)return w/l;const M=h/e;return r.reduce((m,I)=>m+(I-M)**2,0)/l};switch(t){case 1:if(e===0)throw f.DIV0;return s();case 2:return e;case 3:return n.length;case 4:return e===0?0:Math.max(...r);case 5:return e===0?0:Math.min(...r);case 6:return e===0?0:r.reduce((c,h)=>c*h,1);case 7:if(e<2)throw f.DIV0;return Math.sqrt(i(!1));case 8:if(e===0)throw f.DIV0;return Math.sqrt(i(!0));case 9:return o();case 10:if(e<2)throw f.DIV0;return i(!1);case 11:if(e===0)throw f.DIV0;return i(!0);default:throw f.VALUE}}A("SUBTOTAL",2,255,(t,r,n,e)=>st(r,o=>{const s=Math.trunc(a(t.scalar(o[0],n,e))),i=s>=1&&s<=11,c=s>100?s-100:s;if(c<1||c>11)throw f.VALUE;const h=c===2?"ignore":c===3?"count":"throw",u=[];for(let w=1;w<o.length;w++){const l=o[w]instanceof b?o[w].refs:[o[w]];for(const M of l){if(M instanceof V)throw M;if(!(M instanceof E))throw f.VALUE;J(t,M,i&&!ht(t,M.sheet),!1,h,u)}}return K(c,u.filter(w=>typeof w=="number"),u)}));function y(t,r){const n=t.length;if(n===0||r<0||r>1)throw f.NUM;const e=r*(n-1),o=Math.floor(e),s=e-o;return s===0?t[o]:t[o]+s*(t[o+1]-t[o])}function q(t,r){const n=t.length,e=r*(n+1)-1;if(r<=0||r>=1||e<0||e>n-1)throw f.NUM;const o=Math.floor(e),s=e-o;return s===0?t[o]:t[o]+s*(t[o+1]-t[o])}A("AGGREGATE",3,255,(t,r,n,e)=>{const o=Math.trunc(a(t.scalar(r[0],n,e))),s=Math.trunc(a(t.scalar(r[1],n,e)));if(s<0||s>7)throw f.VALUE;const i=s===1||s===3||s===5||s===7,c=s===2||s===3||s===6||s===7,h=l=>{const M=[],m=l instanceof b?l.refs:[l];for(const I of m)if(I instanceof E)J(t,I,!i,!i,c?"ignore":"throw",M,s<4);else if(I instanceof p)for(const C of I.data)if(C instanceof V){if(!c)throw C}else C!==null&&M.push(C);else if(I instanceof V){if(!c)throw I}else I!==L&&I!==null&&I!==void 0&&M.push(a(I));return M};if(o>=1&&o<=13){for(let l=2;l<r.length;l++)if(r[l]instanceof V)throw r[l];for(let l=2;l<r.length;l++)if(!(r[l]instanceof E||r[l]instanceof b))throw f.VALUE}if(o>=14&&o<=19&&r.length<4)throw f.VALUE;if(o>=1&&o<=13){const l=[];for(let m=2;m<r.length;m++)l.push(...h(r[m]));const M=l.filter(m=>typeof m=="number");return o===12?y(M.sort((m,I)=>m-I),.5):o===13?Z(M):K(o,M,l)}const u=h(r[2]).filter(l=>typeof l=="number").sort((l,M)=>l-M),w=l=>{switch(o){case 14:{const M=Math.ceil(l);if(M<1||M>u.length)throw f.NUM;return u[u.length-M]}case 15:{const M=Math.ceil(l);if(M<1||M>u.length)throw f.NUM;return u[M-1]}case 16:return y(u,l);case 17:return y(u,l/4);case 18:return q(u,l);case 19:return q(u,l/4);default:throw f.VALUE}};return r[3]instanceof E&&!r[3].isCell&&!t.legacy?w(a(t.read(r[3].sheet,r[3].r0,r[3].c0))):w(a(t.scalar(r[3],n,e)))},{array:!0});function Z(t){const r=new Map;for(const o of t)r.set(o,(r.get(o)??0)+1);let n=null,e=1;for(const[o,s]of r)s>e&&(n=o,e=s);if(n===null)throw f.NA;return n}function ut(t){const r=t.length,n=t.map(o=>o.slice());let e=1;for(let o=0;o<r;o++){let s=o;for(let i=o+1;i<r;i++)Math.abs(n[i][o])>Math.abs(n[s][o])&&(s=i);if(n[s][o]===0)return 0;s!==o&&([n[s],n[o]]=[n[o],n[s]],e=-e),e*=n[o][o];for(let i=o+1;i<r;i++){const c=n[i][o]/n[o][o];for(let h=o;h<r;h++)n[i][h]-=c*n[o][h]}}return e}A("MDETERM",1,1,(t,r)=>{const n=T(t,r[0]);if(n.length!==n[0].length)throw f.VALUE;return ut(n)},{array:!0}),A("MINVERSE",1,1,(t,r)=>{const n=T(t,r[0]),e=n.length;if(e!==n[0].length)throw f.VALUE;const o=n.map((s,i)=>[...s,...s.map((c,h)=>i===h?1:0)]);for(let s=0;s<e;s++){let i=s;for(let h=s+1;h<e;h++)Math.abs(o[h][s])>Math.abs(o[i][s])&&(i=h);if(Math.abs(o[i][s])<1e-300)throw f.NUM;[o[i],o[s]]=[o[s],o[i]];const c=o[s][s];for(let h=0;h<2*e;h++)o[s][h]/=c;for(let h=0;h<e;h++){if(h===s)continue;const u=o[h][s];for(let w=0;w<2*e;w++)o[h][w]-=u*o[s][w]}}return p.of2D(o.map(s=>s.slice(e)))},{array:!0}),A("MMULT",2,2,(t,r)=>{const n=T(t,r[0]),e=T(t,r[1]);if(n[0].length!==e.length)throw f.VALUE;const o=[];for(let s=0;s<n.length;s++){const i=[];for(let c=0;c<e[0].length;c++){let h=0;for(let u=0;u<e.length;u++)h+=n[s][u]*e[u][c];i.push(d(h))}o.push(i)}return p.of2D(o)},{array:!0}),N("MUNIT",1,1,t=>{const r=Math.trunc(a(t));if(r<1)throw f.VALUE;const n=new Array(r*r).fill(0);for(let e=0;e<r;e++)n[e*r+e]=1;return new p(r,r,n)});export{rt as bool,Z as modeOf,nt as opt,q as percentileExc,y as percentileInc,G as sumIfs};