@jsb188/mday 1.1.11 → 1.1.12
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/package.json +58 -23
- package/src/constants/agentMemory.ts +14 -45
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +5 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +156 -47
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +12 -38
- package/src/constants/dataTableFieldTypes.generated.ts +3 -7
- package/src/constants/docAccess.ts +3 -2
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +114 -12
- package/src/constants/fieldDoc.ts +74 -4
- package/src/constants/graphqlReactivity.ts +108 -25
- package/src/constants/log.ts +9 -0
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +1834 -730
- package/src/constants/organization.ts +13 -14
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +65 -101
- package/src/constants/sms.ts +18 -0
- package/src/constants/storage.ts +33 -9
- package/src/constants/usage.ts +3 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +106 -283
- package/src/schemas/inboundContact.ts +5 -5
- package/src/types/agentMemory.d.ts +38 -14
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1043 -193
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +102 -309
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +144 -55
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +579 -150
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +8 -0
- package/src/types/log.d.ts +94 -3
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +8 -3
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +180 -463
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +266 -27
- package/src/types/workflow.d.ts +196 -66
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +31 -14
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +642 -209
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +18 -556
- package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/email.ts +208 -1
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +81 -37
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +232 -141
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logStoreRead.ts +9 -13
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomyDisplay.ts +80 -124
- package/src/utils/noteDoc.ts +66 -284
- package/src/utils/organization.ts +117 -283
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +123 -4834
- package/src/utils/sheetAxisMove.ts +77 -291
- package/src/utils/sheetBootstrap.ts +771 -1138
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +23 -88
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +17 -69
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +388 -403
- package/src/utils/sheetFormulaEvaluator.ts +589 -444
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +140 -18
- package/src/utils/sheetNumberFormat.ts +68 -267
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +13 -43
- package/src/utils/sheetSnapshot.ts +176 -429
- package/src/utils/sheetStructureEdit.ts +537 -907
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/storage.ts +49 -3
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +199 -73
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +37 -192
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +49 -40
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +93 -200
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +4 -23
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +492 -37
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/src/constants/agentDoc.ts +0 -12
- package/src/constants/logActivityGroupColors.generated.ts +0 -19
- package/src/constants/schedule.ts +0 -220
- package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
- package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
- package/src/constants/sheetRegionSources.generated.ts +0 -578
- package/src/schemas/fieldDocsQuery.ts +0 -12
- package/src/schemas/organization.ts +0 -68
- package/src/types/agentDoc.d.ts +0 -64
- package/src/types/noteDoc.d.ts +0 -86
- package/src/types/schedule.d.ts +0 -391
- package/src/utils/agentDoc.ts +0 -109
- package/src/utils/campaignFormEditor.ts +0 -871
- package/src/utils/dataTableRealtimeAuthority.ts +0 -209
- package/src/utils/dataTableRealtimeSelection.ts +0 -134
- package/src/utils/dataTableRowDocument.ts +0 -508
- package/src/utils/dataTableSource.ts +0 -230
- package/src/utils/schedule.ts +0 -799
- package/src/utils/scheduleEvaluation.ts +0 -1994
- package/src/utils/scheduleICS.ts +0 -246
- package/src/utils/scheduleRuleTemplates.ts +0 -188
- package/src/utils/sheetRegion.ts +0 -374
- package/src/utils/sheetRegionFilter.ts +0 -1259
- package/src/utils/sheetRegionSourceRegistry.ts +0 -594
- package/tests/schedule.test.ts +0 -693
- package/tests/scheduleEvaluation.test.ts +0 -1970
- package/tests/scheduleICS.test.ts +0 -208
- package/tests/scheduleRuleTemplates.test.ts +0 -176
- package/tests/scheduleSweepInvariants.test.ts +0 -428
- package/tests/sheetRegionFilter.test.ts +0 -1135
|
@@ -1,34 +1,29 @@
|
|
|
1
|
+
import { chainMaybePromise, isPromiseLike, type MaybePromise } from '@jsb188/app/utils/promise.ts';
|
|
2
|
+
import { DEFAULT_TIMEZONE, isValidTimeZone } from '@jsb188/app/utils/timeZone.ts';
|
|
3
|
+
import type { SheetFormulaErrorObj, SheetFormulaReferenceObj } from '../types/sheet.d.ts';
|
|
4
|
+
import { getSheetFormulaSyntaxErrorMessage, parseSheetFormulaExpression } from './sheetFormulaParser.ts';
|
|
5
|
+
import type { SheetFormulaASTNode } from './sheetFormulaSyntax.ts';
|
|
6
|
+
import { SHEET_CELL_TEXT_MAX_CHARS } from '../constants/sheet.ts';
|
|
1
7
|
import {
|
|
2
|
-
chainMaybePromise,
|
|
3
|
-
isPromiseLike,
|
|
4
|
-
type MaybePromise,
|
|
5
|
-
} from '@jsb188/app/utils/promise.ts';
|
|
6
|
-
import {
|
|
7
|
-
DEFAULT_TIMEZONE,
|
|
8
|
-
isValidTimeZone,
|
|
9
|
-
} from '@jsb188/app/utils/timeZone.ts';
|
|
10
|
-
import type {
|
|
11
|
-
SheetFormulaErrorObj,
|
|
12
|
-
SheetFormulaReferenceObj,
|
|
13
|
-
} from '../types/sheet.d.ts';
|
|
14
|
-
import {
|
|
15
|
-
getSheetFormulaSyntaxErrorMessage,
|
|
16
|
-
parseSheetFormulaExpression,
|
|
17
|
-
type SheetFormulaASTNode,
|
|
18
|
-
} from './sheet.ts';
|
|
19
|
-
import {
|
|
20
|
-
evaluateSheetFormulaEagerBuiltinFunction,
|
|
21
8
|
getSheetFormulaBuiltinFunctionName,
|
|
22
9
|
getSheetFormulaDateTimeShorthandKeyValue,
|
|
23
10
|
getSheetFormulaDateTimeShorthandValue,
|
|
11
|
+
getSheetFormulaErrorCodeDisplayValue,
|
|
24
12
|
getSheetFormulaLogicalBooleanValue,
|
|
25
13
|
getSheetFormulaNumericValue,
|
|
14
|
+
getSheetFormulaPowerResult,
|
|
26
15
|
getSheetFormulaStringValue,
|
|
16
|
+
getSheetFormulaTextValue,
|
|
27
17
|
isSheetFormulaConditionalFunctionName,
|
|
28
18
|
isSheetFormulaNotFoundError,
|
|
29
19
|
isSheetFormulaRangeLikeValue,
|
|
30
20
|
sheetFormulaRangeIncludesValue,
|
|
31
21
|
} from './sheetFormulaEvaluation.ts';
|
|
22
|
+
import { sheetFormulaSwitchValuesEqual } from './sheetFormulaCriteria.ts';
|
|
23
|
+
import { evaluateSheetFormulaEagerBuiltinFunction } from './sheetFormulaFunctionHandlers.ts';
|
|
24
|
+
import { evaluateSheetFormulaInfoFunction } from './sheetFormulaLogical.ts';
|
|
25
|
+
import { searchSheetFormulaXLookup } from './sheetFormulaLookup.ts';
|
|
26
|
+
import { getSheetFormulaRangeBounds, withSheetFormulaRangeShape } from './sheetFormulaRange.ts';
|
|
32
27
|
|
|
33
28
|
/**
|
|
34
29
|
* Types
|
|
@@ -45,18 +40,22 @@ export interface SheetFormulaEvaluatorResult {
|
|
|
45
40
|
value: unknown;
|
|
46
41
|
}
|
|
47
42
|
|
|
48
|
-
/* Range value produced by range resolvers; `reference` is server-only extra.
|
|
43
|
+
/* Range value produced by range resolvers; `reference` is server-only extra.
|
|
44
|
+
* Resolvers return dense row-major values over the clamped rectangle
|
|
45
|
+
* (getSheetFormulaRangeBounds); the core stamps rowCount/columnCount. */
|
|
49
46
|
export interface SheetFormulaEvaluatorRangeValue {
|
|
47
|
+
columnCount?: number;
|
|
50
48
|
kind: 'SHEET_RANGE_VALUE';
|
|
51
49
|
reference?: SheetFormulaReferenceObj;
|
|
50
|
+
rowCount?: number;
|
|
51
|
+
source?: 'ARRAY' | 'RANGE';
|
|
52
52
|
values: unknown[];
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
/* The injected reference resolver. TContext is opaque to the core: the client
|
|
56
56
|
* threads {cell, cellsByCoord, crossTab, referencesById, stack}; the server
|
|
57
57
|
* threads {cache, columnIndex, rowIndex, organizationId, sheetId, sheetTabId,
|
|
58
|
-
* sheetTabByName}.
|
|
59
|
-
* node is not a data-table call (the core then reports UNKNOWN_FUNCTION). */
|
|
58
|
+
* sheetTabByName}. */
|
|
60
59
|
export interface SheetFormulaReferenceResolver<TContext> {
|
|
61
60
|
resolveCellReference(
|
|
62
61
|
context: TContext,
|
|
@@ -68,16 +67,6 @@ export interface SheetFormulaReferenceResolver<TContext> {
|
|
|
68
67
|
node: Extract<SheetFormulaASTNode, { kind: 'RANGE_REFERENCE' }>,
|
|
69
68
|
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
70
69
|
): MaybePromise<SheetFormulaEvaluatorResult>;
|
|
71
|
-
resolveDataTableQuery(
|
|
72
|
-
context: TContext,
|
|
73
|
-
node: Extract<SheetFormulaASTNode, { kind: 'DATA_TABLE_QUERY' }>,
|
|
74
|
-
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
75
|
-
): MaybePromise<SheetFormulaEvaluatorResult>;
|
|
76
|
-
resolveDataTableFunction(
|
|
77
|
-
context: TContext,
|
|
78
|
-
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
79
|
-
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
80
|
-
): MaybePromise<SheetFormulaEvaluatorResult | null>;
|
|
81
70
|
}
|
|
82
71
|
|
|
83
72
|
/* Constant per-evaluation runtime handed to the core and to resolvers (so
|
|
@@ -109,17 +98,16 @@ type SheetFormulaFunctionArgsResult =
|
|
|
109
98
|
};
|
|
110
99
|
|
|
111
100
|
const FILE_CONSTANTS = {
|
|
101
|
+
arrayRootMessage: 'UNIQUE returns several values; wrap it in a function such as COUNTA, INDEX, TEXTJOIN, or SUM.',
|
|
112
102
|
comparisonOperators: ['=', '!=', '<>', '<', '<=', '>', '>='],
|
|
113
103
|
syncResolverViolationCode: 'SHEET_FORMULA_SYNC_RESOLVER_VIOLATION',
|
|
104
|
+
textTooLongMessage: `Formula text result is longer than ${SHEET_CELL_TEXT_MAX_CHARS} characters.`,
|
|
114
105
|
};
|
|
115
106
|
|
|
116
107
|
/*
|
|
117
108
|
* Return a stable formula error object for runtime formula metadata.
|
|
118
109
|
*/
|
|
119
|
-
function getSheetFormulaError(
|
|
120
|
-
code: string,
|
|
121
|
-
message: string,
|
|
122
|
-
): SheetFormulaErrorObj {
|
|
110
|
+
function getSheetFormulaError(code: string, message: string): SheetFormulaErrorObj {
|
|
123
111
|
return {
|
|
124
112
|
code,
|
|
125
113
|
message,
|
|
@@ -127,14 +115,8 @@ function getSheetFormulaError(
|
|
|
127
115
|
}
|
|
128
116
|
|
|
129
117
|
/*
|
|
130
|
-
*
|
|
131
|
-
|
|
132
|
-
function getSheetFormulaErrorDisplayValue(error: SheetFormulaErrorObj) {
|
|
133
|
-
return error.code === 'NOT_FOUND' ? '#N/A' : '#ERROR!';
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/*
|
|
137
|
-
* Build a normalized error result ('#N/A' for NOT_FOUND, '#ERROR!' otherwise).
|
|
118
|
+
* Build a normalized error result whose value is the shared display token for
|
|
119
|
+
* the code (#N/A, #VALUE!, #REF!, #NUM!, #DIV/0!, or #ERROR!).
|
|
138
120
|
*/
|
|
139
121
|
export function buildSheetFormulaErrorResult(
|
|
140
122
|
code: string,
|
|
@@ -146,73 +128,67 @@ export function buildSheetFormulaErrorResult(
|
|
|
146
128
|
return {
|
|
147
129
|
error,
|
|
148
130
|
references,
|
|
149
|
-
value:
|
|
131
|
+
value: getSheetFormulaErrorCodeDisplayValue(error.code),
|
|
150
132
|
};
|
|
151
133
|
}
|
|
152
134
|
|
|
153
135
|
/*
|
|
154
136
|
* Return whether one evaluated value is a range value.
|
|
155
137
|
*/
|
|
156
|
-
export function isSheetFormulaRangeEvalValue(
|
|
157
|
-
value: unknown,
|
|
158
|
-
): value is SheetFormulaEvaluatorRangeValue {
|
|
138
|
+
export function isSheetFormulaRangeEvalValue(value: unknown): value is SheetFormulaEvaluatorRangeValue {
|
|
159
139
|
return isSheetFormulaRangeLikeValue(value);
|
|
160
140
|
}
|
|
161
141
|
|
|
162
142
|
/*
|
|
163
143
|
* Return an invalid-arity result for one formula function.
|
|
164
144
|
*/
|
|
165
|
-
function buildSheetFormulaFunctionArityErrorResult(
|
|
166
|
-
name
|
|
167
|
-
expectedText: string,
|
|
168
|
-
references: SheetFormulaReferenceObj[] = [],
|
|
169
|
-
) {
|
|
170
|
-
return buildSheetFormulaErrorResult(
|
|
171
|
-
'INVALID_FORMULA',
|
|
172
|
-
`${name} requires ${expectedText}.`,
|
|
173
|
-
references,
|
|
174
|
-
);
|
|
145
|
+
function buildSheetFormulaFunctionArityErrorResult(name: string, expectedText: string, references: SheetFormulaReferenceObj[] = []) {
|
|
146
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', `${name} requires ${expectedText}.`, references);
|
|
175
147
|
}
|
|
176
148
|
|
|
177
149
|
/*
|
|
178
|
-
* Evaluate
|
|
179
|
-
*
|
|
150
|
+
* Evaluate the argument nodes at the listed indexes in order, stopping at the
|
|
151
|
+
* first error or loading result, and recurse into the next position so a
|
|
152
|
+
* fully-synchronous resolver never allocates a promise.
|
|
180
153
|
*/
|
|
181
|
-
function
|
|
154
|
+
function evaluateSheetFormulaFunctionArgListAt<TContext>(
|
|
182
155
|
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
183
156
|
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
184
|
-
|
|
157
|
+
argIndexes: number[],
|
|
158
|
+
position: number,
|
|
185
159
|
references: SheetFormulaReferenceObj[],
|
|
186
160
|
values: unknown[],
|
|
187
161
|
): MaybePromise<SheetFormulaFunctionArgsResult> {
|
|
188
|
-
if (
|
|
162
|
+
if (position >= argIndexes.length) {
|
|
189
163
|
return {
|
|
190
164
|
references,
|
|
191
165
|
values,
|
|
192
166
|
};
|
|
193
167
|
}
|
|
194
168
|
|
|
195
|
-
return chainMaybePromise(
|
|
196
|
-
|
|
197
|
-
(result)
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
return {
|
|
201
|
-
...result,
|
|
202
|
-
references,
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
values.push(result.value);
|
|
207
|
-
return evaluateSheetFormulaFunctionArgAt(
|
|
208
|
-
runtime,
|
|
209
|
-
node,
|
|
210
|
-
index + 1,
|
|
169
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[argIndexes[position]]), (result) => {
|
|
170
|
+
references.push(...result.references);
|
|
171
|
+
if (result.loading || result.error) {
|
|
172
|
+
return {
|
|
173
|
+
...result,
|
|
211
174
|
references,
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
values.push(result.value);
|
|
179
|
+
return evaluateSheetFormulaFunctionArgListAt(runtime, node, argIndexes, position + 1, references, values);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
* Evaluate the argument nodes at the listed indexes (eagerly, in order).
|
|
185
|
+
*/
|
|
186
|
+
function evaluateSheetFormulaFunctionArgIndexes<TContext>(
|
|
187
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
188
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
189
|
+
argIndexes: number[],
|
|
190
|
+
): MaybePromise<SheetFormulaFunctionArgsResult> {
|
|
191
|
+
return evaluateSheetFormulaFunctionArgListAt(runtime, node, argIndexes, 0, [], []);
|
|
216
192
|
}
|
|
217
193
|
|
|
218
194
|
/*
|
|
@@ -222,7 +198,24 @@ function evaluateSheetFormulaFunctionArgs<TContext>(
|
|
|
222
198
|
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
223
199
|
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
224
200
|
): MaybePromise<SheetFormulaFunctionArgsResult> {
|
|
225
|
-
return
|
|
201
|
+
return evaluateSheetFormulaFunctionArgIndexes(runtime, node, Array.from(node.args.keys()));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/*
|
|
205
|
+
* Return one lazily evaluated branch result with the references of every
|
|
206
|
+
* argument evaluated before it.
|
|
207
|
+
*/
|
|
208
|
+
function getSheetFormulaBranchResult(
|
|
209
|
+
references: SheetFormulaReferenceObj[],
|
|
210
|
+
branch: SheetFormulaEvaluatorResult,
|
|
211
|
+
): SheetFormulaEvaluatorResult {
|
|
212
|
+
return {
|
|
213
|
+
error: branch.error,
|
|
214
|
+
keyValue: branch.keyValue,
|
|
215
|
+
loading: branch.loading,
|
|
216
|
+
references: [...references, ...branch.references],
|
|
217
|
+
value: branch.value,
|
|
218
|
+
};
|
|
226
219
|
}
|
|
227
220
|
|
|
228
221
|
/*
|
|
@@ -236,49 +229,38 @@ function evaluateSheetFormulaIfFunction<TContext>(
|
|
|
236
229
|
return buildSheetFormulaFunctionArityErrorResult('IF', 'three arguments');
|
|
237
230
|
}
|
|
238
231
|
|
|
239
|
-
return chainMaybePromise(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
return condition;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (isSheetFormulaRangeEvalValue(condition.value)) {
|
|
247
|
-
return buildSheetFormulaErrorResult(
|
|
248
|
-
'INVALID_FORMULA',
|
|
249
|
-
'IF condition requires a scalar boolean-compatible value.',
|
|
250
|
-
condition.references,
|
|
251
|
-
);
|
|
252
|
-
}
|
|
232
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[0]), (condition) => {
|
|
233
|
+
if (condition.loading || condition.error) {
|
|
234
|
+
return condition;
|
|
235
|
+
}
|
|
253
236
|
|
|
254
|
-
|
|
255
|
-
|
|
237
|
+
if (isSheetFormulaRangeEvalValue(condition.value)) {
|
|
238
|
+
return buildSheetFormulaErrorResult(
|
|
239
|
+
'INVALID_FORMULA',
|
|
240
|
+
'IF condition requires a scalar boolean-compatible value.',
|
|
241
|
+
condition.references,
|
|
256
242
|
);
|
|
257
|
-
|
|
258
|
-
return buildSheetFormulaErrorResult(
|
|
259
|
-
'INVALID_FORMULA',
|
|
260
|
-
'IF condition requires a boolean-compatible value.',
|
|
261
|
-
condition.references,
|
|
262
|
-
);
|
|
263
|
-
}
|
|
243
|
+
}
|
|
264
244
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return {
|
|
272
|
-
error: branch.error,
|
|
273
|
-
keyValue: branch.keyValue,
|
|
274
|
-
loading: branch.loading,
|
|
275
|
-
references: [...condition.references, ...branch.references],
|
|
276
|
-
value: branch.value,
|
|
277
|
-
};
|
|
278
|
-
},
|
|
245
|
+
const conditionValue = getSheetFormulaLogicalBooleanValue(condition.value);
|
|
246
|
+
if (conditionValue === null) {
|
|
247
|
+
return buildSheetFormulaErrorResult(
|
|
248
|
+
'INVALID_FORMULA',
|
|
249
|
+
'IF condition requires a boolean-compatible value.',
|
|
250
|
+
condition.references,
|
|
279
251
|
);
|
|
280
|
-
}
|
|
281
|
-
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[conditionValue ? 1 : 2]), (branch) => {
|
|
255
|
+
return {
|
|
256
|
+
error: branch.error,
|
|
257
|
+
keyValue: branch.keyValue,
|
|
258
|
+
loading: branch.loading,
|
|
259
|
+
references: [...condition.references, ...branch.references],
|
|
260
|
+
value: branch.value,
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
});
|
|
282
264
|
}
|
|
283
265
|
|
|
284
266
|
/*
|
|
@@ -293,122 +275,303 @@ function evaluateSheetFormulaErrorFallbackFunction<TContext>(
|
|
|
293
275
|
return buildSheetFormulaFunctionArityErrorResult(name, 'two arguments');
|
|
294
276
|
}
|
|
295
277
|
|
|
296
|
-
return chainMaybePromise(
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
return valueResult;
|
|
301
|
-
}
|
|
278
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[0]), (valueResult) => {
|
|
279
|
+
if (valueResult.loading) {
|
|
280
|
+
return valueResult;
|
|
281
|
+
}
|
|
302
282
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return valueResult;
|
|
308
|
-
}
|
|
283
|
+
const shouldUseFallback = valueResult.error && (name === 'IFERROR' || isSheetFormulaNotFoundError(valueResult.error));
|
|
284
|
+
if (!shouldUseFallback) {
|
|
285
|
+
return valueResult;
|
|
286
|
+
}
|
|
309
287
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
],
|
|
321
|
-
value: fallbackResult.value,
|
|
322
|
-
};
|
|
323
|
-
},
|
|
324
|
-
);
|
|
325
|
-
},
|
|
326
|
-
);
|
|
288
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[1]), (fallbackResult) => {
|
|
289
|
+
return {
|
|
290
|
+
error: fallbackResult.error,
|
|
291
|
+
keyValue: fallbackResult.keyValue,
|
|
292
|
+
loading: fallbackResult.loading,
|
|
293
|
+
references: [...valueResult.references, ...fallbackResult.references],
|
|
294
|
+
value: fallbackResult.value,
|
|
295
|
+
};
|
|
296
|
+
});
|
|
297
|
+
});
|
|
327
298
|
}
|
|
328
299
|
|
|
329
300
|
/*
|
|
330
301
|
* Evaluate an INCLUDES formula function lazily: INCLUDES(needle, range) returns
|
|
331
302
|
* a boolean; INCLUDES(needle, range, if_true, if_false) evaluates only the
|
|
332
303
|
* taken branch. The range's cells are already resolved scalars, so membership
|
|
333
|
-
* works over
|
|
304
|
+
* works over literal values and formula results alike.
|
|
334
305
|
*/
|
|
335
306
|
function evaluateSheetFormulaIncludesFunction<TContext>(
|
|
336
307
|
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
337
308
|
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
338
309
|
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
339
310
|
if (node.args.length !== 2 && node.args.length !== 4) {
|
|
311
|
+
return buildSheetFormulaFunctionArityErrorResult('INCLUDES', 'two or four arguments');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[0]), (needle) => {
|
|
315
|
+
if (needle.loading || needle.error) {
|
|
316
|
+
return needle;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[1]), (range) => {
|
|
320
|
+
const references = [...needle.references, ...range.references];
|
|
321
|
+
if (range.loading) {
|
|
322
|
+
return {
|
|
323
|
+
...range,
|
|
324
|
+
references,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (range.error) {
|
|
329
|
+
return {
|
|
330
|
+
error: range.error,
|
|
331
|
+
references,
|
|
332
|
+
value: range.value,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (!isSheetFormulaRangeEvalValue(range.value)) {
|
|
337
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', 'INCLUDES requires a cell range as its second argument.', references);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const included = sheetFormulaRangeIncludesValue(range.value, needle.value);
|
|
341
|
+
if (node.args.length === 2) {
|
|
342
|
+
return {
|
|
343
|
+
references,
|
|
344
|
+
value: included,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[included ? 2 : 3]), (branch) => {
|
|
349
|
+
return {
|
|
350
|
+
error: branch.error,
|
|
351
|
+
keyValue: branch.keyValue,
|
|
352
|
+
loading: branch.loading,
|
|
353
|
+
references: [...references, ...branch.references],
|
|
354
|
+
value: branch.value,
|
|
355
|
+
};
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/*
|
|
362
|
+
* Evaluate the IFS condition/value pair at one pair index, recursing into the
|
|
363
|
+
* next pair only while conditions are FALSE, so later branches never run.
|
|
364
|
+
*/
|
|
365
|
+
function evaluateSheetFormulaIfsPairAt<TContext>(
|
|
366
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
367
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
368
|
+
pairIndex: number,
|
|
369
|
+
references: SheetFormulaReferenceObj[],
|
|
370
|
+
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
371
|
+
if (pairIndex * 2 >= node.args.length) {
|
|
372
|
+
return buildSheetFormulaErrorResult('NOT_FOUND', 'IFS found no TRUE condition.', references);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[pairIndex * 2]), (condition) => {
|
|
376
|
+
const conditionReferences = [...references, ...condition.references];
|
|
377
|
+
if (condition.loading || condition.error) {
|
|
378
|
+
return { ...condition, references: conditionReferences };
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
const conditionValue = isSheetFormulaRangeEvalValue(condition.value) ? null : getSheetFormulaLogicalBooleanValue(condition.value);
|
|
382
|
+
if (conditionValue === null) {
|
|
383
|
+
return buildSheetFormulaErrorResult(
|
|
384
|
+
'INVALID_VALUE',
|
|
385
|
+
'IFS conditions must each be a single TRUE/FALSE value.',
|
|
386
|
+
conditionReferences,
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (!conditionValue) {
|
|
391
|
+
return evaluateSheetFormulaIfsPairAt(runtime, node, pairIndex + 1, conditionReferences);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[pairIndex * 2 + 1]), (branch) =>
|
|
395
|
+
getSheetFormulaBranchResult(conditionReferences, branch),
|
|
396
|
+
);
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/*
|
|
401
|
+
* Evaluate IFS(condition1, value1, ...) lazily: the value of the first TRUE
|
|
402
|
+
* condition, or NOT_FOUND (#N/A) when none is TRUE.
|
|
403
|
+
*/
|
|
404
|
+
function evaluateSheetFormulaIfsFunction<TContext>(
|
|
405
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
406
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
407
|
+
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
408
|
+
if (node.args.length < 2 || node.args.length % 2 !== 0) {
|
|
409
|
+
return buildSheetFormulaFunctionArityErrorResult('IFS', 'condition and value pairs');
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return evaluateSheetFormulaIfsPairAt(runtime, node, 0, []);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/*
|
|
416
|
+
* Evaluate the SWITCH case at one argument index against the already
|
|
417
|
+
* evaluated expression: a match evaluates its value, a lone trailing
|
|
418
|
+
* argument is the default, and no match without a default is NOT_FOUND.
|
|
419
|
+
*/
|
|
420
|
+
function evaluateSheetFormulaSwitchCaseAt<TContext>(
|
|
421
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
422
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
423
|
+
expressionValue: unknown,
|
|
424
|
+
argIndex: number,
|
|
425
|
+
references: SheetFormulaReferenceObj[],
|
|
426
|
+
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
427
|
+
const remaining = node.args.length - argIndex;
|
|
428
|
+
if (remaining === 0) {
|
|
429
|
+
return buildSheetFormulaErrorResult('NOT_FOUND', 'SWITCH found no matching case.', references);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (remaining === 1) {
|
|
433
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[argIndex]), (fallback) =>
|
|
434
|
+
getSheetFormulaBranchResult(references, fallback),
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[argIndex]), (caseResult) => {
|
|
439
|
+
const caseReferences = [...references, ...caseResult.references];
|
|
440
|
+
if (caseResult.loading || caseResult.error) {
|
|
441
|
+
return { ...caseResult, references: caseReferences };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
if (isSheetFormulaRangeEvalValue(caseResult.value)) {
|
|
445
|
+
return buildSheetFormulaErrorResult('INVALID_VALUE', 'SWITCH cases must be single values, not ranges.', caseReferences);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
if (!sheetFormulaSwitchValuesEqual(expressionValue, caseResult.value)) {
|
|
449
|
+
return evaluateSheetFormulaSwitchCaseAt(runtime, node, expressionValue, argIndex + 2, caseReferences);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[argIndex + 1]), (branch) =>
|
|
453
|
+
getSheetFormulaBranchResult(caseReferences, branch),
|
|
454
|
+
);
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/*
|
|
459
|
+
* Evaluate SWITCH(expression, case1, value1, ..., [default]) lazily: the
|
|
460
|
+
* expression once, then cases in order until one equals it (lookup equality:
|
|
461
|
+
* case-insensitive text, 1 equals "1"), evaluating only the chosen value.
|
|
462
|
+
*/
|
|
463
|
+
function evaluateSheetFormulaSwitchFunction<TContext>(
|
|
464
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
465
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
466
|
+
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
467
|
+
if (node.args.length < 3) {
|
|
468
|
+
return buildSheetFormulaFunctionArityErrorResult('SWITCH', 'an expression and at least one case and value pair');
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[0]), (expression) => {
|
|
472
|
+
if (expression.loading || expression.error) {
|
|
473
|
+
return expression;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (isSheetFormulaRangeEvalValue(expression.value)) {
|
|
477
|
+
return buildSheetFormulaErrorResult(
|
|
478
|
+
'INVALID_VALUE',
|
|
479
|
+
'SWITCH expression must be a single value, not a range.',
|
|
480
|
+
expression.references,
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
return evaluateSheetFormulaSwitchCaseAt(runtime, node, expression.value, 1, expression.references);
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/*
|
|
489
|
+
* Evaluate XLOOKUP(key, lookup_range, result_range, [if_not_found],
|
|
490
|
+
* [match_mode], [search_mode]): every argument except if_not_found is
|
|
491
|
+
* evaluated first, and if_not_found only on a miss.
|
|
492
|
+
*/
|
|
493
|
+
function evaluateSheetFormulaXLookupFunction<TContext>(
|
|
494
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
495
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
496
|
+
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
497
|
+
const argCount = node.args.length;
|
|
498
|
+
if (argCount < 3 || argCount > 6) {
|
|
340
499
|
return buildSheetFormulaFunctionArityErrorResult(
|
|
341
|
-
'
|
|
342
|
-
'
|
|
500
|
+
'XLOOKUP',
|
|
501
|
+
'a search key, a lookup range, a result range, and optional if_not_found, match_mode and search_mode arguments',
|
|
343
502
|
);
|
|
344
503
|
}
|
|
345
504
|
|
|
346
505
|
return chainMaybePromise(
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
506
|
+
evaluateSheetFormulaFunctionArgIndexes(
|
|
507
|
+
runtime,
|
|
508
|
+
node,
|
|
509
|
+
[0, 1, 2, 4, 5].filter((index) => index < argCount),
|
|
510
|
+
),
|
|
511
|
+
(argsResult) => {
|
|
512
|
+
if (!('values' in argsResult)) {
|
|
513
|
+
return argsResult;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
const { references, values } = argsResult;
|
|
517
|
+
const search = searchSheetFormulaXLookup({
|
|
518
|
+
key: values[0],
|
|
519
|
+
lookupRange: values[1],
|
|
520
|
+
matchMode: argCount > 4 ? values[3] : undefined,
|
|
521
|
+
resultRange: values[2],
|
|
522
|
+
searchMode: argCount > 5 ? values[4] : undefined,
|
|
523
|
+
});
|
|
524
|
+
if (!search.ok) {
|
|
525
|
+
return buildSheetFormulaErrorResult(
|
|
526
|
+
search.result.error?.code || 'INVALID_VALUE',
|
|
527
|
+
search.result.error?.message || 'XLOOKUP arguments are invalid.',
|
|
528
|
+
references,
|
|
529
|
+
);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
if (search.found) {
|
|
533
|
+
return { references, value: search.value };
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (argCount < 4) {
|
|
537
|
+
return buildSheetFormulaErrorResult('NOT_FOUND', 'XLOOKUP did not find the search key.', references);
|
|
351
538
|
}
|
|
352
539
|
|
|
353
|
-
return chainMaybePromise(
|
|
354
|
-
|
|
355
|
-
(range) => {
|
|
356
|
-
const references = [...needle.references, ...range.references];
|
|
357
|
-
if (range.loading) {
|
|
358
|
-
return {
|
|
359
|
-
...range,
|
|
360
|
-
references,
|
|
361
|
-
};
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if (range.error) {
|
|
365
|
-
return {
|
|
366
|
-
error: range.error,
|
|
367
|
-
references,
|
|
368
|
-
value: range.value,
|
|
369
|
-
};
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
if (!isSheetFormulaRangeEvalValue(range.value)) {
|
|
373
|
-
return buildSheetFormulaErrorResult(
|
|
374
|
-
'INVALID_FORMULA',
|
|
375
|
-
'INCLUDES requires a cell range as its second argument.',
|
|
376
|
-
references,
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
const included = sheetFormulaRangeIncludesValue(
|
|
381
|
-
range.value,
|
|
382
|
-
needle.value,
|
|
383
|
-
);
|
|
384
|
-
if (node.args.length === 2) {
|
|
385
|
-
return {
|
|
386
|
-
references,
|
|
387
|
-
value: included,
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
return chainMaybePromise(
|
|
392
|
-
evaluateSheetFormulaNodeGeneric(
|
|
393
|
-
runtime,
|
|
394
|
-
node.args[included ? 2 : 3],
|
|
395
|
-
),
|
|
396
|
-
(branch) => {
|
|
397
|
-
return {
|
|
398
|
-
error: branch.error,
|
|
399
|
-
keyValue: branch.keyValue,
|
|
400
|
-
loading: branch.loading,
|
|
401
|
-
references: [...references, ...branch.references],
|
|
402
|
-
value: branch.value,
|
|
403
|
-
};
|
|
404
|
-
},
|
|
405
|
-
);
|
|
406
|
-
},
|
|
540
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[3]), (fallback) =>
|
|
541
|
+
getSheetFormulaBranchResult(references, fallback),
|
|
407
542
|
);
|
|
408
543
|
},
|
|
409
544
|
);
|
|
410
545
|
}
|
|
411
546
|
|
|
547
|
+
/*
|
|
548
|
+
* Evaluate ISBLANK, ISERROR, ISNA, ISNUMBER or ISTEXT: the single argument is
|
|
549
|
+
* evaluated and its error, if any, becomes the TRUE/FALSE answer instead of
|
|
550
|
+
* propagating (so ISNUMBER(SEARCH("x", A1)) is FALSE when nothing is found).
|
|
551
|
+
*/
|
|
552
|
+
function evaluateSheetFormulaInfoFunctionNode<TContext>(
|
|
553
|
+
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
554
|
+
node: Extract<SheetFormulaASTNode, { kind: 'FUNCTION_CALL' }>,
|
|
555
|
+
name: 'ISBLANK' | 'ISERROR' | 'ISNA' | 'ISNUMBER' | 'ISTEXT',
|
|
556
|
+
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
557
|
+
if (node.args.length !== 1) {
|
|
558
|
+
return buildSheetFormulaFunctionArityErrorResult(name, 'one value');
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.args[0]), (argument) => {
|
|
562
|
+
if (argument.loading) {
|
|
563
|
+
return argument;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const result = evaluateSheetFormulaInfoFunction(name, argument);
|
|
567
|
+
return {
|
|
568
|
+
error: result.error,
|
|
569
|
+
references: argument.references,
|
|
570
|
+
value: result.value,
|
|
571
|
+
};
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
|
|
412
575
|
/*
|
|
413
576
|
* Evaluate a lazy builtin formula function that controls argument evaluation.
|
|
414
577
|
*/
|
|
@@ -429,10 +592,23 @@ function evaluateSheetFormulaConditionalFunction<TContext>(
|
|
|
429
592
|
return evaluateSheetFormulaIncludesFunction(runtime, node);
|
|
430
593
|
}
|
|
431
594
|
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
595
|
+
if (name === 'IFS') {
|
|
596
|
+
return evaluateSheetFormulaIfsFunction(runtime, node);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (name === 'SWITCH') {
|
|
600
|
+
return evaluateSheetFormulaSwitchFunction(runtime, node);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if (name === 'XLOOKUP') {
|
|
604
|
+
return evaluateSheetFormulaXLookupFunction(runtime, node);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
if (name === 'ISBLANK' || name === 'ISERROR' || name === 'ISNA' || name === 'ISNUMBER' || name === 'ISTEXT') {
|
|
608
|
+
return evaluateSheetFormulaInfoFunctionNode(runtime, node, name);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
return buildSheetFormulaErrorResult('UNKNOWN_FUNCTION', 'Formula function is not supported.');
|
|
436
612
|
}
|
|
437
613
|
|
|
438
614
|
/*
|
|
@@ -447,47 +623,24 @@ function evaluateSheetFormulaFunctionCallNode<TContext>(
|
|
|
447
623
|
const builtinName = getSheetFormulaBuiltinFunctionName(node.name);
|
|
448
624
|
if (builtinName) {
|
|
449
625
|
if (isSheetFormulaConditionalFunctionName(builtinName)) {
|
|
450
|
-
return evaluateSheetFormulaConditionalFunction(
|
|
451
|
-
runtime,
|
|
452
|
-
node,
|
|
453
|
-
builtinName,
|
|
454
|
-
);
|
|
626
|
+
return evaluateSheetFormulaConditionalFunction(runtime, node, builtinName);
|
|
455
627
|
}
|
|
456
628
|
|
|
457
|
-
return chainMaybePromise(
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
);
|
|
470
|
-
return {
|
|
471
|
-
error: result.error,
|
|
472
|
-
references: argsResult.references,
|
|
473
|
-
value: result.value,
|
|
474
|
-
};
|
|
475
|
-
},
|
|
476
|
-
);
|
|
629
|
+
return chainMaybePromise(evaluateSheetFormulaFunctionArgs(runtime, node), (argsResult) => {
|
|
630
|
+
if (!('values' in argsResult)) {
|
|
631
|
+
return argsResult;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
const result = evaluateSheetFormulaEagerBuiltinFunction(builtinName, argsResult.values, runtime.timeZone, runtime.now);
|
|
635
|
+
return {
|
|
636
|
+
error: result.error,
|
|
637
|
+
references: argsResult.references,
|
|
638
|
+
value: result.value,
|
|
639
|
+
};
|
|
640
|
+
});
|
|
477
641
|
}
|
|
478
642
|
|
|
479
|
-
return
|
|
480
|
-
runtime.resolver.resolveDataTableFunction(runtime.context, node, runtime),
|
|
481
|
-
(dataTableResult) => {
|
|
482
|
-
return (
|
|
483
|
-
dataTableResult ||
|
|
484
|
-
buildSheetFormulaErrorResult(
|
|
485
|
-
'UNKNOWN_FUNCTION',
|
|
486
|
-
'Formula function is not supported.',
|
|
487
|
-
)
|
|
488
|
-
);
|
|
489
|
-
},
|
|
490
|
-
);
|
|
643
|
+
return buildSheetFormulaErrorResult('UNKNOWN_FUNCTION', 'Formula function is not supported.');
|
|
491
644
|
}
|
|
492
645
|
|
|
493
646
|
/*
|
|
@@ -497,53 +650,36 @@ function evaluateSheetFormulaUnaryNode<TContext>(
|
|
|
497
650
|
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
498
651
|
node: Extract<SheetFormulaASTNode, { kind: 'UNARY_EXPRESSION' }>,
|
|
499
652
|
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
500
|
-
return chainMaybePromise(
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
return result;
|
|
505
|
-
}
|
|
653
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.value), (result) => {
|
|
654
|
+
if (result.loading || result.error) {
|
|
655
|
+
return result;
|
|
656
|
+
}
|
|
506
657
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
'Formula arithmetic cannot use a range directly.',
|
|
511
|
-
result.references,
|
|
512
|
-
);
|
|
513
|
-
}
|
|
658
|
+
if (isSheetFormulaRangeEvalValue(result.value)) {
|
|
659
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', 'Formula arithmetic cannot use a range directly.', result.references);
|
|
660
|
+
}
|
|
514
661
|
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
'Formula arithmetic requires numeric values.',
|
|
520
|
-
result.references,
|
|
521
|
-
);
|
|
522
|
-
}
|
|
662
|
+
const numberValue = getSheetFormulaNumericValue(result.value);
|
|
663
|
+
if (numberValue === null) {
|
|
664
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', 'Formula arithmetic requires numeric values.', result.references);
|
|
665
|
+
}
|
|
523
666
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
);
|
|
667
|
+
return {
|
|
668
|
+
references: result.references,
|
|
669
|
+
value: node.operator === '-' ? -numberValue : numberValue,
|
|
670
|
+
};
|
|
671
|
+
});
|
|
530
672
|
}
|
|
531
673
|
|
|
532
674
|
/*
|
|
533
675
|
* Compare two scalar formula values.
|
|
534
676
|
*/
|
|
535
|
-
function compareSheetFormulaScalarValues(
|
|
536
|
-
leftValue: unknown,
|
|
537
|
-
rightValue: unknown,
|
|
538
|
-
operator: string,
|
|
539
|
-
) {
|
|
677
|
+
function compareSheetFormulaScalarValues(leftValue: unknown, rightValue: unknown, operator: string) {
|
|
540
678
|
const leftNumber = getSheetFormulaNumericValue(leftValue);
|
|
541
679
|
const rightNumber = getSheetFormulaNumericValue(rightValue);
|
|
542
680
|
const useNumbers = leftNumber !== null && rightNumber !== null;
|
|
543
681
|
const left = useNumbers ? leftNumber : getSheetFormulaStringValue(leftValue);
|
|
544
|
-
const right = useNumbers
|
|
545
|
-
? rightNumber
|
|
546
|
-
: getSheetFormulaStringValue(rightValue);
|
|
682
|
+
const right = useNumbers ? rightNumber : getSheetFormulaStringValue(rightValue);
|
|
547
683
|
|
|
548
684
|
switch (operator) {
|
|
549
685
|
case '=':
|
|
@@ -565,65 +701,95 @@ function compareSheetFormulaScalarValues(
|
|
|
565
701
|
}
|
|
566
702
|
|
|
567
703
|
/*
|
|
568
|
-
*
|
|
704
|
+
* Return a text operator result, or INVALID_VALUE when the joined text would
|
|
705
|
+
* exceed the per-cell text cap (keeps chains such as A2=A1&A1, A3=A2&A2, ...
|
|
706
|
+
* from growing without bound).
|
|
569
707
|
*/
|
|
570
|
-
function
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
): SheetFormulaEvaluatorResult {
|
|
575
|
-
if (operator === '+') {
|
|
576
|
-
const leftNumber = getSheetFormulaNumericValue(leftValue);
|
|
577
|
-
const rightNumber = getSheetFormulaNumericValue(rightValue);
|
|
578
|
-
if (leftNumber !== null && rightNumber !== null) {
|
|
579
|
-
return {
|
|
708
|
+
function getSheetFormulaJoinedTextResult(text: string): SheetFormulaEvaluatorResult {
|
|
709
|
+
return text.length > SHEET_CELL_TEXT_MAX_CHARS
|
|
710
|
+
? buildSheetFormulaErrorResult('INVALID_VALUE', FILE_CONSTANTS.textTooLongMessage)
|
|
711
|
+
: {
|
|
580
712
|
references: [],
|
|
581
|
-
value:
|
|
713
|
+
value: text,
|
|
582
714
|
};
|
|
583
|
-
|
|
715
|
+
}
|
|
584
716
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
717
|
+
/*
|
|
718
|
+
* Return one numeric operator result, or INVALID_NUMBER when it overflowed to
|
|
719
|
+
* a non-finite value.
|
|
720
|
+
*/
|
|
721
|
+
function getSheetFormulaFiniteNumberResult(value: number): SheetFormulaEvaluatorResult {
|
|
722
|
+
return Number.isFinite(value)
|
|
723
|
+
? {
|
|
724
|
+
references: [],
|
|
725
|
+
value,
|
|
726
|
+
}
|
|
727
|
+
: buildSheetFormulaErrorResult('INVALID_NUMBER', 'Formula arithmetic result is not a finite number.');
|
|
728
|
+
}
|
|
592
729
|
|
|
730
|
+
/*
|
|
731
|
+
* Evaluate the + operator: numeric addition when both sides coerce to
|
|
732
|
+
* numbers, otherwise the dialect's legacy string join (lowercase true/false).
|
|
733
|
+
*/
|
|
734
|
+
function evaluateSheetFormulaAdditionValues(leftValue: unknown, rightValue: unknown): SheetFormulaEvaluatorResult {
|
|
593
735
|
const leftNumber = getSheetFormulaNumericValue(leftValue);
|
|
594
736
|
const rightNumber = getSheetFormulaNumericValue(rightValue);
|
|
595
|
-
if (leftNumber
|
|
596
|
-
return
|
|
597
|
-
'INVALID_FORMULA',
|
|
598
|
-
'Formula arithmetic requires numeric values.',
|
|
599
|
-
);
|
|
737
|
+
if (leftNumber !== null && rightNumber !== null) {
|
|
738
|
+
return getSheetFormulaFiniteNumberResult(leftNumber + rightNumber);
|
|
600
739
|
}
|
|
601
740
|
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
741
|
+
return getSheetFormulaJoinedTextResult(getSheetFormulaStringValue(leftValue) + getSheetFormulaStringValue(rightValue));
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/*
|
|
745
|
+
* Evaluate one numeric binary operator (-, *, /, ^) over coerced operands.
|
|
746
|
+
* Every operator is dispatched explicitly; an operator this build does not
|
|
747
|
+
* know is an INVALID_FORMULA error, never a silent division.
|
|
748
|
+
*/
|
|
749
|
+
function evaluateSheetFormulaNumericOperatorValues(
|
|
750
|
+
leftNumber: number,
|
|
751
|
+
rightNumber: number,
|
|
752
|
+
operator: string,
|
|
753
|
+
): SheetFormulaEvaluatorResult {
|
|
754
|
+
switch (operator) {
|
|
755
|
+
case '-':
|
|
756
|
+
return getSheetFormulaFiniteNumberResult(leftNumber - rightNumber);
|
|
757
|
+
case '*':
|
|
758
|
+
return getSheetFormulaFiniteNumberResult(leftNumber * rightNumber);
|
|
759
|
+
case '/':
|
|
760
|
+
return rightNumber === 0
|
|
761
|
+
? buildSheetFormulaErrorResult('DIVIDE_BY_ZERO', 'Formula cannot divide by zero.')
|
|
762
|
+
: getSheetFormulaFiniteNumberResult(leftNumber / rightNumber);
|
|
763
|
+
case '^': {
|
|
764
|
+
const result = getSheetFormulaPowerResult(leftNumber, rightNumber);
|
|
765
|
+
return result.error
|
|
766
|
+
? buildSheetFormulaErrorResult(result.error.code, result.error.message)
|
|
767
|
+
: { references: [], value: result.value };
|
|
768
|
+
}
|
|
769
|
+
default:
|
|
770
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', `Formula operator "${operator}" is not supported.`);
|
|
607
771
|
}
|
|
772
|
+
}
|
|
608
773
|
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
774
|
+
/*
|
|
775
|
+
* Evaluate an arithmetic or text binary formula expression over two scalars.
|
|
776
|
+
*/
|
|
777
|
+
function evaluateSheetFormulaArithmeticValues(leftValue: unknown, rightValue: unknown, operator: string): SheetFormulaEvaluatorResult {
|
|
778
|
+
if (operator === '&') {
|
|
779
|
+
return getSheetFormulaJoinedTextResult(getSheetFormulaTextValue(leftValue) + getSheetFormulaTextValue(rightValue));
|
|
614
780
|
}
|
|
615
781
|
|
|
616
|
-
if (
|
|
617
|
-
return
|
|
618
|
-
'DIVIDE_BY_ZERO',
|
|
619
|
-
'Formula cannot divide by zero.',
|
|
620
|
-
);
|
|
782
|
+
if (operator === '+') {
|
|
783
|
+
return evaluateSheetFormulaAdditionValues(leftValue, rightValue);
|
|
621
784
|
}
|
|
622
785
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
786
|
+
const leftNumber = getSheetFormulaNumericValue(leftValue);
|
|
787
|
+
const rightNumber = getSheetFormulaNumericValue(rightValue);
|
|
788
|
+
if (leftNumber === null || rightNumber === null) {
|
|
789
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', 'Formula arithmetic requires numeric values.');
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
return evaluateSheetFormulaNumericOperatorValues(leftNumber, rightNumber, operator);
|
|
627
793
|
}
|
|
628
794
|
|
|
629
795
|
/*
|
|
@@ -633,68 +799,49 @@ function evaluateSheetFormulaBinaryNode<TContext>(
|
|
|
633
799
|
runtime: SheetFormulaEvaluatorRuntime<TContext>,
|
|
634
800
|
node: Extract<SheetFormulaASTNode, { kind: 'BINARY_EXPRESSION' }>,
|
|
635
801
|
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
636
|
-
return chainMaybePromise(
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
802
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.left), (left) => {
|
|
803
|
+
if (left.loading || left.error) {
|
|
804
|
+
return left;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, node.right), (right) => {
|
|
808
|
+
const references = [...left.references, ...right.references];
|
|
809
|
+
if (right.loading) {
|
|
810
|
+
return {
|
|
811
|
+
...right,
|
|
812
|
+
references,
|
|
813
|
+
};
|
|
641
814
|
}
|
|
642
815
|
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
if (FILE_CONSTANTS.comparisonOperators.includes(node.operator)) {
|
|
674
|
-
return {
|
|
675
|
-
references,
|
|
676
|
-
value: compareSheetFormulaScalarValues(
|
|
677
|
-
left.value,
|
|
678
|
-
right.value,
|
|
679
|
-
node.operator,
|
|
680
|
-
),
|
|
681
|
-
};
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
const result = evaluateSheetFormulaArithmeticValues(
|
|
685
|
-
left.value,
|
|
686
|
-
right.value,
|
|
687
|
-
node.operator,
|
|
688
|
-
);
|
|
689
|
-
return {
|
|
690
|
-
error: result.error,
|
|
691
|
-
references,
|
|
692
|
-
value: result.value,
|
|
693
|
-
};
|
|
694
|
-
},
|
|
695
|
-
);
|
|
696
|
-
},
|
|
697
|
-
);
|
|
816
|
+
if (right.error) {
|
|
817
|
+
return {
|
|
818
|
+
error: right.error,
|
|
819
|
+
references,
|
|
820
|
+
value: right.value,
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
if (isSheetFormulaRangeEvalValue(left.value) || isSheetFormulaRangeEvalValue(right.value)) {
|
|
825
|
+
return node.operator === '&'
|
|
826
|
+
? buildSheetFormulaErrorResult('INVALID_VALUE', 'Concatenation (&) needs single values, not a range.', references)
|
|
827
|
+
: buildSheetFormulaErrorResult('INVALID_FORMULA', 'Formula arithmetic cannot use a range directly.', references);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
if (FILE_CONSTANTS.comparisonOperators.includes(node.operator)) {
|
|
831
|
+
return {
|
|
832
|
+
references,
|
|
833
|
+
value: compareSheetFormulaScalarValues(left.value, right.value, node.operator),
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
const result = evaluateSheetFormulaArithmeticValues(left.value, right.value, node.operator);
|
|
838
|
+
return {
|
|
839
|
+
error: result.error,
|
|
840
|
+
references,
|
|
841
|
+
value: result.value,
|
|
842
|
+
};
|
|
843
|
+
});
|
|
844
|
+
});
|
|
698
845
|
}
|
|
699
846
|
|
|
700
847
|
/*
|
|
@@ -705,20 +852,40 @@ function evaluateSheetFormulaDateTimeShorthandNode<TContext>(
|
|
|
705
852
|
node: Extract<SheetFormulaASTNode, { kind: 'DATE_TIME_SHORTHAND' }>,
|
|
706
853
|
): SheetFormulaEvaluatorResult {
|
|
707
854
|
return {
|
|
708
|
-
keyValue: getSheetFormulaDateTimeShorthandKeyValue(
|
|
709
|
-
node.name,
|
|
710
|
-
runtime.now,
|
|
711
|
-
runtime.timeZone,
|
|
712
|
-
),
|
|
855
|
+
keyValue: getSheetFormulaDateTimeShorthandKeyValue(node.name, runtime.now, runtime.timeZone),
|
|
713
856
|
references: [],
|
|
714
|
-
value: getSheetFormulaDateTimeShorthandValue(
|
|
715
|
-
node.name,
|
|
716
|
-
runtime.now,
|
|
717
|
-
runtime.timeZone,
|
|
718
|
-
),
|
|
857
|
+
value: getSheetFormulaDateTimeShorthandValue(node.name, runtime.now, runtime.timeZone),
|
|
719
858
|
};
|
|
720
859
|
}
|
|
721
860
|
|
|
861
|
+
/*
|
|
862
|
+
* Return one resolved range result with its shape stamped from the reference
|
|
863
|
+
* node (clamped to the sheet caps), so positional functions can rely on
|
|
864
|
+
* rowCount/columnCount whatever resolver produced the values.
|
|
865
|
+
*/
|
|
866
|
+
function withSheetFormulaRangeResultShape(
|
|
867
|
+
result: SheetFormulaEvaluatorResult,
|
|
868
|
+
node: Extract<SheetFormulaASTNode, { kind: 'RANGE_REFERENCE' }>,
|
|
869
|
+
): SheetFormulaEvaluatorResult {
|
|
870
|
+
if (result.error || result.loading || !isSheetFormulaRangeEvalValue(result.value)) {
|
|
871
|
+
return result;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return {
|
|
875
|
+
...result,
|
|
876
|
+
value: withSheetFormulaRangeShape(result.value, getSheetFormulaRangeBounds(node.reference)),
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/*
|
|
881
|
+
* Evaluate one error literal node such as #REF! into its formula error.
|
|
882
|
+
*/
|
|
883
|
+
function evaluateSheetFormulaErrorLiteralNode(
|
|
884
|
+
node: Extract<SheetFormulaASTNode, { kind: 'ERROR_LITERAL' }>,
|
|
885
|
+
): SheetFormulaEvaluatorResult {
|
|
886
|
+
return buildSheetFormulaErrorResult(node.code, `Formula contains ${node.text}: it refers to a cell or range that no longer exists.`);
|
|
887
|
+
}
|
|
888
|
+
|
|
722
889
|
/*
|
|
723
890
|
* Evaluate one AST node (maybe-promise).
|
|
724
891
|
*/
|
|
@@ -737,23 +904,13 @@ export function evaluateSheetFormulaNodeGeneric<TContext>(
|
|
|
737
904
|
case 'DATE_TIME_SHORTHAND':
|
|
738
905
|
return evaluateSheetFormulaDateTimeShorthandNode(runtime, node);
|
|
739
906
|
case 'CELL_REFERENCE':
|
|
740
|
-
return runtime.resolver.resolveCellReference(
|
|
741
|
-
runtime.context,
|
|
742
|
-
node,
|
|
743
|
-
runtime,
|
|
744
|
-
);
|
|
907
|
+
return runtime.resolver.resolveCellReference(runtime.context, node, runtime);
|
|
745
908
|
case 'RANGE_REFERENCE':
|
|
746
|
-
return runtime.resolver.resolveRangeReference(
|
|
747
|
-
|
|
748
|
-
node,
|
|
749
|
-
runtime,
|
|
750
|
-
);
|
|
751
|
-
case 'DATA_TABLE_QUERY':
|
|
752
|
-
return runtime.resolver.resolveDataTableQuery(
|
|
753
|
-
runtime.context,
|
|
754
|
-
node,
|
|
755
|
-
runtime,
|
|
909
|
+
return chainMaybePromise(runtime.resolver.resolveRangeReference(runtime.context, node, runtime), (result) =>
|
|
910
|
+
withSheetFormulaRangeResultShape(result, node),
|
|
756
911
|
);
|
|
912
|
+
case 'ERROR_LITERAL':
|
|
913
|
+
return evaluateSheetFormulaErrorLiteralNode(node);
|
|
757
914
|
case 'FUNCTION_CALL':
|
|
758
915
|
return evaluateSheetFormulaFunctionCallNode(runtime, node);
|
|
759
916
|
case 'UNARY_EXPRESSION':
|
|
@@ -761,10 +918,7 @@ export function evaluateSheetFormulaNodeGeneric<TContext>(
|
|
|
761
918
|
case 'BINARY_EXPRESSION':
|
|
762
919
|
return evaluateSheetFormulaBinaryNode(runtime, node);
|
|
763
920
|
default:
|
|
764
|
-
return buildSheetFormulaErrorResult(
|
|
765
|
-
'INVALID_FORMULA',
|
|
766
|
-
'Formula syntax is not supported.',
|
|
767
|
-
);
|
|
921
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', 'Formula syntax is not supported.');
|
|
768
922
|
}
|
|
769
923
|
}
|
|
770
924
|
|
|
@@ -777,38 +931,29 @@ export function evaluateSheetFormulaExpressionGeneric<TContext>(
|
|
|
777
931
|
): MaybePromise<SheetFormulaEvaluatorResult> {
|
|
778
932
|
// Stored ASTs are a cache only. Parse canonical formula text whenever the
|
|
779
933
|
// caller cannot supply a trustworthy current tree.
|
|
780
|
-
const rootNode =
|
|
781
|
-
params.node || parseSheetFormulaExpression(params.expression);
|
|
934
|
+
const rootNode = params.node || parseSheetFormulaExpression(params.expression);
|
|
782
935
|
if (!rootNode) {
|
|
783
|
-
return buildSheetFormulaErrorResult(
|
|
784
|
-
'INVALID_FORMULA',
|
|
785
|
-
getSheetFormulaSyntaxErrorMessage(params.expression),
|
|
786
|
-
);
|
|
936
|
+
return buildSheetFormulaErrorResult('INVALID_FORMULA', getSheetFormulaSyntaxErrorMessage(params.expression));
|
|
787
937
|
}
|
|
788
938
|
|
|
789
939
|
const runtime: SheetFormulaEvaluatorRuntime<TContext> = {
|
|
790
940
|
context: params.resolverContext,
|
|
791
941
|
now: params.now || new Date(),
|
|
792
942
|
resolver: params.resolver,
|
|
793
|
-
timeZone: isValidTimeZone(params.timeZone)
|
|
794
|
-
? params.timeZone || DEFAULT_TIMEZONE
|
|
795
|
-
: DEFAULT_TIMEZONE,
|
|
943
|
+
timeZone: isValidTimeZone(params.timeZone) ? params.timeZone || DEFAULT_TIMEZONE : DEFAULT_TIMEZONE,
|
|
796
944
|
};
|
|
797
945
|
|
|
798
|
-
return chainMaybePromise(
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
return
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
);
|
|
807
|
-
}
|
|
946
|
+
return chainMaybePromise(evaluateSheetFormulaNodeGeneric(runtime, rootNode), (result) => {
|
|
947
|
+
if (!result.error && isSheetFormulaRangeEvalValue(result.value)) {
|
|
948
|
+
return buildSheetFormulaErrorResult(
|
|
949
|
+
'INVALID_FORMULA',
|
|
950
|
+
result.value.source === 'ARRAY' ? FILE_CONSTANTS.arrayRootMessage : 'Formula cannot return a range directly.',
|
|
951
|
+
result.references,
|
|
952
|
+
);
|
|
953
|
+
}
|
|
808
954
|
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
);
|
|
955
|
+
return result;
|
|
956
|
+
});
|
|
812
957
|
}
|
|
813
958
|
|
|
814
959
|
/*
|