@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,1012 @@
|
|
|
1
|
+
import { getCurrencySymbol } from '@jsb188/app/utils/number.ts';
|
|
2
|
+
import { DateTime } from 'luxon';
|
|
3
|
+
import { SHEET_CELL_NUMBER_FORMAT_TYPE_ENUMS, SHEET_NUMBER_FORMAT_PRESET_PATTERNS } from '../constants/sheet.ts';
|
|
4
|
+
import type { SheetCellFormatObj, SheetCellNumberFormatTypeEnum } from '../types/sheet.d.ts';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Google/Excel spreadsheet serial date epoch (1899-12-30T00:00:00 UTC).
|
|
8
|
+
* Serial values count wall-clock days since this epoch; the fraction is the
|
|
9
|
+
* time of day. Serials are zone-free by definition.
|
|
10
|
+
*/
|
|
11
|
+
const SHEET_SERIAL_EPOCH_MS = Date.UTC(1899, 11, 30);
|
|
12
|
+
|
|
13
|
+
const MS_PER_DAY = 86400000;
|
|
14
|
+
|
|
15
|
+
// Numbers at or beyond this magnitude stringify in exponential notation and
|
|
16
|
+
// cannot round-trip through the digit-placeholder renderer
|
|
17
|
+
const SHEET_NUMBER_FORMAT_MAX_MAGNITUDE = 1e21;
|
|
18
|
+
|
|
19
|
+
// Parsed pattern cache; cleared whole when it hits the cap so long sessions
|
|
20
|
+
// with many custom patterns cannot grow it unbounded
|
|
21
|
+
const SHEET_NUMBER_FORMAT_PARSE_CACHE = new Map<string, SheetParsedNumberFormat | null>();
|
|
22
|
+
const SHEET_NUMBER_FORMAT_PARSE_CACHE_MAX = 256;
|
|
23
|
+
|
|
24
|
+
const SHEET_NUMBER_FORMAT_COLOR_NAMES = ['black', 'blue', 'cyan', 'green', 'magenta', 'red', 'white', 'yellow'];
|
|
25
|
+
|
|
26
|
+
export type SheetNumberFormatConditionOperator = '<' | '<=' | '>' | '>=' | '=' | '<>';
|
|
27
|
+
|
|
28
|
+
export type SheetNumberFormatToken =
|
|
29
|
+
| { kind: 'literal'; text: string }
|
|
30
|
+
| { kind: 'fill' }
|
|
31
|
+
| { kind: 'digit'; placeholder: '0' | '#' | '?' }
|
|
32
|
+
| { kind: 'decimal' }
|
|
33
|
+
| { kind: 'comma' }
|
|
34
|
+
| { kind: 'percent' }
|
|
35
|
+
| { kind: 'slash' }
|
|
36
|
+
| { kind: 'text' }
|
|
37
|
+
| { kind: 'exponent'; sign: '+' | '-'; digits: number }
|
|
38
|
+
| {
|
|
39
|
+
kind: 'date';
|
|
40
|
+
unit: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
|
|
41
|
+
length: number;
|
|
42
|
+
}
|
|
43
|
+
| { kind: 'meridiem'; style: 'AM/PM' | 'am/pm' | 'A/P' | 'a/p' }
|
|
44
|
+
| { kind: 'fractionalSecond'; length: number }
|
|
45
|
+
| { kind: 'elapsed'; unit: 'hour' | 'minute' | 'second'; length: number };
|
|
46
|
+
|
|
47
|
+
export interface SheetNumberFormatSection {
|
|
48
|
+
kind: 'number' | 'date' | 'text';
|
|
49
|
+
tokens: SheetNumberFormatToken[];
|
|
50
|
+
condition?: {
|
|
51
|
+
operator: SheetNumberFormatConditionOperator;
|
|
52
|
+
value: number;
|
|
53
|
+
} | null;
|
|
54
|
+
color?: string | null;
|
|
55
|
+
/* Number-section layout facts precomputed at parse time */
|
|
56
|
+
intPlaceholders: string;
|
|
57
|
+
decPlaceholders: string;
|
|
58
|
+
percentCount: number;
|
|
59
|
+
scaleCommaCount: number;
|
|
60
|
+
grouping: boolean;
|
|
61
|
+
hasFraction: boolean;
|
|
62
|
+
hasExponent: boolean;
|
|
63
|
+
/* Date-section facts */
|
|
64
|
+
hasMeridiem: boolean;
|
|
65
|
+
hasElapsed: boolean;
|
|
66
|
+
hasElapsedHour: boolean;
|
|
67
|
+
hasElapsedMinute: boolean;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface SheetParsedNumberFormat {
|
|
71
|
+
sections: SheetNumberFormatSection[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface SheetResolvedNumberFormat {
|
|
75
|
+
type: SheetCellNumberFormatTypeEnum;
|
|
76
|
+
pattern: string | null;
|
|
77
|
+
currency: string | null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/*
|
|
81
|
+
* Minimal cell value shape the formatter reads; both SheetCellGQL and
|
|
82
|
+
* SheetCellData rows satisfy it structurally.
|
|
83
|
+
*/
|
|
84
|
+
export interface SheetNumberFormatCellValues {
|
|
85
|
+
rawInput?: string | null;
|
|
86
|
+
formulaText?: string | null;
|
|
87
|
+
textValue?: string | null;
|
|
88
|
+
numberValue?: number | null;
|
|
89
|
+
booleanValue?: boolean | null;
|
|
90
|
+
dateValue?: Date | string | null;
|
|
91
|
+
datetimeValue?: Date | string | null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/*
|
|
95
|
+
* Return whether a value is a supported number format type enum.
|
|
96
|
+
*/
|
|
97
|
+
export function isSheetCellNumberFormatType(value: unknown): value is SheetCellNumberFormatTypeEnum {
|
|
98
|
+
return SHEET_CELL_NUMBER_FORMAT_TYPE_ENUMS.includes(value as SheetCellNumberFormatTypeEnum);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/*
|
|
102
|
+
* Return the effective {type, pattern, currency} for one resolved cell format
|
|
103
|
+
* object, filling preset patterns from the type and substituting the locale
|
|
104
|
+
* currency symbol into currency-family presets. Null when the format object
|
|
105
|
+
* carries no usable number format.
|
|
106
|
+
*/
|
|
107
|
+
export function resolveSheetCellNumberFormat(
|
|
108
|
+
format?: SheetCellFormatObj | null,
|
|
109
|
+
locale?: string | null,
|
|
110
|
+
): SheetResolvedNumberFormat | null {
|
|
111
|
+
const numberFormat = format?.numberFormat;
|
|
112
|
+
|
|
113
|
+
if (!numberFormat || typeof numberFormat !== 'object' || !isSheetCellNumberFormatType(numberFormat.type)) {
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const currency = typeof numberFormat.currency === 'string' && numberFormat.currency ? numberFormat.currency : null;
|
|
118
|
+
const explicitPattern = typeof numberFormat.pattern === 'string' && numberFormat.pattern ? numberFormat.pattern : null;
|
|
119
|
+
let pattern = explicitPattern || SHEET_NUMBER_FORMAT_PRESET_PATTERNS[numberFormat.type];
|
|
120
|
+
|
|
121
|
+
// Preset currency-family patterns carry a "$" literal that stands in for
|
|
122
|
+
// the resolved locale symbol; explicit custom patterns are left untouched
|
|
123
|
+
if (!explicitPattern && pattern && currency) {
|
|
124
|
+
let symbol = '';
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
symbol = getCurrencySymbol(locale || 'en-US', currency);
|
|
128
|
+
} catch {
|
|
129
|
+
symbol = '';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (symbol) {
|
|
133
|
+
pattern = pattern.replaceAll('"$"', `"${symbol.replaceAll('"', '')}"`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
currency,
|
|
139
|
+
pattern: pattern || null,
|
|
140
|
+
type: numberFormat.type,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/*
|
|
145
|
+
* Split one pattern into top-level sections on ";", respecting quoted
|
|
146
|
+
* literals, escapes, and bracket blocks.
|
|
147
|
+
*/
|
|
148
|
+
function splitSheetNumberFormatSections(pattern: string): string[] {
|
|
149
|
+
const sections: string[] = [];
|
|
150
|
+
let current = '';
|
|
151
|
+
let inQuote = false;
|
|
152
|
+
let inBracket = false;
|
|
153
|
+
|
|
154
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
155
|
+
const char = pattern[i];
|
|
156
|
+
|
|
157
|
+
if (char === '\\' && !inQuote) {
|
|
158
|
+
current += char + (pattern[i + 1] || '');
|
|
159
|
+
i++;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (char === '"') {
|
|
164
|
+
inQuote = !inQuote;
|
|
165
|
+
} else if (!inQuote && char === '[') {
|
|
166
|
+
inBracket = true;
|
|
167
|
+
} else if (!inQuote && char === ']') {
|
|
168
|
+
inBracket = false;
|
|
169
|
+
} else if (!inQuote && !inBracket && char === ';') {
|
|
170
|
+
sections.push(current);
|
|
171
|
+
current = '';
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
current += char;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
sections.push(current);
|
|
179
|
+
return sections.slice(0, 4);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/*
|
|
183
|
+
* Return one parsed bracket block token/metadata for a section, or null when
|
|
184
|
+
* the block is unknown and should be ignored.
|
|
185
|
+
*/
|
|
186
|
+
function parseSheetNumberFormatBracket(content: string, section: SheetNumberFormatSection): SheetNumberFormatToken | null {
|
|
187
|
+
// Currency with optional locale id, e.g. [$€-407] or [$USD]
|
|
188
|
+
const currencyMatch = content.match(/^\$([^-\]]*)(?:-[^\]]*)?$/);
|
|
189
|
+
if (currencyMatch) {
|
|
190
|
+
return currencyMatch[1] ? { kind: 'literal', text: currencyMatch[1] } : null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Elapsed duration units, e.g. [h] [mm] [s]
|
|
194
|
+
const elapsedMatch = content.match(/^(h+|m+|s+)$/i);
|
|
195
|
+
if (elapsedMatch) {
|
|
196
|
+
const letter = elapsedMatch[1][0].toLowerCase();
|
|
197
|
+
const unit = letter === 'h' ? 'hour' : letter === 'm' ? 'minute' : 'second';
|
|
198
|
+
return { kind: 'elapsed', length: elapsedMatch[1].length, unit };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Section condition, e.g. [>100] or [<=-5.5]
|
|
202
|
+
const conditionMatch = content.match(/^(<=|>=|<>|<|>|=)\s*(-?\d+(?:\.\d+)?)$/);
|
|
203
|
+
if (conditionMatch) {
|
|
204
|
+
section.condition = {
|
|
205
|
+
operator: conditionMatch[1] as SheetNumberFormatConditionOperator,
|
|
206
|
+
value: Number(conditionMatch[2]),
|
|
207
|
+
};
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Color tags are parsed as metadata only; the canvas does not paint them yet
|
|
212
|
+
const lowerContent = content.toLowerCase();
|
|
213
|
+
if (SHEET_NUMBER_FORMAT_COLOR_NAMES.includes(lowerContent) || /^color\s*\d+$/.test(lowerContent)) {
|
|
214
|
+
section.color = content;
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/*
|
|
222
|
+
* Tokenize one pattern section into a raw token stream. Date-vs-minute and
|
|
223
|
+
* comma semantics are resolved afterwards in classifySheetNumberFormatSection.
|
|
224
|
+
*/
|
|
225
|
+
function tokenizeSheetNumberFormatSection(src: string): SheetNumberFormatSection | null {
|
|
226
|
+
const section: SheetNumberFormatSection = {
|
|
227
|
+
condition: null,
|
|
228
|
+
color: null,
|
|
229
|
+
decPlaceholders: '',
|
|
230
|
+
grouping: false,
|
|
231
|
+
hasElapsed: false,
|
|
232
|
+
hasElapsedHour: false,
|
|
233
|
+
hasElapsedMinute: false,
|
|
234
|
+
hasExponent: false,
|
|
235
|
+
hasFraction: false,
|
|
236
|
+
hasMeridiem: false,
|
|
237
|
+
intPlaceholders: '',
|
|
238
|
+
kind: 'number',
|
|
239
|
+
percentCount: 0,
|
|
240
|
+
scaleCommaCount: 0,
|
|
241
|
+
tokens: [],
|
|
242
|
+
};
|
|
243
|
+
const tokens = section.tokens;
|
|
244
|
+
|
|
245
|
+
/*
|
|
246
|
+
* Append literal text, merging into a previous literal token.
|
|
247
|
+
*/
|
|
248
|
+
const pushLiteral = (text: string) => {
|
|
249
|
+
if (!text) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const lastToken = tokens[tokens.length - 1];
|
|
254
|
+
|
|
255
|
+
if (lastToken?.kind === 'literal') {
|
|
256
|
+
lastToken.text += text;
|
|
257
|
+
} else {
|
|
258
|
+
tokens.push({ kind: 'literal', text });
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
for (let i = 0; i < src.length; i++) {
|
|
263
|
+
const char = src[i];
|
|
264
|
+
|
|
265
|
+
if (char === '"') {
|
|
266
|
+
const end = src.indexOf('"', i + 1);
|
|
267
|
+
if (end === -1) {
|
|
268
|
+
pushLiteral(src.slice(i + 1));
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
pushLiteral(src.slice(i + 1, end));
|
|
272
|
+
i = end;
|
|
273
|
+
} else if (char === '\\') {
|
|
274
|
+
pushLiteral(src[i + 1] || '');
|
|
275
|
+
i++;
|
|
276
|
+
} else if (char === '_') {
|
|
277
|
+
// Skip-width placeholder renders as one space
|
|
278
|
+
pushLiteral(' ');
|
|
279
|
+
i++;
|
|
280
|
+
} else if (char === '*') {
|
|
281
|
+
tokens.push({ kind: 'fill' });
|
|
282
|
+
i++;
|
|
283
|
+
} else if (char === '[') {
|
|
284
|
+
const end = src.indexOf(']', i + 1);
|
|
285
|
+
if (end === -1) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
const bracketToken = parseSheetNumberFormatBracket(src.slice(i + 1, end), section);
|
|
289
|
+
if (bracketToken) {
|
|
290
|
+
tokens.push(bracketToken);
|
|
291
|
+
}
|
|
292
|
+
i = end;
|
|
293
|
+
} else if (char === '0' || char === '#' || char === '?') {
|
|
294
|
+
tokens.push({ kind: 'digit', placeholder: char });
|
|
295
|
+
} else if (char === '.') {
|
|
296
|
+
tokens.push({ kind: 'decimal' });
|
|
297
|
+
} else if (char === ',') {
|
|
298
|
+
tokens.push({ kind: 'comma' });
|
|
299
|
+
} else if (char === '%') {
|
|
300
|
+
tokens.push({ kind: 'percent' });
|
|
301
|
+
} else if (char === '/') {
|
|
302
|
+
tokens.push({ kind: 'slash' });
|
|
303
|
+
} else if (char === '@') {
|
|
304
|
+
tokens.push({ kind: 'text' });
|
|
305
|
+
} else if ((char === 'E' || char === 'e') && (src[i + 1] === '+' || src[i + 1] === '-')) {
|
|
306
|
+
const sign = src[i + 1] as '+' | '-';
|
|
307
|
+
let digits = 0;
|
|
308
|
+
let j = i + 2;
|
|
309
|
+
while (j < src.length && (src[j] === '0' || src[j] === '#' || src[j] === '?')) {
|
|
310
|
+
digits++;
|
|
311
|
+
j++;
|
|
312
|
+
}
|
|
313
|
+
tokens.push({ digits: Math.max(digits, 1), kind: 'exponent', sign });
|
|
314
|
+
i = j - 1;
|
|
315
|
+
} else if (/^(am\/pm|a\/p)/i.test(src.slice(i, i + 5))) {
|
|
316
|
+
const isLong = src.slice(i, i + 5).toLowerCase() === 'am/pm';
|
|
317
|
+
const isLower = char === 'a';
|
|
318
|
+
tokens.push({
|
|
319
|
+
kind: 'meridiem',
|
|
320
|
+
style: isLong ? (isLower ? 'am/pm' : 'AM/PM') : isLower ? 'a/p' : 'A/P',
|
|
321
|
+
});
|
|
322
|
+
i += isLong ? 4 : 2;
|
|
323
|
+
} else if (/[ymdhs]/i.test(char)) {
|
|
324
|
+
const letter = char.toLowerCase();
|
|
325
|
+
let length = 1;
|
|
326
|
+
while (src[i + 1]?.toLowerCase() === letter) {
|
|
327
|
+
length++;
|
|
328
|
+
i++;
|
|
329
|
+
}
|
|
330
|
+
const unit =
|
|
331
|
+
letter === 'y'
|
|
332
|
+
? ('year' as const)
|
|
333
|
+
: letter === 'm'
|
|
334
|
+
? ('month' as const)
|
|
335
|
+
: letter === 'd'
|
|
336
|
+
? ('day' as const)
|
|
337
|
+
: letter === 'h'
|
|
338
|
+
? ('hour' as const)
|
|
339
|
+
: ('second' as const);
|
|
340
|
+
tokens.push({ kind: 'date', length, unit });
|
|
341
|
+
} else {
|
|
342
|
+
pushLiteral(char);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return section;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/*
|
|
350
|
+
* Resolve token ambiguities and precompute section layout facts: classify the
|
|
351
|
+
* section kind, disambiguate month-vs-minute, fold date punctuation into
|
|
352
|
+
* literals, and derive digit placeholder/grouping/scaling info.
|
|
353
|
+
*/
|
|
354
|
+
function classifySheetNumberFormatSection(section: SheetNumberFormatSection): SheetNumberFormatSection {
|
|
355
|
+
const tokens = section.tokens;
|
|
356
|
+
const hasDateTokens = tokens.some((token) => token.kind === 'date' || token.kind === 'elapsed' || token.kind === 'meridiem');
|
|
357
|
+
const hasTextToken = tokens.some((token) => token.kind === 'text');
|
|
358
|
+
|
|
359
|
+
if (hasDateTokens) {
|
|
360
|
+
section.kind = 'date';
|
|
361
|
+
section.hasMeridiem = tokens.some((token) => token.kind === 'meridiem');
|
|
362
|
+
section.hasElapsed = tokens.some((token) => token.kind === 'elapsed');
|
|
363
|
+
section.hasElapsedHour = tokens.some((token) => token.kind === 'elapsed' && token.unit === 'hour');
|
|
364
|
+
section.hasElapsedMinute = tokens.some((token) => token.kind === 'elapsed' && token.unit === 'minute');
|
|
365
|
+
|
|
366
|
+
// Month tokens mean minutes when preceded by an hour token or followed
|
|
367
|
+
// by a seconds token within the same section
|
|
368
|
+
let lastTimeUnit: string | null = null;
|
|
369
|
+
tokens.forEach((token, index) => {
|
|
370
|
+
if (token.kind === 'elapsed') {
|
|
371
|
+
lastTimeUnit = token.unit;
|
|
372
|
+
} else if (token.kind !== 'date') {
|
|
373
|
+
return;
|
|
374
|
+
} else if (token.unit === 'month' && token.length <= 2) {
|
|
375
|
+
const nextDateToken = tokens.slice(index + 1).find((t) => t.kind === 'date' || t.kind === 'elapsed') as
|
|
376
|
+
| Extract<SheetNumberFormatToken, { kind: 'date' | 'elapsed' }>
|
|
377
|
+
| undefined;
|
|
378
|
+
|
|
379
|
+
if (lastTimeUnit === 'hour' || nextDateToken?.unit === 'second') {
|
|
380
|
+
token.unit = 'minute';
|
|
381
|
+
}
|
|
382
|
+
lastTimeUnit = token.unit;
|
|
383
|
+
} else {
|
|
384
|
+
lastTimeUnit = token.unit;
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
// Date punctuation renders literally; ".0" runs after seconds become
|
|
389
|
+
// fractional-second tokens
|
|
390
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
391
|
+
const token = tokens[i];
|
|
392
|
+
|
|
393
|
+
if (token.kind === 'decimal') {
|
|
394
|
+
const prevToken = tokens[i - 1];
|
|
395
|
+
const nextToken = tokens[i + 1];
|
|
396
|
+
const followsSeconds =
|
|
397
|
+
(prevToken?.kind === 'date' && prevToken.unit === 'second') ||
|
|
398
|
+
(prevToken?.kind === 'elapsed' && prevToken.unit === 'second');
|
|
399
|
+
|
|
400
|
+
if (followsSeconds && nextToken?.kind === 'digit' && nextToken.placeholder === '0') {
|
|
401
|
+
let length = 0;
|
|
402
|
+
while (tokens[i + 1]?.kind === 'digit') {
|
|
403
|
+
tokens.splice(i + 1, 1);
|
|
404
|
+
length++;
|
|
405
|
+
}
|
|
406
|
+
tokens[i] = { kind: 'fractionalSecond', length };
|
|
407
|
+
} else {
|
|
408
|
+
tokens[i] = { kind: 'literal', text: '.' };
|
|
409
|
+
}
|
|
410
|
+
} else if (token.kind === 'comma') {
|
|
411
|
+
tokens[i] = { kind: 'literal', text: ',' };
|
|
412
|
+
} else if (token.kind === 'slash') {
|
|
413
|
+
tokens[i] = { kind: 'literal', text: '/' };
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return section;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (hasTextToken) {
|
|
421
|
+
section.kind = 'text';
|
|
422
|
+
return section;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
section.kind = 'number';
|
|
426
|
+
|
|
427
|
+
const digitIndexes = tokens.reduce<number[]>((indexes, token, index) => {
|
|
428
|
+
if (token.kind === 'digit') {
|
|
429
|
+
indexes.push(index);
|
|
430
|
+
}
|
|
431
|
+
return indexes;
|
|
432
|
+
}, []);
|
|
433
|
+
|
|
434
|
+
if (!digitIndexes.length) {
|
|
435
|
+
return section;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
const firstDigitIndex = digitIndexes[0];
|
|
439
|
+
const lastDigitIndex = digitIndexes[digitIndexes.length - 1];
|
|
440
|
+
const decimalIndex = tokens.findIndex((token) => token.kind === 'decimal');
|
|
441
|
+
|
|
442
|
+
section.hasExponent = tokens.some((token) => token.kind === 'exponent');
|
|
443
|
+
section.percentCount = tokens.filter((token) => token.kind === 'percent').length;
|
|
444
|
+
section.hasFraction = tokens.some(
|
|
445
|
+
(token, index) =>
|
|
446
|
+
token.kind === 'slash' &&
|
|
447
|
+
digitIndexes.some((digitIndex) => digitIndex < index) &&
|
|
448
|
+
digitIndexes.some((digitIndex) => digitIndex > index),
|
|
449
|
+
);
|
|
450
|
+
|
|
451
|
+
// Commas between digit placeholders enable grouping; commas after the last
|
|
452
|
+
// digit placeholder scale the value by 1000 each; the rest are literals
|
|
453
|
+
tokens.forEach((token, index) => {
|
|
454
|
+
if (token.kind !== 'comma') {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (index > firstDigitIndex && index < lastDigitIndex) {
|
|
459
|
+
section.grouping = true;
|
|
460
|
+
} else if (index > lastDigitIndex) {
|
|
461
|
+
section.scaleCommaCount++;
|
|
462
|
+
} else {
|
|
463
|
+
tokens[index] = { kind: 'literal', text: ',' };
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
tokens.forEach((token, index) => {
|
|
468
|
+
if (token.kind !== 'digit') {
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
if (decimalIndex !== -1 && index > decimalIndex) {
|
|
473
|
+
section.decPlaceholders += token.placeholder;
|
|
474
|
+
} else {
|
|
475
|
+
section.intPlaceholders += token.placeholder;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
return section;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/*
|
|
483
|
+
* Parse one Google-Sheets-style number format pattern into render-ready
|
|
484
|
+
* sections. Results are cached by pattern; null means unusable pattern (the
|
|
485
|
+
* caller should fall back to raw display).
|
|
486
|
+
*/
|
|
487
|
+
export function parseSheetNumberFormatPattern(pattern: string): SheetParsedNumberFormat | null {
|
|
488
|
+
if (typeof pattern !== 'string' || !pattern || pattern.length > 500) {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
if (SHEET_NUMBER_FORMAT_PARSE_CACHE.has(pattern)) {
|
|
493
|
+
return SHEET_NUMBER_FORMAT_PARSE_CACHE.get(pattern) || null;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
let parsed: SheetParsedNumberFormat | null = null;
|
|
497
|
+
|
|
498
|
+
try {
|
|
499
|
+
const sections = splitSheetNumberFormatSections(pattern).map((sectionSrc) => tokenizeSheetNumberFormatSection(sectionSrc));
|
|
500
|
+
|
|
501
|
+
if (!sections.some((section) => section === null) && sections.length) {
|
|
502
|
+
parsed = {
|
|
503
|
+
sections: (sections as SheetNumberFormatSection[]).map(classifySheetNumberFormatSection),
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
} catch {
|
|
507
|
+
parsed = null;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (SHEET_NUMBER_FORMAT_PARSE_CACHE.size >= SHEET_NUMBER_FORMAT_PARSE_CACHE_MAX) {
|
|
511
|
+
SHEET_NUMBER_FORMAT_PARSE_CACHE.clear();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
SHEET_NUMBER_FORMAT_PARSE_CACHE.set(pattern, parsed);
|
|
515
|
+
return parsed;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/*
|
|
519
|
+
* Return the serial-day value for one Luxon date-time, reading its wall-clock
|
|
520
|
+
* components so the serial stays zone-free.
|
|
521
|
+
*/
|
|
522
|
+
export function getSheetSerialFromDateTime(dateTime: DateTime): number {
|
|
523
|
+
const utcClock = DateTime.utc(
|
|
524
|
+
dateTime.year,
|
|
525
|
+
dateTime.month,
|
|
526
|
+
dateTime.day,
|
|
527
|
+
dateTime.hour,
|
|
528
|
+
dateTime.minute,
|
|
529
|
+
dateTime.second,
|
|
530
|
+
dateTime.millisecond,
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
return (utcClock.toMillis() - SHEET_SERIAL_EPOCH_MS) / MS_PER_DAY;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/*
|
|
537
|
+
* Return the UTC Luxon date-time represented by one serial-day value.
|
|
538
|
+
*/
|
|
539
|
+
export function getSheetDateTimeFromSerial(serial: number): DateTime {
|
|
540
|
+
return DateTime.fromMillis(SHEET_SERIAL_EPOCH_MS + Math.round(serial * MS_PER_DAY), { zone: 'utc' });
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/*
|
|
544
|
+
* Insert thousands separators into one plain digit string.
|
|
545
|
+
*/
|
|
546
|
+
function addSheetNumberFormatGrouping(digits: string): string {
|
|
547
|
+
return digits.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/*
|
|
551
|
+
* Render the integer digit region for one number section: pads missing
|
|
552
|
+
* leading positions per placeholder type and applies grouping.
|
|
553
|
+
*/
|
|
554
|
+
function renderSheetNumberFormatIntegerPart(intDigits: string, placeholders: string, grouping: boolean): string {
|
|
555
|
+
let zeroPad = '';
|
|
556
|
+
let spacePad = '';
|
|
557
|
+
const missingCount = Math.max(placeholders.length - intDigits.length, 0);
|
|
558
|
+
|
|
559
|
+
for (let i = 0; i < missingCount; i++) {
|
|
560
|
+
if (placeholders[i] === '0') {
|
|
561
|
+
zeroPad += '0';
|
|
562
|
+
} else if (placeholders[i] === '?') {
|
|
563
|
+
spacePad += ' ';
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const fullDigits = zeroPad + intDigits;
|
|
568
|
+
|
|
569
|
+
return spacePad + (grouping && fullDigits ? addSheetNumberFormatGrouping(fullDigits) : fullDigits);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/*
|
|
573
|
+
* Render the decimal digit region for one number section: trims insignificant
|
|
574
|
+
* trailing zeros per placeholder type ("#" drops, "?" pads with space).
|
|
575
|
+
*/
|
|
576
|
+
function renderSheetNumberFormatDecimalPart(decDigits: string, placeholders: string): string {
|
|
577
|
+
const chars = decDigits.split('');
|
|
578
|
+
|
|
579
|
+
for (let i = chars.length - 1; i >= 0; i--) {
|
|
580
|
+
if (chars[i] !== '0' || placeholders[i] === '0') {
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
if (placeholders[i] === '#') {
|
|
585
|
+
chars.pop();
|
|
586
|
+
} else if (placeholders[i] === '?') {
|
|
587
|
+
chars[i] = ' ';
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
return chars.join('');
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/*
|
|
595
|
+
* Return {mantissa, exponent} for scientific rendering where the mantissa
|
|
596
|
+
* keeps as many integer digits as the section has integer placeholders.
|
|
597
|
+
*/
|
|
598
|
+
function getSheetNumberFormatScientificParts(absValue: number, intPlaceholderCount: number, decCount: number) {
|
|
599
|
+
if (!absValue) {
|
|
600
|
+
return { exponent: 0, mantissa: 0 };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const digitCount = Math.max(intPlaceholderCount, 1);
|
|
604
|
+
let exponent = Math.floor(Math.log10(absValue)) - (digitCount - 1);
|
|
605
|
+
let mantissa = absValue / Math.pow(10, exponent);
|
|
606
|
+
|
|
607
|
+
// Rounding can push the mantissa past its digit budget (9.99 -> 10.0)
|
|
608
|
+
if (Number(mantissa.toFixed(decCount)) >= Math.pow(10, digitCount)) {
|
|
609
|
+
exponent += 1;
|
|
610
|
+
mantissa = absValue / Math.pow(10, exponent);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
return { exponent, mantissa };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/*
|
|
617
|
+
* Render one non-negative number through one parsed number section's tokens.
|
|
618
|
+
*/
|
|
619
|
+
function renderSheetNumberFormatSection(section: SheetNumberFormatSection, absValue: number): string {
|
|
620
|
+
// Fraction patterns degrade to the plain decimal representation in v1
|
|
621
|
+
if (section.hasFraction) {
|
|
622
|
+
return String(absValue);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
const decCount = section.decPlaceholders.length;
|
|
626
|
+
let intDigits = '';
|
|
627
|
+
let decDigits = '';
|
|
628
|
+
let exponentValue = 0;
|
|
629
|
+
|
|
630
|
+
if (section.hasExponent) {
|
|
631
|
+
const scientific = getSheetNumberFormatScientificParts(absValue, section.intPlaceholders.length, decCount);
|
|
632
|
+
const fixedParts = scientific.mantissa.toFixed(decCount).split('.');
|
|
633
|
+
intDigits = fixedParts[0];
|
|
634
|
+
decDigits = fixedParts[1] || '';
|
|
635
|
+
exponentValue = scientific.exponent;
|
|
636
|
+
} else {
|
|
637
|
+
const fixedParts = absValue.toFixed(decCount).split('.');
|
|
638
|
+
intDigits = fixedParts[0];
|
|
639
|
+
decDigits = fixedParts[1] || '';
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// A zero integer part yields no digits so "#" renders empty, "0" pads back
|
|
643
|
+
// to "0", and "?" pads to one space
|
|
644
|
+
if (intDigits === '0') {
|
|
645
|
+
intDigits = '';
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const decOut = renderSheetNumberFormatDecimalPart(decDigits, section.decPlaceholders);
|
|
649
|
+
let out = '';
|
|
650
|
+
let intRendered = false;
|
|
651
|
+
let decRendered = false;
|
|
652
|
+
let passedDecimal = false;
|
|
653
|
+
|
|
654
|
+
for (const token of section.tokens) {
|
|
655
|
+
switch (token.kind) {
|
|
656
|
+
case 'literal':
|
|
657
|
+
out += token.text;
|
|
658
|
+
break;
|
|
659
|
+
case 'percent':
|
|
660
|
+
out += '%';
|
|
661
|
+
break;
|
|
662
|
+
case 'decimal':
|
|
663
|
+
passedDecimal = true;
|
|
664
|
+
if (decOut) {
|
|
665
|
+
out += '.';
|
|
666
|
+
}
|
|
667
|
+
break;
|
|
668
|
+
case 'digit':
|
|
669
|
+
// Each digit region renders whole at its first placeholder; the
|
|
670
|
+
// remaining placeholders emit nothing
|
|
671
|
+
if (!passedDecimal && !intRendered) {
|
|
672
|
+
out += renderSheetNumberFormatIntegerPart(intDigits, section.intPlaceholders, section.grouping);
|
|
673
|
+
intRendered = true;
|
|
674
|
+
} else if (passedDecimal && !decRendered) {
|
|
675
|
+
out += decOut;
|
|
676
|
+
decRendered = true;
|
|
677
|
+
}
|
|
678
|
+
break;
|
|
679
|
+
case 'exponent': {
|
|
680
|
+
const absExponent = Math.abs(exponentValue);
|
|
681
|
+
const sign = exponentValue < 0 ? '-' : token.sign === '+' ? '+' : '';
|
|
682
|
+
out += `E${sign}${String(absExponent).padStart(token.digits, '0')}`;
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
685
|
+
default:
|
|
686
|
+
// fill/slash/comma/text/date tokens render nothing in number sections
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
return out;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/*
|
|
695
|
+
* Render one serial-day duration/date value through one parsed date section.
|
|
696
|
+
*/
|
|
697
|
+
function renderSheetDateFormatSection(
|
|
698
|
+
section: SheetNumberFormatSection,
|
|
699
|
+
dateTime: DateTime,
|
|
700
|
+
serial: number,
|
|
701
|
+
locale?: string | null,
|
|
702
|
+
): string | null {
|
|
703
|
+
if (!section.hasElapsed && (serial < 0 || !dateTime.isValid)) {
|
|
704
|
+
return null;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const localized = locale ? dateTime.setLocale(locale) : dateTime;
|
|
708
|
+
const totalSeconds = Math.round(Math.abs(serial) * 86400);
|
|
709
|
+
let out = section.hasElapsed && serial < 0 ? '-' : '';
|
|
710
|
+
|
|
711
|
+
for (const token of section.tokens) {
|
|
712
|
+
if (token.kind === 'literal') {
|
|
713
|
+
out += token.text;
|
|
714
|
+
continue;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
if (token.kind === 'meridiem') {
|
|
718
|
+
const isPM = localized.hour >= 12;
|
|
719
|
+
const long = token.style === 'AM/PM' ? (isPM ? 'PM' : 'AM') : token.style === 'am/pm' ? (isPM ? 'pm' : 'am') : null;
|
|
720
|
+
out += long ?? (token.style === 'A/P' ? (isPM ? 'P' : 'A') : isPM ? 'p' : 'a');
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
if (token.kind === 'elapsed') {
|
|
725
|
+
const total =
|
|
726
|
+
token.unit === 'hour'
|
|
727
|
+
? Math.floor(totalSeconds / 3600)
|
|
728
|
+
: token.unit === 'minute'
|
|
729
|
+
? Math.floor(totalSeconds / 60)
|
|
730
|
+
: totalSeconds;
|
|
731
|
+
out += String(total).padStart(token.length, '0');
|
|
732
|
+
continue;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (token.kind === 'fractionalSecond') {
|
|
736
|
+
out += '.' + String(Math.floor((localized.millisecond * Math.pow(10, token.length)) / 1000)).padStart(token.length, '0');
|
|
737
|
+
continue;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (token.kind !== 'date') {
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
switch (token.unit) {
|
|
745
|
+
case 'year':
|
|
746
|
+
out += token.length >= 3 ? String(localized.year).padStart(4, '0') : String(localized.year % 100).padStart(2, '0');
|
|
747
|
+
break;
|
|
748
|
+
case 'month':
|
|
749
|
+
out +=
|
|
750
|
+
token.length === 1
|
|
751
|
+
? String(localized.month)
|
|
752
|
+
: token.length === 2
|
|
753
|
+
? String(localized.month).padStart(2, '0')
|
|
754
|
+
: token.length === 3
|
|
755
|
+
? localized.monthShort
|
|
756
|
+
: token.length === 4
|
|
757
|
+
? localized.monthLong
|
|
758
|
+
: (localized.monthLong || '').slice(0, 1);
|
|
759
|
+
break;
|
|
760
|
+
case 'day':
|
|
761
|
+
out +=
|
|
762
|
+
token.length === 1
|
|
763
|
+
? String(localized.day)
|
|
764
|
+
: token.length === 2
|
|
765
|
+
? String(localized.day).padStart(2, '0')
|
|
766
|
+
: token.length === 3
|
|
767
|
+
? localized.weekdayShort
|
|
768
|
+
: localized.weekdayLong;
|
|
769
|
+
break;
|
|
770
|
+
case 'hour': {
|
|
771
|
+
// Remainder hours under an elapsed-minute/second section make no
|
|
772
|
+
// sense; hours always come from the clock
|
|
773
|
+
const hourValue = section.hasMeridiem ? localized.hour % 12 || 12 : localized.hour;
|
|
774
|
+
out += token.length >= 2 ? String(hourValue).padStart(2, '0') : String(hourValue);
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
case 'minute': {
|
|
778
|
+
const minuteValue = section.hasElapsedHour ? Math.floor((totalSeconds % 3600) / 60) : localized.minute;
|
|
779
|
+
out += token.length >= 2 ? String(minuteValue).padStart(2, '0') : String(minuteValue);
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
case 'second': {
|
|
783
|
+
const secondValue = section.hasElapsed ? totalSeconds % 60 : localized.second;
|
|
784
|
+
out += token.length >= 2 ? String(secondValue).padStart(2, '0') : String(secondValue);
|
|
785
|
+
break;
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
return out;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/*
|
|
794
|
+
* Render one text value through one parsed text section ("@" substitution).
|
|
795
|
+
*/
|
|
796
|
+
function renderSheetTextFormatSection(section: SheetNumberFormatSection, text: string): string {
|
|
797
|
+
return section.tokens.reduce((out, token) => {
|
|
798
|
+
if (token.kind === 'text') {
|
|
799
|
+
return out + text;
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
return token.kind === 'literal' ? out + token.text : out;
|
|
803
|
+
}, '');
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/*
|
|
807
|
+
* Return whether one section condition matches a value.
|
|
808
|
+
*/
|
|
809
|
+
function sheetNumberFormatConditionMatches(
|
|
810
|
+
condition: { operator: SheetNumberFormatConditionOperator; value: number },
|
|
811
|
+
value: number,
|
|
812
|
+
): boolean {
|
|
813
|
+
switch (condition.operator) {
|
|
814
|
+
case '<':
|
|
815
|
+
return value < condition.value;
|
|
816
|
+
case '<=':
|
|
817
|
+
return value <= condition.value;
|
|
818
|
+
case '>':
|
|
819
|
+
return value > condition.value;
|
|
820
|
+
case '>=':
|
|
821
|
+
return value >= condition.value;
|
|
822
|
+
case '=':
|
|
823
|
+
return value === condition.value;
|
|
824
|
+
case '<>':
|
|
825
|
+
return value !== condition.value;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/*
|
|
830
|
+
* Pick the section for one numeric value: explicit conditions win, otherwise
|
|
831
|
+
* positional pos/neg/zero semantics with Google's round-before-choosing rule.
|
|
832
|
+
*/
|
|
833
|
+
function pickSheetNumberFormatSection(
|
|
834
|
+
parsed: SheetParsedNumberFormat,
|
|
835
|
+
value: number,
|
|
836
|
+
): { section: SheetNumberFormatSection; useAutoMinus: boolean } | null {
|
|
837
|
+
const sections = parsed.sections.filter((section) => section.kind !== 'text' || parsed.sections.length === 1);
|
|
838
|
+
|
|
839
|
+
if (!sections.length) {
|
|
840
|
+
return null;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
if (sections.some((section) => section.condition)) {
|
|
844
|
+
const matched =
|
|
845
|
+
sections.find((section) => section.condition && sheetNumberFormatConditionMatches(section.condition, value)) ||
|
|
846
|
+
sections.find((section) => !section.condition) ||
|
|
847
|
+
sections[sections.length - 1];
|
|
848
|
+
|
|
849
|
+
return { section: matched, useAutoMinus: value < 0 };
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
const positive = sections[0];
|
|
853
|
+
const negative = sections.length >= 2 ? sections[1] : null;
|
|
854
|
+
const zero = sections.length >= 3 ? sections[2] : null;
|
|
855
|
+
|
|
856
|
+
// Round with the sign-picked section first so values like -0.004 with two
|
|
857
|
+
// decimals land in the positive/zero section, not "(0.00)"
|
|
858
|
+
const tentative = value < 0 && negative ? negative : positive;
|
|
859
|
+
const scaled = (value * Math.pow(100, tentative.percentCount)) / Math.pow(1000, tentative.scaleCommaCount);
|
|
860
|
+
const rounded =
|
|
861
|
+
tentative.kind === 'number' && !tentative.hasExponent
|
|
862
|
+
? Number(Math.abs(scaled).toFixed(tentative.decPlaceholders.length))
|
|
863
|
+
: Math.abs(scaled);
|
|
864
|
+
|
|
865
|
+
if (rounded === 0) {
|
|
866
|
+
return { section: zero || positive, useAutoMinus: false };
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
if (value < 0) {
|
|
870
|
+
return negative ? { section: negative, useAutoMinus: false } : { section: positive, useAutoMinus: true };
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
return { section: positive, useAutoMinus: false };
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/*
|
|
877
|
+
* Format one numeric value with one parsed pattern, dispatching to date
|
|
878
|
+
* rendering via serial conversion when the chosen section uses date tokens.
|
|
879
|
+
*/
|
|
880
|
+
export function formatSheetNumberValue(value: number, parsed: SheetParsedNumberFormat, locale?: string | null): string | null {
|
|
881
|
+
if (!Number.isFinite(value) || Math.abs(value) >= SHEET_NUMBER_FORMAT_MAX_MAGNITUDE) {
|
|
882
|
+
return null;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
const picked = pickSheetNumberFormatSection(parsed, value);
|
|
886
|
+
|
|
887
|
+
if (!picked) {
|
|
888
|
+
return null;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
if (picked.section.kind === 'date') {
|
|
892
|
+
return renderSheetDateFormatSection(picked.section, getSheetDateTimeFromSerial(value), value, locale);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
if (picked.section.kind === 'text') {
|
|
896
|
+
return null;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
const scaled = (Math.abs(value) * Math.pow(100, picked.section.percentCount)) / Math.pow(1000, picked.section.scaleCommaCount);
|
|
900
|
+
const rendered = renderSheetNumberFormatSection(picked.section, scaled);
|
|
901
|
+
const roundedIsZero = !picked.section.hasExponent && Number(scaled.toFixed(picked.section.decPlaceholders.length)) === 0;
|
|
902
|
+
|
|
903
|
+
return picked.useAutoMinus && !roundedIsZero ? `-${rendered}` : rendered;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
/*
|
|
907
|
+
* Return the Luxon date-time for one cell's date-like value. Canonical typed
|
|
908
|
+
* mirrors win; a strict ISO text mirror also supports serialized cells.
|
|
909
|
+
* Date-only values keep their wall clock, while datetimes shift by time zone.
|
|
910
|
+
*/
|
|
911
|
+
function getSheetCellFormatDateTime(cell: SheetNumberFormatCellValues, timeZone?: string | null): DateTime | null {
|
|
912
|
+
const isoTextValue = typeof cell.textValue === 'string' ? cell.textValue.trim() : '';
|
|
913
|
+
const textDateValue = /^\d{4}-\d{2}-\d{2}$/.test(isoTextValue) ? isoTextValue : null;
|
|
914
|
+
const textDateTimeValue = /^\d{4}-\d{2}-\d{2}T/.test(isoTextValue) ? isoTextValue : null;
|
|
915
|
+
const dateValue = cell.dateValue || textDateValue;
|
|
916
|
+
const datetimeValue = cell.datetimeValue || textDateTimeValue;
|
|
917
|
+
const value = dateValue || datetimeValue;
|
|
918
|
+
|
|
919
|
+
if (!value) {
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
const dateTime =
|
|
924
|
+
value instanceof Date ? DateTime.fromJSDate(value, { zone: 'utc' }) : DateTime.fromISO(String(value), { setZone: true });
|
|
925
|
+
|
|
926
|
+
if (!dateTime.isValid) {
|
|
927
|
+
return null;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
if (datetimeValue && !dateValue && timeZone) {
|
|
931
|
+
const zoned = dateTime.setZone(timeZone);
|
|
932
|
+
return zoned.isValid ? zoned : dateTime;
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
return dateTime;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
/*
|
|
939
|
+
* SINGLE SOURCE OF TRUTH for explicit sheet value formatting: return the
|
|
940
|
+
* display string one resolved cell format produces for one cell's typed
|
|
941
|
+
* values, or null when no explicit format applies so each caller can use its
|
|
942
|
+
* own raw/default display. AUTOMATIC/TEXT and unformattable values return null.
|
|
943
|
+
*/
|
|
944
|
+
export function getSheetCellFormattedDisplayValue(
|
|
945
|
+
cell: SheetNumberFormatCellValues,
|
|
946
|
+
format?: SheetCellFormatObj | null,
|
|
947
|
+
timeZone?: string | null,
|
|
948
|
+
): string | null {
|
|
949
|
+
return formatSheetCellValue(cell, format, timeZone);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/*
|
|
953
|
+
* MAIN ENTRY: return the display string for one cell under one resolved cell
|
|
954
|
+
* format object, or null when the number format does not apply.
|
|
955
|
+
*/
|
|
956
|
+
export function formatSheetCellValue(
|
|
957
|
+
cell: SheetNumberFormatCellValues,
|
|
958
|
+
format?: SheetCellFormatObj | null,
|
|
959
|
+
timeZone?: string | null,
|
|
960
|
+
locale?: string | null,
|
|
961
|
+
): string | null {
|
|
962
|
+
const resolved = resolveSheetCellNumberFormat(format, locale);
|
|
963
|
+
|
|
964
|
+
// AUTOMATIC and TEXT render the raw value; CUSTOM without a pattern has
|
|
965
|
+
// nothing to render with
|
|
966
|
+
if (!resolved?.pattern || resolved.type === 'TEXT') {
|
|
967
|
+
return null;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
const parsed = parseSheetNumberFormatPattern(resolved.pattern);
|
|
971
|
+
|
|
972
|
+
if (!parsed) {
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
try {
|
|
977
|
+
if (typeof cell.numberValue === 'number') {
|
|
978
|
+
return formatSheetNumberValue(cell.numberValue, parsed, locale);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
const dateTime = getSheetCellFormatDateTime(cell, timeZone);
|
|
982
|
+
|
|
983
|
+
if (dateTime) {
|
|
984
|
+
const serial = getSheetSerialFromDateTime(dateTime);
|
|
985
|
+
const picked = pickSheetNumberFormatSection(parsed, serial);
|
|
986
|
+
|
|
987
|
+
if (picked?.section.kind === 'date') {
|
|
988
|
+
return renderSheetDateFormatSection(picked.section, dateTime, serial, locale);
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
return formatSheetNumberValue(serial, parsed, locale);
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
// Text values only pick up an explicit "@" section: the 4th section of a
|
|
995
|
+
// full pattern, or a single-section pure text pattern
|
|
996
|
+
const textValue = typeof cell.textValue === 'string' ? cell.textValue : null;
|
|
997
|
+
const textSection =
|
|
998
|
+
parsed.sections.length === 4 && parsed.sections[3].kind === 'text'
|
|
999
|
+
? parsed.sections[3]
|
|
1000
|
+
: parsed.sections.length === 1 && parsed.sections[0].kind === 'text'
|
|
1001
|
+
? parsed.sections[0]
|
|
1002
|
+
: null;
|
|
1003
|
+
|
|
1004
|
+
if (textValue !== null && textSection) {
|
|
1005
|
+
return renderSheetTextFormatSection(textSection, textValue);
|
|
1006
|
+
}
|
|
1007
|
+
} catch {
|
|
1008
|
+
return null;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
return null;
|
|
1012
|
+
}
|