@allyhub/sdk 0.15.2 → 0.17.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 (131) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/node_modules/@allyhub/audio/dist/index.d.ts +1 -0
  3. package/dist/node_modules/@allyhub/audio/dist/index.js +1 -1
  4. package/dist/node_modules/@allyhub/audio/dist/place.d.ts +26 -0
  5. package/dist/node_modules/@allyhub/audio/dist/place.js +2 -0
  6. package/dist/node_modules/@allyhub/audio/package.json +1 -1
  7. package/dist/node_modules/@allyhub/chart/package.json +1 -1
  8. package/dist/node_modules/@allyhub/diagnostics/package.json +1 -1
  9. package/dist/node_modules/@allyhub/formats/dist/data/slides.d.ts +15 -3
  10. package/dist/node_modules/@allyhub/formats/dist/data/slides.js +1 -1
  11. package/dist/node_modules/@allyhub/formats/dist/data/website.js +1 -1
  12. package/dist/node_modules/@allyhub/formats/dist/items.d.ts +70 -11
  13. package/dist/node_modules/@allyhub/formats/dist/items.js +1 -1
  14. package/dist/node_modules/@allyhub/formats/dist/layout.d.ts +15 -4
  15. package/dist/node_modules/@allyhub/formats/dist/layout.js +1 -1
  16. package/dist/node_modules/@allyhub/formats/dist/schemas.d.ts +4 -0
  17. package/dist/node_modules/@allyhub/formats/package.json +1 -1
  18. package/dist/node_modules/@allyhub/image-draw/dist/stage.js +1 -1
  19. package/dist/node_modules/@allyhub/image-draw/package.json +1 -1
  20. package/dist/node_modules/@allyhub/image-measure/package.json +1 -1
  21. package/dist/node_modules/@allyhub/image-sdk/package.json +1 -1
  22. package/dist/node_modules/@allyhub/measure/package.json +1 -1
  23. package/dist/node_modules/@allyhub/ooxml/package.json +1 -1
  24. package/dist/node_modules/@allyhub/piece/dist/layer.d.ts +15 -0
  25. package/dist/node_modules/@allyhub/piece/dist/page/index.d.ts +1 -0
  26. package/dist/node_modules/@allyhub/piece/dist/page/index.js +1 -1
  27. package/dist/node_modules/@allyhub/piece/dist/page/runtimes.d.ts +9 -0
  28. package/dist/node_modules/@allyhub/piece/dist/page/runtimes.js +2 -0
  29. package/dist/node_modules/@allyhub/piece/dist/source.d.ts +53 -10
  30. package/dist/node_modules/@allyhub/piece/dist/source.js +1 -1
  31. package/dist/node_modules/@allyhub/piece/package.json +1 -1
  32. package/dist/node_modules/@allyhub/protocol/dist/account.d.ts +10 -11
  33. package/dist/node_modules/@allyhub/protocol/dist/account.js +1 -1
  34. package/dist/node_modules/@allyhub/protocol/dist/auth.d.ts +7 -0
  35. package/dist/node_modules/@allyhub/protocol/dist/auth.js +1 -1
  36. package/dist/node_modules/@allyhub/protocol/dist/blobs.d.ts +9 -0
  37. package/dist/node_modules/@allyhub/protocol/dist/blobs.js +1 -1
  38. package/dist/node_modules/@allyhub/protocol/dist/cli.d.ts +2 -0
  39. package/dist/node_modules/@allyhub/protocol/dist/cli.js +1 -1
  40. package/dist/node_modules/@allyhub/protocol/dist/cloud-computer.d.ts +77 -0
  41. package/dist/node_modules/@allyhub/protocol/dist/cloud-computer.js +2 -0
  42. package/dist/node_modules/@allyhub/protocol/dist/cloud.d.ts +39 -9
  43. package/dist/node_modules/@allyhub/protocol/dist/cloud.js +1 -1
  44. package/dist/node_modules/@allyhub/protocol/dist/editor.d.ts +15 -15
  45. package/dist/node_modules/@allyhub/protocol/dist/events.d.ts +441 -5
  46. package/dist/node_modules/@allyhub/protocol/dist/events.js +1 -1
  47. package/dist/node_modules/@allyhub/protocol/dist/files.d.ts +2 -2
  48. package/dist/node_modules/@allyhub/protocol/dist/frame.d.ts +4 -1
  49. package/dist/node_modules/@allyhub/protocol/dist/frame.js +1 -1
  50. package/dist/node_modules/@allyhub/protocol/dist/index.d.ts +8 -0
  51. package/dist/node_modules/@allyhub/protocol/dist/index.js +1 -1
  52. package/dist/node_modules/@allyhub/protocol/dist/jobs.d.ts +5 -5
  53. package/dist/node_modules/@allyhub/protocol/dist/kinds.d.ts +45 -0
  54. package/dist/node_modules/@allyhub/protocol/dist/kinds.js +1 -1
  55. package/dist/node_modules/@allyhub/protocol/dist/paywall.d.ts +134 -0
  56. package/dist/node_modules/@allyhub/protocol/dist/paywall.js +2 -0
  57. package/dist/node_modules/@allyhub/protocol/dist/place.js +1 -1
  58. package/dist/node_modules/@allyhub/protocol/dist/presence.d.ts +3 -3
  59. package/dist/node_modules/@allyhub/protocol/dist/purposes.d.ts +47 -0
  60. package/dist/node_modules/@allyhub/protocol/dist/purposes.js +2 -0
  61. package/dist/node_modules/@allyhub/protocol/dist/routes.js +1 -1
  62. package/dist/node_modules/@allyhub/protocol/dist/shares.d.ts +52 -5
  63. package/dist/node_modules/@allyhub/protocol/dist/shares.js +1 -1
  64. package/dist/node_modules/@allyhub/protocol/dist/sidebar-deletion.d.ts +34 -0
  65. package/dist/node_modules/@allyhub/protocol/dist/sidebar-deletion.js +2 -0
  66. package/dist/node_modules/@allyhub/protocol/dist/sites.d.ts +1 -0
  67. package/dist/node_modules/@allyhub/protocol/dist/snapshots.d.ts +21 -52
  68. package/dist/node_modules/@allyhub/protocol/dist/snapshots.js +1 -1
  69. package/dist/node_modules/@allyhub/protocol/dist/studio-server.d.ts +264 -0
  70. package/dist/node_modules/@allyhub/protocol/dist/studio-server.js +2 -0
  71. package/dist/node_modules/@allyhub/protocol/dist/studio-tool-calls.d.ts +18 -0
  72. package/dist/node_modules/@allyhub/protocol/dist/studio-tool-calls.js +2 -0
  73. package/dist/node_modules/@allyhub/protocol/dist/studio-window-requests.d.ts +97 -0
  74. package/dist/node_modules/@allyhub/protocol/dist/studio-window-requests.js +2 -0
  75. package/dist/node_modules/@allyhub/protocol/dist/teams.d.ts +3 -2
  76. package/dist/node_modules/@allyhub/protocol/dist/teams.js +1 -1
  77. package/dist/node_modules/@allyhub/protocol/dist/tools.d.ts +26 -20
  78. package/dist/node_modules/@allyhub/protocol/dist/tools.js +1 -1
  79. package/dist/node_modules/@allyhub/protocol/dist/turns.d.ts +71 -0
  80. package/dist/node_modules/@allyhub/protocol/dist/turns.js +2 -0
  81. package/dist/node_modules/@allyhub/protocol/package.json +1 -1
  82. package/dist/node_modules/@allyhub/runtimes/package.json +1 -1
  83. package/dist/node_modules/@allyhub/scenery/package.json +1 -1
  84. package/dist/node_modules/@allyhub/sheet/dist/calc/coerce.js +1 -1
  85. package/dist/node_modules/@allyhub/sheet/dist/calc/engine.d.ts +12 -0
  86. package/dist/node_modules/@allyhub/sheet/dist/calc/engine.js +1 -1
  87. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.js +1 -1
  88. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/date.js +1 -1
  89. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/dynamic.js +1 -1
  90. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/info.js +1 -1
  91. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/logical.js +1 -1
  92. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/lookup.js +1 -1
  93. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/text.js +1 -1
  94. package/dist/node_modules/@allyhub/sheet/dist/formula/parse.js +1 -1
  95. package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.d.ts +1 -1
  96. package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.js +1 -1
  97. package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.js +1 -1
  98. package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.js +2 -2
  99. package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.js +1 -1
  100. package/dist/node_modules/@allyhub/sheet/package.json +1 -1
  101. package/dist/node_modules/@allyhub/slides-core/dist/animation.d.ts +10 -0
  102. package/dist/node_modules/@allyhub/slides-core/dist/animation.js +1 -1
  103. package/dist/node_modules/@allyhub/slides-core/dist/api.js +1 -1
  104. package/dist/node_modules/@allyhub/slides-core/dist/components.js +1 -1
  105. package/dist/node_modules/@allyhub/slides-core/dist/model.d.ts +3 -1
  106. package/dist/node_modules/@allyhub/slides-core/dist/resolve.d.ts +5 -0
  107. package/dist/node_modules/@allyhub/slides-core/dist/resolve.js +1 -1
  108. package/dist/node_modules/@allyhub/slides-core/package.json +1 -1
  109. package/dist/node_modules/@allyhub/video-core/package.json +1 -1
  110. package/dist/node_modules/@allyhub/video-sdk/dist/free.d.ts +1 -1
  111. package/dist/node_modules/@allyhub/video-sdk/package.json +1 -1
  112. package/docs/batches.md +4 -4
  113. package/docs/contract.md +5 -5
  114. package/docs/defaults.md +2 -2
  115. package/docs/docs.md +16 -5
  116. package/docs/footage.md +8 -8
  117. package/docs/image.md +5 -5
  118. package/docs/layers.md +12 -10
  119. package/docs/linkedin.md +1 -1
  120. package/docs/modeling.md +3 -3
  121. package/docs/pictures.md +4 -4
  122. package/docs/project.md +11 -7
  123. package/docs/scenery.md +3 -3
  124. package/docs/sheets.md +5 -5
  125. package/docs/slides.md +3 -3
  126. package/docs/sound.md +7 -7
  127. package/docs/speech.md +7 -7
  128. package/docs/video.md +3 -3
  129. package/docs/websites.md +3 -3
  130. package/docs/x.md +2 -2
  131. package/package.json +1 -1
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{MAX_COLUMNS as P,MAX_ROWS as B}from"../../address.js";import{compareForSort as I}from"../../sort.js";import{CellError as u,ERR as a,Matrix as m}from"../../values.js";import{toBool as Y,toNumber as x,toText as D}from"../coerce.js";import{Lambda as O,Missing as V}from"../types.js";import{cellsOf as w,raw as p}from"./define.js";import{delimiters as S,splitAt as R}from"./delimiters.js";const K=5e6;export const rowsOfCells=n=>Array.from({length:n.rows},(e,r)=>Array.from({length:n.cols},(t,o)=>n.at(r,o))),matrixOf=n=>{const e=n[0]?.length??0;return new m(n.length,e,n.flat())},transposed=n=>{const e=n[0]?.length??0;return Array.from({length:e},(r,t)=>n.map(o=>o[t]??null))};function E(n,e){if(!(n===void 0||n instanceof V))return e.calc.scalar(n,e.frame)}export function numberOf(n,e,r){const t=E(n,e);return t==null?r:x(t)}export function boolOf(n,e,r){const t=E(n,e);return t==null?r:Y(t)}export function indexes(n,e,r){if(n===void 0||n instanceof V)return[r];const t=w(n,e);if(t instanceof u)return t;const o=[];for(let s=0;s<t.rows;s++)for(let c=0;c<t.cols;c++){const i=x(t.at(s,c));if(i instanceof u)return i;o.push(Math.trunc(i))}return o}const U=n=>n===null||n==="";export function compareBy(n,e,r){if(U(n)||U(e))return U(n)&&U(e)?0:U(n)?1:-1;const t=I(n,e);return r?-t:t}export function included(n){return n instanceof u?n:n===null?!1:typeof n=="boolean"?n:typeof n=="number"?n!==0:a.VALUE}p("FILTER","rr?r",(n,e)=>{const r=w(n[0],e),t=w(n[1],e);if(r instanceof u)return r;if(t instanceof u)return t;if(t.rows>1&&t.cols>1)return a.VALUE;const o=t.cols===1&&t.rows===r.rows?!0:t.rows===1&&t.cols===r.cols?!1:void 0;if(o===void 0)return a.VALUE;const s=rowsOfCells(r),c=o?s:transposed(s),i=[];for(let f=0;f<c.length;f++){const l=included(o?t.at(f,0):t.at(0,f));if(l instanceof u)return l;l&&i.push(c[f]??[])}return i.length===0?n[2]===void 0||n[2]instanceof V?a.CALC:n[2]:matrixOf(o?i:transposed(i))}),p("SORT","r?rrr",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=indexes(n[1],e,1);if(t instanceof u)return t;const o=indexes(n[2],e,1);if(o instanceof u)return o;const s=boolOf(n[3],e,!1);if(s instanceof u)return s;const c=rowsOfCells(r),i=s?transposed(c):c,f=i[0]?.length??0;if(o.length!==1&&o.length!==t.length)return a.VALUE;if(t.some(h=>h<1||h>f))return a.VALUE;if(o.some(h=>h!==1&&h!==-1))return a.VALUE;const l=i.map((h,A)=>({line:h,index:A})).sort((h,A)=>{for(const[L,d]of t.entries()){const y=o[o.length===1?0:L]??1,C=compareBy(h.line[d-1]??null,A.line[d-1]??null,y<0);if(C!==0)return C}return h.index-A.index}).map(h=>h.line);return matrixOf(s?transposed(l):l)}),p("SORTBY","r(r)",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=[];let o;for(let f=1;f<n.length;f+=2){const l=w(n[f],e);if(l instanceof u)return l;if(l.rows>1&&l.cols>1)return a.VALUE;const h=l.cols===1&&l.rows===r.rows,A=l.rows===1&&l.cols===r.cols;if(!h&&!A)return a.VALUE;const L=o??h;if(L?!h:!A)return a.VALUE;o=L;const d=numberOf(n[f+1],e,1);if(d instanceof u)return d;if(d!==1&&d!==-1)return a.VALUE;t.push({values:rowsOfCells(l).flat(),descending:d<0})}const s=rowsOfCells(r),i=(o===!1?transposed(s):s).map((f,l)=>({line:f,index:l})).sort((f,l)=>{for(const{values:h,descending:A}of t){const L=compareBy(h[f.index]??null,h[l.index]??null,A);if(L!==0)return L}return f.index-l.index}).map(f=>f.line);return matrixOf(o===!1?transposed(i):i)});export const keyOf=n=>n instanceof u?`e${n.code}`:typeof n=="string"?`s${n.toLowerCase()}`:`${typeof n}${String(n)}`;p("UNIQUE","r?rr",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=boolOf(n[1],e,!1);if(t instanceof u)return t;const o=boolOf(n[2],e,!1);if(o instanceof u)return o;const s=rowsOfCells(r),c=t?transposed(s):s,i=new Map;for(const l of c){const h=l.map(keyOf).join("\0"),A=i.get(h);A?A.count+=1:i.set(h,{line:l,count:1})}const f=[...i.values()].filter(l=>!o||l.count===1).map(l=>l.line);return f.length===0?a.CALC:matrixOf(t?transposed(f):f)});function M(n,e){if(n instanceof u)return n;if(e instanceof u)return e;const r=Math.trunc(n),t=Math.trunc(e);return r<0||t<0?a.VALUE:r===0||t===0?a.CALC:r>B||t>P||r*t>K?a.VALUE:{rows:r,cols:t}}p("SEQUENCE","r?rrr",(n,e)=>{const r=M(numberOf(n[0],e,1),numberOf(n[1],e,1));if(r instanceof u)return r;const t=numberOf(n[2],e,1);if(t instanceof u)return t;const o=numberOf(n[3],e,1);if(o instanceof u)return o;const s=new Array(r.rows*r.cols);for(let c=0;c<s.length;c++)s[c]=t+c*o;return new m(r.rows,r.cols,s)}),p("RANDARRAY","?rrrrr",(n,e)=>{const r=M(numberOf(n[0],e,1),numberOf(n[1],e,1));if(r instanceof u)return r;const t=numberOf(n[2],e,0);if(t instanceof u)return t;const o=numberOf(n[3],e,1);if(o instanceof u)return o;const s=boolOf(n[4],e,!1);if(s instanceof u)return s;if(t>o)return a.VALUE;if(s&&(!Number.isInteger(t)||!Number.isInteger(o)))return a.VALUE;const c=new Array(r.rows*r.cols);for(let i=0;i<c.length;i++){const f=e.calc.random();c[i]=s?t+Math.floor(f*(o-t+1)):t+f*(o-t)}return new m(r.rows,r.cols,c)},{volatile:!0}),p("TEXTSPLIT","vr?rvvv",(n,e)=>{const r=E(n[0],e);if(r instanceof u)return r;const t=D(r??"");if(t instanceof u)return t;const o=S(n[1],e);if(o instanceof u)return o;const s=S(n[2],e);if(s instanceof u)return s;if(o.length===0&&s.length===0)return a.VALUE;if(o.includes("")||s.includes(""))return a.VALUE;const c=boolOf(n[3],e,!1);if(c instanceof u)return c;const i=numberOf(n[4],e,0);if(i instanceof u)return i;const f=E(n[5],e)??a.NA,l=i===1,A=(s.length===0?[t]:R(t,s,l)).filter(d=>!(c&&d==="")).map(d=>{const y=o.length===0?[d]:R(d,o,l);return c?y.filter(C=>C!==""):y}),L=Math.max(0,...A.map(d=>d.length));return A.length===0||L===0?a.CALC:matrixOf(A.map(d=>Array.from({length:L},(y,C)=>d[C]??f)))});export const oneOf=(n,e)=>{const r=e.calc.flat(n);return r instanceof m?r.at(0,0):r};p("MAP","r(r)",(n,e)=>{const r=n[n.length-1];if(!(r instanceof O))return a.VALUE;const t=[];for(const f of n.slice(0,-1)){const l=w(f,e);if(l instanceof u)return l;t.push(l)}const o=t[0];if(!o)return a.VALUE;const{rows:s,cols:c}=o;if(t.some(f=>f.rows!==s||f.cols!==c))return a.VALUE;const i=new Array(s*c);for(let f=0;f<s;f++)for(let l=0;l<c;l++)i[f*c+l]=oneOf(e.calc.apply(r,t.map(h=>h.at(f,l))),e);return new m(s,c,i)});function N(n,e){const r=n.length===3,t=n[n.length-1];if(!(t instanceof O))return a.VALUE;const o=w(n[r?1:0],e);return o instanceof u?o:{start:r?E(n[0],e)??null:o.at(0,0),cells:o,fn:t}}p("REDUCE","rr?r",(n,e)=>{const r=N(n,e);if(r instanceof u)return r;const{cells:t,fn:o}=r,s=n.length===3?0:1;let c=r.start;for(let i=s;i<t.rows*t.cols;i++)c=oneOf(e.calc.apply(o,[c,t.at(Math.floor(i/t.cols),i%t.cols)]),e);return c}),p("SCAN","rr?r",(n,e)=>{const r=N(n,e);if(r instanceof u)return r;const{cells:t,fn:o}=r,s=t.rows*t.cols,c=n.length===3?0:1,i=new Array(s);let f=r.start;c===1&&(i[0]=f);for(let l=c;l<s;l++)f=oneOf(e.calc.apply(o,[f,t.at(Math.floor(l/t.cols),l%t.cols)]),e),i[l]=f;return new m(t.rows,t.cols,i)});const T=n=>(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=e[1];if(!(o instanceof O))return a.VALUE;const s=rowsOfCells(t),i=(n?s:transposed(s)).map(f=>oneOf(r.calc.apply(o,[n?new m(1,f.length,f):new m(f.length,1,f)]),r));return n?new m(i.length,1,i):new m(1,i.length,i)};p("BYROW","rr",T(!0)),p("BYCOL","rr",T(!1)),p("MAKEARRAY","rrr",(n,e)=>{const r=M(numberOf(n[0],e,1),numberOf(n[1],e,1));if(r instanceof u)return r;const t=n[2];if(!(t instanceof O))return a.VALUE;const o=new Array(r.rows*r.cols);for(let s=0;s<r.rows;s++)for(let c=0;c<r.cols;c++)o[s*r.cols+c]=oneOf(e.calc.apply(t,[s+1,c+1]),e);return new m(r.rows,r.cols,o)});function b(n){return(e,r)=>{const t=[];for(const c of e){const i=w(c,r);if(i instanceof u)return i;const f=rowsOfCells(i);t.push(n?f:transposed(f))}const o=Math.max(0,...t.map(c=>c[0]?.length??0)),s=t.flatMap(c=>c.map(i=>Array.from({length:o},(f,l)=>l<i.length?i[l]??null:a.NA)));return s.length===0?a.CALC:matrixOf(n?s:transposed(s))}}p("VSTACK","(r)",b(!0)),p("HSTACK","(r)",b(!1));function _(n,e,r){const t=Math.min(n,Math.abs(e));return r?e>=0?[0,t]:[n-t,n]:e>=0?[t,n]:[0,n-t]}function k(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=A=>A===void 0||A instanceof V,s=numberOf(e[1],r,n?t.rows:0);if(s instanceof u)return s;const c=numberOf(e[2],r,n?t.cols:0);if(c instanceof u)return c;if(n&&!o(e[1])&&Math.trunc(s)===0)return a.CALC;if(n&&!o(e[2])&&Math.trunc(c)===0)return a.CALC;const[i,f]=_(t.rows,Math.trunc(s),n),[l,h]=_(t.cols,Math.trunc(c),n);return f<=i||h<=l?a.CALC:matrixOf(rowsOfCells(t).slice(i,f).map(A=>A.slice(l,h)))}}p("TAKE","rr?r",k(!0)),p("DROP","rr?r",k(!1));function z(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=numberOf(e[1],r,0);if(o instanceof u)return o;const s=boolOf(e[2],r,!1);if(s instanceof u)return s;if(![0,1,2,3].includes(o))return a.VALUE;const c=rowsOfCells(t),f=(s?transposed(c):c).flat().filter(l=>!((o===1||o===3)&&l===null||(o===2||o===3)&&l instanceof u));return f.length===0?a.CALC:n?new m(f.length,1,f):new m(1,f.length,f)}}p("TOCOL","r?rr",z(!0)),p("TOROW","r?rr",z(!1));function g(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;const o=rowsOfCells(t),s=n?o:transposed(o),c=[];for(const i of e.slice(1)){const f=indexes(i,r,1);if(f instanceof u)return f;for(const l of f){const h=s[l>0?l-1:s.length+l];if(l===0||!h)return a.VALUE;c.push(h)}}return matrixOf(n?c:transposed(c))}}p("CHOOSEROWS","r(r)",g(!0)),p("CHOOSECOLS","r(r)",g(!1)),p("EXPAND","rr?rr",(n,e)=>{const r=w(n[0],e);if(r instanceof u)return r;const t=M(numberOf(n[1],e,r.rows),numberOf(n[2],e,r.cols));if(t instanceof u)return t;if(t.rows<r.rows||t.cols<r.cols)return a.VALUE;const o=E(n[3],e)??a.NA,s=new Array(t.rows*t.cols);for(let c=0;c<t.rows;c++)for(let i=0;i<t.cols;i++)s[c*t.cols+i]=c<r.rows&&i<r.cols?r.at(c,i):o;return new m(t.rows,t.cols,s)});function W(n){return(e,r)=>{const t=w(e[0],r);if(t instanceof u)return t;if(t.rows>1&&t.cols>1)return a.VALUE;const o=numberOf(e[1],r,1);if(o instanceof u)return o;const s=Math.trunc(o);if(s<1)return a.NUM;const c=E(e[2],r)??a.NA,i=rowsOfCells(t).flat(),f=[];for(let l=0;l<i.length;l+=s)f.push(Array.from({length:s},(h,A)=>l+A<i.length?i[l+A]??null:c));return matrixOf(n?f:transposed(f))}}p("WRAPROWS","rr?r",W(!0)),p("WRAPCOLS","rr?r",W(!1));
2
+ import{MAX_COLUMNS as B,MAX_ROWS as I}from"../../address.js";import{compareForSort as Y}from"../../sort.js";import{CellError as u,ERR as a,Matrix as L}from"../../values.js";import{toBool as D,toNumber as x,toText as K}from"../coerce.js";import{Lambda as O,Missing as V}from"../types.js";import{cellsOf as m,raw as A}from"./define.js";import{delimiters as S,splitAt as R}from"./delimiters.js";const N=5e6;export const rowsOfCells=t=>Array.from({length:t.rows},(e,r)=>Array.from({length:t.cols},(n,o)=>t.at(r,o))),matrixOf=t=>{const e=t[0]?.length??0;return new L(t.length,e,t.flat())},transposed=t=>{const e=t[0]?.length??0;return Array.from({length:e},(r,n)=>t.map(o=>o[n]??null))};function y(t,e){if(!(t===void 0||t instanceof V))return e.calc.scalar(t,e.frame)}export function numberOf(t,e,r){const n=y(t,e);return n==null?r:x(n)}export function boolOf(t,e,r){const n=y(t,e);return n==null?r:D(n)}export function indexes(t,e,r){if(t===void 0||t instanceof V)return[r];const n=m(t,e);if(n instanceof u)return n;const o=[];for(let s=0;s<n.rows;s++)for(let c=0;c<n.cols;c++){const i=x(n.at(s,c));if(i instanceof u)return i;o.push(Math.trunc(i))}return o}const U=t=>t===null||t==="";export function compareBy(t,e,r){if(U(t)||U(e))return U(t)&&U(e)?0:U(t)?1:-1;const n=Y(t,e);return r?-n:n}export function included(t){return t instanceof u?t:t===null?!1:typeof t=="boolean"?t:typeof t=="number"?t!==0:a.VALUE}A("FILTER","rr?r",(t,e)=>{const r=m(t[0],e),n=m(t[1],e);if(r instanceof u)return r;if(n instanceof u)return n;if(n.rows>1&&n.cols>1)return a.VALUE;const o=n.cols===1&&n.rows===r.rows?!0:n.rows===1&&n.cols===r.cols?!1:void 0;if(o===void 0)return a.VALUE;const s=rowsOfCells(r),c=o?s:transposed(s),i=[];for(let f=0;f<c.length;f++){const l=included(o?n.at(f,0):n.at(0,f));if(l instanceof u)return l;l&&i.push(c[f]??[])}return i.length===0?t[2]===void 0||t[2]instanceof V?a.CALC:t[2]:matrixOf(o?i:transposed(i))}),A("SORT","r?rrr",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=indexes(t[1],e,1);if(n instanceof u)return n;const o=indexes(t[2],e,1);if(o instanceof u)return o;const s=boolOf(t[3],e,!1);if(s instanceof u)return s;const c=rowsOfCells(r),i=s?transposed(c):c,f=i[0]?.length??0;if(o.length!==1&&o.length!==n.length)return a.VALUE;if(n.some(h=>h<1||h>f))return a.VALUE;if(o.some(h=>h!==1&&h!==-1))return a.VALUE;const l=i.map((h,p)=>({line:h,index:p})).sort((h,p)=>{for(const[w,d]of n.entries()){const C=o[o.length===1?0:w]??1,E=compareBy(h.line[d-1]??null,p.line[d-1]??null,C<0);if(E!==0)return E}return h.index-p.index}).map(h=>h.line);return matrixOf(s?transposed(l):l)}),A("SORTBY","r(r)",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=[];let o;for(let f=1;f<t.length;f+=2){const l=m(t[f],e);if(l instanceof u)return l;if(l.rows>1&&l.cols>1)return a.VALUE;const h=l.cols===1&&l.rows===r.rows,p=l.rows===1&&l.cols===r.cols;if(!h&&!p)return a.VALUE;const w=o??h;if(w?!h:!p)return a.VALUE;o=w;const d=numberOf(t[f+1],e,1);if(d instanceof u)return d;if(d!==1&&d!==-1)return a.VALUE;n.push({values:rowsOfCells(l).flat(),descending:d<0})}const s=rowsOfCells(r),i=(o===!1?transposed(s):s).map((f,l)=>({line:f,index:l})).sort((f,l)=>{for(const{values:h,descending:p}of n){const w=compareBy(h[f.index]??null,h[l.index]??null,p);if(w!==0)return w}return f.index-l.index}).map(f=>f.line);return matrixOf(o===!1?transposed(i):i)});export const keyOf=t=>t instanceof u?`e${t.code}`:typeof t=="string"?`s${t.toLowerCase()}`:`${typeof t}${String(t)}`;A("UNIQUE","r?rr",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=boolOf(t[1],e,!1);if(n instanceof u)return n;const o=boolOf(t[2],e,!1);if(o instanceof u)return o;const s=rowsOfCells(r),c=(r.reference?.rows??r.rows)-r.rows,i=w=>new Array(w).fill(null);if(c>0&&n&&(s.length+c)*r.cols<=N)for(let w=0;w<c;w++)s.push(i(r.cols));const f=n?transposed(s):s,l=new Map,h=(w,d)=>{const C=w.map(keyOf).join("\0"),E=l.get(C);E?E.count+=d:l.set(C,{line:w,count:d})};for(const w of f)h(w,1);c>0&&!n&&h(i(r.cols),c);const p=[...l.values()].filter(w=>!o||w.count===1).map(w=>w.line);return p.length===0?a.CALC:matrixOf(n?transposed(p):p)});function M(t,e){if(t instanceof u)return t;if(e instanceof u)return e;const r=Math.trunc(t),n=Math.trunc(e);return r<0||n<0?a.VALUE:r===0||n===0?a.CALC:r>I||n>B||r*n>N?a.VALUE:{rows:r,cols:n}}A("SEQUENCE","r?rrr",(t,e)=>{const r=M(numberOf(t[0],e,1),numberOf(t[1],e,1));if(r instanceof u)return r;const n=numberOf(t[2],e,1);if(n instanceof u)return n;const o=numberOf(t[3],e,1);if(o instanceof u)return o;const s=new Array(r.rows*r.cols);for(let c=0;c<s.length;c++)s[c]=n+c*o;return new L(r.rows,r.cols,s)}),A("RANDARRAY","?rrrrr",(t,e)=>{const r=M(numberOf(t[0],e,1),numberOf(t[1],e,1));if(r instanceof u)return r;const n=numberOf(t[2],e,0);if(n instanceof u)return n;const o=numberOf(t[3],e,1);if(o instanceof u)return o;const s=boolOf(t[4],e,!1);if(s instanceof u)return s;if(n>o)return a.VALUE;if(s&&(!Number.isInteger(n)||!Number.isInteger(o)))return a.VALUE;const c=new Array(r.rows*r.cols);for(let i=0;i<c.length;i++){const f=e.calc.random();c[i]=s?n+Math.floor(f*(o-n+1)):n+f*(o-n)}return new L(r.rows,r.cols,c)},{volatile:!0}),A("TEXTSPLIT","vr?rvvv",(t,e)=>{const r=y(t[0],e);if(r instanceof u)return r;const n=K(r??"");if(n instanceof u)return n;const o=S(t[1],e);if(o instanceof u)return o;const s=S(t[2],e);if(s instanceof u)return s;if(o.length===0&&s.length===0)return a.VALUE;if(o.includes("")||s.includes(""))return a.VALUE;const c=boolOf(t[3],e,!1);if(c instanceof u)return c;const i=numberOf(t[4],e,0);if(i instanceof u)return i;const f=y(t[5],e)??a.NA,l=i===1,p=(s.length===0?[n]:R(n,s,l)).filter(d=>!(c&&d==="")).map(d=>{const C=o.length===0?[d]:R(d,o,l);return c?C.filter(E=>E!==""):C}),w=Math.max(0,...p.map(d=>d.length));return p.length===0||w===0?a.CALC:matrixOf(p.map(d=>Array.from({length:w},(C,E)=>d[E]??f)))});export const oneOf=(t,e)=>{const r=e.calc.flat(t);return r instanceof L?r.at(0,0):r};A("MAP","r(r)",(t,e)=>{const r=t[t.length-1];if(!(r instanceof O))return a.VALUE;const n=[];for(const f of t.slice(0,-1)){const l=m(f,e);if(l instanceof u)return l;n.push(l)}const o=n[0];if(!o)return a.VALUE;const{rows:s,cols:c}=o;if(n.some(f=>f.rows!==s||f.cols!==c))return a.VALUE;const i=new Array(s*c);for(let f=0;f<s;f++)for(let l=0;l<c;l++)i[f*c+l]=oneOf(e.calc.apply(r,n.map(h=>h.at(f,l))),e);return new L(s,c,i)});function T(t,e){const r=t.length===3,n=t[t.length-1];if(!(n instanceof O))return a.VALUE;const o=m(t[r?1:0],e);return o instanceof u?o:{start:r?y(t[0],e)??null:o.at(0,0),cells:o,fn:n}}A("REDUCE","rr?r",(t,e)=>{const r=T(t,e);if(r instanceof u)return r;const{cells:n,fn:o}=r,s=t.length===3?0:1;let c=r.start;for(let i=s;i<n.rows*n.cols;i++)c=oneOf(e.calc.apply(o,[c,n.at(Math.floor(i/n.cols),i%n.cols)]),e);return c}),A("SCAN","rr?r",(t,e)=>{const r=T(t,e);if(r instanceof u)return r;const{cells:n,fn:o}=r,s=n.rows*n.cols,c=t.length===3?0:1,i=new Array(s);let f=r.start;c===1&&(i[0]=f);for(let l=c;l<s;l++)f=oneOf(e.calc.apply(o,[f,n.at(Math.floor(l/n.cols),l%n.cols)]),e),i[l]=f;return new L(n.rows,n.cols,i)});const b=t=>(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=e[1];if(!(o instanceof O))return a.VALUE;const s=rowsOfCells(n),i=(t?s:transposed(s)).map(f=>oneOf(r.calc.apply(o,[t?new L(1,f.length,f):new L(f.length,1,f)]),r));return t?new L(i.length,1,i):new L(1,i.length,i)};A("BYROW","rr",b(!0)),A("BYCOL","rr",b(!1)),A("MAKEARRAY","rrr",(t,e)=>{const r=M(numberOf(t[0],e,1),numberOf(t[1],e,1));if(r instanceof u)return r;const n=t[2];if(!(n instanceof O))return a.VALUE;const o=new Array(r.rows*r.cols);for(let s=0;s<r.rows;s++)for(let c=0;c<r.cols;c++)o[s*r.cols+c]=oneOf(e.calc.apply(n,[s+1,c+1]),e);return new L(r.rows,r.cols,o)});function _(t){return(e,r)=>{const n=[];for(const c of e){const i=m(c,r);if(i instanceof u)return i;const f=rowsOfCells(i);n.push(t?f:transposed(f))}const o=Math.max(0,...n.map(c=>c[0]?.length??0)),s=n.flatMap(c=>c.map(i=>Array.from({length:o},(f,l)=>l<i.length?i[l]??null:a.NA)));return s.length===0?a.CALC:matrixOf(t?s:transposed(s))}}A("VSTACK","(r)",_(!0)),A("HSTACK","(r)",_(!1));function g(t,e,r){const n=Math.min(t,Math.abs(e));return r?e>=0?[0,n]:[t-n,t]:e>=0?[n,t]:[0,t-n]}function k(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=p=>p===void 0||p instanceof V,s=numberOf(e[1],r,t?n.rows:0);if(s instanceof u)return s;const c=numberOf(e[2],r,t?n.cols:0);if(c instanceof u)return c;if(t&&!o(e[1])&&Math.trunc(s)===0)return a.CALC;if(t&&!o(e[2])&&Math.trunc(c)===0)return a.CALC;const[i,f]=g(n.rows,Math.trunc(s),t),[l,h]=g(n.cols,Math.trunc(c),t);return f<=i||h<=l?a.CALC:matrixOf(rowsOfCells(n).slice(i,f).map(p=>p.slice(l,h)))}}A("TAKE","rr?r",k(!0)),A("DROP","rr?r",k(!1));function z(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=numberOf(e[1],r,0);if(o instanceof u)return o;const s=boolOf(e[2],r,!1);if(s instanceof u)return s;if(![0,1,2,3].includes(o))return a.VALUE;const c=rowsOfCells(n),f=(s?transposed(c):c).flat().filter(l=>!((o===1||o===3)&&l===null||(o===2||o===3)&&l instanceof u));return f.length===0?a.CALC:t?new L(f.length,1,f):new L(1,f.length,f)}}A("TOCOL","r?rr",z(!0)),A("TOROW","r?rr",z(!1));function W(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;const o=rowsOfCells(n),s=t?o:transposed(o),c=[];for(const i of e.slice(1)){const f=indexes(i,r,1);if(f instanceof u)return f;for(const l of f){const h=s[l>0?l-1:s.length+l];if(l===0||!h)return a.VALUE;c.push(h)}}return matrixOf(t?c:transposed(c))}}A("CHOOSEROWS","r(r)",W(!0)),A("CHOOSECOLS","r(r)",W(!1)),A("EXPAND","rr?rr",(t,e)=>{const r=m(t[0],e);if(r instanceof u)return r;const n=M(numberOf(t[1],e,r.rows),numberOf(t[2],e,r.cols));if(n instanceof u)return n;if(n.rows<r.rows||n.cols<r.cols)return a.VALUE;const o=y(t[3],e)??a.NA,s=new Array(n.rows*n.cols);for(let c=0;c<n.rows;c++)for(let i=0;i<n.cols;i++)s[c*n.cols+i]=c<r.rows&&i<r.cols?r.at(c,i):o;return new L(n.rows,n.cols,s)});function P(t){return(e,r)=>{const n=m(e[0],r);if(n instanceof u)return n;if(n.rows>1&&n.cols>1)return a.VALUE;const o=numberOf(e[1],r,1);if(o instanceof u)return o;const s=Math.trunc(o);if(s<1)return a.NUM;const c=y(e[2],r)??a.NA,i=rowsOfCells(n).flat(),f=[];for(let l=0;l<i.length;l+=s)f.push(Array.from({length:s},(h,p)=>l+p<i.length?i[l+p]??null:c));return matrixOf(t?f:transposed(f))}}A("WRAPROWS","rr?r",P(!0)),A("WRAPCOLS","rr?r",P(!1));
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{cellName as N}from"../../address.js";import{CellError as s,ERR as n,Matrix as R}from"../../values.js";import{FormulaCell as E}from"../../workbook.js";import{toNumber as L,toText as d}from"../coerce.js";import{Lambda as I,Reference as l,Union as y}from"../types.js";import{lazy as m,raw as i,scalar as c}from"./define.js";const f=(e,r)=>{c(e,1,1,t=>r(t),{errors:!0})};f("ISBLANK",e=>e==null),f("ISNUMBER",e=>typeof e=="number"),f("ISTEXT",e=>typeof e=="string"),f("ISNONTEXT",e=>typeof e!="string"),f("ISLOGICAL",e=>typeof e=="boolean"),f("ISERROR",e=>e instanceof s),f("ISERR",e=>e instanceof s&&e!==n.NA),f("ISNA",e=>e===n.NA);const p=(e,r)=>{const t=L(e);return t instanceof s?t:Math.abs(Math.trunc(t))%2===r};c("ISEVEN",1,1,e=>p(e,0)),c("ISODD",1,1,e=>p(e,1)),c("N",1,1,e=>typeof e=="number"?e:typeof e=="boolean"?e?1:0:e instanceof s?e:0,{errors:!0}),i("NA","",()=>n.NA),i("TYPE","r",(e,r)=>{const t=r.calc.flat(e[0]);return t instanceof R?64:typeof t=="number"||t===null?1:typeof t=="string"?2:typeof t=="boolean"?4:16});const S=[n.NULL,n.DIV0,n.VALUE,n.REF,n.NAME,n.NUM,n.NA];c("ERROR.TYPE",1,1,e=>{const r=e instanceof s?S.indexOf(e):-1;return r<0?n.NA:r+1},{errors:!0}),m("ISREF",1,1,(e,r)=>{const t=e[0];if(!t)return!1;const o=r.calc.eval(t,r.frame);return o instanceof l||o instanceof y});const A=e=>e instanceof l?e.sheet.at(e.top,e.left):void 0;m("ISFORMULA",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=r.calc.eval(t,r.frame);return o instanceof I?n.VALUE:A(o)instanceof E}),m("FORMULATEXT",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=A(r.calc.eval(t,r.frame));return o instanceof E?o.text:n.NA}),i("CELL","v?r",(e,r)=>{const t=d(r.calc.scalar(e[0],r.frame));if(t instanceof s)return t;const o=e[1],a=o instanceof l?o:new l(r.frame.sheet,r.frame.row,r.frame.col,r.frame.row,r.frame.col),u=r.calc.read(a.sheet,a.top,a.left);switch(t.toLowerCase()){case"row":return a.top+1;case"col":return a.left+1;case"address":return N({row:a.top,col:a.left}).replace(/^([A-Z]+)(\d+)$/u,"$$$1$$$2");case"contents":return u;case"type":return u===null?"b":typeof u=="string"?"l":"v";default:return n.VALUE}}),i("TABLE","?rrrrr",()=>n.NA);
2
+ import{cellName as R}from"../../address.js";import{CellError as s,ERR as n,Matrix as L}from"../../values.js";import{FormulaCell as p}from"../../workbook.js";import{toNumber as d,toText as I}from"../coerce.js";import{Lambda as y,Reference as l,Union as S}from"../types.js";import{lazy as u,raw as i,scalar as c}from"./define.js";const f=(e,r)=>{c(e,1,1,t=>r(t),{errors:!0})};f("ISBLANK",e=>e==null),f("ISNUMBER",e=>typeof e=="number"),f("ISTEXT",e=>typeof e=="string"),f("ISNONTEXT",e=>typeof e!="string"),f("ISLOGICAL",e=>typeof e=="boolean"),f("ISERROR",e=>e instanceof s),f("ISERR",e=>e instanceof s&&e!==n.NA),f("ISNA",e=>e===n.NA);const A=(e,r)=>{const t=d(e);return t instanceof s?t:Math.abs(Math.trunc(t))%2===r};c("ISEVEN",1,1,e=>A(e,0)),c("ISODD",1,1,e=>A(e,1)),c("N",1,1,e=>typeof e=="number"?e:typeof e=="boolean"?e?1:0:e instanceof s?e:0,{errors:!0}),i("NA","",()=>n.NA),i("TYPE","r",(e,r)=>{const t=r.calc.flat(e[0]);return t instanceof L?64:typeof t=="number"||t===null?1:typeof t=="string"?2:typeof t=="boolean"?4:16});const b=[n.NULL,n.DIV0,n.VALUE,n.REF,n.NAME,n.NUM,n.NA];c("ERROR.TYPE",1,1,e=>{const r=e instanceof s?b.indexOf(e):-1;return r<0?n.NA:r+1},{errors:!0}),u("ISREF",1,1,(e,r)=>{const t=e[0];if(!t)return!1;const o=r.calc.eval(t,r.frame);return o instanceof l||o instanceof S});const N=e=>e instanceof l?e.sheet.at(e.top,e.left):void 0;u("ISFORMULA",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=r.calc.eval(t,r.frame);return o instanceof y?n.VALUE:N(o)instanceof p}),u("FORMULATEXT",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=N(r.calc.eval(t,r.frame));return o instanceof p?o.text:n.NA}),i("CELL","v?r",(e,r)=>{const t=I(r.calc.scalar(e[0],r.frame));if(t instanceof s)return t;const o=e[1],a=o instanceof l?o:new l(r.frame.sheet,r.frame.row,r.frame.col,r.frame.row,r.frame.col),m=()=>r.calc.read(a.sheet,a.top,a.left);switch(t.toLowerCase()){case"filename":return`[${r.calc.wb.title??"sheet"}.xlsx]${a.sheet.name}`;case"row":return a.top+1;case"col":return a.left+1;case"address":return R({row:a.top,col:a.left}).replace(/^([A-Z]+)(\d+)$/u,"$$$1$$$2");case"contents":return m();case"type":{const E=m();return E===null?"b":typeof E=="string"?"l":"v"}default:return n.VALUE}}),i("TABLE","?rrrrr",()=>n.NA);
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{CellError as s,ERR as i,Matrix as A}from"../../values.js";import{compare as N,toBool as p,toNumber as U}from"../coerce.js";import{Lambda as R,Missing as b}from"../types.js";import{eachArg as O,lazy as m,many as d,raw as E,scalar as k}from"./define.js";const l=(t,n)=>t?n.calc.eval(t,n.frame):!1;function V(t,n,e){if(!(t instanceof A))return t;const r=t.rows===1?0:n,o=t.cols===1?0:e;return r<t.rows&&o<t.cols?t.at(r,o):i.NA}function L(t,n,e){const r=[t,...n].filter(c=>c instanceof A),o=Math.max(...r.map(c=>c.rows)),f=Math.max(...r.map(c=>c.cols)),a=new Array(o*f);for(let c=0;c<o;c++)for(let u=0;u<f;u++){const h=e(V(t,c,u));a[c*f+u]=h instanceof s?h:V(n[h]??i.VALUE,c,u)}return new A(o,f,a)}m("IF",1,3,(t,n)=>{const e=n.calc.flat(l(t[0],n)),r=f=>{const a=t[f];if(a===void 0)return f===1;const c=n.calc.eval(a,n.frame);return c instanceof b?0:c};if(e instanceof A){const f=a=>a<t.length?n.calc.flat(r(a)):!1;return L(e,[f(1),f(2)],a=>{const c=p(a);return c instanceof s?c:c?0:1})}const o=p(e);return o instanceof s?o:r(o?1:2)}),m("IFS",2,d,(t,n)=>{for(let e=0;e+1<t.length;e+=2){const r=p(n.calc.scalar(l(t[e],n),n.frame));if(r instanceof s)return r;if(r)return l(t[e+1],n)}return i.NA});const M=t=>(n,e)=>{const r=e.calc.flat(l(n[0],e)),o=a=>a instanceof s&&(t===void 0||a===t),f=()=>n[1]?e.calc.flat(e.calc.eval(n[1],e.frame)):"";return r instanceof A?L(r,[r,f()],a=>o(a)?1:0):o(r)?f():r};m("IFERROR",2,2,M()),m("IFNA",2,2,M(i.NA)),m("CHOOSE",2,255,(t,n)=>{const e=n.calc.flat(l(t[0],n));if(e instanceof A){const a=t.slice(1).map(c=>n.calc.flat(n.calc.eval(c,n.frame)));return L(e,a,c=>{const u=U(c);if(u instanceof s)return u;const h=Math.trunc(u)-1;return h<0||h>=a.length?i.VALUE:h})}const r=U(e);if(r instanceof s)return r;const o=Math.trunc(r),f=t[o];return o<1||f===void 0?i.VALUE:n.calc.eval(f,n.frame)}),m("SWITCH",3,d,(t,n)=>{const e=n.calc.scalar(l(t[0],n),n.frame);if(e instanceof s)return e;let r=1;for(;r+1<t.length;r+=2){const o=n.calc.scalar(l(t[r],n),n.frame);if(o instanceof s)return o;if(N(e,o)===0)return l(t[r+1],n)}return r<t.length?l(t[r],n):i.NA});function w(t,n){const e=[];let r;return O(t,n,(o,f)=>{if(r)return;if(o instanceof s){r=o;return}if(typeof o=="string"&&!f||o===null)return;const a=p(o);a instanceof s?r=a:e.push(a)}),r||(e.length===0?i.VALUE:e)}E("AND","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.every(Boolean)}),E("OR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.some(Boolean)}),E("XOR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.filter(Boolean).length%2===1}),k("NOT",1,1,t=>{const n=p(t);return n instanceof s?n:!n}),E("TRUE","",()=>!0),E("FALSE","",()=>!1);const y=(t,n)=>n?{names:t,parent:n}:{names:t};m("LET",3,d,(t,n)=>{if(t.length%2===0)return i.VALUE;const e=new Map,r={...n.frame,scope:y(e,n.frame.scope)};for(let o=0;o+1<t.length;o+=2){const f=t[o];if(f?.kind!=="name")return i.VALUE;e.set(f.name.toLowerCase(),l(t[o+1],{...n,frame:r}))}return l(t[t.length-1],{...n,frame:r})}),m("LAMBDA",1,254,(t,n)=>{const e=[];for(const o of t.slice(0,-1)){if(o.kind!=="name")return i.VALUE;e.push(o.name.toLowerCase())}const r=t[t.length-1];return r?new R(e,r,n.frame):i.VALUE});
2
+ import{CellError as s,ERR as i,Matrix as A}from"../../values.js";import{compare as N,toBool as p,toNumber as U}from"../coerce.js";import{Lambda as R,Missing as b}from"../types.js";import{eachArg as O,lazy as m,many as d,raw as E,scalar as k}from"./define.js";const l=(t,n)=>t?n.calc.eval(t,n.frame):!1;function V(t,n,e){if(!(t instanceof A))return t;const r=t.rows===1?0:n,o=t.cols===1?0:e;return r<t.rows&&o<t.cols?t.at(r,o):i.NA}function L(t,n,e){const r=[t,...n].filter(c=>c instanceof A),o=Math.max(...r.map(c=>c.rows)),f=Math.max(...r.map(c=>c.cols)),a=new Array(o*f);for(let c=0;c<o;c++)for(let u=0;u<f;u++){const h=e(V(t,c,u));a[c*f+u]=h instanceof s?h:V(n[h]??i.VALUE,c,u)}return new A(o,f,a)}m("IF",1,3,(t,n)=>{const e=n.calc.flat(l(t[0],n)),r=f=>{const a=t[f];if(a===void 0)return f===1;const c=n.calc.eval(a,n.frame);return c instanceof b?null:c};if(e instanceof A){const f=a=>a<t.length?n.calc.flat(r(a))??0:!1;return L(e,[f(1),f(2)],a=>{const c=p(a);return c instanceof s?c:c?0:1})}const o=p(e);return o instanceof s?o:r(o?1:2)}),m("IFS",2,d,(t,n)=>{for(let e=0;e+1<t.length;e+=2){const r=p(n.calc.scalar(l(t[e],n),n.frame));if(r instanceof s)return r;if(r)return l(t[e+1],n)}return i.NA});const M=t=>(n,e)=>{const r=e.calc.flat(l(n[0],e)),o=a=>a instanceof s&&(t===void 0||a===t),f=()=>n[1]?e.calc.flat(e.calc.eval(n[1],e.frame)):"";return r instanceof A?L(r,[r,f()],a=>o(a)?1:0):o(r)?f():r};m("IFERROR",2,2,M()),m("IFNA",2,2,M(i.NA)),m("CHOOSE",2,255,(t,n)=>{const e=n.calc.flat(l(t[0],n));if(e instanceof A){const a=t.slice(1).map(c=>n.calc.flat(n.calc.eval(c,n.frame)));return L(e,a,c=>{const u=U(c);if(u instanceof s)return u;const h=Math.trunc(u)-1;return h<0||h>=a.length?i.VALUE:h})}const r=U(e);if(r instanceof s)return r;const o=Math.trunc(r),f=t[o];return o<1||f===void 0?i.VALUE:n.calc.eval(f,n.frame)}),m("SWITCH",3,d,(t,n)=>{const e=n.calc.scalar(l(t[0],n),n.frame);if(e instanceof s)return e;let r=1;for(;r+1<t.length;r+=2){const o=n.calc.scalar(l(t[r],n),n.frame);if(o instanceof s)return o;if(N(e,o)===0)return l(t[r+1],n)}return r<t.length?l(t[r],n):i.NA});function w(t,n){const e=[];let r;return O(t,n,(o,f)=>{if(r)return;if(o instanceof s){r=o;return}if(typeof o=="string"&&!f||o===null)return;const a=p(o);a instanceof s?r=a:e.push(a)}),r||(e.length===0?i.VALUE:e)}E("AND","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.every(Boolean)}),E("OR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.some(Boolean)}),E("XOR","(r)",(t,n)=>{const e=w(t,n);return e instanceof s?e:e.filter(Boolean).length%2===1}),k("NOT",1,1,t=>{const n=p(t);return n instanceof s?n:!n}),E("TRUE","",()=>!0),E("FALSE","",()=>!1);const y=(t,n)=>n?{names:t,parent:n}:{names:t};m("LET",3,d,(t,n)=>{if(t.length%2===0)return i.VALUE;const e=new Map,r={...n.frame,scope:y(e,n.frame.scope)};for(let o=0;o+1<t.length;o+=2){const f=t[o];if(f?.kind!=="name")return i.VALUE;e.set(f.name.toLowerCase(),l(t[o+1],{...n,frame:r}))}return l(t[t.length-1],{...n,frame:r})}),m("LAMBDA",1,254,(t,n)=>{const e=[];for(const o of t.slice(0,-1)){if(o.kind!=="name")return i.VALUE;e.push(o.name.toLowerCase())}const r=t[t.length-1];return r?new R(e,r,n.frame):i.VALUE});
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{CellError as a,ERR as p,Matrix as k}from"../../values.js";import{cellName as K,columnLetter as g,MAX_COLUMNS as W,MAX_ROWS as L}from"../../address.js";import{sheetPrefix as q}from"../../formula/print.js";import{parseFormula as G}from"../../formula/parse.js";import{compare as v,toBool as U,toNumber as M,toText as H}from"../coerce.js";import{Missing as O,Reference as x,Union as F}from"../types.js";import{cellsOf as R,lazy as J,raw as A,scalar as Q}from"./define.js";import{indexKeyOf as Y}from"../indexes.js";import{hasWildcard as Z,indexKey as C,literalOf as nn,wildcard as tn}from"./criteria.js";const I=(n,t)=>typeof n==typeof t&&!(n instanceof a)&&n!==null;function T(n){if(n.rows===1)return{length:n.cols,at:t=>n.at(0,t)};if(n.cols===1)return{length:n.rows,at:t=>n.at(t,0)}}const P=new WeakMap;function en(n,t){const e=P.get(n);if(e)return e;if(e===void 0){P.set(n,null);return}const o=t();return P.set(n,o),o}function V(n,t,e,o,r,s=0,c=1){const f=typeof n=="string"?n:void 0,i=r&&f!==void 0&&Z(f)?nn(f):f;if(f!==void 0&&i===void 0){const l=tn(f);for(let d=s;d>=0&&d<t.length;d+=c){const h=t.at(d);if(typeof h=="string"&&l(h))return d}return-1}const u=C(i??n);if(u===void 0)return-1;if(t.length>=64&&s===0&&c===1){const l=()=>{const w=new Map;for(let m=t.length-1;m>=0;m--){const E=C(t.at(m));E!==void 0&&w.set(E,[m])}return w},d=e.reference,h=d?o.calc.indexes.ofRepeated(Y("find",d),[d],l):e.array&&en(e.array,l);if(h)return h.get(u)?.[0]??-1}for(let l=s;l>=0&&l<t.length;l+=c)if(C(t.at(l))===u)return l;return-1}function N(n,t,e=!1){let o=0,r=t.length-1,s=-1;for(;o<=r;){const c=o+r>>1;let f=c;for(;f>=o&&!I(t.at(f),n);)f-=1;if(f<o){o=c+1;continue}const i=v(t.at(f),n);(e?i>=0:i<=0)?(s=f,o=c+1):r=f-1}return s}const y=(n,t)=>t.calc.scalar(n??null,t.frame),rn=(n,t,e)=>n===void 0?e:n instanceof O?!1:U(t.calc.scalar(n,t.frame));function X(n){return(t,e)=>{const o=y(t[0],e);if(o instanceof a)return o;const r=R(t[1],e);if(r instanceof a)return r;const s=M(e.calc.scalar(t[2],e.frame));if(s instanceof a)return s;const c=Math.trunc(s);if(c<1)return p.VALUE;if(c>(n?r.cols:r.rows))return p.REF;const f=rn(t[3],e,!0);if(f instanceof a)return f;const i={length:n?r.rows:r.cols,at:d=>n?r.at(d,0):r.at(0,d)},u={rows:n?r.rows:1,cols:n?1:r.cols,at:(d,h)=>r.at(d,h),...r.reference?{reference:new x(r.reference.sheet,r.reference.top,r.reference.left,n?r.reference.bottom:r.reference.top,n?r.reference.left:r.reference.right)}:{}},l=f?N(o,i):V(o,i,u,e,!0);return l<0?p.NA:n?r.at(l,c-1):r.at(c-1,l)}}A("VLOOKUP","vrv?v",X(!0)),A("HLOOKUP","vrv?v",X(!1)),A("MATCH","vr?v",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=T(o);if(!r)return p.NA;const s=n[2]===void 0?1:n[2]instanceof O?0:M(t.calc.scalar(n[2],t.frame));if(s instanceof a)return s;const c=s===0?V(e,r,o,t,!0):N(e,r,s<0);return c<0?p.NA:c+1}),A("LOOKUP","vr?r",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=o.rows>=o.cols,s={length:r?o.rows:o.cols,at:u=>r?o.at(u,0):o.at(0,u)},c=N(e,s);if(c<0)return p.NA;if(n[2]===void 0)return r?o.at(c,o.cols-1):o.at(o.rows-1,c);const f=R(n[2],t);if(f instanceof a)return f;const i=T(f);return i?i.at(c):p.NA});function D(n,t,e,o,r){const s=T(t);if(!s)return-1;const c=r===-1;if(Math.abs(r)===2){const l=N(n,s,r<0);if(l>=0&&v(s.at(l),n)===0)return l;if(o===0)return-1}const i=V(n,s,t,e,o===2,c?s.length-1:0,c?-1:1);if(i>=0||o===0||o===2)return i;let u=-1;for(let l=0;l<s.length;l++){const d=s.at(l);if(!I(d,n))continue;const h=v(d,n),w=u<0||(o<0?v(d,s.at(u))>0:v(d,s.at(u))<0);(o<0?h<0:h>0)&&w&&(u=l)}return u}const _=(n,t,e)=>{const o=(c,f)=>c===void 0||c instanceof O?f:M(e.calc.scalar(c,e.frame)),r=o(n,0);if(r instanceof a)return r;const s=o(t,1);return s instanceof a?s:[r,s]};A("XLOOKUP","vrr?rvv",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=R(n[2],t);if(r instanceof a)return r;const s=_(n[4],n[5],t);if(s instanceof a)return s;const c=D(e,o,t,s[0],s[1]);if(c<0){const i=n[3];return i===void 0||i instanceof O?p.NA:t.calc.flat(i)}if(o.cols===1){if(r.cols===1)return r.at(c,0);const i=[];for(let u=0;u<r.cols;u++)i.push(r.at(c,u));return new k(1,r.cols,i)}if(r.rows===1)return r.at(0,c);const f=[];for(let i=0;i<r.rows;i++)f.push(r.at(i,c));return new k(r.rows,1,f)}),A("XMATCH","vr?vv",(n,t)=>{const e=y(n[0],t);if(e instanceof a)return e;const o=R(n[1],t);if(o instanceof a)return o;const r=_(n[2],n[3],t);if(r instanceof a)return r;const s=D(e,o,t,r[0],r[1]);return s<0?p.NA:s+1}),A("INDEX","rv?vv",(n,t)=>{let e=n[0];const o=w=>{if(w===void 0||w instanceof O)return 0;const m=M(t.calc.scalar(w,t.frame));return m instanceof a?m:Math.trunc(m)};if(e instanceof F){const w=o(n[3]??1);if(w instanceof a)return w;const m=e.parts[w-1];if(!m)return p.REF;e=m}if(e instanceof a)return e;const r=o(n[1]);if(r instanceof a)return r;const s=o(n[2]);if(s instanceof a)return s;if(r<0||s<0)return p.VALUE;if(e instanceof x){const w=n[2]===void 0&&e.rows===1,m=w?1:r,E=w?r:s;return m>e.rows||E>e.cols?p.REF:new x(e.sheet,m===0?e.top:e.top+m-1,E===0?e.left:e.left+E-1,m===0?e.bottom:e.top+m-1,E===0?e.right:e.left+E-1)}const c=R(e,t);if(c instanceof a)return c;const f=n[2]===void 0&&c.rows===1,i=f?1:r,u=f?r:s===0&&c.cols===1?1:s;if(i>c.rows||u>c.cols)return p.REF;if(i>0&&u>0)return c.at(i-1,u-1);const l=i===0?c.rows:1,d=u===0?c.cols:1,h=[];for(let w=0;w<l;w++)for(let m=0;m<d;m++)h.push(c.at(i===0?w:i-1,u===0?m:u-1));return new k(l,d,h)}),A("OFFSET","rvv?vv",(n,t)=>{const e=n[0];if(e instanceof a)return e;if(!(e instanceof x))return p.VALUE;const o=(b,B)=>{if(b===void 0||b instanceof O)return B;const S=M(t.calc.scalar(b,t.frame));return S instanceof a?S:Math.trunc(S)},r=o(n[1],0),s=o(n[2],0),c=o(n[3],e.rows),f=o(n[4],e.cols);for(const b of[r,s,c,f])if(b instanceof a)return b;const[i,u,l,d]=[r,s,c,f];if(l===0||d===0)return p.REF;const h=e.top+(i??0),w=e.left+(u??0),m=h+(l??1)-1,E=w+(d??1)-1;return Math.min(h,m)<0||Math.min(w,E)<0||Math.max(h,m)>=L||Math.max(w,E)>=W?p.REF:new x(e.sheet,Math.min(h,m),Math.min(w,E),Math.max(h,m),Math.max(w,E))},{volatile:!0});const on=/^(?:(.*)!)?R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/iu,$=(n,t)=>n===void 0?t:n.startsWith("[")?t+Number(n.slice(1,-1)):Number(n)-1;function sn(n,t,e){const o=on.exec(n.trim());if(!o)return;const[,r,s,c,f,i]=o,u={row:$(s,t),col:$(c,e)};if(u.row<0||u.col<0)return;const l=K(u),d=r===void 0?"":`${r}!`;if(f===void 0&&i===void 0)return`${d}${l}`;const h={row:$(f,t),col:$(i,e)};return h.row<0||h.col<0?void 0:`${d}${l}:${K(h)}`}A("INDIRECT","v?v",(n,t)=>{const e=H(t.calc.scalar(n[0],t.frame));if(e instanceof a)return e;const o=n[1]===void 0?!0:U(t.calc.scalar(n[1],t.frame));if(o instanceof a)return o;const r=o?e:sn(e,t.frame.row,t.frame.col);if(r===void 0)return p.REF;const s=G(r);if(!s.ok)return p.REF;const{node:c}=s;if(c.kind!=="ref"&&c.kind!=="structured"&&c.kind!=="name"&&!(c.kind==="binary"&&c.op===":"))return p.REF;const f=t.calc.eval(c,{...t.frame,dr:0,dc:0});return f instanceof x||f instanceof F?f:p.REF},{volatile:!0});const j=(n,t,e)=>{if(n===void 0||n instanceof O)return t+1;if(n instanceof a)return n;if(!(n instanceof x))return p.VALUE;const[o,r]=e(n);if(r<=1)return o+1;const s=Array.from({length:r},(c,f)=>o+f+1);return e===z?new k(r,1,s):new k(1,r,s)},z=n=>[n.top,n.bottom===L-1?Math.min(n.rows,n.sheet.lastRow-n.top+1):n.rows],cn=n=>[n.left,n.cols];A("ROW","?r",(n,t)=>j(n[0],t.frame.row,z)),A("COLUMN","?r",(n,t)=>j(n[0],t.frame.col,cn)),A("ROWS","r",(n,t)=>{const e=n[0];if(e instanceof x)return e.rows;const o=R(e,t);return o instanceof a?o:o.rows}),A("COLUMNS","r",(n,t)=>{const e=n[0];if(e instanceof x)return e.cols;const o=R(e,t);return o instanceof a?o:o.cols}),A("AREAS","r",n=>{const t=n[0];return t instanceof x?1:t instanceof F?t.parts.length:p.VALUE}),Q("ADDRESS",2,5,(n,t,e,o,r)=>{const s=M(n),c=M(t),f=e==null?1:M(e);for(const b of[s,c,f])if(b instanceof a)return b;const[i,u,l]=[s,c,f];if((i??0)<1||(u??0)<1||(i??0)>L||(u??0)>W)return p.VALUE;const d=o==null?!0:U(o);if(d instanceof a)return d;const h=l===1||l===2,w=l===1||l===3,m=d?`${w?"$":""}${g((u??1)-1)}${h?"$":""}${String(i)}`:`R${h?String(i):`[${String(i)}]`}C${w?String(u):`[${String(u)}]`}`;if(r==null)return m;const E=H(r);return E instanceof a?E:`${q(E)}${m}`}),A("TRANSPOSE","r",(n,t)=>{const e=R(n[0],t);if(e instanceof a)return e;const o=[];for(let r=0;r<e.cols;r++)for(let s=0;s<e.rows;s++)o.push(e.at(s,r));return new k(e.cols,e.rows,o)}),J("SHEET",0,1,(n,t)=>{const e=n[0];if(e===void 0)return t.frame.sheet.index+1;const o=t.calc.eval(e,t.frame);if(o instanceof x)return o.sheet.index+1;const r=t.calc.scalar(o,t.frame);if(typeof r!="string")return p.NA;const s=t.calc.wb.sheet(r);return s?s.index+1:p.NA}),A("SHEETS","?r",(n,t)=>t.calc.wb.sheets.length);
2
+ import{CellError as u,ERR as p,Matrix as M}from"../../values.js";import{cellName as K,columnLetter as q,MAX_COLUMNS as W,MAX_ROWS as L}from"../../address.js";import{sheetPrefix as G}from"../../formula/print.js";import{parseFormula as J}from"../../formula/parse.js";import{compare as y,toBool as U,toNumber as O,toText as H}from"../coerce.js";import{Missing as k,Reference as x,Union as F}from"../types.js";import{cellsOf as R,lazy as Q,raw as A,scalar as Y}from"./define.js";import{indexKeyOf as Z}from"../indexes.js";import{hasWildcard as g,indexKey as C,literalOf as nn,wildcard as tn}from"./criteria.js";const I=(n,t)=>typeof n==typeof t&&!(n instanceof u)&&n!==null;function T(n){if(n.rows===1)return{length:n.cols,at:t=>n.at(0,t)};if(n.cols===1)return{length:n.rows,at:t=>n.at(t,0)}}const V=new WeakMap;function en(n,t){const e=V.get(n);if(e)return e;if(e===void 0){V.set(n,null);return}const o=t();return V.set(n,o),o}function P(n,t,e,o,r,s=0,c=1){const f=typeof n=="string"?n:void 0,i=r&&f!==void 0&&g(f)?nn(f):f;if(f!==void 0&&i===void 0){const l=tn(f);for(let w=s;w>=0&&w<t.length;w+=c){const h=t.at(w);if(typeof h=="string"&&l(h))return w}return-1}const a=C(i??n);if(a===void 0)return-1;if(t.length>=64&&s===0&&c===1){const l=()=>{const d=new Map;for(let m=t.length-1;m>=0;m--){const E=C(t.at(m));E!==void 0&&d.set(E,[m])}return d},w=e.reference,h=w?o.calc.indexes.ofRepeated(Z("find",w),[w],l):e.array&&en(e.array,l);if(h)return h.get(a)?.[0]??-1}for(let l=s;l>=0&&l<t.length;l+=c)if(C(t.at(l))===a)return l;return-1}function $(n,t,e=!1){let o=0,r=t.length-1,s=-1;for(;o<=r;){const c=o+r>>1;let f=c;for(;f>=o&&!I(t.at(f),n);)f-=1;if(f<o){o=c+1;continue}const i=y(t.at(f),n);(e?i>=0:i<=0)?(s=f,o=c+1):r=f-1}return s}const N=(n,t)=>t.calc.scalar(n??null,t.frame),rn=(n,t,e)=>n===void 0?e:n instanceof k?!1:U(t.calc.scalar(n,t.frame));function X(n){return(t,e)=>{const o=N(t[0],e);if(o instanceof u)return o;const r=R(t[1],e);if(r instanceof u)return r;const s=O(e.calc.scalar(t[2],e.frame));if(s instanceof u)return s;const c=Math.trunc(s);if(c<1)return p.VALUE;const f=r.reference,i=f?f.bottom-f.top+1:r.rows;if(c>(n?r.cols:i))return p.REF;const a=rn(t[3],e,!0);if(a instanceof u)return a;const l={length:n?r.rows:r.cols,at:d=>n?r.at(d,0):r.at(0,d)},w={rows:n?r.rows:1,cols:n?1:r.cols,at:(d,m)=>r.at(d,m),...r.reference?{reference:new x(r.reference.sheet,r.reference.top,r.reference.left,n?r.reference.bottom:r.reference.top,n?r.reference.left:r.reference.right)}:{}},h=a?$(o,l):P(o,l,w,e,!0);return h<0?p.NA:n?r.at(h,c-1):r.at(c-1,h)}}A("VLOOKUP","vrv?v",X(!0)),A("HLOOKUP","vrv?v",X(!1)),A("MATCH","vr?v",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=T(o);if(!r)return p.NA;const s=n[2]===void 0?1:n[2]instanceof k?0:O(t.calc.scalar(n[2],t.frame));if(s instanceof u)return s;const c=s===0?P(e,r,o,t,!0):$(e,r,s<0);return c<0?p.NA:c+1}),A("LOOKUP","vr?r",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=o.rows>=o.cols,s={length:r?o.rows:o.cols,at:a=>r?o.at(a,0):o.at(0,a)},c=$(e,s);if(c<0)return p.NA;if(n[2]===void 0)return r?o.at(c,o.cols-1):o.at(o.rows-1,c);const f=R(n[2],t);if(f instanceof u)return f;const i=T(f);return i?i.at(c):p.NA});function D(n,t,e,o,r){const s=T(t);if(!s)return-1;const c=r===-1;if(Math.abs(r)===2){const l=$(n,s,r<0);if(l>=0&&y(s.at(l),n)===0)return l;if(o===0)return-1}const i=P(n,s,t,e,o===2,c?s.length-1:0,c?-1:1);if(i>=0||o===0||o===2)return i;let a=-1;for(let l=0;l<s.length;l++){const w=s.at(l);if(!I(w,n))continue;const h=y(w,n),d=a<0||(o<0?y(w,s.at(a))>0:y(w,s.at(a))<0);(o<0?h<0:h>0)&&d&&(a=l)}return a}const _=(n,t,e)=>{const o=(c,f)=>c===void 0||c instanceof k?f:O(e.calc.scalar(c,e.frame)),r=o(n,0);if(r instanceof u)return r;const s=o(t,1);return s instanceof u?s:[r,s]};A("XLOOKUP","vrr?rvv",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=R(n[2],t);if(r instanceof u)return r;const s=_(n[4],n[5],t);if(s instanceof u)return s;const c=D(e,o,t,s[0],s[1]);if(c<0){const i=n[3];return i===void 0||i instanceof k?p.NA:t.calc.flat(i)}if(o.cols===1){if(r.cols===1)return r.at(c,0);const i=[];for(let a=0;a<r.cols;a++)i.push(r.at(c,a));return new M(1,r.cols,i)}if(r.rows===1)return r.at(0,c);const f=[];for(let i=0;i<r.rows;i++)f.push(r.at(i,c));return new M(r.rows,1,f)}),A("XMATCH","vr?vv",(n,t)=>{const e=N(n[0],t);if(e instanceof u)return e;const o=R(n[1],t);if(o instanceof u)return o;const r=_(n[2],n[3],t);if(r instanceof u)return r;const s=D(e,o,t,r[0],r[1]);return s<0?p.NA:s+1}),A("INDEX","rv?vv",(n,t)=>{let e=n[0];const o=d=>{if(d===void 0||d instanceof k)return 0;const m=O(t.calc.scalar(d,t.frame));return m instanceof u?m:Math.trunc(m)};if(e instanceof F){const d=o(n[3]??1);if(d instanceof u)return d;const m=e.parts[d-1];if(!m)return p.REF;e=m}if(e instanceof u)return e;const r=o(n[1]);if(r instanceof u)return r;const s=o(n[2]);if(s instanceof u)return s;if(r<0||s<0)return p.VALUE;if(e instanceof x){const d=n[2]===void 0&&e.rows===1,m=d?1:r,E=d?r:s;return m>e.rows||E>e.cols?p.REF:new x(e.sheet,m===0?e.top:e.top+m-1,E===0?e.left:e.left+E-1,m===0?e.bottom:e.top+m-1,E===0?e.right:e.left+E-1)}const c=R(e,t);if(c instanceof u)return c;const f=n[2]===void 0&&c.rows===1,i=f?1:r,a=f?r:s===0&&c.cols===1?1:s;if(i>c.rows||a>c.cols)return p.REF;if(i>0&&a>0)return c.at(i-1,a-1);const l=i===0?c.rows:1,w=a===0?c.cols:1,h=[];for(let d=0;d<l;d++)for(let m=0;m<w;m++)h.push(c.at(i===0?d:i-1,a===0?m:a-1));return new M(l,w,h)}),A("OFFSET","rvv?vv",(n,t)=>{const e=n[0];if(e instanceof u)return e;if(!(e instanceof x))return p.VALUE;const o=(b,B)=>{if(b===void 0||b instanceof k)return B;const S=O(t.calc.scalar(b,t.frame));return S instanceof u?S:Math.trunc(S)},r=o(n[1],0),s=o(n[2],0),c=o(n[3],e.rows),f=o(n[4],e.cols);for(const b of[r,s,c,f])if(b instanceof u)return b;const[i,a,l,w]=[r,s,c,f];if(l===0||w===0)return p.REF;const h=e.top+(i??0),d=e.left+(a??0),m=h+(l??1)-1,E=d+(w??1)-1;return Math.min(h,m)<0||Math.min(d,E)<0||Math.max(h,m)>=L||Math.max(d,E)>=W?p.REF:new x(e.sheet,Math.min(h,m),Math.min(d,E),Math.max(h,m),Math.max(d,E))},{volatile:!0});const on=/^(?:(.*)!)?R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?(?::R(\[-?\d+\]|\d+)?C(\[-?\d+\]|\d+)?)?$/iu,v=(n,t)=>n===void 0?t:n.startsWith("[")?t+Number(n.slice(1,-1)):Number(n)-1;function sn(n,t,e){const o=on.exec(n.trim());if(!o)return;const[,r,s,c,f,i]=o,a={row:v(s,t),col:v(c,e)};if(a.row<0||a.col<0)return;const l=K(a),w=r===void 0?"":`${r}!`;if(f===void 0&&i===void 0)return`${w}${l}`;const h={row:v(f,t),col:v(i,e)};return h.row<0||h.col<0?void 0:`${w}${l}:${K(h)}`}A("INDIRECT","v?v",(n,t)=>{const e=H(t.calc.scalar(n[0],t.frame));if(e instanceof u)return e;const o=n[1]===void 0?!0:U(t.calc.scalar(n[1],t.frame));if(o instanceof u)return o;const r=o?e:sn(e,t.frame.row,t.frame.col);if(r===void 0)return p.REF;const s=J(r);if(!s.ok)return p.REF;const{node:c}=s;if(c.kind!=="ref"&&c.kind!=="structured"&&c.kind!=="name"&&!(c.kind==="binary"&&c.op===":"))return p.REF;const f=t.calc.eval(c,{...t.frame,dr:0,dc:0});return f instanceof x||f instanceof F?f:p.REF},{volatile:!0});const j=(n,t,e)=>{if(n===void 0||n instanceof k)return t+1;if(n instanceof u)return n;if(n instanceof M)return new M(n.rows,n.cols,n.data.map(c=>c instanceof u?c:p.VALUE));if(!(n instanceof x))return p.VALUE;const[o,r]=e(n);if(r<=1)return o+1;const s=Array.from({length:r},(c,f)=>o+f+1);return e===z?new M(r,1,s):new M(1,r,s)},z=n=>[n.top,n.bottom===L-1?Math.min(n.rows,n.sheet.lastRow-n.top+1):n.rows],cn=n=>[n.left,n.cols];A("ROW","?r",(n,t)=>j(n[0],t.frame.row,z)),A("COLUMN","?r",(n,t)=>j(n[0],t.frame.col,cn)),A("ROWS","r",(n,t)=>{const e=n[0];if(e instanceof x)return e.rows;const o=R(e,t);return o instanceof u?o:o.rows}),A("COLUMNS","r",(n,t)=>{const e=n[0];if(e instanceof x)return e.cols;const o=R(e,t);return o instanceof u?o:o.cols}),A("AREAS","r",n=>{const t=n[0];return t instanceof x?1:t instanceof F?t.parts.length:p.VALUE}),Y("ADDRESS",2,5,(n,t,e,o,r)=>{const s=O(n),c=O(t),f=e==null?1:O(e);for(const b of[s,c,f])if(b instanceof u)return b;const[i,a,l]=[s,c,f];if((i??0)<1||(a??0)<1||(i??0)>L||(a??0)>W)return p.VALUE;const w=o==null?!0:U(o);if(w instanceof u)return w;const h=l===1||l===2,d=l===1||l===3,m=w?`${d?"$":""}${q((a??1)-1)}${h?"$":""}${String(i)}`:`R${h?String(i):`[${String(i)}]`}C${d?String(a):`[${String(a)}]`}`;if(r==null)return m;const E=H(r);return E instanceof u?E:`${G(E)}${m}`}),A("TRANSPOSE","r",(n,t)=>{const e=R(n[0],t);if(e instanceof u)return e;const o=[];for(let r=0;r<e.cols;r++)for(let s=0;s<e.rows;s++)o.push(e.at(s,r));return new M(e.cols,e.rows,o)}),Q("SHEET",0,1,(n,t)=>{const e=n[0];if(e===void 0)return t.frame.sheet.index+1;const o=t.calc.eval(e,t.frame);if(o instanceof x)return o.sheet.index+1;const r=t.calc.scalar(o,t.frame);if(typeof r!="string")return p.NA;const s=t.calc.wb.sheet(r);return s?s.index+1:p.NA}),A("SHEETS","?r",(n,t)=>t.calc.wb.sheets.length);
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{format as $,isValidFormat as D}from"numfmt";import{CellError as i,ERR as d}from"../../values.js";import{numberOfText as N,toBool as b,toNumber as p,toText as u}from"../coerce.js";import{Missing as F}from"../types.js";import{alias as H,cellsOf as S,eachRow as O,many as B,number as x,numberArg as V,raw as M,scalar as f}from"./define.js";import{literalOf as X,wildcard as j}from"./criteria.js";import{delimiters as k,eachLetter as G,lowerLetters as _,occurrences as J}from"./delimiters.js";const g=n=>r=>{const t=u(r);return t instanceof i?t:n(t)},T=(n,r)=>{if(n===void 0)return r;const t=p(n);return t instanceof i?t:t<0?d.VALUE:Math.trunc(t)},I=n=>G(n,r=>r.toUpperCase()),y=_;f("LEFT",1,2,(n,r)=>{const t=u(n),e=T(r,1);return t instanceof i?t:e instanceof i?e:t.slice(0,e)}),f("RIGHT",1,2,(n,r)=>{const t=u(n),e=T(r,1);return t instanceof i?t:e instanceof i?e:e===0?"":t.slice(-e)}),f("MID",3,3,(n,r,t)=>{const e=u(n),s=p(r),o=T(t,0);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(s<1)return d.VALUE;const c=Math.trunc(s)-1;return e.slice(c,c+o)}),f("LEN",1,1,g(n=>n.length)),f("LOWER",1,1,g(y)),f("UPPER",1,1,g(I)),f("PROPER",1,1,g(n=>y(n).replace(/(^|[^\p{L}])(\p{L})/gu,(r,t,e)=>`${t}${I(e)}`))),f("TRIM",1,1,g(n=>n.replace(/ +/gu," ").replace(/^ | $/gu,""))),f("CLEAN",1,1,g(n=>n.replace(/[\u0000-\u001f]/gu,""))),f("T",1,1,n=>typeof n=="string"?n:""),f("EXACT",2,2,(n,r)=>{const t=u(n),e=u(r);return t instanceof i?t:e instanceof i?e:t===e}),f("REPT",2,2,(n,r)=>{const t=u(n),e=T(r,0);return t instanceof i?t:e instanceof i?e:t.length*e>32767?d.VALUE:t.repeat(e)}),f("CHAR",1,1,x(n=>{const r=Math.trunc(n);return r<1||r>255?d.VALUE:W(r)})),f("UNICHAR",1,1,x(n=>{const r=Math.trunc(n);return r<1||r>1114111?d.VALUE:String.fromCodePoint(r)})),f("CODE",1,1,g(n=>n===""?d.VALUE:n.charCodeAt(0))),f("UNICODE",1,1,g(n=>n.codePointAt(0)??d.VALUE));const K="€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ";function W(n){return n>=128&&n<160?K.charAt(n-128):String.fromCharCode(n)}M("CONCAT","(r)",(n,r)=>{let t="",e;return O(n,r,s=>{if(e)return;const o=u(s);o instanceof i?e=o:t+=o}),e??t}),f("CONCATENATE",1,B,(...n)=>{let r="";for(const t of n){const e=u(t);if(e instanceof i)return e;r+=e}return r}),M("TEXTJOIN","rv(r)",(n,r)=>{const[t,e,...s]=n,o=S(t??"",r);if(o instanceof i)return o;const c=[];for(let h=0;h<o.rows;h++)for(let A=0;A<o.cols;A++){const m=u(o.at(h,A));if(m instanceof i)return m;c.push(m)}const l=b(r.calc.scalar(e??!0,r.frame));if(l instanceof i)return l;const E=[];let a;if(O(s,r,h=>{if(a)return;const A=u(h);A instanceof i?a=A:(!l||A!=="")&&E.push(A)}),a)return a;let L=E[0]??"";for(let h=1;h<E.length;h++)L+=`${c.length>0?c[(h-1)%c.length]??"":""}${E[h]??""}`;return L}),f("SUBSTITUTE",3,4,(n,r,t,e)=>{const s=u(n),o=u(r),c=u(t);if(s instanceof i)return s;if(o instanceof i)return o;if(c instanceof i)return c;if(o==="")return s;if(e===void 0)return s.split(o).join(c);const l=p(e);if(l instanceof i)return l;if(l<1)return d.VALUE;let E=-1;for(let a=0;a<Math.trunc(l);a++)if(E=s.indexOf(o,E+1),E<0)return s;return`${s.slice(0,E)}${c}${s.slice(E+o.length)}`}),f("REPLACE",4,4,(n,r,t,e)=>{const s=u(n),o=p(r),c=T(t,0),l=u(e);if(s instanceof i)return s;if(o instanceof i)return o;if(c instanceof i)return c;if(l instanceof i)return l;if(o<1)return d.VALUE;const E=Math.trunc(o)-1;return`${s.slice(0,E)}${l}${s.slice(E+c)}`}),f("FIND",2,3,(n,r,t)=>{const e=u(n),s=u(r),o=t===void 0?1:p(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(o<1||o>s.length+1)return d.VALUE;const c=s.indexOf(e,Math.trunc(o)-1);return c<0?d.VALUE:c+1}),f("SEARCH",2,3,(n,r,t)=>{const e=u(n),s=u(r),o=t===void 0?1:p(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(o<1||o>s.length+1)return d.VALUE;const c=Math.trunc(o)-1,l=X(e);if(l!==void 0){const a=s.toLowerCase(),L=l.toLowerCase();if(a.length===s.length&&L.length===l.length){const h=a.indexOf(L,c);return h<0?d.VALUE:h+1}}const E=j(`${e}*`);for(let a=c;a<=s.length;a++)if(E(s.slice(a)))return a+1;return d.VALUE}),f("VALUE",1,1,n=>typeof n=="number"?n:n==null?0:typeof n!="string"?d.VALUE:N(n)??d.VALUE),f("NUMBERVALUE",1,3,(n,r,t)=>{const e=u(n),s=u(r??"."),o=u(t??",");if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;const c=e.replace(/\s/gu,"").split(o).join("").replace(s,".");if(c==="")return 0;const l=/%*$/u.exec(c)?.[0].length??0,E=Number(c.slice(0,c.length-l));return Number.isNaN(E)?d.VALUE:E/100**l}),f("TEXT",2,2,(n,r)=>{const t=u(r);if(t instanceof i)return t;if(!D(t))return d.VALUE;if(typeof n=="boolean")return n?"TRUE":"FALSE";const e=typeof n=="string"?N(n)??n:n??0;if(e instanceof i)return e;try{return $(t,e)}catch{return d.VALUE}});function w(n,r,t){const e=t?"#,##0":"0",s=r>0?`.${"0".repeat(r)}`:"";return $(`${e}${s}`,n)}const v=(n,r)=>{const t=10**r;return Math.sign(n)*Math.round(Math.abs(n)*t+1e-9)/t};f("FIXED",1,3,(n,r,t)=>{const e=p(n),s=r==null?2:p(r),o=t===void 0?!1:b(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;const c=Math.trunc(s);return w(v(e,c),Math.max(0,c),!o)}),f("DOLLAR",1,2,(n,r)=>{const t=p(n),e=r==null?2:p(r);if(t instanceof i)return t;if(e instanceof i)return e;const s=Math.trunc(e),o=w(Math.abs(v(t,s)),Math.max(0,s),!0);return t<0?`($${o})`:`$${o}`});function Y(n,r,t,e,s){const o=J(n,r,e);return t>0?(s&&o.push({at:n.length,length:0}),o[t-1]):(s&&o.unshift({at:0,length:0}),o[o.length+t])}function P(n){return(r,t)=>{const[e,s,o,c,l,E]=r.map(U=>U instanceof F?void 0:U),a=u(e??null),L=k(s,t),h=V(o,1),A=V(c,0),m=V(l,0);if(a instanceof i)return a;if(L instanceof i)return L;if(h instanceof i)return h;if(A instanceof i)return A;if(m instanceof i)return m;const C=Math.trunc(h);if(C===0||Math.abs(C)>Math.max(a.length,1))return d.VALUE;if(L.length>0&&L.every(U=>U===""))return C>0===(n==="before")?"":a;const R=Y(a,L,C,A===1,m===1);return R?n==="before"?a.slice(0,R.at):a.slice(R.at+R.length):E===void 0?d.NA:E}}M("TEXTBEFORE","vr?vvvv",P("before")),M("TEXTAFTER","vr?vvvv",P("after")),f("HYPERLINK",1,2,(n,r)=>r===void 0?n??null:r),f("ENCODEURL",1,1,g(n=>encodeURIComponent(n)));for(const n of["LEN","LEFT","RIGHT","MID","FIND","SEARCH","REPLACE"])H(`${n}B`,n);
2
+ import{format as $,isValidFormat as F}from"numfmt";import{CellError as i,ERR as E}from"../../values.js";import{numberOfText as N,toBool as b,toNumber as p,toText as u}from"../coerce.js";import{Missing as O}from"../types.js";import{alias as H,cellsOf as S,eachRow as x,many as B,number as I,numberArg as V,raw as M,scalar as f}from"./define.js";import{literalOf as X,wildcard as j}from"./criteria.js";import{delimiters as k,eachLetter as G,lowerLetters as _,occurrences as J}from"./delimiters.js";const g=n=>r=>{const t=u(r);return t instanceof i?t:n(t)},T=(n,r)=>{if(n===void 0)return r;const t=p(n);return t instanceof i?t:t<0?E.VALUE:Math.trunc(t)},y=n=>G(n,r=>r.toUpperCase()),w=_;f("LEFT",1,2,(n,r)=>{const t=u(n),e=T(r,1);return t instanceof i?t:e instanceof i?e:t.slice(0,e)}),f("RIGHT",1,2,(n,r)=>{const t=u(n),e=T(r,1);return t instanceof i?t:e instanceof i?e:e===0?"":t.slice(-e)}),f("MID",3,3,(n,r,t)=>{const e=u(n),s=p(r),o=T(t,0);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(s<1)return E.VALUE;const c=Math.trunc(s)-1;return e.slice(c,c+o)}),f("LEN",1,1,g(n=>n.length)),f("LOWER",1,1,g(w)),f("UPPER",1,1,g(y)),f("PROPER",1,1,g(n=>w(n).replace(/(^|[^\p{L}])(\p{L})/gu,(r,t,e)=>`${t}${y(e)}`))),f("TRIM",1,1,g(n=>n.replace(/ +/gu," ").replace(/^ | $/gu,""))),f("CLEAN",1,1,g(n=>n.replace(/[\u0000-\u001f]/gu,""))),f("T",1,1,n=>typeof n=="string"?n:""),f("EXACT",2,2,(n,r)=>{const t=u(n),e=u(r);return t instanceof i?t:e instanceof i?e:t===e}),f("REPT",2,2,(n,r)=>{const t=u(n),e=T(r,0);return t instanceof i?t:e instanceof i?e:t.length*e>32767?E.VALUE:t.repeat(e)}),f("CHAR",1,1,I(n=>{const r=Math.trunc(n);return r<1||r>255?E.VALUE:W(r)})),f("UNICHAR",1,1,I(n=>{const r=Math.trunc(n);return r<1||r>1114111?E.VALUE:String.fromCodePoint(r)})),f("CODE",1,1,g(n=>n===""?E.VALUE:n.charCodeAt(0))),f("UNICODE",1,1,g(n=>n.codePointAt(0)??E.VALUE));const K="€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ";function W(n){return n>=128&&n<160?K.charAt(n-128):String.fromCharCode(n)}M("CONCAT","(r)",(n,r)=>{let t="",e;return x(n,r,s=>{if(e)return;const o=u(s);o instanceof i?e=o:t+=o}),e??t}),f("CONCATENATE",1,B,(...n)=>{let r="";for(const t of n){const e=u(t);if(e instanceof i)return e;r+=e}return r}),M("TEXTJOIN","rv(r)",(n,r)=>{const[t,e,...s]=n,o=S(t??"",r);if(o instanceof i)return o;const c=[];for(let h=0;h<o.rows;h++)for(let A=0;A<o.cols;A++){const m=u(o.at(h,A));if(m instanceof i)return m;c.push(m)}const l=b(r.calc.scalar(e===void 0||e instanceof O?!0:e,r.frame));if(l instanceof i)return l;const d=[];let a;if(x(s,r,h=>{if(a)return;const A=u(h);A instanceof i?a=A:(!l||A!=="")&&d.push(A)}),a)return a;let L=d[0]??"";for(let h=1;h<d.length;h++)L+=`${c.length>0?c[(h-1)%c.length]??"":""}${d[h]??""}`;return L}),f("SUBSTITUTE",3,4,(n,r,t,e)=>{const s=u(n),o=u(r),c=u(t);if(s instanceof i)return s;if(o instanceof i)return o;if(c instanceof i)return c;if(o==="")return s;if(e===void 0)return s.split(o).join(c);const l=p(e);if(l instanceof i)return l;if(l<1)return E.VALUE;let d=-1;for(let a=0;a<Math.trunc(l);a++)if(d=s.indexOf(o,d+1),d<0)return s;return`${s.slice(0,d)}${c}${s.slice(d+o.length)}`}),f("REPLACE",4,4,(n,r,t,e)=>{const s=u(n),o=p(r),c=T(t,0),l=u(e);if(s instanceof i)return s;if(o instanceof i)return o;if(c instanceof i)return c;if(l instanceof i)return l;if(o<1)return E.VALUE;const d=Math.trunc(o)-1;return`${s.slice(0,d)}${l}${s.slice(d+c)}`}),f("FIND",2,3,(n,r,t)=>{const e=u(n),s=u(r),o=t===void 0?1:p(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(o<1||o>s.length+1)return E.VALUE;const c=s.indexOf(e,Math.trunc(o)-1);return c<0?E.VALUE:c+1}),f("SEARCH",2,3,(n,r,t)=>{const e=u(n),s=u(r),o=t===void 0?1:p(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;if(o<1||o>s.length+1)return E.VALUE;const c=Math.trunc(o)-1,l=X(e);if(l!==void 0){const a=s.toLowerCase(),L=l.toLowerCase();if(a.length===s.length&&L.length===l.length){const h=a.indexOf(L,c);return h<0?E.VALUE:h+1}}const d=j(`${e}*`);for(let a=c;a<=s.length;a++)if(d(s.slice(a)))return a+1;return E.VALUE}),f("VALUE",1,1,n=>typeof n=="number"?n:n==null?0:typeof n!="string"?E.VALUE:N(n)??E.VALUE),f("NUMBERVALUE",1,3,(n,r,t)=>{const e=u(n),s=u(r??"."),o=u(t??",");if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;const c=e.replace(/\s/gu,"").split(o).join("").replace(s,".");if(c==="")return 0;const l=/%*$/u.exec(c)?.[0].length??0,d=Number(c.slice(0,c.length-l));return Number.isNaN(d)?E.VALUE:d/100**l}),f("TEXT",2,2,(n,r)=>{const t=u(r);if(t instanceof i)return t;if(!F(t))return E.VALUE;if(typeof n=="boolean")return n?"TRUE":"FALSE";const e=typeof n=="string"?N(n)??n:n??0;if(e instanceof i)return e;try{return $(t,e)}catch{return E.VALUE}});function v(n,r,t){const e=t?"#,##0":"0",s=r>0?`.${"0".repeat(r)}`:"";return $(`${e}${s}`,n)}const P=(n,r)=>{const t=10**r;return Math.sign(n)*Math.round(Math.abs(n)*t+1e-9)/t};f("FIXED",1,3,(n,r,t)=>{const e=p(n),s=r==null?2:p(r),o=t===void 0?!1:b(t);if(e instanceof i)return e;if(s instanceof i)return s;if(o instanceof i)return o;const c=Math.trunc(s);return v(P(e,c),Math.max(0,c),!o)}),f("DOLLAR",1,2,(n,r)=>{const t=p(n),e=r==null?2:p(r);if(t instanceof i)return t;if(e instanceof i)return e;const s=Math.trunc(e),o=v(Math.abs(P(t,s)),Math.max(0,s),!0);return t<0?`($${o})`:`$${o}`});function Y(n,r,t,e,s){const o=J(n,r,e);return t>0?(s&&o.push({at:n.length,length:0}),o[t-1]):(s&&o.unshift({at:0,length:0}),o[o.length+t])}function D(n){return(r,t)=>{const[e,s,o,c,l,d]=r.map(U=>U instanceof O?void 0:U),a=u(e??null),L=k(s,t),h=V(o,1),A=V(c,0),m=V(l,0);if(a instanceof i)return a;if(L instanceof i)return L;if(h instanceof i)return h;if(A instanceof i)return A;if(m instanceof i)return m;const C=Math.trunc(h);if(C===0||Math.abs(C)>Math.max(a.length,1))return E.VALUE;if(L.length>0&&L.every(U=>U===""))return C>0===(n==="before")?"":a;const R=Y(a,L,C,A===1,m===1);return R?n==="before"?a.slice(0,R.at):a.slice(R.at+R.length):d===void 0?E.NA:d}}M("TEXTBEFORE","vr?vvvv",D("before")),M("TEXTAFTER","vr?vvvv",D("after")),f("HYPERLINK",1,2,(n,r)=>r===void 0?n??"":r),f("ENCODEURL",1,1,g(n=>encodeURIComponent(n)));for(const n of["LEN","LEFT","RIGHT","MID","FIND","SEARCH","REPLACE"])H(`${n}B`,n);
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{MAX_COLUMNS as N,MAX_ROWS as d,columnIndex as p}from"../address.js";import{errorOf as R}from"../values.js";import{TABLE_SECTIONS as C}from"./ast.js";class g extends Error{at;constructor(t,s){super(t),this.at=s}}const E=new Set(["=","<>","<","<=",">",">="]),w={"=":1,"<>":1,"<":1,"<=":1,">":1,">=":1,"&":2,"+":3,"-":3,"*":4,"/":4,"^":5},x=i=>i>="0"&&i<="9",m=i=>/[\p{L}_\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",A=i=>/[\p{L}\p{N}\p{M}_.?\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",u=(i,t)=>({index:i,abs:t});function f(i,t){const s={};return t.before&&(s.before=t.before),t.inner&&(s.inner=t.inner),t.after&&(s.after=t.after),Object.keys(s).length===0?i:{...i,space:{...i.space,...s}}}class L{at=0;s;constructor(t){this.s=t}blanks(){return this.space()!==""}space(){const t=this.at;for(;/\s/u.test(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}peek(t){return this.s.startsWith(t,this.at)}eat(t){return this.peek(t)?(this.at+=t.length,!0):!1}stop(t,s=this.at){throw new g(t,s)}parse(){this.eat("="),this.s.slice(this.at).trim()===""&&this.stop("a formula with nothing in it",this.s.length);const t=this.expression(0);return this.blanks(),this.at<this.s.length&&this.stop(`"${this.s.charAt(this.at)}" where an operator should be`),t}operator(){const t=this.at,s=this.space(),e=this.s.slice(this.at,this.at+2),n=this.s.charAt(this.at);return this.at=t,E.has(e)?{op:e,inner:s}:w[n]===void 0?void 0:{op:n,inner:s}}expression(t){let s=this.prefix();for(;;){const e=this.operator();if(e===void 0)return s;const{op:n,inner:r}=e,h=w[n]??0;if(h<=t)return s;this.at+=r.length+n.length;const a=this.expression(h);s=f({kind:"binary",op:n,left:s,right:a},{inner:r})}}prefix(){const t=this.at,s=this.space();for(const e of["-","+","@"])if(this.eat(e))return f({kind:"unary",op:e,arg:this.prefix()},{before:s});return this.at=t,this.postfix()}postfix(){let t=this.references();for(;;){const s=this.at;if(this.blanks(),this.eat("%")){t={kind:"postfix",op:"%",arg:t};continue}if(this.at=s,this.peek("#")&&!$(this.s,this.at)){this.at+=1,t={kind:"postfix",op:"#",arg:t};continue}if(this.peek("(")&&(t.kind==="call"||t.kind==="paren")){const{args:e}=this.call("");t={kind:"invoke",callee:t,args:e};continue}return t}}references(){let t=this.primary();for(;;){if(this.peek(":")){this.at+=1,this.blanks();const e=this.primary();t={kind:"binary",op:":",left:t,right:e};continue}const s=this.at;if(S(t)&&this.blanks()&&this.startsReference()){const e=this.primary();t={kind:"binary",op:" ",left:t,right:e};continue}return this.at=s,t}}startsReference(){const t=this.s.charAt(this.at);return t==="$"||t==="'"||t==="("||t==="["||m(t)}primary(){const t=this.space();return f(this.bare(),{before:t})}bare(){const t=this.s.charAt(this.at);if(t===""&&this.stop("the formula ends where a value should be"),t==="(")return this.parenthesis();if(t==="{")return this.arrayConstant();if(t==='"')return{kind:"string",value:this.text()};if(x(t)||t==="."){const s=k(this.s,this.at);return s?(this.at=s.end,{kind:"ref",...s.ref}):this.number()}return t==="#"?this.errorLiteral(void 0):t==="["?this.bracket(void 0):t==="$"||t==="'"||m(t)?this.word():this.stop(`"${t}" where a value should be`)}parenthesis(){const t=this.at;this.at+=1;let s=this.expression(0),e=this.space();for(;this.eat(",");){const n=this.expression(0);s=f({kind:"binary",op:",",left:s,right:n},{inner:e}),e=this.space()}return this.eat(")")||this.stop("a ( that is never closed",t),f({kind:"paren",arg:s},{inner:e})}number(){const t=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/u.exec(this.s.slice(this.at));return t?(this.at+=t[0].length,{kind:"number",value:Number(t[0]),raw:t[0]}):this.stop("a number that is not one")}text(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop('a " that is never closed',t);const e=this.s.charAt(this.at);if(e==='"'){if(this.s.charAt(this.at+1)!=='"')return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}errorLiteral(t){const s=$(this.s,this.at);return s===void 0?this.stop("a # that names no error"):(this.at+=s.length,{kind:"error",code:s,...t===void 0?{}:{sheet:t}})}arrayConstant(){const t=this.at;this.at+=1;const s=[[]];for(;;)if(this.blanks(),s[s.length-1]?.push(this.arrayItem()),this.blanks(),!this.eat(",")){if(this.eat(";")){s.push([]);continue}if(this.eat("}"))break;this.stop("a { that is never closed",t)}const e=s[0]?.length;return s.some(n=>n.length!==e)&&this.stop("an array whose rows are not all as long",t),{kind:"array",rows:s}}arrayItem(){const t=this.s.charAt(this.at);if(t==='"')return{kind:"string",value:this.text()};if(t==="#")return this.errorLiteral(void 0);if(t==="-"||t==="+")return this.at+=1,{kind:"unary",op:t,arg:this.number()};if(x(t)||t===".")return this.number();const s=this.name(),e=s.toUpperCase();return e==="TRUE"||e==="FALSE"?{kind:"boolean",value:e==="TRUE"}:this.stop(`${s} in an array: an array holds only constants`)}name(){const t=this.at;for(;this.at<this.s.length&&A(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}quoted(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop("a ' that is never closed",t);const e=this.s.charAt(this.at);if(e==="'"){if(this.s.charAt(this.at+1)!=="'")return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}word(){const t=this.sheetPrefix();if(t)return this.afterSheet(t.first,t.last);const s=k(this.s,this.at);if(s)return this.at=s.end,{kind:"ref",...s.ref};const e=y(this.s,this.at);if(e)return this.at=e.end,{kind:"ref",...e.ref};if(this.peek("$"))return this.stop('a "$" that is not part of a reference');const n=this.at,r=this.name();if(r==="")return this.stop(`"${this.s.charAt(n)}" in a name`);if(this.peek("("))return this.call(r);if(this.peek("["))return this.bracket(r);const h=r.toUpperCase();return h==="TRUE"||h==="FALSE"?{kind:"boolean",value:h==="TRUE"}:{kind:"name",name:r}}sheetPrefix(){const t=this.at;if(this.peek("'")){const e=this.quoted();if(!this.eat("!")){this.at=t;return}const n=e.indexOf(":");return n>0?{first:e.slice(0,n),last:e.slice(n+1)}:{first:e}}const s=this.name();if(s!==""&&this.eat("!"))return{first:s};if(s!==""&&this.eat(":")){const e=this.name();if(e!==""&&this.eat("!"))return{first:s,last:e}}this.at=t}afterSheet(t,s){const e={sheet:t,...s===void 0?{}:{lastSheet:s}};if(this.peek("#"))return this.errorLiteral(t);const n=k(this.s,this.at);if(n)return this.at=n.end,{kind:"ref",...e,...n.ref};const r=y(this.s,this.at);if(r)return this.at=r.end,{kind:"ref",...e,...r.ref};const h=this.name();return h===""?this.stop(`nothing after ${t}!`):{kind:"name",name:h,sheet:t}}call(t){const s=this.at;this.at+=1;const e=[],n=a=>f({kind:"call",name:t.toUpperCase(),written:t,args:e},{inner:a}),r=this.at,h=this.space();if(this.eat(")"))return n(h);for(this.at=r;;){const a=this.at,c=this.space();let o;this.peek(",")||this.peek(")")?o=f({kind:"missing"},{before:c}):(this.at=a,o=this.expression(0));const b=this.space();if(this.eat(",")){e.push(f(o,{after:b}));continue}if(e.push(o),this.eat(")"))return n(b);this.stop(`a ( of ${t.toUpperCase()} that is never closed`,s)}}columnName(){let t="";for(;;){this.at>=this.s.length&&this.stop("a [ that is never closed");const s=this.s.charAt(this.at);if(s==="]")return t;if(s==="["&&this.stop("a [ inside a column's name"),s==="'"){this.at+=1,t+=this.s.charAt(this.at),this.at+=1;continue}t+=s,this.at+=1}}bracket(t){const s=this.at;this.at+=1;const e={kind:"structured",...t===void 0?{}:{table:t},sections:[],columns:[],thisRow:!1};if(this.eat("]"))return e;if(!(this.peek("[")||this.peek("#")||this.peek("@"))){const r=this.columnName();return this.at+=1,t===void 0&&/[!']/u.test(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),t===void 0&&m(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),e.columns=[r.trim()],e}return this.eat("@")?(e.thisRow=!0,this.eat("]")||(this.peek("[")?this.items(e):e.columns=[this.columnName().trim()],this.eat("]")||this.stop("a [ that is never closed",s)),e):(this.items(e),this.eat("]")||this.stop("a [ that is never closed",s),e)}items(t){for(;;){if(this.blanks(),this.peek("#"))this.section(t);else if(this.eat("["))if(this.peek("#"))this.section(t),this.eat("]")||this.stop("a [ that is never closed");else{const s=this.columnName().trim();this.at+=1,t.columns.push(s),this.eat(":")&&(this.eat("[")||this.stop("a column's name after :"),t.columns.push(this.columnName().trim()),this.at+=1)}else this.stop("a column or a section of a table");if(this.blanks(),!this.eat(","))return}}section(t){const s=this.s.slice(this.at).toLowerCase(),e=C.find(n=>s.startsWith(n.toLowerCase()));e===void 0&&this.stop("a section of a table that is not one"),this.at+=e.length,e==="#This Row"?t.thisRow=!0:t.sections.push(e)}}const S=i=>i.kind==="ref"||i.kind==="name"||i.kind==="structured"||i.kind==="call"||i.kind==="paren"||i.kind==="binary"&&(i.op===":"||i.op===" ");function $(i,t){const s=/^#[A-Za-z0-9_/]+[!?]?/u.exec(i.slice(t));return s?R(s[0])?.code:void 0}const l=(i,t)=>{const s=i.charAt(t);return s!==""&&(A(s)||s==="("||s==="!"||s==="[")};function y(i,t){const s=i.slice(t),e=/^(\$?)([A-Za-z]{1,3}):(\$?)([A-Za-z]{1,3})/u.exec(s);if(e){const a=t+e[0].length,c=p(e[2]??""),o=p(e[4]??"");if(c!==void 0&&o!==void 0&&!l(i,a))return{ref:{col:u(c,e[1]==="$"),col2:u(o,e[3]==="$"),range:!0},end:a}}const n=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(s);if(!n)return;const r=v(n),h=t+n[0].length;if(!(!r||l(i,h))){if(i.charAt(h)===":"){const a=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(i.slice(h+1));if(a){const c=v(a),o=h+1+a[0].length;if(c&&!l(i,o))return{ref:{...r,col2:c.col,row2:c.row,range:!0},end:o}}}return{ref:{...r,range:!1},end:h}}}function v(i){const t=p(i[2]??""),s=Number(i[4])-1;if(!(t===void 0||t>=N||s>=d))return{col:u(t,i[1]==="$"),row:u(s,i[3]==="$")}}function k(i,t){const s=/^(\$?)([1-9]\d*):(\$?)([1-9]\d*)/u.exec(i.slice(t));if(!s)return;const e=t+s[0].length,n=Number(s[2])-1,r=Number(s[4])-1;if(!(n>=d||r>=d||l(i,e)))return{ref:{row:u(n,s[1]==="$"),row2:u(r,s[3]==="$"),range:!0},end:e}}export function parseFormula(i){try{return{ok:!0,node:new L(i).parse()}}catch(t){if(t instanceof g)return{ok:!1,error:{message:t.message,at:t.at}};throw t}}
2
+ import{MAX_COLUMNS as L,MAX_ROWS as p,columnIndex as m}from"../address.js";import{errorOf as R}from"../values.js";import{TABLE_SECTIONS as E}from"./ast.js";class x extends Error{at;constructor(t,s){super(t),this.at=s}}const S=new Set(["=","<>","<","<=",">",">="]),A={"=":1,"<>":1,"<":1,"<=":1,">":1,">=":1,"&":2,"+":3,"-":3,"*":4,"/":4,"^":5},$=i=>i>="0"&&i<="9",k=i=>/[\p{L}_\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",y=i=>/[\p{L}\p{N}\p{M}_.?\\]/u.test(i)||i>="\uD800"&&i<="\uDFFF",l=(i,t)=>({index:i,abs:t});function f(i,t){const s={};return t.before&&(s.before=t.before),t.inner&&(s.inner=t.inner),t.after&&(s.after=t.after),Object.keys(s).length===0?i:{...i,space:{...i.space,...s}}}class O{at=0;s;locals=[];constructor(t){this.s=t}blanks(){return this.space()!==""}space(){const t=this.at;for(;/\s/u.test(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}peek(t){return this.s.startsWith(t,this.at)}eat(t){return this.peek(t)?(this.at+=t.length,!0):!1}stop(t,s=this.at){throw new x(t,s)}parse(){this.eat("="),this.s.slice(this.at).trim()===""&&this.stop("a formula with nothing in it",this.s.length);const t=this.expression(0);return this.blanks(),this.at<this.s.length&&this.stop(`"${this.s.charAt(this.at)}" where an operator should be`),t}operator(){const t=this.at,s=this.space(),e=this.s.slice(this.at,this.at+2),n=this.s.charAt(this.at);return this.at=t,S.has(e)?{op:e,inner:s}:A[n]===void 0?void 0:{op:n,inner:s}}expression(t){let s=this.prefix();for(;;){const e=this.operator();if(e===void 0)return s;const{op:n,inner:r}=e,h=A[n]??0;if(h<=t)return s;this.at+=r.length+n.length;const a=this.expression(h);s=f({kind:"binary",op:n,left:s,right:a},{inner:r})}}prefix(){const t=this.at,s=this.space();for(const e of["-","+","@"])if(this.eat(e))return f({kind:"unary",op:e,arg:this.prefix()},{before:s});return this.at=t,this.postfix()}postfix(){let t=this.references();for(;;){const s=this.at;if(this.blanks(),this.eat("%")){t={kind:"postfix",op:"%",arg:t};continue}if(this.at=s,this.peek("#")&&!b(this.s,this.at)){this.at+=1,t={kind:"postfix",op:"#",arg:t};continue}if(this.peek("(")&&(t.kind==="call"||t.kind==="paren")){const{args:e}=this.call("");t={kind:"invoke",callee:t,args:e};continue}return t}}spilled(){const t=this.primary();return!this.peek("#")||b(this.s,this.at)?t:(this.at+=1,{kind:"postfix",op:"#",arg:t})}references(){let t=this.spilled();for(;;){if(this.peek(":")){this.at+=1,this.blanks();const e=this.spilled();t={kind:"binary",op:":",left:t,right:e};continue}const s=this.at;if(T(t)&&this.blanks()&&this.startsReference()){const e=this.primary();t={kind:"binary",op:" ",left:t,right:e};continue}return this.at=s,t}}startsReference(){const t=this.s.charAt(this.at);return t==="$"||t==="'"||t==="("||t==="["||k(t)}primary(){const t=this.space();return f(this.bare(),{before:t})}bare(){const t=this.s.charAt(this.at);if(t===""&&this.stop("the formula ends where a value should be"),t==="(")return this.parenthesis();if(t==="{")return this.arrayConstant();if(t==='"')return{kind:"string",value:this.text()};if($(t)||t==="."){const s=g(this.s,this.at);return s?(this.at=s.end,{kind:"ref",...s.ref}):this.number()}return t==="#"?this.errorLiteral(void 0):t==="["?this.bracket(void 0):t==="$"||t==="'"||k(t)?this.word():this.stop(`"${t}" where a value should be`)}parenthesis(){const t=this.at;this.at+=1;let s=this.expression(0),e=this.space();for(;this.eat(",");){const n=this.expression(0);s=f({kind:"binary",op:",",left:s,right:n},{inner:e}),e=this.space()}return this.eat(")")||this.stop("a ( that is never closed",t),f({kind:"paren",arg:s},{inner:e})}number(){const t=/^(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/u.exec(this.s.slice(this.at));return t?(this.at+=t[0].length,{kind:"number",value:Number(t[0]),raw:t[0]}):this.stop("a number that is not one")}text(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop('a " that is never closed',t);const e=this.s.charAt(this.at);if(e==='"'){if(this.s.charAt(this.at+1)!=='"')return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}errorLiteral(t){const s=b(this.s,this.at);return s===void 0?this.stop("a # that names no error"):(this.at+=s.length,{kind:"error",code:s,...t===void 0?{}:{sheet:t}})}arrayConstant(){const t=this.at;this.at+=1;const s=[[]];for(;;)if(this.blanks(),s[s.length-1]?.push(this.arrayItem()),this.blanks(),!this.eat(",")){if(this.eat(";")){s.push([]);continue}if(this.eat("}"))break;this.stop("a { that is never closed",t)}const e=s[0]?.length;return s.some(n=>n.length!==e)&&this.stop("an array whose rows are not all as long",t),{kind:"array",rows:s}}arrayItem(){const t=this.s.charAt(this.at);if(t==='"')return{kind:"string",value:this.text()};if(t==="#")return this.errorLiteral(void 0);if(t==="-"||t==="+")return this.at+=1,{kind:"unary",op:t,arg:this.number()};if($(t)||t===".")return this.number();const s=this.name(),e=s.toUpperCase();return e==="TRUE"||e==="FALSE"?{kind:"boolean",value:e==="TRUE"}:this.stop(`${s} in an array: an array holds only constants`)}name(){const t=this.at;for(;this.at<this.s.length&&y(this.s.charAt(this.at));)this.at+=1;return this.s.slice(t,this.at)}quoted(){const t=this.at;this.at+=1;let s="";for(;;){this.at>=this.s.length&&this.stop("a ' that is never closed",t);const e=this.s.charAt(this.at);if(e==="'"){if(this.s.charAt(this.at+1)!=="'")return this.at+=1,s;this.at+=1}s+=e,this.at+=1}}bound(t){const s=t.toLowerCase();return this.locals.some(e=>e.has(s))}word(){const t=this.sheetPrefix();if(t)return this.afterSheet(t.first,t.last);const s=g(this.s,this.at);if(s)return this.at=s.end,{kind:"ref",...s.ref};const e=/^([A-Za-z]{1,3}):([A-Za-z]{1,3})/u.exec(this.s.slice(this.at)),r=e!==null&&(this.bound(e[1]??"")||this.bound(e[2]??""))?void 0:N(this.s,this.at);if(r)return this.at=r.end,{kind:"ref",...r.ref};if(this.peek("$"))return this.stop('a "$" that is not part of a reference');const h=this.at,a=this.name();if(a==="")return this.stop(`"${this.s.charAt(h)}" in a name`);if(this.peek("("))return this.call(a);if(this.peek("["))return this.bracket(a);const o=a.toUpperCase();return o==="TRUE"||o==="FALSE"?{kind:"boolean",value:o==="TRUE"}:{kind:"name",name:a}}sheetPrefix(){const t=this.at;if(this.peek("'")){const e=this.quoted();if(!this.eat("!")){this.at=t;return}const n=e.indexOf(":");return n>0?{first:e.slice(0,n),last:e.slice(n+1)}:{first:e}}const s=this.name();if(s!==""&&this.eat("!"))return{first:s};if(s!==""&&this.eat(":")){const e=this.name();if(e!==""&&this.eat("!"))return{first:s,last:e}}this.at=t}afterSheet(t,s){const e={sheet:t,...s===void 0?{}:{lastSheet:s}};if(this.peek("#"))return this.errorLiteral(t);const n=g(this.s,this.at);if(n)return this.at=n.end,{kind:"ref",...e,...n.ref};const r=N(this.s,this.at);if(r)return this.at=r.end,{kind:"ref",...e,...r.ref};const h=this.name();return h===""?this.stop(`nothing after ${t}!`):{kind:"name",name:h,sheet:t}}call(t){const s=this.at;this.at+=1;const e=[],n=c=>f({kind:"call",name:t.toUpperCase(),written:t,args:e},{inner:c}),r=this.at,h=this.space();if(this.eat(")"))return n(h);this.at=r;const a=U.test(t)?new Set:void 0;a&&this.locals.push(a);const o=c=>(a&&this.locals.pop(),n(c));for(;;){const c=this.at,C=this.space();let u;this.peek(",")||this.peek(")")?u=f({kind:"missing"},{before:C}):(this.at=c,u=this.expression(0));const w=this.space();if(a&&u.kind==="name"&&u.sheet===void 0&&a.add(u.name.replace(/^_xlpm\./iu,"").toLowerCase()),this.eat(",")){e.push(f(u,{after:w}));continue}if(e.push(u),this.eat(")"))return o(w);this.stop(`a ( of ${t.toUpperCase()} that is never closed`,s)}}columnName(){let t="";for(;;){this.at>=this.s.length&&this.stop("a [ that is never closed");const s=this.s.charAt(this.at);if(s==="]")return t;if(s==="["&&this.stop("a [ inside a column's name"),s==="'"){this.at+=1,t+=this.s.charAt(this.at),this.at+=1;continue}t+=s,this.at+=1}}bracket(t){const s=this.at;this.at+=1;const e={kind:"structured",...t===void 0?{}:{table:t},sections:[],columns:[],thisRow:!1};if(this.eat("]"))return e;if(!(this.peek("[")||this.peek("#")||this.peek("@"))){const r=this.columnName();return this.at+=1,t===void 0&&/[!']/u.test(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),t===void 0&&k(this.s.charAt(this.at))&&this.stop("a formula that reaches into another workbook",s),e.columns=[r.trim()],e}return this.eat("@")?(e.thisRow=!0,this.eat("]")||(this.peek("[")?this.items(e):e.columns=[this.columnName().trim()],this.eat("]")||this.stop("a [ that is never closed",s)),e):(this.items(e),this.eat("]")||this.stop("a [ that is never closed",s),e)}items(t){for(;;){if(this.blanks(),this.peek("#"))this.section(t);else if(this.eat("["))if(this.peek("#"))this.section(t),this.eat("]")||this.stop("a [ that is never closed");else{const s=this.columnName().trim();this.at+=1,t.columns.push(s),this.eat(":")&&(this.eat("[")||this.stop("a column's name after :"),t.columns.push(this.columnName().trim()),this.at+=1)}else this.stop("a column or a section of a table");if(this.blanks(),!this.eat(","))return}}section(t){const s=this.s.slice(this.at).toLowerCase(),e=E.find(n=>s.startsWith(n.toLowerCase()));e===void 0&&this.stop("a section of a table that is not one"),this.at+=e.length,e==="#This Row"?t.thisRow=!0:t.sections.push(e)}}const U=/^(?:_xlfn\.)?(?:LET|LAMBDA)$/iu,T=i=>i.kind==="ref"||i.kind==="name"||i.kind==="structured"||i.kind==="call"||i.kind==="paren"||i.kind==="binary"&&(i.op===":"||i.op===" ");function b(i,t){const s=/^#[A-Za-z0-9_/]+[!?]?/u.exec(i.slice(t));return s?R(s[0])?.code:void 0}const d=(i,t)=>{const s=i.charAt(t);return s!==""&&(y(s)||s==="("||s==="!"||s==="[")};function N(i,t){const s=i.slice(t),e=/^(\$?)([A-Za-z]{1,3}):(\$?)([A-Za-z]{1,3})/u.exec(s);if(e){const a=t+e[0].length,o=m(e[2]??""),c=m(e[4]??"");if(o!==void 0&&c!==void 0&&!d(i,a))return{ref:{col:l(o,e[1]==="$"),col2:l(c,e[3]==="$"),range:!0},end:a}}const n=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(s);if(!n)return;const r=v(n),h=t+n[0].length;if(!(!r||d(i,h))){if(i.charAt(h)===":"){const a=/^(\$?)([A-Za-z]{1,3})(\$?)([1-9]\d*)/u.exec(i.slice(h+1));if(a){const o=v(a),c=h+1+a[0].length;if(o&&!d(i,c))return{ref:{...r,col2:o.col,row2:o.row,range:!0},end:c}}}return{ref:{...r,range:!1},end:h}}}function v(i){const t=m(i[2]??""),s=Number(i[4])-1;if(!(t===void 0||t>=L||s>=p))return{col:l(t,i[1]==="$"),row:l(s,i[3]==="$")}}function g(i,t){const s=/^(\$?)([1-9]\d*):(\$?)([1-9]\d*)/u.exec(i.slice(t));if(!s)return;const e=t+s[0].length,n=Number(s[2])-1,r=Number(s[4])-1;if(!(n>=p||r>=p||d(i,e)))return{ref:{row:l(n,s[1]==="$"),row2:l(r,s[3]==="$"),range:!0},end:e}}export function parseFormula(i){try{return{ok:!0,node:new O(i).parse()}}catch(t){if(t instanceof x)return{ok:!1,error:{message:t.message,at:t.at}};throw t}}
@@ -29,7 +29,7 @@ export type ReadChart = SheetChart;
29
29
  * whose numbers are in no cell of the workbook is left out, and `said`
30
30
  * says so: written again, its numbers would be in the chart alone.
31
31
  */
32
- export declare function readChart(root: Tag | undefined, said?: string[], has?: (sheet: string) => boolean): ReadChart | string;
32
+ export declare function readChart(root: Tag | undefined, said?: string[], has?: (sheet: string) => boolean, defined?: (name: string) => boolean): ReadChart | string;
33
33
  export interface Anchored {
34
34
  /** Its relationship in the drawing's part. */
35
35
  id: string;
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{CHART_DEFAULTS as ge,chartSpaceXml as Pe}from"@allyhub/chart";import{columnLetter as we}from"../address.js";import{Reference as ye,Union as Se}from"../calc/types.js";import{CellError as Me}from"../values.js";import{categoryNumbers as Fe,chartDef as Re,chartSize as De}from"../chart.js";import{parseFormula as Ne}from"../formula/parse.js";import{sheetPrefix as be}from"../formula/print.js";import{HEAD as _e,attr as b,el as d,flag as ve,numberAttr as E,tag as r,tags as O,textOf as P}from"./xml.js";const je="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing",Ee="http://schemas.openxmlformats.org/drawingml/2006/main",$e="http://schemas.openxmlformats.org/drawingml/2006/chart",Oe="http://schemas.openxmlformats.org/officeDocument/2006/relationships",v=9525,Le=(e,n)=>`${be(e.sheet.name)}$${we(e.left)}$${String(e.top+1)}${e.single?"":`:$${we(e.right)}$${String(n+1)}`}`;function L(e,n,t){if(!t?.node)return;const o=e.eval(t.node,{sheet:n,row:0,col:0,dr:0,dc:0}),s=l=>Le(l,Math.min(l.bottom,Math.max(l.top,l.sheet.lastRow)));if(o instanceof ye)return s(o);if(o instanceof Se)return`(${o.parts.map(s).join(",")})`;const a=t.node;if(a.kind==="name"&&a.sheet!==void 0&&!(o instanceof Me&&o.code==="#NAME?"))return`${be(a.sheet)}${a.name}`}function ze(e,n,t){if(!t?.node)return!1;const o=e.eval(t.node,{sheet:n,row:0,col:0,dr:0,dc:0});return o instanceof ye&&typeof e.value(o.sheet,o.top,o.left)=="number"}export function chartPart(e,n,t){const o=Re(e,n,t),s=t.series.map((c,f)=>({s:c,i:f,values:L(e,n,c.values)})).filter(c=>c.values!==void 0),a={...o,series:s.flatMap(c=>o.series[c.i]??[])},l=ze(e,n,t.categories),h=l?Fe(e,n,t):void 0,m={name:c=>L(e,n,s[c]?.s.name),categories:L(e,n,t.categories),numeric:l,...h===void 0?{}:{categoryNumbers:h.values,categoryFormat:h.format},values:c=>s[c]?.values??"",x:c=>L(e,n,s[c]?.s.x??t.categories)},y=L(e,n,t.title);return Pe(a,{refs:m,...y===void 0?{}:{titleRef:y}})}const Ge=(e,n,t)=>d("xdr:oneCellAnchor",{},d("xdr:from",{},`<xdr:col>${String(e.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round(e.x*v))}</xdr:colOff>`,`<xdr:row>${String(e.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((e.y??0)*v))}</xdr:rowOff>`),d("xdr:ext",{cx:Math.round(e.width*v),cy:Math.round(e.height*v)}),d("xdr:pic",{},d("xdr:nvPicPr",{},d("xdr:cNvPr",{id:n,name:e.name,descr:e.path}),d("xdr:cNvPicPr",{},d("a:picLocks",{noChangeAspect:!0}))),d("xdr:blipFill",{},d("a:blip",{"xmlns:r":Oe,"r:embed":`rId${String(t)}`}),d("a:stretch",{},d("a:fillRect"))),d("xdr:spPr",{},d("a:xfrm",{},d("a:off",{x:0,y:0}),d("a:ext",{cx:Math.round(e.width*v),cy:Math.round(e.height*v)})),d("a:prstGeom",{prst:"rect"},d("a:avLst")))),d("xdr:clientData"));export function drawingPart(e,n=[]){const t=(o,s)=>{const{width:a,height:l}=De(o.spec);return d("xdr:oneCellAnchor",{},d("xdr:from",{},`<xdr:col>${String(o.at.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round((o.spec.x??0)*v))}</xdr:colOff>`,`<xdr:row>${String(o.at.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((o.spec.y??0)*v))}</xdr:rowOff>`),d("xdr:ext",{cx:Math.round(a*v),cy:Math.round(l*v)}),d("xdr:graphicFrame",{macro:""},d("xdr:nvGraphicFramePr",{},d("xdr:cNvPr",{id:s+2,name:`Chart ${String(s+1)}`}),d("xdr:cNvGraphicFramePr")),d("xdr:xfrm",{},d("a:off",{x:0,y:0}),d("a:ext",{cx:0,cy:0})),d("a:graphic",{},d("a:graphicData",{uri:$e},d("c:chart",{"xmlns:c":$e,"xmlns:r":Oe,"r:id":`rId${String(s+1)}`})))),d("xdr:clientData"))};return _e+d("xdr:wsDr",{"xmlns:xdr":je,"xmlns:a":Ee},...e.map(t),...n.map((o,s)=>Ge(o,e.length+s+2,e.length+s+1)))}function ne(e){if(Array.isArray(e))return e.map(ne).join("");if(typeof e!="object"||e===null)return"";const n=e,t=[];for(const[o,s]of Object.entries(n))o==="t"?t.push(P(s)):typeof s=="object"&&t.push(ne(s));return t.join("")}const te=e=>{const n=r(e?.solidFill),t=b(r(n?.srgbClr),"val");return t!==void 0?`#${t.toUpperCase()}`:Ie[b(r(n?.schemeClr),"val")??""]},Ie={accent1:"#4472C4",accent2:"#ED7D31",accent3:"#A5A5A5",accent4:"#FFC000",accent5:"#5B9BD5",accent6:"#70AD47"},A=(e,n)=>b(r(e?.[n]),"val"),M=(e,n)=>E(r(e?.[n]),"val"),z=(e,n)=>ve(r(e?.[n]),"val"),Te=e=>/^"(?:[^"]|"")*"$/u.test(e)?e.slice(1,-1).replaceAll('""','"'):void 0,Ae=e=>Te(e)??`=${e}`;function Ce(e){if(!e)return;const n=r(e.tx),t=r(n?.strRef);if(t)return Ae(P(t.f));const o=ne(n?.rich);return o===""?void 0:o}function re(e){if(!e)return;const n=r(e.scaling),t=r(e.numFmt),o=b(t,"formatCode"),s={},a=Ce(r(e.title));a!==void 0&&(s.title=a);const l=M(n,"min"),h=M(n,"max"),m=M(e,"majorUnit");return l!==void 0&&(s.min=l),h!==void 0&&(s.max=h),m!==void 0&&m>0&&(s.major=m),z(e,"delete")===!0&&(s.visible=!1),A(n,"orientation")==="maxMin"&&(s.reverse=!0),o!==void 0&&o!=="General"&&ve(t,"sourceLinked")!==!0&&(s.format=o),s}const G=e=>{const n=r(e?.numRef)??r(e?.strRef),t=n?P(n.f):"";return t===""?void 0:t};function We(e,n){const t=G(r(e[n?"yVal":"val"]));if(t===void 0)return"its numbers are written in the chart";const o={values:t},s=r(e.tx),a=G(s);if(a!==void 0)o.name=Ae(a);else{const u=P(s?.v);u!==""&&(o.name=u)}const l=r(e.spPr),h=r(l?.ln),m=r(e.marker),y=n||m!==void 0,c=te(l)??(y?te(h)??te(r(m?.spPr)):void 0);c!==void 0&&(o.color=c);const f=E(h,"w");if(y&&f!==void 0&&h?.noFill===void 0){const u=Math.max(.25,Number((f/12700).toFixed(2)));u!==ge.lineWidth&&(o.line_width=u)}z(e,"smooth")===!0&&(o.smooth=!0);const p=M(m,"size");p!==void 0&&p!==ge.marker&&(o.marker_size=p);const $=n?G(r(e.xVal)):void 0;$!==void 0&&(o.x=$);const g=n?void 0:G(r(e.cat));return{series:o,...g===void 0?{}:{categories:g}}}function oe(e,n){const t=O(n.ser),o=a=>r(r(a.spPr)?.ln)?.noFill!==void 0,s=a=>A(r(a.marker),"symbol")==="none";switch(e){case"barChart":return Ue(n);case"lineChart":return t.every(s)?"line":"line-markers";case"areaChart":return Be(n);case"pieChart":return"pie";case"doughnutChart":return"doughnut";case"scatterChart":return t.every(o)?"scatter":t.some(a=>z(a,"smooth")===!0)?"scatter-smooth":"scatter-lines";default:return}}function Ue(e){const n=A(e,"barDir")==="bar"?"bar":"column",t=A(e,"grouping");return t==="stacked"?`${n}-stacked`:t==="percentStacked"?`${n}-stacked-100`:`${n}-clustered`}function Be(e){const n=A(e,"grouping");return n==="stacked"?"area-stacked":n==="percentStacked"?"area-stacked-100":"area"}function I(e,n){if(e===void 0)return;const t=e.replace(/^=/u,"");if(/(?:^|[(,])'?\[\d+\]/u.test(t))return"it reads another workbook";if(e.includes("#REF!"))return"its cells were deleted (#REF!)";const o=/^(?:'((?:[^']|'')+)'|([^!'(),]+))!/u.exec(t),s=o?.[1]?.replaceAll("''","'")??o?.[2];if(s!==void 0&&!n(s))return`it reads a sheet the workbook does not have (${s})`;const a=Ne(`=${t}`);if(!a.ok||!T(a.node))return`it names no cells (${t})`}function T(e){switch(e.kind){case"ref":case"name":case"structured":return!0;case"paren":return T(e.arg);case"binary":return(e.op===","||e.op===":")&&T(e.left)&&T(e.right);default:return!1}}export function readChart(e,n=[],t=()=>!0){const o=r(e?.chartSpace),s=r(o?.chart),a=r(s?.plotArea);if(!s||!a)return"it has no plot";const l=Object.entries(a).filter(([i])=>i.endsWith("Chart")).flatMap(([i,x])=>O(x).map(k=>({kind:i,plot:k}))),[h]=l;if(!h)return"it has no plot";const m=O(a.valAx),y=i=>O(i.axId).map(x=>b(x,"val")),c=i=>{const x=m.find(k=>y(i).includes(b(r(k.axId),"val")));return x!==void 0&&A(x,"axPos")==="r"},f=l.length>1,p=l.map(({kind:i,plot:x})=>({kind:i,plot:x,type:oe(i,x),secondary:f&&c(x)}));if(f&&p.some(i=>i.type===void 0||!(i.type.startsWith("column")||i.type.startsWith("line")||i.type.startsWith("area"))))return"it combines several kinds of chart, not columns, lines and areas";const $=p.find(i=>!i.secondary)??p[0]??h,{kind:g,plot:u}=$,F=g==="scatterChart",S=[];let R,D;const N=p.flatMap(i=>O(i.plot.ser).map(x=>({s:x,of:i}))),U=N.filter(({s:i})=>i.trendline!==void 0).length;U>0&&n.push(U===1?"trendline":`trendlines of ${String(U)} series`);const B=N.filter(({s:i})=>i.errBars!==void 0).length;B>0&&n.push(B===1?"error bars":`error bars of ${String(B)} series`);for(const[i,{s:x,of:k}]of N.entries()){const w=We(x,F);if(typeof w=="string")return w;if(f){const ee=k.type;ee!==void 0&&ee!==oe(g,u)&&(w.series.type=ee),k.secondary&&(w.series.secondary=!0)}const Z=I(w.series.values,t);if(Z!==void 0){n.push(`series ${String(i+1)}, as ${Z}`),D=Z;continue}w.series.name?.startsWith("=")===!0&&I(w.series.name,t)!==void 0&&delete w.series.name,I(w.series.x,t)!==void 0&&delete w.series.x,S.push(w.series),I(w.categories,t)===void 0&&(R??=w.categories)}if(S.length===0)return D===void 0?"it has no series":`every series ${D.replace(/^it /u,"")}`;const j=oe(g,u);if(!j)return`a sheet has no ${g.replace(/Chart$/u,"")} chart`;const se=Ce(r(s.title)),ie=r(s.legend),V=A(ie,"legendPos"),ce=r(u.dLbls),C=i=>z(ce,i)===!0,ke=C("showVal")||C("showPercent")||C("showCatName")||C("showSerName"),ae=A(ce,"dLblPos"),H=M(u,"gapWidth"),de=M(u,"overlap"),X=M(u,"holeSize"),fe=z(u,"varyColors"),ue=j==="pie"||j==="doughnut",le=i=>y(u).includes(b(r(i.axId),"val")),me=[...O(a.catAx),...O(a.dateAx)],_={value:f?m.find(le)??m[0]:m.find((i,x,k)=>F?x===k.length-1:!0),category:F?m[0]:f?me.find(le)??me[0]:r(a.catAx)??r(a.dateAx),second:p.some(i=>i.secondary)?m.find(i=>A(i,"axPos")==="r"):void 0},q=re(_.value),J=re(_.category);q&&_.value?.majorGridlines===void 0&&(q.gridlines=!1),J&&_.category?.majorGridlines!==void 0&&(J.gridlines=!0);const K=E(r(r(r(r(o?.txPr)?.p)?.pPr)?.defRPr),"sz"),Q=i=>i&&Object.keys(i).length>0?i:void 0,he=Q(q),pe=Q(J),Y=re(_.second);Y&&_.second?.majorGridlines!==void 0&&(Y.gridlines=!0);const xe=Q(Y);return{type:j,at:"A1",...se===void 0?{}:{title:se},...R===void 0?{}:{categories:R},series:ue?S.slice(0,1):S,...ie?{legend:V==="b"?"bottom":V==="t"?"top":V==="l"?"left":"right"}:{},...ke?{labels:{...C("showVal")?{value:!0}:{},...C("showPercent")?{percentage:!0}:{},...C("showCatName")?{category:!0}:{},...C("showSerName")?{series:!0}:{},...ae===void 0?{}:{position:ae}}}:{},...H===void 0||H===150?{}:{gap_width:H},...de===void 0||j.includes("stacked")?{}:{overlap:de},...X===void 0||X===50?{}:{hole_size:X},...fe===!0&&g==="barChart"?{vary_by_categories:!0}:{},...fe===!1&&ue?{vary_by_categories:!1}:{},...he?{value_axis:he}:{},...pe?{category_axis:pe}:{},...xe?{secondary_axis:xe}:{},...K===void 0||K===900?{}:{size:K/100}}}const W=e=>Math.round(e/v);export function chartAnchors(e,n,t){const o=r(e?.wsDr),s=[],a=[];let l=0;const h=c=>({col:Number(P(c?.col)),row:Number(P(c?.row)),colOff:W(Number(P(c?.colOff))),rowOff:W(Number(P(c?.rowOff)))}),m=c=>b(r(r(r(r(c.graphicFrame)?.graphic)?.graphicData)?.chart),"id"),y=(c,f,p,$)=>{const g={at:{row:f.row,col:f.col},...f.colOff>0?{x:f.colOff}:{},...f.rowOff>0?{y:f.rowOff}:{},width:p,height:$},u=r(c.pic),F=b(r(r(u?.blipFill)?.blip),"embed");if(u&&F!==void 0){const R=r(r(u.nvPicPr)?.cNvPr),D=b(R,"name"),N=b(R,"descr");a.push({id:F,...g,...D===void 0?{}:{name:D},...N===void 0?{}:{descr:N}});return}const S=m(c);if(S===void 0){l+=1;return}s.push({id:S,...g})};for(const c of O(o?.oneCellAnchor)){const f=r(c.ext);y(c,h(r(c.from)),W(E(f,"cx")??0),W(E(f,"cy")??0))}for(const c of O(o?.twoCellAnchor)){const f=h(r(c.from)),p=h(r(c.to));let $=p.colOff-f.colOff;for(let u=f.col;u<p.col;u++)$+=n(u);let g=p.rowOff-f.rowOff;for(let u=f.row;u<p.row;u++)g+=t(u);y(c,f,$,g)}return{anchors:s,pictures:a,others:l}}
2
+ import{CHART_DEFAULTS as we,chartSpaceXml as Se}from"@allyhub/chart";import{columnLetter as ye}from"../address.js";import{Reference as be,Union as Me}from"../calc/types.js";import{CellError as Fe}from"../values.js";import{categoryNumbers as Re,chartDef as De,chartSize as Ne}from"../chart.js";import{parseFormula as _e}from"../formula/parse.js";import{sheetPrefix as ve}from"../formula/print.js";import{HEAD as je,attr as v,el as a,flag as $e,numberAttr as E,tag as o,tags as A,textOf as S}from"./xml.js";const Ee="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing",Le="http://schemas.openxmlformats.org/drawingml/2006/main",Oe="http://schemas.openxmlformats.org/drawingml/2006/chart",Ae="http://schemas.openxmlformats.org/officeDocument/2006/relationships",$=9525,ze=(e,t)=>`${ve(e.sheet.name)}$${ye(e.left)}$${String(e.top+1)}${e.single?"":`:$${ye(e.right)}$${String(t+1)}`}`;function L(e,t,r){if(!r?.node)return;const n=e.eval(r.node,{sheet:t,row:0,col:0,dr:0,dc:0}),s=f=>ze(f,Math.min(f.bottom,Math.max(f.top,f.sheet.lastRow)));if(n instanceof be)return s(n);if(n instanceof Me)return`(${n.parts.map(s).join(",")})`;const d=r.node;if(d.kind==="name"&&d.sheet!==void 0&&!(n instanceof Fe&&n.code==="#NAME?"))return`${ve(d.sheet)}${d.name}`}function Ge(e,t,r){if(!r?.node)return!1;const n=e.eval(r.node,{sheet:t,row:0,col:0,dr:0,dc:0});return n instanceof be&&typeof e.value(n.sheet,n.top,n.left)=="number"}export function chartPart(e,t,r){const n=De(e,t,r),s=r.series.map((c,l)=>({s:c,i:l,values:L(e,t,c.values)})).filter(c=>c.values!==void 0),d={...n,series:s.flatMap(c=>n.series[c.i]??[])},f=Ge(e,t,r.categories),m=f?Re(e,t,r):void 0,g={name:c=>L(e,t,s[c]?.s.name),categories:L(e,t,r.categories),numeric:f,...m===void 0?{}:{categoryNumbers:m.values,categoryFormat:m.format},values:c=>s[c]?.values??"",x:c=>L(e,t,s[c]?.s.x??r.categories)},x=L(e,t,r.title);return Se(d,{refs:g,...x===void 0?{}:{titleRef:x}})}const Ie=(e,t,r)=>a("xdr:oneCellAnchor",{},a("xdr:from",{},`<xdr:col>${String(e.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round(e.x*$))}</xdr:colOff>`,`<xdr:row>${String(e.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((e.y??0)*$))}</xdr:rowOff>`),a("xdr:ext",{cx:Math.round(e.width*$),cy:Math.round(e.height*$)}),a("xdr:pic",{},a("xdr:nvPicPr",{},a("xdr:cNvPr",{id:t,name:e.name,descr:e.path}),a("xdr:cNvPicPr",{},a("a:picLocks",{noChangeAspect:!0}))),a("xdr:blipFill",{},a("a:blip",{"xmlns:r":Ae,"r:embed":`rId${String(r)}`}),a("a:stretch",{},a("a:fillRect"))),a("xdr:spPr",{},a("a:xfrm",{},a("a:off",{x:0,y:0}),a("a:ext",{cx:Math.round(e.width*$),cy:Math.round(e.height*$)})),a("a:prstGeom",{prst:"rect"},a("a:avLst")))),a("xdr:clientData"));export function drawingPart(e,t=[]){const r=(n,s)=>{const{width:d,height:f}=Ne(n.spec);return a("xdr:oneCellAnchor",{},a("xdr:from",{},`<xdr:col>${String(n.at.col)}</xdr:col>`,`<xdr:colOff>${String(Math.round((n.spec.x??0)*$))}</xdr:colOff>`,`<xdr:row>${String(n.at.row)}</xdr:row>`,`<xdr:rowOff>${String(Math.round((n.spec.y??0)*$))}</xdr:rowOff>`),a("xdr:ext",{cx:Math.round(d*$),cy:Math.round(f*$)}),a("xdr:graphicFrame",{macro:""},a("xdr:nvGraphicFramePr",{},a("xdr:cNvPr",{id:s+2,name:`Chart ${String(s+1)}`}),a("xdr:cNvGraphicFramePr")),a("xdr:xfrm",{},a("a:off",{x:0,y:0}),a("a:ext",{cx:0,cy:0})),a("a:graphic",{},a("a:graphicData",{uri:Oe},a("c:chart",{"xmlns:c":Oe,"xmlns:r":Ae,"r:id":`rId${String(s+1)}`})))),a("xdr:clientData"))};return je+a("xdr:wsDr",{"xmlns:xdr":Ee,"xmlns:a":Le},...e.map(r),...t.map((n,s)=>Ie(n,e.length+s+2,e.length+s+1)))}function ne(e){if(Array.isArray(e))return e.map(ne).join("");if(typeof e!="object"||e===null)return"";const t=e,r=[];for(const[n,s]of Object.entries(t))n==="t"?r.push(S(s)):typeof s=="object"&&r.push(ne(s));return r.join("")}const re=e=>{const t=o(e?.solidFill),r=v(o(t?.srgbClr),"val");return r!==void 0?`#${r.toUpperCase()}`:Te[v(o(t?.schemeClr),"val")??""]},Te={accent1:"#4472C4",accent2:"#ED7D31",accent3:"#A5A5A5",accent4:"#FFC000",accent5:"#5B9BD5",accent6:"#70AD47"},C=(e,t)=>v(o(e?.[t]),"val"),F=(e,t)=>E(o(e?.[t]),"val"),z=(e,t)=>$e(o(e?.[t]),"val"),We=e=>/^"(?:[^"]|"")*"$/u.test(e)?e.slice(1,-1).replaceAll('""','"'):void 0,Ce=e=>We(e)??`=${e}`;function ke(e){if(!e)return;const t=o(e.tx),r=o(t?.strRef);if(r)return Ce(S(r.f));const n=ne(t?.rich);return n===""?void 0:n}function oe(e){if(!e)return;const t=o(e.scaling),r=o(e.numFmt),n=v(r,"formatCode"),s={},d=ke(o(e.title));d!==void 0&&(s.title=d);const f=F(t,"min"),m=F(t,"max"),g=F(e,"majorUnit");return f!==void 0&&(s.min=f),m!==void 0&&(s.max=m),g!==void 0&&g>0&&(s.major=g),z(e,"delete")===!0&&(s.visible=!1),C(t,"orientation")==="maxMin"&&(s.reverse=!0),n!==void 0&&n!=="General"&&$e(r,"sourceLinked")!==!0&&(s.format=n),s}const G=e=>{const t=o(e?.numRef)??o(e?.strRef),r=t?S(t.f):"";return r===""?void 0:r};function Ue(e,t){const r=G(o(e[t?"yVal":"val"]));if(r===void 0)return"its numbers are written in the chart";const n={values:r},s=o(e.tx),d=G(s);if(d!==void 0)n.name=Ce(d);else{const u=S(s?.v);u!==""&&(n.name=u)}const f=o(e.spPr),m=o(f?.ln),g=o(e.marker),x=t||g!==void 0,c=re(f)??(x?re(m)??re(o(g?.spPr)):void 0);c!==void 0&&(n.color=c);const l=E(m,"w");if(x&&l!==void 0&&m?.noFill===void 0){const u=Math.max(.25,Number((l/12700).toFixed(2)));u!==we.lineWidth&&(n.line_width=u)}z(e,"smooth")===!0&&(n.smooth=!0);const h=F(g,"size");h!==void 0&&h!==we.marker&&(n.marker_size=h);const w=t?G(o(e.xVal)):void 0;w!==void 0&&(n.x=w);const O=t?void 0:G(o(e.cat));return{series:n,...O===void 0?{}:{categories:O}}}function se(e,t){const r=A(t.ser),n=d=>o(o(d.spPr)?.ln)?.noFill!==void 0,s=d=>C(o(d.marker),"symbol")==="none";switch(e){case"barChart":return Be(t);case"lineChart":return r.every(s)?"line":"line-markers";case"areaChart":return Ve(t);case"pieChart":return"pie";case"doughnutChart":return"doughnut";case"scatterChart":return r.every(n)?"scatter":r.some(d=>z(d,"smooth")===!0)?"scatter-smooth":"scatter-lines";default:return}}function Be(e){const t=C(e,"barDir")==="bar"?"bar":"column",r=C(e,"grouping");return r==="stacked"?`${t}-stacked`:r==="percentStacked"?`${t}-stacked-100`:`${t}-clustered`}function Ve(e){const t=C(e,"grouping");return t==="stacked"?"area-stacked":t==="percentStacked"?"area-stacked-100":"area"}function I(e,t,r){if(e===void 0)return;const n=e.replace(/^=/u,"");if(/(?:^|[(,])'?\[\d+\]/u.test(n))return"it reads another workbook";if(e.includes("#REF!"))return"its cells were deleted (#REF!)";const s=/^(?:'((?:[^']|'')+)'|([^!'(),]+))!/u.exec(n),d=s?.[1]?.replaceAll("''","'")??s?.[2];if(d!==void 0&&!t(d))return`it reads a sheet the workbook does not have (${d})`;const f=_e(`=${n}`);if(!f.ok||!T(f.node,r))return`it names no cells (${n})`}function T(e,t){switch(e.kind){case"ref":case"structured":return!0;case"name":return e.sheet!==void 0||t(e.name);case"paren":return T(e.arg,t);case"binary":return(e.op===","||e.op===":")&&T(e.left,t)&&T(e.right,t);default:return!1}}export function readChart(e,t=[],r=()=>!0,n=()=>!0){const s=o(e?.chartSpace),d=o(s?.chart),f=o(d?.plotArea);if(!d||!f)return"it has no plot";const m=Object.entries(f).filter(([i])=>i.endsWith("Chart")).flatMap(([i,p])=>A(p).map(P=>({kind:i,plot:P}))),[g]=m;if(!g)return"it has no plot";const x=A(f.valAx),c=i=>A(i.axId).map(p=>v(p,"val")),l=i=>{const p=x.find(P=>c(i).includes(v(o(P.axId),"val")));return p!==void 0&&C(p,"axPos")==="r"},h=m.length>1,w=m.map(({kind:i,plot:p})=>({kind:i,plot:p,type:se(i,p),secondary:h&&l(p)}));if(h&&w.some(i=>i.type===void 0||!(i.type.startsWith("column")||i.type.startsWith("line")||i.type.startsWith("area"))))return"it combines several kinds of chart, not columns, lines and areas";const O=w.find(i=>!i.secondary)??w[0]??g,{kind:u,plot:b}=O,R=u==="scatterChart",M=[];let D,N;const U=w.flatMap(i=>A(i.plot.ser).map(p=>({s:p,of:i}))),B=U.filter(({s:i})=>i.trendline!==void 0).length;B>0&&t.push(B===1?"trendline":`trendlines of ${String(B)} series`);const V=U.filter(({s:i})=>i.errBars!==void 0).length;V>0&&t.push(V===1?"error bars":`error bars of ${String(V)} series`);for(const[i,{s:p,of:P}]of U.entries()){const y=Ue(p,R);if(typeof y=="string")return y;if(h){const te=P.type;te!==void 0&&te!==se(u,b)&&(y.series.type=te),P.secondary&&(y.series.secondary=!0)}const ee=I(y.series.values,r,n);if(ee!==void 0){t.push(`series ${String(i+1)}, as ${ee}`),N=ee;continue}y.series.name?.startsWith("=")===!0&&I(y.series.name,r,n)!==void 0&&delete y.series.name,I(y.series.x,r,n)!==void 0&&delete y.series.x,M.push(y.series),I(y.categories,r,n)===void 0&&(D??=y.categories)}if(M.length===0)return N===void 0?"it has no series":`every series ${N.replace(/^it /u,"")}`;const j=se(u,b);if(!j)return`a sheet has no ${u.replace(/Chart$/u,"")} chart`;const ie=ke(o(d.title)),ce=o(d.legend),H=C(ce,"legendPos"),ae=o(b.dLbls),k=i=>z(ae,i)===!0,Pe=k("showVal")||k("showPercent")||k("showCatName")||k("showSerName"),de=C(ae,"dLblPos"),X=F(b,"gapWidth"),fe=F(b,"overlap"),q=F(b,"holeSize"),ue=z(b,"varyColors"),le=j==="pie"||j==="doughnut",me=i=>c(b).includes(v(o(i.axId),"val")),he=[...A(f.catAx),...A(f.dateAx)],_={value:h?x.find(me)??x[0]:x.find((i,p,P)=>R?p===P.length-1:!0),category:R?x[0]:h?he.find(me)??he[0]:o(f.catAx)??o(f.dateAx),second:w.some(i=>i.secondary)?x.find(i=>C(i,"axPos")==="r"):void 0},J=oe(_.value),K=oe(_.category);J&&_.value?.majorGridlines===void 0&&(J.gridlines=!1),K&&_.category?.majorGridlines!==void 0&&(K.gridlines=!0);const Q=E(o(o(o(o(s?.txPr)?.p)?.pPr)?.defRPr),"sz"),Y=i=>i&&Object.keys(i).length>0?i:void 0,pe=Y(J),xe=Y(K),Z=oe(_.second);Z&&_.second?.majorGridlines!==void 0&&(Z.gridlines=!0);const ge=Y(Z);return{type:j,at:"A1",...ie===void 0?{}:{title:ie},...D===void 0?{}:{categories:D},series:le?M.slice(0,1):M,...ce?{legend:H==="b"?"bottom":H==="t"?"top":H==="l"?"left":"right"}:{},...Pe?{labels:{...k("showVal")?{value:!0}:{},...k("showPercent")?{percentage:!0}:{},...k("showCatName")?{category:!0}:{},...k("showSerName")?{series:!0}:{},...de===void 0?{}:{position:de}}}:{},...X===void 0||X===150?{}:{gap_width:X},...fe===void 0||j.includes("stacked")?{}:{overlap:fe},...q===void 0||q===50?{}:{hole_size:q},...ue===!0&&u==="barChart"?{vary_by_categories:!0}:{},...ue===!1&&le?{vary_by_categories:!1}:{},...pe?{value_axis:pe}:{},...xe?{category_axis:xe}:{},...ge?{secondary_axis:ge}:{},...Q===void 0||Q===900?{}:{size:Q/100}}}const W=e=>Math.round(e/$);export function chartAnchors(e,t,r){const n=o(e?.wsDr),s=[],d=[];let f=0;const m=c=>({col:Number(S(c?.col)),row:Number(S(c?.row)),colOff:W(Number(S(c?.colOff))),rowOff:W(Number(S(c?.rowOff)))}),g=c=>v(o(o(o(o(c.graphicFrame)?.graphic)?.graphicData)?.chart),"id"),x=(c,l,h,w)=>{const O={at:{row:l.row,col:l.col},...l.colOff>0?{x:l.colOff}:{},...l.rowOff>0?{y:l.rowOff}:{},width:h,height:w},u=o(c.pic),b=v(o(o(u?.blipFill)?.blip),"embed");if(u&&b!==void 0){const M=o(o(u.nvPicPr)?.cNvPr),D=v(M,"name"),N=v(M,"descr");d.push({id:b,...O,...D===void 0?{}:{name:D},...N===void 0?{}:{descr:N}});return}const R=g(c);if(R===void 0){f+=1;return}s.push({id:R,...O})};for(const c of A(n?.oneCellAnchor)){const l=o(c.ext);x(c,m(o(c.from)),W(E(l,"cx")??0),W(E(l,"cy")??0))}for(const c of A(n?.twoCellAnchor)){const l=m(o(c.from)),h=m(o(c.to));let w=h.colOff-l.colOff;for(let u=l.col;u<h.col;u++)w+=t(u);let O=h.rowOff-l.rowOff;for(let u=l.row;u<h.row;u++)O+=r(u);x(c,l,w,O)}return{anchors:s,pictures:d,others:f}}
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{SHEET_FILE as ie,SHEET_NOTE_AUTHOR as le,SHEET_PICTURE_HEIGHT as W,SHEET_SEPARATOR as fe}from"@allyhub/formats";import{cellName as $,columnLetter as M,contains as z,overlaps as R,rangeName as v,readAddress as ae,readArea as B}from"../address.js";import{rectangles as O}from"../api/rectangles.js";import{chartAfter as ce}from"../chart.js";import{analyse as ue}from"../calc/analyse.js";import{ITERATION_DEFAULTS as he}from"../calc/engine.js";import{FUNCTIONS as de}from"../calc/functions/index.js";import{checkWorkbook as me}from"../check.js";import{writeCsv as pe}from"@allyhub/formats";import{pictureSize as we}from"../files.js";import{isDateCode as N}from"../format.js";import{moveFormula as ge,relativeKey as ke}from"../formula/move.js";import{printFormula as S,sheetPrefix as U}from"../formula/print.js";import{mapNode as V,walk as I}from"../formula/walk.js";import{CellError as _,isoOfSerial as be,readTyped as H,writeTyped as j}from"../values.js";import{joinedRuns as ye,plainRuns as $e}from"../rich.js";import{runsOver as xe}from"./runs.js";import{FormulaCell as q,validName as J}from"../workbook.js";import{writeYaml as Le}from"../write.js";import{readFileFormula as ve}from"./formula.js";import{hasDate as K}from"./numfmt.js";import{placesLine as A,readXlsx as Se}from"./read.js";import{LISTS_SHEET as Ce}from"./write.js";const Y=100,Ee=.5,Z=1462,X=64,G=1e4;export const COMPARE_TOLERANCE=1e-9;const Q=f=>f.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/gu,"")||"rows",b=f=>{for(const e in f)if(Object.hasOwn(f,e))return!1;return!0},F=(f,e)=>`${String(f)} ${e}${f===1?"":"s"}`,C=f=>({row:f.row,col:f.col});function ee(f){return f===""||f.length>255||/^[/\\]|^[A-Za-z]:|\\|\0/u.test(f)?!1:f.split("/").every(e=>e!==""&&e!=="."&&e!=="..")}class Me{cells;starts;first;constructor(e){let o=!0;for(let t=1;t<e.length&&o;t++){const s=e[t-1],l=e[t];s&&l&&(l.row<s.row||l.row===s.row&&l.col<=s.col)&&(o=!1)}if(!o){e.sort((t,s)=>t.row-s.row||t.col-s.col);for(let t=e.length-1;t>0;t--){const s=e[t-1],l=e[t];l&&s?.row===l.row&&s.col===l.col&&e.splice(t-1,1)}}this.cells=e;const n=e[0]?.row??0,i=e[e.length-1]?.row??-1;this.first=n,this.starts=new Int32Array(Math.max(0,i-n+2));let r=0;for(let t=0;t<this.starts.length;t++){for(;r<e.length&&(e[r]?.row??0)<n+t;)r++;this.starts[t]=r}}get(e,o){const n=e-this.first;if(n<0||n>=this.starts.length-1)return;let i=this.starts[n]??0,r=(this.starts[n+1]??0)-1;for(;i<=r;){const t=i+r>>1,s=this.cells[t];if(!s)return;if(s.col===o)return s;s.col<o?i=t+1:r=t-1}}}class te{list=[];add(e){this.list.push(e)}has(e,o){for(const n of this.list)if(z(n,e,o))return!0;return!1}}function oe(f,e){const o=e.toLowerCase();return V(f,n=>n.kind!=="structured"||n.table?.toLowerCase()!==o?n:Object.fromEntries(Object.entries(n).filter(([i])=>i!=="table")))}function Oe(f){const e=new Map;for(const n of f.sheets)for(const i of n.tables)i.totals>0&&e.set(i.name.toLowerCase(),{t:i,sheet:n.name});if(e.size===0)return;const o=(n,i)=>{const r=i.table===void 0?void 0:e.get(i.table.toLowerCase());if(!r||i.thisRow)return;const{t}=r,s=i.sections,l=s.includes("#All");if(!l&&!s.includes("#Totals"))return;const a=y=>t.columns.findIndex(x=>x.name.toLowerCase()===y.toLowerCase()),[c,u]=i.columns,h=c===void 0?0:a(c),p=c===void 0?t.columns.length-1:u===void 0?h:a(u);if(h<0||p<0)return;const m=l?t.ref.top:s.includes("#Data")?t.ref.top+t.headers:s.includes("#Headers")?t.ref.top:t.ref.bottom-t.totals+1,w=t.ref.bottom,d=t.ref.left+Math.min(h,p),g=t.ref.left+Math.max(h,p),k=m!==w||d!==g;return{kind:"ref",...r.sheet===n?{}:{sheet:r.sheet},col:{index:d,abs:!0},row:{index:m,abs:!0},...k?{col2:{index:g,abs:!0},row2:{index:w,abs:!0}}:{},range:k}};for(const n of f.sheets)for(const i of n.cells)i.node&&(i.node=V(i.node,r=>r.kind==="structured"?o(n.name,r)??r:r.kind==="unary"&&r.op==="@"&&r.arg.kind==="ref"&&!r.arg.range?r.arg:r))}const _e=1e3;function ne(f){return f.headers===0?"has no header row":f.columns.length!==f.ref.right-f.ref.left+1||!J(f.name)?"cannot be held as a table":new Set(f.columns.map(e=>e.name.toLowerCase())).size===f.columns.length?void 0:"has two columns of one name"}function D(f,e,o){let n;const i=new Set;I(f,t=>{t.kind==="call"&&(t.name==="LET"||t.name==="LAMBDA")&&t.args.forEach(s=>{s.kind==="name"&&i.add(s.name.toLowerCase())})});const r=t=>i.has(t)||o===void 0||o.names.has(t)||o.tables.has(t);return I(f,t=>{n===void 0&&(t.kind==="call"&&!de.has(t.name)&&(r(t.written.toLowerCase())||(n=`no function ${t.name}`)),t.kind==="name"&&!r(t.name.toLowerCase())&&(n=`no name ${t.name}`),t.kind==="structured"&&t.table!==void 0&&!r(t.table.toLowerCase())&&(n=`no table ${t.table}`),(t.kind==="ref"||t.kind==="name"||t.kind==="error")&&t.sheet!==void 0&&(t.sheet.startsWith("[")?n="it reads another workbook":e.has(t.sheet.toLowerCase())||(n=`no sheet ${t.sheet}`)))}),n}function Ae(f){const e=new Set(f.book.sheets.map(o=>o.name.toLowerCase()));for(const o of f.book.sheets){const n=new Map,i=(t,s)=>{const l=n.get(s),a=$(t);l?l.push(a):n.set(s,[a])},r=[];for(const t of o.cells){if(t.unread!==void 0){i(t,/^\[|[!']\[|\[\d+\]/u.test(t.unread)?"it reads another workbook":`it could not be read: ${t.unread}`);continue}if(!t.node)continue;const s=D(t.node,e,f);s!==void 0?(delete t.node,i(t,s)):(f.carried.set(t,o),t.array&&v(t.array)!==$(t)&&r.push(t))}Te(f,o,r);for(const[t,s]of n)f.lost.push(`${o.name}: ${F(s.length,"formula")} kept as ${s.length===1?"its value":"their values"}, as ${t} (${A(s)})`)}}function Te(f,e,o){if(o.length===0)return;const n=new Set;for(const i of o){const{array:r}=i;if(r)for(const t of e.cells)t!==i&&!t.node&&z(r,t.row,t.col)&&n.add(t)}e.cells=e.cells.filter(i=>!n.has(i));for(const i of n)f.spilled.set(i,e)}function E(f,e){const{value:o}=e;if(!f.book.date1904||typeof o!="number")return o;const n=f.book.looks[e.style]?.format;return N(n)&&n!==void 0&&K(n)?o+Z:o}function P(f,e){if(typeof f!="number"||e===void 0||!N(e)||!K(e)||f<1)return;const o=be(f);return H(o).value===f?o:void 0}const ze=(f,e)=>P(f,e)??j(f??null);function se(f,e){const o=P(f,e);return o!==void 0?o:f==null?null:typeof f=="number"||typeof f=="boolean"?f:j(f)}function Re(f){const e=[];for(let s=0;s<f.length;s++){const l=f[s];if(!l)continue;const a=e[e.length-1];a?.row===l.row&&a.right===l.col-1?(a.right=l.col,a.last=s):e.push({row:l.row,left:l.col,right:l.col,first:s,last:s})}const o=e.map((s,l)=>l),n=s=>{let l=s;for(;o[l]!==l;){const a=o[o[l]??l]??l;o[l]=a,l=a}return l};let i=0,r=0;for(let s=0;s<e.length;s++){const l=e[s];if(l){s>0&&e[s-1]?.row!==l.row&&(i=e[s-1]?.row===l.row-1?r:s,r=s);for(let a=i;a<r;a++){const c=e[a];c?.row===l.row-1&&c.right>=l.left&&c.left<=l.right&&(o[n(s)]=n(a))}}}const t=new Map;return e.forEach((s,l)=>{const a=n(l),c=t.get(a),u=s.right-s.left+1;c?(c.box.top=Math.min(c.box.top,s.row),c.box.bottom=Math.max(c.box.bottom,s.row),c.box.left=Math.min(c.box.left,s.left),c.box.right=Math.max(c.box.right,s.right),c.count+=u,c.runs.push(l)):t.set(a,{box:{top:s.row,bottom:s.row,left:s.left,right:s.right},count:u,runs:[l]})}),[...t.values()].map(s=>({box:s.box,count:s.count,cells:()=>s.runs.flatMap(l=>{const a=e[l];return a?f.slice(a.first,a.last+1):[]})}))}const re=f=>JSON.stringify(f);class Ne{ctx;read;cells;inTables=new te;columnLooks=new Map;rowLooks=new Map;lookDone=new te;tables=[];areas=[];filtered=[];ownLooks=new Map;fileColumns=new Map;pictured=new Map;over=[];keys=new Map;constructor(e,o){this.ctx=e,this.read=o,this.cells=new Me(o.cells)}at(e,o){return this.cells.get(e,o)}look(e){return this.ctx.book.looks[e.style]??{}}runs(e){if(!e.runs||e.node||typeof e.value!="string")return;const o=ye(xe(e.runs,this.look(e),this.ctx.book.font));return o.length===0||$e(o)?void 0:o}code(e){return this.look(e).format}text(e){return e.node?S(e.node):ze(E(this.ctx,e),this.code(e))}baseLooks(){const{read:e}=this,{looks:o}=this.ctx.book;let n=-1;for(const r of e.cells)r.col>n&&(n=r.col);for(const r of e.columns){if(r.style===void 0)continue;const t=o[r.style];if(!(!t||b(t)))for(let s=r.min;s<=Math.min(r.max,n);s++)this.columnLooks.set(s,t)}for(const r of e.rows){const t=r.style===void 0?void 0:o[r.style];t&&!b(t)&&this.rowLooks.set(r.row,t)}const i=(r,t)=>{for(const s in t)if(!(s in r))return!1;return!0};for(const r of e.cells){const t=this.look(r),s=this.columnLooks.get(r.col);s&&!i(t,s)&&this.columnLooks.delete(r.col);const l=this.rowLooks.get(r.row);l&&!i(t,l)&&this.rowLooks.delete(r.row)}}ownLook(e){const o=this.columnLooks.get(e.col),n=this.rowLooks.get(e.row),i=this.look(e),r=i.format!==void 0&&N(i.format);if(!o&&!n&&!r){let a=this.ownLooks.get(e.style);return a||(a={look:i,key:re(i)},this.ownLooks.set(e.style,a)),a}const t={...o,...n},s={};for(const[a,c]of Object.entries(i))JSON.stringify(c)!==JSON.stringify(t[a])&&(s[a]=c);const l=s.format;if(typeof l=="string"){const a=e.node?void 0:P(E(this.ctx,e),l);a!==void 0&&H(a).format===l&&delete s.format}return{look:s,key:re(s)}}keyOf(e,o){if(e.key!==void 0)return e.key;const n=ke(o,e.row,e.col),i=this.keys.get(n);return i!==void 0?e.key=i:(this.keys.set(n,n),e.key=n)}columnFormula(e,o,n){const i=new Map,r=n-o+1;for(let s=o;s<=n;s++){const l=this.at(s,e);if(!l||l.value===void 0&&!l.node||l.value===null&&!l.node)return;if(!l.node)continue;const a=this.keyOf(l,l.node),c=i.get(a);c?c.n+=1:i.set(a,{n:1,cell:l})}let t;for(const s of i.values())(!t||s.n>t.n)&&(t=s);return t&&t.n*2>r?t.cell:void 0}csv(e,o){const n=[];for(let i=0;i<e;i+=G){const r=[];for(let t=i;t<Math.min(e,i+G);t++)r.push(o(t));n.push(pe(r))}return n.join("")}table(e){const{read:o,ctx:n}=this,i=d=>n.lost.push(`${o.name}: table ${e.name} ${d}`),r=e.ref.right-e.ref.left+1,t=ne(e);if(t!==void 0)return i(`${t}, so its cells are kept and it is no table`),!1;const s=e.ref.top,l=s+1;let a=e.ref.bottom-e.totals;if(e.totals===0){let d=s;for(const g of o.cells)g.col>=e.ref.left&&g.col<=e.ref.right&&g.row>d&&(d=g.row);a-d>_e&&(i(`runs ${String(a-d)} empty rows past what it holds, which are left out`),a=d)}e.totals>0&&i("has a totals row, kept as plain cells under it");const c=e.columns.map((d,g)=>a>=l?this.columnFormula(e.ref.left+g,l,a):void 0),u=d=>{const g=c[d];if(!g)return!1;for(let k=l;k<=a;k++)if(this.at(k,e.ref.left+d)?.key!==g.key)return!1;return!0};let h=r;for(;h>1&&u(h-1);)h-=1;const p={},m=this.path(e.name);n.files.set(m,this.csv(a-l+2,d=>{if(d===0)return e.columns.slice(0,h).map(y=>y.name);const g=l+d-1,k=[];for(let y=0;y<h;y++){const x=this.at(g,e.ref.left+y),L=c[y];x?L&&x.key===L.key?k.push(""):x.node?k.push(S(oe(x.node,e.name))):k.push(this.text(x)):k.push("")}return k})),this.inTables.add({...e.ref,bottom:a}),e.columns.forEach((d,g)=>{const k=e.ref.left+g,y=this.columnLook(k,l,a),x=c[g];x?.node&&(y.formula=S(oe(ge(x.node,l-x.row,0),e.name)));const L=this.fileColumns.get(k);L!==void 0&&l<=a&&(y.files=L===W?!0:{height:L}),b(y)||(p[d.name]=y)});const w=new Map;for(const[d,g]of e.filter?.columns??[]){const k=e.columns[d]?.name;k!==void 0&&w.set(k,g)}return(e.filter?.unread??0)>0&&i(`has ${F(e.filter?.unread??0,"filter")} by condition, its rows left as they were hidden`),this.tables.push({name:e.name,at:$({row:s,col:e.ref.left}),rows:m,...e.style!==void 0&&e.style!=="TableStyleMedium2"?{style:e.style}:{},...e.options,...b(p)?{}:{columns:p},...e.filter===void 0?{filter:!1}:w.size>0?{filter:{columns:Object.fromEntries(w)}}:{}}),e.filter?.unread===0&&e.filter.columns.size>0&&this.filtered.push({...e.ref,bottom:a}),this.areas.push({...e.ref,bottom:a}),!0}columnLook(e,o,n){let i,r={};for(let t=o;t<=n;t++){const s=this.at(t,e),l=s?this.ownLook(s):{look:{},key:"{}"};if(i===void 0)i=l.key,r=l.look;else if(i!==l.key)return{}}return b(r)?{}:(this.lookDone.add({top:o,bottom:n,left:e,right:e}),{...r})}path(e){const o=Q(e);let n=`${o}.csv`;for(let i=2;this.ctx.files.has(n)||this.ctx.taken(n);i++)n=`${o}-${String(i)}.csv`;return n}loose(){const e=[],o=[];for(const n of this.read.cells)this.inTables.has(n.row,n.col)||(n.node?e.push(n):n.value!==void 0&&n.value!==null&&o.push(n));return{formulas:e,typed:o}}blockAreas(e,o){const n=[],i=O(o.map(C)),r=s=>!n.some(l=>R(l,s))&&!this.areas.some(l=>R(l,s))&&!i.some(l=>R(l,s)),t=s=>(s.bottom-s.top+1)*(s.right-s.left+1);for(const s of Re(e)){const{box:l}=s;if(!(t(l)<Y)){if(s.count/t(l)>=Ee&&r(l)){n.push(l);continue}for(const a of O(s.cells().map(C)))t(a)>=Y&&r(a)&&n.push(a)}}return n.sort((s,l)=>s.top-l.top||s.left-l.left)}cells_(e){const{formulas:o,typed:n}=this.loose(),i=new Map;for(const c of n){const u=this.runs(c);u&&i.set(c,u)}const r=this.read.cells.filter(c=>this.inTables.has(c.row,c.col)&&this.runs(c)!==void 0);r.length>0&&this.ctx.lost.push(`${this.read.name}: ${F(r.length,"cell")} of rich text in a table, kept as plain text (${A(r.map(c=>$(c)))})`);const t=this.blockAreas(n.filter(c=>!i.has(c)),[...o,...i.keys()]),s=c=>t.some(u=>z(u,c.row,c.col));t.length>0&&(e.blocks=t.map(c=>{const u=this.path(this.read.name);return this.ctx.files.set(u,this.csv(c.bottom-c.top+1,h=>{const p=[];for(let m=c.left;m<=c.right;m++){const w=this.at(c.top+h,m);p.push(w?this.text(w):"")}return p})),{at:$({row:c.top,col:c.left}),rows:u}}));const l=[],a=new Map;for(const c of o){if(!c.node)continue;const u=this.keyOf(c,c.node),h=a.get(u);h?h.push(c):a.set(u,[c])}for(const c of a.values())for(const u of O(c.map(C))){const h=this.at(u.top,u.left);h?.node&&l.push([{row:u.top,col:u.left},v(u),S(h.node)])}for(const c of n){if(t.length>0&&s(c))continue;const u=i.get(c);l.push([C(c),$(c),u?{runs:u}:se(E(this.ctx,c),this.code(c))])}l.sort(([c],[u])=>c.row-u.row||c.col-u.col),l.length>0&&(e.cells=Object.fromEntries(l.map(([,c,u])=>[c,u])))}formats(e){const o=new Map,n=this.lookDone.list.length>0;for(const r of this.read.cells){if(n&&this.lookDone.has(r.row,r.col))continue;const t=this.ownLook(r);if(b(t.look))continue;const s=o.get(t.key);s?s.places.push(C(r)):o.set(t.key,{look:t.look,places:[C(r)]})}const i=[];for(const{look:r,places:t}of o.values())for(const s of O(t))i.push({range:v(s),...r});i.length>0&&(e.format=i)}columns(e){const{read:o}=this,n=new Map;let i=0;for(const t of o.cells)t.col>i&&(i=t.col);const r=[];for(const t of o.columns){if(t.width===void 0&&t.hidden!==!0&&t.level===void 0)continue;const s=t.max-Math.max(t.min,i+1)+1>X?Math.max(i,t.min+X-1):t.max;s<t.max&&r.push(`${M(s+1)}:${M(t.max)}`);const l=t.width!==void 0&&t.width<=0;for(let a=t.min;a<=s;a++)n.set(a,{...t.width===void 0||l?{}:{width:t.width},...t.hidden===!0||l?{hidden:!0}:{},...t.level===void 0?{}:{level:t.level}})}for(const[t,s]of this.columnLooks)n.set(t,{...n.get(t),...s});for(const[t,s]of this.fileColumns)this.tableColumn(t)||n.set(t,{...n.get(t),files:s===W?!0:{height:s}});n.size>0&&(e.columns=Object.fromEntries([...n.keys()].sort((t,s)=>t-s).map(t=>[M(t),n.get(t)??{}]))),r.length>0&&this.ctx.lost.push(`${o.name}: the size of columns ${A(r)}, past those that hold anything`)}tableColumn(e){return this.areas.some(o=>e>=o.left&&e<=o.right)}rows(e){const o={};for(const n of this.read.rows){const i=this.rowLooks.get(n.row),r=this.filtered.some(a=>n.row>a.top&&n.row<=a.bottom),t=n.height!==void 0&&this.pictured.get(n.row)!==n.height?n.height:void 0,s=t!==void 0&&t<=0,l={...t===void 0||s?{}:{height:t},...(n.hidden===!0||s)&&!r?{hidden:!0}:{},...n.level===void 0?{}:{level:n.level},...i};b(l)||(o[String(n.row+1)]=l)}b(o)||(e.rows=o)}assetPath(e,o){const n=t=>{const s=this.ctx.assets.get(t);return s?Ie(s,o.bytes):!this.ctx.taken(t)};if(e!==void 0&&ee(e)&&n(e))return e;const i=Q((o.name??"picture").replace(/\.[A-Za-z0-9]+$/u,""));let r=`${i}.${o.type}`;for(let t=2;!n(r);t++)r=`${i}-${String(t)}.${o.type}`;return r}pictures(){const e=this.over;for(const o of this.read.pictures){const n=this.at(o.row,o.col),i=typeof n?.value=="string"?n.value:"";if(o.y===void 0&&o.descr!==void 0&&ee(o.descr)&&i.split(fe).includes(o.descr)&&o.descr!==void 0){this.ctx.assets.set(o.descr,o.bytes),this.fileColumns.set(o.col,Math.max(this.fileColumns.get(o.col)??0,o.height)),this.pictured.set(o.row,Math.max(this.pictured.get(o.row)??0,o.height));continue}if(o.width<=0||o.height<=0){this.ctx.lost.push(`${this.read.name}: a picture of no size at ${$({row:o.row,col:o.col})}`);continue}const t=this.assetPath(o.descr,o);this.ctx.assets.set(t,o.bytes);const s=we(o.bytes),l=s?.width===o.width&&s.height===o.height;e.push({file:t,at:$({row:o.row,col:o.col}),...o.x===void 0?{}:{x:o.x},...o.y===void 0?{}:{y:o.y},...l?{}:{width:o.width,height:o.height}})}}build(){const{read:e}=this;this.baseLooks(),this.pictures();for(const n of e.tables)this.table(n);const o={name:e.name};return e.state!=="visible"&&(o.hidden=!0),e.gridlines===!1&&(o.gridlines=!1),e.tab!==void 0&&(o.tab=e.tab),e.state==="veryHidden"&&this.ctx.lost.push(`${e.name}: being very hidden; it is hidden`),e.freeze&&(e.freeze.row>0||e.freeze.col>0)&&(o.freeze=$(e.freeze)),this.tables.length>0&&(o.tables=this.tables),this.filter(o),this.columns(o),this.rows(o),this.cells_(o),e.merges.length>0&&(o.merges=e.merges.map(v)),this.formats(o),e.conditional.length>0&&(o.conditional=e.conditional),e.validations.length>0&&(o.validations=e.validations),e.charts.length>0&&(o.charts=e.charts.map(n=>this.chart(n))),this.over.length>0&&(o.pictures=this.over),b(e.links)||(o.links=e.links),e.notes.length>0&&(o.notes=Object.fromEntries(e.notes.map(n=>[n.ref,n.author===void 0||n.author===le?n.text:{text:n.text,author:n.author}]))),e.print&&(o.print=e.print),e.protect===!0&&(o.protect=!0),e.outline&&(o.outline=e.outline),o}chart(e){return ce(e,o=>{const n=this.chartReference(o.replace(/^=/u,""));return o.startsWith("=")?`=${n}`:n})}chartReference(e){const o=e.lastIndexOf("!"),i=(o<0?this.read.name:e.slice(0,o)).replace(/^'|'$/gu,"").replaceAll("''","'"),r=B(e.slice(o+1).replaceAll("$",""));if(!r)return e;const t=this.ctx.book.sheets.find(s=>s.name.toLowerCase()===i.toLowerCase());for(const s of t?.tables??[]){const l=s.ref.top+1,c=r.left===r.right&&r.top===l&&r.bottom===s.ref.bottom&&r.left>=s.ref.left&&r.left<=s.ref.right?s.columns[r.left-s.ref.left]:void 0;if(c&&this.ctx.tables.has(s.name.toLowerCase()))return`${s.name}[${c.name}]`}return`${t===this.read||o<0?"":`${e.slice(0,o)}!`}${v(r)}`}filter(e){const{filter:o}=this.read;if(!o)return;const n={};for(const[i,r]of o.columns)n[M(o.ref.left+i)]=r;e.filter={range:v(o.ref),...b(n)?{}:{columns:n}},o.unread===0&&!b(n)&&this.filtered.push(o.ref)}}const Ie=(f,e)=>f.length===e.length&&f.every((o,n)=>o===e[n]),He=new Set(["_xlnm._FilterDatabase","_xlnm.Print_Area","_xlnm.Print_Titles"]);function je(f){const e={},o=[],n=new Map,{book:i,lost:r}=f,t=new Set(i.sheets.map(u=>u.name.toLowerCase())),s=new Set,l=[];let a;const c=u=>{a=void 0;const h=Pe(u.text);if(h!==void 0)return typeof h=="string"?j(h):h;const p=ve(u.text,!0),m=p.ok?D(p.node,t):"it could not be read";if(!p.ok||m!==void 0||De(p.node)){r.push(`the name ${u.name} (${u.text})${m===void 0?"":`: ${m}`}`);return}return a=p.node,S(p.node)};for(const u of i.names){if(u.name.startsWith("_xlnm.")){He.has(u.name)||r.push(`Excel's name ${u.name}`);continue}if(/^_xl(?:fn|pm|ws|udf)/iu.test(u.name))continue;const h=u.name.toLowerCase(),p=u.sheet===void 0?"":String(u.sheet);if(!J(u.name)||f.tables.has(h)){r.push(`the name ${u.name}, which cannot be held here`);continue}if(s.has(`${p}|${h}`)){r.push(`a second name ${u.name}${p===""?"":` of ${i.sheets[u.sheet??0]?.name??""}`}`);continue}const m=c(u);if(m===void 0)continue;s.add(`${p}|${h}`),f.names.add(h);const w=u.sheet===void 0?void 0:n.get(u.sheet)??{names:{},hidden:[]},d=w?.names??e;d[u.name]=m,u.hidden===!0&&(w?.hidden??o).push(u.name),w&&u.sheet!==void 0&&n.set(u.sheet,w),a&&l.push({held:d,n:u,node:a})}for(const{held:u,n:h,node:p}of l){const m=D(p,t,{names:f.names,tables:f.tables});m!==void 0&&(u[h.name]="=#NAME?",r.push(`the name ${h.name} (${h.text}): ${m}; it holds #NAME?, as in Excel`))}return{names:e,hidden:o,sheets:n}}function Fe(f,e){const o={},n=new Map;for(const[i,r]of Object.entries(e))typeof r=="string"&&r.startsWith("=")&&n.set(r.slice(1).replaceAll("$","").toLowerCase(),i);for(const i of f.book.sheets)for(const r of i.scenarios){const t={};let s=!0;for(const{at:l,value:a}of r.cells){const c=ae(l),u=c&&i.cells.find(m=>m.row===c.row&&m.col===c.col);String(u?.value??"")!==a&&(s=!1);const h=n.get(`${U(i.name)}${l}`.toLowerCase())??n.get(l.toLowerCase()),p=H(a);h===void 0?f.lost.push(`${i.name}: scenario ${r.name}'s cell ${l}, which has no name`):t[h]=typeof p.value=="number"?p.value:a}!s&&!b(t)&&(o[r.name]={...o[r.name],...t})}return o}function De(f){let e=!1;return I(f,o=>{e||=o.kind==="error"&&o.code==="#REF!"}),e}function Pe(f){const e=f.trim();return/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/u.test(e)?Number(e):/^true$/iu.test(e)?!0:/^false$/iu.test(e)?!1:/^"((?:[^"]|"")*)"$/u.exec(e)?.[1]?.replaceAll('""','"')}const We=new RegExp(`^${Ce.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}(?: \\(\\d+\\))?$`,"u");function Be(f){const e=f.sheets.find(r=>r.state==="veryHidden"&&We.test(r.name));if(!e)return;const o=U(e.name).toLowerCase(),n=r=>{const t=r.replace(/^=/u,"");if(!t.toLowerCase().startsWith(o))return;const s=B(t.slice(o.length).replaceAll("$",""));if(!s||s.left!==s.right)return;const l=[];for(let a=s.top;a<=s.bottom;a++){const c=e.cells.find(u=>u.row===a&&u.col===s.left);l.push(typeof c?.value=="string"?c.value:"")}return l};for(const r of f.sheets)r.validations=r.validations.map(t=>{const[s]=t.formula??[],l=t.type==="list"&&typeof s=="string"?n(s):void 0;if(!l)return t;const a={...t,values:l};return delete a.formula,a});const i=f.sheets.indexOf(e);f.sheets=f.sheets.filter(r=>r!==e),f.names=f.names.flatMap(r=>r.sheet===void 0||r.sheet<i?[r]:r.sheet===i?[]:[{...r,sheet:r.sheet-1}])}const T=f=>f instanceof _?f.code:f??null,Ue=new Map([["#SPILL!","#VALUE!"],["#CALC!","#VALUE!"]]);export function sameValue(f,e,o){if(typeof f=="number"&&typeof e=="number")return f===e?!0:Math.abs(f-e)<=Math.max(Math.abs(f),Math.abs(e))*o;if(f instanceof _||e instanceof _){const n=i=>i instanceof _?Ue.get(i.code)??i.code:void 0;return n(f)===n(e)}if(f===null||e===null){const n=f??e;return n===null||n===""||n===0||n===!1}return f===e}function Ve(f,e){const o=[];for(const r of e.sheets)for(const t of r.formulas)t.shared.node&&ue(e,t.shared,r).volatile&&o.push(t);const n=new Set(o),i=[...o];for(;;){const r=i.pop();if(!r)return n;for(const t of f.readers(r.sheet,r.row,r.col))n.has(t)||(n.add(t),i.push(t))}}function qe(f){const e=f.file.iteration;return(e===void 0||e===!0?void 0:e.max_change)??he.max_change}function Je(f,e,o,n){const i={formulas:0,same:0,uncached:0,moving:0,differences:[]},r=Ve(o,e);for(const[t,s]of f.carried){if(!t.node)continue;const l=e.sheet(s.name);if(!l)continue;i.formulas+=1;const a=l.at(t.row,t.col);if(a instanceof q&&r.has(a)){i.moving+=1;continue}const c=E(f,t);if(c===void 0){i.uncached+=1;continue}const u=o.value(l,t.row,t.col);a instanceof q&&o.iterates(a)&&typeof c=="number"&&typeof u=="number"&&Math.abs(c-u)<=qe(e)||sameValue(c,u,n)?i.same+=1:i.differences.push({sheet:s.name,cell:$(t),formula:S(t.node),file:T(c),engine:T(u)})}for(const[t,s]of f.spilled){const l=e.sheet(s.name);if(!l)continue;i.formulas+=1;const a=l.spillAt(t.row,t.col);if(a&&r.has(a)){i.moving+=1;continue}const c=E(f,t),u=o.value(l,t.row,t.col);c===void 0?i.uncached+=1:sameValue(c,u,n)?i.same+=1:i.differences.push({sheet:s.name,cell:$(t),formula:a?`${$(a)}#`:"",file:T(c),engine:T(u)})}return i}function Ke(f,e){const o=new Map,n=[];for(const i of f){const r=e.find(l=>i.startsWith(`${l}: `));if(r===void 0){n.push(i);continue}const t=i.slice(r.length+2),s=o.get(t);s?s.push(r):o.set(t,[r])}for(const[i,r]of o)n.push(r.length===1?`${r[0]??""}: ${i}`:`${i}, on ${String(r.length)} sheets (${A(r)})`);return n}function Ye(f,e,o){const n=o.circularCells();if(n.length===0)return!1;const i=new Map;for(const[s,l]of f.carried)i.set(`${l.name}|${String(s.row)}|${String(s.col)}`,s);let r=0,t=0;for(const s of n){const l=i.get(`${s.sheet.name}|${String(s.row)}|${String(s.col)}`),a=l===void 0?void 0:E(f,l),c=e[s.sheet.index],u=s.shared.source.path??[];if(a===void 0||!c||u[2]!=="cells"){a!==void 0&&(t+=1);continue}const h=se(a,void 0),p=c.cells??={},m=$(s),w=p[m],d=p[String(u[3])],g=d!==null&&typeof d=="object"?d:{};p[m]=w!==null&&typeof w=="object"?{...w,start:h}:{...g,formula:s.text,start:h},r+=1}return t>0&&f.lost.push(`where ${String(t)} cells of a table or a block start iterative calculation: they start from 0`),r>0}export function importXlsx(f,e){const o=Se(f,e.zip);if(Be(o),Oe(o),o.sheets.length===0)throw new Error("It holds no sheets.");const n={book:o,taken:e.taken??(()=>!1),files:new Map,assets:new Map,lost:[...o.lost],tables:new Set(o.sheets.flatMap(m=>m.tables.filter(w=>ne(w)===void 0).map(w=>w.name.toLowerCase()))),names:new Set,carried:new Map,spilled:new Map};o.date1904&&n.lost.push(`the 1904 date system: dates are moved to the 1900 one, ${String(Z)} days on`),(o.font.name!==void 0&&o.font.name!=="Calibri"||o.font.size!==void 0&&o.font.size!==11)&&n.lost.push(`its default font, ${o.font.name??""} ${String(o.font.size??"")}`);const i=je(n);Ae(n);const r=o.sheets.map((m,w)=>{const d=new Ne(n,m).build(),g=i.sheets.get(w);return g&&!b(g.names)&&(d.names=g.names),g&&g.hidden.length>0&&(d.hidden_names=g.hidden),d}),t=Fe(n,i.names),s={title:o.title??e.name,...b(i.names)?{}:{names:i.names},...i.hidden.length>0?{hidden_names:i.hidden}:{},sheets:r,...b(t)?{}:{scenarios:t},...o.iteration===void 0?{}:{iteration:o.iteration}},l=()=>{const m=Le(s),w=new Map([[ie,m],...n.files]);return{files:w,...me({text:m,read:d=>w.get(d),exists:d=>w.has(d)||n.assets.has(d)})}};let a=l();a.calc&&Ye(n,r,a.calc)&&(a=l());const{files:c,workbook:u,calc:h,problems:p}=a;return{files:c,assets:n.assets,lost:Ke(n.lost,o.sheets.map(m=>m.name)),problems:p,compared:u&&h?Je(n,u,h,e.tolerance??COMPARE_TOLERANCE):{formulas:0,same:0,uncached:0,moving:0,differences:[]},sheets:o.sheets.length,tables:n.tables.size}}
2
+ import{SHEET_FILE as le,SHEET_NOTE_AUTHOR as fe,SHEET_PICTURE_HEIGHT as W,SHEET_SEPARATOR as ae}from"@allyhub/formats";import{cellName as $,columnLetter as M,contains as z,overlaps as R,rangeName as C,readAddress as ce,readArea as B}from"../address.js";import{rectangles as O}from"../api/rectangles.js";import{chartAfter as ue}from"../chart.js";import{analyse as he}from"../calc/analyse.js";import{ITERATION_DEFAULTS as de}from"../calc/engine.js";import{FUNCTIONS as me}from"../calc/functions/index.js";import{checkWorkbook as pe}from"../check.js";import{writeCsv as we}from"@allyhub/formats";import{pictureSize as ge}from"../files.js";import{isDateCode as N}from"../format.js";import{moveFormula as ke,relativeKey as be}from"../formula/move.js";import{printFormula as L,sheetPrefix as U}from"../formula/print.js";import{mapNode as V,walk as I}from"../formula/walk.js";import{CellError as _,isoOfSerial as ye,readTyped as j,writeTyped as H}from"../values.js";import{joinedRuns as $e,plainRuns as xe}from"../rich.js";import{runsOver as Le}from"./runs.js";import{FormulaCell as Z,validName as q}from"../workbook.js";import{writeYaml as ve}from"../write.js";import{readFileFormula as Ce}from"./formula.js";import{hasDate as J}from"./numfmt.js";import{placesLine as A,readXlsx as Se}from"./read.js";import{LISTS_SHEET as Ee}from"./write.js";const K=100,Me=.5,Y=1462,X=64,G=1e4;export const COMPARE_TOLERANCE=1e-9;const Q=f=>f.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/gu,"")||"rows",b=f=>{for(const e in f)if(Object.hasOwn(f,e))return!1;return!0},F=(f,e)=>`${String(f)} ${e}${f===1?"":"s"}`,S=f=>({row:f.row,col:f.col});function ee(f){return f===""||f.length>255||/^[/\\]|^[A-Za-z]:|\\|\0/u.test(f)?!1:f.split("/").every(e=>e!==""&&e!=="."&&e!=="..")}class Oe{cells;starts;first;constructor(e){let o=!0;for(let t=1;t<e.length&&o;t++){const s=e[t-1],l=e[t];s&&l&&(l.row<s.row||l.row===s.row&&l.col<=s.col)&&(o=!1)}if(!o){e.sort((t,s)=>t.row-s.row||t.col-s.col);for(let t=e.length-1;t>0;t--){const s=e[t-1],l=e[t];l&&s?.row===l.row&&s.col===l.col&&e.splice(t-1,1)}}this.cells=e;const n=e[0]?.row??0,i=e[e.length-1]?.row??-1;this.first=n,this.starts=new Int32Array(Math.max(0,i-n+2));let r=0;for(let t=0;t<this.starts.length;t++){for(;r<e.length&&(e[r]?.row??0)<n+t;)r++;this.starts[t]=r}}get(e,o){const n=e-this.first;if(n<0||n>=this.starts.length-1)return;let i=this.starts[n]??0,r=(this.starts[n+1]??0)-1;for(;i<=r;){const t=i+r>>1,s=this.cells[t];if(!s)return;if(s.col===o)return s;s.col<o?i=t+1:r=t-1}}}class te{list=[];add(e){this.list.push(e)}has(e,o){for(const n of this.list)if(z(n,e,o))return!0;return!1}}function oe(f){const e=/\.([A-Za-z0-9]+)$/u.exec(f)?.[1]?.toLowerCase();return e==="jpeg"?"jpg":e==="tif"?"tiff":e}function ne(f,e){const o=e.toLowerCase();return V(f,n=>n.kind!=="structured"||n.table?.toLowerCase()!==o||!n.thisRow&&n.columns.length===0&&n.sections.length===0?n:Object.fromEntries(Object.entries(n).filter(([i])=>i!=="table")))}function _e(f){const e=new Map;for(const n of f.sheets)for(const i of n.tables)i.totals>0&&e.set(i.name.toLowerCase(),{t:i,sheet:n.name});if(e.size===0)return;const o=(n,i)=>{const r=i.table===void 0?void 0:e.get(i.table.toLowerCase());if(!r||i.thisRow)return;const{t}=r,s=i.sections,l=s.includes("#All");if(!l&&!s.includes("#Totals"))return;const a=y=>t.columns.findIndex(x=>x.name.toLowerCase()===y.toLowerCase()),[c,u]=i.columns,h=c===void 0?0:a(c),p=c===void 0?t.columns.length-1:u===void 0?h:a(u);if(h<0||p<0)return;const m=l?t.ref.top:s.includes("#Data")?t.ref.top+t.headers:s.includes("#Headers")?t.ref.top:t.ref.bottom-t.totals+1,w=t.ref.bottom,d=t.ref.left+Math.min(h,p),g=t.ref.left+Math.max(h,p),k=m!==w||d!==g;return{kind:"ref",...r.sheet===n?{}:{sheet:r.sheet},col:{index:d,abs:!0},row:{index:m,abs:!0},...k?{col2:{index:g,abs:!0},row2:{index:w,abs:!0}}:{},range:k}};for(const n of f.sheets)for(const i of n.cells)i.node&&(i.node=V(i.node,r=>r.kind==="structured"?o(n.name,r)??r:r.kind==="unary"&&r.op==="@"&&r.arg.kind==="ref"&&!r.arg.range?r.arg:r))}const Ae=1e3;function se(f){return f.headers===0?"has no header row":f.columns.length!==f.ref.right-f.ref.left+1||!q(f.name)?"cannot be held as a table":new Set(f.columns.map(e=>e.name.toLowerCase())).size===f.columns.length?void 0:"has two columns of one name"}function D(f,e,o){let n;const i=new Set;I(f,t=>{t.kind==="call"&&(t.name==="LET"||t.name==="LAMBDA")&&t.args.forEach(s=>{s.kind==="name"&&i.add(s.name.toLowerCase())})});const r=t=>i.has(t)||o===void 0||o.names.has(t)||o.tables.has(t);return I(f,t=>{n===void 0&&(t.kind==="call"&&!me.has(t.name)&&(r(t.written.toLowerCase())||(n=`no function ${t.name}`)),t.kind==="name"&&!r(t.name.toLowerCase())&&(n=`no name ${t.name}`),t.kind==="structured"&&t.table!==void 0&&!r(t.table.toLowerCase())&&(n=`no table ${t.table}`),(t.kind==="ref"||t.kind==="name"||t.kind==="error")&&t.sheet!==void 0&&(t.sheet.startsWith("[")?n="it reads another workbook":e.has(t.sheet.toLowerCase())||(n=`no sheet ${t.sheet}`)))}),n}function Te(f){const e=new Set(f.book.sheets.map(o=>o.name.toLowerCase()));for(const o of f.book.sheets){const n=new Map,i=(t,s)=>{const l=n.get(s),a=$(t);l?l.push(a):n.set(s,[a])},r=[];for(const t of o.cells){if(t.unread!==void 0){i(t,/^\[|[!']\[|\[\d+\]/u.test(t.unread)?"it reads another workbook":`it could not be read: ${t.unread}`);continue}if(!t.node)continue;const s=D(t.node,e,f);s!==void 0?(delete t.node,i(t,s)):(f.carried.set(t,o),t.array&&C(t.array)!==$(t)&&r.push(t))}ze(f,o,r);for(const[t,s]of n)f.lost.push(`${o.name}: ${F(s.length,"formula")} kept as ${s.length===1?"its value":"their values"}, as ${t} (${A(s)})`)}}function ze(f,e,o){if(o.length===0)return;const n=new Set;for(const i of o){const{array:r}=i;if(r)for(const t of e.cells)t!==i&&!t.node&&z(r,t.row,t.col)&&n.add(t)}e.cells=e.cells.filter(i=>!n.has(i));for(const i of n)f.spilled.set(i,e)}function E(f,e){const{value:o}=e;if(!f.book.date1904||typeof o!="number")return o;const n=f.book.looks[e.style]?.format;return N(n)&&n!==void 0&&J(n)?o+Y:o}function P(f,e){if(typeof f!="number"||e===void 0||!N(e)||!J(e)||f<1)return;const o=ye(f);return j(o).value===f?o:void 0}const Re=(f,e)=>P(f,e)??H(f??null);function re(f,e){const o=P(f,e);return o!==void 0?o:f==null?null:typeof f=="number"||typeof f=="boolean"?f:H(f)}function Ne(f){const e=[];for(let s=0;s<f.length;s++){const l=f[s];if(!l)continue;const a=e[e.length-1];a?.row===l.row&&a.right===l.col-1?(a.right=l.col,a.last=s):e.push({row:l.row,left:l.col,right:l.col,first:s,last:s})}const o=e.map((s,l)=>l),n=s=>{let l=s;for(;o[l]!==l;){const a=o[o[l]??l]??l;o[l]=a,l=a}return l};let i=0,r=0;for(let s=0;s<e.length;s++){const l=e[s];if(l){s>0&&e[s-1]?.row!==l.row&&(i=e[s-1]?.row===l.row-1?r:s,r=s);for(let a=i;a<r;a++){const c=e[a];c?.row===l.row-1&&c.right>=l.left&&c.left<=l.right&&(o[n(s)]=n(a))}}}const t=new Map;return e.forEach((s,l)=>{const a=n(l),c=t.get(a),u=s.right-s.left+1;c?(c.box.top=Math.min(c.box.top,s.row),c.box.bottom=Math.max(c.box.bottom,s.row),c.box.left=Math.min(c.box.left,s.left),c.box.right=Math.max(c.box.right,s.right),c.count+=u,c.runs.push(l)):t.set(a,{box:{top:s.row,bottom:s.row,left:s.left,right:s.right},count:u,runs:[l]})}),[...t.values()].map(s=>({box:s.box,count:s.count,cells:()=>s.runs.flatMap(l=>{const a=e[l];return a?f.slice(a.first,a.last+1):[]})}))}const ie=f=>JSON.stringify(f);class Ie{ctx;read;cells;inTables=new te;columnLooks=new Map;rowLooks=new Map;lookDone=new te;tables=[];areas=[];filtered=[];ownLooks=new Map;fileColumns=new Map;pictured=new Map;over=[];keys=new Map;constructor(e,o){this.ctx=e,this.read=o,this.cells=new Oe(o.cells)}at(e,o){return this.cells.get(e,o)}look(e){return this.ctx.book.looks[e.style]??{}}runs(e){if(!e.runs||e.node||typeof e.value!="string")return;const o=$e(Le(e.runs,this.look(e),this.ctx.book.font));return o.length===0||xe(o)?void 0:o}code(e){return this.look(e).format}text(e){return e.node?L(e.node):Re(E(this.ctx,e),this.code(e))}baseLooks(){const{read:e}=this,{looks:o}=this.ctx.book;let n=-1;for(const r of e.cells)r.col>n&&(n=r.col);for(const r of e.columns){if(r.style===void 0)continue;const t=o[r.style];if(!(!t||b(t)))for(let s=r.min;s<=Math.min(r.max,n);s++)this.columnLooks.set(s,t)}for(const r of e.rows){const t=r.style===void 0?void 0:o[r.style];t&&!b(t)&&this.rowLooks.set(r.row,t)}const i=(r,t)=>{for(const s in t)if(!(s in r))return!1;return!0};for(const r of e.cells){const t=this.look(r),s=this.columnLooks.get(r.col);s&&!i(t,s)&&this.columnLooks.delete(r.col);const l=this.rowLooks.get(r.row);l&&!i(t,l)&&this.rowLooks.delete(r.row)}}ownLook(e){const o=this.columnLooks.get(e.col),n=this.rowLooks.get(e.row),i=this.look(e),r=i.format!==void 0&&N(i.format);if(!o&&!n&&!r){let a=this.ownLooks.get(e.style);return a||(a={look:i,key:ie(i)},this.ownLooks.set(e.style,a)),a}const t={...o,...n},s={};for(const[a,c]of Object.entries(i))JSON.stringify(c)!==JSON.stringify(t[a])&&(s[a]=c);const l=s.format;if(typeof l=="string"){const a=e.node?void 0:P(E(this.ctx,e),l);a!==void 0&&j(a).format===l&&delete s.format}return{look:s,key:ie(s)}}keyOf(e,o){if(e.key!==void 0)return e.key;const n=be(o,e.row,e.col),i=this.keys.get(n);return i!==void 0?e.key=i:(this.keys.set(n,n),e.key=n)}columnFormula(e,o,n){const i=new Map,r=n-o+1;for(let s=o;s<=n;s++){const l=this.at(s,e);if(!l||l.value===void 0&&!l.node||l.value===null&&!l.node)return;if(!l.node)continue;const a=this.keyOf(l,l.node),c=i.get(a);c?c.n+=1:i.set(a,{n:1,cell:l})}let t;for(const s of i.values())(!t||s.n>t.n)&&(t=s);return t&&t.n*2>r?t.cell:void 0}csv(e,o){const n=[];for(let i=0;i<e;i+=G){const r=[];for(let t=i;t<Math.min(e,i+G);t++)r.push(o(t));n.push(we(r))}return n.join("")}table(e){const{read:o,ctx:n}=this,i=d=>n.lost.push(`${o.name}: table ${e.name} ${d}`),r=e.ref.right-e.ref.left+1,t=se(e);if(t!==void 0)return i(`${t}, so its cells are kept and it is no table`),!1;const s=e.ref.top,l=s+1;let a=e.ref.bottom-e.totals;if(e.totals===0){let d=s;for(const g of o.cells)g.col>=e.ref.left&&g.col<=e.ref.right&&g.row>d&&(d=g.row);a-d>Ae&&(i(`runs ${String(a-d)} empty rows past what it holds, which are left out`),a=d)}e.totals>0&&i("has a totals row, kept as plain cells under it");const c=e.columns.map((d,g)=>a>=l?this.columnFormula(e.ref.left+g,l,a):void 0),u=d=>{const g=c[d];if(!g)return!1;for(let k=l;k<=a;k++)if(this.at(k,e.ref.left+d)?.key!==g.key)return!1;return!0};let h=r;for(;h>1&&u(h-1);)h-=1;const p={},m=this.path(e.name);n.files.set(m,this.csv(a-l+2,d=>{if(d===0)return e.columns.slice(0,h).map(y=>y.name);const g=l+d-1,k=[];for(let y=0;y<h;y++){const x=this.at(g,e.ref.left+y),v=c[y];x?v&&x.key===v.key?k.push(""):x.node?k.push(L(ne(x.node,e.name))):k.push(this.text(x)):k.push("")}return k})),this.inTables.add({...e.ref,bottom:a}),e.columns.forEach((d,g)=>{const k=e.ref.left+g,y=this.columnLook(k,l,a),x=c[g];x?.node&&(y.formula=L(ne(ke(x.node,l-x.row,0),e.name)));const v=this.fileColumns.get(k);v!==void 0&&l<=a&&(y.files=v===W?!0:{height:v}),b(y)||(p[d.name]=y)});const w=new Map;for(const[d,g]of e.filter?.columns??[]){const k=e.columns[d]?.name;k!==void 0&&w.set(k,g)}return(e.filter?.unread??0)>0&&i(`has ${F(e.filter?.unread??0,"filter")} by condition, its rows left as they were hidden`),this.tables.push({name:e.name,at:$({row:s,col:e.ref.left}),rows:m,...e.style!==void 0&&e.style!=="TableStyleMedium2"?{style:e.style}:{},...e.options,...b(p)?{}:{columns:p},...e.filter===void 0?{filter:!1}:w.size>0?{filter:{columns:Object.fromEntries(w)}}:{}}),e.filter?.unread===0&&e.filter.columns.size>0&&this.filtered.push({...e.ref,bottom:a}),this.areas.push({...e.ref,bottom:a}),!0}columnLook(e,o,n){let i,r={};for(let t=o;t<=n;t++){const s=this.at(t,e),l=s?this.ownLook(s):{look:{},key:"{}"};if(i===void 0)i=l.key,r=l.look;else if(i!==l.key)return{}}return b(r)?{}:(this.lookDone.add({top:o,bottom:n,left:e,right:e}),{...r})}path(e){const o=Q(e);let n=`${o}.csv`;for(let i=2;this.ctx.files.has(n)||this.ctx.taken(n);i++)n=`${o}-${String(i)}.csv`;return n}loose(){const e=[],o=[];for(const n of this.read.cells)this.inTables.has(n.row,n.col)||(n.node?e.push(n):n.value!==void 0&&n.value!==null&&o.push(n));return{formulas:e,typed:o}}blockAreas(e,o){const n=[],i=O(o.map(S)),r=s=>!n.some(l=>R(l,s))&&!this.areas.some(l=>R(l,s))&&!i.some(l=>R(l,s)),t=s=>(s.bottom-s.top+1)*(s.right-s.left+1);for(const s of Ne(e)){const{box:l}=s;if(!(t(l)<K)){if(s.count/t(l)>=Me&&r(l)){n.push(l);continue}for(const a of O(s.cells().map(S)))t(a)>=K&&r(a)&&n.push(a)}}return n.sort((s,l)=>s.top-l.top||s.left-l.left)}cells_(e){const{formulas:o,typed:n}=this.loose(),i=new Map;for(const c of n){const u=this.runs(c);u&&i.set(c,u)}const r=this.read.cells.filter(c=>this.inTables.has(c.row,c.col)&&this.runs(c)!==void 0);r.length>0&&this.ctx.lost.push(`${this.read.name}: ${F(r.length,"cell")} of rich text in a table, kept as plain text (${A(r.map(c=>$(c)))})`);const t=this.blockAreas(n.filter(c=>!i.has(c)),[...o,...i.keys()]),s=c=>t.some(u=>z(u,c.row,c.col));t.length>0&&(e.blocks=t.map(c=>{const u=this.path(this.read.name);return this.ctx.files.set(u,this.csv(c.bottom-c.top+1,h=>{const p=[];for(let m=c.left;m<=c.right;m++){const w=this.at(c.top+h,m);p.push(w?this.text(w):"")}return p})),{at:$({row:c.top,col:c.left}),rows:u}}));const l=[],a=new Map;for(const c of o){if(!c.node)continue;const u=this.keyOf(c,c.node),h=a.get(u);h?h.push(c):a.set(u,[c])}for(const c of a.values())for(const u of O(c.map(S))){const h=this.at(u.top,u.left);h?.node&&l.push([{row:u.top,col:u.left},C(u),L(h.node)])}for(const c of n){if(t.length>0&&s(c))continue;const u=i.get(c);l.push([S(c),$(c),u?{runs:u}:re(E(this.ctx,c),this.code(c))])}l.sort(([c],[u])=>c.row-u.row||c.col-u.col),l.length>0&&(e.cells=Object.fromEntries(l.map(([,c,u])=>[c,u])))}formats(e){const o=new Map,n=this.lookDone.list.length>0;for(const r of this.read.cells){if(n&&this.lookDone.has(r.row,r.col))continue;const t=this.ownLook(r);if(b(t.look))continue;const s=o.get(t.key);s?s.places.push(S(r)):o.set(t.key,{look:t.look,places:[S(r)]})}const i=[];for(const{look:r,places:t}of o.values())for(const s of O(t))i.push({range:C(s),...r});i.length>0&&(e.format=i)}columns(e){const{read:o}=this,n=new Map;let i=0;for(const t of o.cells)t.col>i&&(i=t.col);const r=[];for(const t of o.columns){if(t.width===void 0&&t.hidden!==!0&&t.level===void 0)continue;const s=t.max-Math.max(t.min,i+1)+1>X?Math.max(i,t.min+X-1):t.max;s<t.max&&r.push(`${M(s+1)}:${M(t.max)}`);const l=t.width!==void 0&&t.width<=0;for(let a=t.min;a<=s;a++)n.set(a,{...t.width===void 0||l?{}:{width:t.width},...t.hidden===!0||l?{hidden:!0}:{},...t.level===void 0?{}:{level:t.level}})}for(const[t,s]of this.columnLooks)n.set(t,{...n.get(t),...s});for(const[t,s]of this.fileColumns)this.tableColumn(t)||n.set(t,{...n.get(t),files:s===W?!0:{height:s}});n.size>0&&(e.columns=Object.fromEntries([...n.keys()].sort((t,s)=>t-s).map(t=>[M(t),n.get(t)??{}]))),r.length>0&&this.ctx.lost.push(`${o.name}: the size of columns ${A(r)}, past those that hold anything`)}tableColumn(e){return this.areas.some(o=>e>=o.left&&e<=o.right)}rows(e){const o={};for(const n of this.read.rows){const i=this.rowLooks.get(n.row),r=this.filtered.some(a=>n.row>a.top&&n.row<=a.bottom),t=n.height!==void 0&&this.pictured.get(n.row)!==n.height?n.height:void 0,s=t!==void 0&&t<=0,l={...t===void 0||s?{}:{height:t},...(n.hidden===!0||s)&&!r?{hidden:!0}:{},...n.level===void 0?{}:{level:n.level},...i};b(l)||(o[String(n.row+1)]=l)}b(o)||(e.rows=o)}assetPath(e,o){const n=s=>{const l=this.ctx.assets.get(s);return l?je(l,o.bytes):!this.ctx.taken(s)};if(e!==void 0&&oe(e)===oe(`.${o.type}`)&&ee(e)&&n(e))return e;const r=Q((o.name??"picture").replace(/\.[A-Za-z0-9]+$/u,""));let t=`${r}.${o.type}`;for(let s=2;!n(t);s++)t=`${r}-${String(s)}.${o.type}`;return t}pictures(){const e=this.over;for(const o of this.read.pictures){const n=this.at(o.row,o.col),i=typeof n?.value=="string"?n.value:"";if(o.y===void 0&&o.descr!==void 0&&ee(o.descr)&&i.split(ae).includes(o.descr)&&o.descr!==void 0){this.ctx.assets.set(o.descr,o.bytes),this.fileColumns.set(o.col,Math.max(this.fileColumns.get(o.col)??0,o.height)),this.pictured.set(o.row,Math.max(this.pictured.get(o.row)??0,o.height));continue}if(o.width<=0||o.height<=0){this.ctx.lost.push(`${this.read.name}: a picture of no size at ${$({row:o.row,col:o.col})}`);continue}const t=this.assetPath(o.descr,o);this.ctx.assets.set(t,o.bytes);const s=ge(o.bytes),l=s?.width===o.width&&s.height===o.height;e.push({file:t,at:$({row:o.row,col:o.col}),...o.x===void 0?{}:{x:o.x},...o.y===void 0?{}:{y:o.y},...l?{}:{width:o.width,height:o.height}})}}build(){const{read:e}=this;this.baseLooks(),this.pictures();for(const n of e.tables)this.table(n);const o={name:e.name};return e.state!=="visible"&&(o.hidden=!0),e.gridlines===!1&&(o.gridlines=!1),e.tab!==void 0&&(o.tab=e.tab),e.state==="veryHidden"&&this.ctx.lost.push(`${e.name}: being very hidden; it is hidden`),e.freeze&&(e.freeze.row>0||e.freeze.col>0)&&(o.freeze=$(e.freeze)),this.tables.length>0&&(o.tables=this.tables),this.filter(o),this.columns(o),this.rows(o),this.cells_(o),e.merges.length>0&&(o.merges=e.merges.map(C)),this.formats(o),e.conditional.length>0&&(o.conditional=e.conditional),e.validations.length>0&&(o.validations=e.validations),e.charts.length>0&&(o.charts=e.charts.map(n=>this.chart(n))),this.over.length>0&&(o.pictures=this.over),b(e.links)||(o.links=e.links),e.notes.length>0&&(o.notes=Object.fromEntries(e.notes.map(n=>[n.ref,n.author===void 0||n.author===fe?n.text:{text:n.text,author:n.author}]))),e.print&&(o.print=e.print),e.protect===!0&&(o.protect=!0),e.outline&&(o.outline=e.outline),o}chart(e){return ue(e,o=>{const n=this.chartReference(o.replace(/^=/u,""));return o.startsWith("=")?`=${n}`:n})}chartReference(e){const o=e.lastIndexOf("!"),i=(o<0?this.read.name:e.slice(0,o)).replace(/^'|'$/gu,"").replaceAll("''","'"),r=B(e.slice(o+1).replaceAll("$",""));if(!r)return e;const t=this.ctx.book.sheets.find(s=>s.name.toLowerCase()===i.toLowerCase());for(const s of t?.tables??[]){const l=s.ref.top+1,c=r.left===r.right&&r.top===l&&r.bottom===s.ref.bottom&&r.left>=s.ref.left&&r.left<=s.ref.right?s.columns[r.left-s.ref.left]:void 0;if(c&&this.ctx.tables.has(s.name.toLowerCase()))return L({kind:"structured",table:s.name,sections:[],columns:[c.name],thisRow:!1}).slice(1)}return`${t===this.read||o<0?"":`${e.slice(0,o)}!`}${C(r)}`}filter(e){const{filter:o}=this.read;if(!o)return;const n={};for(const[i,r]of o.columns)n[M(o.ref.left+i)]=r;e.filter={range:C(o.ref),...b(n)?{}:{columns:n}},o.unread===0&&!b(n)&&this.filtered.push(o.ref)}}const je=(f,e)=>f.length===e.length&&f.every((o,n)=>o===e[n]),He=new Set(["_xlnm._FilterDatabase","_xlnm.Print_Area","_xlnm.Print_Titles"]);function Fe(f){const e={},o=[],n=new Map,{book:i,lost:r}=f,t=new Set(i.sheets.map(u=>u.name.toLowerCase())),s=new Set,l=[];let a;const c=u=>{a=void 0;const h=We(u.text);if(h!==void 0)return typeof h=="string"?H(h):h;const p=Ce(u.text,!0),m=p.ok?D(p.node,t):"it could not be read";if(!p.ok||m!==void 0||Pe(p.node)){r.push(`the name ${u.name} (${u.text})${m===void 0?"":`: ${m}`}`);return}return a=p.node,L(p.node)};for(const u of i.names){if(u.name.startsWith("_xlnm.")){He.has(u.name)||r.push(`Excel's name ${u.name}`);continue}if(/^_xl(?:fn|pm|ws|udf)/iu.test(u.name))continue;const h=u.name.toLowerCase(),p=u.sheet===void 0?"":String(u.sheet);if(!q(u.name)||f.tables.has(h)){r.push(`the name ${u.name}, which cannot be held here`);continue}if(s.has(`${p}|${h}`)){r.push(`a second name ${u.name}${p===""?"":` of ${i.sheets[u.sheet??0]?.name??""}`}`);continue}const m=c(u);if(m===void 0)continue;s.add(`${p}|${h}`),f.names.add(h);const w=u.sheet===void 0?void 0:n.get(u.sheet)??{names:{},hidden:[]},d=w?.names??e;d[u.name]=m,u.hidden===!0&&(w?.hidden??o).push(u.name),w&&u.sheet!==void 0&&n.set(u.sheet,w),a&&l.push({held:d,n:u,node:a})}for(const{held:u,n:h,node:p}of l){const m=D(p,t,{names:f.names,tables:f.tables});m!==void 0&&(u[h.name]="=#NAME?",r.push(`the name ${h.name} (${h.text}): ${m}; it holds #NAME?, as in Excel`))}return{names:e,hidden:o,sheets:n}}function De(f,e){const o={},n=new Map;for(const[i,r]of Object.entries(e))typeof r=="string"&&r.startsWith("=")&&n.set(r.slice(1).replaceAll("$","").toLowerCase(),i);for(const i of f.book.sheets)for(const r of i.scenarios){const t={};let s=!0;for(const{at:l,value:a}of r.cells){const c=ce(l),u=c&&i.cells.find(m=>m.row===c.row&&m.col===c.col);String(u?.value??"")!==a&&(s=!1);const h=n.get(`${U(i.name)}${l}`.toLowerCase())??n.get(l.toLowerCase()),p=j(a);h===void 0?f.lost.push(`${i.name}: scenario ${r.name}'s cell ${l}, which has no name`):t[h]=typeof p.value=="number"?p.value:a}!s&&!b(t)&&(o[r.name]={...o[r.name],...t})}return o}function Pe(f){let e=!1;return I(f,o=>{e||=o.kind==="error"&&o.code==="#REF!"}),e}function We(f){const e=f.trim();return/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/u.test(e)?Number(e):/^true$/iu.test(e)?!0:/^false$/iu.test(e)?!1:/^"((?:[^"]|"")*)"$/u.exec(e)?.[1]?.replaceAll('""','"')}const Be=new RegExp(`^${Ee.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}(?: \\(\\d+\\))?$`,"u");function Ue(f){const e=f.sheets.find(r=>r.state==="veryHidden"&&Be.test(r.name));if(!e)return;const o=U(e.name).toLowerCase(),n=r=>{const t=r.replace(/^=/u,"");if(!t.toLowerCase().startsWith(o))return;const s=B(t.slice(o.length).replaceAll("$",""));if(!s||s.left!==s.right)return;const l=[];for(let a=s.top;a<=s.bottom;a++){const c=e.cells.find(u=>u.row===a&&u.col===s.left);l.push(typeof c?.value=="string"?c.value:"")}return l};for(const r of f.sheets)r.validations=r.validations.map(t=>{const[s]=t.formula??[],l=t.type==="list"&&typeof s=="string"?n(s):void 0;if(!l)return t;const a={...t,values:l};return delete a.formula,a});const i=f.sheets.indexOf(e);f.sheets=f.sheets.filter(r=>r!==e),f.names=f.names.flatMap(r=>r.sheet===void 0||r.sheet<i?[r]:r.sheet===i?[]:[{...r,sheet:r.sheet-1}])}const T=f=>f instanceof _?f.code:f??null,Ve=new Map([["#SPILL!","#VALUE!"],["#CALC!","#VALUE!"]]);export function sameValue(f,e,o){if(typeof f=="number"&&typeof e=="number")return f===e?!0:Math.abs(f-e)<=Math.max(Math.abs(f),Math.abs(e))*o;if(f instanceof _||e instanceof _){const n=i=>i instanceof _?Ve.get(i.code)??i.code:void 0;return n(f)===n(e)}if(f===null||e===null){const n=f??e;return n===null||n===""||n===0||n===!1}return f===e}function Ze(f,e){const o=[];for(const r of e.sheets)for(const t of r.formulas)t.shared.node&&he(e,t.shared,r).volatile&&o.push(t);const n=new Set(o),i=[...o];for(;;){const r=i.pop();if(!r)return n;for(const t of f.readers(r.sheet,r.row,r.col))n.has(t)||(n.add(t),i.push(t))}}function qe(f){const e=f.file.iteration;return(e===void 0||e===!0?void 0:e.max_change)??de.max_change}function Je(f,e,o,n){const i={formulas:0,same:0,uncached:0,moving:0,differences:[]},r=Ze(o,e);for(const[t,s]of f.carried){if(!t.node)continue;const l=e.sheet(s.name);if(!l)continue;i.formulas+=1;const a=l.at(t.row,t.col);if(a instanceof Z&&r.has(a)){i.moving+=1;continue}const c=E(f,t);if(c===void 0){i.uncached+=1;continue}const u=o.value(l,t.row,t.col);a instanceof Z&&o.iterates(a)&&typeof c=="number"&&typeof u=="number"&&Math.abs(c-u)<=qe(e)||sameValue(c,u,n)?i.same+=1:i.differences.push({sheet:s.name,cell:$(t),formula:L(t.node),file:T(c),engine:T(u)})}for(const[t,s]of f.spilled){const l=e.sheet(s.name);if(!l)continue;i.formulas+=1;const a=l.spillAt(t.row,t.col);if(a&&r.has(a)){i.moving+=1;continue}const c=E(f,t),u=o.value(l,t.row,t.col);c===void 0?i.uncached+=1:sameValue(c,u,n)?i.same+=1:i.differences.push({sheet:s.name,cell:$(t),formula:a?`${$(a)}#`:"",file:T(c),engine:T(u)})}return i}function Ke(f,e){const o=new Map,n=[];for(const i of f){const r=e.find(l=>i.startsWith(`${l}: `));if(r===void 0){n.push(i);continue}const t=i.slice(r.length+2),s=o.get(t);s?s.push(r):o.set(t,[r])}for(const[i,r]of o)n.push(r.length===1?`${r[0]??""}: ${i}`:`${i}, on ${String(r.length)} sheets (${A(r)})`);return n}function Ye(f,e,o){const n=o.circularCells();if(n.length===0)return!1;const i=new Map;for(const[s,l]of f.carried)i.set(`${l.name}|${String(s.row)}|${String(s.col)}`,s);let r=0,t=0;for(const s of n){const l=i.get(`${s.sheet.name}|${String(s.row)}|${String(s.col)}`),a=l===void 0?void 0:E(f,l),c=e[s.sheet.index],u=s.shared.source.path??[];if(a===void 0||!c||u[2]!=="cells"){a!==void 0&&(t+=1);continue}const h=re(a,void 0),p=c.cells??={},m=$(s),w=p[m],d=p[String(u[3])],g=d!==null&&typeof d=="object"?d:{};p[m]=w!==null&&typeof w=="object"?{...w,start:h}:{...g,formula:s.text,start:h},r+=1}return t>0&&f.lost.push(`where ${String(t)} cells of a table or a block start iterative calculation: they start from 0`),r>0}export function importXlsx(f,e){const o=Se(f,e.zip);if(Ue(o),_e(o),o.sheets.length===0)throw new Error("It holds no sheets.");const n={book:o,taken:e.taken??(()=>!1),files:new Map,assets:new Map,lost:[...o.lost],tables:new Set(o.sheets.flatMap(m=>m.tables.filter(w=>se(w)===void 0).map(w=>w.name.toLowerCase()))),names:new Set,carried:new Map,spilled:new Map};o.date1904&&n.lost.push(`the 1904 date system: dates are moved to the 1900 one, ${String(Y)} days on`),(o.font.name!==void 0&&o.font.name!=="Calibri"||o.font.size!==void 0&&o.font.size!==11)&&n.lost.push(`its default font, ${o.font.name??""} ${String(o.font.size??"")}`);const i=Fe(n);Te(n);const r=o.sheets.map((m,w)=>{const d=new Ie(n,m).build(),g=i.sheets.get(w);return g&&!b(g.names)&&(d.names=g.names),g&&g.hidden.length>0&&(d.hidden_names=g.hidden),d}),t=De(n,i.names),s={title:o.title??e.name,...b(i.names)?{}:{names:i.names},...i.hidden.length>0?{hidden_names:i.hidden}:{},sheets:r,...b(t)?{}:{scenarios:t},...o.iteration===void 0?{}:{iteration:o.iteration}},l=()=>{const m=ve(s),w=new Map([[le,m],...n.files]);return{files:w,...pe({text:m,read:d=>w.get(d),exists:d=>w.has(d)||n.assets.has(d)})}};let a=l();a.calc&&Ye(n,r,a.calc)&&(a=l());const{files:c,workbook:u,calc:h,problems:p}=a;return{files:c,assets:n.assets,lost:Ke(n.lost,o.sheets.map(m=>m.name)),problems:p,compared:u&&h?Je(n,u,h,e.tolerance??COMPARE_TOLERANCE):{formulas:0,same:0,uncached:0,moving:0,differences:[]},sheets:o.sheets.length,tables:n.tables.size}}