@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.
Files changed (210) hide show
  1. package/package.json +58 -23
  2. package/src/constants/agentMemory.ts +14 -45
  3. package/src/constants/agentNote.ts +212 -0
  4. package/src/constants/aiChat.ts +5 -0
  5. package/src/constants/billing.ts +126 -0
  6. package/src/constants/campaign.ts +156 -47
  7. package/src/constants/contact.ts +72 -0
  8. package/src/constants/csvExport.ts +133 -0
  9. package/src/constants/dataTable.ts +12 -38
  10. package/src/constants/dataTableFieldTypes.generated.ts +3 -7
  11. package/src/constants/docAccess.ts +3 -2
  12. package/src/constants/dt.ts +234 -0
  13. package/src/constants/email.ts +114 -12
  14. package/src/constants/fieldDoc.ts +74 -4
  15. package/src/constants/graphqlReactivity.ts +108 -25
  16. package/src/constants/log.ts +9 -0
  17. package/src/constants/logDisplay/README.md +88 -0
  18. package/src/constants/logDisplay/defaults.ts +39 -0
  19. package/src/constants/logDisplay/index.ts +27 -0
  20. package/src/constants/logDisplay/operations/arable.ts +16 -0
  21. package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
  22. package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
  23. package/src/constants/logDisplay/shared/accounting.ts +13 -0
  24. package/src/constants/logDisplay/shared/interactions.ts +13 -0
  25. package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
  26. package/src/constants/logTaxonomy.generated.ts +1834 -730
  27. package/src/constants/organization.ts +13 -14
  28. package/src/constants/qrCode.ts +30 -0
  29. package/src/constants/sheet.ts +65 -101
  30. package/src/constants/sms.ts +18 -0
  31. package/src/constants/storage.ts +33 -9
  32. package/src/constants/usage.ts +3 -0
  33. package/src/constants/workflowSteps.ts +60 -0
  34. package/src/schemas/editLogEntry.ts +106 -283
  35. package/src/schemas/inboundContact.ts +5 -5
  36. package/src/types/agentMemory.d.ts +38 -14
  37. package/src/types/agentNote.d.ts +443 -0
  38. package/src/types/billing.d.ts +537 -0
  39. package/src/types/campaign.d.ts +1043 -193
  40. package/src/types/contact.d.ts +15 -0
  41. package/src/types/csvExport.d.ts +213 -0
  42. package/src/types/dataAnalysis.d.ts +44 -0
  43. package/src/types/dataTable.d.ts +102 -309
  44. package/src/types/definitionElement.d.ts +35 -0
  45. package/src/types/dt.d.ts +257 -0
  46. package/src/types/email.d.ts +144 -55
  47. package/src/types/emailReplyReport.d.ts +76 -0
  48. package/src/types/fieldDoc.d.ts +579 -150
  49. package/src/types/fieldDocContract.d.ts +90 -0
  50. package/src/types/inboundContact.d.ts +8 -0
  51. package/src/types/log.d.ts +94 -3
  52. package/src/types/logDisplay.types.ts +85 -0
  53. package/src/types/organization.d.ts +8 -3
  54. package/src/types/qrCode.d.ts +96 -0
  55. package/src/types/richText.d.ts +81 -0
  56. package/src/types/sheet.d.ts +180 -463
  57. package/src/types/sms.d.ts +38 -0
  58. package/src/types/storage.d.ts +266 -27
  59. package/src/types/workflow.d.ts +196 -66
  60. package/src/utils/agentMemory.ts +15 -0
  61. package/src/utils/agentNote.ts +850 -0
  62. package/src/utils/aiChat.ts +31 -14
  63. package/src/utils/aiChatWork.ts +64 -0
  64. package/src/utils/billing.ts +37 -0
  65. package/src/utils/campaign.ts +642 -209
  66. package/src/utils/campaignAI.ts +72 -0
  67. package/src/utils/campaignAccess.ts +239 -0
  68. package/src/utils/campaignEmail.ts +601 -0
  69. package/src/utils/campaignEmailCompiler.ts +372 -0
  70. package/src/utils/campaignEmailImages.ts +60 -0
  71. package/src/utils/campaignEmailStyles.ts +544 -0
  72. package/src/utils/campaignLifecycle.ts +104 -0
  73. package/src/utils/campaignPreview.ts +285 -0
  74. package/src/utils/contact.ts +50 -0
  75. package/src/utils/csvExport.ts +19 -0
  76. package/src/utils/dataTable.ts +18 -556
  77. package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
  78. package/src/utils/dataTableRelativeDate.ts +447 -0
  79. package/src/utils/definitionElement.ts +84 -0
  80. package/src/utils/email.ts +208 -1
  81. package/src/utils/emailReplyReport.test.ts +187 -0
  82. package/src/utils/emailReplyReport.ts +143 -0
  83. package/src/utils/fieldDoc.ts +81 -37
  84. package/src/utils/fieldDocAnswers.ts +128 -0
  85. package/src/utils/fieldDocDefaults.ts +36 -0
  86. package/src/utils/fieldDocProgram.ts +36 -0
  87. package/src/utils/fieldDocSummary.ts +30 -0
  88. package/src/utils/log.ts +232 -141
  89. package/src/utils/logDisplay.helpers.ts +177 -0
  90. package/src/utils/logDisplay.ts +87 -0
  91. package/src/utils/logStoreRead.ts +9 -13
  92. package/src/utils/logTaxonomy.helpers.ts +17 -0
  93. package/src/utils/logTaxonomyDisplay.ts +80 -124
  94. package/src/utils/noteDoc.ts +66 -284
  95. package/src/utils/organization.ts +117 -283
  96. package/src/utils/qrCode.helpers.ts +189 -0
  97. package/src/utils/qrCode.test.ts +519 -0
  98. package/src/utils/qrCode.ts +273 -0
  99. package/src/utils/richTextHTML.ts +385 -0
  100. package/src/utils/sheet.ts +123 -4834
  101. package/src/utils/sheetAxisMove.ts +77 -291
  102. package/src/utils/sheetBootstrap.ts +771 -1138
  103. package/src/utils/sheetCellLiteral.ts +101 -0
  104. package/src/utils/sheetCommand.ts +23 -88
  105. package/src/utils/sheetDesign.ts +705 -0
  106. package/src/utils/sheetExport.ts +17 -69
  107. package/src/utils/sheetFeatureReference.ts +183 -0
  108. package/src/utils/sheetFormulaArgs.ts +168 -0
  109. package/src/utils/sheetFormulaCompile.ts +475 -0
  110. package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
  111. package/src/utils/sheetFormulaCriteria.ts +656 -0
  112. package/src/utils/sheetFormulaDate.ts +444 -0
  113. package/src/utils/sheetFormulaEvaluation.ts +388 -403
  114. package/src/utils/sheetFormulaEvaluator.ts +589 -444
  115. package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
  116. package/src/utils/sheetFormulaLogical.ts +116 -0
  117. package/src/utils/sheetFormulaLookup.ts +488 -0
  118. package/src/utils/sheetFormulaMath.ts +48 -0
  119. package/src/utils/sheetFormulaParser.ts +729 -0
  120. package/src/utils/sheetFormulaRange.ts +330 -0
  121. package/src/utils/sheetFormulaReference.ts +703 -0
  122. package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
  123. package/src/utils/sheetFormulaReferenceDates.ts +237 -0
  124. package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
  125. package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
  126. package/src/utils/sheetFormulaReferenceMath.ts +95 -0
  127. package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
  128. package/src/utils/sheetFormulaReferenceText.ts +462 -0
  129. package/src/utils/sheetFormulaSyntax.ts +1027 -0
  130. package/src/utils/sheetFormulaText.ts +430 -0
  131. package/src/utils/sheetJournal.ts +140 -18
  132. package/src/utils/sheetNumberFormat.ts +68 -267
  133. package/src/utils/sheetPreview.ts +68 -0
  134. package/src/utils/sheetSequence.ts +13 -43
  135. package/src/utils/sheetSnapshot.ts +176 -429
  136. package/src/utils/sheetStructureEdit.ts +537 -907
  137. package/src/utils/sheetStructureReferences.ts +389 -0
  138. package/src/utils/storage.ts +49 -3
  139. package/src/utils/time.ts +12 -0
  140. package/src/utils/workflow.ts +199 -73
  141. package/src/utils/workflowLogFingerprint.ts +302 -0
  142. package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
  143. package/src/utils/workflowProgress.ts +280 -0
  144. package/src/utils/workflowTriggers.ts +61 -0
  145. package/tests/agentNote.test.ts +395 -0
  146. package/tests/campaignAI.test.ts +119 -0
  147. package/tests/campaignAccess.test.ts +239 -0
  148. package/tests/campaignEmail.test.ts +441 -0
  149. package/tests/campaignLifecycle.test.ts +134 -0
  150. package/tests/campaignPublication.test.ts +309 -0
  151. package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
  152. package/tests/dataTableRelativeDate.test.ts +412 -0
  153. package/tests/email.test.ts +39 -0
  154. package/tests/fieldDocProgram.test.ts +114 -0
  155. package/tests/logDisplay.fixtures.ts +67 -0
  156. package/tests/logDisplay.test.ts +227 -0
  157. package/tests/noteDoc.test.ts +37 -192
  158. package/tests/richTextHTML.test.ts +185 -0
  159. package/tests/sheetAxisMove.test.ts +49 -40
  160. package/tests/sheetBootstrap.test.ts +1967 -0
  161. package/tests/sheetFeatureReference.test.ts +22 -0
  162. package/tests/sheetFormulaCriteria.test.ts +269 -0
  163. package/tests/sheetFormulaEvaluator.test.ts +93 -200
  164. package/tests/sheetFormulaFunctions.test.ts +538 -0
  165. package/tests/sheetFormulaLanguage.test.ts +361 -0
  166. package/tests/sheetFormulaParity.fixtures.ts +872 -0
  167. package/tests/sheetFormulaParity.test.ts +188 -0
  168. package/tests/sheetFormulaRange.test.ts +167 -0
  169. package/tests/sheetFormulaReference.test.ts +78 -0
  170. package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
  171. package/tests/sheetFormulaTextDate.test.ts +512 -0
  172. package/tests/sheetSequence.test.ts +4 -23
  173. package/tests/sheetSnapshot.test.ts +134 -0
  174. package/tests/sheetStructureEdit.test.ts +492 -37
  175. package/tests/sheetStructureReferences.test.ts +589 -0
  176. package/tests/workflowLifecycleStatus.test.ts +60 -0
  177. package/tests/workflowLogFingerprint.test.ts +203 -0
  178. package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
  179. package/tests/workflowProgress.test.ts +180 -0
  180. package/tests/workflowTriggers.test.ts +29 -0
  181. package/src/constants/agentDoc.ts +0 -12
  182. package/src/constants/logActivityGroupColors.generated.ts +0 -19
  183. package/src/constants/schedule.ts +0 -220
  184. package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
  185. package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
  186. package/src/constants/sheetRegionSources.generated.ts +0 -578
  187. package/src/schemas/fieldDocsQuery.ts +0 -12
  188. package/src/schemas/organization.ts +0 -68
  189. package/src/types/agentDoc.d.ts +0 -64
  190. package/src/types/noteDoc.d.ts +0 -86
  191. package/src/types/schedule.d.ts +0 -391
  192. package/src/utils/agentDoc.ts +0 -109
  193. package/src/utils/campaignFormEditor.ts +0 -871
  194. package/src/utils/dataTableRealtimeAuthority.ts +0 -209
  195. package/src/utils/dataTableRealtimeSelection.ts +0 -134
  196. package/src/utils/dataTableRowDocument.ts +0 -508
  197. package/src/utils/dataTableSource.ts +0 -230
  198. package/src/utils/schedule.ts +0 -799
  199. package/src/utils/scheduleEvaluation.ts +0 -1994
  200. package/src/utils/scheduleICS.ts +0 -246
  201. package/src/utils/scheduleRuleTemplates.ts +0 -188
  202. package/src/utils/sheetRegion.ts +0 -374
  203. package/src/utils/sheetRegionFilter.ts +0 -1259
  204. package/src/utils/sheetRegionSourceRegistry.ts +0 -594
  205. package/tests/schedule.test.ts +0 -693
  206. package/tests/scheduleEvaluation.test.ts +0 -1970
  207. package/tests/scheduleICS.test.ts +0 -208
  208. package/tests/scheduleRuleTemplates.test.ts +0 -176
  209. package/tests/scheduleSweepInvariants.test.ts +0 -428
  210. package/tests/sheetRegionFilter.test.ts +0 -1135
@@ -1,31 +1,91 @@
1
1
  import { DateTime } from 'luxon';
2
- import type { SheetFormulaDateTimeShorthandName } from './sheet.ts';
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';
3
9
 
4
10
  export type SheetFormulaBuiltinFunctionName =
5
11
  | 'ABS'
12
+ | 'AND'
6
13
  | 'AVERAGE'
14
+ | 'AVERAGEIF'
15
+ | 'AVERAGEIFS'
7
16
  | 'CEILING'
17
+ | 'CONCAT'
18
+ | 'CONCATENATE'
8
19
  | 'COUNT'
9
20
  | 'COUNTA'
21
+ | 'COUNTBLANK'
22
+ | 'COUNTIF'
23
+ | 'COUNTIFS'
10
24
  | 'DATE'
25
+ | 'DATEDIF'
11
26
  | 'DAY'
27
+ | 'DAYS'
28
+ | 'EDATE'
29
+ | 'EOMONTH'
30
+ | 'FIND'
12
31
  | 'FLOOR'
32
+ | 'HLOOKUP'
13
33
  | 'IF'
14
34
  | 'IFERROR'
15
35
  | 'IFNA'
36
+ | 'IFS'
16
37
  | 'INCLUDES'
38
+ | 'INDEX'
39
+ | 'INT'
40
+ | 'ISBLANK'
41
+ | 'ISERROR'
42
+ | 'ISNA'
43
+ | 'ISNUMBER'
44
+ | 'ISTEXT'
45
+ | 'LEFT'
46
+ | 'LEN'
47
+ | 'LOWER'
48
+ | 'MATCH'
17
49
  | 'MAX'
50
+ | 'MID'
18
51
  | 'MIN'
19
52
  | 'MOD'
20
53
  | 'MONTH'
54
+ | 'NETWORKDAYS'
55
+ | 'NOT'
21
56
  | 'NOW'
57
+ | 'OR'
22
58
  | 'POWER'
59
+ | 'PROPER'
60
+ | 'REGEXMATCH'
61
+ | 'REPLACE'
62
+ | 'RIGHT'
23
63
  | 'ROUND'
24
64
  | 'ROUNDDOWN'
25
65
  | 'ROUNDUP'
26
- | 'REGEXMATCH'
66
+ | 'SEARCH'
67
+ | 'SIGN'
68
+ | 'SQRT'
69
+ | 'SUBSTITUTE'
27
70
  | 'SUM'
71
+ | 'SUMIF'
72
+ | 'SUMIFS'
73
+ | 'SUMPRODUCT'
74
+ | 'SWITCH'
75
+ | 'TEXT'
76
+ | 'TEXTJOIN'
28
77
  | 'TODAY'
78
+ | 'TRIM'
79
+ | 'TRUNC'
80
+ | 'UNIQUE'
81
+ | 'UPPER'
82
+ | 'VALUE'
83
+ | 'VLOOKUP'
84
+ | 'WEEKDAY'
85
+ | 'WEEKNUM'
86
+ | 'WORKDAY'
87
+ | 'XLOOKUP'
88
+ | 'XOR'
29
89
  | 'YEAR';
30
90
 
31
91
  export type SheetFormulaPureEvaluationError = {
@@ -38,196 +98,165 @@ export type SheetFormulaPureEvaluationResult = {
38
98
  value: unknown;
39
99
  };
40
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. */
41
104
  export type SheetFormulaRangeLikeValue = {
105
+ columnCount?: number;
42
106
  kind: 'SHEET_RANGE_VALUE';
107
+ rowCount?: number;
108
+ source?: 'ARRAY' | 'RANGE';
43
109
  values: unknown[];
44
110
  };
45
111
 
46
- export type SheetFormulaFunctionGuideArg = {
47
- descriptionI18nKey: string;
48
- key: string;
112
+ /* Constant per-evaluation inputs every eager builtin handler receives. */
113
+ export type SheetFormulaEagerFunctionContext = {
114
+ now: Date;
115
+ timeZone: string;
49
116
  };
50
117
 
51
- export type SheetFormulaFunctionGuide = {
52
- args: readonly SheetFormulaFunctionGuideArg[];
53
- descriptionI18nKey: string;
54
- name: SheetFormulaBuiltinFunctionName;
55
- signature: string;
56
- };
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';
57
130
 
58
131
  type SheetFormulaValueEntry = {
59
132
  fromRange: boolean;
60
133
  value: unknown;
61
134
  };
62
135
 
63
- export const SHEET_FORMULA_BUILTIN_FUNCTION_NAMES: readonly SheetFormulaBuiltinFunctionName[] =
64
- [
65
- 'ABS',
66
- 'AVERAGE',
67
- 'CEILING',
68
- 'COUNT',
69
- 'COUNTA',
70
- 'DATE',
71
- 'DAY',
72
- 'FLOOR',
73
- 'IF',
74
- 'IFERROR',
75
- 'IFNA',
76
- 'INCLUDES',
77
- 'MAX',
78
- 'MIN',
79
- 'MOD',
80
- 'MONTH',
81
- 'NOW',
82
- 'POWER',
83
- 'ROUND',
84
- 'ROUNDDOWN',
85
- 'ROUNDUP',
86
- 'REGEXMATCH',
87
- 'SUM',
88
- 'TODAY',
89
- 'YEAR',
90
- ];
91
-
92
- export const SHEET_FORMULA_FUNCTION_GUIDES: Partial<
93
- Record<SheetFormulaBuiltinFunctionName, SheetFormulaFunctionGuide>
94
- > = {
95
- IF: {
96
- args: [
97
- {
98
- descriptionI18nKey:
99
- 'sheet.formula_function_if_arg_condition_description',
100
- key: 'condition',
101
- },
102
- {
103
- descriptionI18nKey:
104
- 'sheet.formula_function_if_arg_value_if_true_description',
105
- key: 'value_if_true',
106
- },
107
- {
108
- descriptionI18nKey:
109
- 'sheet.formula_function_if_arg_value_if_false_description',
110
- key: 'value_if_false',
111
- },
112
- ],
113
- descriptionI18nKey: 'sheet.formula_function_if_description',
114
- name: 'IF',
115
- signature: 'IF(condition, value_if_true, value_if_false)',
116
- },
117
- IFERROR: {
118
- args: [
119
- {
120
- descriptionI18nKey:
121
- 'sheet.formula_function_iferror_arg_value_description',
122
- key: 'value',
123
- },
124
- {
125
- descriptionI18nKey:
126
- 'sheet.formula_function_iferror_arg_fallback_description',
127
- key: 'fallback',
128
- },
129
- ],
130
- descriptionI18nKey: 'sheet.formula_function_iferror_description',
131
- name: 'IFERROR',
132
- signature: 'IFERROR(value, fallback)',
133
- },
134
- IFNA: {
135
- args: [
136
- {
137
- descriptionI18nKey: 'sheet.formula_function_ifna_arg_value_description',
138
- key: 'value',
139
- },
140
- {
141
- descriptionI18nKey:
142
- 'sheet.formula_function_ifna_arg_fallback_description',
143
- key: 'fallback',
144
- },
145
- ],
146
- descriptionI18nKey: 'sheet.formula_function_ifna_description',
147
- name: 'IFNA',
148
- signature: 'IFNA(value, fallback)',
149
- },
150
- INCLUDES: {
151
- args: [
152
- {
153
- descriptionI18nKey:
154
- 'sheet.formula_function_includes_arg_needle_description',
155
- key: 'value',
156
- },
157
- {
158
- descriptionI18nKey:
159
- 'sheet.formula_function_includes_arg_range_description',
160
- key: 'range',
161
- },
162
- {
163
- descriptionI18nKey:
164
- 'sheet.formula_function_includes_arg_value_if_true_description',
165
- key: 'value_if_true',
166
- },
167
- {
168
- descriptionI18nKey:
169
- 'sheet.formula_function_includes_arg_value_if_false_description',
170
- key: 'value_if_false',
171
- },
172
- ],
173
- descriptionI18nKey: 'sheet.formula_function_includes_description',
174
- name: 'INCLUDES',
175
- signature: 'INCLUDES(value, range, [value_if_true, value_if_false])',
176
- },
177
- REGEXMATCH: {
178
- args: [
179
- {
180
- descriptionI18nKey:
181
- 'sheet.formula_function_regexmatch_arg_range_description',
182
- key: 'range',
183
- },
184
- {
185
- descriptionI18nKey:
186
- 'sheet.formula_function_regexmatch_arg_pattern_description',
187
- key: 'pattern',
188
- },
189
- ],
190
- descriptionI18nKey: 'sheet.formula_function_regexmatch_description',
191
- name: 'REGEXMATCH',
192
- signature: 'REGEXMATCH(range, pattern)',
193
- },
194
- };
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
+ ];
195
217
 
196
218
  const FILE_CONSTANTS = {
219
+ atPrefixedBuiltinNameSet: new Set<string>(SHEET_FORMULA_AT_PREFIX_BUILTIN_FUNCTION_NAMES),
197
220
  builtinFunctionNameSet: new Set<string>(SHEET_FORMULA_BUILTIN_FUNCTION_NAMES),
198
221
  conditionalFunctionNameSet: new Set<string>([
199
222
  'IF',
200
223
  'IFERROR',
201
224
  'IFNA',
225
+ 'IFS',
202
226
  'INCLUDES',
227
+ 'ISBLANK',
228
+ 'ISERROR',
229
+ 'ISNA',
230
+ 'ISNUMBER',
231
+ 'ISTEXT',
232
+ 'SWITCH',
233
+ 'XLOOKUP',
203
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,
204
240
  numericStringPattern: /^[-+]?\d+(?:\.\d+)?$/,
205
241
  };
206
242
 
207
243
  /*
208
- * Return a normalized builtin formula function name.
209
- */
210
- export function getSheetFormulaBuiltinFunctionName(
211
- name: string,
212
- ): SheetFormulaBuiltinFunctionName | null {
213
- const normalizedName = name.replace(/^@/, '').toUpperCase();
214
- return FILE_CONSTANTS.builtinFunctionNameSet.has(normalizedName)
215
- ? (normalizedName as SheetFormulaBuiltinFunctionName)
216
- : null;
217
- }
218
-
219
- /*
220
- * Return shared guide metadata for one formula function, when the UI has
221
- * explicit helper copy for that function.
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(...)'.
222
247
  */
223
- export function getSheetFormulaFunctionGuide(
224
- name: string,
225
- ): SheetFormulaFunctionGuide | null {
226
- const normalizedName = getSheetFormulaBuiltinFunctionName(name);
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
+ }
227
258
 
228
- return normalizedName
229
- ? SHEET_FORMULA_FUNCTION_GUIDES[normalizedName] || null
230
- : null;
259
+ return normalizedName as SheetFormulaBuiltinFunctionName;
231
260
  }
232
261
 
233
262
  /*
@@ -252,6 +281,27 @@ export function getSheetFormulaStringValue(value: unknown) {
252
281
  return value === null || value === undefined ? '' : String(value);
253
282
  }
254
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
+
255
305
  /*
256
306
  * Return a numeric value from a scalar formula value when it can be coerced.
257
307
  */
@@ -264,11 +314,7 @@ export function getSheetFormulaNumericValue(value: unknown) {
264
314
  return value ? 1 : 0;
265
315
  }
266
316
 
267
- if (
268
- typeof value === 'string' &&
269
- value.trim() &&
270
- FILE_CONSTANTS.numericStringPattern.test(value.trim())
271
- ) {
317
+ if (typeof value === 'string' && value.trim() && FILE_CONSTANTS.numericStringPattern.test(value.trim())) {
272
318
  return Number(value.trim());
273
319
  }
274
320
 
@@ -316,9 +362,7 @@ export function getSheetFormulaLogicalBooleanValue(value: unknown) {
316
362
  /*
317
363
  * Return whether one evaluated formula value is a range-like value.
318
364
  */
319
- export function isSheetFormulaRangeLikeValue(
320
- value: unknown,
321
- ): value is SheetFormulaRangeLikeValue {
365
+ export function isSheetFormulaRangeLikeValue(value: unknown): value is SheetFormulaRangeLikeValue {
322
366
  return Boolean(
323
367
  value &&
324
368
  typeof value === 'object' &&
@@ -353,55 +397,54 @@ export function formatSheetFormulaDateValue(dateTime: DateTime) {
353
397
  /*
354
398
  * Return the organization-zoned current value for a formula date/time shorthand.
355
399
  */
356
- export function getSheetFormulaDateTimeShorthandValue(
357
- name: SheetFormulaDateTimeShorthandName,
358
- now: Date,
359
- timeZone: string,
360
- ) {
400
+ export function getSheetFormulaDateTimeShorthandValue(name: SheetFormulaDateTimeShorthandName, now: Date, timeZone: string) {
361
401
  const dateTime = DateTime.fromJSDate(now, { zone: timeZone });
362
402
 
363
- return name === 'CURRENT_DATE'
364
- ? formatSheetFormulaDateValue(dateTime)
365
- : dateTime.toISO();
403
+ return name === 'CURRENT_DATE' ? formatSheetFormulaDateValue(dateTime) : dateTime.toISO();
366
404
  }
367
405
 
368
406
  /*
369
407
  * Return a stable freshness-key value for one date/time shorthand.
370
408
  */
371
- export function getSheetFormulaDateTimeShorthandKeyValue(
372
- name: SheetFormulaDateTimeShorthandName,
373
- now: Date,
374
- timeZone: string,
375
- ) {
409
+ export function getSheetFormulaDateTimeShorthandKeyValue(name: SheetFormulaDateTimeShorthandName, now: Date, timeZone: string) {
376
410
  const dateTime = DateTime.fromJSDate(now, { zone: timeZone });
377
411
 
378
- return name === 'CURRENT_DATE'
379
- ? formatSheetFormulaDateValue(dateTime)
380
- : dateTime.toFormat("yyyy-MM-dd'T'HH:mm");
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());
381
429
  }
382
430
 
383
431
  /*
384
432
  * Return a formula pure evaluation error result.
385
433
  */
386
- export function getSheetFormulaPureErrorResult(
387
- code: string,
388
- message: string,
389
- ): SheetFormulaPureEvaluationResult {
434
+ export function getSheetFormulaPureErrorResult(code: string, message: string): SheetFormulaPureEvaluationResult {
390
435
  return {
391
436
  error: {
392
437
  code,
393
438
  message,
394
439
  },
395
- value: code === 'NOT_FOUND' ? '#N/A' : '#ERROR!',
440
+ value: getSheetFormulaErrorCodeDisplayValue(code),
396
441
  };
397
442
  }
398
443
 
399
444
  /*
400
445
  * Return whether one formula error is the spreadsheet not-found error.
401
446
  */
402
- export function isSheetFormulaNotFoundError(
403
- error?: SheetFormulaPureEvaluationError | null,
404
- ) {
447
+ export function isSheetFormulaNotFoundError(error?: SheetFormulaPureEvaluationError | null) {
405
448
  return error?.code === 'NOT_FOUND';
406
449
  }
407
450
 
@@ -427,9 +470,53 @@ function flattenSheetFormulaValueEntries(values: unknown[]) {
427
470
  }
428
471
 
429
472
  /*
430
- * Return numeric entries for aggregate functions, ignoring range blanks and range text.
473
+ * Return a spreadsheet serial number for one MAX value that Date can parse.
431
474
  */
432
- function getSheetFormulaAggregateNumericValues(values: unknown[]) {
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') {
433
520
  const numbers: number[] = [];
434
521
  const entries = flattenSheetFormulaValueEntries(values);
435
522
  for (const entry of entries) {
@@ -437,7 +524,9 @@ function getSheetFormulaAggregateNumericValues(values: unknown[]) {
437
524
  continue;
438
525
  }
439
526
 
440
- const numberValue = getSheetFormulaNumericValue(entry.value);
527
+ const numberValue = convertMaxDates
528
+ ? getSheetFormulaMaxNumericValue(entry.value, timeZone)
529
+ : getSheetFormulaNumericValue(entry.value);
441
530
  if (numberValue !== null) {
442
531
  numbers.push(numberValue);
443
532
  continue;
@@ -455,41 +544,27 @@ function getSheetFormulaAggregateNumericValues(values: unknown[]) {
455
544
  * Return nonblank entries for COUNTA.
456
545
  */
457
546
  function getSheetFormulaCountAValue(values: unknown[]) {
458
- return flattenSheetFormulaValueEntries(values).filter(
459
- (entry) => !isSheetFormulaBlankValue(entry.value),
460
- ).length;
547
+ return flattenSheetFormulaValueEntries(values).filter((entry) => !isSheetFormulaBlankValue(entry.value)).length;
461
548
  }
462
549
 
463
550
  /*
464
551
  * Return the count of strict numeric values for COUNT.
465
552
  */
466
553
  function getSheetFormulaCountValue(values: unknown[]) {
467
- return flattenSheetFormulaValueEntries(values).filter(
468
- (entry) => getSheetFormulaStrictNumberValue(entry.value) !== null,
469
- ).length;
554
+ return flattenSheetFormulaValueEntries(values).filter((entry) => getSheetFormulaStrictNumberValue(entry.value) !== null).length;
470
555
  }
471
556
 
472
557
  /*
473
558
  * Return scalar numeric function arguments with range values flattened.
474
559
  */
475
- function getSheetFormulaNumericFunctionArgs(
476
- values: unknown[],
477
- minCount: number,
478
- maxCount: number,
479
- ) {
480
- const entries = flattenSheetFormulaValueEntries(values).filter(
481
- (entry) => !isSheetFormulaBlankValue(entry.value),
482
- );
560
+ function getSheetFormulaNumericFunctionArgs(values: unknown[], minCount: number, maxCount: number) {
561
+ const entries = flattenSheetFormulaValueEntries(values).filter((entry) => !isSheetFormulaBlankValue(entry.value));
483
562
  if (entries.length < minCount || entries.length > maxCount) {
484
563
  return null;
485
564
  }
486
565
 
487
- const numbers = entries.map((entry) =>
488
- getSheetFormulaNumericValue(entry.value),
489
- );
490
- return numbers.every((value) => value !== null)
491
- ? (numbers as number[])
492
- : null;
566
+ const numbers = entries.map((entry) => getSheetFormulaNumericValue(entry.value));
567
+ return numbers.every((value) => value !== null) ? (numbers as number[]) : null;
493
568
  }
494
569
 
495
570
  /*
@@ -498,6 +573,7 @@ function getSheetFormulaNumericFunctionArgs(
498
573
  export function evaluateSheetFormulaAggregateFunction(
499
574
  name: string,
500
575
  values: unknown[],
576
+ timeZone = 'UTC',
501
577
  ): SheetFormulaPureEvaluationResult | null {
502
578
  if (!['AVERAGE', 'COUNT', 'COUNTA', 'MAX', 'MIN', 'SUM'].includes(name)) {
503
579
  return null;
@@ -515,12 +591,9 @@ export function evaluateSheetFormulaAggregateFunction(
515
591
  };
516
592
  }
517
593
 
518
- const numbers = getSheetFormulaAggregateNumericValues(values);
594
+ const numbers = getSheetFormulaAggregateNumericValues(values, name === 'MAX', timeZone);
519
595
  if (!numbers) {
520
- return getSheetFormulaPureErrorResult(
521
- 'INVALID_FORMULA',
522
- 'Formula function requires numeric values.',
523
- );
596
+ return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.');
524
597
  }
525
598
 
526
599
  if (name === 'SUM') {
@@ -532,13 +605,9 @@ export function evaluateSheetFormulaAggregateFunction(
532
605
  if (name === 'AVERAGE') {
533
606
  return numbers.length
534
607
  ? {
535
- value:
536
- numbers.reduce((sum, value) => sum + value, 0) / numbers.length,
608
+ value: numbers.reduce((sum, value) => sum + value, 0) / numbers.length,
537
609
  }
538
- : getSheetFormulaPureErrorResult(
539
- 'DIVIDE_BY_ZERO',
540
- 'Formula average requires at least one numeric value.',
541
- );
610
+ : getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula average requires at least one numeric value.');
542
611
  }
543
612
 
544
613
  if (name === 'MIN') {
@@ -546,10 +615,7 @@ export function evaluateSheetFormulaAggregateFunction(
546
615
  ? {
547
616
  value: Math.min(...numbers),
548
617
  }
549
- : getSheetFormulaPureErrorResult(
550
- 'INVALID_FORMULA',
551
- 'Formula function requires numeric values.',
552
- );
618
+ : getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.');
553
619
  }
554
620
 
555
621
  if (name === 'MAX') {
@@ -557,103 +623,118 @@ export function evaluateSheetFormulaAggregateFunction(
557
623
  ? {
558
624
  value: Math.max(...numbers),
559
625
  }
560
- : getSheetFormulaPureErrorResult(
561
- 'INVALID_FORMULA',
562
- 'Formula function requires numeric values.',
563
- );
626
+ : getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.');
564
627
  }
565
628
 
566
629
  return null;
567
630
  }
568
631
 
569
632
  /*
570
- * Return a rounded number at a decimal precision.
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).
571
663
  */
572
664
  function roundSheetFormulaNumber(value: number, places = 0) {
573
- const factor = 10 ** Math.trunc(places);
574
- return Math.round((value + Number.EPSILON) * factor) / factor;
665
+ return roundSheetFormulaMagnitude(value, places, Math.round);
575
666
  }
576
667
 
577
668
  /*
578
669
  * Return a number rounded away from zero at a decimal precision.
579
670
  */
580
671
  function roundUpSheetFormulaNumber(value: number, places = 0) {
581
- const factor = 10 ** Math.trunc(places);
582
- return (Math.sign(value || 1) * Math.ceil(Math.abs(value) * factor)) / factor;
672
+ return roundSheetFormulaMagnitude(value, places, Math.ceil);
583
673
  }
584
674
 
585
675
  /*
586
- * Return a number rounded toward zero at a decimal precision.
676
+ * Return a number rounded toward zero at a decimal precision (ROUNDDOWN and
677
+ * TRUNC).
587
678
  */
588
- function roundDownSheetFormulaNumber(value: number, places = 0) {
589
- const factor = 10 ** Math.trunc(places);
590
- return (
591
- (Math.sign(value || 1) * Math.floor(Math.abs(value) * factor)) / factor
592
- );
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.');
593
698
  }
594
699
 
595
700
  /*
596
701
  * Evaluate a numeric formula function from already-evaluated argument values.
597
702
  */
598
- export function evaluateSheetFormulaMathFunction(
599
- name: string,
600
- values: unknown[],
601
- ): SheetFormulaPureEvaluationResult | null {
602
- const unaryArgs = ['ABS'].includes(name)
603
- ? getSheetFormulaNumericFunctionArgs(values, 1, 1)
604
- : null;
703
+ export function evaluateSheetFormulaMathFunction(name: string, values: unknown[]): SheetFormulaPureEvaluationResult | null {
704
+ const unaryArgs = ['ABS'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 1, 1) : null;
605
705
  if (unaryArgs) {
606
706
  return {
607
707
  value: Math.abs(unaryArgs[0]),
608
708
  };
609
709
  }
610
710
 
611
- const roundedArgs = ['ROUND', 'ROUNDUP', 'ROUNDDOWN'].includes(name)
612
- ? getSheetFormulaNumericFunctionArgs(values, 1, 2)
613
- : null;
711
+ const roundedArgs = ['ROUND', 'ROUNDUP', 'ROUNDDOWN'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 1, 2) : null;
614
712
  if (roundedArgs) {
615
713
  const places = roundedArgs[1] || 0;
616
714
  const roundFn =
617
- name === 'ROUNDUP'
618
- ? roundUpSheetFormulaNumber
619
- : name === 'ROUNDDOWN'
620
- ? roundDownSheetFormulaNumber
621
- : roundSheetFormulaNumber;
715
+ name === 'ROUNDUP' ? roundUpSheetFormulaNumber : name === 'ROUNDDOWN' ? roundDownSheetFormulaNumber : roundSheetFormulaNumber;
622
716
 
623
717
  return {
624
718
  value: roundFn(roundedArgs[0], places),
625
719
  };
626
720
  }
627
721
 
628
- const ceilingFloorArgs = ['CEILING', 'FLOOR'].includes(name)
629
- ? getSheetFormulaNumericFunctionArgs(values, 1, 2)
630
- : null;
722
+ const ceilingFloorArgs = ['CEILING', 'FLOOR'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 1, 2) : null;
631
723
  if (ceilingFloorArgs) {
632
724
  const significance = ceilingFloorArgs[1] || 1;
633
725
  if (significance === 0) {
634
- return getSheetFormulaPureErrorResult(
635
- 'DIVIDE_BY_ZERO',
636
- 'Formula significance cannot be zero.',
637
- );
726
+ return getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula significance cannot be zero.');
638
727
  }
639
728
 
640
729
  return {
641
- value:
642
- (name === 'CEILING' ? Math.ceil : Math.floor)(
643
- ceilingFloorArgs[0] / significance,
644
- ) * significance,
730
+ value: (name === 'CEILING' ? Math.ceil : Math.floor)(ceilingFloorArgs[0] / significance) * significance,
645
731
  };
646
732
  }
647
733
 
648
- const binaryArgs = ['MOD', 'POWER'].includes(name)
649
- ? getSheetFormulaNumericFunctionArgs(values, 2, 2)
650
- : null;
734
+ const binaryArgs = ['MOD', 'POWER'].includes(name) ? getSheetFormulaNumericFunctionArgs(values, 2, 2) : null;
651
735
  if (binaryArgs && name === 'MOD') {
652
736
  if (binaryArgs[1] === 0) {
653
- return getSheetFormulaPureErrorResult(
654
- 'DIVIDE_BY_ZERO',
655
- 'Formula cannot divide by zero.',
656
- );
737
+ return getSheetFormulaPureErrorResult('DIVIDE_BY_ZERO', 'Formula cannot divide by zero.');
657
738
  }
658
739
 
659
740
  return {
@@ -662,25 +743,11 @@ export function evaluateSheetFormulaMathFunction(
662
743
  }
663
744
 
664
745
  if (binaryArgs && name === 'POWER') {
665
- return {
666
- value: binaryArgs[0] ** binaryArgs[1],
667
- };
746
+ return getSheetFormulaPowerResult(binaryArgs[0], binaryArgs[1]);
668
747
  }
669
748
 
670
- return [
671
- 'ABS',
672
- 'CEILING',
673
- 'FLOOR',
674
- 'MOD',
675
- 'POWER',
676
- 'ROUND',
677
- 'ROUNDDOWN',
678
- 'ROUNDUP',
679
- ].includes(name)
680
- ? getSheetFormulaPureErrorResult(
681
- 'INVALID_FORMULA',
682
- 'Formula function requires numeric values.',
683
- )
749
+ return ['ABS', 'CEILING', 'FLOOR', 'MOD', 'POWER', 'ROUND', 'ROUNDDOWN', 'ROUNDUP'].includes(name)
750
+ ? getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula function requires numeric values.')
684
751
  : null;
685
752
  }
686
753
 
@@ -695,23 +762,15 @@ export function evaluateSheetFormulaDateFunction(
695
762
  ): SheetFormulaPureEvaluationResult | null {
696
763
  if (name === 'TODAY') {
697
764
  return values.length
698
- ? getSheetFormulaPureErrorResult(
699
- 'INVALID_FORMULA',
700
- 'TODAY does not accept arguments.',
701
- )
765
+ ? getSheetFormulaPureErrorResult('INVALID_FORMULA', 'TODAY does not accept arguments.')
702
766
  : {
703
- value: formatSheetFormulaDateValue(
704
- DateTime.fromJSDate(now, { zone: timeZone }),
705
- ),
767
+ value: formatSheetFormulaDateValue(DateTime.fromJSDate(now, { zone: timeZone })),
706
768
  };
707
769
  }
708
770
 
709
771
  if (name === 'NOW') {
710
772
  return values.length
711
- ? getSheetFormulaPureErrorResult(
712
- 'INVALID_FORMULA',
713
- 'NOW does not accept arguments.',
714
- )
773
+ ? getSheetFormulaPureErrorResult('INVALID_FORMULA', 'NOW does not accept arguments.')
715
774
  : {
716
775
  value: DateTime.fromJSDate(now, { zone: timeZone }).toISO(),
717
776
  };
@@ -736,34 +795,18 @@ export function evaluateSheetFormulaDateFunction(
736
795
  ? {
737
796
  value: formatSheetFormulaDateValue(dateTime),
738
797
  }
739
- : getSheetFormulaPureErrorResult(
740
- 'INVALID_FORMULA',
741
- 'DATE requires valid year, month, and day values.',
742
- );
798
+ : getSheetFormulaPureErrorResult('INVALID_FORMULA', 'DATE requires valid year, month, and day values.');
743
799
  }
744
800
 
745
801
  if (['YEAR', 'MONTH', 'DAY'].includes(name)) {
746
- const entries = flattenSheetFormulaValueEntries(values).filter(
747
- (entry) => !isSheetFormulaBlankValue(entry.value),
748
- );
749
- const dateTime =
750
- entries.length === 1
751
- ? getSheetFormulaDateTimeValue(entries[0].value, timeZone)
752
- : null;
802
+ const entries = flattenSheetFormulaValueEntries(values).filter((entry) => !isSheetFormulaBlankValue(entry.value));
803
+ const dateTime = entries.length === 1 ? getSheetFormulaDateTimeValue(entries[0].value, timeZone) : null;
753
804
  if (!dateTime?.isValid) {
754
- return getSheetFormulaPureErrorResult(
755
- 'INVALID_FORMULA',
756
- 'Formula date function requires one date value.',
757
- );
805
+ return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'Formula date function requires one date value.');
758
806
  }
759
807
 
760
808
  return {
761
- value:
762
- name === 'YEAR'
763
- ? dateTime.year
764
- : name === 'MONTH'
765
- ? dateTime.month
766
- : dateTime.day,
809
+ value: name === 'YEAR' ? dateTime.year : name === 'MONTH' ? dateTime.month : dateTime.day,
767
810
  };
768
811
  }
769
812
 
@@ -775,40 +818,27 @@ export function evaluateSheetFormulaDateFunction(
775
818
  * numeric compare when both coerce to numbers, otherwise string compare. Shared
776
819
  * so range membership matches the binary '=' operator in both evaluators.
777
820
  */
778
- export function sheetFormulaScalarValuesEqual(
779
- leftValue: unknown,
780
- rightValue: unknown,
781
- ) {
821
+ export function sheetFormulaScalarValuesEqual(leftValue: unknown, rightValue: unknown) {
782
822
  const leftNumber = getSheetFormulaNumericValue(leftValue);
783
823
  const rightNumber = getSheetFormulaNumericValue(rightValue);
784
824
  if (leftNumber !== null && rightNumber !== null) {
785
825
  return leftNumber === rightNumber;
786
826
  }
787
827
 
788
- return (
789
- getSheetFormulaStringValue(leftValue) ===
790
- getSheetFormulaStringValue(rightValue)
791
- );
828
+ return getSheetFormulaStringValue(leftValue) === getSheetFormulaStringValue(rightValue);
792
829
  }
793
830
 
794
831
  /*
795
832
  * Return whether a range value contains one scalar needle. Blank cells and a
796
833
  * blank needle never match. Range entries are already resolved scalars, so this
797
- * works over formula-fed and data-table region cells alike.
834
+ * works over literal and calculated cells alike.
798
835
  */
799
- export function sheetFormulaRangeIncludesValue(
800
- rangeValue: SheetFormulaRangeLikeValue,
801
- needle: unknown,
802
- ) {
836
+ export function sheetFormulaRangeIncludesValue(rangeValue: SheetFormulaRangeLikeValue, needle: unknown) {
803
837
  if (isSheetFormulaBlankValue(needle)) {
804
838
  return false;
805
839
  }
806
840
 
807
- return rangeValue.values.some(
808
- (value) =>
809
- !isSheetFormulaBlankValue(value) &&
810
- sheetFormulaScalarValuesEqual(value, needle),
811
- );
841
+ return rangeValue.values.some((value) => !isSheetFormulaBlankValue(value) && sheetFormulaScalarValuesEqual(value, needle));
812
842
  }
813
843
 
814
844
  /*
@@ -827,32 +857,18 @@ function getSheetFormulaRegexMatchRegExp(pattern: string): RegExp | null {
827
857
  * Return the first range value (row-major) whose string form matches the search
828
858
  * pattern, or a NOT_FOUND result when nothing matches (so IFNA can catch it).
829
859
  */
830
- function evaluateSheetFormulaRegexMatchFunction(
831
- values: unknown[],
832
- ): SheetFormulaPureEvaluationResult {
860
+ function evaluateSheetFormulaRegexMatchFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
833
861
  if (values.length !== 2) {
834
- return getSheetFormulaPureErrorResult(
835
- 'INVALID_FORMULA',
836
- 'REGEXMATCH requires a range and a search pattern.',
837
- );
862
+ return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'REGEXMATCH requires a range and a search pattern.');
838
863
  }
839
864
 
840
865
  const [rangeValue, patternValue] = values;
841
866
  if (!isSheetFormulaRangeLikeValue(rangeValue)) {
842
- return getSheetFormulaPureErrorResult(
843
- 'INVALID_FORMULA',
844
- 'REGEXMATCH requires a cell range as its first argument.',
845
- );
867
+ return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'REGEXMATCH requires a cell range as its first argument.');
846
868
  }
847
869
 
848
- if (
849
- isSheetFormulaRangeLikeValue(patternValue) ||
850
- isSheetFormulaBlankValue(patternValue)
851
- ) {
852
- return getSheetFormulaPureErrorResult(
853
- 'INVALID_FORMULA',
854
- 'REGEXMATCH requires a search pattern as its second argument.',
855
- );
870
+ if (isSheetFormulaRangeLikeValue(patternValue) || isSheetFormulaBlankValue(patternValue)) {
871
+ return getSheetFormulaPureErrorResult('INVALID_FORMULA', 'REGEXMATCH requires a search pattern as its second argument.');
856
872
  }
857
873
 
858
874
  const pattern = getSheetFormulaStringValue(patternValue);
@@ -864,9 +880,7 @@ function evaluateSheetFormulaRegexMatchFunction(
864
880
  }
865
881
 
866
882
  const text = getSheetFormulaStringValue(value);
867
- const matched = regExp
868
- ? regExp.test(text)
869
- : text.toLowerCase().includes(substringNeedle);
883
+ const matched = regExp ? regExp.test(text) : text.toLowerCase().includes(substringNeedle);
870
884
  if (matched) {
871
885
  return {
872
886
  value,
@@ -874,41 +888,12 @@ function evaluateSheetFormulaRegexMatchFunction(
874
888
  }
875
889
  }
876
890
 
877
- return getSheetFormulaPureErrorResult(
878
- 'NOT_FOUND',
879
- 'REGEXMATCH found no matching value.',
880
- );
891
+ return getSheetFormulaPureErrorResult('NOT_FOUND', 'REGEXMATCH found no matching value.');
881
892
  }
882
893
 
883
894
  /*
884
895
  * Evaluate a lookup formula function from already-evaluated argument values.
885
896
  */
886
- export function evaluateSheetFormulaLookupFunction(
887
- name: string,
888
- values: unknown[],
889
- ): SheetFormulaPureEvaluationResult | null {
890
- return name === 'REGEXMATCH'
891
- ? evaluateSheetFormulaRegexMatchFunction(values)
892
- : null;
893
- }
894
-
895
- /*
896
- * Evaluate one eager builtin formula function from already-evaluated argument values.
897
- */
898
- export function evaluateSheetFormulaEagerBuiltinFunction(
899
- name: string,
900
- values: unknown[],
901
- timeZone: string,
902
- now: Date,
903
- ) {
904
- return (
905
- evaluateSheetFormulaAggregateFunction(name, values) ||
906
- evaluateSheetFormulaLookupFunction(name, values) ||
907
- evaluateSheetFormulaDateFunction(name, values, timeZone, now) ||
908
- evaluateSheetFormulaMathFunction(name, values) ||
909
- getSheetFormulaPureErrorResult(
910
- 'UNKNOWN_FUNCTION',
911
- 'Formula function is not supported.',
912
- )
913
- );
897
+ export function evaluateSheetFormulaLookupFunction(name: string, values: unknown[]): SheetFormulaPureEvaluationResult | null {
898
+ return name === 'REGEXMATCH' ? evaluateSheetFormulaRegexMatchFunction(values) : null;
914
899
  }