@jsb188/mday 1.1.10 → 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 +140 -54
- package/src/constants/agentMemory.ts +123 -0
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +10 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +236 -0
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +21 -21
- package/src/constants/dataTableFieldTypes.generated.ts +59 -0
- package/src/constants/docAccess.ts +33 -0
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +125 -7
- package/src/constants/fieldDoc.ts +104 -0
- package/src/constants/graphqlReactivity.ts +196 -0
- package/src/constants/integration.ts +8 -9
- package/src/constants/log.ts +14 -408
- 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 +2207 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +18 -21
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +126 -14
- package/src/constants/sms.ts +18 -0
- package/src/constants/square.ts +6 -6
- package/src/constants/storage.ts +80 -7
- package/src/constants/usage.ts +82 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +836 -780
- package/src/schemas/inboundContact.ts +60 -55
- package/src/schemas/logsQuery.ts +55 -49
- package/src/types/account.d.ts +4 -1
- package/src/types/agentMemory.d.ts +98 -0
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1533 -0
- 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 +115 -403
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +184 -24
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +702 -0
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +10 -1
- package/src/types/log.d.ts +254 -295
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +91 -43
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +678 -129
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +449 -129
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +315 -54
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +43 -0
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +1174 -0
- 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 +95 -618
- package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +317 -51
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +157 -0
- 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 +276 -192
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +262 -0
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +395 -0
- package/src/utils/noteDoc.ts +609 -0
- package/src/utils/organization.ts +337 -84
- package/src/utils/pageRoute.ts +71 -0
- 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 +199 -107
- package/src/utils/sheetAxisMove.ts +616 -0
- package/src/utils/sheetBootstrap.ts +1402 -0
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +358 -0
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +186 -0
- 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 +899 -0
- package/src/utils/sheetFormulaEvaluator.ts +997 -0
- 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 +200 -0
- package/src/utils/sheetNumberFormat.ts +1012 -0
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +118 -0
- package/src/utils/sheetSnapshot.ts +739 -0
- package/src/utils/sheetStructureEdit.ts +914 -0
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/square.ts +22 -12
- package/src/utils/storage.ts +198 -0
- package/src/utils/table-route.ts +127 -11
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +334 -40
- 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 +111 -0
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -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 +514 -0
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +352 -0
- 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 +468 -0
- 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 +22 -0
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +537 -0
- 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/tsconfig.json +10 -10
- package/vitest.config.ts +8 -0
- package/src/constants/log-activityTexts.ts +0 -235
- package/src/constants/product.ts +0 -42
- package/src/constants/report.ts +0 -44
- package/src/schemas/organization.ts +0 -65
- package/src/schemas/productsQuery.ts +0 -53
- package/src/schemas/reportsQuery.ts +0 -20
- package/src/types/product.d.ts +0 -84
- package/src/types/report.d.ts +0 -290
- package/src/utils/product-calEvent.ts +0 -455
- package/src/utils/report.ts +0 -158
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
import { DateTime } from 'luxon';
|
|
2
|
+
import {
|
|
3
|
+
SHEET_FORMULA_AT_PREFIX_BUILTIN_FUNCTION_NAMES,
|
|
4
|
+
SHEET_FORMULA_ERROR_DISPLAY_BY_CODE,
|
|
5
|
+
SHEET_FORMULA_ERROR_FALLBACK_DISPLAY,
|
|
6
|
+
} from '../constants/sheet.ts';
|
|
7
|
+
import type { SheetFormulaDateTimeShorthandName } from './sheetFormulaSyntax.ts';
|
|
8
|
+
import { getSheetSerialFromDateTime } from './sheetNumberFormat.ts';
|
|
9
|
+
|
|
10
|
+
export type SheetFormulaBuiltinFunctionName =
|
|
11
|
+
| 'ABS'
|
|
12
|
+
| 'AND'
|
|
13
|
+
| 'AVERAGE'
|
|
14
|
+
| 'AVERAGEIF'
|
|
15
|
+
| 'AVERAGEIFS'
|
|
16
|
+
| 'CEILING'
|
|
17
|
+
| 'CONCAT'
|
|
18
|
+
| 'CONCATENATE'
|
|
19
|
+
| 'COUNT'
|
|
20
|
+
| 'COUNTA'
|
|
21
|
+
| 'COUNTBLANK'
|
|
22
|
+
| 'COUNTIF'
|
|
23
|
+
| 'COUNTIFS'
|
|
24
|
+
| 'DATE'
|
|
25
|
+
| 'DATEDIF'
|
|
26
|
+
| 'DAY'
|
|
27
|
+
| 'DAYS'
|
|
28
|
+
| 'EDATE'
|
|
29
|
+
| 'EOMONTH'
|
|
30
|
+
| 'FIND'
|
|
31
|
+
| 'FLOOR'
|
|
32
|
+
| 'HLOOKUP'
|
|
33
|
+
| 'IF'
|
|
34
|
+
| 'IFERROR'
|
|
35
|
+
| 'IFNA'
|
|
36
|
+
| 'IFS'
|
|
37
|
+
| 'INCLUDES'
|
|
38
|
+
| 'INDEX'
|
|
39
|
+
| 'INT'
|
|
40
|
+
| 'ISBLANK'
|
|
41
|
+
| 'ISERROR'
|
|
42
|
+
| 'ISNA'
|
|
43
|
+
| 'ISNUMBER'
|
|
44
|
+
| 'ISTEXT'
|
|
45
|
+
| 'LEFT'
|
|
46
|
+
| 'LEN'
|
|
47
|
+
| 'LOWER'
|
|
48
|
+
| 'MATCH'
|
|
49
|
+
| 'MAX'
|
|
50
|
+
| 'MID'
|
|
51
|
+
| 'MIN'
|
|
52
|
+
| 'MOD'
|
|
53
|
+
| 'MONTH'
|
|
54
|
+
| 'NETWORKDAYS'
|
|
55
|
+
| 'NOT'
|
|
56
|
+
| 'NOW'
|
|
57
|
+
| 'OR'
|
|
58
|
+
| 'POWER'
|
|
59
|
+
| 'PROPER'
|
|
60
|
+
| 'REGEXMATCH'
|
|
61
|
+
| 'REPLACE'
|
|
62
|
+
| 'RIGHT'
|
|
63
|
+
| 'ROUND'
|
|
64
|
+
| 'ROUNDDOWN'
|
|
65
|
+
| 'ROUNDUP'
|
|
66
|
+
| 'SEARCH'
|
|
67
|
+
| 'SIGN'
|
|
68
|
+
| 'SQRT'
|
|
69
|
+
| 'SUBSTITUTE'
|
|
70
|
+
| 'SUM'
|
|
71
|
+
| 'SUMIF'
|
|
72
|
+
| 'SUMIFS'
|
|
73
|
+
| 'SUMPRODUCT'
|
|
74
|
+
| 'SWITCH'
|
|
75
|
+
| 'TEXT'
|
|
76
|
+
| 'TEXTJOIN'
|
|
77
|
+
| 'TODAY'
|
|
78
|
+
| 'TRIM'
|
|
79
|
+
| 'TRUNC'
|
|
80
|
+
| 'UNIQUE'
|
|
81
|
+
| 'UPPER'
|
|
82
|
+
| 'VALUE'
|
|
83
|
+
| 'VLOOKUP'
|
|
84
|
+
| 'WEEKDAY'
|
|
85
|
+
| 'WEEKNUM'
|
|
86
|
+
| 'WORKDAY'
|
|
87
|
+
| 'XLOOKUP'
|
|
88
|
+
| 'XOR'
|
|
89
|
+
| 'YEAR';
|
|
90
|
+
|
|
91
|
+
export type SheetFormulaPureEvaluationError = {
|
|
92
|
+
code: string;
|
|
93
|
+
message: string;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export type SheetFormulaPureEvaluationResult = {
|
|
97
|
+
error?: SheetFormulaPureEvaluationError;
|
|
98
|
+
value: unknown;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/* Range or intermediate array value. `values` is row-major; rowCount and
|
|
102
|
+
* columnCount are stamped by the shared core from the reference when the
|
|
103
|
+
* resolver returned a dense rectangle, and are always set on ARRAY values. */
|
|
104
|
+
export type SheetFormulaRangeLikeValue = {
|
|
105
|
+
columnCount?: number;
|
|
106
|
+
kind: 'SHEET_RANGE_VALUE';
|
|
107
|
+
rowCount?: number;
|
|
108
|
+
source?: 'ARRAY' | 'RANGE';
|
|
109
|
+
values: unknown[];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/* Constant per-evaluation inputs every eager builtin handler receives. */
|
|
113
|
+
export type SheetFormulaEagerFunctionContext = {
|
|
114
|
+
now: Date;
|
|
115
|
+
timeZone: string;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/* One eager builtin: already-evaluated arguments in, pure result out. */
|
|
119
|
+
export type SheetFormulaEagerFunctionHandler = (
|
|
120
|
+
values: unknown[],
|
|
121
|
+
context: SheetFormulaEagerFunctionContext,
|
|
122
|
+
) => SheetFormulaPureEvaluationResult;
|
|
123
|
+
|
|
124
|
+
export {
|
|
125
|
+
getSheetFormulaFunctionReference as getSheetFormulaFunctionGuide,
|
|
126
|
+
SHEET_FORMULA_REFERENCE as SHEET_FORMULA_FUNCTION_GUIDES,
|
|
127
|
+
type SheetFormulaFunctionReference as SheetFormulaFunctionGuide,
|
|
128
|
+
type SheetFormulaReferenceArg as SheetFormulaFunctionGuideArg,
|
|
129
|
+
} from './sheetFormulaReference.ts';
|
|
130
|
+
|
|
131
|
+
type SheetFormulaValueEntry = {
|
|
132
|
+
fromRange: boolean;
|
|
133
|
+
value: unknown;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export const SHEET_FORMULA_BUILTIN_FUNCTION_NAMES: readonly SheetFormulaBuiltinFunctionName[] = [
|
|
137
|
+
'ABS',
|
|
138
|
+
'AND',
|
|
139
|
+
'AVERAGE',
|
|
140
|
+
'AVERAGEIF',
|
|
141
|
+
'AVERAGEIFS',
|
|
142
|
+
'CEILING',
|
|
143
|
+
'CONCAT',
|
|
144
|
+
'CONCATENATE',
|
|
145
|
+
'COUNT',
|
|
146
|
+
'COUNTA',
|
|
147
|
+
'COUNTBLANK',
|
|
148
|
+
'COUNTIF',
|
|
149
|
+
'COUNTIFS',
|
|
150
|
+
'DATE',
|
|
151
|
+
'DATEDIF',
|
|
152
|
+
'DAY',
|
|
153
|
+
'DAYS',
|
|
154
|
+
'EDATE',
|
|
155
|
+
'EOMONTH',
|
|
156
|
+
'FIND',
|
|
157
|
+
'FLOOR',
|
|
158
|
+
'HLOOKUP',
|
|
159
|
+
'IF',
|
|
160
|
+
'IFERROR',
|
|
161
|
+
'IFNA',
|
|
162
|
+
'IFS',
|
|
163
|
+
'INCLUDES',
|
|
164
|
+
'INDEX',
|
|
165
|
+
'INT',
|
|
166
|
+
'ISBLANK',
|
|
167
|
+
'ISERROR',
|
|
168
|
+
'ISNA',
|
|
169
|
+
'ISNUMBER',
|
|
170
|
+
'ISTEXT',
|
|
171
|
+
'LEFT',
|
|
172
|
+
'LEN',
|
|
173
|
+
'LOWER',
|
|
174
|
+
'MATCH',
|
|
175
|
+
'MAX',
|
|
176
|
+
'MID',
|
|
177
|
+
'MIN',
|
|
178
|
+
'MOD',
|
|
179
|
+
'MONTH',
|
|
180
|
+
'NETWORKDAYS',
|
|
181
|
+
'NOT',
|
|
182
|
+
'NOW',
|
|
183
|
+
'OR',
|
|
184
|
+
'POWER',
|
|
185
|
+
'PROPER',
|
|
186
|
+
'REGEXMATCH',
|
|
187
|
+
'REPLACE',
|
|
188
|
+
'RIGHT',
|
|
189
|
+
'ROUND',
|
|
190
|
+
'ROUNDDOWN',
|
|
191
|
+
'ROUNDUP',
|
|
192
|
+
'SEARCH',
|
|
193
|
+
'SIGN',
|
|
194
|
+
'SQRT',
|
|
195
|
+
'SUBSTITUTE',
|
|
196
|
+
'SUM',
|
|
197
|
+
'SUMIF',
|
|
198
|
+
'SUMIFS',
|
|
199
|
+
'SUMPRODUCT',
|
|
200
|
+
'SWITCH',
|
|
201
|
+
'TEXT',
|
|
202
|
+
'TEXTJOIN',
|
|
203
|
+
'TODAY',
|
|
204
|
+
'TRIM',
|
|
205
|
+
'TRUNC',
|
|
206
|
+
'UNIQUE',
|
|
207
|
+
'UPPER',
|
|
208
|
+
'VALUE',
|
|
209
|
+
'VLOOKUP',
|
|
210
|
+
'WEEKDAY',
|
|
211
|
+
'WEEKNUM',
|
|
212
|
+
'WORKDAY',
|
|
213
|
+
'XLOOKUP',
|
|
214
|
+
'XOR',
|
|
215
|
+
'YEAR',
|
|
216
|
+
];
|
|
217
|
+
|
|
218
|
+
const FILE_CONSTANTS = {
|
|
219
|
+
atPrefixedBuiltinNameSet: new Set<string>(SHEET_FORMULA_AT_PREFIX_BUILTIN_FUNCTION_NAMES),
|
|
220
|
+
builtinFunctionNameSet: new Set<string>(SHEET_FORMULA_BUILTIN_FUNCTION_NAMES),
|
|
221
|
+
conditionalFunctionNameSet: new Set<string>([
|
|
222
|
+
'IF',
|
|
223
|
+
'IFERROR',
|
|
224
|
+
'IFNA',
|
|
225
|
+
'IFS',
|
|
226
|
+
'INCLUDES',
|
|
227
|
+
'ISBLANK',
|
|
228
|
+
'ISERROR',
|
|
229
|
+
'ISNA',
|
|
230
|
+
'ISNUMBER',
|
|
231
|
+
'ISTEXT',
|
|
232
|
+
'SWITCH',
|
|
233
|
+
'XLOOKUP',
|
|
234
|
+
]),
|
|
235
|
+
errorDisplayValueSet: new Set<string>(
|
|
236
|
+
[...Object.values(SHEET_FORMULA_ERROR_DISPLAY_BY_CODE), SHEET_FORMULA_ERROR_FALLBACK_DISPLAY].map((value) => value.toLowerCase()),
|
|
237
|
+
),
|
|
238
|
+
explicitDateTimeZonePattern:
|
|
239
|
+
/(?:[zZ]|[+-]\d{2}:?\d{2}|(?:GMT|UTC)(?:[+-]\d{1,2}(?::?\d{2})?)?|\b(?:EST|EDT|CST|CDT|MST|MDT|PST|PDT)\b)(?=$|[\s(])/i,
|
|
240
|
+
numericStringPattern: /^[-+]?\d+(?:\.\d+)?$/,
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/*
|
|
244
|
+
* Return a normalized builtin formula function name. The legacy '@name(...)'
|
|
245
|
+
* spelling resolves only for the original builtins, so newer builtin names
|
|
246
|
+
* never shadow a data table lookup such as '@index(...)'.
|
|
247
|
+
*/
|
|
248
|
+
export function getSheetFormulaBuiltinFunctionName(name: string): SheetFormulaBuiltinFunctionName | null {
|
|
249
|
+
const trimmedName = name.trim();
|
|
250
|
+
const isAtPrefixed = trimmedName.startsWith('@');
|
|
251
|
+
const normalizedName = trimmedName.replace(/^@/, '').toUpperCase();
|
|
252
|
+
if (
|
|
253
|
+
!FILE_CONSTANTS.builtinFunctionNameSet.has(normalizedName) ||
|
|
254
|
+
(isAtPrefixed && !FILE_CONSTANTS.atPrefixedBuiltinNameSet.has(normalizedName))
|
|
255
|
+
) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return normalizedName as SheetFormulaBuiltinFunctionName;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/*
|
|
263
|
+
* Return whether one builtin function must control evaluation of its arguments.
|
|
264
|
+
*/
|
|
265
|
+
export function isSheetFormulaConditionalFunctionName(name: string) {
|
|
266
|
+
const normalizedName = name.replace(/^@/, '').toUpperCase();
|
|
267
|
+
return FILE_CONSTANTS.conditionalFunctionNameSet.has(normalizedName);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/*
|
|
271
|
+
* Return whether one formula scalar should be treated as blank.
|
|
272
|
+
*/
|
|
273
|
+
export function isSheetFormulaBlankValue(value: unknown) {
|
|
274
|
+
return value === null || value === undefined || value === '';
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/*
|
|
278
|
+
* Return a string value for formula concatenation and lookups.
|
|
279
|
+
*/
|
|
280
|
+
export function getSheetFormulaStringValue(value: unknown) {
|
|
281
|
+
return value === null || value === undefined ? '' : String(value);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/*
|
|
285
|
+
* Return the spreadsheet text form of one scalar for the & operator and text
|
|
286
|
+
* functions: blank is '', booleans are TRUE/FALSE, and numbers print at 15
|
|
287
|
+
* significant digits so 0.1+0.2 joins as "0.3".
|
|
288
|
+
*/
|
|
289
|
+
export function getSheetFormulaTextValue(value: unknown) {
|
|
290
|
+
if (value === null || value === undefined) {
|
|
291
|
+
return '';
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (typeof value === 'boolean') {
|
|
295
|
+
return value ? 'TRUE' : 'FALSE';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
299
|
+
return String(Number(value.toPrecision(15)));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return String(value);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/*
|
|
306
|
+
* Return a numeric value from a scalar formula value when it can be coerced.
|
|
307
|
+
*/
|
|
308
|
+
export function getSheetFormulaNumericValue(value: unknown) {
|
|
309
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
310
|
+
return value;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (typeof value === 'boolean') {
|
|
314
|
+
return value ? 1 : 0;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (typeof value === 'string' && value.trim() && FILE_CONSTANTS.numericStringPattern.test(value.trim())) {
|
|
318
|
+
return Number(value.trim());
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/*
|
|
325
|
+
* Return a strict numeric value without coercing strings or booleans.
|
|
326
|
+
*/
|
|
327
|
+
export function getSheetFormulaStrictNumberValue(value: unknown) {
|
|
328
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/*
|
|
332
|
+
* Return a logical boolean value from a formula scalar, or null when unsupported.
|
|
333
|
+
*/
|
|
334
|
+
export function getSheetFormulaLogicalBooleanValue(value: unknown) {
|
|
335
|
+
if (typeof value === 'boolean') {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const numberValue = getSheetFormulaNumericValue(value);
|
|
340
|
+
if (numberValue !== null) {
|
|
341
|
+
return numberValue !== 0;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (typeof value === 'string') {
|
|
345
|
+
const normalized = value.trim().toUpperCase();
|
|
346
|
+
if (normalized === 'TRUE') {
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (normalized === 'FALSE' || normalized === '') {
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (value === null || value === undefined) {
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return null;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/*
|
|
363
|
+
* Return whether one evaluated formula value is a range-like value.
|
|
364
|
+
*/
|
|
365
|
+
export function isSheetFormulaRangeLikeValue(value: unknown): value is SheetFormulaRangeLikeValue {
|
|
366
|
+
return Boolean(
|
|
367
|
+
value &&
|
|
368
|
+
typeof value === 'object' &&
|
|
369
|
+
(value as SheetFormulaRangeLikeValue).kind === 'SHEET_RANGE_VALUE' &&
|
|
370
|
+
Array.isArray((value as SheetFormulaRangeLikeValue).values),
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/*
|
|
375
|
+
* Return a timezone-aware DateTime for one formula date-like value.
|
|
376
|
+
*/
|
|
377
|
+
export function getSheetFormulaDateTimeValue(value: unknown, timeZone: string) {
|
|
378
|
+
if (value instanceof Date && Number.isFinite(value.getTime())) {
|
|
379
|
+
return DateTime.fromJSDate(value, { zone: timeZone });
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (typeof value === 'string' && value.trim()) {
|
|
383
|
+
const dateTime = DateTime.fromISO(value.trim(), { zone: timeZone });
|
|
384
|
+
return dateTime.isValid ? dateTime : null;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return null;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/*
|
|
391
|
+
* Return a canonical sheet date string.
|
|
392
|
+
*/
|
|
393
|
+
export function formatSheetFormulaDateValue(dateTime: DateTime) {
|
|
394
|
+
return dateTime.toFormat('yyyy-MM-dd');
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/*
|
|
398
|
+
* Return the organization-zoned current value for a formula date/time shorthand.
|
|
399
|
+
*/
|
|
400
|
+
export function getSheetFormulaDateTimeShorthandValue(name: SheetFormulaDateTimeShorthandName, now: Date, timeZone: string) {
|
|
401
|
+
const dateTime = DateTime.fromJSDate(now, { zone: timeZone });
|
|
402
|
+
|
|
403
|
+
return name === 'CURRENT_DATE' ? formatSheetFormulaDateValue(dateTime) : dateTime.toISO();
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/*
|
|
407
|
+
* Return a stable freshness-key value for one date/time shorthand.
|
|
408
|
+
*/
|
|
409
|
+
export function getSheetFormulaDateTimeShorthandKeyValue(name: SheetFormulaDateTimeShorthandName, now: Date, timeZone: string) {
|
|
410
|
+
const dateTime = DateTime.fromJSDate(now, { zone: timeZone });
|
|
411
|
+
|
|
412
|
+
return name === 'CURRENT_DATE' ? formatSheetFormulaDateValue(dateTime) : dateTime.toFormat("yyyy-MM-dd'T'HH:mm");
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/*
|
|
416
|
+
* Return the display token for one formula error code: Excel tokens for the
|
|
417
|
+
* codes that have one (#N/A, #VALUE!, #REF!, #NUM!, #DIV/0!), '#ERROR!'
|
|
418
|
+
* otherwise. The single display map for client, server and every evaluator.
|
|
419
|
+
*/
|
|
420
|
+
export function getSheetFormulaErrorCodeDisplayValue(code?: string | null) {
|
|
421
|
+
return SHEET_FORMULA_ERROR_DISPLAY_BY_CODE[String(code || '')] || SHEET_FORMULA_ERROR_FALLBACK_DISPLAY;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/*
|
|
425
|
+
* Return whether one scalar is exactly a formula error display token.
|
|
426
|
+
*/
|
|
427
|
+
export function isSheetFormulaErrorDisplayToken(value: unknown) {
|
|
428
|
+
return typeof value === 'string' && FILE_CONSTANTS.errorDisplayValueSet.has(value.trim().toLowerCase());
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/*
|
|
432
|
+
* Return a formula pure evaluation error result.
|
|
433
|
+
*/
|
|
434
|
+
export function getSheetFormulaPureErrorResult(code: string, message: string): SheetFormulaPureEvaluationResult {
|
|
435
|
+
return {
|
|
436
|
+
error: {
|
|
437
|
+
code,
|
|
438
|
+
message,
|
|
439
|
+
},
|
|
440
|
+
value: getSheetFormulaErrorCodeDisplayValue(code),
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/*
|
|
445
|
+
* Return whether one formula error is the spreadsheet not-found error.
|
|
446
|
+
*/
|
|
447
|
+
export function isSheetFormulaNotFoundError(error?: SheetFormulaPureEvaluationError | null) {
|
|
448
|
+
return error?.code === 'NOT_FOUND';
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/*
|
|
452
|
+
* Flatten scalar and range formula values into function argument entries.
|
|
453
|
+
*/
|
|
454
|
+
function flattenSheetFormulaValueEntries(values: unknown[]) {
|
|
455
|
+
return values.flatMap((value): SheetFormulaValueEntry[] => {
|
|
456
|
+
if (isSheetFormulaRangeLikeValue(value)) {
|
|
457
|
+
return value.values.map((rangeValue) => ({
|
|
458
|
+
fromRange: true,
|
|
459
|
+
value: rangeValue,
|
|
460
|
+
}));
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return [
|
|
464
|
+
{
|
|
465
|
+
fromRange: false,
|
|
466
|
+
value,
|
|
467
|
+
},
|
|
468
|
+
];
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/*
|
|
473
|
+
* Return a spreadsheet serial number for one MAX value that Date can parse.
|
|
474
|
+
*/
|
|
475
|
+
function getSheetFormulaMaxDateSerialValue(value: unknown, timeZone: string) {
|
|
476
|
+
const formulaDateTime = getSheetFormulaDateTimeValue(value, timeZone);
|
|
477
|
+
if (formulaDateTime) {
|
|
478
|
+
return getSheetSerialFromDateTime(formulaDateTime);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const trimmedValue = value.trim();
|
|
486
|
+
const parsedDate = new Date(trimmedValue);
|
|
487
|
+
if (!Number.isFinite(parsedDate.getTime())) {
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const parsedDateTime = FILE_CONSTANTS.explicitDateTimeZonePattern.test(trimmedValue)
|
|
492
|
+
? DateTime.fromJSDate(parsedDate, { zone: timeZone })
|
|
493
|
+
: DateTime.fromObject(
|
|
494
|
+
{
|
|
495
|
+
year: parsedDate.getFullYear(),
|
|
496
|
+
month: parsedDate.getMonth() + 1,
|
|
497
|
+
day: parsedDate.getDate(),
|
|
498
|
+
hour: parsedDate.getHours(),
|
|
499
|
+
minute: parsedDate.getMinutes(),
|
|
500
|
+
second: parsedDate.getSeconds(),
|
|
501
|
+
millisecond: parsedDate.getMilliseconds(),
|
|
502
|
+
},
|
|
503
|
+
{ zone: timeZone },
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
return parsedDateTime.isValid ? getSheetSerialFromDateTime(parsedDateTime) : null;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/*
|
|
510
|
+
* Return one MAX argument as a number, including Date-compatible values.
|
|
511
|
+
*/
|
|
512
|
+
function getSheetFormulaMaxNumericValue(value: unknown, timeZone: string) {
|
|
513
|
+
return getSheetFormulaNumericValue(value) ?? getSheetFormulaMaxDateSerialValue(value, timeZone);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/*
|
|
517
|
+
* Return numeric entries for aggregate functions, optionally converting MAX dates.
|
|
518
|
+
*/
|
|
519
|
+
function getSheetFormulaAggregateNumericValues(values: unknown[], convertMaxDates = false, timeZone = 'UTC') {
|
|
520
|
+
const numbers: number[] = [];
|
|
521
|
+
const entries = flattenSheetFormulaValueEntries(values);
|
|
522
|
+
for (const entry of entries) {
|
|
523
|
+
if (isSheetFormulaBlankValue(entry.value)) {
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const numberValue = convertMaxDates
|
|
528
|
+
? getSheetFormulaMaxNumericValue(entry.value, timeZone)
|
|
529
|
+
: getSheetFormulaNumericValue(entry.value);
|
|
530
|
+
if (numberValue !== null) {
|
|
531
|
+
numbers.push(numberValue);
|
|
532
|
+
continue;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (!entry.fromRange) {
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return numbers;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/*
|
|
544
|
+
* Return nonblank entries for COUNTA.
|
|
545
|
+
*/
|
|
546
|
+
function getSheetFormulaCountAValue(values: unknown[]) {
|
|
547
|
+
return flattenSheetFormulaValueEntries(values).filter((entry) => !isSheetFormulaBlankValue(entry.value)).length;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/*
|
|
551
|
+
* Return the count of strict numeric values for COUNT.
|
|
552
|
+
*/
|
|
553
|
+
function getSheetFormulaCountValue(values: unknown[]) {
|
|
554
|
+
return flattenSheetFormulaValueEntries(values).filter((entry) => getSheetFormulaStrictNumberValue(entry.value) !== null).length;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/*
|
|
558
|
+
* Return scalar numeric function arguments with range values flattened.
|
|
559
|
+
*/
|
|
560
|
+
function getSheetFormulaNumericFunctionArgs(values: unknown[], minCount: number, maxCount: number) {
|
|
561
|
+
const entries = flattenSheetFormulaValueEntries(values).filter((entry) => !isSheetFormulaBlankValue(entry.value));
|
|
562
|
+
if (entries.length < minCount || entries.length > maxCount) {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const numbers = entries.map((entry) => getSheetFormulaNumericValue(entry.value));
|
|
567
|
+
return numbers.every((value) => value !== null) ? (numbers as number[]) : null;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/*
|
|
571
|
+
* Evaluate an aggregate formula function from already-evaluated argument values.
|
|
572
|
+
*/
|
|
573
|
+
export function evaluateSheetFormulaAggregateFunction(
|
|
574
|
+
name: string,
|
|
575
|
+
values: unknown[],
|
|
576
|
+
timeZone = 'UTC',
|
|
577
|
+
): SheetFormulaPureEvaluationResult | null {
|
|
578
|
+
if (!['AVERAGE', 'COUNT', 'COUNTA', 'MAX', 'MIN', 'SUM'].includes(name)) {
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
if (name === 'COUNT') {
|
|
583
|
+
return {
|
|
584
|
+
value: getSheetFormulaCountValue(values),
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
if (name === 'COUNTA') {
|
|
589
|
+
return {
|
|
590
|
+
value: getSheetFormulaCountAValue(values),
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const numbers = getSheetFormulaAggregateNumericValues(values, name === 'MAX', timeZone);
|
|
595
|
+
if (!numbers) {
|
|
596
|
+
return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.');
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
if (name === 'SUM') {
|
|
600
|
+
return {
|
|
601
|
+
value: numbers.reduce((sum, value) => sum + value, 0),
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (name === 'AVERAGE') {
|
|
606
|
+
return numbers.length
|
|
607
|
+
? {
|
|
608
|
+
value: numbers.reduce((sum, value) => sum + value, 0) / numbers.length,
|
|
609
|
+
}
|
|
610
|
+
: getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula average requires at least one numeric value.');
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (name === 'MIN') {
|
|
614
|
+
return numbers.length
|
|
615
|
+
? {
|
|
616
|
+
value: Math.min(...numbers),
|
|
617
|
+
}
|
|
618
|
+
: getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.');
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
if (name === 'MAX') {
|
|
622
|
+
return numbers.length
|
|
623
|
+
? {
|
|
624
|
+
value: Math.max(...numbers),
|
|
625
|
+
}
|
|
626
|
+
: getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.');
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
return null;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/*
|
|
633
|
+
* Round the magnitude of one number at a decimal precision with one integer
|
|
634
|
+
* rounding function. The scaled magnitude is pre-rounded to 15 significant
|
|
635
|
+
* digits so binary representation error (1.005 * 100 = 100.49999...) never
|
|
636
|
+
* decides the result, and negative places divide instead of multiplying by a
|
|
637
|
+
* fraction (25 / 10 instead of 25 * 0.1) so the result stays exact.
|
|
638
|
+
*/
|
|
639
|
+
function roundSheetFormulaMagnitude(value: number, places: number, roundInteger: (value: number) => number) {
|
|
640
|
+
const digits = Math.trunc(places);
|
|
641
|
+
const factor = 10 ** Math.abs(digits);
|
|
642
|
+
if (!Number.isFinite(factor)) {
|
|
643
|
+
// Beyond double precision: nothing left to round, or rounds to zero
|
|
644
|
+
return digits >= 0 ? value : 0;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const magnitude = Math.abs(value);
|
|
648
|
+
const scaled = digits >= 0 ? magnitude * factor : magnitude / factor;
|
|
649
|
+
if (!Number.isFinite(scaled)) {
|
|
650
|
+
// Scaled magnitude overflows double precision: no digits left to round
|
|
651
|
+
return value;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const rounded = roundInteger(Number(scaled.toPrecision(15)));
|
|
655
|
+
const result = digits >= 0 ? rounded / factor : rounded * factor;
|
|
656
|
+
|
|
657
|
+
return result === 0 ? 0 : Math.sign(value) * result;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/*
|
|
661
|
+
* Return a number rounded half away from zero at a decimal precision
|
|
662
|
+
* (Excel ROUND: ROUND(-2.5) = -3, ROUND(1.005, 2) = 1.01).
|
|
663
|
+
*/
|
|
664
|
+
function roundSheetFormulaNumber(value: number, places = 0) {
|
|
665
|
+
return roundSheetFormulaMagnitude(value, places, Math.round);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/*
|
|
669
|
+
* Return a number rounded away from zero at a decimal precision.
|
|
670
|
+
*/
|
|
671
|
+
function roundUpSheetFormulaNumber(value: number, places = 0) {
|
|
672
|
+
return roundSheetFormulaMagnitude(value, places, Math.ceil);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/*
|
|
676
|
+
* Return a number rounded toward zero at a decimal precision (ROUNDDOWN and
|
|
677
|
+
* TRUNC).
|
|
678
|
+
*/
|
|
679
|
+
export function roundDownSheetFormulaNumber(value: number, places = 0) {
|
|
680
|
+
return roundSheetFormulaMagnitude(value, places, Math.floor);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/*
|
|
684
|
+
* Return base raised to exponent, with non-finite results reported as formula
|
|
685
|
+
* errors instead of Infinity/NaN values: 0 to a negative power divides by
|
|
686
|
+
* zero, and NaN or overflow is an invalid number (#NUM!). Shared by POWER()
|
|
687
|
+
* and the ^ operator.
|
|
688
|
+
*/
|
|
689
|
+
export function getSheetFormulaPowerResult(base: number, exponent: number): SheetFormulaPureEvaluationResult {
|
|
690
|
+
if (base === 0 && exponent < 0) {
|
|
691
|
+
return getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula cannot raise zero to a negative power.');
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const value = base ** exponent;
|
|
695
|
+
return Number.isFinite(value)
|
|
696
|
+
? { value }
|
|
697
|
+
: getSheetFormulaPureErrorResult('INVALID_NUMBER', 'Formula power result is not a finite real number.');
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/*
|
|
701
|
+
* Evaluate a numeric formula function from already-evaluated argument values.
|
|
702
|
+
*/
|
|
703
|
+
export function evaluateSheetFormulaMathFunction(name: string, values: unknown[]): SheetFormulaPureEvaluationResult | null {
|
|
704
|
+
const unaryArgs = ['ABS'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 1, 1) : null;
|
|
705
|
+
if (unaryArgs) {
|
|
706
|
+
return {
|
|
707
|
+
value: Math.abs(unaryArgs[0]),
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
const roundedArgs = ['ROUND', 'ROUNDUP', 'ROUNDDOWN'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 1, 2) : null;
|
|
712
|
+
if (roundedArgs) {
|
|
713
|
+
const places = roundedArgs[1] || 0;
|
|
714
|
+
const roundFn =
|
|
715
|
+
name === 'ROUNDUP' ? roundUpSheetFormulaNumber : name === 'ROUNDDOWN' ? roundDownSheetFormulaNumber : roundSheetFormulaNumber;
|
|
716
|
+
|
|
717
|
+
return {
|
|
718
|
+
value: roundFn(roundedArgs[0], places),
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
const ceilingFloorArgs = ['CEILING', 'FLOOR'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 1, 2) : null;
|
|
723
|
+
if (ceilingFloorArgs) {
|
|
724
|
+
const significance = ceilingFloorArgs[1] || 1;
|
|
725
|
+
if (significance === 0) {
|
|
726
|
+
return getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula significance cannot be zero.');
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
return {
|
|
730
|
+
value: (name === 'CEILING' ? Math.ceil : Math.floor)(ceilingFloorArgs[0] / significance) * significance,
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
const binaryArgs = ['MOD', 'POWER'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 2, 2) : null;
|
|
735
|
+
if (binaryArgs && name === 'MOD') {
|
|
736
|
+
if (binaryArgs[1] === 0) {
|
|
737
|
+
return getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula cannot divide by zero.');
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
return {
|
|
741
|
+
value: ((binaryArgs[0] % binaryArgs[1]) + binaryArgs[1]) % binaryArgs[1],
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
if (binaryArgs && name === 'POWER') {
|
|
746
|
+
return getSheetFormulaPowerResult(binaryArgs[0], binaryArgs[1]);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
return ['ABS', 'CEILING', 'FLOOR', 'MOD', 'POWER', 'ROUND', 'ROUNDDOWN', 'ROUNDUP'].includes(name)
|
|
750
|
+
? getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.')
|
|
751
|
+
: null;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
/*
|
|
755
|
+
* Evaluate a date or time formula function from already-evaluated argument values.
|
|
756
|
+
*/
|
|
757
|
+
export function evaluateSheetFormulaDateFunction(
|
|
758
|
+
name: string,
|
|
759
|
+
values: unknown[],
|
|
760
|
+
timeZone: string,
|
|
761
|
+
now: Date,
|
|
762
|
+
): SheetFormulaPureEvaluationResult | null {
|
|
763
|
+
if (name === 'TODAY') {
|
|
764
|
+
return values.length
|
|
765
|
+
? getSheetFormulaPureErrorResult('INVALID_FORMULA', 'TODAY does not accept arguments.')
|
|
766
|
+
: {
|
|
767
|
+
value: formatSheetFormulaDateValue(DateTime.fromJSDate(now, { zone: timeZone })),
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
if (name === 'NOW') {
|
|
772
|
+
return values.length
|
|
773
|
+
? getSheetFormulaPureErrorResult('INVALID_FORMULA', 'NOW does not accept arguments.')
|
|
774
|
+
: {
|
|
775
|
+
value: DateTime.fromJSDate(now, { zone: timeZone }).toISO(),
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
if (name === 'DATE') {
|
|
780
|
+
const args = getSheetFormulaNumericFunctionArgs(values, 3, 3);
|
|
781
|
+
const dateTime = args
|
|
782
|
+
? DateTime.fromObject(
|
|
783
|
+
{
|
|
784
|
+
year: Math.trunc(args[0]),
|
|
785
|
+
month: Math.trunc(args[1]),
|
|
786
|
+
day: Math.trunc(args[2]),
|
|
787
|
+
},
|
|
788
|
+
{
|
|
789
|
+
zone: timeZone,
|
|
790
|
+
},
|
|
791
|
+
)
|
|
792
|
+
: null;
|
|
793
|
+
|
|
794
|
+
return dateTime?.isValid
|
|
795
|
+
? {
|
|
796
|
+
value: formatSheetFormulaDateValue(dateTime),
|
|
797
|
+
}
|
|
798
|
+
: getSheetFormulaPureErrorResult('INVALID_FORMULA', 'DATE requires valid year, month, and day values.');
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
if (['YEAR', 'MONTH', 'DAY'].includes(name)) {
|
|
802
|
+
const entries = flattenSheetFormulaValueEntries(values).filter((entry) => !isSheetFormulaBlankValue(entry.value));
|
|
803
|
+
const dateTime = entries.length === 1 ? getSheetFormulaDateTimeValue(entries[0].value, timeZone) : null;
|
|
804
|
+
if (!dateTime?.isValid) {
|
|
805
|
+
return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula date function requires one date value.');
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
return {
|
|
809
|
+
value: name === 'YEAR' ? dateTime.year : name === 'MONTH' ? dateTime.month : dateTime.day,
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
return null;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/*
|
|
817
|
+
* Return whether two formula scalars are equal using '=' comparison semantics:
|
|
818
|
+
* numeric compare when both coerce to numbers, otherwise string compare. Shared
|
|
819
|
+
* so range membership matches the binary '=' operator in both evaluators.
|
|
820
|
+
*/
|
|
821
|
+
export function sheetFormulaScalarValuesEqual(leftValue: unknown, rightValue: unknown) {
|
|
822
|
+
const leftNumber = getSheetFormulaNumericValue(leftValue);
|
|
823
|
+
const rightNumber = getSheetFormulaNumericValue(rightValue);
|
|
824
|
+
if (leftNumber !== null && rightNumber !== null) {
|
|
825
|
+
return leftNumber === rightNumber;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
return getSheetFormulaStringValue(leftValue) === getSheetFormulaStringValue(rightValue);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
/*
|
|
832
|
+
* Return whether a range value contains one scalar needle. Blank cells and a
|
|
833
|
+
* blank needle never match. Range entries are already resolved scalars, so this
|
|
834
|
+
* works over literal and calculated cells alike.
|
|
835
|
+
*/
|
|
836
|
+
export function sheetFormulaRangeIncludesValue(rangeValue: SheetFormulaRangeLikeValue, needle: unknown) {
|
|
837
|
+
if (isSheetFormulaBlankValue(needle)) {
|
|
838
|
+
return false;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
return rangeValue.values.some((value) => !isSheetFormulaBlankValue(value) && sheetFormulaScalarValuesEqual(value, needle));
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/*
|
|
845
|
+
* Return a case-insensitive RegExp for one search pattern, or null when the
|
|
846
|
+
* pattern is not valid regex (the caller falls back to substring matching).
|
|
847
|
+
*/
|
|
848
|
+
function getSheetFormulaRegexMatchRegExp(pattern: string): RegExp | null {
|
|
849
|
+
try {
|
|
850
|
+
return new RegExp(pattern, 'i');
|
|
851
|
+
} catch {
|
|
852
|
+
return null;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/*
|
|
857
|
+
* Return the first range value (row-major) whose string form matches the search
|
|
858
|
+
* pattern, or a NOT_FOUND result when nothing matches (so IFNA can catch it).
|
|
859
|
+
*/
|
|
860
|
+
function evaluateSheetFormulaRegexMatchFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
|
|
861
|
+
if (values.length !== 2) {
|
|
862
|
+
return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'REGEXMATCH requires a range and a search pattern.');
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
const [rangeValue, patternValue] = values;
|
|
866
|
+
if (!isSheetFormulaRangeLikeValue(rangeValue)) {
|
|
867
|
+
return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'REGEXMATCH requires a cell range as its first argument.');
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (isSheetFormulaRangeLikeValue(patternValue) || isSheetFormulaBlankValue(patternValue)) {
|
|
871
|
+
return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'REGEXMATCH requires a search pattern as its second argument.');
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const pattern = getSheetFormulaStringValue(patternValue);
|
|
875
|
+
const regExp = getSheetFormulaRegexMatchRegExp(pattern);
|
|
876
|
+
const substringNeedle = pattern.toLowerCase();
|
|
877
|
+
for (const value of rangeValue.values) {
|
|
878
|
+
if (isSheetFormulaBlankValue(value)) {
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const text = getSheetFormulaStringValue(value);
|
|
883
|
+
const matched = regExp ? regExp.test(text) : text.toLowerCase().includes(substringNeedle);
|
|
884
|
+
if (matched) {
|
|
885
|
+
return {
|
|
886
|
+
value,
|
|
887
|
+
};
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
return getSheetFormulaPureErrorResult('NOT_FOUND', 'REGEXMATCH found no matching value.');
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
/*
|
|
895
|
+
* Evaluate a lookup formula function from already-evaluated argument values.
|
|
896
|
+
*/
|
|
897
|
+
export function evaluateSheetFormulaLookupFunction(name: string, values: unknown[]): SheetFormulaPureEvaluationResult | null {
|
|
898
|
+
return name === 'REGEXMATCH' ? evaluateSheetFormulaRegexMatchFunction(values) : null;
|
|
899
|
+
}
|