@jsb188/mday 1.1.11 → 1.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +58 -23
- package/src/constants/agentMemory.ts +14 -45
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +5 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +156 -47
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +12 -38
- package/src/constants/dataTableFieldTypes.generated.ts +3 -7
- package/src/constants/docAccess.ts +3 -2
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +114 -12
- package/src/constants/fieldDoc.ts +74 -4
- package/src/constants/graphqlReactivity.ts +108 -25
- package/src/constants/log.ts +9 -0
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +1834 -730
- package/src/constants/organization.ts +13 -14
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +65 -101
- package/src/constants/sms.ts +18 -0
- package/src/constants/storage.ts +33 -9
- package/src/constants/usage.ts +3 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +106 -283
- package/src/schemas/inboundContact.ts +5 -5
- package/src/types/agentMemory.d.ts +38 -14
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1043 -193
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +102 -309
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +144 -55
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +579 -150
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +8 -0
- package/src/types/log.d.ts +94 -3
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +8 -3
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +180 -463
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +266 -27
- package/src/types/workflow.d.ts +196 -66
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +31 -14
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +642 -209
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +18 -556
- package/src/utils/dataTableFieldTypeRegistry.ts +1 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/email.ts +208 -1
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +81 -37
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +232 -141
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logStoreRead.ts +9 -13
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomyDisplay.ts +80 -124
- package/src/utils/noteDoc.ts +66 -284
- package/src/utils/organization.ts +117 -283
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +123 -4834
- package/src/utils/sheetAxisMove.ts +77 -291
- package/src/utils/sheetBootstrap.ts +771 -1138
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +23 -88
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +17 -69
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +388 -403
- package/src/utils/sheetFormulaEvaluator.ts +589 -444
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +140 -18
- package/src/utils/sheetNumberFormat.ts +68 -267
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +13 -43
- package/src/utils/sheetSnapshot.ts +176 -429
- package/src/utils/sheetStructureEdit.ts +537 -907
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/storage.ts +49 -3
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +199 -73
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +2 -1
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +37 -192
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +49 -40
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +93 -200
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +4 -23
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +492 -37
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/src/constants/agentDoc.ts +0 -12
- package/src/constants/logActivityGroupColors.generated.ts +0 -19
- package/src/constants/schedule.ts +0 -220
- package/src/constants/scheduleRuleTemplateSeed.ts +0 -138
- package/src/constants/scheduleRuleTemplates.generated.ts +0 -145
- package/src/constants/sheetRegionSources.generated.ts +0 -578
- package/src/schemas/fieldDocsQuery.ts +0 -12
- package/src/schemas/organization.ts +0 -68
- package/src/types/agentDoc.d.ts +0 -64
- package/src/types/noteDoc.d.ts +0 -86
- package/src/types/schedule.d.ts +0 -391
- package/src/utils/agentDoc.ts +0 -109
- package/src/utils/campaignFormEditor.ts +0 -871
- package/src/utils/dataTableRealtimeAuthority.ts +0 -209
- package/src/utils/dataTableRealtimeSelection.ts +0 -134
- package/src/utils/dataTableRowDocument.ts +0 -508
- package/src/utils/dataTableSource.ts +0 -230
- package/src/utils/schedule.ts +0 -799
- package/src/utils/scheduleEvaluation.ts +0 -1994
- package/src/utils/scheduleICS.ts +0 -246
- package/src/utils/scheduleRuleTemplates.ts +0 -188
- package/src/utils/sheetRegion.ts +0 -374
- package/src/utils/sheetRegionFilter.ts +0 -1259
- package/src/utils/sheetRegionSourceRegistry.ts +0 -594
- package/tests/schedule.test.ts +0 -693
- package/tests/scheduleEvaluation.test.ts +0 -1970
- package/tests/scheduleICS.test.ts +0 -208
- package/tests/scheduleRuleTemplates.test.ts +0 -176
- package/tests/scheduleSweepInvariants.test.ts +0 -428
- package/tests/sheetRegionFilter.test.ts +0 -1135
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { collectSheetFormulaStaticReferences } from '../src/utils/sheetFormulaCompile.ts';
|
|
3
|
+
import { parseSheetFormulaExpression } from '../src/utils/sheetFormulaParser.ts';
|
|
4
|
+
import { getSheetFormulaColumnLabel, type SheetFormulaASTNode } from '../src/utils/sheetFormulaSyntax.ts';
|
|
5
|
+
import {
|
|
6
|
+
evaluateSheetFormulaExpressionAsync,
|
|
7
|
+
evaluateSheetFormulaExpressionSync,
|
|
8
|
+
type SheetFormulaEvaluatorResult,
|
|
9
|
+
type SheetFormulaReferenceResolver,
|
|
10
|
+
} from '../src/utils/sheetFormulaEvaluator.ts';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Types
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
type TestCells = Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
/* Per-test context: literal cells keyed by A1, plus the A1 keys read so far
|
|
19
|
+
* (to prove lazy branches never evaluate). */
|
|
20
|
+
type TestContext = {
|
|
21
|
+
cells: TestCells;
|
|
22
|
+
reads: string[];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const TEST_NOW = new Date('2026-01-02T03:04:05.000Z');
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Return the A1 key of one parsed cell coordinate.
|
|
29
|
+
*/
|
|
30
|
+
function getTestA1(rowIndex: number, columnIndex: number) {
|
|
31
|
+
return `${getSheetFormulaColumnLabel(columnIndex)}${rowIndex}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* Dense in-memory resolver over A1-keyed literal cells. A cell holding an
|
|
35
|
+
* Error object resolves as that formula error (a referenced error cell). */
|
|
36
|
+
const TEST_RESOLVER: SheetFormulaReferenceResolver<TestContext> = {
|
|
37
|
+
resolveCellReference(context, node) {
|
|
38
|
+
const key = getTestA1(node.reference.rowIndex, node.reference.columnIndex);
|
|
39
|
+
context.reads.push(key);
|
|
40
|
+
const value = context.cells[key];
|
|
41
|
+
return value instanceof Error
|
|
42
|
+
? {
|
|
43
|
+
error: { code: value.message, message: value.message },
|
|
44
|
+
references: [],
|
|
45
|
+
value: '#ERROR!',
|
|
46
|
+
}
|
|
47
|
+
: { references: [], value: value ?? null };
|
|
48
|
+
},
|
|
49
|
+
resolveRangeReference(context, node) {
|
|
50
|
+
const values: unknown[] = [];
|
|
51
|
+
for (let row = node.reference.startRowIndex; row <= Math.min(node.reference.endRowIndex, 500); row++) {
|
|
52
|
+
for (let column = node.reference.startColumnIndex; column <= Math.min(node.reference.endColumnIndex, 256); column++) {
|
|
53
|
+
const value = context.cells[getTestA1(row, column)];
|
|
54
|
+
// Error cells inside ranges read as blank (policy B)
|
|
55
|
+
values.push(value instanceof Error ? null : (value ?? null));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { references: [], value: { kind: 'SHEET_RANGE_VALUE', values } };
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
/*
|
|
63
|
+
* Evaluate one formula against literal cells.
|
|
64
|
+
*/
|
|
65
|
+
function evaluate(formula: string, cells: TestCells = {}, context: TestContext = { cells, reads: [] }): SheetFormulaEvaluatorResult {
|
|
66
|
+
return evaluateSheetFormulaExpressionSync({
|
|
67
|
+
expression: formula.replace(/^=/, ''),
|
|
68
|
+
now: TEST_NOW,
|
|
69
|
+
resolver: TEST_RESOLVER,
|
|
70
|
+
resolverContext: context,
|
|
71
|
+
timeZone: 'UTC',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/*
|
|
76
|
+
* Assert one formula evaluates to a value without an error.
|
|
77
|
+
*/
|
|
78
|
+
function expectValue(formula: string, cells: TestCells, expected: unknown) {
|
|
79
|
+
const result = evaluate(formula, cells);
|
|
80
|
+
expect(result.error, `${formula}: ${result.error?.message}`).toBeFalsy();
|
|
81
|
+
expect(result.value, formula).toEqual(expected);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/*
|
|
85
|
+
* Assert one formula evaluates to an error code and its display token.
|
|
86
|
+
*/
|
|
87
|
+
function expectError(formula: string, cells: TestCells, code: string, display?: string) {
|
|
88
|
+
const result = evaluate(formula, cells);
|
|
89
|
+
expect(result.error?.code, formula).toBe(code);
|
|
90
|
+
if (display) {
|
|
91
|
+
expect(result.value, formula).toBe(display);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/*
|
|
96
|
+
* Return A1-keyed cells for one column of values starting at a row.
|
|
97
|
+
*/
|
|
98
|
+
function column(label: string, values: unknown[], startRow = 1): TestCells {
|
|
99
|
+
return Object.fromEntries(values.map((value, index) => [`${label}${startRow + index}`, value]));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
describe('AND / OR / XOR / NOT', () => {
|
|
103
|
+
it('combines scalars and ranges', () => {
|
|
104
|
+
expectValue('=AND(TRUE, 1, "true")', {}, true);
|
|
105
|
+
expectValue('=AND(TRUE, 0)', {}, false);
|
|
106
|
+
expectValue('=OR(FALSE, 0, A1)', { A1: 2 }, true);
|
|
107
|
+
expectValue('=XOR(TRUE, FALSE)', {}, true);
|
|
108
|
+
expectValue('=XOR(TRUE, TRUE)', {}, false);
|
|
109
|
+
expectValue('=AND(A1:A4)', column('A', [true, 'text', null, 5]), true);
|
|
110
|
+
expectValue('=OR(A1:A3)', column('A', ['yes', null, 0]), false);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('skips blank scalars but needs at least one logical value', () => {
|
|
114
|
+
expectValue('=AND(A1, TRUE)', {}, true);
|
|
115
|
+
expectError('=AND(A1)', {}, 'INVALID_VALUE', '#VALUE!');
|
|
116
|
+
expectError('=OR(A1:A3)', column('A', ['x', null, 'y']), 'INVALID_VALUE');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('rejects scalar text that is not TRUE/FALSE', () => {
|
|
120
|
+
expectError('=OR("maybe", TRUE)', {}, 'INVALID_VALUE', '#VALUE!');
|
|
121
|
+
expectError('=NOT("maybe")', {}, 'INVALID_VALUE');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('evaluates every argument (errors propagate, no short circuit)', () => {
|
|
125
|
+
expectError('=AND(FALSE, 1/0)', {}, 'DIVIDE_BY_ZERO', '#DIV/0!');
|
|
126
|
+
expectError('=OR(TRUE, 1/0)', {}, 'DIVIDE_BY_ZERO');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('checks arity', () => {
|
|
130
|
+
expectError('=AND()', {}, 'INVALID_FORMULA');
|
|
131
|
+
expectError('=NOT(TRUE, FALSE)', {}, 'INVALID_FORMULA');
|
|
132
|
+
expectError('=NOT(A1:A2)', {}, 'INVALID_VALUE');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('reverses one value; NOT of blank is TRUE', () => {
|
|
136
|
+
expectValue('=NOT(0)', {}, true);
|
|
137
|
+
expectValue('=NOT(A1)', {}, true);
|
|
138
|
+
expectValue('=NOT("TRUE")', {}, false);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('IFS / SWITCH', () => {
|
|
143
|
+
it('returns the first TRUE branch and never evaluates later branches', () => {
|
|
144
|
+
const context: TestContext = { cells: { A1: 5 }, reads: [] };
|
|
145
|
+
const result = evaluate('=IFS(A1 > 10, B1, A1 > 1, "mid", A1 > 0, C1, TRUE, 1/0)', {}, context);
|
|
146
|
+
expect(result.value).toBe('mid');
|
|
147
|
+
expect(context.reads).toEqual(['A1', 'A1']);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('reports misses, bad conditions and arity', () => {
|
|
151
|
+
expectError('=IFS(FALSE, 1)', {}, 'NOT_FOUND', '#N/A');
|
|
152
|
+
expectError('=IFS("maybe", 1)', {}, 'INVALID_VALUE');
|
|
153
|
+
expectError('=IFS(A1:A2, 1)', {}, 'INVALID_VALUE');
|
|
154
|
+
expectError('=IFS(TRUE)', {}, 'INVALID_FORMULA');
|
|
155
|
+
expectError('=IFS(TRUE, 1, FALSE)', {}, 'INVALID_FORMULA');
|
|
156
|
+
expectError('=IFS(1/0, 1, TRUE, 2)', {}, 'DIVIDE_BY_ZERO');
|
|
157
|
+
expectValue('=IFERROR(IFS(FALSE, 1), "none")', {}, 'none');
|
|
158
|
+
expectValue('=IFNA(IFS(FALSE, 1), "none")', {}, 'none');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('matches SWITCH cases case-insensitively and lazily', () => {
|
|
162
|
+
const context: TestContext = { cells: { A1: 'B' }, reads: [] };
|
|
163
|
+
const result = evaluate('=SWITCH(A1, "a", C1, "b", "bee", D1, E1, 1/0)', {}, context);
|
|
164
|
+
expect(result.value).toBe('bee');
|
|
165
|
+
expect(context.reads).toEqual(['A1']);
|
|
166
|
+
expectValue('=SWITCH(2, "2", "two", "other")', {}, 'two');
|
|
167
|
+
expectValue('=SWITCH(A1, "", "empty", "other")', {}, 'empty');
|
|
168
|
+
expectValue('=SWITCH(7, 1, "one", "other")', {}, 'other');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('reports SWITCH misses and errors', () => {
|
|
172
|
+
expectError('=SWITCH(7, 1, "one", 2, "two")', {}, 'NOT_FOUND', '#N/A');
|
|
173
|
+
expectError('=SWITCH(1/0, 1, "one")', {}, 'DIVIDE_BY_ZERO');
|
|
174
|
+
expectError('=SWITCH(1, 1/0, "one")', {}, 'DIVIDE_BY_ZERO');
|
|
175
|
+
expectError('=SWITCH(A1:A2, 1, "one")', {}, 'INVALID_VALUE');
|
|
176
|
+
expectError('=SWITCH(1, 1)', {}, 'INVALID_FORMULA');
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
describe('conditional aggregates', () => {
|
|
181
|
+
const sales = {
|
|
182
|
+
...column('A', ['East', 'west', 'east', null, 'EAST']),
|
|
183
|
+
...column('B', [10, 20, 'x', 40, true]),
|
|
184
|
+
...column('C', ['2026-01-01', '2026-01-05', '2026-01-09', null, 'soon']),
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
it('counts, sums and averages with one or many criteria', () => {
|
|
188
|
+
expectValue('=COUNTIF(A1:A5, "east")', sales, 3);
|
|
189
|
+
expectValue('=COUNTIF(A1:A5, "<>east")', sales, 2);
|
|
190
|
+
expectValue('=COUNTIF(B1:B5, ">15")', sales, 2);
|
|
191
|
+
expectValue('=SUMIF(A1:A5, "east", B1:B5)', sales, 11);
|
|
192
|
+
expectValue('=SUMIF(B1:B5, ">15")', sales, 60);
|
|
193
|
+
expectValue('=AVERAGEIF(A1:A5, "east", B1:B5)', sales, 5.5);
|
|
194
|
+
expectValue('=COUNTIFS(A1:A5, "east", C1:C5, ">=2026-01-02")', sales, 1);
|
|
195
|
+
expectValue('=SUMIFS(B1:B5, A1:A5, "e*", C1:C5, "<2026-01-05")', sales, 10);
|
|
196
|
+
expectValue('=AVERAGEIFS(B1:B5, A1:A5, "*", B1:B5, "<>20")', sales, 5.5);
|
|
197
|
+
expectValue('=COUNTIFS(A1:A5, "", B1:B5, 40)', sales, 1);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('builds criteria with & from cells and TODAY()', () => {
|
|
201
|
+
expectValue('=COUNTIF(B1:B5, ">="&D1)', { ...sales, D1: 20 }, 2);
|
|
202
|
+
expectValue('=COUNTIF(C1:C5, "<"&TODAY())', sales, 1);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it('rejects mismatched shapes (D12) and range criteria', () => {
|
|
206
|
+
expectError('=SUMIF(A1:A5, "east", B1:B4)', sales, 'INVALID_VALUE', '#VALUE!');
|
|
207
|
+
expectError('=COUNTIFS(A1:A5, "east", B1:C5, 1)', sales, 'INVALID_VALUE');
|
|
208
|
+
expectError('=SUMIFS(B1:B3, A1:A5, "east")', sales, 'INVALID_VALUE');
|
|
209
|
+
expectError('=COUNTIF(A1:A5, B1:B2)', sales, 'INVALID_VALUE');
|
|
210
|
+
expectError('=COUNTIF(A1, "east")', sales, 'INVALID_VALUE');
|
|
211
|
+
expectError('=SUMIF(A1:A5, "east", B1)', sales, 'INVALID_VALUE');
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it('checks arity and pairs', () => {
|
|
215
|
+
expectError('=COUNTIF(A1:A5)', sales, 'INVALID_FORMULA');
|
|
216
|
+
expectError('=COUNTIFS(A1:A5, "x", B1:B5)', sales, 'INVALID_FORMULA');
|
|
217
|
+
expectError('=SUMIFS(B1:B5, A1:A5)', sales, 'INVALID_FORMULA');
|
|
218
|
+
expectError('=AVERAGEIFS(B1:B5, A1:A5, "x", C1:C5)', sales, 'INVALID_FORMULA');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('divides by zero when an average has no numeric matches', () => {
|
|
222
|
+
expectError('=AVERAGEIF(A1:A5, "nobody", B1:B5)', sales, 'DIVIDE_BY_ZERO', '#DIV/0!');
|
|
223
|
+
expectError('=AVERAGEIFS(B1:B5, A1:A5, "west", B1:B5, ">100")', sales, 'DIVIDE_BY_ZERO');
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('reads error cells inside ranges as blank and propagates argument errors', () => {
|
|
227
|
+
const cells = { ...column('A', [1, new Error('DIVIDE_BY_ZERO'), 3]) };
|
|
228
|
+
expectValue('=SUMIF(A1:A3, "<>0")', cells, 4);
|
|
229
|
+
expectValue('=COUNTBLANK(A1:A3)', cells, 1);
|
|
230
|
+
expectValue('=COUNTIF(A1:A3, "")', cells, 1);
|
|
231
|
+
expectError('=COUNTIF(A1:A3, 1/0)', cells, 'DIVIDE_BY_ZERO');
|
|
232
|
+
expectError('=COUNTIF(A1:A3, A2)', cells, 'DIVIDE_BY_ZERO');
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('rejects wildcard criteria over 255 characters', () => {
|
|
236
|
+
expectError(`=COUNTIF(A1:A3, "${'a'.repeat(260)}*")`, {}, 'INVALID_VALUE');
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('counts blanks', () => {
|
|
240
|
+
expectValue('=COUNTBLANK(A1:B2)', { A1: 0, B2: '' }, 3);
|
|
241
|
+
expectError('=COUNTBLANK(1)', {}, 'INVALID_VALUE');
|
|
242
|
+
expectError('=COUNTBLANK(A1:A2, B1:B2)', {}, 'INVALID_FORMULA');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('multiplies and sums same-shape ranges', () => {
|
|
246
|
+
const cells = {
|
|
247
|
+
...column('A', [1, 2, 3]),
|
|
248
|
+
...column('B', [4, '5', 'x']),
|
|
249
|
+
};
|
|
250
|
+
expectValue('=SUMPRODUCT(A1:A3, B1:B3)', cells, 14);
|
|
251
|
+
expectValue('=SUMPRODUCT(A1:A3)', cells, 6);
|
|
252
|
+
expectValue('=SUMPRODUCT(4, 5)', {}, 20);
|
|
253
|
+
expectError('=SUMPRODUCT(A1:A3, B1:B2)', cells, 'INVALID_VALUE');
|
|
254
|
+
expectError('=SUMPRODUCT(A1:A3, 2)', cells, 'INVALID_VALUE');
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('scans a whole-sheet range quickly', () => {
|
|
258
|
+
const started = performance.now();
|
|
259
|
+
expectValue('=COUNTIF(A1:IV500, "*x*")', { B7: 'axe' }, 1);
|
|
260
|
+
expect(performance.now() - started).toBeLessThan(1_000);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe('VLOOKUP / HLOOKUP', () => {
|
|
265
|
+
const prices = {
|
|
266
|
+
...column('A', ['apple', 'Banana', 'cherry', 10, null, 'date']),
|
|
267
|
+
...column('B', [1.5, 2, null, 'ten', 'blank key', 4]),
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
it('finds exact matches case-insensitively with wildcards', () => {
|
|
271
|
+
expectValue('=VLOOKUP("banana", A1:B6, 2, FALSE)', prices, 2);
|
|
272
|
+
expectValue('=VLOOKUP("ch*", A1:B6, 2, FALSE)', prices, null);
|
|
273
|
+
expectValue('=VLOOKUP("d?te", A1:B6, 2, FALSE)', prices, 4);
|
|
274
|
+
expectValue('=VLOOKUP("10", A1:B6, 2, FALSE)', prices, 'ten');
|
|
275
|
+
expectValue('=VLOOKUP("apple", A1:B6, 1, FALSE)', prices, 'apple');
|
|
276
|
+
expectError('=VLOOKUP("kiwi", A1:B6, 2, FALSE)', prices, 'NOT_FOUND', '#N/A');
|
|
277
|
+
expectError('=VLOOKUP(A10, A1:B6, 2, FALSE)', prices, 'NOT_FOUND');
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('defaults to the approximate match on sorted keys', () => {
|
|
281
|
+
const tiers = {
|
|
282
|
+
...column('A', [0, 100, 500, 1000]),
|
|
283
|
+
...column('B', ['bronze', 'silver', 'gold', 'platinum']),
|
|
284
|
+
};
|
|
285
|
+
expectValue('=VLOOKUP(250, A1:B4, 2)', tiers, 'silver');
|
|
286
|
+
expectValue('=VLOOKUP(500, A1:B4, 2, TRUE)', tiers, 'gold');
|
|
287
|
+
expectValue('=VLOOKUP(5000, A1:B4, 2)', tiers, 'platinum');
|
|
288
|
+
expectError('=VLOOKUP(-1, A1:B4, 2)', tiers, 'NOT_FOUND');
|
|
289
|
+
// Unsorted: the last candidate before the first larger key
|
|
290
|
+
expectValue('=VLOOKUP(7, A1:B4, 2)', { ...column('A', [1, 5, 9, 3]), ...column('B', ['a', 'b', 'c', 'd']) }, 'b');
|
|
291
|
+
// Other types are skipped, so text keys do not end a numeric scan
|
|
292
|
+
expectValue('=VLOOKUP(7, A1:B3, 2)', { ...column('A', [1, 'x', 5]), ...column('B', ['a', 'b', 'c']) }, 'c');
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('validates the index', () => {
|
|
296
|
+
expectError('=VLOOKUP("apple", A1:B6, 3, FALSE)', prices, 'INVALID_REFERENCE', '#REF!');
|
|
297
|
+
expectError('=VLOOKUP("apple", A1:B6, 0, FALSE)', prices, 'INVALID_VALUE');
|
|
298
|
+
expectError('=VLOOKUP("apple", A1:B6, "x", FALSE)', prices, 'INVALID_VALUE');
|
|
299
|
+
expectError('=VLOOKUP("apple", A1:B6, 2, "maybe")', prices, 'INVALID_VALUE');
|
|
300
|
+
expectError('=VLOOKUP(A1:A2, A1:B6, 2, FALSE)', prices, 'INVALID_VALUE');
|
|
301
|
+
expectError('=VLOOKUP("apple", 5, 2, FALSE)', prices, 'INVALID_VALUE');
|
|
302
|
+
expectError('=VLOOKUP("apple", A1:B6)', prices, 'INVALID_FORMULA');
|
|
303
|
+
expectValue('=VLOOKUP("apple", A1:B6, 2.9, FALSE)', prices, 1.5);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('looks across the first row with HLOOKUP', () => {
|
|
307
|
+
const quarters = {
|
|
308
|
+
A1: 'Q1',
|
|
309
|
+
B1: 'Q2',
|
|
310
|
+
C1: 'Q3',
|
|
311
|
+
A2: 10,
|
|
312
|
+
B2: 20,
|
|
313
|
+
C2: 30,
|
|
314
|
+
};
|
|
315
|
+
expectValue('=HLOOKUP("q3", A1:C2, 2, FALSE)', quarters, 30);
|
|
316
|
+
expectValue('=HLOOKUP("Q2", A1:C2, 1, FALSE)', quarters, 'Q2');
|
|
317
|
+
expectError('=HLOOKUP("Q2", A1:C2, 3, FALSE)', quarters, 'INVALID_REFERENCE');
|
|
318
|
+
expectValue('=HLOOKUP(25, A2:C2, 1)', quarters, 20);
|
|
319
|
+
});
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
describe('INDEX / MATCH', () => {
|
|
323
|
+
const grid = {
|
|
324
|
+
A1: 1,
|
|
325
|
+
B1: 2,
|
|
326
|
+
C1: 3,
|
|
327
|
+
A2: 4,
|
|
328
|
+
B2: 5,
|
|
329
|
+
C2: null,
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
it('reads positions of 1-D and 2-D ranges', () => {
|
|
333
|
+
expectValue('=INDEX(A1:C2, 2, 2)', grid, 5);
|
|
334
|
+
expectValue('=INDEX(A1:C2, 2, 3)', grid, null);
|
|
335
|
+
expectValue('=INDEX(A1:C1, 2)', grid, 2);
|
|
336
|
+
expectValue('=INDEX(A1:A2, 2)', grid, 4);
|
|
337
|
+
expectValue('=INDEX(A1:A2, 2, 1)', grid, 4);
|
|
338
|
+
expectValue('=INDEX(A1:A1, 1)', grid, 1);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('reports out-of-range, zero and missing column positions', () => {
|
|
342
|
+
expectError('=INDEX(A1:C2, 3, 1)', grid, 'INVALID_REFERENCE', '#REF!');
|
|
343
|
+
expectError('=INDEX(A1:C2, 1, 4)', grid, 'INVALID_REFERENCE');
|
|
344
|
+
expectError('=INDEX(A1:C2, 0, 1)', grid, 'INVALID_VALUE');
|
|
345
|
+
expectError('=INDEX(A1:C2, -1, 1)', grid, 'INVALID_VALUE');
|
|
346
|
+
expectError('=INDEX(A1:C2, 1)', grid, 'INVALID_VALUE');
|
|
347
|
+
expectError('=INDEX(5, 1)', grid, 'INVALID_VALUE');
|
|
348
|
+
expectError('=INDEX(A1:C2, "x", 1)', grid, 'INVALID_VALUE');
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
it('finds 1-based positions with each match type', () => {
|
|
352
|
+
const names = column('A', ['apple', 'Banana', 'cherry']);
|
|
353
|
+
expectValue('=MATCH("banana", A1:A3, 0)', names, 2);
|
|
354
|
+
expectValue('=MATCH("*rry", A1:A3, 0)', names, 3);
|
|
355
|
+
expectValue('=MATCH(25, A1:A3)', column('A', [10, 20, 30]), 2);
|
|
356
|
+
expectValue('=MATCH(30, A1:A3, 1)', column('A', [10, 20, 30]), 3);
|
|
357
|
+
expectValue('=MATCH(15, A1:A3, -1)', column('A', [30, 20, 10]), 2);
|
|
358
|
+
expectValue('=MATCH(20, A1:C1, 0)', { A1: 10, B1: 20, C1: 30 }, 2);
|
|
359
|
+
expectError('=MATCH(5, A1:A3)', column('A', [10, 20, 30]), 'NOT_FOUND');
|
|
360
|
+
expectError('=MATCH("kiwi", A1:A3, 0)', names, 'NOT_FOUND', '#N/A');
|
|
361
|
+
expectError('=MATCH(1, A1:C2, 0)', grid, 'NOT_FOUND');
|
|
362
|
+
expectError('=MATCH(1, A1:A3, "x")', names, 'INVALID_VALUE');
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('combines INDEX and MATCH for a left lookup', () => {
|
|
366
|
+
expectValue('=INDEX(A1:A3, MATCH("y", B1:B3, 0))', { ...column('A', [1, 2, 3]), ...column('B', ['x', 'Y', 'z']) }, 2);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
describe('XLOOKUP', () => {
|
|
371
|
+
const cells = {
|
|
372
|
+
...column('A', ['a', 'B', 'c', 'b']),
|
|
373
|
+
...column('B', [1, 2, 3, 4]),
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
it('finds exact matches first to last or last to first', () => {
|
|
377
|
+
expectValue('=XLOOKUP("b", A1:A4, B1:B4)', cells, 2);
|
|
378
|
+
expectValue('=XLOOKUP("b", A1:A4, B1:B4, "none", 0, -1)', cells, 4);
|
|
379
|
+
expectValue('=XLOOKUP("b", A1:A4, B1:B4, "none", 0, 2)', cells, 2);
|
|
380
|
+
expectValue('=XLOOKUP("b", A1:A4, B1:B4, "none", 0, -2)', cells, 4);
|
|
381
|
+
expectValue('=XLOOKUP("b*", A1:A4, B1:B4, "none")', cells, 'none');
|
|
382
|
+
expectValue('=XLOOKUP("b*", A1:A4, B1:B4, "none", 2)', cells, 2);
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it('finds the next smaller or larger value without sorted data', () => {
|
|
386
|
+
const scores = {
|
|
387
|
+
...column('A', [30, 10, 20, 20]),
|
|
388
|
+
...column('B', ['c', 'a', 'b1', 'b2']),
|
|
389
|
+
};
|
|
390
|
+
expectValue('=XLOOKUP(25, A1:A4, B1:B4, "none", -1)', scores, 'b1');
|
|
391
|
+
expectValue('=XLOOKUP(25, A1:A4, B1:B4, "none", -1, -1)', scores, 'b2');
|
|
392
|
+
expectValue('=XLOOKUP(15, A1:A4, B1:B4, "none", 1)', scores, 'b1');
|
|
393
|
+
expectValue('=XLOOKUP(20, A1:A4, B1:B4, "none", 1)', scores, 'b1');
|
|
394
|
+
expectValue('=XLOOKUP(5, A1:A4, B1:B4, "none", -1)', scores, 'none');
|
|
395
|
+
expectValue('=XLOOKUP(99, A1:A4, B1:B4, "none", 1)', scores, 'none');
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it('evaluates if_not_found only on a miss', () => {
|
|
399
|
+
const context: TestContext = { cells, reads: [] };
|
|
400
|
+
expect(evaluate('=XLOOKUP("a", A1:A4, B1:B4, D1)', {}, context).value).toBe(1);
|
|
401
|
+
expect(context.reads).toEqual([]);
|
|
402
|
+
expectValue('=XLOOKUP("a", A1:A4, B1:B4, 1/0)', cells, 1);
|
|
403
|
+
expectError('=XLOOKUP("z", A1:A4, B1:B4, 1/0)', cells, 'DIVIDE_BY_ZERO');
|
|
404
|
+
expectError('=XLOOKUP("z", A1:A4, B1:B4)', cells, 'NOT_FOUND', '#N/A');
|
|
405
|
+
expectValue('=XLOOKUP("z", A1:A4, B1:B4, D1)', { ...cells, D1: 'fallback' }, 'fallback');
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it('validates ranges and modes', () => {
|
|
409
|
+
expectError('=XLOOKUP("a", A1:A4, B1:B3)', cells, 'INVALID_VALUE');
|
|
410
|
+
expectError('=XLOOKUP("a", A1:A4, B1:C4)', cells, 'INVALID_VALUE');
|
|
411
|
+
expectError('=XLOOKUP("a", A1:B4, A1:B4)', cells, 'INVALID_VALUE');
|
|
412
|
+
expectError('=XLOOKUP("a", A1:A4, B1:B4, "x", 3)', cells, 'INVALID_VALUE');
|
|
413
|
+
expectError('=XLOOKUP("a", A1:A4, B1:B4, "x", 0, 0)', cells, 'INVALID_VALUE');
|
|
414
|
+
expectError('=XLOOKUP("a", A1:A4)', cells, 'INVALID_FORMULA');
|
|
415
|
+
expectError('=XLOOKUP(1/0, A1:A4, B1:B4, "x")', cells, 'DIVIDE_BY_ZERO');
|
|
416
|
+
});
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
describe('UNIQUE', () => {
|
|
420
|
+
it('returns an intermediate array for range-reading functions', () => {
|
|
421
|
+
const cells = column('A', ['a', 'A', 'b', null, 'c', 'b']);
|
|
422
|
+
expectValue('=COUNTA(UNIQUE(A1:A6))', cells, 3);
|
|
423
|
+
expectValue('=INDEX(UNIQUE(A1:A6), 3)', cells, 'c');
|
|
424
|
+
expectValue('=COUNTA(UNIQUE(A1:A6, FALSE, TRUE))', cells, 1);
|
|
425
|
+
expectValue('=SUM(UNIQUE(A1:A4))', column('A', [1, '1', 2, 2]), 3);
|
|
426
|
+
expectValue('=INCLUDES("b", UNIQUE(A1:A6))', cells, true);
|
|
427
|
+
expectValue('=COUNTIF(UNIQUE(A1:A6), "b")', cells, 1);
|
|
428
|
+
expectValue('=COUNTA(UNIQUE(A1:A2))', {}, 0);
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('deduplicates rows and columns of 2-D ranges', () => {
|
|
432
|
+
const rows = {
|
|
433
|
+
A1: 'x',
|
|
434
|
+
B1: 1,
|
|
435
|
+
A2: 'X',
|
|
436
|
+
B2: '1',
|
|
437
|
+
A3: 'x',
|
|
438
|
+
B3: 2,
|
|
439
|
+
A4: null,
|
|
440
|
+
B4: null,
|
|
441
|
+
};
|
|
442
|
+
expectValue('=COUNTA(UNIQUE(A1:B4))', rows, 4);
|
|
443
|
+
expectValue('=INDEX(UNIQUE(A1:B4), 2, 2)', rows, 2);
|
|
444
|
+
const columns = { A1: 1, B1: 1, C1: 2, A2: 'a', B2: 'A', C2: 'b' };
|
|
445
|
+
expectValue('=COUNTA(UNIQUE(A1:C2, TRUE))', columns, 4);
|
|
446
|
+
expectValue('=INDEX(UNIQUE(A1:C2, TRUE), 2, 2)', columns, 'b');
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('cannot be the whole formula result', () => {
|
|
450
|
+
const result = evaluate('=UNIQUE(A1:A3)', column('A', [1, 2, 3]));
|
|
451
|
+
expect(result.error?.code).toBe('INVALID_FORMULA');
|
|
452
|
+
expect(result.error?.message).toContain('UNIQUE returns several values');
|
|
453
|
+
expectError('=UNIQUE(A1:A3)+1', {}, 'INVALID_FORMULA');
|
|
454
|
+
expectError('=UNIQUE(5)', {}, 'INVALID_VALUE');
|
|
455
|
+
expectError('=UNIQUE(A1:A3, "maybe")', {}, 'INVALID_VALUE');
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('stays linear on a whole-sheet range', () => {
|
|
459
|
+
const cells: TestCells = {};
|
|
460
|
+
for (let row = 1; row <= 500; row++) {
|
|
461
|
+
cells[`A${row}`] = `v${row % 250}`;
|
|
462
|
+
}
|
|
463
|
+
const started = performance.now();
|
|
464
|
+
expectValue('=COUNTA(UNIQUE(A1:IV500))', cells, 250);
|
|
465
|
+
expect(performance.now() - started).toBeLessThan(1_000);
|
|
466
|
+
});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
describe('new builtin names', () => {
|
|
470
|
+
it('reject unsupported @name spellings', () => {
|
|
471
|
+
expectError('=@vlookup("a", "b")', {}, 'UNKNOWN_FUNCTION');
|
|
472
|
+
expectError('=@index("a", "b")', {}, 'UNKNOWN_FUNCTION');
|
|
473
|
+
expectError('=@and("a", "b")', {}, 'UNKNOWN_FUNCTION');
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
it('register every range argument as a dependency, including lazy branches', () => {
|
|
477
|
+
const node = parseSheetFormulaExpression(
|
|
478
|
+
'SUMIFS(C2:C10, A2:A10, D1, sales!B2:B, ">"&E1) + XLOOKUP(F1, G1:G5, H1:H5, I1) + IFS(J1, K1:K2, TRUE, 0) + SWITCH(L1, M1, N1)',
|
|
479
|
+
) as SheetFormulaASTNode;
|
|
480
|
+
const references = collectSheetFormulaStaticReferences(node);
|
|
481
|
+
expect(
|
|
482
|
+
references.ranges.map(
|
|
483
|
+
(range) =>
|
|
484
|
+
`${range.sheetName || ''}!${range.startRowIndex}:${range.startColumnIndex}-${range.endRowIndex}:${range.endColumnIndex}`,
|
|
485
|
+
),
|
|
486
|
+
).toEqual(['!2:3-10:3', '!2:1-10:1', 'sales!2:2-500:2', '!1:7-5:7', '!1:8-5:8', '!1:11-2:11']);
|
|
487
|
+
expect(references.cells.map((cell) => getTestA1(cell.rowIndex, cell.columnIndex))).toEqual([
|
|
488
|
+
'D1',
|
|
489
|
+
'E1',
|
|
490
|
+
'F1',
|
|
491
|
+
'I1',
|
|
492
|
+
'J1',
|
|
493
|
+
'L1',
|
|
494
|
+
'M1',
|
|
495
|
+
'N1',
|
|
496
|
+
]);
|
|
497
|
+
expect(references.volatile).toBe(false);
|
|
498
|
+
expect(
|
|
499
|
+
collectSheetFormulaStaticReferences(parseSheetFormulaExpression('COUNTIF(A1:A5, "<"&TODAY())') as SheetFormulaASTNode).volatile,
|
|
500
|
+
).toBe(true);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('evaluate the lazy functions through an async resolver', async () => {
|
|
504
|
+
const asyncResolver: SheetFormulaReferenceResolver<TestContext> = {
|
|
505
|
+
...TEST_RESOLVER,
|
|
506
|
+
resolveCellReference: (context, node, runtime) => Promise.resolve(TEST_RESOLVER.resolveCellReference(context, node, runtime)),
|
|
507
|
+
resolveRangeReference: (context, node, runtime) => Promise.resolve(TEST_RESOLVER.resolveRangeReference(context, node, runtime)),
|
|
508
|
+
};
|
|
509
|
+
const cells = {
|
|
510
|
+
...column('A', ['x', 'y']),
|
|
511
|
+
...column('B', [1, 2]),
|
|
512
|
+
C1: 'y',
|
|
513
|
+
};
|
|
514
|
+
for (const [formula, expected] of [
|
|
515
|
+
['=XLOOKUP(C1, A1:A2, B1:B2, 1/0)', 2],
|
|
516
|
+
['=IFS(C1 = "x", 1/0, C1 = "y", B2)', 2],
|
|
517
|
+
['=SWITCH(C1, "x", 1/0, "y", B1)', 1],
|
|
518
|
+
['=SUMIFS(B1:B2, A1:A2, C1)', 2],
|
|
519
|
+
] as const) {
|
|
520
|
+
const result = await evaluateSheetFormulaExpressionAsync({
|
|
521
|
+
expression: formula.slice(1),
|
|
522
|
+
now: TEST_NOW,
|
|
523
|
+
resolver: asyncResolver,
|
|
524
|
+
resolverContext: { cells, reads: [] },
|
|
525
|
+
timeZone: 'UTC',
|
|
526
|
+
});
|
|
527
|
+
expect(result.error, formula).toBeFalsy();
|
|
528
|
+
expect(result.value, formula).toBe(expected);
|
|
529
|
+
}
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it('parse every new function name as a builtin call', () => {
|
|
533
|
+
for (const formula of ['AND(1)', 'COUNTIFS(A1:A2, 1)', 'XLOOKUP(1, A1:A2, B1:B2)']) {
|
|
534
|
+
const node = parseSheetFormulaExpression(formula);
|
|
535
|
+
expect(node?.kind).toBe('FUNCTION_CALL');
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
});
|