@8sheets/data-grid 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/a11y/cell-alt-text.js +1 -1
- package/api/serialize.js +1 -1
- package/api/style-refs.js +1 -1
- package/calc/adjust.js +1 -1
- package/calc/compile.js +1 -1
- package/calc/engine.js +1 -1
- package/calc/functions/array.js +1 -1
- package/calc/functions/database.js +1 -1
- package/calc/functions/date.js +1 -1
- package/calc/functions/engineering.js +1 -1
- package/calc/functions/financial.js +1 -1
- package/calc/functions/helpers.js +1 -1
- package/calc/functions/index.js +3 -2
- package/calc/functions/info.js +1 -1
- package/calc/functions/logical.js +1 -1
- package/calc/functions/lookup.js +1 -1
- package/calc/functions/math.js +1 -1
- package/calc/functions/stat.js +1 -1
- package/calc/functions/text.js +1 -1
- package/calc/parser.js +2 -2
- package/calc/shown-refs.js +1 -0
- package/calc/values.js +1 -1
- package/calcx/calc-control.js +1 -0
- package/calcx/external-read.js +1 -1
- package/calcx/external.js +1 -1
- package/calcx/lang/convert.js +1 -1
- package/cells/axis-ops.js +1 -1
- package/cells/book-css.js +7 -2
- package/cells/cell-styles-xml.js +1 -1
- package/cells/cf-engine.js +1 -1
- package/cells/cf-file.js +1 -1
- package/cells/docprops-xml.js +2 -2
- package/cells/filter-eval.js +1 -0
- package/cells/filter-file.js +1 -1
- package/cells/find-engine.js +2 -2
- package/cells/find.js +1 -1
- package/cells/goto.js +1 -1
- package/cells/link-model.js +1 -0
- package/cells/name-box.js +1 -1
- package/cells/note-box.js +3 -0
- package/cells/options.js +1 -1
- package/cells/outline-column.js +1 -1
- package/cells/protected-menu.js +1 -0
- package/cells/rich-data-xml.js +1 -1
- package/cells/selection-modes.js +1 -1
- package/cells/sheet-views-file.js +2 -0
- package/cells/shift-check.js +1 -1
- package/cells/sort-compare.js +1 -0
- package/cells/ui-kit.js +1 -1
- package/cells/undo-group.js +1 -1
- package/celltypes/dropdowns.js +2 -2
- package/core/address.js +1 -1
- package/core/autofill.js +1 -1
- package/core/axis.js +1 -1
- package/core/cond-format.js +1 -1
- package/core/culture.js +5 -1
- package/core/custom-views.js +1 -1
- package/core/data-tools.js +1 -0
- package/core/date-codes.js +1 -0
- package/core/default-names.js +1 -1
- package/core/edit-ranges.js +1 -0
- package/core/formula-refs.js +1 -1
- package/core/host-rules.js +1 -0
- package/core/input.js +1 -1
- package/core/numfmt.js +1 -1
- package/core/precision.js +1 -0
- package/core/protection.js +1 -1
- package/core/region-input.js +1 -1
- package/core/rule-refs.js +1 -0
- package/core/shared-view.js +1 -0
- package/core/sheet.js +1 -1
- package/core/sparklines.js +1 -1
- package/core/sparse.js +1 -1
- package/core/spills.js +1 -0
- package/core/strings.js +1 -1
- package/core/structure.js +1 -1
- package/core/table-columns.js +1 -0
- package/core/tables.js +2 -2
- package/core/validation-ranges.js +1 -1
- package/core/whatif.js +1 -1
- package/core/workbook.js +1 -1
- package/datasheets/formula.js +1 -1
- package/datasheets/manager.js +1 -1
- package/datasheets/path-binding.js +1 -1
- package/datasheets/persist-writer.js +1 -1
- package/datasheets/persist.js +1 -1
- package/datasheets/report.js +1 -1
- package/extras/getdata/persist.js +2 -2
- package/extras/getdata/rows.js +1 -1
- package/extras/getdata/shared-steps.js +1 -0
- package/extras/getdata/steps-formula.js +1 -0
- package/extras/getdata/steps-more.js +1 -0
- package/extras/getdata/steps-tables.js +1 -0
- package/extras/getdata/steps.js +1 -1
- package/i18n/de/a11y.js +1 -0
- package/i18n/de/cells.js +2 -0
- package/i18n/de/charts.js +1 -0
- package/i18n/de/common.js +1 -0
- package/i18n/de/datasheets.js +1 -0
- package/i18n/de/format.js +1 -0
- package/i18n/de/misc.js +1 -0
- package/i18n/de/pivot.js +1 -0
- package/i18n/de/proofing.js +1 -0
- package/i18n/de/ribbon.js +1 -0
- package/i18n/de/shapes.js +1 -0
- package/i18n/de/shell.js +3 -0
- package/i18n/de/touch.js +1 -0
- package/i18n/de.js +1 -0
- package/i18n/en/cells.js +2 -1
- package/i18n/en/charts.js +1 -1
- package/i18n/en/format.js +1 -1
- package/i18n/en/misc.js +1 -1
- package/i18n/en/pivot.js +1 -1
- package/i18n/en/proofing.js +1 -1
- package/i18n/en/ribbon.js +1 -1
- package/i18n/en/shapes.js +1 -1
- package/i18n/en/shell.js +3 -2
- package/i18n/en/touch.js +1 -1
- package/i18n/es/a11y.js +1 -0
- package/i18n/es/cells.js +2 -0
- package/i18n/es/charts.js +1 -0
- package/i18n/es/common.js +1 -0
- package/i18n/es/datasheets.js +1 -0
- package/i18n/es/format.js +1 -0
- package/i18n/es/misc.js +1 -0
- package/i18n/es/pivot.js +1 -0
- package/i18n/es/proofing.js +1 -0
- package/i18n/es/ribbon.js +1 -0
- package/i18n/es/shapes.js +1 -0
- package/i18n/es/shell.js +3 -0
- package/i18n/es/touch.js +1 -0
- package/i18n/es.js +1 -0
- package/i18n/fr/a11y.js +1 -0
- package/i18n/fr/cells.js +2 -0
- package/i18n/fr/charts.js +1 -0
- package/i18n/fr/common.js +1 -0
- package/i18n/fr/datasheets.js +1 -0
- package/i18n/fr/format.js +1 -0
- package/i18n/fr/misc.js +1 -0
- package/i18n/fr/pivot.js +1 -0
- package/i18n/fr/proofing.js +1 -0
- package/i18n/fr/ribbon.js +1 -0
- package/i18n/fr/shapes.js +1 -0
- package/i18n/fr/shell.js +3 -0
- package/i18n/fr/touch.js +1 -0
- package/i18n/fr.js +1 -0
- package/i18n/index.js +1 -1
- package/i18n/languages.js +1 -1
- package/i18n/pl/cells.js +2 -1
- package/i18n/pl/charts.js +1 -1
- package/i18n/pl/format.js +1 -1
- package/i18n/pl/misc.js +1 -1
- package/i18n/pl/pivot.js +1 -1
- package/i18n/pl/proofing.js +1 -1
- package/i18n/pl/ribbon.js +1 -1
- package/i18n/pl/shapes.js +1 -1
- package/i18n/pl/shell.js +3 -2
- package/i18n/pl/touch.js +1 -1
- package/io/blob-sink.js +1 -0
- package/io/canonical-xml.js +1 -0
- package/io/client.js +1 -1
- package/io/crypto/cfb-build.js +1 -0
- package/io/crypto/cfb-read.js +1 -1
- package/io/crypto/cfb-write.js +1 -1
- package/io/crypto/errors.js +1 -1
- package/io/csv.js +8 -8
- package/io/encoding.js +1 -0
- package/io/foreign-sheets.js +2 -2
- package/io/io.worker.js +1 -1
- package/io/open-package.js +1 -0
- package/io/package-paths.js +1 -1
- package/io/plan.js +1 -1
- package/io/text-split.js +3 -3
- package/io/xls/chart-sheet.js +1 -0
- package/io/xls/chart.js +1 -0
- package/io/xls/crypt.js +1 -0
- package/io/xls/dml-color.js +1 -0
- package/io/xls/drawing.js +1 -0
- package/io/xls/formula.js +1 -0
- package/io/xls/geometry.js +1 -0
- package/io/xls/index.js +1 -0
- package/io/xls/pictures.js +1 -0
- package/io/xls/pivot.js +1 -0
- package/io/xls/props.js +1 -0
- package/io/xls/records.js +1 -0
- package/io/xls/shapes.js +1 -0
- package/io/xls/sheet-ext.js +1 -0
- package/io/xls/sheet.js +1 -0
- package/io/xls/styles.js +1 -0
- package/io/xls/vba.js +1 -0
- package/io/xls/workbook.js +1 -0
- package/io/xlsb/filter.js +1 -0
- package/io/xlsb/formula.js +2 -0
- package/io/xlsb/functions.js +1 -0
- package/io/xlsb/index.js +1 -0
- package/io/xlsb/parts.js +1 -0
- package/io/xlsb/pivot.js +1 -0
- package/io/xlsb/records.js +1 -0
- package/io/xlsb/sheet-ext.js +1 -0
- package/io/xlsb/sheet.js +1 -0
- package/io/xlsb/slicer.js +1 -0
- package/io/xlsb/styles.js +1 -0
- package/io/xlsb/workbook.js +1 -0
- package/io/xlsb/xml.js +2 -0
- package/io/xlsx-read.js +1 -1
- package/io/xlsx-styles.js +2 -2
- package/io/xlsx-tail.js +1 -1
- package/io/xlsx-write.js +2 -2
- package/io/xml-escape.js +1 -1
- package/io/xml-scan.js +2 -2
- package/io/zip-reader.js +1 -1
- package/objects/chart-model.js +1 -1
- package/objects/drawing-xml.js +1 -1
- package/package.json +1 -1
- package/pivot/calc-items.js +1 -0
- package/pivot/engine.js +1 -1
- package/pivot/xml.js +2 -2
- package/print/note-pages.js +2 -0
- package/print/page-setup.js +1 -1
- package/print/paginate.js +1 -1
- package/print/pdf-crypt.js +1 -0
- package/print/pdf-links.js +1 -0
- package/print/pdf-tags.js +1 -0
- package/print/pdf.js +24 -13
- package/print/render-page.js +3 -3
- package/print/shown-notes.js +1 -0
- package/shapes/form-xml.js +1 -1
- package/slicers/custom-styles.js +1 -1
- package/slicers/format.js +1 -1
- package/slicers/structured.js +1 -1
- package/slicers/write.js +1 -1
- package/table-view/files.js +1 -1
- package/table-view/mount.js +1 -1
- package/table-view/version.js +1 -1
- package/ui/dialogs.js +3 -3
- package/ui/icons.js +1 -1
- package/ui/menu-base.js +1 -1
- package/ui/protection-ui.js +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A data grid for the web in plain JavaScript modules with no runtime dependencies. Open a CSV or Excel file, or pass your records, and search, filter, sort, group, total, pivot and export them as CSV, Excel or PDF. Files are read in a Worker as a stream, the page keeps a bounded DOM, and a million records stay responsive. Server data plugs in through a query contract, so search, filters and totals cover the whole result.
|
|
4
4
|
|
|
5
|
-
This is version 1.
|
|
5
|
+
This is version 1.2.0. Components for [React](https://www.npmjs.com/package/@8sheets/data-grid-react), [Vue](https://www.npmjs.com/package/@8sheets/data-grid-vue) and [Angular](https://www.npmjs.com/package/@8sheets/data-grid-angular) build on it. For a full Excel-compatible workbook, see the Spreadsheet, [`@8sheets/core`](https://www.npmjs.com/package/@8sheets/core).
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
package/a11y/cell-alt-text.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{registerSparseMap as u,setSparse as f}from"../core/sparse.js";import{registerSection as p}from"../cells/sections.js";const i="cellAltText",s=16384,a=2e3;u(i),p(i,{sheet:{save(r){const e=r[i];if(!(!e||e.size===0))return[...e].map(([t,n])=>[Math.floor(t/s),t%s,n])},load(r,e){if(!Array.isArray(e))return;const t=new Map;for(const n of e){if(!Array.isArray(n)||n.length!==3)continue;const[o,l,c]=n;!Number.isInteger(o)||!Number.isInteger(l)||o<0||l<0||l>=s||o>=1048576||typeof c!="string"||c===""||t.set(o*s+l,c.slice(0,a))}r[i]=t.size>0?t:null}}});function g(r,e,t){return r[i]?.get(e*s+t)??null}function x(r,e){return r.replace(/\{value\}/g,e)}function y(r,e,t){const n=typeof t=="string"&&t.trim()!==""?t.slice(0,a):void 0;r.transact("alt-text",o=>f(o.sheet,i,e,n))}export{g as cellAltText,y as setCellAltText,x as spokenAltText};
|
|
1
|
+
import{registerSparseMap as u,setSparse as f}from"../core/sparse.js";import{registerSection as p}from"../cells/sections.js";const i="cellAltText",s=16384,a=2e3;u(i),p(i,{sheet:{save(r){const e=r[i];if(!(!e||e.size===0))return[...e].map(([t,n])=>[Math.floor(t/s),t%s,n])},load(r,e){if(!Array.isArray(e))return;const t=new Map;for(const n of e){if(!Array.isArray(n)||n.length!==3)continue;const[o,l,c]=n;!Number.isInteger(o)||!Number.isInteger(l)||o<0||l<0||l>=s||o>=1048576||typeof c!="string"||c===""||t.set(o*s+l,c.slice(0,a))}r[i]=t.size>0?t:null}}});function g(r,e,t){return r[i]?.get(e*s+t)??null}function x(r,e){return r.replace(/\{value\}/g,()=>e)}function y(r,e,t){const n=typeof t=="string"&&t.trim()!==""?t.slice(0,a):void 0;r.transact("alt-text",o=>f(o.sheet,i,e,n))}export{g as cellAltText,y as setCellAltText,x as spokenAltText};
|
package/api/serialize.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{BOOL as E,CHUNK_BITS as Y,CHUNK_MASK as Z,CHUNK_SIZE as F,ERR as M,ERRORS as I,FORMULA as b,KIND_MASK as J,NUM as T,SPILL as v,STR as R,Sheet as G}from"../core/sheet.js";import{MAX_COLS as L}from"../core/address.js";import{defaultStyle as U}from"../core/styles.js";import{Workbook as k}from"../core/workbook.js";import{Engine as ee}from"../calc/engine.js";const j="8sheets",B=3,ne=new Set(["uid","name","columns","maxRow","maxCol","rows","cols","formulas","spills","rowStyles","colStyles","merges","mergeIndex","frozenRows","frozenCols","showGridlines","zoom","sourceId","sourceIndex","revision","hidden","tabColor","rtl"]),te=new Set(["apiObjects","cfEval","structureOps","dataBindings","report","isVirtual","split","onRefresh","onRejected","recorder"]),K={cf:"cfChanged",comments:"commentsChanged",hyperlinks:"hyperlinksChanged",validations:"validationsChanged",pageSetup:"pageSetupChanged",protection:"protectionChanged",scenarios:"scenariosChanged",sparklines:"sparklinesChanged",threads:"threadsChanged",autoFilter:"filterChanged",cellTypes:"cellTypesChanged",cellTypeRules:"cellTypesChanged"},_=new Map;function Ne(e,n){_.set(e,n)}function se(e,n){return _.has(e)?_.get(e)(n):n}const re=new G(""),oe=e=>{if(e===null||typeof e!="object")return!0;const n=Object.getPrototypeOf(e);return n===Object.prototype||n===null};function m(e,n,t){if(e===null||typeof e=="number"||typeof e=="string"||typeof e=="boolean")return e;if(e===void 0)return;if(typeof e=="function"){n?.push(`${t}: a function was left out`);return}if(Array.isArray(e))return e.map((o,s)=>m(o,n,`${t}[${s}]`)??null);if(e instanceof Map)return{$map:[...e].map(([o,s],i)=>[m(o,n,t),m(s,n,`${t}.${i}`)??null])};if(e instanceof Set)return{$set:[...e].map(o=>m(o,n,t))};if(ArrayBuffer.isView(e))return{$typed:e.constructor.name,v:[...e]};if(e instanceof Date)return{$date:e.toISOString()};if(!oe(e)){if(typeof e.toJSON=="function")return m(e.toJSON(),n,t);n?.push(`${t}: a ${e.constructor?.name??"class"} instance was left out`);return}if("$map"in e||"$set"in e)throw new Error(`${t}: a key starting with $ can't be stored`);const r={};for(const o of Object.keys(e)){if(o[0]==="_")continue;const s=m(e[o],n,`${t}.${o}`);s!==void 0&&(r[o]=s)}return r}const ie={Uint8Array,Int8Array,Uint16Array,Int16Array,Uint32Array,Int32Array,Float32Array,Float64Array};function g(e){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(g);if(e.$map)return new Map(e.$map.map(([t,r])=>[g(t),g(r)]));if(e.$set)return new Set(e.$set.map(g));if(e.$typed)return ie[e.$typed].from(e.v);if(e.$date)return new Date(e.$date);const n={};for(const t of Object.keys(e))n[t]=g(e[t]);return n}function V(e){const n={size:e.defaultSize};if(e.sizes!==null){const t=[],{sizes:r}=e;for(let o=0;o<r.length;o++){if(!e.isCustom(o)||e.isHidden(o))continue;const s=r[o],i=e.isAuto(o);let f=o;for(;f+1<r.length&&r[f+1]===s&&e.isCustom(f+1)&&!e.isHidden(f+1)&&e.isAuto(f+1)===i;)f++;t.push(i?[o,f-o+1,s,1]:[o,f-o+1,s]),o=f}t.length&&(n.runs=t)}return e.hiddenSizes!==null&&e.hiddenSizes.size>0&&(n.hidden=[...e.hiddenSizes]),n}function P(e,n){if(n){n.size!==void 0&&n.size!==e.defaultSize&&e.setDefaultSize(n.size);for(const[t,r,o,s]of n.runs??[])for(let i=t;i<t+r;i++)e.setSize(i,o,s===1);for(const[t,r]of n.hidden??[])r>=0&&e.setSize(t,r),e.setHidden(t,!0)}}function le(e){const n=[];let t;for(const r of[...e.keys()].sort((o,s)=>o-s)){const o=e.get(r);t&&r===t[0]+t[1]&&o===t[2]?t[1]++:(t=[r,1,o],n.push(t))}return n}function fe(e){const n=new Map;for(const t of e){if(t.length===2){const[i,f]=t;n.set(i,f);continue}const[r,o,s]=t;if(t.length!==3||!Number.isInteger(r)||!Number.isInteger(o)||r<0||o<1||r+o>L)throw new RangeError("Column style run exceeds the sheet or has invalid bounds");for(let i=r;i<r+o;i++)n.set(i,s)}return n}const ce=16;function ue(e){const n={};for(const t of Object.keys(e))t!=="tpl"&&t[0]!=="_"&&(n[t]=e[t]);return n}function q(e,n,t){switch(n&J){case T:return t;case R:return e.strings.get(t);case E:return t!==0;case M:return{e:I[t]??"#N/A"};default:return null}}function ae(e,n,t,r,o){const s=[],i=[];let f=null,c=-2,d=-1,l=0,u=-1;const{formulas:y}=n,{rich:a}=e.strings,S=o.styles===!1,C=o.formulas===!1;for(let p=0;p<r.length;p++){const D=r[p];if(D===void 0)continue;const W=p*F,{kinds:X,values:x,styles:$}=D;for(let h=0;h<F;h++){const O=X[h],w=W+h;if($!==null&&!S&&$[h]!==0&&($[h]===l&&w===u+1?u=w:(l!==0&&i.push([d,u-d+1,l]),d=w,u=w,l=$[h])),O===0||O&v)continue;let z;if(O&b){const N=y.get(w*L+t),A=q(e,O,x[h]);N&&!C?(z={f:ue(N)},A!==null&&(z.v=A)):z=A}else(O&J)===R?(z=e.strings.get(x[h]),a!==null&&a.has(x[h])&&(z={s:z,rich:a.get(x[h])})):z=q(e,O,x[h]);const H=w-c-1;if(f===null||H>ce)f={r:w,v:[]},s.push(f);else for(let N=0;N<H;N++)f.v.push(null);f.v.push(z),c=w}}return l!==0&&i.push([d,u-d+1,l]),{blocks:s,styles:i}}function de(e,n,t,r){for(const{r:o,v:s}of r){let i=null,f=-1;for(let d=0;d<s.length;d++){const l=s[d];if(l===null)continue;const u=o+d,y=u>>>Y;y!==f&&(i=n._ensure(u,t),f=y);const a=u&Z;if(typeof l=="number")i.kinds[a]=T,i.values[a]=l;else if(typeof l=="string")i.kinds[a]=R,i.values[a]=e.strings.add(l);else if(typeof l=="boolean")i.kinds[a]=E,i.values[a]=l?1:0;else if(l.e!==void 0)i.kinds[a]=M,i.values[a]=Math.max(0,I.indexOf(l.e));else if(l.s!==void 0){const S=e.strings.add(l.s);i.kinds[a]=R,i.values[a]=S,e.strings.rich??=new Map,e.strings.rich.set(S,l.rich)}else if(l.f!==void 0){let S=b,C=0;const p=l.v;typeof p=="number"?(S|=T,C=p):typeof p=="string"?(S|=R,C=e.strings.add(p)):typeof p=="boolean"?(S|=E,C=p?1:0):p&&p.e!==void 0&&(S|=M,C=Math.max(0,I.indexOf(p.e))),i.kinds[a]=S,i.values[a]=C,n.setFormula(u,t,typeof l.f=="string"?{f:l.f,typed:!0}:l.f)}}const c=o+s.length-1;c>n.maxRow&&(n.maxRow=c)}t>n.maxCol&&(n.maxCol=t)}function me(e,n,t){for(const[r,o,s]of t)for(let i=r;i<r+o;i++)e.setStyle(i,n,s)}function Q(e,n){const t={};for(const r of Object.keys(e)){if(r[0]==="_")continue;const o=e[r];o!==n[r]&&!(typeof o=="object"&&o!==null&&JSON.stringify(o)===JSON.stringify(n[r]))&&(t[r]=o)}return t}function pe(e,n,t,r,o){const s={name:n.name};n.hidden&&(s.hidden=!0),n.hidden&&n.veryHidden&&(s.veryHidden=!0),n.tabColor&&(s.tabColor=n.tabColor),n.rtl&&(s.rtl=!0),n.zoom!==1&&(s.zoom=n.zoom),n.frozenRows&&(s.frozenRows=n.frozenRows),n.frozenCols&&(s.frozenCols=n.frozenCols),n.showGridlines===!1&&(s.showGridlines=!1),s.rows=V(n.rows),s.cols=V(n.cols),n.rowStyles?.size&&(s.rowStyles=[...n.rowStyles]),n.colStyles?.size&&(s.colStyles=le(n.colStyles)),n.merges.length&&(s.merges=n.merges.map(({r0:l,c0:u,r1:y,c1:a})=>[l,u,y,a]));const i=[],f=[];n.columns.forEach((l,u)=>{if(r.cells===!1)return;const{blocks:y,styles:a}=ae(e,n,u,l,r);y.length&&i.push({c:u,blocks:y}),a.length&&f.push({c:u,runs:a})}),i.length&&(s.cells=i),f.length&&(s.styles=f);const c={};for(const l of Object.keys(n)){if(ne.has(l)||te.has(l)||l[0]==="_"||l.endsWith("Changed"))continue;const u=n[l];if(u==null||u===re[l])continue;const y=m(u,o,`${n.name}.${l}`);y!==void 0&&(c[l]=y)}Object.keys(c).length&&(s.features=c);const d=t?.objectsOf?.(n);if(d?.length){const l=m(d.map(ye),o,`${n.name}.objects`);l.length&&(s.objects=l)}return s}function ye(e){const{bitmap:n,blob:t,canvas:r,modelEpoch:o,...s}=e;return t&&(s.imageType=t.type),s}function Re(e,n=null,t={}){const r=t.warnings??[],o=U(),s={format:j,version:1};s.activeSheet=e.activeSheet,e.fileName&&(s.fileName=e.fileName),e.themeInfo?s.theme=m(e.themeInfo,r,"theme"):e.theme&&(s.themeColors=e.theme);const i=e.styles.list;s.styles=i.length===1&&Object.keys(Q(i[0],o)).length===0?[]:i.map(c=>Q(c,o)),e.dxfs?.length&&(s.dxfs=m(e.dxfs,r,"dxfs")),e.persons?.length&&(s.persons=m(e.persons,r,"persons")),e.protection&&(s.protection=m(e.protection,r,"protection")),e.calcMode&&(s.calcMode=e.calcMode),e.getDataQueries&&(s.queries=m(e.getDataQueries,r,"queries")),e.customData&&(s.custom=m(e.customData,r,"custom"));const f=e.engine;if(f&&(f.manual&&(s.manualCalc=!0),f.names.size&&(s.names=[...f.names].map(([c,d])=>{const l=c.indexOf("!");return{name:d.name??(l>=0?c.slice(l+1):c),text:d.text,...l>=0?{sheet:Number(c.slice(0,l))}:{}}})),f.tables.size&&(s.tables=[...f.tables.values()].map(c=>{const{sheet:d,source:l,part:u,id:y,isNew:a,dirty:S,...C}=c;return{...m(C,r,`table ${c.name}`),sheet:e.sheets.indexOf(d)}}))),s.sheets=e.sheets.map(c=>pe(e,c,n,t,r)),s.sheets.some(c=>c.colStyles)&&(s.version=2),s.sheets.some(c=>c.features?.cellTypeRules)&&(s.version=3),n?.extrasToJSON){const c=n.extrasToJSON(r);c&&(s.extras=c)}return r.length&&(s.warnings=r),s}function he(e,n){n.hidden&&(e.hidden=!0),n.hidden&&n.veryHidden&&(e.veryHidden=!0),n.tabColor&&(e.tabColor=n.tabColor,e.tabColorChanged=!0),n.rtl&&(e.rtl=!0),n.zoom&&(e.zoom=n.zoom),e.frozenRows=n.frozenRows??0,e.frozenCols=n.frozenCols??0,n.showGridlines===!1&&(e.showGridlines=!1),P(e.rows,n.rows),P(e.cols,n.cols),(n.cols?.runs||n.cols?.hidden)&&(e.colsChanged=!0),n.rowStyles&&(e.rowStyles=new Map(n.rowStyles)),n.colStyles&&(e.colStyles=fe(n.colStyles)),n.merges&&(e.merges=n.merges.map(([t,r,o,s])=>({r0:t,c0:r,r1:o,c1:s}))),e.viewChanged=!0;for(const[t,r]of Object.entries(n.features??{})){const o=g(r);e[t]=se(t,o),K[t]&&(e[K[t]]=!0)}}function ge(e,n){const t=U();n.styles?.length&&e.styles.load(n.styles.map(r=>({...t,...r}))),n.dxfs&&(e.dxfs=g(n.dxfs),e.sourceDxfs=0),n.persons&&(e.persons=g(n.persons),e.personsChanged=!0),n.protection&&(e.protection=g(n.protection),e.protectionChanged=!0),n.calcMode&&(e.calcMode=n.calcMode,e.calcModeChanged=!0),n.queries&&(e.getDataQueries=g(n.queries)),n.custom&&(e.customData=g(n.custom)),n.fileName&&(e.fileName=n.fileName),n.themeColors&&(e.theme=n.themeColors)}function Se(e,n,t={}){const{engine:r}=e;e.formulasEdited=!0,e.activeSheet=Math.min(n.activeSheet??0,e.sheets.length-1),r.sheetsChanged(),n.manualCalc&&(r.manual=!0),r.setNames((n.names??[]).map(o=>({name:o.name,text:o.text,localSheetId:o.sheet}))),n.names?.length&&(e.namesChanged=!0),r.setTables((n.tables??[]).map(o=>{const{sheet:s,...i}=o;return{...g(i),sheet:e.sheets[s],isNew:!0,dirty:!0}})),r.buildGraph({fullCalc:t.recalculate===!0}),e.dirty=!1}function $e(e,n={}){if(e?.format!==j)throw new Error(`This isn't ${j} JSON`);if(e.version>B)throw new Error(`This JSON has version ${e.version}; this code reads up to ${B}`);const t=new k;return t.engine=new ee(t),ge(t,n.styles===!1?{...e,styles:[]}:e),t.sheets=e.sheets.map(r=>new G(r.name)),e.sheets.forEach((r,o)=>{const s=t.sheets[o];he(s,r);for(const i of r.cells??[])de(t,s,i.c,i.blocks);if(n.styles!==!1)for(const i of r.styles??[])me(s,i.c,i.runs);if(r.styles?.length)for(const i of r.styles)i.c>s.maxCol&&(s.maxCol=i.c)}),Se(t,e,n),t}export{j as FORMAT_NAME,B as FORMAT_VERSION,ge as applyBookMeta,he as applySheetMeta,$e as bookFromJSON,Re as bookToJSON,g as decodeData,m as encodeData,Se as finishLoad,Ne as registerFeatureReviver,se as reviveSheetFeature};
|
|
1
|
+
import{withShared as Z}from"../core/shared-view.js";import{BOOL as A,CHUNK_BITS as k,CHUNK_MASK as ee,CHUNK_SIZE as L,ERR as $,ERRORS as M,FORMULA as F,KIND_MASK as J,NUM as I,SPILL as ne,STR as T,Sheet as v}from"../core/sheet.js";import{MAX_COLS as U}from"../core/address.js";import{defaultStyle as G}from"../core/styles.js";import{Workbook as te}from"../core/workbook.js";import{Engine as se}from"../calc/engine.js";import{applyCalcSettings as re}from"../calcx/calc-control.js";const j="8sheets",V=4,ie=new Set(["uid","name","columns","maxRow","maxCol","rows","cols","formulas","spills","rowStyles","colStyles","merges","mergeIndex","frozenRows","frozenCols","frozenTop","frozenLeft","showGridlines","zoom","sourceId","sourceIndex","revision","hidden","tabColor","rtl"]),b=new Set(["apiObjects","cfEval","structureOps","dataBindings","report","isVirtual","split","onRefresh","onRejected","recorder","hostRules","valueListeners"]),B={cf:"cfChanged",comments:"commentsChanged",hyperlinks:"hyperlinksChanged",validations:"validationsChanged",pageSetup:"pageSetupChanged",protection:"protectionChanged",protectedRanges:"protectedRangesChanged",scenarios:"scenariosChanged",sparklines:"sparklinesChanged",threads:"threadsChanged",autoFilter:"filterChanged",namedViews:"namedViewsChanged",cellTypes:"cellTypesChanged",cellTypeRules:"cellTypesChanged"},_=new Map;function Ie(e,n){_.set(e,n)}function oe(e,n){return _.has(e)?_.get(e)(n):n}const fe=new v(""),le=e=>{if(e===null||typeof e!="object")return!0;const n=Object.getPrototypeOf(e);return n===Object.prototype||n===null};function m(e,n,t){if(e===null||typeof e=="number"||typeof e=="string"||typeof e=="boolean")return e;if(e===void 0)return;if(typeof e=="function"){n?.push(`${t}: a function was left out`);return}if(Array.isArray(e))return e.map((i,s)=>m(i,n,`${t}[${s}]`)??null);if(e instanceof Map)return{$map:[...e].map(([i,s],o)=>[m(i,n,t),m(s,n,`${t}.${o}`)??null])};if(e instanceof Set)return{$set:[...e].map(i=>m(i,n,t))};if(ArrayBuffer.isView(e))return{$typed:e.constructor.name,v:[...e]};if(e instanceof Date)return{$date:e.toISOString()};if(!le(e)){if(typeof e.toJSON=="function")return m(e.toJSON(),n,t);n?.push(`${t}: a ${e.constructor?.name??"class"} instance was left out`);return}if("$map"in e||"$set"in e)throw new Error(`${t}: a key starting with $ can't be stored`);const r={};for(const i of Object.keys(e)){if(i[0]==="_")continue;const s=m(e[i],n,`${t}.${i}`);s!==void 0&&(r[i]=s)}return r}const ce={Uint8Array,Int8Array,Uint16Array,Int16Array,Uint32Array,Int32Array,Float32Array,Float64Array};function y(e){if(e===null||typeof e!="object")return e;if(Array.isArray(e))return e.map(y);if(e.$map)return new Map(e.$map.map(([t,r])=>[y(t),y(r)]));if(e.$set)return new Set(e.$set.map(y));if(e.$typed)return ce[e.$typed].from(e.v);if(e.$date)return new Date(e.$date);const n={};for(const t of Object.keys(e))n[t]=y(e[t]);return n}function K(e){const n={size:e.defaultSize};if(e.sizes!==null){const t=[],{sizes:r}=e;for(let i=0;i<r.length;i++){if(!e.isCustom(i)||e.isHidden(i))continue;const s=r[i],o=e.isAuto(i);let f=i;for(;f+1<r.length&&r[f+1]===s&&e.isCustom(f+1)&&!e.isHidden(f+1)&&e.isAuto(f+1)===o;)f++;t.push(o?[i,f-i+1,s,1]:[i,f-i+1,s]),i=f}t.length&&(n.runs=t)}return e.hiddenSizes!==null&&e.hiddenSizes.size>0&&(n.hidden=[...e.hiddenSizes]),n}function P(e,n){if(n){n.size!==void 0&&n.size!==e.defaultSize&&e.setDefaultSize(n.size);for(const[t,r,i,s]of n.runs??[])for(let o=t;o<t+r;o++)e.setSize(o,i,s===1);for(const[t,r]of n.hidden??[])r>=0&&e.setSize(t,r),e.setHidden(t,!0)}}function ue(e){const n=[];let t;for(const r of[...e.keys()].sort((i,s)=>i-s)){const i=e.get(r);t&&r===t[0]+t[1]&&i===t[2]?t[1]++:(t=[r,1,i],n.push(t))}return n}function ae(e){const n=new Map;for(const t of e){if(t.length===2){const[o,f]=t;n.set(o,f);continue}const[r,i,s]=t;if(t.length!==3||!Number.isInteger(r)||!Number.isInteger(i)||r<0||i<1||r+i>U)throw new RangeError("Column style run exceeds the sheet or has invalid bounds");for(let o=r;o<r+i;o++)n.set(o,s)}return n}const de=16;function me(e){const n={};for(const t of Object.keys(e))t!=="tpl"&&t[0]!=="_"&&(n[t]=e[t]);return n}function q(e,n,t){switch(n&J){case I:return t;case T:return e.strings.get(t);case A:return t!==0;case $:return{e:M[t]??"#N/A"};default:return null}}function pe(e,n,t,r,i){const s=[],o=[];let f=null,l=-2,u=-1,c=0,d=-1;const{formulas:S}=n,{rich:a}=e.strings,h=i.styles===!1,C=i.formulas===!1;for(let p=0;p<r.length;p++){const D=r[p];if(D===void 0)continue;const X=p*L,{kinds:Y,values:N,styles:x}=D;for(let g=0;g<L;g++){const O=Y[g],w=X+g;if(x!==null&&!h&&x[g]!==0&&(x[g]===c&&w===d+1?d=w:(c!==0&&o.push([u,d-u+1,c]),u=w,d=w,c=x[g])),O===0||O&ne)continue;let z;if(O&F){const R=S.get(w*U+t),E=q(e,O,N[g]);R&&!C?(z={f:me(R)},E!==null&&(z.v=E)):z=E}else(O&J)===T?(z=e.strings.get(N[g]),a!==null&&a.has(N[g])&&(z={s:z,rich:a.get(N[g])})):z=q(e,O,N[g]);const H=w-l-1;if(f===null||H>de)f={r:w,v:[]},s.push(f);else for(let R=0;R<H;R++)f.v.push(null);f.v.push(z),l=w}}return c!==0&&o.push([u,d-u+1,c]),{blocks:s,styles:o}}function ye(e,n,t,r){for(const{r:i,v:s}of r){let o=null,f=-1;for(let u=0;u<s.length;u++){const c=s[u];if(c===null)continue;const d=i+u,S=d>>>k;S!==f&&(o=n._ensure(d,t),f=S);const a=dⅇif(typeof c=="number")o.kinds[a]=I,o.values[a]=c;else if(typeof c=="string")o.kinds[a]=T,o.values[a]=e.strings.add(c);else if(typeof c=="boolean")o.kinds[a]=A,o.values[a]=c?1:0;else if(c.e!==void 0)o.kinds[a]=$,o.values[a]=Math.max(0,M.indexOf(c.e));else if(c.s!==void 0){const h=e.strings.add(c.s);o.kinds[a]=T,o.values[a]=h,e.strings.rich??=new Map,e.strings.rich.set(h,c.rich)}else if(c.f!==void 0){let h=F,C=0;const p=c.v;typeof p=="number"?(h|=I,C=p):typeof p=="string"?(h|=T,C=e.strings.add(p)):typeof p=="boolean"?(h|=A,C=p?1:0):p&&p.e!==void 0&&(h|=$,C=Math.max(0,M.indexOf(p.e))),o.kinds[a]=h,o.values[a]=C,n.setFormula(d,t,typeof c.f=="string"?{f:c.f,typed:!0}:c.f)}}const l=i+s.length-1;l>n.maxRow&&(n.maxRow=l)}t>n.maxCol&&(n.maxCol=t)}function ge(e,n,t){for(const[r,i,s]of t)for(let o=r;o<r+i;o++)e.setStyle(o,n,s)}function Q(e,n){const t={};for(const r of Object.keys(e)){if(r[0]==="_")continue;const i=e[r];i!==n[r]&&!(typeof i=="object"&&i!==null&&JSON.stringify(i)===JSON.stringify(n[r]))&&(t[r]=i)}return t}function he(e,n,t,r,i){const s={name:n.name};if(n.hidden&&(s.hidden=!0),n.hidden&&n.veryHidden&&(s.veryHidden=!0),n.tabColor&&(s.tabColor=n.tabColor),n.rtl&&(s.rtl=!0),n.zoom!==1&&(s.zoom=n.zoom),n.frozenRows&&(s.frozenRows=n.frozenRows),n.frozenCols&&(s.frozenCols=n.frozenCols),n.frozenTop&&(s.frozenTop=n.frozenTop),n.frozenLeft&&(s.frozenLeft=n.frozenLeft),n.showGridlines===!1&&(s.showGridlines=!1),n.isVirtual&&r.dataSheets==="inline")return{...s,...W(n,t,i,Se)};s.rows=K(n.rows),s.cols=K(n.cols),n.rowStyles?.size&&(s.rowStyles=[...n.rowStyles]),n.colStyles?.size&&(s.colStyles=ue(n.colStyles)),n.merges.length&&(s.merges=n.merges.map(({r0:u,c0:c,r1:d,c1:S})=>[u,c,d,S]));const f=[],l=[];return n.columns.forEach((u,c)=>{if(r.cells===!1)return;const{blocks:d,styles:S}=pe(e,n,c,u,r);d.length&&f.push({c,blocks:d}),S.length&&l.push({c,runs:S})}),f.length&&(s.cells=f),l.length&&(s.styles=l),{...s,...W(n,t,i)}}const Se=new Set(["pageSetup","cf","validations","hyperlinks","protection","comments","sparklines","rowExtras","colExtras"]);function W(e,n,t,r=null){const i={},s={};for(const f of Object.keys(e)){if(ie.has(f)||b.has(f)||f[0]==="_"||f.endsWith("Changed")||r&&!r.has(f))continue;const l=e[f];if(l==null||l===fe[f])continue;const u=m(l,t,`${e.name}.${f}`);u!==void 0&&(s[f]=u)}Object.keys(s).length&&(i.features=s);const o=n?.objectsOf?.(e);if(o?.length){const f=m(o.map(Ce),t,`${e.name}.objects`);f.length&&(i.objects=f)}return i}function Ce(e){const{bitmap:n,blob:t,canvas:r,modelEpoch:i,...s}=e;return t&&(s.imageType=t.type),s}function je(e,n=null,t={}){return Z(e,()=>we(e,n,t))}function we(e,n,t){const r=t.warnings??[],i=G(),s={format:j,version:1};s.activeSheet=e.activeSheet,e.fileName&&(s.fileName=e.fileName),e.themeInfo?s.theme=m(e.themeInfo,r,"theme"):e.theme&&(s.themeColors=e.theme);const o=e.styles.list;s.styles=o.length===1&&Object.keys(Q(o[0],i)).length===0?[]:o.map(l=>Q(l,i)),e.dxfs?.length&&(s.dxfs=m(e.dxfs,r,"dxfs")),e.persons?.length&&(s.persons=m(e.persons,r,"persons")),e.protection&&(s.protection=m(e.protection,r,"protection")),e.calcMode&&(s.calcMode=e.calcMode),e.calcSettings&&(s.calcSettings=m(e.calcSettings,r,"calcSettings")),e.getDataQueries&&(s.queries=m(e.getDataQueries,r,"queries")),e.customData&&(s.custom=m(e.customData,r,"custom"));const f=e.engine;if(f&&(f.manual&&(s.manualCalc=!0),f.names.size&&(s.names=[...f.names].map(([l,u])=>{const c=l.indexOf("!");return{name:u.name??(c>=0?l.slice(c+1):l),text:u.text,...c>=0?{sheet:Number(l.slice(0,c))}:{}}})),f.tables.size&&(s.tables=[...f.tables.values()].map(l=>{const{sheet:u,source:c,part:d,id:S,isNew:a,dirty:h,...C}=l;return{...m(C,r,`table ${l.name}`),sheet:e.sheets.indexOf(u)}}))),s.sheets=e.sheets.map(l=>he(e,l,n,t,r)),s.sheets.some(l=>l.colStyles)&&(s.version=2),s.sheets.some(l=>l.features?.cellTypeRules)&&(s.version=3),t.pivots==="definitions"&&n?.pivotsToJSON){const l=n.pivotsToJSON(t);l&&(s.pivots=l,s.version=4)}if((t.dataSheets==="inline"||n?.keepsDataSheets)&&n?.dataSheetsToJSON){const l=n.dataSheetsToJSON({rows:t.rows!==!1});l&&(s.dataSheets=l,s.version=4)}if(n?.extrasToJSON){const l=n.extrasToJSON(r,t);l&&(s.extras=l)}return r.length&&(s.warnings=r),s}function ze(e,n){n.hidden&&(e.hidden=!0),n.hidden&&n.veryHidden&&(e.veryHidden=!0),n.tabColor&&(e.tabColor=n.tabColor,e.tabColorChanged=!0),n.rtl&&(e.rtl=!0),n.zoom&&(e.zoom=n.zoom),e.frozenRows=n.frozenRows??0,e.frozenCols=n.frozenCols??0,e.frozenTop=n.frozenTop??0,e.frozenLeft=n.frozenLeft??0,n.showGridlines===!1&&(e.showGridlines=!1),P(e.rows,n.rows),P(e.cols,n.cols),(n.cols?.runs||n.cols?.hidden)&&(e.colsChanged=!0),n.rowStyles&&(e.rowStyles=new Map(n.rowStyles)),n.colStyles&&(e.colStyles=ae(n.colStyles)),n.merges&&(e.merges=n.merges.map(([t,r,i,s])=>({r0:t,c0:r,r1:i,c1:s}))),e.viewChanged=!0;for(const[t,r]of Object.entries(n.features??{})){if(b.has(t))continue;let i=y(r);(t==="cf"||t==="validations")&&Array.isArray(i)&&(i=i.filter(s=>!s?.managedBy)),e[t]=oe(t,i),B[t]&&(e[B[t]]=!0)}}function Oe(e,n){const t=G();n.styles?.length&&e.styles.load(n.styles.map(r=>({...t,...r}))),n.dxfs&&(e.dxfs=y(n.dxfs),e.sourceDxfs=0),n.persons&&(e.persons=y(n.persons),e.personsChanged=!0),n.protection&&(e.protection=y(n.protection),e.protectionChanged=!0),n.calcMode&&(e.calcMode=n.calcMode,e.calcModeChanged=!0),n.calcSettings&&(re(e.engine,y(n.calcSettings)),e.calcSettingsChanged=!0),n.queries&&(e.getDataQueries=y(n.queries)),n.custom&&(e.customData=y(n.custom)),n.fileName&&(e.fileName=n.fileName),n.themeColors&&(e.theme=n.themeColors)}function Ne(e,n,t={}){const{engine:r}=e;e.formulasEdited=!0,e.activeSheet=Math.min(n.activeSheet??0,e.sheets.length-1),r.sheetsChanged(),n.manualCalc&&(r.manual=!0),r.setNames((n.names??[]).map(i=>({name:i.name,text:i.text,localSheetId:i.sheet}))),n.names?.length&&(e.namesChanged=!0),r.setTables((n.tables??[]).map(i=>{const{sheet:s,...o}=i;return{...y(o),sheet:e.sheets[s],isNew:!0,dirty:!0}})),r.buildGraph({fullCalc:t.recalculate===!0}),e.dirty=!1}function _e(e,n={}){if(e?.format!==j)throw new Error(`This isn't ${j} JSON`);if(e.version>V)throw new Error(`This JSON has version ${e.version}; this code reads up to ${V}`);const t=new te;return t.engine=new se(t),Oe(t,n.styles===!1?{...e,styles:[]}:e),t.sheets=e.sheets.map(r=>new v(r.name)),e.sheets.forEach((r,i)=>{const s=t.sheets[i];ze(s,r);for(const o of r.cells??[])ye(t,s,o.c,o.blocks);if(n.styles!==!1)for(const o of r.styles??[])ge(s,o.c,o.runs);if(r.styles?.length)for(const o of r.styles)o.c>s.maxCol&&(s.maxCol=o.c)}),Ne(t,e,n),t}export{j as FORMAT_NAME,V as FORMAT_VERSION,Oe as applyBookMeta,ze as applySheetMeta,_e as bookFromJSON,je as bookToJSON,y as decodeData,m as encodeData,Ne as finishLoad,Ie as registerFeatureReviver,oe as reviveSheetFeature};
|
package/api/style-refs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const u=(t,e)=>t&&e&&t.hex?.toUpperCase()===String(e).toUpperCase();function c(t,e){return!t||t.a.theme===void 0||!e?null:{t:Number(t.a.theme),tint:t.a.tint?Number(t.a.tint):0,hex:e}}function h(t,e){return t&&u(t,e??"#000000")?`theme="${t.t}"${t.tint?` tint="${t.tint}"`:""}`:`rgb="FF${(e??"#000000").slice(1)}"`}function x(t,e){const s=t.fonts[Number(e.fontId??0)],r=t.fills[Number(e.fillId??0)],n=t.borders[Number(e.borderId??0)];return!s&&!r&&!n?null:{...s,...r,...n?{border:n}:{}}}function b(t,e,s){const r=e(t,"color"),n=c(r,s(r)),o=e(t,"scheme")?.a.val;return!n&&!o?null:{...n?{color:n}:{},...o==="major"||o==="minor"?{font:o}:{}}}function T(t,e,s){const r=e(t,"patternFill");if(!r)return null;const n=e(r,"fgColor"),o=e(r,"bgColor");if(r.a.patternType==="solid"){const a=n?.a.theme!==void 0?n:o,f=c(a,s(a));return f?{fill:f}:null}const l=c(o,s(o)),i=c(n,s(n));return l||i?{...l?{fill:l}:{},...i?{patternFg:i}:{}}:null}function g(t,e,s){const r={};let n=!1;for(const[o,l]of[["left","l"],["right","r"],["top","t"],["bottom","b"],["diagonal","d"]]){const i=e(e(t,o),"color"),a=c(i,s(i));a&&(r[l]=a,n=!0)}return n?r:null}const m="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",p="application/vnd.openxmlformats-officedocument.theme+xml",d="xl/theme/theme1.xml";async function y(t,e,s){let{types:r,rels:n}=e;const o=/<Relationship\b[^>]*\/theme"[^>]*\/>/.exec(n);let l=e.part??null;if(!o)n=n.replace("</Relationships>"
|
|
1
|
+
const u=(t,e)=>t&&e&&t.hex?.toUpperCase()===String(e).toUpperCase();function c(t,e){return!t||t.a.theme===void 0||!e?null:{t:Number(t.a.theme),tint:t.a.tint?Number(t.a.tint):0,hex:e}}function h(t,e){return t&&u(t,e??"#000000")?`theme="${t.t}"${t.tint?` tint="${t.tint}"`:""}`:`rgb="FF${(e??"#000000").slice(1)}"`}function x(t,e){const s=t.fonts[Number(e.fontId??0)],r=t.fills[Number(e.fillId??0)],n=t.borders[Number(e.borderId??0)];return!s&&!r&&!n?null:{...s,...r,...n?{border:n}:{}}}function b(t,e,s){const r=e(t,"color"),n=c(r,s(r)),o=e(t,"scheme")?.a.val;return!n&&!o?null:{...n?{color:n}:{},...o==="major"||o==="minor"?{font:o}:{}}}function T(t,e,s){const r=e(t,"patternFill");if(!r)return null;const n=e(r,"fgColor"),o=e(r,"bgColor");if(r.a.patternType==="solid"){const a=n?.a.theme!==void 0?n:o,f=c(a,s(a));return f?{fill:f}:null}const l=c(o,s(o)),i=c(n,s(n));return l||i?{...l?{fill:l}:{},...i?{patternFg:i}:{}}:null}function g(t,e,s){const r={};let n=!1;for(const[o,l]of[["left","l"],["right","r"],["top","t"],["bottom","b"],["diagonal","d"]]){const i=e(e(t,o),"color"),a=c(i,s(i));a&&(r[l]=a,n=!0)}return n?r:null}const m="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",p="application/vnd.openxmlformats-officedocument.theme+xml",d="xl/theme/theme1.xml";async function y(t,e,s){let{types:r,rels:n}=e;const o=/<Relationship\b[^>]*\/theme"[^>]*\/>/.exec(n);let l=e.part??null;if(!o)n=n.replace("</Relationships>",()=>`<Relationship Id="rIdTheme8c" Type="${m}" Target="theme/theme1.xml"/></Relationships>`),l=d;else if(l===null){const i=/Target="([^"]+)"/.exec(o[0])[1];l=i.startsWith("/")?i.slice(1):`xl/${i}`}return r.includes(`PartName="/${l}"`)||(r=r.replace("</Types>",()=>`<Override PartName="/${l}" ContentType="${p}"/></Types>`)),await t.addBytes(l,s),e.handled?.add(l),{types:r,rels:n}}export{g as borderRefs,h as colorAttrs,T as fillRefs,b as fontRefs,x as readStyleRefs,c as refFromNode,u as refLive,y as writeThemePart};
|
package/calc/adjust.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{colName as
|
|
1
|
+
import{colName as O,MAX_COLS as U,MAX_ROWS as k}from"../core/address.js";import{tokenize as X}from"./parser.js";function T(t,e,c){return t<e?t:c>0?t+c:t<e-c?null:t+c}function A(t,e,c,s,u){if(s>0){const f=t>=c?t+s:t,C=e>=c?Math.min(e+s,u-1):e;return f>=u?null:[f,C]}const $=c-s;if(t>=c&&e<$)return null;const w=t<c?t:t<$?c:t+s,S=e<c?e:e<$?c-1:e+s;return[w,S]}const M=(t,e,c,s)=>`${c?"$":""}${O(t)}${s?"$":""}${e+1}`;function _(t,e){const c=t[e],s=t[e+1];if(c.t!=="err"||c.v?.code!=="#REF!"||s===void 0||s.s!==c.e||!(s.t==="cell"||s.t==="rows"||s.t==="cols"))return 0;const u=t[e+2];return s.t==="cell"&&u?.t==="op"&&u.v===":"&&u.s===s.e&&t[e+3]?.t==="cell"?4:2}function g(t){if(!t?.length)return null;const e=new Map;return t.forEach((c,s)=>{for(const u of c)e.set(u.toUpperCase(),s)}),e}function z(t,e,c,s){if(t===null||e.toUpperCase()!==s)return!1;const u=t.get(s);return u!==void 0&&u===t.get(c.toUpperCase())}function L(t,e,c,s){const u=(i,m)=>!e.span||i>=e.span[0]&&m<=e.span[1];let $;try{$=X(t)}catch{return t}const w=s.toUpperCase(),S=g(e.group),f=e.axis==="row";let C="",y=0;const d=(i,m,E)=>{C+=t.slice(y,i)+E,y=m};for(let i=0;i<$.length;i++){const m=$[i],E=_($,i);if(E>0){i+=E-1;continue}let b=c,o=null,p=i;m.t==="sheet"&&(o=m,b=m.v.book===null&&(m.v.sheet2===null?m.v.sheet.toUpperCase()===w:z(S,m.v.sheet,m.v.sheet2,w)),p=i+1);const l=$[p];if(l===void 0)break;if(l.t==="cell"){const n=l.v,h=$[p+1],v=$[p+2];if(h?.t==="op"&&h.v===":"&&v?.t==="cell"){const r=v.v,F=f?[Math.min(n.c,r.c),Math.max(n.c,r.c)]:[Math.min(n.r,r.r),Math.max(n.r,r.r)];if(b&&u(F[0],F[1])){const x=o?t.slice(o.s,o.e):"",a=f?A(Math.min(n.r,r.r),Math.max(n.r,r.r),e.at,e.n,k):A(Math.min(n.c,r.c),Math.max(n.c,r.c),e.at,e.n,U),j=o?o.s:l.s;if(a===null)d(j,v.e,`${x}#REF!`);else if(f){const R=n.r<=r.r;d(j,v.e,`${x}${M(n.c,R?a[0]:a[1],n.cAbs,n.rAbs)}:${M(r.c,R?a[1]:a[0],r.cAbs,r.rAbs)}`)}else{const R=n.c<=r.c;d(j,v.e,`${x}${M(R?a[0]:a[1],n.r,n.cAbs,n.rAbs)}:${M(R?a[1]:a[0],r.r,r.cAbs,r.rAbs)}`)}}i=p+2;continue}if(b&&u(f?n.c:n.r,f?n.c:n.r)){const r=T(f?n.r:n.c,e.at,e.n),F=f?k:U,x=o?o.s:l.s,a=o?t.slice(o.s,o.e):"";r===null||r>=F?d(x,l.e,`${a}#REF!`):d(x,l.e,a+(f?M(n.c,r,n.cAbs,n.rAbs):M(r,n.r,n.cAbs,n.rAbs)))}i=p}else if(l.t==="rows"&&f&&b&&!e.span){const n=l.v,h=A(n.r0,n.r1,e.at,e.n,k),v=o?o.s:l.s,r=o?t.slice(o.s,o.e):"";h===null?d(v,l.e,`${r}#REF!`):d(v,l.e,`${r}${n.a0?"$":""}${h[0]+1}:${n.a1?"$":""}${h[1]+1}`),i=p}else if(l.t==="cols"&&!f&&b&&!e.span){const n=l.v,h=A(n.c0,n.c1,e.at,e.n,U),v=o?o.s:l.s,r=o?t.slice(o.s,o.e):"";h===null?d(v,l.e,`${r}#REF!`):d(v,l.e,`${r}${n.a0?"$":""}${O(h[0])}:${n.a1?"$":""}${O(h[1])}`),i=p}else o&&(i=p-1)}return C+t.slice(y)}function N(t,e){const c=e.axis==="row",s=c?A(t.r0,t.r1,e.at,e.n,k):A(t.c0,t.c1,e.at,e.n,U);return s===null?null:c?{...t,r0:s[0],r1:s[1]}:{...t,c0:s[0],c1:s[1]}}export{L as adjustFormula,N as adjustRange,_ as brokenReference,M as cellText,T as moveIndex};
|
package/calc/compile.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{MAX_COLS as d,MAX_ROWS as A}from"../core/address.js";import{Arr as x,compareValues as L,ERR as p,Lambda as w,num as U,OMITTED as E,Ref as m,RefArray as M,RefList as g,REJECTED as C,str as O,XlError as b}from"./values.js";import{FUNCS as v}from"./functions/registry.js";const k=5e-16;class B{constructor(e,c=!1){this.definedName=c,this.cx=e,this.refs=[],this.names=[],this.tables=[],this.isVolatile=!1,this.readsSheets=!1,this.mayArray=!1,this.external=!1,this.keepImportedResult=!1,this.arrayCtx=!1,this.scopes=[],this.slots=0,this.rangeSlots=new Set}compileTop(e){const c=this.any(e),{slots:n,cx:i}=this;return n===0?c:(o,t,r)=>{const s=i.frame;i.frame=new Array(n);try{return c(o,t,r)}finally{i.frame=s}}}sheetOf(e){const{cx:c}=this;if(e==null)return t=>t;if(e.book!==null&&e.book!==void 0){const t=c.externalSheet?.(e.book,e.sheet)??null;return t===null&&(this.external=!0),()=>t}const n=e.sheet;let i=-1,o=null;return()=>(i!==c.sheetGen&&(o=c.sheetByName(n),i=c.sheetGen),o)}cellRefClosure(e){const c=this.sheetOf(e.sheet),n=this.definedName,{rAbs:i,cAbs:o,r:t,c:r}=e;return this.refs.push({wrap:n,sheet:e.sheet,rAbs0:i,r0:t,rAbs1:i,r1:t,cAbs0:o,c0:r,cAbs1:o,c1:r}),e.sheet?.sheet2?this.sheet3d(e.sheet,(s,a,u)=>{const h=n?(i?t:a+t)%A:i?t:a+t,f=n?(o?r:u+r)%d:o?r:u+r;return new m(s,h,f,h,f)}):(s,a,u)=>{const h=n?(i?t:s+t)%A:i?t:s+t,f=n?(o?r:a+r)%d:o?r:a+r,l=c(u);return l===null||h<0||f<0||h>=A||f>=d?p.REF:new m(l,h,f,h,f)}}sheet3d(e,c){const{cx:n}=this;return(i,o)=>{const t=n.sheetsBetween(e.sheet,e.sheet2);return t===null?p.REF:new g(t.map(r=>c(r,i,o)))}}rangeClosure(e){const{a:c,b:n}=e,i=this.sheetOf(e.sheet),o=this.definedName;this.refs.push({wrap:o,sheet:e.sheet,rAbs0:c.rAbs,r0:c.r,rAbs1:n.rAbs,r1:n.r,cAbs0:c.cAbs,c0:c.c,cAbs1:n.cAbs,c1:n.c});const t=(r,s,a)=>{const u=o?(c.rAbs?c.r:s+c.r)%A:c.rAbs?c.r:s+c.r,h=o?(c.cAbs?c.c:a+c.c)%d:c.cAbs?c.c:a+c.c,f=o?(n.rAbs?n.r:s+n.r)%A:n.rAbs?n.r:s+n.r,l=o?(n.cAbs?n.c:a+n.c)%d:n.cAbs?n.c:a+n.c;return u<0||h<0||f<0||l<0||u>=A||f>=A||h>=d||l>=d?p.REF:new m(r,Math.min(u,f),Math.min(h,l),Math.max(u,f),Math.max(h,l))};return e.sheet?.sheet2?this.sheet3d(e.sheet,t):(r,s,a)=>{const u=i(a);return u===null?p.REF:t(u,r,s)}}rowsClosure(e){const c=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:e.a0,r0:e.r0,rAbs1:e.a1,r1:e.r1,cAbs0:!0,c0:0,cAbs1:!0,c1:d-1}),(n,i,o)=>{const t=e.a0?e.r0:n+e.r0,r=e.a1?e.r1:n+e.r1,s=c(o);return s===null||t<0||r<0||t>=A||r>=A?p.REF:new m(s,Math.min(t,r),0,Math.max(t,r),d-1)}}colsClosure(e){const c=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:!0,r0:0,rAbs1:!0,r1:A-1,cAbs0:e.a0,c0:e.c0,cAbs1:e.a1,c1:e.c1}),(n,i,o)=>{const t=e.a0?e.c0:i+e.c0,r=e.a1?e.c1:i+e.c1,s=c(o);return s===null||t<0||r<0||t>=d||r>=d?p.REF:new m(s,0,Math.min(t,r),A-1,Math.max(t,r))}}any(e){const{cx:c}=this;switch(e.t){case"lit":{const n=e.v;return()=>n}case"omit":return()=>E;case"paren":return this.any(e.e);case"cell":return this.cellRefClosure(e);case"range":return this.rangeClosure(e);case"rows":return this.rowsClosure(e);case"cols":return this.colsClosure(e);case"array":{const n=x.of2D(e.rows);return()=>n}case"union":{const n=e.items.map(i=>this.any(i));return(i,o,t)=>{const r=[];for(const s of n){const a=s(i,o,t);if(a instanceof m)r.push(a);else if(a instanceof g)r.push(...a.refs);else return a instanceof b?a:p.VALUE}return new g(r)}}case"intersect":{const n=this.any(e.l),i=this.any(e.r);return(o,t,r)=>{const s=n(o,t,r),a=i(o,t,r);if(s instanceof b)return s;if(a instanceof b)return a;if(!(s instanceof m)||!(a instanceof m)||s.sheet!==a.sheet)return p.VALUE;const u=Math.max(s.r0,a.r0),h=Math.min(s.r1,a.r1),f=Math.max(s.c0,a.c0),l=Math.min(s.c1,a.c1);return u>h||f>l?p.NULL:new m(s.sheet,u,f,h,l)}}case"rangeOp":{const n=this.any(e.l),i=this.any(e.r);return(o,t,r)=>{const s=n(o,t,r),a=i(o,t,r);return s instanceof b?s:a instanceof b?a:!(s instanceof m)||!(a instanceof m)||s.sheet!==a.sheet?p.REF:new m(s.sheet,Math.min(s.r0,a.r0),Math.min(s.c0,a.c0),Math.max(s.r1,a.r1),Math.max(s.c1,a.c1))}}case"implicit":{const n=this.any(e.e);return(i,o,t)=>{const r=n(i,o,t);return r instanceof m?r.isCell?c.read(r.sheet,r.r0,r.c0):c.intersect(r,i,o):r instanceof x?r.data[0]:r}}case"spillRef":{const n=this.any(e.e);return(i,o,t)=>{const r=n(i,o,t);return r instanceof m?c.spillRange(r):p.REF}}case"name":return this.nameNode(e);case"table":return this.tables.push({name:e.name,spec:e.spec}),(n,i,o)=>c.resolveTable(e.name,e.spec,n,i,o);case"call":return this.call(e);case"apply":{const n=this.any(e.fn),i=e.args.map(o=>this.any(o));return(o,t,r)=>{const s=n(o,t,r);return s instanceof b?s:s instanceof w?c.invoke(s,i.map(a=>a(o,t,r)),o,t,r):p.VALUE}}default:return this.value(e)}}nameNode(e){const{cx:c}=this,n=this.lookupVar(e.name);if(n!==null){const s=n;return()=>c.frame[s]}const i=c.tables?.get(e.name.toUpperCase());if(i&&(!e.sheet||e.sheet.book===null&&!e.sheet.sheet2&&e.sheet.sheet.toUpperCase()===i.sheet.name.toUpperCase()))return this.any({t:"table",name:e.name,spec:"[]"});this.names.push(e.name);const o=e.name,t=e.sheet?this.sheetOf(e.sheet):s=>s,r=e.sheet?void 0:v.get(o.toUpperCase());if(r!==void 0){const s=new w([],null,null);return s.native=r,s.label=o.toUpperCase(),(a,u,h)=>{const f=c.evalName(o,a,u,h);return f===p.NAME?s:f}}return(s,a,u)=>c.evalName(o,s,a,t(u)??u)}lookupVar(e){const c=e.toUpperCase();for(let n=this.scopes.length-1;n>=0;n--){const i=this.scopes[n].get(c);if(i!==void 0)return i}return null}deref(e){const{cx:c}=this;return this.arrayCtx?(n,i,o)=>{const t=e(n,i,o);return t instanceof M?c.toArr(t):t instanceof m||t instanceof g?c.refToValue(t):t}:(n,i,o)=>{const t=e(n,i,o);return t instanceof M?c.toArr(t):t instanceof m||t instanceof g?c.refAuto(t,n,i):t}}value(e){const{cx:c}=this;switch(e.t){case"lit":{const n=e.v;return()=>n}case"paren":return this.value(e.e);case"cell":{if(this.definedName){const s=this.cellRefClosure(e);return(a,u,h)=>c.refAuto(s(a,u,h),a,u)}const{rAbs:n,cAbs:i,r:o,c:t}=e;if(this.refs.push({sheet:e.sheet,rAbs0:n,r0:o,rAbs1:n,r1:o,cAbs0:i,c0:t,cAbs1:i,c1:t}),e.sheet?.sheet2)return()=>p.VALUE;const r=e.sheet?this.sheetOf(e.sheet):null;return r===null?!n&&!i?(s,a,u)=>{const h=s+o,f=a+t;return h<0||f<0||h>=A||f>=d?p.REF:c.read(u,h,f)}:(s,a,u)=>{const h=n?o:s+o,f=i?t:a+t;return h<0||f<0||h>=A||f>=d?p.REF:c.read(u,h,f)}:(s,a,u)=>{const h=n?o:s+o,f=i?t:a+t,l=r(u);return l===null||h<0||f<0||h>=A||f>=d?p.REF:c.read(l,h,f)}}case"range":case"rows":case"cols":return this.arrayCtx||(this.mayArray=!0),this.deref(this.any(e));case"neg":{const n=this.value(e.e);return(i,o,t)=>{const r=n(i,o,t);return typeof r=="number"?-r:c.unary(r,s=>-s)}}case"pct":{const n=this.value(e.e);return(i,o,t)=>{const r=n(i,o,t);return typeof r=="number"?r/100:c.unary(r,s=>s/100)}}case"bin":return this.binary(e);default:return!this.arrayCtx&&this.mayYieldRange(e)&&(this.mayArray=!0),this.deref(this.any(e))}}mayYieldRange(e){switch(e.t){case"paren":return this.mayYieldRange(e.e);case"range":case"rows":case"cols":case"union":case"rangeOp":case"intersect":case"spillRef":case"apply":return!0;case"table":return!/^\[\[#This Row\],\[[^\]]+\]\]$/i.test(e.spec);case"name":{if(e.sheet)return!0;const c=this.lookupVar(e.name);return c!==null?this.rangeSlots.has(c):this.cx.nameMayYieldRange?.(e.name)??!0}case"call":{if(e.name==="LET"||e.name==="LAMBDA"||this.lookupVar(e.name)!==null)return!0;const c=v.get(e.name);return c===void 0||c.refResult?!0:c.passes?e.args.some((n,i)=>c.passes(i,e.args.length)&&this.mayYieldRange(n)):!1}default:return!1}}binary(e){const{cx:c}=this,n=this.value(e.l),i=this.value(e.r);switch(e.op){case"+":return(o,t,r)=>{const s=n(o,t,r),a=i(o,t,r);if(typeof s=="number"&&typeof a=="number"){const u=s+a;return Math.abs(u)<Math.abs(s)*k?0:u}return c.slowBin("+",s,a)};case"-":return(o,t,r)=>{const s=n(o,t,r),a=i(o,t,r);if(typeof s=="number"&&typeof a=="number"){const u=s-a;return Math.abs(u)<Math.abs(s)*k?0:u}return c.slowBin("-",s,a)};case"*":return(o,t,r)=>{const s=n(o,t,r),a=i(o,t,r);return typeof s=="number"&&typeof a=="number"?s*a:c.slowBin("*",s,a)};case"/":return(o,t,r)=>{const s=n(o,t,r),a=i(o,t,r);return typeof s=="number"&&typeof a=="number"&&a!==0?s/a:c.slowBin("/",s,a)};case"^":return(o,t,r)=>c.slowBin("^",n(o,t,r),i(o,t,r));case"&":return(o,t,r)=>c.slowBin("&",n(o,t,r),i(o,t,r));default:{const o=e.op;return(t,r,s)=>c.slowBin(o,n(t,r,s),i(t,r,s))}}}call(e){const{cx:c}=this,{args:n}=e;let{name:i}=e;if(i.startsWith("_XLUDF.")&&(i=i.slice(7),!v.get(i)?.custom))return()=>p.NAME;if(i==="LET")return this.compileLet(n);if(i==="LAMBDA")return this.compileLambda(n);const o=this.lookupVar(i);if(o!==null){const r=o,s=n.map(a=>this.any(a));return(a,u,h)=>{const f=c.frame[r];return f instanceof w?c.invoke(f,s.map(l=>l(a,u,h)),a,u,h):p.VALUE}}const t=v.get(i);if(t===void 0){this.names.push(i);const r=n.map(s=>this.any(s));return(s,a,u)=>{const h=c.evalName(i,s,a,u);return h instanceof w?c.invoke(h,r.map(f=>f(s,a,u)),s,a,u):p.NAME}}if((n.length<t.min||n.length>t.max)&&!(n.length===1&&n[0].t==="omit"&&t.min===0))return()=>p.VALUE;if(t.volatile&&(this.isVolatile=!0),t.readsSheets&&(this.readsSheets=!0),t.notInExcel&&(this.notInExcel=!0),t.frozen&&(this.external=!0),(typeof t.keepImportedResult=="function"?t.keepImportedResult(n):t.keepImportedResult)&&(this.keepImportedResult=!0),t.kind==="lazy"||t.kind==="raw"){const r=this.arrayCtx,s=n.map((u,h)=>(this.arrayCtx=r||t.array===!0||typeof t.array=="function"&&t.array(h),this.any(u)));this.arrayCtx=r;const{fn:a}=t;return t.kind==="lazy"?(u,h,f)=>{try{return a(c,s,u,h,f)}catch(l){if(l instanceof b)return l;throw l}}:(u,h,f)=>{const l=new Array(s.length);for(let y=0;y<s.length;y++)l[y]=s[y](u,h,f);try{return a(c,l,u,h,f,r)}catch(y){if(y instanceof b)return y;throw y}}}return this.lifted(t,n)}toolpakArg(e,c){const{cx:n}=this,i=t=>typeof t=="boolean"&&!c?C:t;if(e.t!=="range"&&e.t!=="rows"&&e.t!=="cols"&&e.t!=="union"&&e.t!=="call"&&e.t!=="name"){const t=this.value(e);return(r,s,a)=>i(t(r,s,a))}const o=this.any(e);return(t,r,s)=>{const a=o(t,r,s);return a instanceof g||!n.legacy&&a instanceof m&&(a.r0!==a.r1||a.c0!==a.c1)?C:i(a instanceof m?n.refAuto(a,t,r):a)}}lifted(e,c){const{cx:n}=this,{errors:i}=e,o=e.dateSystem?(...s)=>e.fn(n.book.dateOffset,...s):e.fn,t=c.map((s,a)=>s.t==="omit"?()=>E:e.toolpak?this.toolpakArg(s,e.toolpak.logical.includes(a)):this.value(s)),r=t.length;if(r===1){const s=t[0];return(a,u,h)=>{const f=s(a,u,h);if(f instanceof x)return n.liftArr(o,[f],i);if(!i&&f instanceof b)return f;try{return o(f)}catch(l){if(l instanceof b)return l;throw l}}}if(r===2){const[s,a]=t;return(u,h,f)=>{const l=s(u,h,f),y=a(u,h,f);if(l instanceof x||y instanceof x)return n.liftArr(o,[l,y],i);if(!i){if(l instanceof b)return l;if(y instanceof b)return y}try{return o(l,y)}catch(R){if(R instanceof b)return R;throw R}}}return(s,a,u)=>{const h=new Array(r);let f=!1;for(let l=0;l<r;l++){const y=t[l](s,a,u);h[l]=y,y instanceof x&&(f=!0)}if(f)return n.liftArr(o,h,i);if(!i){for(let l=0;l<r;l++)if(h[l]instanceof b)return h[l]}try{return o(...h)}catch(l){if(l instanceof b)return l;throw l}}}compileLet(e){const{cx:c}=this;if(e.length<3||e.length%2===0)return()=>p.VALUE;const n=new Map;this.scopes.push(n);const i=[];for(let t=0;t+1<e.length;t+=2){const r=e[t];if(r.t!=="name")return this.scopes.pop(),()=>p.VALUE;const s=this.any(e[t+1]),a=this.slots++;this.mayYieldRange(e[t+1])&&this.rangeSlots.add(a),n.set(r.name.toUpperCase(),a),i.push(a,s)}const o=this.any(e[e.length-1]);return this.scopes.pop(),(t,r,s)=>{for(let a=0;a<i.length;a+=2)c.frame[i[a]]=i[a+1](t,r,s);return o(t,r,s)}}compileLambda(e){const{cx:c}=this;if(e.length===0)return()=>p.VALUE;const n=new Map;this.scopes.push(n);const i=[];for(let t=0;t<e.length-1;t++){const r=e[t];if(r.t!=="name")return this.scopes.pop(),()=>p.VALUE;const s=this.slots++;this.rangeSlots.add(s),n.set(r.name.toUpperCase(),s),i.push(s)}const o=this.any(e[e.length-1]);return this.scopes.pop(),()=>new w(i,o,c.frame)}}export{B as Compiler,L as compareValues,U as num,O as str};
|
|
1
|
+
import{MAX_COLS as b,MAX_ROWS as g}from"../core/address.js";import{Arr as C,compareValues as k,ERR as m,Lambda as M,num as T,OMITTED as L,Ref as y,RefArray as U,RefList as O,REJECTED as v,str as V,within as w,XlError as d}from"./values.js";import{FUNCS as S}from"./functions/registry.js";const N=5e-16,F=new Set(["BYCOL","BYROW","CHOOSECOLS","CHOOSEROWS","DROP","EXPAND","FILTER","FREQUENCY","GROUPBY","GROWTH","HSTACK","LINEST","LOGEST","MAKEARRAY","MAP","MINVERSE","MMULT","MODE.MULT","MUNIT","PIVOTBY","RANDARRAY","SCAN","SEQUENCE","SORT","SORTBY","TAKE","TEXTSPLIT","TOCOL","TOROW","TRANSPOSE","TREND","UNIQUE","VSTACK","WRAPCOLS","WRAPROWS"]);function x(p,e=new Set){switch(p.t){case"paren":case"neg":case"pct":return x(p.e,e);case"bin":return x(p.l,e)||x(p.r,e);case"name":return!p.sheet&&e.has(p.name.toUpperCase());case"call":{if(F.has(p.name))return!0;if(p.name==="LET"){const s=new Set(e);for(let i=0;i+1<p.args.length;i+=2)p.args[i].t==="name"&&x(p.args[i+1],s)&&s.add(p.args[i].name.toUpperCase());return p.args.length%2===1&&x(p.args[p.args.length-1],s)}const c=S.get(p.name);return c===void 0?!1:c.kind==="lift"?p.args.some(s=>x(s,e)):c.passes!==void 0&&p.args.some((s,i)=>c.passes(i,p.args.length)&&x(s,e))}default:return!1}}class D{constructor(e,c=!1){this.definedName=c,this.cx=e,this.refs=[],this.hulls=[],this.names=[],this.tables=[],this.isVolatile=!1,this.readsSheets=!1,this.mayArray=!1,this.external=!1,this.keepImportedResult=!1,this.arrayCtx=!1,this.scopes=[],this.slots=0,this.rangeSlots=new Set}compileTop(e){const c=this.any(e);x(e)&&(this.mayArray=!0);const{slots:s,cx:i}=this;return s===0?c:(o,t,r)=>{const n=i.frame;i.frame=new Array(s);try{return c(o,t,r)}finally{i.frame=n}}}sheetOf(e){const{cx:c}=this;if(e==null)return t=>t;if(e.book!==null&&e.book!==void 0){const t=c.externalSheet?.(e.book,e.sheet)??null;return t===null&&(this.external=!0),()=>t}const s=e.sheet;let i=-1,o=null;return()=>(i!==c.sheetGen&&(o=c.sheetByName(s),i=c.sheetGen),o)}cellRefClosure(e){const c=this.sheetOf(e.sheet),s=this.definedName,{rAbs:i,cAbs:o,r:t,c:r}=e;return this.refs.push({wrap:s,sheet:e.sheet,rAbs0:i,r0:t,rAbs1:i,r1:t,cAbs0:o,c0:r,cAbs1:o,c1:r}),e.sheet?.sheet2?this.sheet3d(e.sheet,(n,a,u)=>{const l=s?(i?t:a+t)%g:i?t:a+t,h=s?(o?r:u+r)%b:o?r:u+r;return new y(n,l,h,l,h)}):(n,a,u)=>{const l=s?(i?t:n+t)%g:i?t:n+t,h=s?(o?r:a+r)%b:o?r:a+r,f=c(u);return f===null||l<0||h<0||l>=g||h>=b?m.REF:new y(f,l,h,l,h)}}sheet3d(e,c){const{cx:s}=this;return(i,o)=>{const t=s.sheetsBetween(e.sheet,e.sheet2);return t===null?m.REF:new O(t.map(r=>c(r,i,o)))}}rangeClosure(e){const{a:c,b:s}=e,i=this.sheetOf(e.sheet),o=this.definedName;this.refs.push({wrap:o,sheet:e.sheet,rAbs0:c.rAbs,r0:c.r,rAbs1:s.rAbs,r1:s.r,cAbs0:c.cAbs,c0:c.c,cAbs1:s.cAbs,c1:s.c});const t=(r,n,a)=>{const u=o?(c.rAbs?c.r:n+c.r)%g:c.rAbs?c.r:n+c.r,l=o?(c.cAbs?c.c:a+c.c)%b:c.cAbs?c.c:a+c.c,h=o?(s.rAbs?s.r:n+s.r)%g:s.rAbs?s.r:n+s.r,f=o?(s.cAbs?s.c:a+s.c)%b:s.cAbs?s.c:a+s.c;return u<0||l<0||h<0||f<0||u>=g||h>=g||l>=b||f>=b?m.REF:new y(r,Math.min(u,h),Math.min(l,f),Math.max(u,h),Math.max(l,f))};return e.sheet?.sheet2?this.sheet3d(e.sheet,t):(r,n,a)=>{const u=i(a);return u===null?m.REF:t(u,r,n)}}rowsClosure(e){const c=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:e.a0,r0:e.r0,rAbs1:e.a1,r1:e.r1,cAbs0:!0,c0:0,cAbs1:!0,c1:b-1}),(s,i,o)=>{const t=e.a0?e.r0:s+e.r0,r=e.a1?e.r1:s+e.r1,n=c(o);return n===null||t<0||r<0||t>=g||r>=g?m.REF:new y(n,Math.min(t,r),0,Math.max(t,r),b-1)}}colsClosure(e){const c=this.sheetOf(e.sheet);return this.refs.push({sheet:e.sheet,rAbs0:!0,r0:0,rAbs1:!0,r1:g-1,cAbs0:e.a0,c0:e.c0,cAbs1:e.a1,c1:e.c1}),(s,i,o)=>{const t=e.a0?e.c0:i+e.c0,r=e.a1?e.c1:i+e.c1,n=c(o);return n===null||t<0||r<0||t>=b||r>=b?m.REF:new y(n,0,Math.min(t,r),g-1,Math.max(t,r))}}any(e){const{cx:c}=this;switch(e.t){case"lit":{const s=e.v;return()=>s}case"omit":return()=>L;case"paren":return this.any(e.e);case"cell":return this.cellRefClosure(e);case"range":return this.rangeClosure(e);case"rows":return this.rowsClosure(e);case"cols":return this.colsClosure(e);case"array":{const s=C.of2D(e.rows);return()=>s}case"union":{const s=e.items.map(i=>this.any(i));return(i,o,t)=>{const r=[];for(const n of s){const a=n(i,o,t);if(a instanceof y)r.push(a);else if(a instanceof O)r.push(...a.refs);else return a instanceof d?a:m.VALUE}return new O(r)}}case"intersect":{const s=this.scopes.length>0?[0,0,0]:[this.refs.length,this.names.length,this.tables.length],i=this.any(e.l),o=this.any(e.r);return this.hulls.push({refs:this.refs.slice(s[0]),names:this.names.slice(s[1]),tables:this.tables.slice(s[2])}),(t,r,n)=>{const a=i(t,r,n),u=o(t,r,n);if(a instanceof d)return a;if(u instanceof d)return u;if(!(a instanceof y)||!(u instanceof y)||a.sheet!==u.sheet)return m.VALUE;const l=Math.max(a.r0,u.r0),h=Math.min(a.r1,u.r1),f=Math.max(a.c0,u.c0),A=Math.min(a.c1,u.c1);return l>h||f>A?m.NULL:new y(a.sheet,l,f,h,A)}}case"rangeOp":{const s=this.scopes.length>0?[0,0,0]:[this.refs.length,this.names.length,this.tables.length],i=this.any(e.l),o=this.any(e.r);return this.hulls.push({refs:this.refs.slice(s[0]),names:this.names.slice(s[1]),tables:this.tables.slice(s[2])}),(t,r,n)=>{const a=i(t,r,n),u=o(t,r,n);return a instanceof d?a:u instanceof d?u:!(a instanceof y)||!(u instanceof y)||a.sheet!==u.sheet?m.REF:new y(a.sheet,Math.min(a.r0,u.r0),Math.min(a.c0,u.c0),Math.max(a.r1,u.r1),Math.max(a.c1,u.c1))}}case"implicit":{const s=this.any(e.e);return(i,o,t)=>{const r=s(i,o,t);return r instanceof y?r.isCell?c.read(r.sheet,r.r0,r.c0):c.intersect(r,i,o):r instanceof C?r.data[0]:r}}case"spillRef":{const s=this.any(e.e);return(i,o,t)=>{const r=s(i,o,t);return r instanceof y?c.spillRange(r):m.REF}}case"name":return this.nameNode(e);case"table":return this.tables.push({name:e.name,spec:e.spec}),(s,i,o)=>c.resolveTable(e.name,e.spec,s,i,o);case"call":return this.call(e);case"apply":{const s=this.any(e.fn),i=e.args.map(o=>this.any(o));return(o,t,r)=>{const n=s(o,t,r);return n instanceof d?n:n instanceof M?c.invoke(n,i.map(a=>a(o,t,r)),o,t,r):m.VALUE}}default:return this.value(e)}}nameNode(e){const{cx:c}=this;if(e.sheet?.book!==null&&e.sheet?.book!==void 0){const n=c.externalName?.(e.sheet.book,e.name)??null;return n===null&&(this.external=!0),()=>n??m.NAME}const s=this.lookupVar(e.name);if(s!==null){const n=s;return()=>c.frame[n]}const i=c.tables?.get(e.name.toUpperCase());if(i&&(!e.sheet||e.sheet.book===null&&!e.sheet.sheet2&&e.sheet.sheet.toUpperCase()===i.sheet.name.toUpperCase()))return this.any({t:"table",name:e.name,spec:"[]"});this.names.push(e.name);const o=e.name,t=e.sheet?this.sheetOf(e.sheet):n=>n,r=e.sheet?void 0:S.get(o.toUpperCase());if(r!==void 0){const n=new M([],null,null);return n.native=r,n.label=o.toUpperCase(),(a,u,l)=>{const h=c.evalName(o,a,u,l);return h===m.NAME?n:h}}return(n,a,u)=>c.evalName(o,n,a,t(u)??u)}lookupVar(e){const c=e.toUpperCase();for(let s=this.scopes.length-1;s>=0;s--){const i=this.scopes[s].get(c);if(i!==void 0)return i}return null}deref(e){const{cx:c}=this;return this.arrayCtx?(s,i,o)=>{const t=e(s,i,o);return t instanceof U?c.toArr(t):t instanceof y||t instanceof O?c.refToValue(t):t}:(s,i,o)=>{const t=e(s,i,o);return t instanceof U?c.toArr(t):t instanceof y||t instanceof O?c.refAuto(t,s,i):t}}value(e){const{cx:c}=this;switch(e.t){case"lit":{const s=e.v;return()=>s}case"paren":return this.value(e.e);case"cell":{if(this.definedName){const n=this.cellRefClosure(e);return(a,u,l)=>c.refAuto(n(a,u,l),a,u)}const{rAbs:s,cAbs:i,r:o,c:t}=e;if(this.refs.push({sheet:e.sheet,rAbs0:s,r0:o,rAbs1:s,r1:o,cAbs0:i,c0:t,cAbs1:i,c1:t}),e.sheet?.sheet2)return()=>m.VALUE;const r=e.sheet?this.sheetOf(e.sheet):null;return r===null?!s&&!i?(n,a,u)=>{const l=n+o,h=a+t;return l<0||h<0||l>=g||h>=b?m.REF:c.read(u,l,h)}:(n,a,u)=>{const l=s?o:n+o,h=i?t:a+t;return l<0||h<0||l>=g||h>=b?m.REF:c.read(u,l,h)}:(n,a,u)=>{const l=s?o:n+o,h=i?t:a+t,f=r(u);return f===null||l<0||h<0||l>=g||h>=b?m.REF:c.read(f,l,h)}}case"range":case"rows":case"cols":return this.arrayCtx||(this.mayArray=!0),this.deref(this.any(e));case"neg":{const s=this.value(e.e);return(i,o,t)=>{const r=s(i,o,t);return typeof r=="number"?-r:c.unary(r,n=>-n)}}case"pct":{const s=this.value(e.e);return(i,o,t)=>{const r=s(i,o,t);return typeof r=="number"?r/100:c.unary(r,n=>n/100)}}case"bin":return this.binary(e);default:return!this.arrayCtx&&this.mayYieldRange(e)&&(this.mayArray=!0),this.deref(this.any(e))}}mayYieldRange(e){switch(e.t){case"paren":return this.mayYieldRange(e.e);case"range":case"rows":case"cols":case"union":case"rangeOp":case"intersect":case"spillRef":case"apply":return!0;case"table":return!/^\[\[#This Row\],\[[^\]]+\]\]$/i.test(e.spec);case"name":{if(e.sheet)return!0;const c=this.lookupVar(e.name);return c!==null?this.rangeSlots.has(c):this.cx.nameMayYieldRange?.(e.name)??!0}case"call":{if(e.name==="LET"||e.name==="LAMBDA"||this.lookupVar(e.name)!==null)return!0;const c=S.get(e.name);return c===void 0||c.refResult?!0:c.passes?e.args.some((s,i)=>c.passes(i,e.args.length)&&this.mayYieldRange(s)):!1}default:return!1}}binary(e){const{cx:c}=this,s=this.value(e.l),i=this.value(e.r);switch(e.op){case"+":return(o,t,r)=>{const n=s(o,t,r),a=i(o,t,r);if(typeof n=="number"&&typeof a=="number"){const u=n+a;return Math.abs(u)<Math.abs(n)*N?0:w(u)}return c.slowBin("+",n,a)};case"-":return(o,t,r)=>{const n=s(o,t,r),a=i(o,t,r);if(typeof n=="number"&&typeof a=="number"){const u=n-a;return Math.abs(u)<Math.abs(n)*N?0:w(u)}return c.slowBin("-",n,a)};case"*":return(o,t,r)=>{const n=s(o,t,r),a=i(o,t,r);return typeof n=="number"&&typeof a=="number"?w(n*a):c.slowBin("*",n,a)};case"/":return(o,t,r)=>{const n=s(o,t,r),a=i(o,t,r);return typeof n=="number"&&typeof a=="number"&&a!==0?w(n/a):c.slowBin("/",n,a)};case"^":return(o,t,r)=>c.slowBin("^",s(o,t,r),i(o,t,r));case"&":return(o,t,r)=>c.slowBin("&",s(o,t,r),i(o,t,r));default:{const o=e.op;return(t,r,n)=>c.slowBin(o,s(t,r,n),i(t,r,n))}}}call(e){const{cx:c}=this,{args:s}=e;let{name:i}=e;if(i.startsWith("_XLUDF.")&&(i=i.slice(7),!S.get(i)?.custom))return()=>m.NAME;if(i==="LET")return this.compileLet(s);if(i==="LAMBDA")return this.compileLambda(s);const o=this.lookupVar(i);if(o!==null){const r=o,n=s.map(a=>this.any(a));return(a,u,l)=>{const h=c.frame[r];return h instanceof M?c.invoke(h,n.map(f=>f(a,u,l)),a,u,l):m.VALUE}}const t=S.get(i);if(t===void 0){this.names.push(i);const r=s.map(n=>this.any(n));return(n,a,u)=>{const l=c.evalName(i,n,a,u);return l instanceof M?c.invoke(l,r.map(h=>h(n,a,u)),n,a,u):m.NAME}}if((s.length<t.min||s.length>t.max)&&!(s.length===1&&s[0].t==="omit"&&t.min===0))return()=>m.VALUE;if(t.volatile&&(this.isVolatile=!0),t.readsSheets&&(this.readsSheets=!0),t.notInExcel&&(this.notInExcel=!0),t.frozen&&(this.external=!0),(typeof t.keepImportedResult=="function"?t.keepImportedResult(s):t.keepImportedResult)&&(this.keepImportedResult=!0),t.kind==="lazy"||t.kind==="raw"){const r=this.arrayCtx,n=s.map((h,f)=>h.t==="omit"&&f<t.required?()=>m.NA:(this.arrayCtx=r||t.array===!0||typeof t.array=="function"&&t.array(f),this.any(h)));this.arrayCtx=r;const{fn:a}=t;if(t.kind==="lazy")return(h,f,A)=>{try{return w(a(c,n,h,f,A))}catch(R){if(R instanceof d)return R;throw R}};const{scalars:u}=t,l=u?s.map((h,f)=>f).filter(h=>u(h,s.length,s)):null;return l!==null&&!r&&l.some(h=>(t.scalarRanges||t.rangeAt?.(h))&&this.mayYieldRange(s[h]))&&(this.mayArray=!0),(h,f,A)=>{const R=new Array(n.length);for(let E=0;E<n.length;E++)R[E]=n[E](h,f,A);try{return l!==null&&l.some(E=>c.isManyValues(R[E],t.scalarRanges||t.rangeAt?.(E),r))?c.liftScalars(t,l,R,h,f,A,r):w(a(c,R,h,f,A,r))}catch(E){if(E instanceof d)return E;throw E}}}return this.lifted(t,s)}toolpakArg(e,c){const{cx:s}=this,i=t=>typeof t=="boolean"&&!c?v:t;if(e.t!=="range"&&e.t!=="rows"&&e.t!=="cols"&&e.t!=="union"&&e.t!=="call"&&e.t!=="name"){const t=this.value(e);return(r,n,a)=>i(t(r,n,a))}const o=this.any(e);return(t,r,n)=>{const a=o(t,r,n);return a instanceof O||!s.legacy&&a instanceof y&&(a.r0!==a.r1||a.c0!==a.c1)?v:i(a instanceof y?s.refAuto(a,t,r):a)}}lifted(e,c){const{cx:s}=this,{errors:i}=e,o=e.dateSystem?(...n)=>e.fn(s.book.dateOffset,...n):e.fn,t=c.map((n,a)=>n.t==="omit"?a<e.required?()=>m.NA:()=>L:e.toolpak?this.toolpakArg(n,e.toolpak.logical.includes(a)):this.value(n)),r=t.length;if(r===1){const n=t[0];return(a,u,l)=>{const h=n(a,u,l);if(h instanceof C)return s.liftArr(o,[h],i);if(!i&&h instanceof d)return h;try{return w(o(h))}catch(f){if(f instanceof d)return f;throw f}}}if(r===2){const[n,a]=t;return(u,l,h)=>{const f=n(u,l,h),A=a(u,l,h);if(f instanceof C||A instanceof C)return s.liftArr(o,[f,A],i);if(!i){if(f instanceof d)return f;if(A instanceof d)return A}try{return w(o(f,A))}catch(R){if(R instanceof d)return R;throw R}}}return(n,a,u)=>{const l=new Array(r);let h=!1;for(let f=0;f<r;f++){const A=t[f](n,a,u);l[f]=A,A instanceof C&&(h=!0)}if(h)return s.liftArr(o,l,i);if(!i){for(let f=0;f<r;f++)if(l[f]instanceof d)return l[f]}try{return w(o(...l))}catch(f){if(f instanceof d)return f;throw f}}}compileLet(e){const{cx:c}=this;if(e.length<3||e.length%2===0)return()=>m.VALUE;const s=new Map;this.scopes.push(s);const i=[];for(let t=0;t+1<e.length;t+=2){const r=e[t];if(r.t!=="name")return this.scopes.pop(),()=>m.VALUE;const n=this.any(e[t+1]),a=this.slots++;this.mayYieldRange(e[t+1])&&this.rangeSlots.add(a),s.set(r.name.toUpperCase(),a),i.push(a,n)}const o=this.any(e[e.length-1]);return this.scopes.pop(),(t,r,n)=>{for(let a=0;a<i.length;a+=2)c.frame[i[a]]=i[a+1](t,r,n);return o(t,r,n)}}compileLambda(e){const{cx:c}=this;if(e.length===0)return()=>m.VALUE;const s=new Map;this.scopes.push(s);const i=[];for(let t=0;t<e.length-1;t++){const r=e[t];if(r.t!=="name")return this.scopes.pop(),()=>m.VALUE;const n=this.slots++;this.rangeSlots.add(n),s.set(r.name.toUpperCase(),n),i.push(n)}const o=this.any(e[e.length-1]);return this.scopes.pop(),()=>new M(i,o,c.frame)}}export{D as Compiler,k as compareValues,T as num,V as str};
|
package/calc/engine.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{resolveStructuredSpec as gt}from"../slicers/structured.js";import{Arr as A,compareValues as O,ERR as g,ERR_BY_INDEX as Q,Lambda as bt,num as N,power as wt,Ref as V,RefArray as tt,RefList as B,RichValue as H,str as lt,XlError as C}from"./values.js";import{entityVm as ot,richValueAt as Mt}from"../cells/rich-values.js";import{Compiler as ct}from"./compile.js";import{formulaEntryLimit as Rt,FormulaSyntaxError as K,Parser as J,templateKey as At,tokenize as Z,toDisplayFormula as Ct}from"./parser.js";import{BOOL as W,CHUNK_BITS as T,CHUNK_MASK as x,CONTENT_MASK as et,DIRTY as w,EMPTY as v,EVALUATING as X,FORMULA as L,KIND_MASK as E,NUM as U,SPILL as Y,STR as G,ERR as I}from"../core/sheet.js";import{colName as ht,MAX_COLS as b,MAX_ROWS as D,parseRangeRef as P}from"../core/address.js";import{CellSet as xt}from"./cell-set.js";import{isRowBelow as Tt}from"./fill-shape.js";import{FUNCS as Lt}from"./functions/registry.js";import"./functions/index.js";const St=new Set(["range","rows","cols","rangeOp","intersect","union"]);function ut(p){for(;p.t==="paren";)p=p.e;if(St.has(p.t))return!0;if(p.t!=="call")return!1;const t=Lt.get(p.name),e=t?.refResult;return(typeof e=="function"?e(p.args):e===!0)?!0:t?.passes!==void 0&&p.args.some((s,n)=>t.passes(n,p.args.length)&&ut(s))}const st=Symbol("pending"),ft=200,Et=1e6,It=4096,Nt=64,nt=2147483647,$=4;class it{constructor(){this.n=0,this.data=new Int32Array($*8)}push(t,e,s,n){if((this.n+1)*$>this.data.length){const r=new Int32Array(this.data.length*2);r.set(this.data),this.data=r}const i=this.n++*$,a=this.data;a[i]=t,a[i+1]=e,a[i+2]=s,a[i+3]=n}}function Ut(p,t){if(!p.valueMeta.has(t))return;const e=new Map(p.valueMeta);e.delete(t),p.valueMeta=e.size>0?e:null}function dt(p,t,e){if(p.valueMeta?.get(t)===e)return;const s=new Map(p.valueMeta??[]);s.set(t,e),p.valueMeta=s}function j(p,t){let e=p.get(t);return e===void 0&&(e=new xt,p.set(t,e)),e}class Ft{constructor(){this.buckets=[],this.wide=[],this.all=new it,this.allCols=[]}}class Yt{constructor(t){this.book=t,this.sheetGen=0,this.sheetMap=new Map,this.deps=new Map,this.templates=new Map,this.lastOwn={row:-2,col:-1,text:"",tpl:{cacheKey:void 0}},this.frame=null,this.depth=0,this.legacy=!1,this.pendingSheet=null,this.pendingRow=0,this.pendingCol=0,this.circular=null,this.evalStack=new Int32Array(3*256),this.evalTop=0,this.cycleLow=nt,this.iterate=null,this.cycleFound=[],this.cycleMembers=null,this.iterating=!1,this.sliceMs=10,this.syncLimit=4e3,this.externalLinks=null,this.names=new Map,this.nameTemplates=new Map,this.tables=new Map,this.volatile=new Map,this.visibilityReaders=new Map,this.sheetReaders=new Map,this.styleReaders=new Map,this.wakePending=0,this.queue=new Int32Array(4096),this.qHead=0,this.qLen=0,this.sheetsByUid=new Map,this.running=!1,this.timer=0,this.manual=!1,this.graphReady=!0,this.pendingChanges=[],this.waiters=[],this.onChange=null,this.dataTables=[],this.tablesDirty=!1,this.inTables=!1,this.tablesForced=!1,this.onProgress=null,this.lookupCache=new Map,this.colRevisions=new Map,this.channel=null,this.sheetsChanged()}orderChanged(){this.sheetsChanged(),this.has3D&&this.rebuild()}sheetsChanged(){this.sheetGen++,this.sheetMap.clear(),this.sheetsByUid.clear();for(const t of this.book.sheets)this.sheetMap.set(t.name.toUpperCase(),t),this.sheetsByUid.set(t.uid,t),t.rows.watcher=this,t.cols.watcher=this;this._wakeReaders(this.sheetReaders)}sheetByName(t){return this.sheetMap.get(t.toUpperCase())??null}externalSheet(t,e){return this.externalLinks?.sheet(t,e)??null}sheetsBetween(t,e){const{sheets:s}=this.book,n=s.findIndex(a=>a.name.toUpperCase()===t.toUpperCase()),i=s.findIndex(a=>a.name.toUpperCase()===e.toUpperCase());return n<0||i<0?null:s.slice(Math.min(n,i),Math.max(n,i)+1)}setNames(t){this.names.clear(),this.nameTemplates.clear();for(const e of t){const s=e.localSheetId!==void 0?`${e.localSheetId}!${e.name.toUpperCase()}`:e.name.toUpperCase();this.names.set(s,{text:e.text,name:e.name,hidden:e.hidden,comment:e.comment})}}setTables(t){this.tables.clear();for(const e of t)this.tables.set(e.name.toUpperCase(),e)}template(t,e,s){let n;try{n=Z(t)}catch(r){if(r instanceof K)return this._broken(t);throw r}const i=At(n,e,s);let a=this.templates.get(i);if(a===void 0){try{const r=new J(n,e,s).parse();a=this._compile(r)}catch(r){if(!(r instanceof K))throw r;a=this._broken(t)}a.cacheKey=i,this.templates.set(i,a)}return a}_ownTemplate(t,e,s){const n=this.lastOwn;let i;return n.row===e-1&&n.col===s&&this.templates.get(n.tpl.cacheKey)===n.tpl&&Tt(n.text,t)?i=n.tpl:i=this.template(t,e,s),n.row=e,n.col=s,n.text=t,n.tpl=i,i}_compile(t,e=!1){const s=new ct(this,e);return{fn:s.compileTop(t),refs:s.refs,names:s.names,tables:s.tables,isVolatile:s.isVolatile,readsSheets:s.readsSheets,notInExcel:s.notInExcel===!0,mayArray:s.mayArray,rangeResult:ut(t),external:s.external,keepImportedResult:s.keepImportedResult}}_broken(){return{invalid:!0,fn:()=>g.NAME,refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1,external:!0}}validate(t){const e=Rt(t);if(e)throw new K(e);at(new J(Z(t),0,0).parse())}_templateFor(t,e,s,n){if(n.tpl!==void 0)return n.tpl;let i;return n.t==="dataTable"?i=this._dataTableTemplate():n.f!==void 0&&n.base===void 0?i=this._ownTemplate(n.f,e,s):n.f!==void 0?i=this.template(n.f,Math.floor(n.base/b),n.base%b):n.sf!==void 0?i=this.template(n.sf,Math.floor(n.base/b),n.base%b):i=this._broken(),n.tpl=i,i}_index(t){let e=this.deps.get(t);return e===void 0&&(e=new Ft,this.deps.set(t,e)),e}_addDep(t,e,s,n,i,a,r,o){const l=this._index(t),c=a.uid<<14|o;if(i-s>=Nt){l.all.push(r,c,e,n),l.allCols.push(s,i);return}if(n-e>=It){for(let m=s;m<=i;m++){let h=l.wide[m];h===void 0&&(h=new it,l.wide[m]=h),h.push(r,c,e,n)}return}const f=e>>>T,u=n>>>T;for(let m=s;m<=i;m++){let h=l.buckets[m];h===void 0&&(h=[],l.buckets[m]=h);for(let y=f;y<=u;y++){let _=h[y];_===void 0&&(_=new it,h[y]=_),_.push(r,c,e,n)}}}register(t,e,s,n){const i=this._templateFor(t,e,s,n);if(i.external||i.keepImportedResult&&!n.typed&&(t.kind(e,s)&E)!==v)return i;for(const a of i.refs)this._addRefDep(a,t,e,s);for(const a of i.names)this._addNameDeps(a,t,e,s);for(const a of i.tables)this._addTableDep(a,t,e,s);return i.readsSheets&&j(this.sheetReaders,t).add(e,s),i.isVolatile&&j(this.volatile,t).add(e,s),i.readsVisibility??=/SUBTOTAL|AGGREGATE|SPARK_/i.test(n.f??n.sf??""),i.readsVisibility&&j(this.visibilityReaders,t).add(e,s),i.readsStyles??=/SPARK_BLOCK/i.test(n.f??n.sf??""),i.readsStyles&&j(this.styleReaders,t).add(e,s),i}_addRefDep(t,e,s,n){const i=t.wrap?(t.rAbs0?t.r0:s+t.r0)%D:t.rAbs0?t.r0:s+t.r0,a=t.wrap?(t.rAbs1?t.r1:s+t.r1)%D:t.rAbs1?t.r1:s+t.r1,r=t.wrap?(t.cAbs0?t.c0:n+t.c0)%b:t.cAbs0?t.c0:n+t.c0,o=t.wrap?(t.cAbs1?t.c1:n+t.c1)%b:t.cAbs1?t.c1:n+t.c1,l=Math.max(0,Math.min(i,a)),c=Math.min(D-1,Math.max(i,a)),f=Math.max(0,Math.min(r,o)),u=Math.min(b-1,Math.max(r,o));if(l>c||f>u)return;if(t.sheet?.sheet2&&(t.sheet.book===null||t.sheet.book===void 0)){this.has3D=!0;for(const h of this.sheetsBetween(t.sheet.sheet,t.sheet.sheet2)??[])this._addDep(h,l,f,c,u,e,s,n);return}if(t.sheet?.book!==null&&t.sheet?.book!==void 0)return;const m=t.sheet?this.sheetByName(t.sheet.sheet):e;m&&this._addDep(m,l,f,c,u,e,s,n)}_addNameDeps(t,e,s,n){const i=this._nameTemplate(t,e);if(i!==null){i.readsSheets&&j(this.sheetReaders,e).add(s,n);for(const a of i.refs)this._addRefDep(a,e,s,n);for(const a of i.tables)this._addTableDep(a,e,s,n);for(const a of i.names)a.toUpperCase()!==t.toUpperCase()&&this._addNameDeps(a,e,s,n)}}_addTableDep(t,e,s,n){const i=this.resolveTable(t.name,t.spec,s,n,e);i instanceof V&&this._addDep(i.sheet,i.r0,i.c0,i.r1,i.c1,e,s,n)}rebuild(){this.has3D=!1,this.deps.clear(),this.volatile.clear(),this.visibilityReaders.clear(),this.styleReaders.clear(),this.sheetReaders.clear(),this.lookupCache.clear(),this.qHead=0,this.qLen=0,this.cycleFound=[],this.cycleMembers=null,this.sheetGen++,this.buildGraph({fullCalc:!0})}buildGraph({fullCalc:t=!1,onDone:e}={}){this.graphReady=!1,this.dataTables=[],t&&(this.tablesDirty=!0);const s=this,n=this.sheetGen,i=this.book.sheets.map(f=>[f,f.formulas]);let a=0,r=0,o=0,l=0;const c=()=>{const f=performance.now()+s.sliceMs;let u=0;for(;a<i.length;a++,r=0,o=0,l=0){const[h,y]=i[a],{columns:_}=y;for(;r<_.length;r++,o=0,l=0){const d=_[r];if(d!==void 0)for(;o<d.length;o++,l=0){const k=d[o];if(k===void 0)continue;const{ids:R}=k,S=o<<T;for(;l<=x;l++){const M=R[l];if(M===0)continue;const F=y.infos[M],q=S+l;F.t==="dataTable"&&s.dataTables.push({sheet:h,row:q,col:r,info:F});const _t=s.register(h,q,r,F);if(s._initFormula(h,q,r,F,_t,t),++u%256===0&&performance.now()>f){l++,s.onProgress?.({preparing:!0}),s._later(c);return}}}}}s.graphReady=!0,s.sheetGen!==n&&s._wakeReaders(s.sheetReaders);const m=s.pendingChanges;s.pendingChanges=[];for(const[h,y,_,d]of m)s.cellsChanged(h,y,_,d);s._schedule(),e?.()};c()}_initFormula(t,e,s,n,i,a){const r=t._chunk(e,s);if(r===void 0)return;const o=e&x;n.t==="array"&&n.ref&&this._markArrayChildren(t,e,s,n);const l=r.kinds[o];if(i.external||i.keepImportedResult&&!n.typed&&(l&E)!==v)return;const c=n.ca==="1"||n.ca==="true";(a||c||i.isVolatile||(l&E)===v||i.notInExcel&&(l&E)===I)&&(r.kinds[o]=l|w,this._enqueue(t,e,s))}_markArrayChildren(t,e,s,n){const i=P(n.ref);if(i!==null){t.spills||(t.spills=new Map),t.spills.set(e*b+s,{...i,blocked:!1,fixed:!n.cm});for(let a=i.r0;a<=i.r1;a++)for(let r=i.c0;r<=i.c1;r++){if(a===e&&r===s)continue;const o=t._ensure(a,r);o.kinds[a&x]|=Y}}}_enqueue(t,e,s){if(this.qLen+3>this.queue.length)if(this.qHead>this.queue.length>>>1)this.queue.copyWithin(0,this.qHead,this.qLen),this.qLen-=this.qHead,this.qHead=0;else{const n=new Int32Array(this.queue.length*2);n.set(this.queue),this.queue=n}this.queue[this.qLen++]=t.uid,this.queue[this.qLen++]=e,this.queue[this.qLen++]=s}_dirtyFrom(t,e,s,n,i){const a=[t,e,s,n,i];for(;a.length>0;){const r=a.pop(),o=a.pop(),l=a.pop(),c=a.pop(),f=a.pop(),u=this.deps.get(f);if(u===void 0)continue;const m=(_,d)=>{const k=this.sheetsByUid.get(d>>>14);if(k===void 0)return;const R=d&16383,S=k._chunk(_,R);if(S===void 0)return;const M=_&x,F=S.kinds[M];(F&L)===0||(F&w)!==0||(S.kinds[M]=F|w,this._enqueue(k,_,R),a.push(k,R,R,_,_))},h=o>>>T,y=r>>>T;for(let _=c;_<=l;_++){const d=u.buckets[_];if(d!==void 0)for(let R=h;R<=y;R++){const S=d[R];if(S===void 0)continue;const{data:M}=S,F=S.n*$;for(let q=0;q<F;q+=$)r>=M[q+2]&&o<=M[q+3]&&m(M[q],M[q+1])}const k=u.wide[_];if(k!==void 0){const{data:R}=k,S=k.n*$;for(let M=0;M<S;M+=$)r>=R[M+2]&&o<=R[M+3]&&m(R[M],R[M+1])}}if(u.all.n>0){const{data:_}=u.all;for(let d=0;d<u.all.n;d++){const k=d*$;r>=_[k+2]&&o<=_[k+3]&&l>=u.allCols[d*2]&&c<=u.allCols[d*2+1]&&m(_[k],_[k+1])}}}}cellsChanged(t,e,s,n){if(n===0)return;if(t.valueListeners)for(const a of t.valueListeners)for(let r=0;r<n;r++)a(e[r],s[r],e[r],s[r]);if(this.inTables||(this.tablesDirty=!0),!this.graphReady){this.pendingChanges.push([t,e.slice(0,n),s.slice(0,n),n]);return}this.lookupCache.clear();const i=new Map;for(let a=0;a<n;a++){const r=e[a],o=s[a],l=i.get(o);l===void 0?i.set(o,[r,r]):(r<l[0]&&(l[0]=r),r>l[1]&&(l[1]=r));const c=t._chunk(r,o);if(c===void 0)continue;const f=r&x,u=c.kinds[f];if(u&L){const m=t.formula(r,o);m&&(this.register(t,r,o,m),u&w||(c.kinds[f]=u|w,this._enqueue(t,r,o)))}t.spills&&this._spillTouched(t,r,o)}for(const[a,r]of i)this._dirtyFrom(t,a,a,r[0],r[1]);this._markVolatile(),this._schedule()}_spillTouched(t,e,s){const n=e*b+s;for(const[i,a]of t.spills){const r=Math.floor(i/b),o=i-r*b;if(i===n){(t.kind(r,o)&L)===0&&(this._clearSpill(t,a,r,o),t.spills.delete(i));continue}if(e>=a.r0&&e<=a.r1&&s>=a.c0&&s<=a.c1){const l=t._chunk(r,o);if(l===void 0)continue;const c=r&x,f=l.kinds[c];f&L&&!(f&w)&&(l.kinds[c]=f|w,this._enqueue(t,r,o),this._dirtyFrom(t,o,o,r,r))}}}_markVolatile(){for(const[t,e]of this.volatile)e.forEach((s,n)=>{const i=t._chunk(s,n);if(i===void 0)return;const a=s&x,r=i.kinds[a];if(!(r&L)){e.delete(s,n);return}r&w||(i.kinds[a]=r|w,this._enqueue(t,s,n),this._dirtyFrom(t,n,n,s,s))})}get pending(){return(this.qLen-this.qHead)/3}_later(t){if(this._next=t,typeof MessageChannel>"u"){setTimeout(()=>this._tick(),0);return}this.channel===null&&(this.channel=new MessageChannel,this.channel.port1.onmessage=()=>this._tick()),this.channel.port2.postMessage(0)}_tick(){const t=this._next;this._next=null,t?.(),this._next===null&&this.channel!==null&&(this.channel.port1.close(),this.channel=null)}_schedule(){if(this.manual||this.inTables||this.running||!this.graphReady)return;if(this.pending===0){this._finish();return}const t=this.pending;if(t<=this.syncLimit&&(this._slice(this.syncLimit===1/0?1/0:this.sliceMs),this.pending===0)){this._finish();return}this.running=!0,this._total=t;const e=()=>{this._slice(this.sliceMs),this.pending>0?(this.onProgress?.({remaining:this.pending,total:Math.max(this._total,this.pending)}),this._later(e)):(this.running=!1,this._finish())};this._later(e)}_slice(t){const e=t!==1/0;let s=e?performance.now():0;const n=s+t;let i=1,a=1;for(;this.qHead<this.qLen;){const r=this.queue[this.qHead++],o=this.queue[this.qHead++],l=this.queue[this.qHead++],c=this.sheetsByUid.get(r);if(c!==void 0&&(this._run(c,o,l),e&&--a===0)){const f=performance.now();if(f>n)break;i=f-s<.5?Math.min(i*2,16):1,a=i,s=f}}this.qHead>=this.qLen&&(this.qHead=0,this.qLen=0)}_finish(){for(;this.cycleFound.length>0;)this._iterateCycles();const t=this.tablesForced;this.tablesForced=!1,this.tablesDirty&&this.dataTables.length>0&&(!this.skipTables||t)&&(this.tablesDirty=!1,this._computeDataTables()),this.book.formatValueRevision=(this.book.formatValueRevision??0)+1,this.onChange?.();const e=this.waiters;this.waiters=[];for(const s of e)s()}_dataTableTemplate(){const t=this;return{fn:(s,n,i)=>t._plainValue(i,s,n),refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1}}_plainValue(t,e,s){const n=t.value(e,s);switch(t.kind(e,s)&E){case U:return n;case G:return this.book.strings.get(n);case W:return n!==0;case I:return Q[n];default:return null}}_writePlain(t,e,s,n){const i=t.kind(e,s)&L;n==null?t.setCell(e,s,i|U,0):typeof n=="number"?Number.isFinite(n)?t.setCell(e,s,i|U,n):t.setCell(e,s,i|I,g.NUM.index):typeof n=="boolean"?t.setCell(e,s,i|W,n?1:0):n instanceof C?t.setCell(e,s,i|I,n.index):t.setCell(e,s,i|G,this.book.strings.add(String(n)))}_setInputs(t,e){const s=new Uint32Array(e.length),n=new Uint32Array(e.length);e.forEach((i,a)=>{t.setCell(i.row,i.col,i.kind,i.value),s[a]=i.row,n[a]=i.col}),this.cellsChanged(t,s,n,e.length),this._slice(1/0)}evaluateWith(t,e,s){const n=e.map(({row:a,col:r})=>({row:a,col:r,kind:t.kind(a,r)&(E|L),value:t.value(a,r)})),i=this.inTables;this.inTables=!0;try{return this._setInputs(t,e.map(({row:a,col:r,text:o})=>{const l=Number(o);return o!==""&&Number.isFinite(l)?{row:a,col:r,kind:U,value:l}:{row:a,col:r,kind:G,value:this.book.strings.add(String(o))}})),s()}finally{this._setInputs(t,n),this.inTables=i}}_computeDataTables(){this.inTables=!0;const t=this.dataTables.filter(e=>e.sheet.formula(e.row,e.col)===e.info&&this.book.sheets.includes(e.sheet));this.dataTables=t;try{for(const e of t)this._computeDataTable(e)}finally{this.inTables=!1}}_computeDataTable({sheet:t,info:e}){const s=P(e.ref);if(!s)return;const n=P(e.r1??""),i=e.r2?P(e.r2):null,a=e.dt2D==="1"||e.dt2D==="true",r=e.dtr==="1"||e.dtr==="true",o=e.del1==="1"||e.del1==="true"||a&&(e.del2==="1"||e.del2==="true");if(!o&&(!n||a&&!i))return;const l=(d,k)=>({kind:t.kind(d,k)&E,value:t.value(d,k)}),f=(o?[]:[n,i].filter(Boolean)).map(d=>({row:d.r0,col:d.c0,kind:t.kind(d.r0,d.c0)&(E|L),value:t.value(d.r0,d.c0)})),u=[],m=(d,k,R,S)=>{const M=this.read(t,R,S);u.push(d,k,M instanceof A?M.data[0]:M)};try{if(o)for(let d=s.r0;d<=s.r1;d++)for(let k=s.c0;k<=s.c1;k++)u.push(d,k,g.REF);else if(a)for(let d=s.r0;d<=s.r1;d++)for(let k=s.c0;k<=s.c1;k++)this._setInputs(t,[{row:n.r0,col:n.c0,...l(s.r0-1,k)},{row:i.r0,col:i.c0,...l(d,s.c0-1)}]),m(d,k,s.r0-1,s.c0-1);else if(r)for(let d=s.c0;d<=s.c1;d++){this._setInputs(t,[{row:n.r0,col:n.c0,...l(s.r0-1,d)}]);for(let k=s.r0;k<=s.r1;k++)m(k,d,k,s.c0-1)}else for(let d=s.r0;d<=s.r1;d++){this._setInputs(t,[{row:n.r0,col:n.c0,...l(d,s.c0-1)}]);for(let k=s.c0;k<=s.c1;k++)m(d,k,s.r0-1,k)}}finally{f.length>0&&this._setInputs(t,f)}const h=u.length/3,y=new Uint32Array(h),_=new Uint32Array(h);for(let d=0;d<h;d++)y[d]=u[d*3],_[d]=u[d*3+1],this._writePlain(t,y[d],_[d],u[d*3+2]);this.cellsChanged(t,y,_,h),this._slice(1/0),t.revision++}dataTableAt(t,e,s){for(const n of this.dataTables){if(n.sheet!==t)continue;const i=P(n.info.ref);if(i&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1)return n}return null}calculateNow(){const{manual:t,skipTables:e}=this;this.manual=!1,this.skipTables=!1,this.tablesDirty=!0,this.tablesForced=!0,this._markVolatile(),this._markCycles(),this._schedule(),this.manual=t,this.skipTables=e}whenIdle(){return this.pending===0&&this.graphReady&&!this.running?Promise.resolve():new Promise(t=>{this.waiters.push(t),this._schedule()})}_iterateCycles(){const t=this.cycleFound;this.cycleFound=[];const e=new Map,s=(r,o,l)=>{let c=e.get(r.uid);c===void 0&&(c=new Map,e.set(r.uid,c)),c.set(o*b+l,[r,o,l])};for(let r=0;r<t.length;r+=3)s(t[r],t[r+1],t[r+2]);const n=[];for(const r of this.book.sheets){const o=e.get(r.uid);o&&n.push(...[...o.keys()].sort((l,c)=>l-c).map(l=>o.get(l)))}this.cycleMembers??=new Map;for(const r of n)this.cycleMembers.set(`${r[0].uid}:${r[1]}:${r[2]}`,r);const{count:i,delta:a}=this.iterate;for(let r=1;r<=i;r++){let o=0;for(const[l,c,f]of n){const u=l._chunk(c,f);if(u===void 0)continue;const m=c&x;if(!(u.kinds[m]&L))continue;const h=u.values[m];u.kinds[m]|=w,this.iterating=!0,this._run(l,c,f),this.iterating=!1;const y=Math.abs(u.values[m]-h);y>o&&(o=y)}if(this.onProgress?.({iteration:r,iterations:i}),o<a)break}for(const[r,o,l]of n)this._dirtyFrom(r,l,l,o,o);for(const[r,o,l]of n){const c=r._chunk(o,l);c!==void 0&&(c.kinds[o&x]&=~w)}this._slice(1/0)}_markCycles(){if(!(this.iterate===null||this.cycleMembers===null))for(const[t,e,s]of this.cycleMembers.values()){const n=t._chunk(e,s);n===void 0||!(n.kinds[e&x]&L)||(n.kinds[e&x]|=w,this._enqueue(t,e,s))}}_run(t,e,s){const n=[t,e,s];for(;n.length>0;){const i=n[n.length-1],a=n[n.length-2],r=n[n.length-3];try{this.depth=0,this.evalTop=0,this.cycleLow=nt,this._calc(r,a,i),n.length-=3}catch(o){if(o!==st)throw o;n.push(this.pendingSheet,this.pendingRow,this.pendingCol)}}}_calc(t,e,s){const n=t._chunk(e,s);if(n===void 0)return;const i=e&x,a=n.kinds[i];if(!(a&w))return;if(a&X){this.circular={sheet:t,row:e,col:s},this.iterating||this._cycleHit(t.uid,e,s);return}if(this.depth>=ft)throw this.pendingSheet=t,this.pendingRow=e,this.pendingCol=s,st;const r=t.formula(e,s);if(r===void 0||!(a&L)){n.kinds[i]=a&~w;return}const o=this._templateFor(t,e,s,r);if(o.external||o.keepImportedResult&&!r.typed&&(a&E)!==v){n.kinds[i]=a&~w;return}n.kinds[i]=a|X;const l=this.evalTop;l+3>this.evalStack.length&&this._growEvalStack(),this.evalStack[l]=t.uid,this.evalStack[l+1]=e,this.evalStack[l+2]=s,this.evalTop=l+3;const c=this.cycleLow;this.cycleLow=nt;const f=this.legacy,u=this.frame,m=r.typed!==!0&&r.t!=="array";this.legacy=m,this.depth++;let h;try{h=o.fn(e,s,t),(h instanceof V||h instanceof B)&&(h=this.refAuto(h,e,s)),h instanceof tt&&(h=this.toArr(h))}catch(_){if(n.kinds[i]&=~X,this.depth--,this.legacy=f,this.frame=u,this.evalTop=l,this.cycleLow=c,_===st)throw _;_ instanceof C?h=_:(console.error("Formula evaluation failed",_),h=g.VALUE),this._store(t,n,i,e,s,h,r,m);return}this.depth--,this.legacy=f,this.frame=u,this.evalTop=l;const y=this.cycleLow;if(this.cycleLow=y<l&&y<c?y:c,y<=l){if(this.iterate!==null){n.kinds[i]=a&~(w|X),this.cycleFound.push(t,e,s);return}if(r.typed===void 0&&(a&E)!==v){n.kinds[i]=a&~(w|X);return}h=0}this._store(t,n,i,e,s,h,r,m)}_cycleHit(t,e,s){const n=this.evalStack;for(let i=this.evalTop-3;i>=0;i-=3)if(n[i]===t&&n[i+1]===e&&n[i+2]===s){i<this.cycleLow&&(this.cycleLow=i);return}}_growEvalStack(){const t=new Int32Array(this.evalStack.length*2);t.set(this.evalStack),this.evalStack=t}_store(t,e,s,n,i,a,r,o){let l=a;l instanceof bt&&(l=g.CALC),l instanceof A?r.t==="array"&&!r.cm&&r.ref?l=this._fillFixedArray(t,n,i,r,l):o||l.spillRows===1&&l.spillCols===1?(l=l.data[0],t.spills&&this._releaseSpill(t,n,i)):l=this._spill(t,n,i,r,l):t.spills&&!(r.t==="array"&&!r.cm)&&this._releaseSpill(t,n,i),r.t==="array"&&!r.cm&&r.ref&&!(a instanceof A)&&(l=this._fillFixedArray(t,n,i,r,A.filled(1,1,l)));let c=L,f=0;if(typeof l=="number"?Number.isFinite(l)?(c|=U,f=l):(c|=I,f=g.NUM.index):typeof l=="string"?(c|=G,f=this.book.strings.add(l)):typeof l=="boolean"?(c|=W,f=l?1:0):l instanceof C?(c|=I,f=l.index):l instanceof H?(c|=I,f=g.VALUE.index):c|=U,l instanceof H?dt(t,n*b+i,ot(this.book,l.index)):t.valueMeta!==null&&((e.kinds[s]&et)!==c||e.values[s]!==f)&&!/IMAGE\(/i.test(r.f??r.sf??"")&&Ut(t,n*b+i),t.valueListeners&&((e.kinds[s]&et)!==c||e.values[s]!==f))for(const u of t.valueListeners)u(n,i,n,i);e.kinds[s]=c,e.values[s]=f,n>t.maxRow&&(t.maxRow=n),i>t.maxCol&&(t.maxCol=i)}_writeArrayCell(t,e,s,n){let i=Y,a=0;typeof n=="number"?(i|=Number.isFinite(n)?U:I,a=Number.isFinite(n)?n:g.NUM.index):typeof n=="string"?(i|=G,a=this.book.strings.add(n)):typeof n=="boolean"?(i|=W,a=n?1:0):n instanceof C?(i|=I,a=n.index):n==null?i|=U:(i|=I,a=g.VALUE.index,n instanceof H&&dt(t,e*b+s,ot(this.book,n.index))),t.setCell(e,s,i,a)}_fillFixedArray(t,e,s,n,i){const a=P(n.ref);if(a===null)return i.data[0];t.spills||(t.spills=new Map),t.spills.set(e*b+s,{...a,blocked:!1,fixed:!0});const r=(l,c)=>{const f=i.rows===1?0:l,u=i.cols===1?0:c;return f>=i.rows||u>=i.cols?g.NA:i.get(f,u)};for(let l=a.r0;l<=a.r1;l++)for(let c=a.c0;c<=a.c1;c++)l===e&&c===s||this._writeArrayCell(t,l,c,r(l-a.r0,c-a.c0));(a.r0!==a.r1||a.c0!==a.c1)&&this._dirtyFrom(t,a.c0,a.c1,a.r0,a.r1);const o=r(e-a.r0,s-a.c0);return o===null?0:o}_spill(t,e,s,n,i){const a=e*b+s;t.spills||(t.spills=new Map);const r=t.spills.get(a),o=e+i.rows-1,l=s+i.cols-1,c={r0:e,c0:s,r1:o,c1:l,blocked:!1,fixed:!1},f=(h,y)=>r&&h>=r.r0&&h<=r.r1&&y>=r.c0&&y<=r.c1;let u=e+i.spillRows>D||s+i.spillCols>b;if(!u)t:for(let h=e;h<=o;h++)for(let y=s;y<=l;y++){if(h===e&&y===s)continue;const _=t.kind(h,y);if((_&et)!==0&&!(_&Y&&f(h,y))){u=!0;break t}if(t.merges.length>0&&t.mergeAt(h,y)){u=!0;break t}}if(u)return r&&!r.blocked&&this._clearSpill(t,r,e,s),c.blocked=!0,c.r1=Math.min(o,D-1),c.c1=Math.min(l,b-1),t.spills.set(a,c),g.SPILL;if(r&&!r.blocked){for(let h=r.r0;h<=r.r1;h++)for(let y=r.c0;y<=r.c1;y++)h>=c.r0&&h<=c.r1&&y>=c.c0&&y<=c.c1||(t.kind(h,y)&Y)!==0&&t.setCell(h,y,v,0);this._dirtyFrom(t,r.c0,r.c1,r.r0,r.r1)}t.spills.set(a,c);for(let h=e;h<=o;h++)for(let y=s;y<=l;y++)h===e&&y===s||this._writeArrayCell(t,h,y,i.get(h-e,y-s));n.t="array",n.ref=`${pt(s)}${e+1}:${pt(l)}${o+1}`,n.cm=1,delete n.raw,this._dirtyFrom(t,s,l,e,o);const m=i.data[0];return m===null?0:m}_clearSpill(t,e,s,n){for(let i=e.r0;i<=e.r1;i++)for(let a=e.c0;a<=e.c1;a++)i===s&&a===n||(t.kind(i,a)&Y)!==0&&t.setCell(i,a,v,0);this._dirtyFrom(t,e.c0,e.c1,e.r0,e.r1)}_releaseSpill(t,e,s){const n=e*b+s,i=t.spills.get(n);if(i===void 0)return;i.blocked||this._clearSpill(t,i,e,s),t.spills.delete(n);const a=t.formula(e,s);a&&a.typed&&(delete a.ref,delete a.cm,delete a.t)}spillMaster(t,e,s){if(!t.spills)return null;for(const[n,i]of t.spills)if(!i.blocked&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1){const a=Math.floor(n/b);return{row:a,col:n-a*b,rect:i}}return null}spillRange(t){const{sheet:e,r0:s,c0:n}=t;this.read(e,s,n);const i=e.spills?.get(s*b+n);return!i||i.blocked?g.REF:new V(e,i.r0,i.c0,i.r1,i.c1)}read(t,e,s){const n=t.columns[s];if(n===void 0)return null;const i=n[e>>>T];if(i===void 0)return null;const a=e&x;let r=i.kinds[a];if(r===0)return null;switch(r&w&&(this._calc(t,e,s),r=i.kinds[a]),r&E){case U:return i.values[a];case G:return this.book.strings.get(i.values[a]);case W:return i.values[a]!==0;case I:{if(t.valueMeta!==null&&this.book.richValues){const o=Mt(this.book,t,e,s);if(o!==null)return o}return Q[i.values[a]]??g.NA}default:return null}}refToValue(t){return t instanceof B?t.refs.length===1?this.refToValue(t.refs[0]):g.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.toArr(t)}refAuto(t,e,s){return t instanceof B?t.refs.length===1?this.refAuto(t.refs[0],e,s):g.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.legacy?this.intersect(t,e,s):this.toArr(t)}intersect(t,e,s){return t.cols===1&&e>=t.r0&&e<=t.r1?this.read(t.sheet,e,t.c0):t.rows===1&&s>=t.c0&&s<=t.c1?this.read(t.sheet,t.r0,s):g.VALUE}trim(t){const{sheet:e}=t;return{r0:t.r0,c0:t.c0,r1:Math.min(t.r1,Math.max(e.maxRow,t.r0)),c1:Math.min(t.c1,Math.max(e.maxCol,t.c0))}}toArr(t){if(t instanceof tt)return new A(t.rows,t.cols,t.data.map(e=>e instanceof V?this.read(e.sheet,e.r0,e.c0):e));if(t instanceof A)return t;if(t instanceof V){const e=t.rows===D||t.cols===b,{r0:s,c0:n,r1:i,c1:a}=e||t.rows*t.cols>Et?this.trim(t):t,r=i-s+1,o=a-n+1,l=new Array(r*o);let c=0;for(let f=s;f<=i;f++)for(let u=n;u<=a;u++)l[c++]=this.read(t.sheet,f,u);return new A(r,o,l,t.rows===D?D:r,t.cols===b?b:o)}if(t instanceof B){if(t.refs.length===1)return this.toArr(t.refs[0]);throw g.VALUE}if(t instanceof C)throw t;return new A(1,1,[t])}scalar(t,e,s){return t instanceof V?t.isCell?this.read(t.sheet,t.r0,t.c0):this.intersect(t,e,s):t instanceof B?g.VALUE:t instanceof tt?this.scalar(t.data[0],e,s):t instanceof A?t.data[0]:t}scan(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);for(let a=t.c0;a<=n;a++){const r=s.columns[a];if(r!==void 0)for(let o=t.r0>>>T;o<=i>>>T;o++){const l=r[o];if(l===void 0)continue;const c=o<<T,f=Math.max(t.r0-c,0),u=Math.min(i-c,x),{kinds:m}=l;for(let h=f;h<=u;h++){if(m[h]===0)continue;const y=this.read(s,c+h,a);y!==null&&e(y,c+h,a)}}}}sumRange(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);let{sum:a,count:r}=e;for(let o=t.c0;o<=n;o++){const l=s.columns[o];if(l!==void 0)for(let c=t.r0>>>T;c<=i>>>T;c++){const f=l[c];if(f===void 0)continue;const u=c<<T,m=Math.max(t.r0-u,0),h=Math.min(i-u,x),{kinds:y,values:_}=f;for(let d=m;d<=h;d++){let k=y[d];if(k===0)continue;k&w&&(this._calc(s,u+d,o),k=y[d]);const R=k&E;if(R===U)a+=_[d],r++;else if(R===I)throw Q[_[d]]}}}e.sum=a,e.count=r}averageRange(t,e){if(t.cols===1)return this.sumRange(t,e);const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow),a=[],r=[];for(let o=t.r0>>>T;o<=i>>>T;o++){a.length=0,r.length=0;for(let u=t.c0;u<=n;u++){const m=s.columns[u]?.[o];m!==void 0&&(a.push(u),r.push(m))}if(r.length===0)continue;const l=o<<T,c=Math.max(t.r0-l,0),f=Math.min(i-l,x);for(let u=c;u<=f;u++)for(let m=0;m<r.length;m++){const{kinds:h,values:y}=r[m];h[u]&w&&this._calc(s,l+u,a[m]);const _=h[u]&E;if(_===U)e.sum+=y[u],e.count++;else if(_===I)throw Q[y[u]]}}}unary(t,e){if(t instanceof C)return t;if(t instanceof A)return new A(t.rows,t.cols,t.data.map(s=>{try{return s instanceof C?s:e(N(s))}catch(n){return n}}),t.spillRows,t.spillCols);try{return e(N(t))}catch(s){if(s instanceof C)return s;throw s}}slowBin(t,e,s){if(e instanceof A||s instanceof A)return this._binArr(t,e,s);if(e instanceof C)return e;if(s instanceof C)return s;try{return mt(t,e,s)}catch(n){if(n instanceof C)return n;throw n}}_binArr(t,e,s){const n=e instanceof A?e:null,i=s instanceof A?s:null,a=Math.max(n?n.rows:1,i?i.rows:1),r=Math.max(n?n.cols:1,i?i.cols:1),o=new Array(a*r);for(let l=0;l<a;l++)for(let c=0;c<r;c++){const f=rt(e,l,c),u=rt(s,l,c);let m;if(f instanceof C)m=f;else if(u instanceof C)m=u;else try{m=mt(t,f,u)}catch(h){m=h}o[l*r+c]=m}return new A(a,r,o,Math.max(n?.spillRows??1,i?.spillRows??1),Math.max(n?.spillCols??1,i?.spillCols??1))}liftArr(t,e,s){let n=1,i=1,a=1,r=1;for(const f of e)f instanceof A&&(f.rows>n&&(n=f.rows),f.cols>i&&(i=f.cols),f.spillRows>a&&(a=f.spillRows),f.spillCols>r&&(r=f.spillCols));const o=new Array(n*i),l=e.length,c=new Array(l);for(let f=0;f<n;f++)for(let u=0;u<i;u++){let m=null;for(let h=0;h<l;h++){const y=rt(e[h],f,u);c[h]=y,!s&&m===null&&y instanceof C&&(m=y)}if(m!==null){o[f*i+u]=m;continue}try{o[f*i+u]=t(...c)}catch(h){if(h instanceof C)o[f*i+u]=h;else throw h}}return new A(n,i,o,a,r)}invoke(t,e,s,n,i){if(t.native)return this._invokeNative(t.native,e,s,n,i);const a=t.frame?t.frame.slice():[],{params:r}=t;if(e.length>r.length)return g.VALUE;for(let l=0;l<r.length;l++)a[r[l]]=l<e.length?e[l]:void 0;const o=this.frame;if(this.frame=a,this.depth++,this.depth>=ft*4)return this.depth--,this.frame=o,g.NUM;try{return t.body(s,n,i)}finally{this.depth--,this.frame=o}}_invokeNative(t,e,s,n,i){if(e.length<t.min||e.length>t.max)return g.VALUE;try{if(t.kind==="raw")return t.fn(this,e,s,n,i);if(t.kind==="lazy")return t.fn(this,e.map(o=>()=>o),s,n,i);const a=t.dateSystem?(...o)=>t.fn(this.book.dateOffset,...o):t.fn,r=e.map(o=>o instanceof V||o instanceof B?this.refToValue(o):o);if(r.some(o=>o instanceof A))return this.liftArr(a,r,t.errors);if(!t.errors){for(const o of r)if(o instanceof C)return o}return a(...r)}catch(a){if(a instanceof C)return a;throw a}}_nameEntry(t,e){const s=t.toUpperCase();if(e){const i=this.book.sheets.indexOf(e),a=this.names.get(`${i}!${s}`);if(a)return{key:`${i}!${s}`,entry:a}}const n=this.names.get(s);return n?{key:s,entry:n}:null}_nameTemplate(t,e){const s=this._nameEntry(t,e);if(s===null)return null;let n=this.nameTemplates.get(s.key);if(n===void 0){try{n=this._compile(new J(Z(s.entry.text,!0),0,0).parse(!0),!0)}catch(i){if(!(i instanceof K))throw i;n=this._broken()}this.nameTemplates.set(s.key,n)}return n}nameMayYieldRange(t){const e=t.toUpperCase(),s=[];for(const[n,i]of this.names)(n===e||n.endsWith(`!${e}`))&&s.push(i);if(s.length===0||(this._rangeNames??=new Set,this._rangeNames.has(e)))return!0;this._rangeNames.add(e);try{return s.some(n=>{try{return new ct(this).mayYieldRange(new J(Z(n.text,!0),0,0).parse(!0))}catch(i){if(i instanceof K)return!1;throw i}})}finally{this._rangeNames.delete(e)}}evalName(t,e,s,n){const i=this._nameTemplate(t,n);return i===null?g.NAME:i.fn(e,s,n)}tableSpecToA1(t,e,s){const n=kt(t,e,s);if(!(n instanceof V))return null;const i=/#This Row|^\[\s*@/i.test(e),a=(r,o)=>i?`${ht(o)}${r+1}`:`$${ht(o)}$${r+1}`;return n.isCell?a(n.r0,n.c0):`${a(n.r0,n.c0)}:${a(n.r1,n.c1)}`}resolveTable(t,e,s,n,i){let a=t?this.tables.get(t.toUpperCase()):null;if(!t){for(const r of this.tables.values())if(r.sheet===i&&s>=r.r0&&s<=r.r1&&n>=r.c0&&n<=r.c1){a=r;break}}return a?kt(a,e,s):g.REF}exactIndex(t){const e=`${t.sheet.uid}:${t.r0}:${t.c0}:${t.r1}:${t.c1}`;let s=this.lookupCache.get(e);if(s!==void 0)return s;s=new Map;const n=t.cols===1;return this.scan(t,(i,a,r)=>{const o=typeof i=="string"?`s${i.toLowerCase()}`:typeof i=="number"?i:`b${i}`;if(i instanceof C)return;const l=n?a-t.r0:r-t.c0,c=s.get(o);(c===void 0||l<c)&&s.set(o,l)}),this.lookupCache.set(e,s),s}visibilityChanged(){this._wakeReaders(this.visibilityReaders)}presentationChanged(){this._wakeLater(2)}axisHiddenChanged(){this._wakeLater(1)}axisResized(){this._wakeLater(2)}_wakeLater(t){(t===1?this.visibilityReaders:this.styleReaders).size!==0&&(this.wakePending===0&&queueMicrotask(()=>{const e=this.wakePending;this.wakePending=0,e&1&&this._wakeReaders(this.visibilityReaders),e&2&&this._wakeReaders(this.styleReaders)}),this.wakePending|=t)}_wakeReaders(t){if(!this.graphReady||t.size===0)return;let e=!1;for(const[s,n]of t)n.forEach((i,a)=>{const r=s._chunk(i,a);if(r===void 0)return;const o=i&x,l=r.kinds[o];if(!(l&L)){n.delete(i,a);return}l&w||(r.kinds[o]=l|w,this._enqueue(s,i,a)),this._dirtyFrom(s,a,a,i,i),e=!0});e&&this._schedule()}isSubtotalCell(t,e,s){if((t.kind(e,s)&L)===0)return!1;const n=t.formula(e,s),i=n?.f??n?.sf??"";return/^\s*(_xlfn\.)?(SUBTOTAL|AGGREGATE)\(/i.test(i)}displayFormula(t){return Ct(t)}}function pt(p){let t="";for(let e=p+1;e>0;e=Math.floor((e-1)/26))t=String.fromCharCode(65+(e-1)%26)+t;return t}function rt(p,t,e){if(!(p instanceof A))return p;const s=p.rows===1?0:t,n=p.cols===1?0:e;return s>=p.rows||n>=p.cols?g.NA:p.data[s*p.cols+n]}function mt(p,t,e){if((t instanceof H||e instanceof H)&&p!=="="&&p!=="<>"&&p!=="&")throw g.VALUE;switch(p){case"+":{const s=N(t),n=s+N(e);return Math.abs(n)<Math.abs(s)*yt?0:z(n)}case"-":{const s=N(t),n=s-N(e);return Math.abs(n)<Math.abs(s)*yt?0:z(n)}case"*":return z(N(t)*N(e));case"/":{const s=N(t),n=N(e);if(n===0)throw g.DIV0;return z(s/n)}case"^":{const s=N(t),n=N(e);if(s===0&&n===0)throw g.NUM;if(s===0&&n<0)throw g.DIV0;const i=wt(s,n);if(Number.isNaN(i))throw g.NUM;return z(i)}case"&":return lt(t)+lt(e);case"=":return O(t,e)===0;case"<>":return O(t,e)!==0;case"<":return O(t,e)<0;case">":return O(t,e)>0;case"<=":return O(t,e)<=0;case">=":return O(t,e)>=0;default:throw g.VALUE}}const yt=5e-16;function z(p){if(!Number.isFinite(p))throw g.NUM;return p}function kt(p,t,e){const s=gt(p,t,e);return s==="REF"?g.REF:s==="VALUE"?g.VALUE:new V(p.sheet,s.top,s.left,s.bottom,s.right)}const qt={COUNTIF:[0],COUNTIFS:["even",0],SUMIF:[0,2],SUMIFS:[0,"odd",1],AVERAGEIF:[0,2],AVERAGEIFS:[0,"odd",1],MAXIFS:[0,"odd",1],MINIFS:[0,"odd",1],COUNTBLANK:[0],RANK:[1],"RANK.EQ":[1],"RANK.AVG":[1]};function Vt(p,t){for(let e=0;e<p.length;e++){const s=p[e];if(s===t||s==="odd"&&t>=p[e+1]&&t%2===1||s==="even"&&t>=p[e+1]&&t%2===0)return!0}return!1}function at(p){if(!(p===null||typeof p!="object")){if(Array.isArray(p)){for(const t of p)at(t);return}if(p.t==="call"){const t=qt[String(p.name).replace(/^_xl(?:fn|ws)\./i,"").toUpperCase()];t&&p.args.forEach((e,s)=>{if(e?.t==="array"&&Vt(t,s))throw new Error(`${p.name} takes a range here`)})}for(const t of Object.values(p))t!==null&&typeof t=="object"&&at(t)}}export{Yt as Engine};
|
|
1
|
+
import{resolveStructuredSpec as At}from"../slicers/structured.js";import{Arr as R,compareValues as P,ERR as w,ERR_BY_INDEX as st,Lambda as Rt,num as F,OMITTED as Ct,power as xt,Ref as U,RefArray as nt,RefList as $,RichValue as j,str as ht,within as z,XlError as C}from"./values.js";import{entityVm as ft,richValueAt as Tt}from"../cells/rich-values.js";import{Compiler as dt}from"./compile.js";import{formulaEntryLimit as Lt,FormulaSyntaxError as H,Parser as W,templateKey as St,tokenize as Y,toDisplayFormula as Et}from"./parser.js";import{BOOL as Q,CHUNK_BITS as I,CHUNK_MASK as x,CONTENT_MASK as J,DIRTY as M,EMPTY as v,EVALUATING as K,FORMULA as L,KIND_MASK as S,NUM as V,SPILL as Z,STR as X,ERR as N}from"../core/sheet.js";import{displayedValue as pt}from"../core/precision.js";import{cellRef as It,colName as mt,MAX_COLS as b,MAX_ROWS as G,parseRangeRef as B}from"../core/address.js";import{CellSet as Nt}from"./cell-set.js";import{isRowBelow as Ut}from"./fill-shape.js";import{FUNCS as Ft}from"./functions/registry.js";import"./functions/index.js";const Vt=new Set(["range","rows","cols","rangeOp","intersect","union"]);function yt(m){for(;m.t==="paren";)m=m.e;if(Vt.has(m.t))return!0;if(m.t!=="call")return!1;const t=Ft.get(m.name),e=t?.refResult;return(typeof e=="function"?e(m.args):e===!0)?!0:t?.passes!==void 0&&m.args.some((s,n)=>t.passes(n,m.args.length)&&yt(s))}const rt=Symbol("pending"),kt=200,Dt=1e6,qt=4096,vt=64,lt=2147483647,Gt=3,at=(m,t,e)=>(m*G+t)*b+e,O=4;class ot{constructor(){this.n=0,this.data=new Int32Array(O*8)}push(t,e,s,n){if((this.n+1)*O>this.data.length){const r=new Int32Array(this.data.length*2);r.set(this.data),this.data=r}const i=this.n++*O,l=this.data;l[i]=t,l[i+1]=e,l[i+2]=s,l[i+3]=n}}function $t(m,t){if(!m.valueMeta.has(t))return;const e=new Map(m.valueMeta);e.delete(t),m.valueMeta=e.size>0?e:null}function _t(m,t,e){if(m.valueMeta?.get(t)===e)return;const s=new Map(m.valueMeta??[]);s.set(t,e),m.valueMeta=s}function tt(m,t){let e=m.get(t);return e===void 0&&(e=new Nt,m.set(t,e)),e}class Ot{constructor(){this.buckets=[],this.wide=[],this.all=new ot,this.allCols=[]}}class se{constructor(t){this.book=t,this.sheetGen=0,this.sheetMap=new Map,this.deps=new Map,this.templates=new Map,this.lastOwn={row:-2,col:-1,text:"",tpl:{cacheKey:void 0}},this.frame=null,this.depth=0,this.legacy=!1,this.pendingSheet=null,this.pendingRow=0,this.pendingCol=0,this.circular=null,this.evalStack=new Int32Array(3*256),this.evalTop=0,this.cycleLow=lt,this.iterate=null,this.cycleFound=[],this.cycleMembers=null,this.iterating=!1,this.sliceMs=10,this.syncLimit=4e3,this.externalLinks=null,this.names=new Map,this.nameTemplates=new Map,this.tables=new Map,this.volatile=new Map,this.precise=!1,this.sharedRows=!1,this.visibilityReaders=new Map,this.sheetReaders=new Map,this.styleReaders=new Map,this.wakePending=0,this.queue=new Int32Array(4096),this.qHead=0,this.hullAreas=null,this.late=null,this.lateRuns=null,this.qLen=0,this.sheetsByUid=new Map,this.running=!1,this.timer=0,this.manual=!1,this.calcOnSave=!0,this.graphReady=!0,this.pendingChanges=[],this.waiters=[],this.onChange=null,this.dataTables=[],this.tablesDirty=!1,this.inTables=!1,this.trial=null,this.tablesForced=!1,this.onProgress=null,this.lookupCache=new Map,this.colRevisions=new Map,this.channel=null,this.sheetsChanged()}orderChanged(){this.sheetsChanged(),this.has3D&&this.rebuild()}sheetsChanged(){this.sheetGen++,this.sheetMap.clear(),this.sheetsByUid.clear();for(const t of this.book.sheets)this.sheetMap.set(t.name.toUpperCase(),t),this.sheetsByUid.set(t.uid,t),t.rows.watcher=this,t.cols.watcher=this;this._wakeReaders(this.sheetReaders)}sheetByName(t){return this.sheetMap.get(t.toUpperCase())??null}externalName(t,e){const s=this.externalLinks?.range(t,e);return s?new U(s.sheet,s.r0,s.c0,s.r1,s.c1):null}externalSheet(t,e){return this.externalLinks?.sheet(t,e)??null}sheetsBetween(t,e){const{sheets:s}=this.book,n=s.findIndex(l=>l.name.toUpperCase()===t.toUpperCase()),i=s.findIndex(l=>l.name.toUpperCase()===e.toUpperCase());return n<0||i<0?null:s.slice(Math.min(n,i),Math.max(n,i)+1)}setNames(t){this.names.clear(),this.nameTemplates.clear();for(const e of t){const s=e.localSheetId!==void 0?`${e.localSheetId}!${e.name.toUpperCase()}`:e.name.toUpperCase();this.names.set(s,{text:e.text,name:e.name,hidden:e.hidden,comment:e.comment})}}setTables(t){this.tables.clear();for(const e of t)this.tables.set(e.name.toUpperCase(),e)}template(t,e,s){let n;try{n=Y(t)}catch(r){if(r instanceof H)return this._broken(t);throw r}const i=St(n,e,s);let l=this.templates.get(i);if(l===void 0){try{const r=new W(n,e,s).parse();l=this._compile(r)}catch(r){if(!(r instanceof H))throw r;l=this._broken(t)}l.cacheKey=i,this.templates.set(i,l)}return l}_ownTemplate(t,e,s){const n=this.lastOwn;let i;return n.row===e-1&&n.col===s&&this.templates.get(n.tpl.cacheKey)===n.tpl&&Ut(n.text,t)?i=n.tpl:i=this.template(t,e,s),n.row=e,n.col=s,n.text=t,n.tpl=i,i}_compile(t,e=!1){const s=new dt(this,e);return{fn:s.compileTop(t),refs:s.refs,hulls:s.hulls.length>0?s.hulls:null,names:s.names,tables:s.tables,isVolatile:s.isVolatile,readsSheets:s.readsSheets,notInExcel:s.notInExcel===!0,mayArray:s.mayArray,rangeResult:yt(t),external:s.external,keepImportedResult:s.keepImportedResult}}_broken(){return{invalid:!0,fn:()=>w.NAME,refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1,external:!0}}validate(t){const e=Lt(t);if(e)throw new H(e);ut(new W(Y(t),0,0).parse())}_templateFor(t,e,s,n){if(n.tpl!==void 0)return n.tpl;let i;return n.t==="dataTable"?i=this._dataTableTemplate():n.f!==void 0&&n.base===void 0?i=this._ownTemplate(n.f,e,s):n.f!==void 0?i=this.template(n.f,Math.floor(n.base/b),n.base%b):n.sf!==void 0?i=this.template(n.sf,Math.floor(n.base/b),n.base%b):i=this._broken(),n.tpl=i,i}_index(t){let e=this.deps.get(t);return e===void 0&&(e=new Ot,this.deps.set(t,e)),e}_addDep(t,e,s,n,i,l,r,o){if(this.hullAreas!==null){this.hullAreas.push([t,e,s,n,i]);return}const a=this._index(t),c=l.uid<<14|o;if(i-s>=vt){a.all.push(r,c,e,n),a.allCols.push(s,i);return}if(n-e>=qt){for(let d=s;d<=i;d++){let p=a.wide[d];p===void 0&&(p=new ot,a.wide[d]=p),p.push(r,c,e,n)}return}const h=e>>>I,f=n>>>I;for(let d=s;d<=i;d++){let p=a.buckets[d];p===void 0&&(p=[],a.buckets[d]=p);for(let _=h;_<=f;_++){let y=p[_];y===void 0&&(y=new ot,p[_]=y),y.push(r,c,e,n)}}}register(t,e,s,n){const i=this._templateFor(t,e,s,n);if(i.external||i.keepImportedResult&&!n.typed&&(t.kind(e,s)&S)!==v)return i;for(const l of i.refs)this._addRefDep(l,t,e,s);for(const l of i.names)this._addNameDeps(l,t,e,s);for(const l of i.tables)this._addTableDep(l,t,e,s);if(i.hulls)for(const l of i.hulls)this._addHullDep(l,t,e,s);return i.readsSheets&&tt(this.sheetReaders,t).add(e,s),i.isVolatile&&tt(this.volatile,t).add(e,s),i.readsVisibility??=/SUBTOTAL|AGGREGATE|SPARK_/i.test(n.f??n.sf??""),i.readsVisibility&&tt(this.visibilityReaders,t).add(e,s),i.readsStyles??=/SPARK_BLOCK/i.test(n.f??n.sf??""),i.readsStyles&&tt(this.styleReaders,t).add(e,s),i}_addRefDep(t,e,s,n){const i=t.wrap?(t.rAbs0?t.r0:s+t.r0)%G:t.rAbs0?t.r0:s+t.r0,l=t.wrap?(t.rAbs1?t.r1:s+t.r1)%G:t.rAbs1?t.r1:s+t.r1,r=t.wrap?(t.cAbs0?t.c0:n+t.c0)%b:t.cAbs0?t.c0:n+t.c0,o=t.wrap?(t.cAbs1?t.c1:n+t.c1)%b:t.cAbs1?t.c1:n+t.c1,a=Math.max(0,Math.min(i,l)),c=Math.min(G-1,Math.max(i,l)),h=Math.max(0,Math.min(r,o)),f=Math.min(b-1,Math.max(r,o));if(a>c||h>f)return;if(t.sheet?.sheet2&&(t.sheet.book===null||t.sheet.book===void 0)){this.has3D=!0;for(const p of this.sheetsBetween(t.sheet.sheet,t.sheet.sheet2)??[])this._addDep(p,a,h,c,f,e,s,n);return}if(t.sheet?.book!==null&&t.sheet?.book!==void 0)return;const d=t.sheet?this.sheetByName(t.sheet.sheet):e;d&&this._addDep(d,a,h,c,f,e,s,n)}_addHullDep(t,e,s,n){const i=this.hullAreas,l=[];this.hullAreas=l;for(const r of t.refs)this._addRefDep(r,e,s,n);for(const r of t.names)this._addNameDeps(r,e,s,n);for(const r of t.tables)this._addTableDep(r,e,s,n);this.hullAreas=i;for(const r of new Set(l.map(o=>o[0]))){const o=l.filter(h=>h[0]===r),a=h=>Math.min(...o.map(f=>f[h])),c=h=>Math.max(...o.map(f=>f[h]));this._addDep(r,a(1),a(2),c(3),c(4),e,s,n)}}_addNameDeps(t,e,s,n){const i=this._nameTemplate(t,e);if(i!==null){i.readsSheets&&tt(this.sheetReaders,e).add(s,n);for(const l of i.refs)this._addRefDep(l,e,s,n);for(const l of i.tables)this._addTableDep(l,e,s,n);if(i.hulls)for(const l of i.hulls)this._addHullDep(l,e,s,n);for(const l of i.names)l.toUpperCase()!==t.toUpperCase()&&this._addNameDeps(l,e,s,n)}}_addTableDep(t,e,s,n){const i=this.resolveTable(t.name,t.spec,s,n,e);i instanceof U&&this._addDep(i.sheet,i.r0,i.c0,i.r1,i.c1,e,s,n)}rebuild(){this.has3D=!1,this.deps.clear(),this.volatile.clear(),this.visibilityReaders.clear(),this.styleReaders.clear(),this.sheetReaders.clear(),this.lookupCache.clear(),this.qHead=0,this.qLen=0,this.cycleFound=[],this.cycleMembers=null,this.sheetGen++,this.buildGraph({fullCalc:!0})}buildGraph({fullCalc:t=!1,onDone:e}={}){const s=this.buildGeneration=(this.buildGeneration??0)+1;this.running=!1,this.graphReady=!1,this.dataTables=[],t&&(this.tablesDirty=!0);const n=this,i=this.sheetGen,l=this.book.sheets.map(f=>[f,f.formulas]);let r=0,o=0,a=0,c=0;const h=()=>{if(n.buildGeneration!==s)return;const f=performance.now()+n.sliceMs;let d=0;for(;r<l.length;r++,o=0,a=0,c=0){const[_,y]=l[r],{columns:u}=y;for(;o<u.length;o++,a=0,c=0){const k=u[o];if(k!==void 0)for(;a<k.length;a++,c=0){const g=k[a];if(g===void 0)continue;const{ids:A}=g,T=a<<I;for(;c<=x;c++){const E=A[c];if(E===0)continue;const D=y.infos[E],q=T+c;D.t==="dataTable"&&n.dataTables.push({sheet:_,row:q,col:o,info:D});const Mt=n.register(_,q,o,D);if(n._initFormula(_,q,o,D,Mt,t),++d%256===0&&performance.now()>f){c++,n.onProgress?.({preparing:!0}),n.buildGeneration===s&&n._later(h);return}}}}}n.graphReady=!0,n.sheetGen!==i&&n._wakeReaders(n.sheetReaders);const p=n.pendingChanges;n.pendingChanges=[];for(const[_,y,u,k]of p)n.cellsChanged(_,y,u,k);n._schedule(),e?.()};h()}_initFormula(t,e,s,n,i,l){const r=t._chunk(e,s);if(r===void 0)return;const o=e&x,a=r.kinds[o],c=n.cm&&(a&S)===N&&r.values[o]===w.SPILL.index,h=n.t==="array"&&n.ref&&!c?this._markArrayChildren(t,e,s,n):!1;if(i.external||i.keepImportedResult&&!n.typed&&(a&S)!==v)return;const f=n.ca==="1"||n.ca==="true";(l||c||h||f||i.isVolatile||(a&S)===v||i.notInExcel&&(a&S)===N)&&(r.kinds[o]=a|M,this._enqueue(t,e,s))}_markArrayChildren(t,e,s,n){const i=B(n.ref);if(i===null)return!1;const l=e*b+s;if(n.cm&&t.spills?.has(l))return!1;t.spills||(t.spills=new Map),t.spills.set(l,{...i,blocked:!1,fixed:!n.cm});let r=!1;for(let o=i.r0;o<=i.r1;o++)for(let a=i.c0;a<=i.c1;a++){if(o===e&&a===s)continue;const c=t._ensure(o,a);(c.kinds[o&x]&S)===v&&(r=!0),c.kinds[o&x]|=Z}return r}_enqueue(t,e,s){if(this.qLen+3>this.queue.length)if(this.qHead>this.queue.length>>>1)this.queue.copyWithin(0,this.qHead,this.qLen),this.qLen-=this.qHead,this.qHead=0;else{const n=new Int32Array(this.queue.length*2);n.set(this.queue),this.queue=n}this.queue[this.qLen++]=t.uid,this.queue[this.qLen++]=e,this.queue[this.qLen++]=s}_dirtyFrom(t,e,s,n,i,l=!1){const r=[t,e,s,n,i];for(;r.length>0;){const o=r.pop(),a=r.pop(),c=r.pop(),h=r.pop(),f=r.pop(),d=this.deps.get(f);if(d===void 0)continue;const p=(u,k)=>{const g=this.sheetsByUid.get(k>>>14);if(g===void 0)return;const A=k&16383,T=g._chunk(u,A);if(T===void 0)return;const E=u&x,D=T.kinds[E];(D&L)!==0&&((D&K)!==0&&l&&(this.late??=new Set).add(at(g.uid,u,A)),(D&M)===0&&(T.kinds[E]=D|M,this._enqueue(g,u,A),r.push(g,A,A,u,u)))},_=a>>>I,y=o>>>I;for(let u=h;u<=c;u++){const k=d.buckets[u];if(k!==void 0)for(let A=_;A<=y;A++){const T=k[A];if(T===void 0)continue;const{data:E}=T,D=T.n*O;for(let q=0;q<D;q+=O)o>=E[q+2]&&a<=E[q+3]&&p(E[q],E[q+1])}const g=d.wide[u];if(g!==void 0){const{data:A}=g,T=g.n*O;for(let E=0;E<T;E+=O)o>=A[E+2]&&a<=A[E+3]&&p(A[E],A[E+1])}}if(d.all.n>0){const{data:u}=d.all;for(let k=0;k<d.all.n;k++){const g=k*O;o>=u[g+2]&&a<=u[g+3]&&c>=d.allCols[k*2]&&h<=d.allCols[k*2+1]&&p(u[g],u[g+1])}}}}rangeChanged(t,e,s,n,i){if(t.valueListeners)for(const l of t.valueListeners)l(e,s,n,i);if(!this.graphReady){const l=(i-s+1)*2,r=new Uint32Array(l),o=new Uint16Array(l);for(let a=s;a<=i;a++)r[(a-s)*2]=e,r[(a-s)*2+1]=n,o[(a-s)*2]=a,o[(a-s)*2+1]=a;this.pendingChanges.push([t,r,o,l]);return}if(this.tablesDirty=!0,this.lookupCache.clear(),t.spills)for(const[l,r]of t.spills){if(r.r0>n||r.r1<e||r.c0>i||r.c1<s)continue;const o=Math.floor(l/b);this._spillTouched(t,o,l-o*b)}this._dirtyFrom(t,s,i,e,n),this._markVolatile(),this._schedule()}cellsChanged(t,e,s,n){if(n===0)return;if(t.valueListeners)for(const l of t.valueListeners)for(let r=0;r<n;r++)l(e[r],s[r],e[r],s[r]);if(this.inTables||(this.tablesDirty=!0),!this.graphReady){this.pendingChanges.push([t,e.slice(0,n),s.slice(0,n),n]);return}this.lookupCache.clear();const i=new Map;for(let l=0;l<n;l++){const r=e[l],o=s[l],a=i.get(o);a===void 0?i.set(o,[r,r]):(r<a[0]&&(a[0]=r),r>a[1]&&(a[1]=r));const c=t._chunk(r,o);if(c===void 0)continue;const h=r&x,f=c.kinds[h];if(f&L){const d=t.formula(r,o);d&&(this.register(t,r,o,d),f&M||(c.kinds[h]=f|M,this._enqueue(t,r,o)))}t.spills&&this._spillTouched(t,r,o)}for(const[l,r]of i)this._dirtyFrom(t,l,l,r[0],r[1]);this._markVolatile(),this._schedule()}arraysRestored(t,e){for(let s=0;s<e.count;s++){if(!(e.kinds[s]&L))continue;const n=e.rows[s],i=e.cols[s],l=t.formula(n,i),r=l?.t==="array"&&l.ref?B(l.ref):null;if(r===null||r.r0===r.r1&&r.c0===r.c1)continue;const o=n*b+i;t.spills||(t.spills=new Map),t.spills.has(o)||t.spills.set(o,{...r,blocked:!1,fixed:!l.cm})}}_spillTouched(t,e,s){const n=e*b+s;for(const[i,l]of t.spills){const r=Math.floor(i/b),o=i-r*b;if(i===n){(t.kind(r,o)&L)===0&&(l.blocked||this._clearSpill(t,l,r,o),t.spills.delete(i));continue}if(e>=l.r0&&e<=l.r1&&s>=l.c0&&s<=l.c1){const a=t._chunk(r,o);if(a===void 0)continue;const c=r&x,h=a.kinds[c];h&L&&!(h&M)&&(a.kinds[c]=h|M,this._enqueue(t,r,o),this._dirtyFrom(t,o,o,r,r))}}}mergesChanged(t){if(!t.spills)return;let e=!1;for(const[s,n]of t.spills){if(n.fixed||!(n.blocked||t.merges.some(a=>a.r0<=n.r1&&a.r1>=n.r0&&a.c0<=n.c1&&a.c1>=n.c0)))continue;const i=Math.floor(s/b),l=s-i*b,r=t._chunk(i,l),o=r?.kinds[i&x]??0;!(o&L)||o&M||(r.kinds[i&x]=o|M,this._enqueue(t,i,l),this._dirtyFrom(t,l,l,i,i),e=!0)}e&&this._schedule()}_markVolatile(){for(const[t,e]of this.volatile)e.forEach((s,n)=>{const i=t._chunk(s,n);if(i===void 0)return;const l=s&x,r=i.kinds[l];if(!(r&L)){e.delete(s,n);return}r&M||(i.kinds[l]=r|M,this._enqueue(t,s,n),this._dirtyFrom(t,n,n,s,s))})}get pending(){return(this.qLen-this.qHead)/3}_later(t){if(this._next=t,typeof MessageChannel>"u"){setTimeout(()=>this._tick(),0);return}this.channel===null&&(this.channel=new MessageChannel,this.channel.port1.onmessage=()=>this._tick()),this.channel.port2.postMessage(0)}_tick(){const t=this._next;this._next=null,t?.(),this._next===null&&this.channel!==null&&(this.channel.port1.close(),this.channel=null)}_schedule(){if(this.manual&&!this.tablesForced||this.inTables||this.running||!this.graphReady)return;if(this.pending===0){this._finish();return}const t=this.pending,e=this.buildGeneration;if(t<=this.syncLimit){if(this._slice(this.syncLimit===1/0?1/0:this.sliceMs),this.buildGeneration!==e)return;if(this.pending===0){this._finish();return}}this.running=!0,this._total=t;const s=()=>{this.buildGeneration===e&&(this._slice(this.sliceMs),this.buildGeneration===e&&(this.pending>0?(this.onProgress?.({remaining:this.pending,total:Math.max(this._total,this.pending)}),this.buildGeneration===e&&this._later(s)):(this.running=!1,this._finish())))};this._later(s)}_slice(t){const e=t!==1/0;let s=e?performance.now():0;const n=s+t;let i=1,l=1;for(;this.qHead<this.qLen;){const r=this.queue[this.qHead++],o=this.queue[this.qHead++],a=this.queue[this.qHead++],c=this.sheetsByUid.get(r);if(c!==void 0&&(this._run(c,o,a),e&&--l===0)){const h=performance.now();if(h>n)break;i=h-s<.5?Math.min(i*2,16):1,l=i,s=h}}this.qHead>=this.qLen&&(this.qHead=0,this.qLen=0)}_finish(){for(this.late=null,this.lateRuns=null;this.cycleFound.length>0;)this._iterateCycles();const t=this.tablesForced;this.tablesForced=!1,this.tablesDirty&&this.dataTables.length>0&&(!this.skipTables||t)&&(this.tablesDirty=!1,this._computeDataTables()),this.book.formatValueRevision=(this.book.formatValueRevision??0)+1,this.onChange?.();const e=this.waiters;this.waiters=[];for(const s of e)s()}_dataTableTemplate(){const t=this;return{fn:(s,n,i)=>t._plainValue(i,s,n),refs:[],names:[],tables:[],isVolatile:!1,mayArray:!1}}_plainValue(t,e,s){const n=t.value(e,s);switch(t.kind(e,s)&S){case V:return n;case X:return this.book.strings.get(n);case Q:return n!==0;case N:return st[n];default:return null}}_writePlain(t,e,s,n){const i=t.kind(e,s)&L;n==null?t.setCell(e,s,i|V,0):typeof n=="number"?Number.isFinite(n)?t.setCell(e,s,i|V,n):t.setCell(e,s,i|N,w.NUM.index):typeof n=="boolean"?t.setCell(e,s,i|Q,n?1:0):n instanceof C?t.setCell(e,s,i|N,n.index):t.setCell(e,s,i|X,this.book.strings.add(String(n)))}_setInputs(t,e){const s=new Uint32Array(e.length),n=new Uint32Array(e.length);e.forEach((i,l)=>{t.setCell(i.row,i.col,i.kind,i.value),s[l]=i.row,n[l]=i.col}),this.cellsChanged(t,s,n,e.length),this._slice(1/0)}evaluateWith(t,e,s){const n=e.map(({row:r,col:o})=>({row:r,col:o,kind:t.kind(r,o)&(S|L),value:t.value(r,o)})),i=this.inTables,l=this.trial;this.inTables=!0,this.trial??=new Map;try{return this._setInputs(t,e.map(({row:r,col:o,text:a})=>{const c=Number(a);return a!==""&&Number.isFinite(c)?{row:r,col:o,kind:V,value:c}:{row:r,col:o,kind:X,value:this.book.strings.add(String(a))}})),s()}finally{this._setInputs(t,n),this.inTables=i,this.trial=l}}_computeDataTables(){this.inTables=!0;const t=this.dataTables.filter(e=>e.sheet.formula(e.row,e.col)===e.info&&this.book.sheets.includes(e.sheet));this.dataTables=t;try{for(const e of t)this._computeDataTable(e)}finally{this.inTables=!1}}_computeDataTable({sheet:t,info:e}){const s=B(e.ref);if(!s)return;const n=B(e.r1??""),i=e.r2?B(e.r2):null,l=e.dt2D==="1"||e.dt2D==="true",r=e.dtr==="1"||e.dtr==="true",o=e.del1==="1"||e.del1==="true"||l&&(e.del2==="1"||e.del2==="true");if(!o&&(!n||l&&!i))return;const a=(u,k)=>({kind:t.kind(u,k)&S,value:t.value(u,k)}),h=(o?[]:[n,i].filter(Boolean)).map(u=>({row:u.r0,col:u.c0,kind:t.kind(u.r0,u.c0)&(S|L),value:t.value(u.r0,u.c0)})),f=[];this.trial=new Map;const d=(u,k,g,A)=>{const T=this.read(t,g,A);f.push(u,k,T instanceof R?T.data[0]:T)};try{if(o)for(let u=s.r0;u<=s.r1;u++)for(let k=s.c0;k<=s.c1;k++)f.push(u,k,w.REF);else if(l)for(let u=s.r0;u<=s.r1;u++)for(let k=s.c0;k<=s.c1;k++)this._setInputs(t,[{row:n.r0,col:n.c0,...a(s.r0-1,k)},{row:i.r0,col:i.c0,...a(u,s.c0-1)}]),d(u,k,s.r0-1,s.c0-1);else if(r)for(let u=s.c0;u<=s.c1;u++){this._setInputs(t,[{row:n.r0,col:n.c0,...a(s.r0-1,u)}]);for(let k=s.r0;k<=s.r1;k++)d(k,u,k,s.c0-1)}else for(let u=s.r0;u<=s.r1;u++){this._setInputs(t,[{row:n.r0,col:n.c0,...a(u,s.c0-1)}]);for(let k=s.c0;k<=s.c1;k++)d(u,k,s.r0-1,k)}}finally{h.length>0&&this._setInputs(t,h),this.trial=null}const p=f.length/3,_=new Uint32Array(p),y=new Uint32Array(p);for(let u=0;u<p;u++)_[u]=f[u*3],y[u]=f[u*3+1],this._writePlain(t,_[u],y[u],f[u*3+2]);this.cellsChanged(t,_,y,p),this._slice(1/0),t.revision++}dataTableAt(t,e,s){for(const n of this.dataTables){if(n.sheet!==t)continue;const i=B(n.info.ref);if(i&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1)return n}return null}calculateNow(){const{manual:t,skipTables:e}=this;this.manual=!1,this.skipTables=!1,this.tablesDirty=!0,this.tablesForced=!0,this._markVolatile(),this._markCycles(),this._schedule(),this.manual=t,this.skipTables=e}whenIdle(){return this.pending===0&&this.graphReady&&!this.running?Promise.resolve():new Promise(t=>{this.waiters.push(t),this._schedule()})}settle({saving:t=!1}={}){if(this.manual){if(!t||!this.calcOnSave)return Promise.resolve();this.calculateNow()}return this.whenIdle()}_iterateCycles(){const t=this.cycleFound;this.cycleFound=[];const e=new Map,s=(r,o,a)=>{let c=e.get(r.uid);c===void 0&&(c=new Map,e.set(r.uid,c)),c.set(o*b+a,[r,o,a])};for(let r=0;r<t.length;r+=3)s(t[r],t[r+1],t[r+2]);const n=[];for(const r of this.book.sheets){const o=e.get(r.uid);o&&n.push(...[...o.keys()].sort((a,c)=>a-c).map(a=>o.get(a)))}this.cycleMembers??=new Map;for(const r of n)this.cycleMembers.set(`${r[0].uid}:${r[1]}:${r[2]}`,r);const{count:i,delta:l}=this.iterate;for(let r=1;r<=i;r++){let o=0;for(const[a,c,h]of n){const f=a._chunk(c,h);if(f===void 0)continue;const d=c&x;if(!(f.kinds[d]&L))continue;const p=f.values[d];f.kinds[d]|=M,this.iterating=!0,this._run(a,c,h),this.iterating=!1;const _=Math.abs(f.values[d]-p);_>o&&(o=_)}if(this.onProgress?.({iteration:r,iterations:i}),o<l)break}for(const[r,o,a]of n)this._dirtyFrom(r,a,a,o,o);for(const[r,o,a]of n){const c=r._chunk(o,a);c!==void 0&&(c.kinds[o&x]&=~M)}this._slice(1/0)}_markCycles(){if(!(this.iterate===null||this.cycleMembers===null))for(const[t,e,s]of this.cycleMembers.values()){const n=t._chunk(e,s);n===void 0||!(n.kinds[e&x]&L)||(n.kinds[e&x]|=M,this._enqueue(t,e,s))}}_run(t,e,s){const n=[t,e,s];for(;n.length>0;){const i=n[n.length-1],l=n[n.length-2],r=n[n.length-3];try{this.depth=0,this.evalTop=0,this.cycleLow=lt,this._calc(r,l,i),n.length-=3}catch(o){if(o!==rt)throw o;n.push(this.pendingSheet,this.pendingRow,this.pendingCol)}}}_calc(t,e,s){const n=t._chunk(e,s);if(n===void 0)return;const i=e&x,l=n.kinds[i];if(!(l&M))return;if(l&K){this.circular={sheet:t,row:e,col:s},this.iterating||this._cycleHit(t.uid,e,s);return}if(this.depth>=kt)throw this.pendingSheet=t,this.pendingRow=e,this.pendingCol=s,rt;const r=t.formula(e,s);if(r===void 0||!(l&L)){n.kinds[i]=l&~M;return}const o=this._templateFor(t,e,s,r);if(o.external||o.keepImportedResult&&!r.typed&&(l&S)!==v){n.kinds[i]=l&~M;return}n.kinds[i]=l|K;const a=this.evalTop;a+3>this.evalStack.length&&this._growEvalStack(),this.evalStack[a]=t.uid,this.evalStack[a+1]=e,this.evalStack[a+2]=s,this.evalTop=a+3;const c=this.cycleLow;this.cycleLow=lt;const h=this.legacy,f=this.frame,d=r.typed!==!0&&r.t!=="array";this.legacy=d,this.depth++;let p;try{p=o.fn(e,s,t),(p instanceof U||p instanceof $)&&(p=this.refAuto(p,e,s)),p instanceof nt&&(p=this.toArr(p))}catch(y){n.kinds[i]&=~K,this.depth--,this.legacy=h,this.frame=f,this.evalTop=a;const u=this.cycleLow;if(this.cycleLow=c,y===rt)throw y;y instanceof C?p=y:(console.error("Formula evaluation failed",y),p=w.VALUE),this._store(t,n,i,e,s,p,r,d),this.late!==null&&u>a&&this._runLate(t,n,i,e,s);return}this.depth--,this.legacy=h,this.frame=f,this.evalTop=a;const _=this.cycleLow;if(this.cycleLow=_<a&&_<c?_:c,_<=a){if(this.iterate!==null){n.kinds[i]=l&~(M|K),this.cycleFound.push(t,e,s);return}if((l&S)!==v&&this._keepsInCycle(t,e,s,r)){n.kinds[i]=l&~(M|K),this._dropTrialValue(t,n,i,e,s);return}p=0}this._store(t,n,i,e,s,p,r,d),this.late!==null&&_>a&&this._runLate(t,n,i,e,s)}_runLate(t,e,s,n,i){const l=at(t.uid,n,i);this.late.delete(l)&&(this.late.size===0&&(this.late=null),this._mayRunAgain(l)&&(e.kinds[s]|=M,this._enqueue(t,n,i),this._dirtyFrom(t,i,i,n,n,!0)))}_keepsInCycle(t,e,s,n){return n.typed===void 0?!0:this.book.collab===void 0&&!t.spills?.has(e*b+s)}_dropTrialValue(t,e,s,n,i){const l=this.trial?.get(t)?.get(n*b+i);if(!(l===void 0||!((e.kinds[s]&J)!==l[0]||e.values[s]!==l[1]))&&(e.kinds[s]=l[0],e.values[s]=l[1],t.valueListeners))for(const o of t.valueListeners)o(n,i,n,i)}_saveTrialValue(t,e,s,n,i){let l=this.trial.get(t);l===void 0&&(l=new Map,this.trial.set(t,l));const r=n*b+i;if(l.has(r))return;const o=e.kinds[s]&J;l.set(r,(o&S)===v?[L|V,0]:[o,e.values[s]])}_cycleHit(t,e,s){const n=this.evalStack;for(let i=this.evalTop-3;i>=0;i-=3)if(n[i]===t&&n[i+1]===e&&n[i+2]===s){i<this.cycleLow&&(this.cycleLow=i);return}}_growEvalStack(){const t=new Int32Array(this.evalStack.length*2);t.set(this.evalStack),this.evalStack=t}_store(t,e,s,n,i,l,r,o){let a=l;a instanceof Rt&&(a=w.CALC),a instanceof R?r.t==="array"&&!r.cm&&r.ref?a=this._fillFixedArray(t,n,i,r,a):o||a.spillRows===1&&a.spillCols===1?(a=a.data[0],t.spills&&this._releaseSpill(t,n,i)):a=this._spill(t,n,i,r,a):t.spills&&!(r.t==="array"&&!r.cm)&&this._releaseSpill(t,n,i),r.t==="array"&&!r.cm&&r.ref&&!(l instanceof R)&&(a=this._fillFixedArray(t,n,i,r,R.filled(1,1,a)));let c=L,h=0;if(typeof a=="number"?Number.isFinite(a)?(c|=V,h=this.precise?pt(this.book,t,n,i,a):a):(c|=N,h=w.NUM.index):typeof a=="string"?(c|=X,h=this.book.strings.add(a)):typeof a=="boolean"?(c|=Q,h=a?1:0):a instanceof C?(c|=N,h=a.index):a instanceof j?(c|=N,h=w.VALUE.index):c|=V,a instanceof j?_t(t,n*b+i,ft(this.book,a.index)):t.valueMeta!==null&&((e.kinds[s]&J)!==c||e.values[s]!==h)&&!/IMAGE\(/i.test(r.f??r.sf??"")&&$t(t,n*b+i),t.valueListeners&&((e.kinds[s]&J)!==c||e.values[s]!==h))for(const f of t.valueListeners)f(n,i,n,i);this.trial!==null&&this.iterate===null&&this._saveTrialValue(t,e,s,n,i),e.kinds[s]=c,e.values[s]=h,n>t.maxRow&&(t.maxRow=n),i>t.maxCol&&(t.maxCol=i)}_writeArrayCell(t,e,s,n){let i=Z,l=0;typeof n=="number"?(i|=Number.isFinite(n)?V:N,l=Number.isFinite(n)?this.precise?pt(this.book,t,e,s,n):n:w.NUM.index):typeof n=="string"?(i|=X,l=this.book.strings.add(n)):typeof n=="boolean"?(i|=Q,l=n?1:0):n instanceof C?(i|=N,l=n.index):n==null?i|=V:(i|=N,l=w.VALUE.index,n instanceof j&&_t(t,e*b+s,ft(this.book,n.index)));const r=t.value(e,s)!==l||(t.kind(e,s)&S)!==(i&S);return t.setCell(e,s,i,l),r}_fillFixedArray(t,e,s,n,i){const l=B(n.ref);if(l===null)return i.data[0];t.spills||(t.spills=new Map),t.spills.set(e*b+s,{...l,blocked:!1,fixed:!0});const r=(c,h)=>{const f=i.rows===1?0:c,d=i.cols===1?0:h;return f>=i.rows||d>=i.cols?w.NA:i.get(f,d)};let o=!1;for(let c=l.r0;c<=l.r1;c++)for(let h=l.c0;h<=l.c1;h++)c===e&&h===s||this._writeArrayCell(t,c,h,r(c-l.r0,h-l.c0))&&(o=!0);(l.r0!==l.r1||l.c0!==l.c1)&&this._dirtyFrom(t,l.c0,l.c1,l.r0,l.r1,o);const a=r(e-l.r0,s-l.c0);return a===null?0:a}_spill(t,e,s,n,i){const l=e*b+s;t.spills||(t.spills=new Map);const r=t.spills.get(l),o=e+i.rows-1,a=s+i.cols-1,c={r0:e,c0:s,r1:o,c1:a,blocked:!1,fixed:!1};let h=null;const f=(y,u)=>{if(r&&!r.blocked&&y>=r.r0&&y<=r.r1&&u>=r.c0&&u<=r.c1)return!0;if(h===null){h=[];for(const[k,g]of t.spills)k!==l&&!g.blocked&&g.r0<=o&&g.r1>=e&&g.c0<=a&&g.c1>=s&&h.push(g)}return!h.some(k=>y>=k.r0&&y<=k.r1&&u>=k.c0&&u<=k.c1)};let d=e+i.spillRows>G||s+i.spillCols>b;if(!d)t:for(let y=e;y<=o;y++)for(let u=s;u<=a;u++){if(y===e&&u===s)continue;const k=t.kind(y,u);if((k&J)!==0&&!((k&Z)!==0&&f(y,u))){d=!0;break t}if(t.merges.length>0&&t.mergeAt(y,u)){d=!0;break t}}if(d)return r&&!r.blocked&&this._clearSpill(t,r,e,s),c.blocked=!0,c.r1=Math.min(o,G-1),c.c1=Math.min(a,b-1),t.spills.set(l,c),n.t="array",n.ref=`${ct(s)}${e+1}`,n.cm=1,delete n.raw,w.SPILL;if(r&&!r.blocked){for(let y=r.r0;y<=r.r1;y++)for(let u=r.c0;u<=r.c1;u++)y>=c.r0&&y<=c.r1&&u>=c.c0&&u<=c.c1||(t.kind(y,u)&Z)!==0&&t.setCell(y,u,v,0);this._dirtyFrom(t,r.c0,r.c1,r.r0,r.r1,r.r1!==o||r.c1!==a),(r.r1>o||r.c1>a)&&this._retryBlocked(t,r)}t.spills.set(l,c);let p=!r||r.blocked||r.r1!==o||r.c1!==a;for(let y=e;y<=o;y++)for(let u=s;u<=a;u++)y===e&&u===s||this._writeArrayCell(t,y,u,i.get(y-e,u-s))&&(p=!0);n.t="array",n.ref=`${ct(s)}${e+1}:${ct(a)}${o+1}`,n.cm=1,delete n.raw,this._dirtyFrom(t,s,a,e,o,p);const _=i.data[0];return _===null?0:_}_retryBlocked(t,e){for(const[s,n]of t.spills){if(!n.blocked||n.r0>e.r1||n.r1<e.r0||n.c0>e.c1||n.c1<e.c0)continue;const i=Math.floor(s/b),l=s-i*b,r=t._chunk(i,l),o=r?.kinds[i&x]??0;!(o&L)||o&M||!this._mayRunAgain(at(t.uid,i,l))||(r.kinds[i&x]=o|M,this._enqueue(t,i,l),this._dirtyFrom(t,l,l,i,i,!0))}}_mayRunAgain(t){const e=((this.lateRuns??=new Map).get(t)??0)+1;return e>Gt?!1:(this.lateRuns.set(t,e),!0)}_clearSpill(t,e,s,n){for(let i=e.r0;i<=e.r1;i++)for(let l=e.c0;l<=e.c1;l++)i===s&&l===n||(t.kind(i,l)&Z)!==0&&t.setCell(i,l,v,0);this._dirtyFrom(t,e.c0,e.c1,e.r0,e.r1,!0),this._retryBlocked(t,e)}_releaseSpill(t,e,s){const n=e*b+s,i=t.spills.get(n);if(i===void 0)return;i.blocked||this._clearSpill(t,i,e,s),t.spills.delete(n);const l=t.formula(e,s);l&&l.typed?(delete l.ref,delete l.cm,delete l.t):l?.cm&&l.ref&&(l.ref=It(e,s))}spillMaster(t,e,s){if(!t.spills)return null;for(const[n,i]of t.spills)if(!i.blocked&&e>=i.r0&&e<=i.r1&&s>=i.c0&&s<=i.c1){const l=Math.floor(n/b);return{row:l,col:n-l*b,rect:i}}return null}spillRange(t){const{sheet:e,r0:s,c0:n}=t;this.read(e,s,n);const i=e.spills?.get(s*b+n);return!i||i.blocked?w.REF:new U(e,i.r0,i.c0,i.r1,i.c1)}read(t,e,s){const n=t.columns[s];if(n===void 0)return null;const i=n[e>>>I];if(i===void 0)return null;const l=e&x;let r=i.kinds[l];if(r===0)return null;switch(r&M&&(this._calc(t,e,s),r=i.kinds[l]),r&S){case V:return i.values[l];case X:return this.book.strings.get(i.values[l]);case Q:return i.values[l]!==0;case N:{if(t.valueMeta!==null&&this.book.richValues){const o=Tt(this.book,t,e,s);if(o!==null)return o}return st[i.values[l]]??w.NA}default:return null}}refToValue(t){return t instanceof $?t.refs.length===1?this.refToValue(t.refs[0]):w.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.toArr(t)}refAuto(t,e,s){return t instanceof $?t.refs.length===1?this.refAuto(t.refs[0],e,s):w.VALUE:t.isCell?this.read(t.sheet,t.r0,t.c0):this.legacy?this.intersect(t,e,s):this.toArr(t)}intersect(t,e,s){return t.cols===1&&e>=t.r0&&e<=t.r1?this.read(t.sheet,e,t.c0):t.rows===1&&s>=t.c0&&s<=t.c1?this.read(t.sheet,t.r0,s):w.VALUE}trim(t){const{sheet:e}=t;return{r0:t.r0,c0:t.c0,r1:Math.min(t.r1,Math.max(e.maxRow,t.r0)),c1:Math.min(t.c1,Math.max(e.maxCol,t.c0))}}toArr(t){if(t instanceof nt)return new R(t.rows,t.cols,t.data.map(e=>e instanceof U?this.read(e.sheet,e.r0,e.c0):e));if(t instanceof R)return t;if(t instanceof U){const e=t.rows===G||t.cols===b,{r0:s,c0:n,r1:i,c1:l}=e||t.rows*t.cols>Dt?this.trim(t):t,r=i-s+1,o=l-n+1,a=new Array(r*o);let c=0;for(let h=s;h<=i;h++)for(let f=n;f<=l;f++)a[c++]=this.read(t.sheet,h,f);return new R(r,o,a,t.rows===G?G:r,t.cols===b?b:o)}if(t instanceof $){if(t.refs.length===1)return this.toArr(t.refs[0]);throw w.VALUE}if(t instanceof C)throw t;if(t===Ct)throw w.VALUE;return new R(1,1,[t])}scalar(t,e,s){return t instanceof U?t.isCell?this.read(t.sheet,t.r0,t.c0):this.intersect(t,e,s):t instanceof $?w.VALUE:t instanceof nt?this.scalar(t.data[0],e,s):t instanceof R?t.data[0]:t}scan(t,e){if(t.c0===t.c1){this._scan(t,e);return}let s=null;try{this._scan(t,(n,i,l)=>{s=n,e(n,i,l)})}catch(n){throw n instanceof C&&n===s?this._firstError(t):n}}_firstError(t){let e=1/0,s=null;return this._scan(t,(n,i)=>{n instanceof C&&i<e&&(e=i,s=n)}),s}_scan(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);for(let l=t.c0;l<=n;l++){const r=s.columns[l];if(r!==void 0)for(let o=t.r0>>>I;o<=i>>>I;o++){const a=r[o];if(a===void 0)continue;const c=o<<I,h=Math.max(t.r0-c,0),f=Math.min(i-c,x),{kinds:d}=a;for(let p=h;p<=f;p++){if(d[p]===0)continue;const _=this.read(s,c+p,l);_!==null&&e(_,c+p,l)}}}}sumRange(t,e){const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow);let{sum:l,count:r}=e;for(let o=t.c0;o<=n;o++){const a=s.columns[o];if(a!==void 0)for(let c=t.r0>>>I;c<=i>>>I;c++){const h=a[c];if(h===void 0)continue;const f=c<<I,d=Math.max(t.r0-f,0),p=Math.min(i-f,x),{kinds:_,values:y}=h;for(let u=d;u<=p;u++){let k=_[u];if(k===0)continue;k&M&&(this._calc(s,f+u,o),k=_[u]);const g=k&S;if(g===V)l+=y[u],r++;else if(g===N)throw t.c0===t.c1?st[y[u]]:this._firstError(t)}}}e.sum=l,e.count=r}averageRange(t,e){if(t.cols===1)return this.sumRange(t,e);const{sheet:s}=t,n=Math.min(t.c1,s.maxCol),i=Math.min(t.r1,s.maxRow),l=[],r=[];for(let o=t.r0>>>I;o<=i>>>I;o++){l.length=0,r.length=0;for(let f=t.c0;f<=n;f++){const d=s.columns[f]?.[o];d!==void 0&&(l.push(f),r.push(d))}if(r.length===0)continue;const a=o<<I,c=Math.max(t.r0-a,0),h=Math.min(i-a,x);for(let f=c;f<=h;f++)for(let d=0;d<r.length;d++){const{kinds:p,values:_}=r[d];p[f]&M&&this._calc(s,a+f,l[d]);const y=p[f]&S;if(y===V)e.sum+=_[f],e.count++;else if(y===N)throw st[_[f]]}}}unary(t,e){if(t instanceof C)return t;if(t instanceof R)return new R(t.rows,t.cols,t.data.map(s=>{try{return s instanceof C?s:e(F(s))}catch(n){return n}}),t.spillRows,t.spillCols);try{return e(F(t))}catch(s){if(s instanceof C)return s;throw s}}slowBin(t,e,s){if(e instanceof R||s instanceof R)return this._binArr(t,e,s);if(e instanceof C)return e;if(s instanceof C)return s;try{return gt(t,e,s)}catch(n){if(n instanceof C)return n;throw n}}_binArr(t,e,s){const n=e instanceof R?e:null,i=s instanceof R?s:null,l=Math.max(n?n.rows:1,i?i.rows:1),r=Math.max(n?n.cols:1,i?i.cols:1),o=new Array(l*r);for(let a=0;a<l;a++)for(let c=0;c<r;c++){const h=it(e,a,c),f=it(s,a,c);let d;if(h instanceof C)d=h;else if(f instanceof C)d=f;else try{d=gt(t,h,f)}catch(p){d=p}o[a*r+c]=d}return new R(l,r,o,Math.max(n?.spillRows??1,i?.spillRows??1),Math.max(n?.spillCols??1,i?.spillCols??1))}isManyValues(t,e,s){return t instanceof R?t.data.length>1||t instanceof nt:e===!0&&t instanceof U&&!t.isCell&&(!this.legacy||s===!0)}liftScalars(t,e,s,n,i,l,r){const o=new Map;let a=1,c=1,h=1,f=1;for(const y of e){if(!this.isManyValues(s[y],t.scalarRanges||t.rangeAt?.(y),r))continue;const u=this.toArr(s[y]);o.set(y,u),a=Math.max(a,u.rows),c=Math.max(c,u.cols),h=Math.max(h,u.spillRows),f=Math.max(f,u.spillCols)}const d=o.get(t.shapeFrom);d&&({rows:a,cols:c,spillRows:h,spillCols:f}=d);const p=new Array(a*c),_=s.slice();for(let y=0;y<a;y++)for(let u=0;u<c;u++){let k=null;for(const[A,T]of o)_[A]=d&&T!==d?T.data[0]:it(T,y,u),k===null&&_[A]instanceof C&&(k=_[A]);let g=k;if(g===null)try{g=z(t.fn(this,_,n,i,l,r))}catch(A){if(!(A instanceof C))throw A;g=A}g instanceof $&&(g=w.VALUE),g instanceof U?g=this.read(g.sheet,g.r0,g.c0):g instanceof R&&(g=g.data[0]),p[y*c+u]=g===null?0:g}return new R(a,c,p,h,f)}liftArr(t,e,s){let n=1,i=1,l=1,r=1;for(const h of e)h instanceof R&&(h.rows>n&&(n=h.rows),h.cols>i&&(i=h.cols),h.spillRows>l&&(l=h.spillRows),h.spillCols>r&&(r=h.spillCols));const o=new Array(n*i),a=e.length,c=new Array(a);for(let h=0;h<n;h++)for(let f=0;f<i;f++){let d=null;for(let p=0;p<a;p++){const _=it(e[p],h,f);c[p]=_,!s&&d===null&&_ instanceof C&&(d=_)}if(d!==null){o[h*i+f]=d;continue}try{const p=z(t(...c));o[h*i+f]=p instanceof R?p.data[0]:p}catch(p){if(p instanceof C)o[h*i+f]=p;else throw p}}return new R(n,i,o,l,r)}invoke(t,e,s,n,i){if(t.native)return this._invokeNative(t.native,e,s,n,i);const l=t.frame?t.frame.slice():[],{params:r}=t;if(e.length>r.length)return w.VALUE;for(let a=0;a<r.length;a++)l[r[a]]=a<e.length?e[a]:void 0;const o=this.frame;if(this.frame=l,this.depth++,this.depth>=kt*4)return this.depth--,this.frame=o,w.NUM;try{return t.body(s,n,i)}finally{this.depth--,this.frame=o}}_invokeNative(t,e,s,n,i){if(e.length<t.min||e.length>t.max)return w.VALUE;try{if(t.kind==="raw"){const o=t.scalars?e.map((a,c)=>c).filter(a=>t.scalars(a,e.length,[])):[];return o.some(a=>this.isManyValues(e[a],t.scalarRanges||t.rangeAt?.(a),!0))?this.liftScalars(t,o,e,s,n,i,!0):z(t.fn(this,e,s,n,i))}if(t.kind==="lazy")return z(t.fn(this,e.map(o=>()=>o),s,n,i));const l=t.dateSystem?(...o)=>t.fn(this.book.dateOffset,...o):t.fn,r=e.map(o=>o instanceof U||o instanceof $?this.refToValue(o):o);if(r.some(o=>o instanceof R))return this.liftArr(l,r,t.errors);if(!t.errors){for(const o of r)if(o instanceof C)return o}return z(l(...r))}catch(l){if(l instanceof C)return l;throw l}}_nameEntry(t,e){const s=t.toUpperCase();if(e){const i=this.book.sheets.indexOf(e),l=this.names.get(`${i}!${s}`);if(l)return{key:`${i}!${s}`,entry:l}}const n=this.names.get(s);return n?{key:s,entry:n}:null}_nameTemplate(t,e){const s=this._nameEntry(t,e);if(s===null)return null;let n=this.nameTemplates.get(s.key);if(n===void 0){try{n=this._compile(new W(Y(s.entry.text,!0),0,0).parse(!0),!0)}catch(i){if(!(i instanceof H))throw i;n=this._broken()}this.nameTemplates.set(s.key,n)}return n}nameMayYieldRange(t){const e=t.toUpperCase(),s=[];for(const[n,i]of this.names)(n===e||n.endsWith(`!${e}`))&&s.push(i);if(s.length===0||(this._rangeNames??=new Set,this._rangeNames.has(e)))return!0;this._rangeNames.add(e);try{return s.some(n=>{try{return new dt(this).mayYieldRange(new W(Y(n.text,!0),0,0).parse(!0))}catch(i){if(i instanceof H)return!1;throw i}})}finally{this._rangeNames.delete(e)}}evalName(t,e,s,n){const i=this._nameTemplate(t,n);return i===null?w.NAME:i.fn(e,s,n)}rangesOf(t,e){const{legacy:s,frame:n,depth:i,evalTop:l,cycleLow:r}=this;try{const o=this._compile(new W(Y(t,!0),0,0).parse(!0),!0);for(;;)try{const a=o.external?null:o.fn(0,0,e);return a instanceof $?a.refs:a instanceof U?[a]:[]}catch(a){if(typeof a!="symbol")throw a;this._run(this.pendingSheet,this.pendingRow,this.pendingCol)}}catch(o){if(o instanceof H||o instanceof C)return[];throw o}finally{Object.assign(this,{legacy:s,frame:n,depth:i,evalTop:l,cycleLow:r})}}tableSpecToA1(t,e,s){const n=wt(t,e,s);if(!(n instanceof U))return null;const i=/#This Row|^\[\s*@/i.test(e),l=(r,o)=>i?`${mt(o)}${r+1}`:`$${mt(o)}$${r+1}`;return n.isCell?l(n.r0,n.c0):`${l(n.r0,n.c0)}:${l(n.r1,n.c1)}`}resolveTable(t,e,s,n,i){let l=t?this.tables.get(t.toUpperCase()):null;if(!t){for(const r of this.tables.values())if(r.sheet===i&&s>=r.r0&&s<=r.r1&&n>=r.c0&&n<=r.c1){l=r;break}}return l?wt(l,e,s):w.REF}exactIndex(t){const e=`${t.sheet.uid}:${t.r0}:${t.c0}:${t.r1}:${t.c1}`;let s=this.lookupCache.get(e);if(s!==void 0)return s;s=new Map;const n=t.cols===1;return this.scan(t,(i,l,r)=>{const o=typeof i=="string"?`s${i.toLowerCase()}`:typeof i=="number"?i:`b${i}`;if(i instanceof C)return;const a=n?l-t.r0:r-t.c0,c=s.get(o);(c===void 0||a<c)&&s.set(o,a)}),this.lookupCache.set(e,s),s}visibilityChanged(){this._wakeReaders(this.visibilityReaders)}presentationChanged(){this._wakeLater(2)}axisHiddenChanged(){this._wakeLater(1)}axisResized(){this._wakeLater(2)}_wakeLater(t){(t===1?this.visibilityReaders:this.styleReaders).size!==0&&(this.wakePending===0&&queueMicrotask(()=>{const e=this.wakePending;this.wakePending=0,e&1&&this._wakeReaders(this.visibilityReaders),e&2&&this._wakeReaders(this.styleReaders)}),this.wakePending|=t)}_wakeReaders(t){if(!this.graphReady||t.size===0)return;let e=!1;for(const[s,n]of t)n.forEach((i,l)=>{const r=s._chunk(i,l);if(r===void 0)return;const o=i&x,a=r.kinds[o];if(!(a&L)){n.delete(i,l);return}a&M||(r.kinds[o]=a|M,this._enqueue(s,i,l)),this._dirtyFrom(s,l,l,i,i),e=!0});e&&this._schedule()}isSubtotalCell(t,e,s){if((t.kind(e,s)&L)===0)return!1;const n=t.formula(e,s),i=n?.f??n?.sf??"";return/^\s*(_xlfn\.)?(SUBTOTAL|AGGREGATE)\(/i.test(i)}displayFormula(t){return Et(t)}}function ct(m){let t="";for(let e=m+1;e>0;e=Math.floor((e-1)/26))t=String.fromCharCode(65+(e-1)%26)+t;return t}function it(m,t,e){if(!(m instanceof R))return m;const s=m.rows===1?0:t,n=m.cols===1?0:e;return s>=m.rows||n>=m.cols?w.NA:m.data[s*m.cols+n]}function gt(m,t,e){if((t instanceof j||e instanceof j)&&m!=="="&&m!=="<>"&&m!=="&")throw w.VALUE;switch(m){case"+":{const s=F(t),n=s+F(e);return Math.abs(n)<Math.abs(s)*bt?0:et(n)}case"-":{const s=F(t),n=s-F(e);return Math.abs(n)<Math.abs(s)*bt?0:et(n)}case"*":return et(F(t)*F(e));case"/":{const s=F(t),n=F(e);if(n===0)throw w.DIV0;return et(s/n)}case"^":{const s=F(t),n=F(e);if(s===0&&n===0)throw w.NUM;if(s===0&&n<0)throw w.DIV0;const i=xt(s,n);if(Number.isNaN(i))throw w.NUM;return et(i)}case"&":return ht(t)+ht(e);case"=":return P(t,e)===0;case"<>":return P(t,e)!==0;case"<":return P(t,e)<0;case">":return P(t,e)>0;case"<=":return P(t,e)<=0;case">=":return P(t,e)>=0;default:throw w.VALUE}}const bt=5e-16;function et(m){if(!Number.isFinite(m))throw w.NUM;return m}function wt(m,t,e){const s=At(m,t,e);return s==="REF"?w.REF:s==="VALUE"?w.VALUE:new U(m.sheet,s.top,s.left,s.bottom,s.right)}const Bt={COUNTIF:[0],COUNTIFS:["even",0],SUMIF:[0,2],SUMIFS:[0,"odd",1],AVERAGEIF:[0,2],AVERAGEIFS:[0,"odd",1],MAXIFS:[0,"odd",1],MINIFS:[0,"odd",1],COUNTBLANK:[0],RANK:[1],"RANK.EQ":[1],"RANK.AVG":[1]};function Pt(m,t){for(let e=0;e<m.length;e++){const s=m[e];if(s===t||s==="odd"&&t>=m[e+1]&&t%2===1||s==="even"&&t>=m[e+1]&&t%2===0)return!0}return!1}function ut(m){if(!(m===null||typeof m!="object")){if(Array.isArray(m)){for(const t of m)ut(t);return}if(m.t==="call"){const t=Bt[String(m.name).replace(/^_xl(?:fn|ws)\./i,"").toUpperCase()];t&&m.args.forEach((e,s)=>{if(e?.t==="array"&&Pt(t,s))throw new Error(`${m.name} takes a range here`)})}for(const t of Object.values(m))t!==null&&typeof t=="object"&&ut(t)}}export{se as Engine};
|
package/calc/functions/array.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as
|
|
1
|
+
import{setCategory as j}from"./registry.js";import{lift as D,raw as m}from"./registry.js";import{Arr as p,bool as _,compareValues as G,ERR as A,num as E,OMITTED as L,Ref as V,str as K,XlError as C}from"../values.js";import{MAX_COLS as Y,MAX_ROWS as Q}from"../../core/address.js";j("Lookup & Reference");const M=o=>o===void 0||o===L,R=(o,t,s,a,r)=>{if(M(t))return r;if(t instanceof p&&t.data.length>1)throw A.VALUE;return Math.trunc(E(o.scalar(t,s,a)))},N=(o,t,s,a,r)=>M(t)?r:_(o.scalar(t,s,a)),O=o=>Array.from({length:o.rows},(t,s)=>o.data.slice(s*o.cols,(s+1)*o.cols)),U=o=>Array.from({length:o.cols},(t,s)=>Array.from({length:o.rows},(a,r)=>o.get(r,s))),y=o=>o===null?0:o;D("SEQUENCE",1,4,(o,t,s,a)=>{const r=o===L?1:Math.trunc(E(o)),n=t===void 0||t===L?1:Math.trunc(E(t)),e=s===void 0||s===L?1:E(s),i=a===void 0||a===L?1:E(a);if(r<0||n<0)throw A.VALUE;if(r===0||n===0)throw A.CALC;if(r*n>1<<27)throw A.NUM;const c=new Array(r*n);for(let l=0;l<c.length;l++)c[l]=e+l*i;return new p(r,n,c)}),D("RANDARRAY",0,5,(o,t,s,a,r)=>{const n=o===void 0||o===L?1:Math.trunc(E(o)),e=t===void 0||t===L?1:Math.trunc(E(t)),i=s===void 0||s===L?0:E(s),c=a===void 0||a===L?1:E(a),l=r===void 0||r===L?!1:_(r);if(n<0||e<0||i>c)throw A.VALUE;if(n===0||e===0)throw A.CALC;const f=new Array(n*e);for(let h=0;h<f.length;h++)f[h]=l?Math.ceil(i)+Math.floor(Math.random()*(Math.floor(c)-Math.ceil(i)+1)):i+Math.random()*(c-i);return new p(n,e,f)},{volatile:!0}),m("FILTER",2,3,(o,t,s,a)=>{const r=o.toArr(t[0]),n=o.toArr(t[1]),e=n.cols===1&&n.rows===r.rows,i=n.rows===1&&n.cols===r.cols;if(!e&&!i)throw A.VALUE;const c=n.data.map(u=>{if(u instanceof C)throw u;if(typeof u=="string")throw A.VALUE;return u===null?!1:_(u)}),l=e?O(r).filter((u,d)=>c[d]):null,f=e?null:U(r).filter((u,d)=>c[d]);if(e?l.length===0:f.length===0){if(M(t[2]))throw A.CALC;return t[2]}if(e)return p.of2D(l.map(u=>u.map(y)));const w=new p(r.rows,f.length,new Array(r.rows*f.length));return f.forEach((u,d)=>u.forEach((g,T)=>{w.data[T*f.length+d]=y(g)})),w},{array:!0,passes:o=>o===2});function b(o,t){const s=o[0].length,a=Array.from({length:s},(r,n)=>n);return a.sort((r,n)=>{for(let e=0;e<o.length;e++){const i=o[e][r],c=o[e][n],l=i==null||i instanceof C,f=c==null||c instanceof C;if(l||f){if(l&&f)continue;return l?1:-1}const h=G(i,c);if(h!==0)return h*t[e]}return r-n}),a}m("SORT",1,4,(o,t,s,a)=>{const r=o.toArr(t[0]),n=N(o,t[3],s,a,!1),e=M(t[1])?[1]:o.toArr(t[1]).data.map(g=>Math.trunc(E(g))),i=M(t[2])?[1]:o.toArr(t[2]).data.map(g=>Math.trunc(E(g))),c=i.length===1?e.map(()=>i[0]):i;if(c.length!==e.length||c.some(g=>g!==1&&g!==-1))throw A.VALUE;const l=n?U(r):O(r),f=n?r.rows:r.cols;for(const g of e)if(g<1||g>f)throw A.VALUE;const h=e.map(g=>l.map(T=>T[g-1])),u=b(h,c).map(g=>l[g]);if(!n)return p.of2D(u.map(g=>g.map(y)));const d=new Array(r.rows*r.cols);return u.forEach((g,T)=>g.forEach(($,X)=>{d[X*r.cols+T]=y($)})),new p(r.rows,r.cols,d)},{array:!0}),m("SORTBY",2,255,(o,t,s,a)=>{const r=o.toArr(t[0]),n=[],e=[];for(let h=1;h<t.length;h+=2){const w=o.toArr(t[h]),u=M(t[h+1])?1:E(o.scalar(t[h+1],s,a));if(u!==1&&u!==-1)throw A.VALUE;n.push(w),e.push(u)}const i=n[0].cols===1;for(const h of n)if(i?h.rows!==r.rows||h.cols!==1:h.cols!==r.cols||h.rows!==1)throw A.VALUE;const c=b(n.map(h=>h.data),e);if(i){const h=O(r);return p.of2D(c.map(w=>h[w].map(y)))}const l=U(r),f=new Array(r.rows*r.cols);return c.forEach((h,w)=>l[h].forEach((u,d)=>{f[d*r.cols+w]=y(u)})),new p(r.rows,r.cols,f)},{array:!0});const k=o=>o.map(t=>t===null?"\0":typeof t=="string"?`s${t.toLowerCase()}`:t instanceof C?`e${t.code}`:`${typeof t}${t}`).join("");m("UNIQUE",1,3,(o,t,s,a)=>{const r=o.toArr(t[0]),n=N(o,t[1],s,a,!1),e=N(o,t[2],s,a,!1),i=n?U(r):O(r),c=new Map;for(const w of i){const u=k(w);c.set(u,(c.get(u)??0)+1)}const l=new Set,f=[];for(const w of i){const u=k(w);(e?c.get(u)!==1:l.has(u))||(l.add(u),f.push(w))}if(f.length===0)throw A.CALC;if(!n)return p.of2D(f.map(w=>w.map(y)));const h=new Array(r.rows*f.length);return f.forEach((w,u)=>w.forEach((d,g)=>{h[g*f.length+u]=y(d)})),new p(r.rows,f.length,h)},{array:!0}),m("TRANSPOSE",1,1,(o,t)=>{const s=o.toArr(t[0]),a=new Array(s.data.length);for(let r=0;r<s.rows;r++)for(let n=0;n<s.cols;n++)a[n*s.rows+r]=y(s.get(r,n));return new p(s.cols,s.rows,a)},{array:!0});function I(o,t,s){const a=t.map(c=>o.toArr(c));if(!s){const c=Math.max(...a.map(f=>f.cols)),l=[];for(const f of a)for(let h=0;h<f.rows;h++){const w=new Array(c);for(let u=0;u<c;u++)w[u]=u<f.cols?y(f.get(h,u)):A.NA;l.push(w)}return p.of2D(l)}const r=Math.max(...a.map(c=>c.rows)),n=a.reduce((c,l)=>c+l.cols,0),e=new Array(r*n);let i=0;for(const c of a){for(let l=0;l<c.cols;l++)for(let f=0;f<r;f++)e[f*n+i+l]=f<c.rows?y(c.get(f,l)):A.NA;i+=c.cols}return new p(r,n,e)}m("VSTACK",1,254,(o,t)=>I(o,t,!1),{array:!0}),m("HSTACK",1,254,(o,t)=>I(o,t,!0),{array:!0});function H(o,t,s,a,r){const n=s-t+1,e=r-a+1,i=new Array(n*e);for(let c=0;c<n;c++)for(let l=0;l<e;l++)i[c*e+l]=y(o.get(t+c,a+l));return new p(n,e,i)}m("TAKE",2,3,(o,t,s,a)=>{const r=o.toArr(t[0]),n=R(o,t[1],s,a,r.rows),e=R(o,t[2],s,a,r.cols);if(n===0||e===0)throw A.CALC;const i=Math.min(Math.abs(n),r.rows),c=Math.min(Math.abs(e),r.cols),l=n<0?r.rows-i:0,f=e<0?r.cols-c:0;return H(r,l,l+i-1,f,f+c-1)},{array:!0}),m("DROP",2,3,(o,t,s,a)=>{const r=o.toArr(t[0]),n=R(o,t[1],s,a,0),e=R(o,t[2],s,a,0),i=r.rows-Math.abs(n),c=r.cols-Math.abs(e);if(i<=0||c<=0)throw A.CALC;const l=n>0?n:0,f=e>0?e:0;return H(r,l,l+i-1,f,f+c-1)},{array:!0}),m("EXPAND",2,4,(o,t,s,a)=>{const r=o.toArr(t[0]),n=R(o,t[1],s,a,r.rows),e=R(o,t[2],s,a,r.cols),i=M(t[3])?A.NA:o.scalar(t[3],s,a);if(n<r.rows||e<r.cols||n>Q||e>Y)throw A.VALUE;const c=new Array(n*e);for(let l=0;l<n;l++)for(let f=0;f<e;f++)c[l*e+f]=l<r.rows&&f<r.cols?y(r.get(l,f)):i;return new p(n,e,c)},{array:!0});function F(o,t,s,a,r){const n=o.toArr(t[0]),e=r?O(n):U(n),i=[];for(let l=1;l<t.length;l++){const f=o.toArr(t[l]);if(f.rows>1&&f.cols>1)throw A.VALUE;for(const h of f.data){const w=Math.trunc(E(h)),u=w<0?e.length+w:w-1;if(w===0||u<0||u>=e.length)throw A.VALUE;i.push(e[u])}}if(r)return p.of2D(i.map(l=>l.map(y)));const c=new Array(n.rows*i.length);return i.forEach((l,f)=>l.forEach((h,w)=>{c[w*i.length+f]=y(h)})),new p(n.rows,i.length,c)}m("CHOOSEROWS",2,255,(o,t,s,a)=>F(o,t,s,a,!0),{array:!0}),m("CHOOSECOLS",2,255,(o,t,s,a)=>F(o,t,s,a,!1),{array:!0});function P(o,t,s,a){const r=o.toArr(t[0]),n=R(o,t[1],s,a,0),e=N(o,t[2],s,a,!1);if(n<0||n>3)throw A.VALUE;const i=[],c=l=>{(n===1||n===3)&&l===null||(n===2||n===3)&&l instanceof C||i.push(y(l))};if(e)for(let l=0;l<r.cols;l++)for(let f=0;f<r.rows;f++)c(r.get(f,l));else for(const l of r.data)c(l);if(i.length===0)throw A.CALC;return i}m("TOCOL",1,3,(o,t,s,a)=>p.column(P(o,t,s,a)),{array:!0}),m("TOROW",1,3,(o,t,s,a)=>p.row(P(o,t,s,a)),{array:!0});function W(o,t,s,a,r){const n=o.toArr(t[0]);if(n.rows>1&&n.cols>1)throw A.VALUE;const e=R(o,t[1],s,a,0);if(e<1)throw A.NUM;const i=M(t[2])?A.NA:o.scalar(t[2],s,a),c=n.data.length,l=Math.ceil(c/e),f=r?l:e,h=r?e:l,w=new Array(f*h);for(let u=0;u<f*h;u++){const d=u<c?y(n.data[u]):i;r?w[u]=d:w[u%e*h+Math.floor(u/e)]=d}return new p(f,h,w)}m("WRAPROWS",2,3,(o,t,s,a)=>W(o,t,s,a,!0),{array:!0}),m("WRAPCOLS",2,3,(o,t,s,a)=>W(o,t,s,a,!1),{array:!0});function S(o,t,s,a){if(!(t instanceof V))return t;const r=o.trim(t);let{r0:n,c0:e}=t,i=s&2?r.r1:t.r1,c=a&2?r.c1:t.c1;s&2&&r.r1<n&&(i=n),a&2&&r.c1<e&&(c=e);const l=(u,d)=>t.sheet.kind(u,d)!==0,f=u=>{for(let d=e;d<=c;d++)if(l(u,d))return!0;return!1},h=u=>{for(let d=n;d<=i;d++)if(l(d,u))return!0;return!1};if(s&1)for(;n<i&&!f(n);)n++;if(s&2)for(;i>n&&!f(i);)i--;if(a&1)for(;e<c&&!h(e);)e++;if(a&2)for(;c>e&&!h(c);)c--;if(s?n===i&&!f(n):a?e===c&&!h(e):!1)throw A.REF;return new V(t.sheet,n,e,i,c)}m("TRIMRANGE",1,3,(o,t,s,a)=>{const r=n=>{const e=t[n]===void 0||t[n]===null?3:R(o,t[n],s,a,3);if(e<0||e>3)throw A.VALUE;return e};return S(o,t[0],r(1),r(2))},{refResult:!0}),m("_TRO_ALL",1,1,(o,t)=>S(o,t[0],3,3),{refResult:!0,category:"Hidden"}),m("_TRO_LEADING",1,1,(o,t)=>S(o,t[0],1,1),{refResult:!0,category:"Hidden"}),m("_TRO_TRAILING",1,1,(o,t)=>S(o,t[0],2,2),{refResult:!0,category:"Hidden"}),m("ARRAYTOTEXT",1,2,(o,t,s,a)=>{const r=o.toArr(t[0]),n=R(o,t[1],s,a,0);if(n!==0&&n!==1)throw A.VALUE;const e=n===1,i=c=>c===null?"":typeof c=="string"?e?`"${c.replace(/"/g,'""')}"`:c:c instanceof C?c.code:K(c);return e?`{${O(r).map(c=>c.map(i).join(",")).join(";")}}`:r.data.map(i).join(", ")},{array:!0}),m("PERCENTOF",2,2,(o,t)=>{const s=r=>{if(!(r instanceof V)&&!(r instanceof p)){if(r instanceof C)throw r;return r===null||r===L?0:E(r)}let n=0;for(const e of o.toArr(r).data){if(e instanceof C)throw e;typeof e=="number"&&(n+=e)}return n},a=s(t[1]);if(a===0)throw A.DIV0;return s(t[0])/a},{array:!0}),m("SINGLE",1,1,(o,t,s,a)=>{const r=o.scalar(t[0],s,a);return r===null?0:r}),m("ANCHORARRAY",1,1,(o,t)=>t[0]instanceof V?o.spillRange(t[0]):A.REF,{refResult:!0});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as O}from"./registry.js";import{raw as y}from"./registry.js";import{checked as I,ERR as
|
|
1
|
+
import{setCategory as O}from"./registry.js";import{raw as y}from"./registry.js";import{checked as I,ERR as h,OMITTED as T,Ref as S,RefList as P,textToNumber as q,XlError as U}from"../values.js";import{makeCriteria as G,scalarOf as X}from"./helpers.js";O("Database");function L(e){if(e instanceof U)throw e;const t=e instanceof P&&e.refs.length===1?e.refs[0]:e;if(!(t instanceof S))throw h.VALUE;return t}function M(e){return e===null||e instanceof U?"":(typeof e=="string"?e:String(e)).toLowerCase()}function $(e,t){const n=new Map,{c1:s}=e.trim(t);for(let r=t.c0;r<=s;r++){const o=M(e.read(t.sheet,t.r0,r));o!==""&&!n.has(o)&&n.set(o,r)}return n}function F(e,t,n,s,r,o,f){if(s===T||s===void 0){if(f)return null;throw h.VALUE}const u=X(e,s,r,o),c=u===!0?1:u;if(typeof c=="number"){const i=Math.trunc(c);if(i<1||i>t.cols)throw h.VALUE;return t.c0+i-1}const m=typeof c=="string"?n.get(c.toLowerCase()):void 0;if(m===void 0)throw h.VALUE;return m}function K(e){const t=typeof e=="string"&&!/^[<>=]/.test(e)&&q(e)===null&&!/^(true|false)$/i.test(e);return G(t?`${e}*`:e)}function _(e,t,n,s){const{sheet:r,r0:o,c0:f}=t,u=e.trim(t),c=[];for(let i=f;i<=u.c1;i++){const a=M(e.read(r,o,i));c.push([i,s.get(a),a])}if(t.r1>u.r1)return null;const m=[];for(let i=o+1;i<=u.r1;i++){const a=[];let w=!0;for(const[p,l,V]of c){if(l===void 0){const D=r.formula(i,p);if(D&&D.t!=="dataTable"){const k=e._templateFor(r,i,p,D);a.push(N=>{const R=i+N-n.r0-1,C=e.scalar(k.fn(R,p,r),R,p);return C===!0||typeof C=="number"&&C!==0});continue}if(V==="")continue}const g=e.read(r,i,p);if(g===null)continue;if(l===void 0){if(g===!0||typeof g=="number"&&g!==0)continue;w=!1;break}const b=K(g);a.push(D=>b(e.read(n.sheet,D,l)))}if(w){if(a.length===0)return null;m.push(a)}}return m}function E(e,t,n,s,r,o){const f=L(t[0]),u=L(t[2]);if(u.r0===u.r1)throw h.VALUE;const c=$(e,f),m=F(e,f,c,t[1],n,s,r),i=_(e,u,f,c),{sheet:a}=f,w=e.trim(f).r1,p=l=>i===null||i.some(V=>V.every(g=>g(l)));for(let l=f.r0+1;l<=w;l++)p(l)&&o(m===null?null:e.read(a,l,m))}function j(e,t,n,s){const r=[];return E(e,t,n,s,!1,o=>{if(o instanceof U)throw o;typeof o=="number"&&r.push(o)}),r}const d=(e,t)=>y(e,3,3,(n,s,r,o)=>I(t(j(n,s,r,o))));function A(e,t){const n=e.length;if(n<(t?1:2))throw h.DIV0;const s=e.reduce((r,o)=>r+o,0)/n;return e.reduce((r,o)=>r+(o-s)**2,0)/(t?n:n-1)}d("DAVERAGE",e=>{if(e.length===0)throw h.DIV0;return e.reduce((t,n)=>t+n,0)/e.length}),d("DMAX",e=>e.length===0?0:e.reduce((t,n)=>n>t?n:t)),d("DMIN",e=>e.length===0?0:e.reduce((t,n)=>n<t?n:t)),d("DPRODUCT",e=>e.length===0?0:e.reduce((t,n)=>t*n,1)),d("DSTDEV",e=>Math.sqrt(A(e,!1))),d("DSTDEVP",e=>Math.sqrt(A(e,!0))),d("DSUM",e=>e.reduce((t,n)=>t+n,0)),d("DVAR",e=>A(e,!1)),d("DVARP",e=>A(e,!0)),y("DCOUNT",3,3,(e,t,n,s)=>{const r=t[1]===T;let o=0;return E(e,t,n,s,!0,f=>{(r||typeof f=="number")&&o++}),o}),y("DCOUNTA",3,3,(e,t,n,s)=>{const r=t[1]===T;let o=0;return E(e,t,n,s,!0,f=>{(r||f!==null)&&o++}),o}),y("DGET",3,3,(e,t,n,s)=>{let r=0,o=null;if(E(e,t,n,s,!1,f=>{r++,o=f}),r===0)throw h.VALUE;if(r>1)throw h.NUM;return o===null?0:o});
|
package/calc/functions/date.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{setCategory as
|
|
1
|
+
import{setCategory as g}from"./registry.js";import{alias as K,lift as p,raw as U}from"./registry.js";import{Arr as C,ERR as f,num as u,OMITTED as S,Ref as F,RefList as H,str as B,XlError as X}from"../values.js";import{isDateFormat as $}from"../../core/autofill.js";import{partsToSerial as y,serialToParts as d}from"../../core/numfmt.js";import{eachValue as j,toolpakScalar as T}from"./helpers.js";import{parseInput as q}from"../../core/input.js";import{NUM as P}from"../../core/sheet.js";import{days360 as _,yearFrac as z}from"./financial.js";g("Date & Time");const w=t=>t===void 0||t===S,h=(t,o,r,n,e)=>p(t,o,r,n,{...e,dateSystem:!0}),N=2958465;function c(t,o){const r=Math.floor(u(t));if(r<0||r>N-o)throw f.NUM;return r+o}function G(t,o,r,n){let e=t;if(e>=0&&e<1900&&(e+=1900),e<0||e>9999)throw f.NUM;if(o<-32767||o>32766)throw f.NUM;const a=e*12+(o-1),i=Math.floor(a/12),s=a-i*12+1,M=y(i,s,1)+r-1;if(M<n||M>N)throw f.NUM;return M-n}h("DATE",3,3,(t,o,r,n)=>G(Math.trunc(u(o)),Math.trunc(u(r)),Math.trunc(u(n)),t)),h("YEAR",1,1,(t,o)=>d(c(o,t)).year),h("MONTH",1,1,(t,o)=>d(c(o,t)).month),h("DAY",1,1,(t,o)=>d(c(o,t)).day);const L=t=>{const o=u(t);if(o<0)throw f.NUM;if(o>=N+1)throw f.NUM;return Math.round((o-Math.floor(o))*86400)};p("HOUR",1,1,t=>Math.floor(L(t)/3600)%24),p("MINUTE",1,1,t=>Math.floor(L(t)%3600/60)),p("SECOND",1,1,t=>L(t)%60),p("TIME",3,3,(t,o,r)=>{const n=Math.trunc(u(t))*3600+Math.trunc(u(o))*60+Math.trunc(u(r));if(n<0)throw f.NUM;return n%86400/86400});const R=()=>{const t=new Date;return{whole:y(t.getFullYear(),t.getMonth()+1,t.getDate()),frac:(t.getHours()*3600+t.getMinutes()*60+t.getSeconds()+t.getMilliseconds()/1e3)/86400}};h("NOW",0,0,t=>{const o=R();return o.whole-t+o.frac},{volatile:!0}),h("TODAY",0,0,t=>R().whole-t,{volatile:!0});function O(t){const o=t.trim(),r=q(o);return r.kind===P&&r.format&&$(r.format)?r.value:null}h("DATEVALUE",1,1,(t,o)=>{if(typeof o!="string")throw f.VALUE;const r=o.split(/\s+\d{1,2}:\d{2}/)[0],n=O(r);if(n===null||n<t)throw f.VALUE;return Math.floor(n)-t}),p("TIMEVALUE",1,1,t=>{if(typeof t!="string")throw f.VALUE;const o=/\b(\d+)\s*:(\d*)(?::(\d+(?:\.\d+)?))?\s*(am|pm|a|p)?/i.exec(t)??/^\s*(\d+)()()\s+(am|pm|a|p)\s*$/i.exec(t);if(!o)throw f.VALUE;let r=Number(o[1]);if(o[4]){const n=o[4][0].toLowerCase()==="p";if(r>12)throw f.VALUE;r=r%12+(n?12:0)}return(r*3600+Number(o[2])*60+Number(o[3]||0))/86400%1}),h("DAYS",2,2,(t,o,r)=>c(o,t)-c(r,t)),h("DAYS360",2,3,(t,o,r,n)=>_(c(o,t),c(r,t),w(n)?!1:!!u(n)));function J(t,o){const r=d(t),n=r.year*12+(r.month-1)+o,e=Math.floor(n/12),a=n-e*12+1;if(e<1900||e>9999)throw f.NUM;return y(e,a,Math.min(r.day,k(e,a)))}function k(t,o){return o===2?t%4===0&&(t%100!==0||t%400===0)?29:28:[31,28,31,30,31,30,31,31,30,31,30,31][o-1]}h("EDATE",2,2,(t,o,r)=>J(c(o,t),Math.trunc(u(r)))-t),h("EOMONTH",2,2,(t,o,r)=>{const n=d(c(o,t)),e=n.year*12+(n.month-1)+Math.trunc(u(r)),a=Math.floor(e/12),i=e-a*12+1;if(a<1900||a>9999)throw f.NUM;return y(a,i,k(a,i))-t}),h("WEEKDAY",1,2,(t,o,r)=>{const n=c(o,t),e=r===void 0?1:r===S?0:Math.trunc(u(r)),a=d(n).weekday,i={1:0,2:1,3:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[e];if(i===void 0)throw f.NUM;const s=(a-i+7)%7;return e===3?s:s+1}),h("ISOWEEKNUM",1,1,(t,o)=>V(c(o,t)));function V(t){const o=(d(t).weekday+6)%7,r=t-o+3,n=d(r).year;return Math.floor((r-y(n,1,1))/7)+1}h("WEEKNUM",1,2,(t,o,r)=>{const n=c(o,t),e=w(r)?1:Math.trunc(u(r));if(e===21)return V(n);const a={1:0,2:1,11:1,12:2,13:3,14:4,15:5,16:6,17:0}[e];if(a===void 0)throw f.NUM;const i=y(d(n).year,1,1),s=(d(i).weekday-a+7)%7;return Math.floor((n-i+s)/7)+1});function A(t){if(w(t))return[!1,!1,!1,!1,!1,!0,!0];if(typeof t=="string"){if(!/^[01]{7}$/.test(t))throw f.VALUE;return[...t].map(n=>n==="1")}const o=Math.trunc(u(t)),r=new Array(7).fill(!1);if(o>=1&&o<=7)r[(o+4)%7]=!0,r[(o+5)%7]=!0;else if(o>=11&&o<=17)r[(o-5)%7]=!0;else throw f.NUM;return r}function D(t,o){const r=new Set;return w(o)||o===null||j(t,o,n=>{if(n instanceof X)throw n;if(typeof n=="number")r.add(Math.floor(n)+t.book.dateOffset);else if(typeof n=="string"){const e=O(n);if(e===null)throw f.VALUE;r.add(Math.floor(e))}}),r}const Y=(t,o)=>o[(d(t).weekday+6)%7];function W(t,o,r,n){const e=o>=t?1:-1,a=Math.min(t,o),i=Math.max(t,o),s=r.filter(l=>!l).length,M=i-a+1;let m=Math.floor(M/7)*s;for(let l=a+Math.floor(M/7)*7;l<=i;l++)Y(l,r)||m++;for(const l of n)l>=a&&l<=i&&!Y(l,r)&&m--;return m*e}const E=(t,o,r,n)=>{const e=T(t,o,r,n);if(typeof e=="string"){const a=O(e);if(a===null)throw f.VALUE;return Math.floor(a)}return c(e,t.book.dateOffset)};U("NETWORKDAYS",2,3,(t,o,r,n)=>W(E(t,o[0],r,n),E(t,o[1],r,n),A(void 0),D(t,o[2]))),U("NETWORKDAYS.INTL",2,4,(t,o,r,n)=>W(E(t,o[0],r,n),E(t,o[1],r,n),A(w(o[2])?void 0:t.scalar(o[2],r,n)),D(t,o[3])));function I(t,o,r,n){const e=Math.trunc(o),a=e<0?-1:1;let i=Math.abs(e),s=t;if(r.every(Boolean))throw f.VALUE;for(;i>0;){if(s+=a,s<0||s>N)throw f.NUM;!Y(s,r)&&!n.has(s)&&i--}return s}U("WORKDAY",2,3,(t,o,r,n)=>I(E(t,o[0],r,n),u(T(t,o[1],r,n)),A(void 0),D(t,o[2]))-t.book.dateOffset),U("WORKDAY.INTL",2,4,(t,o,r,n)=>I(E(t,o[0],r,n),u(T(t,o[1],r,n)),A(w(o[2])?void 0:t.scalar(o[2],r,n)),D(t,o[3]))-t.book.dateOffset),h("YEARFRAC",2,3,(t,o,r,n)=>{const e=w(n)?0:Math.trunc(u(n));if(e<0||e>4)throw f.NUM;return z(c(o,t),c(r,t),e)}),h("DATEDIF",3,3,(t,o,r,n)=>{const e=c(o,t),a=c(r,t);if(e>a)throw f.NUM;const i=d(e),s=d(a),M=B(n).toUpperCase();let m=(s.year-i.year)*12+s.month-i.month;switch(s.day<i.day&&m--,M){case"D":return a-e;case"M":return m;case"Y":return Math.floor(m/12);case"YM":return m%12;case"YD":{let l=s.year;return(s.month<i.month||s.month===i.month&&s.day<i.day)&&l--,a-(y(l,i.month,1)+i.day-1)}case"MD":{if(s.day>=i.day)return s.day-i.day;const l=s.month===1?12:s.month-1,b=s.month===1?s.year-1:s.year;return k(b,l)-i.day+s.day}default:throw f.NUM}});export{C as Arr,F as Ref,H as RefList,K as alias};
|