@jsb188/mday 1.1.2
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 +142 -0
- package/src/constants/agentMemory.ts +166 -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 +22 -0
- 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 +146 -0
- package/src/constants/fieldDoc.ts +104 -0
- package/src/constants/graphqlReactivity.ts +196 -0
- package/src/constants/inboundContact.ts +8 -0
- package/src/constants/integration.ts +13 -0
- package/src/constants/log.ts +21 -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 +2199 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +60 -0
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +132 -0
- package/src/constants/sms.ts +18 -0
- package/src/constants/square.ts +16 -0
- package/src/constants/storage.ts +85 -0
- package/src/constants/usage.ts +82 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +879 -0
- package/src/schemas/inboundContact.ts +77 -0
- package/src/schemas/logsQuery.ts +93 -0
- package/src/types/account.d.ts +81 -0
- package/src/types/agentMemory.d.ts +100 -0
- package/src/types/agentNote.d.ts +441 -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 +159 -0
- 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 +231 -0
- 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 +65 -0
- package/src/types/integration.d.ts +42 -0
- package/src/types/log.d.ts +338 -0
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +350 -0
- 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 +847 -0
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +481 -0
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +435 -0
- 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 +1173 -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 +311 -0
- 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 +379 -0
- 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 +357 -0
- 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 +669 -0
- 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 +262 -0
- 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 +32 -0
- package/src/utils/storage.ts +198 -0
- package/src/utils/table-route.ts +262 -0
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +506 -0
- 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 +12 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,1027 @@
|
|
|
1
|
+
export type SheetFormulaOperatorTerm<Operator extends string> = {
|
|
2
|
+
expression: string;
|
|
3
|
+
operator: Operator;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type SheetFormulaCellReference = {
|
|
7
|
+
columnAbsolute?: boolean;
|
|
8
|
+
columnIndex: number;
|
|
9
|
+
columnLabel: string;
|
|
10
|
+
rowAbsolute?: boolean;
|
|
11
|
+
rowIndex: number;
|
|
12
|
+
sheetName?: string | null;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type SheetFormulaRangeReference = {
|
|
16
|
+
endColumnIndex: number;
|
|
17
|
+
endColumnLabel: string;
|
|
18
|
+
endRowOpen?: boolean;
|
|
19
|
+
endRowIndex: number;
|
|
20
|
+
sheetName?: string | null;
|
|
21
|
+
startColumnIndex: number;
|
|
22
|
+
startColumnLabel: string;
|
|
23
|
+
startRowIndex: number;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type SheetFormulaCall = {
|
|
27
|
+
args: string[];
|
|
28
|
+
name: string;
|
|
29
|
+
text: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type SheetFormulaDateTimeShorthandName = 'CURRENT_DATE' | 'CURRENT_DATETIME';
|
|
33
|
+
|
|
34
|
+
export type SheetFormulaASTNode =
|
|
35
|
+
| {
|
|
36
|
+
kind: 'BOOLEAN_LITERAL';
|
|
37
|
+
text: string;
|
|
38
|
+
value: boolean;
|
|
39
|
+
}
|
|
40
|
+
| {
|
|
41
|
+
kind: 'CELL_REFERENCE';
|
|
42
|
+
reference: SheetFormulaCellReference;
|
|
43
|
+
text: string;
|
|
44
|
+
}
|
|
45
|
+
| {
|
|
46
|
+
args: SheetFormulaASTNode[];
|
|
47
|
+
kind: 'FUNCTION_CALL';
|
|
48
|
+
name: string;
|
|
49
|
+
text: string;
|
|
50
|
+
}
|
|
51
|
+
| {
|
|
52
|
+
kind: 'DATE_TIME_SHORTHAND';
|
|
53
|
+
name: SheetFormulaDateTimeShorthandName;
|
|
54
|
+
text: string;
|
|
55
|
+
}
|
|
56
|
+
| {
|
|
57
|
+
/* Spreadsheet error literal such as #REF! (a reference that a row or
|
|
58
|
+
* column delete invalidated). Evaluates to the error `code`, which
|
|
59
|
+
* IFERROR catches. */
|
|
60
|
+
code: string;
|
|
61
|
+
kind: 'ERROR_LITERAL';
|
|
62
|
+
text: string;
|
|
63
|
+
}
|
|
64
|
+
| {
|
|
65
|
+
kind: 'NUMBER_LITERAL';
|
|
66
|
+
text: string;
|
|
67
|
+
value: number;
|
|
68
|
+
}
|
|
69
|
+
| {
|
|
70
|
+
kind: 'RANGE_REFERENCE';
|
|
71
|
+
reference: SheetFormulaRangeReference;
|
|
72
|
+
text: string;
|
|
73
|
+
}
|
|
74
|
+
| {
|
|
75
|
+
kind: 'STRING_LITERAL';
|
|
76
|
+
text: string;
|
|
77
|
+
value: string;
|
|
78
|
+
}
|
|
79
|
+
| {
|
|
80
|
+
kind: 'BINARY_EXPRESSION';
|
|
81
|
+
left: SheetFormulaASTNode;
|
|
82
|
+
operator: SheetFormulaBinaryOperator;
|
|
83
|
+
right: SheetFormulaASTNode;
|
|
84
|
+
text: string;
|
|
85
|
+
}
|
|
86
|
+
| {
|
|
87
|
+
kind: 'UNARY_EXPRESSION';
|
|
88
|
+
operator: '+' | '-';
|
|
89
|
+
text: string;
|
|
90
|
+
value: SheetFormulaASTNode;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export type SheetFormulaBinaryOperator = '!=' | '&' | '*' | '+' | '-' | '/' | '<' | '<=' | '<>' | '=' | '>' | '>=' | '^';
|
|
94
|
+
|
|
95
|
+
export type SheetFormulaReferenceToken =
|
|
96
|
+
| (SheetFormulaCellReference & {
|
|
97
|
+
coordinateEndIndex: number;
|
|
98
|
+
coordinateStartIndex: number;
|
|
99
|
+
endIndex: number;
|
|
100
|
+
kind: 'SHEET_CELL';
|
|
101
|
+
startIndex: number;
|
|
102
|
+
text: string;
|
|
103
|
+
})
|
|
104
|
+
| (SheetFormulaRangeReference & {
|
|
105
|
+
endReference: SheetFormulaCellReference;
|
|
106
|
+
endReferenceCoordinateEndIndex: number;
|
|
107
|
+
endReferenceCoordinateStartIndex: number;
|
|
108
|
+
endIndex: number;
|
|
109
|
+
kind: 'SHEET_RANGE';
|
|
110
|
+
startReference: SheetFormulaCellReference;
|
|
111
|
+
startReferenceCoordinateEndIndex: number;
|
|
112
|
+
startReferenceCoordinateStartIndex: number;
|
|
113
|
+
startIndex: number;
|
|
114
|
+
text: string;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
export type SheetFormulaFunctionCallArgToken = {
|
|
118
|
+
argIndex: number;
|
|
119
|
+
endIndex: number;
|
|
120
|
+
startIndex: number;
|
|
121
|
+
text: string;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export type SheetFormulaFunctionCallToken = {
|
|
125
|
+
args: SheetFormulaFunctionCallArgToken[];
|
|
126
|
+
callEndIndex: number;
|
|
127
|
+
callStartIndex: number;
|
|
128
|
+
endIndex: number;
|
|
129
|
+
hasOpenParen: boolean;
|
|
130
|
+
isComplete: boolean;
|
|
131
|
+
name: string;
|
|
132
|
+
nameEndIndex: number;
|
|
133
|
+
nameStartIndex: number;
|
|
134
|
+
openParenIndex: number | null;
|
|
135
|
+
startIndex: number;
|
|
136
|
+
text: string;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
type SheetFormulaSheetTabNamePrefixToken = {
|
|
140
|
+
endIndex: number;
|
|
141
|
+
sheetName: string;
|
|
142
|
+
startIndex: number;
|
|
143
|
+
text: string;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export type SheetFormulaSheetTabNameRewriteResult = {
|
|
147
|
+
changed: boolean;
|
|
148
|
+
text: string;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const FILE_CONSTANTS = {
|
|
152
|
+
stringQuoteChars: ['"', "'", '`'] as const,
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
/*
|
|
156
|
+
* Return whether one character starts a supported formula string literal.
|
|
157
|
+
*/
|
|
158
|
+
export function isSheetFormulaStringQuoteChar(char?: string): char is (typeof FILE_CONSTANTS.stringQuoteChars)[number] {
|
|
159
|
+
return FILE_CONSTANTS.stringQuoteChars.includes(char as (typeof FILE_CONSTANTS.stringQuoteChars)[number]);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
* Return whether one text input should be treated as a sheet formula.
|
|
164
|
+
*/
|
|
165
|
+
export function isSheetFormulaText(value: unknown): value is string {
|
|
166
|
+
return typeof value === 'string' && value.trim().startsWith('=');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/*
|
|
170
|
+
* Return the expression portion of a formula without the leading equals sign.
|
|
171
|
+
*/
|
|
172
|
+
export function getSheetFormulaExpression(text: string) {
|
|
173
|
+
return text.trim().replace(/^=/, '').trim();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/*
|
|
177
|
+
* Return whether one character is an identifier character in formula text.
|
|
178
|
+
*/
|
|
179
|
+
export function isSheetFormulaIdentifierChar(char?: string) {
|
|
180
|
+
return Boolean(char && /[A-Za-z0-9_@]/.test(char));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/*
|
|
184
|
+
* Return the index after a quoted string literal in formula text.
|
|
185
|
+
*/
|
|
186
|
+
export function getSheetFormulaStringEndIndex(value: string, startIndex: number) {
|
|
187
|
+
const quoteChar = value[startIndex];
|
|
188
|
+
if (!isSheetFormulaStringQuoteChar(quoteChar)) {
|
|
189
|
+
return startIndex;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
let escaped = false;
|
|
193
|
+
|
|
194
|
+
for (let index = startIndex + 1; index < value.length; index += 1) {
|
|
195
|
+
const char = value[index];
|
|
196
|
+
|
|
197
|
+
if (escaped) {
|
|
198
|
+
escaped = false;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (char === '\\') {
|
|
203
|
+
escaped = true;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (char === quoteChar) {
|
|
208
|
+
return index + 1;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return value.length;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/*
|
|
216
|
+
* Return the index after a function call in formula text, respecting nested calls and strings.
|
|
217
|
+
*/
|
|
218
|
+
export function getSheetFormulaCallEndIndex(value: string, openParenIndex: number) {
|
|
219
|
+
let depth = 0;
|
|
220
|
+
let index = openParenIndex;
|
|
221
|
+
|
|
222
|
+
while (index < value.length) {
|
|
223
|
+
const char = value[index];
|
|
224
|
+
|
|
225
|
+
if (isSheetFormulaStringQuoteChar(char)) {
|
|
226
|
+
index = getSheetFormulaStringEndIndex(value, index);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (char === '(') {
|
|
231
|
+
depth += 1;
|
|
232
|
+
} else if (char === ')') {
|
|
233
|
+
depth -= 1;
|
|
234
|
+
|
|
235
|
+
if (depth === 0) {
|
|
236
|
+
return index + 1;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (depth < 0) {
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
index += 1;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/*
|
|
251
|
+
* Split a formula string on one top-level separator while respecting strings and calls.
|
|
252
|
+
*/
|
|
253
|
+
export function splitSheetFormulaTopLevel(value: string, separator: string) {
|
|
254
|
+
return splitSheetFormulaTopLevelSpans(value, separator).map((part) => part.text.trim());
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/*
|
|
258
|
+
* Split a formula string into top-level spans while preserving local indexes
|
|
259
|
+
* and whitespace.
|
|
260
|
+
*/
|
|
261
|
+
export function splitSheetFormulaTopLevelSpans(value: string, separator: string) {
|
|
262
|
+
const parts: Array<{ endIndex: number; startIndex: number; text: string }> = [];
|
|
263
|
+
let depth = 0;
|
|
264
|
+
let escaped = false;
|
|
265
|
+
let partStartIndex = 0;
|
|
266
|
+
let quoteChar: string | null = null;
|
|
267
|
+
|
|
268
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
269
|
+
const char = value[index];
|
|
270
|
+
|
|
271
|
+
if (escaped) {
|
|
272
|
+
escaped = false;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (char === '\\' && quoteChar) {
|
|
277
|
+
escaped = true;
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (quoteChar ? char === quoteChar : isSheetFormulaStringQuoteChar(char)) {
|
|
282
|
+
quoteChar = quoteChar ? null : char;
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (!quoteChar && char === '(') {
|
|
287
|
+
depth += 1;
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (!quoteChar && char === ')') {
|
|
292
|
+
depth = Math.max(0, depth - 1);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
if (!quoteChar && depth === 0 && value.startsWith(separator, index)) {
|
|
297
|
+
parts.push({
|
|
298
|
+
endIndex: index,
|
|
299
|
+
startIndex: partStartIndex,
|
|
300
|
+
text: value.slice(partStartIndex, index),
|
|
301
|
+
});
|
|
302
|
+
index += separator.length - 1;
|
|
303
|
+
partStartIndex = index + 1;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
parts.push({
|
|
308
|
+
endIndex: value.length,
|
|
309
|
+
startIndex: partStartIndex,
|
|
310
|
+
text: value.slice(partStartIndex),
|
|
311
|
+
});
|
|
312
|
+
return parts;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/*
|
|
316
|
+
* Return a function-like identifier token that starts at one formula index.
|
|
317
|
+
*/
|
|
318
|
+
function getSheetFormulaFunctionNameTokenAtIndex(value: string, index: number) {
|
|
319
|
+
if (isSheetFormulaIdentifierChar(value[index - 1])) {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const match = value.slice(index).match(/^@?[A-Za-z_][A-Za-z0-9_]*/);
|
|
324
|
+
if (!match) {
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
endIndex: index + match[0].length,
|
|
330
|
+
name: match[0],
|
|
331
|
+
startIndex: index,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/*
|
|
336
|
+
* Return the next non-whitespace index at or after the provided formula index.
|
|
337
|
+
*/
|
|
338
|
+
function getSheetFormulaNextNonWhitespaceIndex(value: string, index: number) {
|
|
339
|
+
let nextIndex = index;
|
|
340
|
+
|
|
341
|
+
while (nextIndex < value.length && /\s/.test(value[nextIndex])) {
|
|
342
|
+
nextIndex += 1;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
return nextIndex;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/*
|
|
349
|
+
* Return argument tokens for one function call argument span.
|
|
350
|
+
*/
|
|
351
|
+
function getSheetFormulaFunctionCallArgTokens(value: string, startIndex: number, endIndex: number) {
|
|
352
|
+
const argsText = value.slice(startIndex, endIndex);
|
|
353
|
+
|
|
354
|
+
return splitSheetFormulaTopLevelSpans(argsText, ',').map((part, argIndex) => ({
|
|
355
|
+
argIndex,
|
|
356
|
+
endIndex: startIndex + part.endIndex,
|
|
357
|
+
startIndex: startIndex + part.startIndex,
|
|
358
|
+
text: part.text,
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/*
|
|
363
|
+
* Return one function call token at the provided formula index.
|
|
364
|
+
*/
|
|
365
|
+
function getSheetFormulaFunctionCallTokenAtIndex(value: string, index: number): SheetFormulaFunctionCallToken | null {
|
|
366
|
+
const nameToken = getSheetFormulaFunctionNameTokenAtIndex(value, index);
|
|
367
|
+
if (!nameToken) {
|
|
368
|
+
return null;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const openParenIndex = getSheetFormulaNextNonWhitespaceIndex(value, nameToken.endIndex);
|
|
372
|
+
if (value[openParenIndex] !== '(') {
|
|
373
|
+
return {
|
|
374
|
+
args: [],
|
|
375
|
+
callEndIndex: nameToken.endIndex,
|
|
376
|
+
callStartIndex: nameToken.startIndex,
|
|
377
|
+
endIndex: nameToken.endIndex,
|
|
378
|
+
hasOpenParen: false,
|
|
379
|
+
isComplete: false,
|
|
380
|
+
name: nameToken.name,
|
|
381
|
+
nameEndIndex: nameToken.endIndex,
|
|
382
|
+
nameStartIndex: nameToken.startIndex,
|
|
383
|
+
openParenIndex: null,
|
|
384
|
+
startIndex: nameToken.startIndex,
|
|
385
|
+
text: value.slice(nameToken.startIndex, nameToken.endIndex),
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
const completeEndIndex = getSheetFormulaCallEndIndex(value, openParenIndex);
|
|
390
|
+
const callEndIndex = completeEndIndex || value.length;
|
|
391
|
+
const argsEndIndex = completeEndIndex ? completeEndIndex - 1 : callEndIndex;
|
|
392
|
+
|
|
393
|
+
return {
|
|
394
|
+
args: getSheetFormulaFunctionCallArgTokens(value, openParenIndex + 1, argsEndIndex),
|
|
395
|
+
callEndIndex,
|
|
396
|
+
callStartIndex: nameToken.startIndex,
|
|
397
|
+
endIndex: nameToken.endIndex,
|
|
398
|
+
hasOpenParen: true,
|
|
399
|
+
isComplete: Boolean(completeEndIndex),
|
|
400
|
+
name: nameToken.name,
|
|
401
|
+
nameEndIndex: nameToken.endIndex,
|
|
402
|
+
nameStartIndex: nameToken.startIndex,
|
|
403
|
+
openParenIndex,
|
|
404
|
+
startIndex: nameToken.startIndex,
|
|
405
|
+
text: value.slice(nameToken.startIndex, callEndIndex),
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/*
|
|
410
|
+
* Return function-call-like tokens from formula text for live client helpers.
|
|
411
|
+
*/
|
|
412
|
+
export function tokenizeSheetFormulaFunctionCalls(value: string): SheetFormulaFunctionCallToken[] {
|
|
413
|
+
if (!isSheetFormulaText(value)) {
|
|
414
|
+
return [];
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const tokens: SheetFormulaFunctionCallToken[] = [];
|
|
418
|
+
let index = 0;
|
|
419
|
+
|
|
420
|
+
while (index < value.length) {
|
|
421
|
+
const char = value[index];
|
|
422
|
+
|
|
423
|
+
if (isSheetFormulaStringQuoteChar(char)) {
|
|
424
|
+
index = getSheetFormulaStringEndIndex(value, index);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const token = getSheetFormulaFunctionCallTokenAtIndex(value, index);
|
|
429
|
+
if (token) {
|
|
430
|
+
tokens.push(token);
|
|
431
|
+
index = token.nameEndIndex;
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
index += 1;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return tokens;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/*
|
|
442
|
+
* Return whether a keyword starts at one index with non-identifier boundaries.
|
|
443
|
+
*/
|
|
444
|
+
function isSheetFormulaTopLevelKeywordAtIndex(value: string, index: number, keyword: string) {
|
|
445
|
+
const previousChar = value[index - 1];
|
|
446
|
+
const nextChar = value[index + keyword.length];
|
|
447
|
+
|
|
448
|
+
return (
|
|
449
|
+
value.slice(index, index + keyword.length).toLowerCase() === keyword.toLowerCase() &&
|
|
450
|
+
!isSheetFormulaIdentifierChar(previousChar) &&
|
|
451
|
+
!isSheetFormulaIdentifierChar(nextChar)
|
|
452
|
+
);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/*
|
|
456
|
+
* Split a formula string on one top-level keyword while respecting strings and calls.
|
|
457
|
+
*/
|
|
458
|
+
export function splitSheetFormulaTopLevelKeyword(value: string, keyword: string) {
|
|
459
|
+
const parts: string[] = [];
|
|
460
|
+
let current = '';
|
|
461
|
+
let depth = 0;
|
|
462
|
+
let quoteChar: string | null = null;
|
|
463
|
+
let escaped = false;
|
|
464
|
+
|
|
465
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
466
|
+
const char = value[index];
|
|
467
|
+
|
|
468
|
+
if (escaped) {
|
|
469
|
+
current += char;
|
|
470
|
+
escaped = false;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (char === '\\' && quoteChar) {
|
|
475
|
+
current += char;
|
|
476
|
+
escaped = true;
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (quoteChar ? char === quoteChar : isSheetFormulaStringQuoteChar(char)) {
|
|
481
|
+
quoteChar = quoteChar ? null : char;
|
|
482
|
+
current += char;
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (!quoteChar && char === '(') {
|
|
487
|
+
depth += 1;
|
|
488
|
+
current += char;
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (!quoteChar && char === ')') {
|
|
493
|
+
depth = Math.max(0, depth - 1);
|
|
494
|
+
current += char;
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
if (!quoteChar && depth === 0 && isSheetFormulaTopLevelKeywordAtIndex(value, index, keyword)) {
|
|
499
|
+
parts.push(current.trim());
|
|
500
|
+
current = '';
|
|
501
|
+
index += keyword.length - 1;
|
|
502
|
+
continue;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
current += char;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
parts.push(current.trim());
|
|
509
|
+
return parts;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/*
|
|
513
|
+
* Parse top-level operator terms while respecting strings, calls, and unary signs.
|
|
514
|
+
*/
|
|
515
|
+
function parseSheetFormulaOperatorTerms<Operator extends string>(params: {
|
|
516
|
+
defaultOperator: Operator;
|
|
517
|
+
operators: readonly Operator[];
|
|
518
|
+
value: string;
|
|
519
|
+
}) {
|
|
520
|
+
const terms: Array<SheetFormulaOperatorTerm<Operator>> = [];
|
|
521
|
+
const operatorSet = new Set<string>(params.operators);
|
|
522
|
+
let current = '';
|
|
523
|
+
let depth = 0;
|
|
524
|
+
let quoteChar: string | null = null;
|
|
525
|
+
let escaped = false;
|
|
526
|
+
let operator = params.defaultOperator;
|
|
527
|
+
|
|
528
|
+
for (let index = 0; index < params.value.length; index += 1) {
|
|
529
|
+
const char = params.value[index];
|
|
530
|
+
|
|
531
|
+
if (escaped) {
|
|
532
|
+
current += char;
|
|
533
|
+
escaped = false;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
if (char === '\\' && quoteChar) {
|
|
538
|
+
current += char;
|
|
539
|
+
escaped = true;
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (quoteChar ? char === quoteChar : isSheetFormulaStringQuoteChar(char)) {
|
|
544
|
+
quoteChar = quoteChar ? null : char;
|
|
545
|
+
current += char;
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (!quoteChar && char === '(') {
|
|
550
|
+
depth += 1;
|
|
551
|
+
current += char;
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (!quoteChar && char === ')') {
|
|
556
|
+
if (depth <= 0) {
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
depth -= 1;
|
|
561
|
+
current += char;
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (!quoteChar && depth === 0 && operatorSet.has(char)) {
|
|
566
|
+
const expression = current.trim();
|
|
567
|
+
const previousChar = current.trimEnd().slice(-1);
|
|
568
|
+
|
|
569
|
+
if ((char === '+' || char === '-') && (!expression || previousChar === '*' || previousChar === '/' || previousChar === '(')) {
|
|
570
|
+
current += char;
|
|
571
|
+
continue;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (!expression) {
|
|
575
|
+
return null;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
terms.push({
|
|
579
|
+
operator,
|
|
580
|
+
expression,
|
|
581
|
+
});
|
|
582
|
+
current = '';
|
|
583
|
+
operator = char as Operator;
|
|
584
|
+
continue;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
current += char;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
if (quoteChar || depth !== 0) {
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const expression = current.trim();
|
|
595
|
+
if (!expression) {
|
|
596
|
+
return null;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
terms.push({
|
|
600
|
+
operator,
|
|
601
|
+
expression,
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
return terms;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/*
|
|
608
|
+
* Parse top-level additive operators while preserving signs on numeric literals.
|
|
609
|
+
*/
|
|
610
|
+
export function parseSheetFormulaAdditiveTerms(value: string) {
|
|
611
|
+
return parseSheetFormulaOperatorTerms({
|
|
612
|
+
defaultOperator: '+' as const,
|
|
613
|
+
operators: ['+', '-'] as const,
|
|
614
|
+
value,
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/*
|
|
619
|
+
* Parse top-level multiplicative operators while preserving signed right-hand literals.
|
|
620
|
+
*/
|
|
621
|
+
export function parseSheetFormulaMultiplicativeTerms(value: string) {
|
|
622
|
+
return parseSheetFormulaOperatorTerms({
|
|
623
|
+
defaultOperator: '*' as const,
|
|
624
|
+
operators: ['*', '/'] as const,
|
|
625
|
+
value,
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/*
|
|
630
|
+
* Return a decoded escape sequence for a flexible formula string literal.
|
|
631
|
+
*/
|
|
632
|
+
function getSheetFormulaStringEscapeValue(char: string) {
|
|
633
|
+
switch (char) {
|
|
634
|
+
case 'n':
|
|
635
|
+
return '\n';
|
|
636
|
+
case 'r':
|
|
637
|
+
return '\r';
|
|
638
|
+
case 't':
|
|
639
|
+
return '\t';
|
|
640
|
+
default:
|
|
641
|
+
return char;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
/*
|
|
646
|
+
* Parse a formula string literal quoted with double quotes, single quotes, or backticks.
|
|
647
|
+
*/
|
|
648
|
+
export function parseSheetFormulaStringLiteral(value: string) {
|
|
649
|
+
const trimmed = value.trim();
|
|
650
|
+
const quoteChar = trimmed[0];
|
|
651
|
+
if (!isSheetFormulaStringQuoteChar(quoteChar) || trimmed[trimmed.length - 1] !== quoteChar) {
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (quoteChar === '"') {
|
|
656
|
+
try {
|
|
657
|
+
const parsed = JSON.parse(trimmed);
|
|
658
|
+
return typeof parsed === 'string' ? parsed : null;
|
|
659
|
+
} catch {
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
let escaped = false;
|
|
665
|
+
let result = '';
|
|
666
|
+
|
|
667
|
+
for (let index = 1; index < trimmed.length - 1; index += 1) {
|
|
668
|
+
const char = trimmed[index];
|
|
669
|
+
|
|
670
|
+
if (escaped) {
|
|
671
|
+
result += getSheetFormulaStringEscapeValue(char);
|
|
672
|
+
escaped = false;
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
if (char === '\\') {
|
|
677
|
+
escaped = true;
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
if (char === quoteChar) {
|
|
682
|
+
return null;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
result += char;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
return escaped ? null : result;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/*
|
|
692
|
+
* Parse a formula number literal.
|
|
693
|
+
*/
|
|
694
|
+
export function parseSheetFormulaNumberLiteral(value: string) {
|
|
695
|
+
const trimmed = value.trim();
|
|
696
|
+
return /^[-+]?\d+(?:\.\d+)?$/.test(trimmed) ? Number(trimmed) : null;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/*
|
|
700
|
+
* Return a normalized sheet-tab name parsed from a formula prefix.
|
|
701
|
+
*/
|
|
702
|
+
export function normalizeSheetFormulaReferenceSheetName(value: string) {
|
|
703
|
+
const sheetName = value.trim();
|
|
704
|
+
|
|
705
|
+
return sheetName ? sheetName : null;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
/*
|
|
709
|
+
* Return the case-insensitive lookup key for a formula sheet-tab name.
|
|
710
|
+
*/
|
|
711
|
+
export function getSheetFormulaSheetTabNameLookupKey(value?: string | null) {
|
|
712
|
+
return String(value || '')
|
|
713
|
+
.trim()
|
|
714
|
+
.toLowerCase();
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/*
|
|
718
|
+
* Return the end index of a cell reference that starts immediately after a
|
|
719
|
+
* sheet-tab prefix, or null when the prefix is not followed by a cell ref.
|
|
720
|
+
*/
|
|
721
|
+
export function getSheetFormulaCellReferenceEndIndex(value: string, startIndex: number) {
|
|
722
|
+
const match = value.slice(startIndex).match(/^(\$?[A-Za-z]+\$?[1-9]\d*)/);
|
|
723
|
+
if (!match) {
|
|
724
|
+
return null;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
const endIndex = startIndex + match[1].length;
|
|
728
|
+
return isSheetFormulaIdentifierChar(value[endIndex]) ? null : endIndex;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/*
|
|
732
|
+
* Return a quoted sheet-tab prefix token at one formula text index when that
|
|
733
|
+
* prefix is followed by a valid cell reference.
|
|
734
|
+
*/
|
|
735
|
+
function getQuotedSheetFormulaSheetTabNamePrefixTokenAtIndex(
|
|
736
|
+
value: string,
|
|
737
|
+
startIndex: number,
|
|
738
|
+
): SheetFormulaSheetTabNamePrefixToken | null {
|
|
739
|
+
if (value[startIndex] !== "'") {
|
|
740
|
+
return null;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
let sheetName = '';
|
|
744
|
+
for (let index = startIndex + 1; index < value.length; index += 1) {
|
|
745
|
+
const char = value[index];
|
|
746
|
+
|
|
747
|
+
if (char === "'" && value[index + 1] === "'") {
|
|
748
|
+
sheetName += "'";
|
|
749
|
+
index += 1;
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
if (char === "'") {
|
|
754
|
+
const endIndex = index + 2;
|
|
755
|
+
const normalizedName = normalizeSheetFormulaReferenceSheetName(sheetName);
|
|
756
|
+
if (!normalizedName || value[index + 1] !== '!' || !getSheetFormulaCellReferenceEndIndex(value, endIndex)) {
|
|
757
|
+
return null;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
return {
|
|
761
|
+
endIndex,
|
|
762
|
+
sheetName: normalizedName,
|
|
763
|
+
startIndex,
|
|
764
|
+
text: value.slice(startIndex, endIndex),
|
|
765
|
+
};
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
sheetName += char;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
return null;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/*
|
|
775
|
+
* Return an unquoted sheet-tab prefix token at one formula text index when
|
|
776
|
+
* that prefix is followed by a valid cell reference.
|
|
777
|
+
*/
|
|
778
|
+
function getUnquotedSheetFormulaSheetTabNamePrefixTokenAtIndex(
|
|
779
|
+
value: string,
|
|
780
|
+
startIndex: number,
|
|
781
|
+
): SheetFormulaSheetTabNamePrefixToken | null {
|
|
782
|
+
const match = value.slice(startIndex).match(/^([A-Za-z_][A-Za-z0-9_ .-]*)!/);
|
|
783
|
+
if (!match) {
|
|
784
|
+
return null;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
const endIndex = startIndex + match[0].length;
|
|
788
|
+
const sheetName = normalizeSheetFormulaReferenceSheetName(match[1]);
|
|
789
|
+
if (!sheetName || !getSheetFormulaCellReferenceEndIndex(value, endIndex)) {
|
|
790
|
+
return null;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
return {
|
|
794
|
+
endIndex,
|
|
795
|
+
sheetName,
|
|
796
|
+
startIndex,
|
|
797
|
+
text: match[0],
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/*
|
|
802
|
+
* Return a sheet-tab prefix token at one formula text index, if the text
|
|
803
|
+
* starts a cross-tab cell or range reference.
|
|
804
|
+
*/
|
|
805
|
+
function getSheetFormulaSheetTabNamePrefixTokenAtIndex(value: string, startIndex: number): SheetFormulaSheetTabNamePrefixToken | null {
|
|
806
|
+
return (
|
|
807
|
+
getQuotedSheetFormulaSheetTabNamePrefixTokenAtIndex(value, startIndex) ||
|
|
808
|
+
getUnquotedSheetFormulaSheetTabNamePrefixTokenAtIndex(value, startIndex)
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/*
|
|
813
|
+
* Rewrite cross-tab formula prefixes that reference one stored sheet-tab name.
|
|
814
|
+
* Title-like references are not normalized into names; they only change when
|
|
815
|
+
* their parsed sheet name already matches fromSheetTabName.
|
|
816
|
+
*/
|
|
817
|
+
export function rewriteSheetFormulaSheetTabNameReferences(
|
|
818
|
+
value: string,
|
|
819
|
+
fromSheetTabName?: string | null,
|
|
820
|
+
toSheetTabName?: string | null,
|
|
821
|
+
): SheetFormulaSheetTabNameRewriteResult {
|
|
822
|
+
const fromKey = getSheetFormulaSheetTabNameLookupKey(fromSheetTabName);
|
|
823
|
+
const candidateName = String(toSheetTabName || '')
|
|
824
|
+
.trim()
|
|
825
|
+
.toLowerCase();
|
|
826
|
+
const toName = /^[a-z0-9]+(?:_[a-z0-9]+)*$/.test(candidateName) ? candidateName : '';
|
|
827
|
+
if (!isSheetFormulaText(value) || !fromKey || !toName || fromKey === getSheetFormulaSheetTabNameLookupKey(toName)) {
|
|
828
|
+
return {
|
|
829
|
+
changed: false,
|
|
830
|
+
text: value,
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
// Unquoted prefixes must start with a letter or underscore, so quote names like 2026_01
|
|
835
|
+
const toPrefix = /^[a-z_]/.test(toName) ? toName : `'${toName}'`;
|
|
836
|
+
let changed = false;
|
|
837
|
+
let cursor = 0;
|
|
838
|
+
let index = 0;
|
|
839
|
+
let text = '';
|
|
840
|
+
|
|
841
|
+
while (index < value.length) {
|
|
842
|
+
const token = getSheetFormulaSheetTabNamePrefixTokenAtIndex(value, index);
|
|
843
|
+
if (token) {
|
|
844
|
+
if (getSheetFormulaSheetTabNameLookupKey(token.sheetName) === fromKey) {
|
|
845
|
+
text += value.slice(cursor, token.startIndex);
|
|
846
|
+
text += `${toPrefix}!`;
|
|
847
|
+
cursor = token.endIndex;
|
|
848
|
+
changed = true;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
index = token.endIndex;
|
|
852
|
+
continue;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
if (isSheetFormulaStringQuoteChar(value[index])) {
|
|
856
|
+
index = getSheetFormulaStringEndIndex(value, index);
|
|
857
|
+
continue;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
index += 1;
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
if (!changed) {
|
|
864
|
+
return {
|
|
865
|
+
changed: false,
|
|
866
|
+
text: value,
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
return {
|
|
871
|
+
changed,
|
|
872
|
+
text: text + value.slice(cursor),
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/*
|
|
877
|
+
* Return a one-based column index from a spreadsheet-style column label.
|
|
878
|
+
*/
|
|
879
|
+
export function getSheetFormulaColumnIndex(columnLabel: string) {
|
|
880
|
+
return columnLabel
|
|
881
|
+
.toUpperCase()
|
|
882
|
+
.split('')
|
|
883
|
+
.reduce((index, char) => {
|
|
884
|
+
return index * 26 + char.charCodeAt(0) - 64;
|
|
885
|
+
}, 0);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
/*
|
|
889
|
+
* Return a spreadsheet-style column label from a one-based column index.
|
|
890
|
+
*/
|
|
891
|
+
export function getSheetFormulaColumnLabel(columnIndex: number) {
|
|
892
|
+
let label = '';
|
|
893
|
+
let remaining = Math.floor(columnIndex);
|
|
894
|
+
|
|
895
|
+
while (remaining > 0) {
|
|
896
|
+
const remainder = (remaining - 1) % 26;
|
|
897
|
+
label = String.fromCharCode(65 + remainder) + label;
|
|
898
|
+
remaining = Math.floor((remaining - 1) / 26);
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
return label;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/*
|
|
905
|
+
* Render a parsed spreadsheet column reference while preserving its absolute marker.
|
|
906
|
+
*/
|
|
907
|
+
export function getSheetFormulaColumnReferenceText(reference: Pick<SheetFormulaCellReference, 'columnAbsolute' | 'columnIndex'>) {
|
|
908
|
+
return `${reference.columnAbsolute ? '$' : ''}${getSheetFormulaColumnLabel(reference.columnIndex)}`;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/*
|
|
912
|
+
* Render a parsed spreadsheet cell reference while preserving its absolute
|
|
913
|
+
* row and column markers.
|
|
914
|
+
*/
|
|
915
|
+
export function getSheetFormulaCellReferenceText(reference: SheetFormulaCellReference) {
|
|
916
|
+
return `${getSheetFormulaColumnReferenceText(reference)}${reference.rowAbsolute ? '$' : ''}${reference.rowIndex}`;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
/*
|
|
920
|
+
* Parse a spreadsheet-style cell reference like C12 or $C$12.
|
|
921
|
+
*/
|
|
922
|
+
export function parseSheetFormulaCellReference(value: string): SheetFormulaCellReference | null {
|
|
923
|
+
const trimmed = value.trim();
|
|
924
|
+
const bangIndex = trimmed.lastIndexOf('!');
|
|
925
|
+
if (bangIndex > 0) {
|
|
926
|
+
const sheetNameText = trimmed.slice(0, bangIndex);
|
|
927
|
+
const cellText = trimmed.slice(bangIndex + 1);
|
|
928
|
+
const sheetName =
|
|
929
|
+
sheetNameText.startsWith("'") && sheetNameText.endsWith("'")
|
|
930
|
+
? normalizeSheetFormulaReferenceSheetName(sheetNameText.slice(1, -1).replaceAll("''", "'"))
|
|
931
|
+
: normalizeSheetFormulaReferenceSheetName(sheetNameText);
|
|
932
|
+
const reference = parseSheetFormulaCellReference(cellText);
|
|
933
|
+
|
|
934
|
+
return reference && sheetName
|
|
935
|
+
? {
|
|
936
|
+
...reference,
|
|
937
|
+
sheetName,
|
|
938
|
+
}
|
|
939
|
+
: null;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
const match = trimmed.match(/^(\$?)([A-Za-z]+)(\$?)([1-9]\d*)$/);
|
|
943
|
+
if (!match) {
|
|
944
|
+
return null;
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
const columnLabel = match[2].toUpperCase();
|
|
948
|
+
const columnIndex = getSheetFormulaColumnIndex(columnLabel);
|
|
949
|
+
const rowIndex = Number(match[4]);
|
|
950
|
+
|
|
951
|
+
return {
|
|
952
|
+
...(match[1] ? { columnAbsolute: true } : {}),
|
|
953
|
+
columnIndex,
|
|
954
|
+
columnLabel,
|
|
955
|
+
...(match[3] ? { rowAbsolute: true } : {}),
|
|
956
|
+
rowIndex,
|
|
957
|
+
};
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/*
|
|
961
|
+
* Parse a formula function call by name.
|
|
962
|
+
*/
|
|
963
|
+
export function parseSheetFormulaCall(value: string, functionName?: string | null): SheetFormulaCall | null {
|
|
964
|
+
const trimmed = value.trim();
|
|
965
|
+
const match = trimmed.match(/^(@?[A-Za-z_][A-Za-z0-9_]*)\s*\(/);
|
|
966
|
+
if (!match || !trimmed.endsWith(')')) {
|
|
967
|
+
return null;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
const name = match[1];
|
|
971
|
+
if (functionName && name.toLowerCase() !== functionName.toLowerCase()) {
|
|
972
|
+
return null;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
const argsText = trimmed.slice(match[0].length, -1);
|
|
976
|
+
return {
|
|
977
|
+
args: splitSheetFormulaTopLevel(argsText, ','),
|
|
978
|
+
name,
|
|
979
|
+
text: trimmed,
|
|
980
|
+
};
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/*
|
|
984
|
+
* Return the canonical formula date/time shorthand name for a bare or called identifier.
|
|
985
|
+
*/
|
|
986
|
+
function normalizeSheetFormulaDateTimeShorthandName(value: string): SheetFormulaDateTimeShorthandName | null {
|
|
987
|
+
const normalized = value.trim().toUpperCase();
|
|
988
|
+
|
|
989
|
+
if (normalized === 'TODAY' || normalized === 'CURRENT_DATE') {
|
|
990
|
+
return 'CURRENT_DATE';
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
if (normalized === 'NOW' || normalized === 'CURRENT_DATETIME') {
|
|
994
|
+
return 'CURRENT_DATETIME';
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
return null;
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/*
|
|
1001
|
+
* Parse a supported query date/time shorthand such as TODAY or NOW().
|
|
1002
|
+
*/
|
|
1003
|
+
export function parseSheetFormulaDateTimeShorthand(
|
|
1004
|
+
value: string,
|
|
1005
|
+
): Extract<SheetFormulaASTNode, { kind: 'DATE_TIME_SHORTHAND' }> | null {
|
|
1006
|
+
const trimmed = value.trim();
|
|
1007
|
+
const call = parseSheetFormulaCall(trimmed);
|
|
1008
|
+
const isEmptyCall = call ? call.args.length === 0 || (call.args.length === 1 && call.args[0].trim() === '') : false;
|
|
1009
|
+
const shorthandName = call
|
|
1010
|
+
? isEmptyCall
|
|
1011
|
+
? normalizeSheetFormulaDateTimeShorthandName(call.name)
|
|
1012
|
+
: null
|
|
1013
|
+
: normalizeSheetFormulaDateTimeShorthandName(trimmed);
|
|
1014
|
+
|
|
1015
|
+
return shorthandName
|
|
1016
|
+
? {
|
|
1017
|
+
kind: 'DATE_TIME_SHORTHAND',
|
|
1018
|
+
name: shorthandName,
|
|
1019
|
+
text: trimmed,
|
|
1020
|
+
}
|
|
1021
|
+
: null;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
export const SHEET_FORMULA_REF_ERROR = '#REF!';
|
|
1025
|
+
|
|
1026
|
+
/* Display token for cells whose referenced log/entity no longer exists */
|
|
1027
|
+
export const SHEET_DELETED_REFERENCE_ERROR = '#DELETED!';
|