@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,488 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSheetFormulaLogicalBooleanValue,
|
|
3
|
+
getSheetFormulaNumericValue,
|
|
4
|
+
getSheetFormulaPureErrorResult,
|
|
5
|
+
isSheetFormulaBlankValue,
|
|
6
|
+
isSheetFormulaRangeLikeValue,
|
|
7
|
+
type SheetFormulaPureEvaluationResult,
|
|
8
|
+
} from './sheetFormulaEvaluation.ts';
|
|
9
|
+
import {
|
|
10
|
+
compareSheetFormulaLookupValues,
|
|
11
|
+
getSheetFormulaLookupMatcher,
|
|
12
|
+
getSheetFormulaLookupValueClass,
|
|
13
|
+
getSheetFormulaLookupValueKey,
|
|
14
|
+
type SheetFormulaLookupMatcher,
|
|
15
|
+
} from './sheetFormulaCriteria.ts';
|
|
16
|
+
import { buildSheetFormulaArrayValue, getSheetFormulaShapedRangeArg, type SheetFormulaShapedRange } from './sheetFormulaRange.ts';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Types
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/* Outcome of an XLOOKUP search: the matched result value, a miss (the caller
|
|
23
|
+
* then evaluates if_not_found lazily), or an argument error. */
|
|
24
|
+
export type SheetFormulaXLookupSearchResult =
|
|
25
|
+
| { found: true; ok: true; value: unknown }
|
|
26
|
+
| { found: false; ok: true }
|
|
27
|
+
| { ok: false; result: SheetFormulaPureEvaluationResult };
|
|
28
|
+
|
|
29
|
+
/* Direction of a sorted approximate scan: ASCENDING finds the largest value
|
|
30
|
+
* <= key, DESCENDING the smallest value >= key. */
|
|
31
|
+
type SheetFormulaSortedLookupDirection = 'ASCENDING' | 'DESCENDING';
|
|
32
|
+
|
|
33
|
+
type SheetFormulaIntegerArgResult = { ok: true; value: number } | { ok: false; result: SheetFormulaPureEvaluationResult };
|
|
34
|
+
|
|
35
|
+
const FILE_CONSTANTS = {
|
|
36
|
+
xLookupMatchModes: new Set([-1, 0, 1, 2]),
|
|
37
|
+
xLookupSearchModes: new Set([-2, -1, 1, 2]),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/*
|
|
41
|
+
* Return the INVALID_VALUE result for a scalar argument that received a range.
|
|
42
|
+
*/
|
|
43
|
+
function getSheetFormulaScalarArgResult(name: string, argLabel: string) {
|
|
44
|
+
return getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} requires a single value for ${argLabel}, not a range.`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* Return one whole-number argument (numbers truncate toward zero), or
|
|
49
|
+
* INVALID_VALUE when it is a range or not numeric.
|
|
50
|
+
*/
|
|
51
|
+
function getSheetFormulaIntegerArg(name: string, value: unknown, argLabel: string): SheetFormulaIntegerArgResult {
|
|
52
|
+
const numberValue =
|
|
53
|
+
isSheetFormulaRangeLikeValue(value) || isSheetFormulaBlankValue(value) ? null : getSheetFormulaNumericValue(value);
|
|
54
|
+
return numberValue === null
|
|
55
|
+
? {
|
|
56
|
+
ok: false,
|
|
57
|
+
result: getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} requires a number for ${argLabel}.`),
|
|
58
|
+
}
|
|
59
|
+
: { ok: true, value: Math.trunc(numberValue) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* Return one optional TRUE/FALSE argument (the default when omitted), or
|
|
64
|
+
* INVALID_VALUE when it cannot be read as a boolean.
|
|
65
|
+
*/
|
|
66
|
+
function getSheetFormulaFlagArg(
|
|
67
|
+
name: string,
|
|
68
|
+
values: unknown[],
|
|
69
|
+
index: number,
|
|
70
|
+
fallback: boolean,
|
|
71
|
+
): { ok: true; value: boolean } | { ok: false; result: SheetFormulaPureEvaluationResult } {
|
|
72
|
+
if (values.length <= index) {
|
|
73
|
+
return { ok: true, value: fallback };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const flag = isSheetFormulaRangeLikeValue(values[index]) ? null : getSheetFormulaLogicalBooleanValue(values[index]);
|
|
77
|
+
return flag === null
|
|
78
|
+
? {
|
|
79
|
+
ok: false,
|
|
80
|
+
result: getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} requires TRUE or FALSE for argument ${index + 1}.`),
|
|
81
|
+
}
|
|
82
|
+
: { ok: true, value: flag };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/*
|
|
86
|
+
* Return the NOT_FOUND (#N/A) result of a lookup miss.
|
|
87
|
+
*/
|
|
88
|
+
function getSheetFormulaLookupMissResult(name: string) {
|
|
89
|
+
return getSheetFormulaPureErrorResult('NOT_FOUND', `${name} did not find the search key.`);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/*
|
|
93
|
+
* Return the first position whose value satisfies an exact-match test, or -1.
|
|
94
|
+
*/
|
|
95
|
+
function findSheetFormulaExactLookupIndex(values: unknown[], matches: SheetFormulaLookupMatcher) {
|
|
96
|
+
return values.findIndex((value) => matches(value));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/*
|
|
100
|
+
* Return the approximate-match position in data sorted by the direction:
|
|
101
|
+
* a linear scan that skips blanks and other value types and stops at the
|
|
102
|
+
* first value past the key. On sorted data this is Excel's binary-search
|
|
103
|
+
* answer; on unsorted data it is the last candidate before the first value
|
|
104
|
+
* past the key, deterministically. -1 when nothing qualifies.
|
|
105
|
+
*/
|
|
106
|
+
export function findSheetFormulaSortedLookupIndex(values: unknown[], key: unknown, direction: SheetFormulaSortedLookupDirection) {
|
|
107
|
+
const keyClass = getSheetFormulaLookupValueClass(key);
|
|
108
|
+
if (keyClass === 'BLANK') {
|
|
109
|
+
return -1;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let candidate = -1;
|
|
113
|
+
for (let index = 0; index < values.length; index++) {
|
|
114
|
+
if (getSheetFormulaLookupValueClass(values[index]) !== keyClass) {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const comparison = compareSheetFormulaLookupValues(values[index], key);
|
|
119
|
+
if (direction === 'ASCENDING' ? comparison > 0 : comparison < 0) {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
candidate = index;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return candidate;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
* Return the lookup keys of a table: its first column for VLOOKUP, first row
|
|
131
|
+
* for HLOOKUP.
|
|
132
|
+
*/
|
|
133
|
+
function getSheetFormulaTableLookupKeys(table: SheetFormulaShapedRange, vertical: boolean) {
|
|
134
|
+
return vertical
|
|
135
|
+
? Array.from({ length: table.rowCount }, (_, row) => table.values[row * table.columnCount])
|
|
136
|
+
: table.values.slice(0, table.columnCount);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
* Evaluate VLOOKUP/HLOOKUP(key, range, index, [is_sorted=TRUE]). Sorted mode
|
|
141
|
+
* is an approximate match on the first column (row); exact mode compares
|
|
142
|
+
* case-insensitively and treats * ? ~ in a text key as wildcards.
|
|
143
|
+
*/
|
|
144
|
+
export function evaluateSheetFormulaTableLookupFunction(
|
|
145
|
+
name: 'HLOOKUP' | 'VLOOKUP',
|
|
146
|
+
values: unknown[],
|
|
147
|
+
): SheetFormulaPureEvaluationResult {
|
|
148
|
+
const [key, tableValue, indexValue] = values;
|
|
149
|
+
if (isSheetFormulaRangeLikeValue(key)) {
|
|
150
|
+
return getSheetFormulaScalarArgResult(name, 'the search key');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const tableArg = getSheetFormulaShapedRangeArg(name, tableValue, 'argument 2');
|
|
154
|
+
if (!tableArg.ok) {
|
|
155
|
+
return tableArg.result;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const indexArg = getSheetFormulaIntegerArg(name, indexValue, 'the index');
|
|
159
|
+
if (!indexArg.ok) {
|
|
160
|
+
return indexArg.result;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const sortedArg = getSheetFormulaFlagArg(name, values, 3, true);
|
|
164
|
+
if (!sortedArg.ok) {
|
|
165
|
+
return sortedArg.result;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const table = tableArg.range;
|
|
169
|
+
const vertical = name === 'VLOOKUP';
|
|
170
|
+
const span = vertical ? table.columnCount : table.rowCount;
|
|
171
|
+
if (indexArg.value < 1) {
|
|
172
|
+
return getSheetFormulaPureErrorResult('INVALID_VALUE', `${name} index must be 1 or greater.`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (indexArg.value > span) {
|
|
176
|
+
return getSheetFormulaPureErrorResult(
|
|
177
|
+
'INVALID_REFERENCE',
|
|
178
|
+
`${name} index ${indexArg.value} is beyond the ${span} ${vertical ? 'columns' : 'rows'} of the range.`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const keys = getSheetFormulaTableLookupKeys(table, vertical);
|
|
183
|
+
let position = -1;
|
|
184
|
+
if (sortedArg.value) {
|
|
185
|
+
position = findSheetFormulaSortedLookupIndex(keys, key, 'ASCENDING');
|
|
186
|
+
} else {
|
|
187
|
+
const matcher = getSheetFormulaLookupMatcher(key, true);
|
|
188
|
+
if (!matcher.ok) {
|
|
189
|
+
return matcher.result;
|
|
190
|
+
}
|
|
191
|
+
position = findSheetFormulaExactLookupIndex(keys, matcher.matches);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (position < 0) {
|
|
195
|
+
return getSheetFormulaLookupMissResult(name);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const offset = vertical ? position * table.columnCount + indexArg.value - 1 : (indexArg.value - 1) * table.columnCount + position;
|
|
199
|
+
return { value: table.values[offset] ?? null };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/*
|
|
203
|
+
* Evaluate MATCH(key, range, [type=1]): the 1-based position of the key in a
|
|
204
|
+
* one-row or one-column range. Type 0 is exact (case-insensitive, with
|
|
205
|
+
* wildcards), 1 (any positive) finds the largest value <= key in ascending
|
|
206
|
+
* data, -1 (any negative) the smallest value >= key in descending data.
|
|
207
|
+
*/
|
|
208
|
+
export function evaluateSheetFormulaMatchFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
|
|
209
|
+
const [key, rangeValue] = values;
|
|
210
|
+
if (isSheetFormulaRangeLikeValue(key)) {
|
|
211
|
+
return getSheetFormulaScalarArgResult('MATCH', 'the search key');
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const rangeArg = getSheetFormulaShapedRangeArg('MATCH', rangeValue, 'argument 2');
|
|
215
|
+
if (!rangeArg.ok) {
|
|
216
|
+
return rangeArg.result;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const typeArg =
|
|
220
|
+
values.length > 2 ? getSheetFormulaIntegerArg('MATCH', values[2], 'the match type') : ({ ok: true, value: 1 } as const);
|
|
221
|
+
if (!typeArg.ok) {
|
|
222
|
+
return typeArg.result;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const range = rangeArg.range;
|
|
226
|
+
if (range.rowCount !== 1 && range.columnCount !== 1) {
|
|
227
|
+
return getSheetFormulaPureErrorResult('NOT_FOUND', 'MATCH requires a one-row or one-column range.');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
let position = -1;
|
|
231
|
+
if (typeArg.value === 0) {
|
|
232
|
+
const matcher = getSheetFormulaLookupMatcher(key, true);
|
|
233
|
+
if (!matcher.ok) {
|
|
234
|
+
return matcher.result;
|
|
235
|
+
}
|
|
236
|
+
position = findSheetFormulaExactLookupIndex(range.values, matcher.matches);
|
|
237
|
+
} else {
|
|
238
|
+
position = findSheetFormulaSortedLookupIndex(range.values, key, typeArg.value > 0 ? 'ASCENDING' : 'DESCENDING');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return position < 0 ? getSheetFormulaLookupMissResult('MATCH') : { value: position + 1 };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/*
|
|
245
|
+
* Evaluate INDEX(range, row, [column]): the value at 1-based offsets. With
|
|
246
|
+
* one index, a one-row range reads a column and a one-column range a row.
|
|
247
|
+
* Row or column 0 (whole-row/column slices) is not supported (#VALUE!), and
|
|
248
|
+
* offsets outside the range are #REF!.
|
|
249
|
+
*/
|
|
250
|
+
export function evaluateSheetFormulaIndexFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
|
|
251
|
+
const rangeArg = getSheetFormulaShapedRangeArg('INDEX', values[0], 'argument 1');
|
|
252
|
+
if (!rangeArg.ok) {
|
|
253
|
+
return rangeArg.result;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const firstArg = getSheetFormulaIntegerArg('INDEX', values[1], 'the row');
|
|
257
|
+
if (!firstArg.ok) {
|
|
258
|
+
return firstArg.result;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const secondArg = values.length > 2 ? getSheetFormulaIntegerArg('INDEX', values[2], 'the column') : null;
|
|
262
|
+
if (secondArg && !secondArg.ok) {
|
|
263
|
+
return secondArg.result;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const range = rangeArg.range;
|
|
267
|
+
if (!secondArg && range.rowCount !== 1 && range.columnCount !== 1) {
|
|
268
|
+
return getSheetFormulaPureErrorResult('INVALID_VALUE', 'INDEX needs a column number for a range with several rows and columns.');
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const row = secondArg ? firstArg.value : range.rowCount === 1 ? 1 : firstArg.value;
|
|
272
|
+
const column = secondArg ? secondArg.value : range.rowCount === 1 ? firstArg.value : 1;
|
|
273
|
+
if (row < 1 || column < 1) {
|
|
274
|
+
return getSheetFormulaPureErrorResult('INVALID_VALUE', 'INDEX row and column must be 1 or greater.');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (row > range.rowCount || column > range.columnCount) {
|
|
278
|
+
return getSheetFormulaPureErrorResult(
|
|
279
|
+
'INVALID_REFERENCE',
|
|
280
|
+
`INDEX position is outside the ${range.rowCount} x ${range.columnCount} range.`,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return {
|
|
285
|
+
value: range.values[(row - 1) * range.columnCount + column - 1] ?? null,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/*
|
|
290
|
+
* Return the XLOOKUP approximate position: the best value of the key's type
|
|
291
|
+
* that is <= key (mode -1) or >= key (mode 1), an exact match being the best;
|
|
292
|
+
* ties go to the first position in search order. Data need not be sorted.
|
|
293
|
+
*/
|
|
294
|
+
function findSheetFormulaXLookupApproximateIndex(values: unknown[], key: unknown, matchMode: -1 | 1, order: number[]) {
|
|
295
|
+
const keyClass = getSheetFormulaLookupValueClass(key);
|
|
296
|
+
if (keyClass === 'BLANK') {
|
|
297
|
+
return -1;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
let best = -1;
|
|
301
|
+
for (const index of order) {
|
|
302
|
+
const value = values[index];
|
|
303
|
+
if (getSheetFormulaLookupValueClass(value) !== keyClass) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const comparison = compareSheetFormulaLookupValues(value, key);
|
|
308
|
+
if (matchMode === -1 ? comparison > 0 : comparison < 0) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (best < 0) {
|
|
313
|
+
best = index;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Closer to the key wins: larger for mode -1, smaller for mode 1
|
|
318
|
+
const versusBest = compareSheetFormulaLookupValues(value, values[best]);
|
|
319
|
+
if (matchMode === -1 ? versusBest > 0 : versusBest < 0) {
|
|
320
|
+
best = index;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return best;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/*
|
|
328
|
+
* Search for XLOOKUP(key, lookup_range, result_range, [if_not_found],
|
|
329
|
+
* [match_mode=0], [search_mode=1]) over already-evaluated arguments (without
|
|
330
|
+
* if_not_found, which the lazy caller evaluates only on a miss). Both ranges
|
|
331
|
+
* must be one-row or one-column ranges of the same shape. Match modes: 0
|
|
332
|
+
* exact, -1 exact or next smaller, 1 exact or next larger, 2 wildcard. Search
|
|
333
|
+
* modes 1 (first to last) and -1 (last to first); the binary modes 2 and -2
|
|
334
|
+
* scan linearly in the same directions.
|
|
335
|
+
*/
|
|
336
|
+
export function searchSheetFormulaXLookup(params: {
|
|
337
|
+
key: unknown;
|
|
338
|
+
lookupRange: unknown;
|
|
339
|
+
matchMode?: unknown;
|
|
340
|
+
resultRange: unknown;
|
|
341
|
+
searchMode?: unknown;
|
|
342
|
+
}): SheetFormulaXLookupSearchResult {
|
|
343
|
+
if (isSheetFormulaRangeLikeValue(params.key)) {
|
|
344
|
+
return {
|
|
345
|
+
ok: false,
|
|
346
|
+
result: getSheetFormulaScalarArgResult('XLOOKUP', 'the search key'),
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const lookupArg = getSheetFormulaShapedRangeArg('XLOOKUP', params.lookupRange, 'argument 2');
|
|
351
|
+
if (!lookupArg.ok) {
|
|
352
|
+
return lookupArg;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const resultArg = getSheetFormulaShapedRangeArg('XLOOKUP', params.resultRange, 'argument 3');
|
|
356
|
+
if (!resultArg.ok) {
|
|
357
|
+
return resultArg;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const lookup = lookupArg.range;
|
|
361
|
+
const result = resultArg.range;
|
|
362
|
+
if (
|
|
363
|
+
(lookup.rowCount !== 1 && lookup.columnCount !== 1) ||
|
|
364
|
+
lookup.rowCount !== result.rowCount ||
|
|
365
|
+
lookup.columnCount !== result.columnCount
|
|
366
|
+
) {
|
|
367
|
+
return {
|
|
368
|
+
ok: false,
|
|
369
|
+
result: getSheetFormulaPureErrorResult(
|
|
370
|
+
'INVALID_VALUE',
|
|
371
|
+
'XLOOKUP needs a one-row or one-column lookup range and a result range of the same size.',
|
|
372
|
+
),
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const matchModeArg =
|
|
377
|
+
params.matchMode === undefined
|
|
378
|
+
? ({ ok: true, value: 0 } as const)
|
|
379
|
+
: getSheetFormulaIntegerArg('XLOOKUP', params.matchMode, 'the match mode');
|
|
380
|
+
const searchModeArg =
|
|
381
|
+
params.searchMode === undefined
|
|
382
|
+
? ({ ok: true, value: 1 } as const)
|
|
383
|
+
: getSheetFormulaIntegerArg('XLOOKUP', params.searchMode, 'the search mode');
|
|
384
|
+
if (!matchModeArg.ok) {
|
|
385
|
+
return matchModeArg;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
if (!searchModeArg.ok) {
|
|
389
|
+
return searchModeArg;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (!FILE_CONSTANTS.xLookupMatchModes.has(matchModeArg.value) || !FILE_CONSTANTS.xLookupSearchModes.has(searchModeArg.value)) {
|
|
393
|
+
return {
|
|
394
|
+
ok: false,
|
|
395
|
+
result: getSheetFormulaPureErrorResult(
|
|
396
|
+
'INVALID_VALUE',
|
|
397
|
+
'XLOOKUP match mode must be 0, -1, 1 or 2 and search mode 1, -1, 2 or -2.',
|
|
398
|
+
),
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const order = Array.from(lookup.values.keys());
|
|
403
|
+
if (searchModeArg.value < 0) {
|
|
404
|
+
order.reverse();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
let position = -1;
|
|
408
|
+
if (matchModeArg.value === -1 || matchModeArg.value === 1) {
|
|
409
|
+
position = findSheetFormulaXLookupApproximateIndex(lookup.values, params.key, matchModeArg.value, order);
|
|
410
|
+
} else {
|
|
411
|
+
const matcher = getSheetFormulaLookupMatcher(params.key, matchModeArg.value === 2);
|
|
412
|
+
if (!matcher.ok) {
|
|
413
|
+
return matcher;
|
|
414
|
+
}
|
|
415
|
+
position = order.find((index) => matcher.matches(lookup.values[index])) ?? -1;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return position < 0 ? { found: false, ok: true } : { found: true, ok: true, value: result.values[position] ?? null };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/*
|
|
422
|
+
* Return the lines (rows, or columns when byColumn) of a range as value lists.
|
|
423
|
+
*/
|
|
424
|
+
function getSheetFormulaRangeLines(range: SheetFormulaShapedRange, byColumn: boolean) {
|
|
425
|
+
const lineCount = byColumn ? range.columnCount : range.rowCount;
|
|
426
|
+
const lineLength = byColumn ? range.rowCount : range.columnCount;
|
|
427
|
+
return Array.from({ length: lineCount }, (_, line) =>
|
|
428
|
+
Array.from(
|
|
429
|
+
{ length: lineLength },
|
|
430
|
+
(_, offset) => range.values[byColumn ? offset * range.columnCount + line : line * range.columnCount + offset],
|
|
431
|
+
),
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/*
|
|
436
|
+
* Evaluate UNIQUE(range, [by_column=FALSE], [exactly_once=FALSE]) into an
|
|
437
|
+
* intermediate array (no spilling): distinct rows (or columns) in first-seen
|
|
438
|
+
* order under lookup equality (case-insensitive, 1 equals "1"). All-blank
|
|
439
|
+
* rows are ignored. Hash keys keep it linear for large ranges.
|
|
440
|
+
*/
|
|
441
|
+
export function evaluateSheetFormulaUniqueFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
|
|
442
|
+
const rangeArg = getSheetFormulaShapedRangeArg('UNIQUE', values[0], 'argument 1');
|
|
443
|
+
if (!rangeArg.ok) {
|
|
444
|
+
return rangeArg.result;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
const byColumnArg = getSheetFormulaFlagArg('UNIQUE', values, 1, false);
|
|
448
|
+
if (!byColumnArg.ok) {
|
|
449
|
+
return byColumnArg.result;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const exactlyOnceArg = getSheetFormulaFlagArg('UNIQUE', values, 2, false);
|
|
453
|
+
if (!exactlyOnceArg.ok) {
|
|
454
|
+
return exactlyOnceArg.result;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
const range = rangeArg.range;
|
|
458
|
+
const lines = getSheetFormulaRangeLines(range, byColumnArg.value).filter((line) => !line.every(isSheetFormulaBlankValue));
|
|
459
|
+
const counts = new Map<string, { count: number; line: unknown[] }>();
|
|
460
|
+
for (const line of lines) {
|
|
461
|
+
const key = line.length === 1 ? getSheetFormulaLookupValueKey(line[0]) : JSON.stringify(line.map(getSheetFormulaLookupValueKey));
|
|
462
|
+
const entry = counts.get(key);
|
|
463
|
+
if (entry) {
|
|
464
|
+
entry.count++;
|
|
465
|
+
} else {
|
|
466
|
+
counts.set(key, { count: 1, line });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const kept = [...counts.values()].filter((entry) => !exactlyOnceArg.value || entry.count === 1).map((entry) => entry.line);
|
|
471
|
+
if (!byColumnArg.value) {
|
|
472
|
+
return {
|
|
473
|
+
value: buildSheetFormulaArrayValue(kept.flat(), kept.length, range.columnCount),
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// Columns were kept as lines; lay them back out row-major
|
|
478
|
+
const arrayValues: unknown[] = [];
|
|
479
|
+
for (let row = 0; row < range.rowCount; row++) {
|
|
480
|
+
for (const column of kept) {
|
|
481
|
+
arrayValues.push(column[row]);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return {
|
|
486
|
+
value: buildSheetFormulaArrayValue(arrayValues, range.rowCount, kept.length),
|
|
487
|
+
};
|
|
488
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { getSheetFormulaNumberArg } from './sheetFormulaArgs.ts';
|
|
2
|
+
import {
|
|
3
|
+
getSheetFormulaPureErrorResult,
|
|
4
|
+
roundDownSheetFormulaNumber,
|
|
5
|
+
type SheetFormulaPureEvaluationResult,
|
|
6
|
+
} from './sheetFormulaEvaluation.ts';
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* Evaluate INT, SIGN or SQRT over one number (blank is 0): INT rounds down
|
|
10
|
+
* (INT(-2.5) is -3), SIGN is -1, 0 or 1, and SQRT of a negative number is
|
|
11
|
+
* INVALID_NUMBER (#NUM!).
|
|
12
|
+
*/
|
|
13
|
+
export function evaluateSheetFormulaUnaryMathFunction(
|
|
14
|
+
name: 'INT' | 'SIGN' | 'SQRT',
|
|
15
|
+
values: unknown[],
|
|
16
|
+
): SheetFormulaPureEvaluationResult {
|
|
17
|
+
const number = getSheetFormulaNumberArg(name, values[0], 'value');
|
|
18
|
+
if (!number.ok) {
|
|
19
|
+
return number.result;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (name === 'INT') {
|
|
23
|
+
return { value: Math.floor(number.value) };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (name === 'SIGN') {
|
|
27
|
+
return { value: number.value > 0 ? 1 : number.value < 0 ? -1 : 0 };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return number.value < 0
|
|
31
|
+
? getSheetFormulaPureErrorResult('INVALID_NUMBER', 'SQRT requires a number that is not negative.')
|
|
32
|
+
: { value: Math.sqrt(number.value) };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/*
|
|
36
|
+
* Evaluate TRUNC(value, [places=0]): cuts digits after the decimal places
|
|
37
|
+
* toward zero (negative places zero out tens, hundreds, ...), the same as
|
|
38
|
+
* ROUNDDOWN.
|
|
39
|
+
*/
|
|
40
|
+
export function evaluateSheetFormulaTruncFunction(values: unknown[]): SheetFormulaPureEvaluationResult {
|
|
41
|
+
const number = getSheetFormulaNumberArg('TRUNC', values[0], 'value');
|
|
42
|
+
if (!number.ok) {
|
|
43
|
+
return number.result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const places = values.length > 1 ? getSheetFormulaNumberArg('TRUNC', values[1], 'places') : { ok: true as const, value: 0 };
|
|
47
|
+
return places.ok ? { value: roundDownSheetFormulaNumber(number.value, places.value) } : places.result;
|
|
48
|
+
}
|