@univerjs-pro/engine-formula 1.5.2 → 1.6.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/lib/es/worker.js CHANGED
@@ -1 +1 @@
1
- export{t as UniverProFormulaEnginePlugin}from"./plugin-CnOguRnb.js";
1
+ export{t as UniverProFormulaEnginePlugin}from"./plugin-ZrkSfRv2.js";
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- import{A as e,C as t,D as n,E as r,O as i,S as a,T as o,_ as s,a as c,b as l,c as u,d as ee,f as te,g as d,h as f,i as p,j as m,k as ne,l as h,m as g,n as _,o as v,p as y,r as b,s as re,t as ie,u as ae,v as x,w as oe,x as S,y as se}from"./plugin-CnOguRnb.js";import{CellValueType as C,DEFAULT_NUMBER_FORMAT as w,Disposable as T,Inject as E,ObjectMatrix as D,Tools as O,numfmt as k}from"@univerjs/core";import{FormulaCalculationTriggerService as A,FormulaDataModel as j,FormulaDataModel as M,IActiveDirtyManagerService as N,ISuperTableService as P,RemoveSuperTableMutation as F,SetFormulaCalculationResultMutation as I,SetFormulaCalculationStartMutation as L,SetFormulaDataMutation as R,SetSuperTableMutation as z,SetTriggerFormulaCalculationStartMutation as B,deserializeRangeWithSheet as V,normalizeFormulaUnitName as H,refactorFormulaUnitQualifier as U,sequenceNodeType as W,serializeRangeWithSheet as G,serializeRangeWithSpreadsheet as K,splitTableStructuredRef as ce}from"@univerjs/engine-formula";const le=/'?\[([^\]]+)\]([^'!]+)'?!\$?([A-Za-z]+)\$?([1-9]\d*)(?:\s*:\s*\$?([A-Za-z]+)\$?([1-9]\d*))?/g,ue=/(?:'((?:[^']|'')+)'|\[([^\]]+)\]|([A-Za-z0-9_.-]+))!([^\s!\[\]]+)\[/g,de=/\bINDIRECT\s*\(\s*"((?:[^"]|"")*)"/gi;function fe(e){let t=``,n=!1;for(let r=0;r<e.length;r++){let i=e[r];i===`"`?n&&e[r+1]===`"`?r++:(n=!n,t+=` `):n||(t+=i)}let r=[t];for(let t of e.matchAll(de))r.push(t[1].replace(/""/g,`"`));return r}let q=class{constructor(e){this._externalReferenceModel=e,m(this,`_calculationCounter`,0),m(this,`_requestCounter`,0)}plan(e){let t=`external-calculation-${++this._calculationCounter}`,n=/* @__PURE__ */ new Map;for(let[r,i]of Object.entries(e.formulaData??{}))if(this._externalReferenceModel.get(r))for(let e of Object.values(i??{}))for(let i of Object.values(e??{}))for(let e of Object.values(i??{})){let i=pe(e);if(typeof i==`string`)for(let e of fe(i))this._collectA1(n,t,r,e),this._collectTables(n,t,r,e)}return Array.from(n.values())}planDynamic(e,t){let n=`external-calculation-${++this._calculationCounter}`,r=/* @__PURE__ */ new Map,i=this._externalReferenceModel.get(e);if(!i)return[];for(let a of t){let t=i.links.find(t=>S(e,t.slot)===a.unitId);t&&a.sheetName&&a.range&&this._addRange(r,n,e,t,{sheetName:a.sheetName,range:a.range})}return Array.from(r.values())}_collectA1(e,t,n,r){for(let i of r.matchAll(le)){let r=this._externalReferenceModel.resolveBinding(n,i[1]);if(r.status!==`resolved`)continue;let a=O.ABCatNum(i[3]),o=O.ABCatNum(i[5]??i[3]),s=Number(i[4])-1,c=Number(i[6]??i[4])-1;this._addRange(e,t,n,r.link,{sheetName:i[2].replace(/''/g,`'`),range:{startRow:Math.min(s,c),endRow:Math.max(s,c),startColumn:Math.min(a,o),endColumn:Math.max(a,o)}})}}_collectTables(e,t,n,r){for(let i of r.matchAll(ue)){let r=(i[1]??i[2]??i[3]).replace(/''/g,`'`),a=this._externalReferenceModel.resolveBinding(n,r);if(a.status!==`resolved`)continue;let o=a.link.tables.find(e=>e.name.toLowerCase()===i[4].toLowerCase());this._addRange(e,t,n,a.link,{tableName:i[4],sheetName:o==null?void 0:o.sheetName,range:o==null?void 0:o.range})}}_addRange(e,t,n,r,i){let a=`${n}\0${r.slot}`,o=e.get(a)??{requestId:`external-request-${++this._requestCounter}`,calculationId:t,hostUnitId:n,bindingSlot:r.slot,syntheticUnitId:S(n,r.slot),target:r.target,ranges:[],requireFresh:!1};o.ranges.some(e=>JSON.stringify(e)===JSON.stringify(i))||o.ranges.push(i),e.set(a,o)}};q=d([s(0,E(x))],q);function pe(e){return typeof e==`object`&&e&&`f`in e&&typeof e.f==`string`?e.f:void 0}const me={maxCells:1e5,maxBytes:5e6,concurrency:4,timeoutMs:15e3};let J=class extends T{constructor(e,t,n){super(),this._referenceDataService=e,this._overlayModel=t,this._formulaDataModel=n,m(this,`_inflight`,/* @__PURE__ */ new Map),m(this,`_latestCalculation`,/* @__PURE__ */ new Map)}async prefetch(e,t,n={}){this._hydrateLiveUnits(t);let r={...me,...n},i=he(e).map(e=>({...e,ranges:e.ranges.filter(t=>!t.range||!this._overlayModel.isRangeCovered(e.syntheticUnitId,t.sheetId??t.sheetName,t.range))})).filter(e=>e.ranges.length>0);ge(i,r);for(let e of i)this._latestCalculation.set(e.hostUnitId,e.calculationId);let a=[],o=[],s=0,c=0,l=Array.from({length:Math.min(r.concurrency,i.length)},async()=>{for(;c<i.length;){let e=i[c++],n=this._hasWorkerLiveUnit(e,t)?ve(e):await this._readWithInflight(e,r.timeoutMs);if(this._latestCalculation.get(e.hostUnitId)!==e.calculationId||n.calculationId!==e.calculationId)s++;else if(a.push(n),n.source!==`worker-live`){let t=this._overlayModel.apply(e,n);this._overlayModel.enforceLimits(r.maxCells,r.maxBytes);for(let n of t)o.push({unitId:e.syntheticUnitId,...n})}}});return await Promise.all(l),this._overlayModel.materialize(t),{responses:a,dirtyRanges:o,discarded:s}}cancelHost(e,t){this._latestCalculation.set(e,t)}getInflightCount(){return this._inflight.size}drainDirtyRanges(){return this._overlayModel.drainDirtyRanges()}dispose(){super.dispose(),this._inflight.clear(),this._latestCalculation.clear()}_hydrateLiveUnits(e){let t=this._formulaDataModel.getCalculateData();e.allUnitData={...e.allUnitData,...t.allUnitData},e.unitSheetNameMap={...e.unitSheetNameMap,...t.unitSheetNameMap},e.unitStylesData={...e.unitStylesData,...t.unitStylesData},e.unitNameMap={...e.unitNameMap,...t.unitNameMap}}_hasWorkerLiveUnit(e,t){let n=t.allUnitData??{};if(e.target.liveUnitId&&e.target.liveUnitId!==e.syntheticUnitId&&n[e.target.liveUnitId])return!0;let r=Object.entries(t.unitNameMap??{}).filter(([t,r])=>t!==e.syntheticUnitId&&!t.startsWith(`external:`)&&!!n[t]&&r.unitType===e.target.unitType),i=e.target.name.toLowerCase(),a=r.filter(([,e])=>e.name.toLowerCase()===i);if(a.length>0)return a.length===1;let o=H(e.target.name);return r.filter(([,e])=>H(e.name)===o).length===1}_readWithInflight(e,t){let n=_e(e),r=this._inflight.get(n);if(r)return r.then(t=>({...t,requestId:e.requestId,calculationId:e.calculationId}));let i=Promise.race([this._referenceDataService.readData(e),new Promise(n=>{setTimeout(()=>n(g(e)),t)})]).finally(()=>this._inflight.delete(n));return this._inflight.set(n,i),i}};J=d([s(0,y),s(1,E(h)),s(2,E(M))],J);function he(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=`${n.hostUnitId}\0${n.bindingSlot}\0${n.calculationId}`,r=t.get(e);if(!r)t.set(e,{...n,ranges:[...n.ranges]});else for(let e of n.ranges)r.ranges.some(t=>JSON.stringify(t)===JSON.stringify(e))||r.ranges.push(e)}return Array.from(t.values())}function ge(e,t){let n=0;for(let t of e)for(let e of t.ranges)e.range&&(n+=(e.range.endRow-e.range.startRow+1)*(e.range.endColumn-e.range.startColumn+1));if(n>t.maxCells)throw Error(`External formula prefetch cell limit exceeded`);if(JSON.stringify(e).length>t.maxBytes)throw Error(`External formula prefetch byte limit exceeded`)}function _e(e){return`${e.hostUnitId}\0${e.bindingSlot}\0${JSON.stringify(e.ranges)}`}function ve(e){return{requestId:e.requestId,calculationId:e.calculationId,source:`worker-live`,freshness:`fresh`,sheets:[]}}let ye=/* @__PURE__ */ function(e){return e.SHEET_RANGE=`sheet-range`,e.TABLE_COLUMN=`table-column`,e}({});function be(e){let{hostUnitId:t,unit:n,target:r}=e,i=n.unitId===t;if(r.kind===`sheet-range`)return i?G(r.sheetName,r.range):K(n.formulaQualifier,r.sheetName,r.range);let a=r.endColumnName&&r.endColumnName!==r.columnName?`[[${Y(r.columnName)}]:[${Y(r.endColumnName)}]]`:`[${Y(r.columnName)}]`,o=`${xe(r.tableName)}${a}`;return i?o:`[${Se(n.formulaQualifier)}]!${o}`}function xe(e){return/[\s!'\[\]]/.test(e)?`'${e.replaceAll(`'`,`''`)}'`:e}function Y(e){return e.replaceAll(`]`,`]]`)}function Se(e){return e.replaceAll(`]`,`]]`)}function Ce(e){var t,n,r;let i=X(e.result),a=i??Te(e.lastValue),o=i?`calculated`:`persisted`,s=i==null&&e.lastValue!=null,c=((t=e.numberFormat)==null?void 0:t.pattern)??$(i)??((n=e.lastValue)==null?void 0:n.pattern)??w,l=a==null?void 0:a.v,u=l==null||(r=k.formatColor(c,l,{throws:!1}))==null?void 0:r.toString();return l==null?{text:``,pattern:c,cell:a,source:o,stale:s}:(a==null?void 0:a.t)===C.BOOLEAN?{text:l?`TRUE`:`FALSE`,color:u,pattern:c,cell:a,source:o,stale:s}:(a==null?void 0:a.t)===C.NUMBER?{text:k.format(c,l,{throws:!1}),color:u,pattern:c,cell:a,source:o,stale:s}:{text:String(l),color:u,pattern:c,cell:a,source:o,stale:s}}function X(e){let{cell:t,count:n}=Z(e);return n>1?{v:`#VALUE!`,t:C.STRING}:t}function we(e){let{cell:t,count:n}=Z(e);return n===1?t:void 0}function Z(e){if(!(e!=null&&e.result))return{cell:void 0,count:0};let t=new D(e.result),n=0,r;return t.forValue((e,t,i)=>{for(let e of i)for(let t of e)t!=null&&(n+=1,r=t)}),{cell:r,count:n}}function Q(e){if(e.v!==null&&typeof e.v!=`string`&&typeof e.v!=`number`&&typeof e.v!=`boolean`)return null;let t=$(e);return{v:e.v??null,...e.t==null?null:{t:e.t},...t==null?null:{pattern:t}}}function Te(e){if(e)return{v:e.v,...e.t==null?null:{t:e.t}}}function $(e){var t;return e!=null&&e.s&&typeof e.s==`object`?(t=e.s.n)==null?void 0:t.pattern:void 0}function Ee(t){let n=/* @__PURE__ */ new Map;for(let r of t){if(typeof r==`string`)continue;let t=``;r.nodeType===W.REFERENCE?t=V(r.token).unitId:r.nodeType===W.TABLE&&(t=ce(r.token).unitQualifier);let i=t.trim();i&&n.set(e(i),i)}return Array.from(n.values())}function De(t){let n=/* @__PURE__ */ new Map;for(let r of t.explicitReferences){let t=e(r.qualifier),i=n.get(t);if(i&&(i.sourceUnitId!==r.sourceUnitId||i.sourceUnitType!==r.sourceUnitType))return{status:`ambiguous`,qualifier:r.qualifier};n.set(t,r)}let r=[];for(let i of t.qualifiers){let a=n.get(e(i));if(a){r.push(a);continue}let o=t.resolveBinding(i);if(o.status!==`resolved`)return{status:o.status,qualifier:i};r.push(o.reference)}return{status:`resolved`,references:r}}export{f as ExternalCacheReferenceDataProvider,p as ExternalFormulaUnitReferenceResolver,ae as ExternalReferenceDataProvidersController,x as ExternalReferenceModel,h as ExternalReferenceOverlayModel,q as ExternalReferencePrefetchPlanner,J as ExternalReferencePrefetchService,u as ExternalReferenceRenameController,re as ExternalReferenceResourceController,c as FORMULA_REFERENCE_DATA_RPC_CHANNEL,b as FormulaCacheEligibilityService,A as FormulaCalculationTriggerService,j as FormulaDataModel,t as FormulaLastValuePersistenceService,ee as FormulaReferenceDataProviderRegistry,v as FormulaReferenceDataRpcController,ye as FormulaReferenceType,i as HostExternalReferenceModel,N as IActiveDirtyManagerService,te as IFormulaReferenceDataProviderRegistry,y as IFormulaReferenceDataService,P as ISuperTableService,_ as MainFormulaReferenceDataService,a as PersistFormulaLastValuesCommand,oe as RemoveHostExternalReferenceCommand,F as RemoveSuperTableMutation,se as SHEET_EXTERNAL_DATA_PLUGIN,I as SetFormulaCalculationResultMutation,L as SetFormulaCalculationStartMutation,R as SetFormulaDataMutation,z as SetSuperTableMutation,B as SetTriggerFormulaCalculationStartMutation,ne as UNIVER_EXTERNAL_REFERENCE_PLUGIN,ie as UniverProFormulaEnginePlugin,o as UpsertHostExternalReferenceCommand,r as UpsertHostExternalReferencesCommand,be as buildFormulaReference,n as buildUpsertHostExternalReferenceMutations,Ee as collectFormulaExternalReferenceQualifiers,l as createEmptyExternalReferenceResource,S as createExternalSyntheticUnitId,g as createUnavailableReferenceDataResponse,X as getFormulaResultCell,we as getScalarFormulaResultCell,e as normalizeHostExternalReferenceQualifier,U as refactorFormulaUnitQualifier,Ce as resolveFormulaResultPresentation,De as resolveHostFormulaExternalReferences,Q as toFormulaLastValue};
1
+ import{A as e,C as t,D as n,E as r,O as i,S as a,T as o,_ as s,a as c,b as l,c as u,d as ee,f as te,g as d,h as f,i as p,j as m,k as ne,l as h,m as g,n as _,o as v,p as y,r as b,s as re,t as ie,u as ae,v as x,w as oe,x as S,y as se}from"./plugin-ZrkSfRv2.js";import{CellValueType as C,DEFAULT_NUMBER_FORMAT as w,Disposable as T,Inject as E,ObjectMatrix as D,Tools as O,numfmt as k}from"@univerjs/core";import{FormulaCalculationTriggerService as A,FormulaDataModel as j,FormulaDataModel as M,IActiveDirtyManagerService as N,ISuperTableService as P,RemoveSuperTableMutation as F,SetFormulaCalculationResultMutation as I,SetFormulaCalculationStartMutation as L,SetFormulaDataMutation as R,SetSuperTableMutation as z,SetTriggerFormulaCalculationStartMutation as B,deserializeRangeWithSheet as V,normalizeFormulaUnitName as H,refactorFormulaUnitQualifier as U,sequenceNodeType as W,serializeRangeWithSheet as G,serializeRangeWithSpreadsheet as K,splitTableStructuredRef as ce}from"@univerjs/engine-formula";const le=/'?\[([^\]]+)\]([^'!]+)'?!\$?([A-Za-z]+)\$?([1-9]\d*)(?:\s*:\s*\$?([A-Za-z]+)\$?([1-9]\d*))?/g,ue=/(?:'((?:[^']|'')+)'|\[([^\]]+)\]|([A-Za-z0-9_.-]+))!([^\s!\[\]]+)\[/g,de=/\bINDIRECT\s*\(\s*"((?:[^"]|"")*)"/gi;function fe(e){let t=``,n=!1;for(let r=0;r<e.length;r++){let i=e[r];i===`"`?n&&e[r+1]===`"`?r++:(n=!n,t+=` `):n||(t+=i)}let r=[t];for(let t of e.matchAll(de))r.push(t[1].replace(/""/g,`"`));return r}let q=class{constructor(e){this._externalReferenceModel=e,m(this,`_calculationCounter`,0),m(this,`_requestCounter`,0)}plan(e){let t=`external-calculation-${++this._calculationCounter}`,n=/* @__PURE__ */ new Map;for(let[r,i]of Object.entries(e.formulaData??{}))if(this._externalReferenceModel.get(r))for(let e of Object.values(i??{}))for(let i of Object.values(e??{}))for(let e of Object.values(i??{})){let i=pe(e);if(typeof i==`string`)for(let e of fe(i))this._collectA1(n,t,r,e),this._collectTables(n,t,r,e)}return Array.from(n.values())}planDynamic(e,t){let n=`external-calculation-${++this._calculationCounter}`,r=/* @__PURE__ */ new Map,i=this._externalReferenceModel.get(e);if(!i)return[];for(let a of t){let t=i.links.find(t=>S(e,t.slot)===a.unitId);t&&a.sheetName&&a.range&&this._addRange(r,n,e,t,{sheetName:a.sheetName,range:a.range})}return Array.from(r.values())}_collectA1(e,t,n,r){for(let i of r.matchAll(le)){let r=this._externalReferenceModel.resolveBinding(n,i[1]);if(r.status!==`resolved`)continue;let a=O.ABCatNum(i[3]),o=O.ABCatNum(i[5]??i[3]),s=Number(i[4])-1,c=Number(i[6]??i[4])-1;this._addRange(e,t,n,r.link,{sheetName:i[2].replace(/''/g,`'`),range:{startRow:Math.min(s,c),endRow:Math.max(s,c),startColumn:Math.min(a,o),endColumn:Math.max(a,o)}})}}_collectTables(e,t,n,r){for(let i of r.matchAll(ue)){let r=(i[1]??i[2]??i[3]).replace(/''/g,`'`),a=this._externalReferenceModel.resolveBinding(n,r);if(a.status!==`resolved`)continue;let o=a.link.tables.find(e=>e.name.toLowerCase()===i[4].toLowerCase());this._addRange(e,t,n,a.link,{tableName:i[4],sheetName:o==null?void 0:o.sheetName,range:o==null?void 0:o.range})}}_addRange(e,t,n,r,i){let a=`${n}\0${r.slot}`,o=e.get(a)??{requestId:`external-request-${++this._requestCounter}`,calculationId:t,hostUnitId:n,bindingSlot:r.slot,syntheticUnitId:S(n,r.slot),target:r.target,ranges:[],requireFresh:!1};o.ranges.some(e=>JSON.stringify(e)===JSON.stringify(i))||o.ranges.push(i),e.set(a,o)}};q=d([s(0,E(x))],q);function pe(e){return typeof e==`object`&&e&&`f`in e&&typeof e.f==`string`?e.f:void 0}const me={maxCells:1e5,maxBytes:5e6,concurrency:4,timeoutMs:15e3};let J=class extends T{constructor(e,t,n){super(),this._referenceDataService=e,this._overlayModel=t,this._formulaDataModel=n,m(this,`_inflight`,/* @__PURE__ */ new Map),m(this,`_latestCalculation`,/* @__PURE__ */ new Map)}async prefetch(e,t,n={}){this._hydrateLiveUnits(t);let r={...me,...n},i=he(e).map(e=>({...e,ranges:e.ranges.filter(t=>!t.range||!this._overlayModel.isRangeCovered(e.syntheticUnitId,t.sheetId??t.sheetName,t.range))})).filter(e=>e.ranges.length>0);ge(i,r);for(let e of i)this._latestCalculation.set(e.hostUnitId,e.calculationId);let a=[],o=[],s=0,c=0,l=Array.from({length:Math.min(r.concurrency,i.length)},async()=>{for(;c<i.length;){let e=i[c++],n=this._hasWorkerLiveUnit(e,t)?ve(e):await this._readWithInflight(e,r.timeoutMs);if(this._latestCalculation.get(e.hostUnitId)!==e.calculationId||n.calculationId!==e.calculationId)s++;else if(a.push(n),n.source!==`worker-live`){let t=this._overlayModel.apply(e,n);this._overlayModel.enforceLimits(r.maxCells,r.maxBytes);for(let n of t)o.push({unitId:e.syntheticUnitId,...n})}}});return await Promise.all(l),this._overlayModel.materialize(t),{responses:a,dirtyRanges:o,discarded:s}}cancelHost(e,t){this._latestCalculation.set(e,t)}getInflightCount(){return this._inflight.size}drainDirtyRanges(){return this._overlayModel.drainDirtyRanges()}dispose(){super.dispose(),this._inflight.clear(),this._latestCalculation.clear()}_hydrateLiveUnits(e){let t=this._formulaDataModel.getCalculateData();e.allUnitData={...e.allUnitData,...t.allUnitData},e.unitSheetNameMap={...e.unitSheetNameMap,...t.unitSheetNameMap},e.unitStylesData={...e.unitStylesData,...t.unitStylesData},e.unitNameMap={...e.unitNameMap,...t.unitNameMap}}_hasWorkerLiveUnit(e,t){let n=t.allUnitData??{};if(e.target.liveUnitId&&e.target.liveUnitId!==e.syntheticUnitId&&n[e.target.liveUnitId])return!0;let r=Object.entries(t.unitNameMap??{}).filter(([t,r])=>t!==e.syntheticUnitId&&!t.startsWith(`external:`)&&!!n[t]&&r.unitType===e.target.unitType),i=e.target.name.toLowerCase(),a=r.filter(([,e])=>e.name.toLowerCase()===i);if(a.length>0)return a.length===1;let o=H(e.target.name);return r.filter(([,e])=>H(e.name)===o).length===1}_readWithInflight(e,t){let n=_e(e),r=this._inflight.get(n);if(r)return r.then(t=>({...t,requestId:e.requestId,calculationId:e.calculationId}));let i=Promise.race([this._referenceDataService.readData(e),new Promise(n=>{setTimeout(()=>n(g(e)),t)})]).finally(()=>this._inflight.delete(n));return this._inflight.set(n,i),i}};J=d([s(0,y),s(1,E(h)),s(2,E(M))],J);function he(e){let t=/* @__PURE__ */ new Map;for(let n of e){let e=`${n.hostUnitId}\0${n.bindingSlot}\0${n.calculationId}`,r=t.get(e);if(!r)t.set(e,{...n,ranges:[...n.ranges]});else for(let e of n.ranges)r.ranges.some(t=>JSON.stringify(t)===JSON.stringify(e))||r.ranges.push(e)}return Array.from(t.values())}function ge(e,t){let n=0;for(let t of e)for(let e of t.ranges)e.range&&(n+=(e.range.endRow-e.range.startRow+1)*(e.range.endColumn-e.range.startColumn+1));if(n>t.maxCells)throw Error(`External formula prefetch cell limit exceeded`);if(JSON.stringify(e).length>t.maxBytes)throw Error(`External formula prefetch byte limit exceeded`)}function _e(e){return`${e.hostUnitId}\0${e.bindingSlot}\0${JSON.stringify(e.ranges)}`}function ve(e){return{requestId:e.requestId,calculationId:e.calculationId,source:`worker-live`,freshness:`fresh`,sheets:[]}}let ye=/* @__PURE__ */ function(e){return e.SHEET_RANGE=`sheet-range`,e.TABLE_COLUMN=`table-column`,e}({});function be(e){let{hostUnitId:t,unit:n,target:r}=e,i=n.unitId===t;if(r.kind===`sheet-range`)return i?G(r.sheetName,r.range):K(n.formulaQualifier,r.sheetName,r.range);let a=r.endColumnName&&r.endColumnName!==r.columnName?`[[${Y(r.columnName)}]:[${Y(r.endColumnName)}]]`:`[${Y(r.columnName)}]`,o=`${xe(r.tableName)}${a}`;return i?o:`[${Se(n.formulaQualifier)}]!${o}`}function xe(e){return/[\s!'\[\]]/.test(e)?`'${e.replaceAll(`'`,`''`)}'`:e}function Y(e){return e.replaceAll(`]`,`]]`)}function Se(e){return e.replaceAll(`]`,`]]`)}function Ce(e){var t,n,r;let i=X(e.result),a=i??Te(e.lastValue),o=i?`calculated`:`persisted`,s=i==null&&e.lastValue!=null,c=((t=e.numberFormat)==null?void 0:t.pattern)??$(i)??((n=e.lastValue)==null?void 0:n.pattern)??w,l=a==null?void 0:a.v,u=l==null||(r=k.formatColor(c,l,{throws:!1}))==null?void 0:r.toString();return l==null?{text:``,pattern:c,cell:a,source:o,stale:s}:(a==null?void 0:a.t)===C.BOOLEAN?{text:l?`TRUE`:`FALSE`,color:u,pattern:c,cell:a,source:o,stale:s}:(a==null?void 0:a.t)===C.NUMBER?{text:k.format(c,l,{throws:!1}),color:u,pattern:c,cell:a,source:o,stale:s}:{text:String(l),color:u,pattern:c,cell:a,source:o,stale:s}}function X(e){let{cell:t,count:n}=Z(e);return n>1?{v:`#VALUE!`,t:C.STRING}:t}function we(e){let{cell:t,count:n}=Z(e);return n===1?t:void 0}function Z(e){if(!(e!=null&&e.result))return{cell:void 0,count:0};let t=new D(e.result),n=0,r;return t.forValue((e,t,i)=>{for(let e of i)for(let t of e)t!=null&&(n+=1,r=t)}),{cell:r,count:n}}function Q(e){if(e.v!==null&&typeof e.v!=`string`&&typeof e.v!=`number`&&typeof e.v!=`boolean`)return null;let t=$(e);return{v:e.v??null,...e.t==null?null:{t:e.t},...t==null?null:{pattern:t}}}function Te(e){if(e)return{v:e.v,...e.t==null?null:{t:e.t}}}function $(e){var t;return e!=null&&e.s&&typeof e.s==`object`?(t=e.s.n)==null?void 0:t.pattern:void 0}function Ee(t){let n=/* @__PURE__ */ new Map;for(let r of t){if(typeof r==`string`)continue;let t=``;r.nodeType===W.REFERENCE?t=V(r.token).unitId:r.nodeType===W.TABLE&&(t=ce(r.token).unitQualifier);let i=t.trim();i&&n.set(e(i),i)}return Array.from(n.values())}function De(t){let n=/* @__PURE__ */ new Map;for(let r of t.explicitReferences){let t=e(r.qualifier),i=n.get(t);if(i&&(i.sourceUnitId!==r.sourceUnitId||i.sourceUnitType!==r.sourceUnitType))return{status:`ambiguous`,qualifier:r.qualifier};n.set(t,r)}let r=[];for(let i of t.qualifiers){let a=n.get(e(i));if(a){r.push(a);continue}let o=t.resolveBinding(i);if(o.status!==`resolved`)return{status:o.status,qualifier:i};r.push(o.reference)}return{status:`resolved`,references:r}}export{f as ExternalCacheReferenceDataProvider,p as ExternalFormulaUnitReferenceResolver,ae as ExternalReferenceDataProvidersController,x as ExternalReferenceModel,h as ExternalReferenceOverlayModel,q as ExternalReferencePrefetchPlanner,J as ExternalReferencePrefetchService,u as ExternalReferenceRenameController,re as ExternalReferenceResourceController,c as FORMULA_REFERENCE_DATA_RPC_CHANNEL,b as FormulaCacheEligibilityService,A as FormulaCalculationTriggerService,j as FormulaDataModel,t as FormulaLastValuePersistenceService,ee as FormulaReferenceDataProviderRegistry,v as FormulaReferenceDataRpcController,ye as FormulaReferenceType,i as HostExternalReferenceModel,N as IActiveDirtyManagerService,te as IFormulaReferenceDataProviderRegistry,y as IFormulaReferenceDataService,P as ISuperTableService,_ as MainFormulaReferenceDataService,a as PersistFormulaLastValuesCommand,oe as RemoveHostExternalReferenceCommand,F as RemoveSuperTableMutation,se as SHEET_EXTERNAL_DATA_PLUGIN,I as SetFormulaCalculationResultMutation,L as SetFormulaCalculationStartMutation,R as SetFormulaDataMutation,z as SetSuperTableMutation,B as SetTriggerFormulaCalculationStartMutation,ne as UNIVER_EXTERNAL_REFERENCE_PLUGIN,ie as UniverProFormulaEnginePlugin,o as UpsertHostExternalReferenceCommand,r as UpsertHostExternalReferencesCommand,be as buildFormulaReference,n as buildUpsertHostExternalReferenceMutations,Ee as collectFormulaExternalReferenceQualifiers,l as createEmptyExternalReferenceResource,S as createExternalSyntheticUnitId,g as createUnavailableReferenceDataResponse,X as getFormulaResultCell,we as getScalarFormulaResultCell,e as normalizeHostExternalReferenceQualifier,U as refactorFormulaUnitQualifier,Ce as resolveFormulaResultPresentation,De as resolveHostFormulaExternalReferences,Q as toFormulaLastValue};