dsh-artifact-studio 0.8.0 → 0.13.0
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/README.md +72 -10
- package/THIRD_PARTY_NOTICES.md +57 -0
- package/assets/canvas.engine.js +72 -72
- package/assets/canvas.viewer.js +6415 -0
- package/client.canvas.js +1 -1
- package/client.doc.js +67 -79
- package/client.js +313 -291
- package/client.sheet.js +218 -0
- package/dist/contract.js +32 -5
- package/dist/host/lib/assets.js +12 -0
- package/dist/host/lib/brief.js +127 -0
- package/dist/host/lib/checks.js +62 -8
- package/dist/host/lib/design-catalog.js +149 -0
- package/dist/host/lib/design-render.js +37 -0
- package/dist/host/lib/design-system.js +1 -1
- package/dist/host/lib/export.js +123 -5
- package/dist/host/lib/fsutil.js +41 -0
- package/dist/host/lib/generation.js +36 -0
- package/dist/host/lib/guide.js +130 -0
- package/dist/host/lib/paths.js +2 -1
- package/dist/host/lib/recipes.js +224 -0
- package/dist/host/lib/rpc.js +20 -5
- package/dist/host/lib/service.js +253 -24
- package/dist/host/lib/skill.js +59 -18
- package/dist/host/lib/store.js +107 -25
- package/dist/host/lib/svg-model.js +85 -0
- package/dist/host/lib/svg-viewer.js +380 -0
- package/dist/host/lib/tools.js +346 -35
- package/dist/host/lib/validate.js +24 -0
- package/dist/host/lib/viewer.js +150 -0
- package/dist/host/lib/xlsx.js +314 -0
- package/dist/host/worker/design-worker.js +71 -5
- package/dist/host/worker/page-model.js +169 -0
- package/dist/html-tags.js +51 -0
- package/dist/kit.js +224 -0
- package/dist/render.js +10 -3
- package/dist/sheet/formula.js +475 -0
- package/dist/sheet/functions.js +463 -0
- package/dist/sheet/index.js +9 -0
- package/dist/sheet/model.js +695 -0
- package/dist/sheet/operations.js +384 -0
- package/dist/sheet/output.js +182 -0
- package/package.json +15 -3
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formula evaluation for sheet artifacts.
|
|
3
|
+
*
|
|
4
|
+
* The parser is fast-formula-parser 1.0.19 (MIT). It reads cells and ranges
|
|
5
|
+
* through callbacks, so the workbook model stays ours while the engine brings
|
|
6
|
+
* Excel's function set. This module adds what the library leaves out:
|
|
7
|
+
* circular-reference detection, a bounded evaluation budget, normalising its
|
|
8
|
+
* thrown #ERROR! into Excel codes, and a per-cell display string.
|
|
9
|
+
*/
|
|
10
|
+
import FormulaParser from 'fast-formula-parser';
|
|
11
|
+
import { createSheetFunctions } from './functions.js';
|
|
12
|
+
import { SHEET_LIMITS, formatCellRef } from './model.js';
|
|
13
|
+
function emptyFlags() {
|
|
14
|
+
return { cycle: false, limit: false, syntax: undefined, referenceError: undefined, unresolved: false };
|
|
15
|
+
}
|
|
16
|
+
function isError(outcome) {
|
|
17
|
+
return 'error' in outcome;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Read the error cell a parser returns.
|
|
21
|
+
* @param value a parser result.
|
|
22
|
+
* @returns the error code, or undefined for a value.
|
|
23
|
+
*/
|
|
24
|
+
function errorCodeOf(value) {
|
|
25
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
26
|
+
return undefined;
|
|
27
|
+
const record = { ...value };
|
|
28
|
+
const code = record._error ?? record.name;
|
|
29
|
+
return typeof code === 'string' && code.startsWith('#') ? code : undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The message a parser error carries.
|
|
33
|
+
* @param value the error object.
|
|
34
|
+
* @returns the message, or an empty string.
|
|
35
|
+
*/
|
|
36
|
+
function errorMessageOf(value) {
|
|
37
|
+
if (value === null || typeof value !== 'object')
|
|
38
|
+
return '';
|
|
39
|
+
const record = { ...value };
|
|
40
|
+
return typeof record.message === 'string' ? record.message : '';
|
|
41
|
+
}
|
|
42
|
+
function normaliseCode(code, message) {
|
|
43
|
+
switch (code) {
|
|
44
|
+
case '#VALUE!':
|
|
45
|
+
case '#DIV/0!':
|
|
46
|
+
case '#NAME?':
|
|
47
|
+
case '#REF!':
|
|
48
|
+
case '#N/A':
|
|
49
|
+
case '#NUM!':
|
|
50
|
+
case '#NULL!':
|
|
51
|
+
return code;
|
|
52
|
+
case '#CYCLE!':
|
|
53
|
+
case '#LIMIT!':
|
|
54
|
+
case '#SYNTAX!':
|
|
55
|
+
return code;
|
|
56
|
+
case '#ERROR!':
|
|
57
|
+
return /not implemented/i.test(message) ? '#NAME?' : '#SYNTAX!';
|
|
58
|
+
default:
|
|
59
|
+
return '#ERROR!'.startsWith(code) ? '#SYNTAX!' : '#VALUE!';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const CATALOG = FormulaParser.FormulaError;
|
|
63
|
+
function libraryError(code) {
|
|
64
|
+
switch (code) {
|
|
65
|
+
case '#DIV/0!':
|
|
66
|
+
return CATALOG.DIV0;
|
|
67
|
+
case '#N/A':
|
|
68
|
+
return CATALOG.NA;
|
|
69
|
+
case '#NAME?':
|
|
70
|
+
return CATALOG.NAME;
|
|
71
|
+
case '#NUM!':
|
|
72
|
+
return CATALOG.NUM;
|
|
73
|
+
case '#REF!':
|
|
74
|
+
return CATALOG.REF;
|
|
75
|
+
case '#NULL!':
|
|
76
|
+
return CATALOG.NULL;
|
|
77
|
+
default:
|
|
78
|
+
return CATALOG.VALUE;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Render a number for display. Formats are deterministic and use US groups.
|
|
83
|
+
* @param value the number.
|
|
84
|
+
* @param format the cell number format.
|
|
85
|
+
* @returns the display text.
|
|
86
|
+
*/
|
|
87
|
+
export function formatNumber(value, format) {
|
|
88
|
+
if (!Number.isFinite(value))
|
|
89
|
+
return '#NUM!';
|
|
90
|
+
switch (format) {
|
|
91
|
+
case 'number':
|
|
92
|
+
return groupFixed(value, 2);
|
|
93
|
+
case 'integer':
|
|
94
|
+
return groupFixed(Math.round(value), 0);
|
|
95
|
+
case 'currency': {
|
|
96
|
+
const text = groupFixed(Math.abs(value), 2);
|
|
97
|
+
return (value < 0 ? '-$' : '$') + text;
|
|
98
|
+
}
|
|
99
|
+
case 'percent':
|
|
100
|
+
return groupFixed(value * 100, 2) + '%';
|
|
101
|
+
case 'date':
|
|
102
|
+
return formatDate(value);
|
|
103
|
+
case undefined:
|
|
104
|
+
case 'general':
|
|
105
|
+
default:
|
|
106
|
+
return generalNumber(value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function groupFixed(value, digits) {
|
|
110
|
+
const fixed = Math.abs(value).toFixed(digits);
|
|
111
|
+
const parts = fixed.split('.');
|
|
112
|
+
const whole = parts[0] ?? '0';
|
|
113
|
+
const fraction = parts[1];
|
|
114
|
+
const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
115
|
+
const text = fraction === undefined ? grouped : grouped + '.' + fraction;
|
|
116
|
+
return value < 0 && Number(fixed) !== 0 ? '-' + text : text;
|
|
117
|
+
}
|
|
118
|
+
function generalNumber(value) {
|
|
119
|
+
if (Number.isInteger(value))
|
|
120
|
+
return String(value);
|
|
121
|
+
const magnitude = Math.abs(value);
|
|
122
|
+
if (magnitude >= 1e15 || (magnitude > 0 && magnitude < 1e-9))
|
|
123
|
+
return value.toExponential(6);
|
|
124
|
+
return String(Number(value.toPrecision(12)));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Excel stores a date as a day count from 1899; this renders it as ISO text so
|
|
128
|
+
* the panel, export, and CSV agree without a time zone.
|
|
129
|
+
* @param serial the Excel serial number.
|
|
130
|
+
* @returns the date, with a time when the serial has a fraction.
|
|
131
|
+
*/
|
|
132
|
+
function formatDate(serial) {
|
|
133
|
+
if (serial <= 0 || serial > 2958465)
|
|
134
|
+
return generalNumber(serial);
|
|
135
|
+
const whole = Math.floor(serial);
|
|
136
|
+
const fraction = serial - whole;
|
|
137
|
+
const epoch = whole < 60 ? Date.UTC(1899, 11, 31) + whole * 86400000 : Date.UTC(1899, 11, 30) + whole * 86400000;
|
|
138
|
+
const date = new Date(epoch + Math.round(fraction * 86400000));
|
|
139
|
+
const pad = (value) => String(value).padStart(2, '0');
|
|
140
|
+
const text = String(date.getUTCFullYear()) + '-' + pad(date.getUTCMonth() + 1) + '-' + pad(date.getUTCDate());
|
|
141
|
+
if (fraction === 0)
|
|
142
|
+
return text;
|
|
143
|
+
return text + ' ' + pad(date.getUTCHours()) + ':' + pad(date.getUTCMinutes());
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Render one scalar for the panel, a print copy, or CSV.
|
|
147
|
+
* @param value the value.
|
|
148
|
+
* @param format the cell number format.
|
|
149
|
+
* @returns the display text.
|
|
150
|
+
*/
|
|
151
|
+
export function formatScalar(value, format) {
|
|
152
|
+
if (value === null)
|
|
153
|
+
return '';
|
|
154
|
+
if (typeof value === 'boolean')
|
|
155
|
+
return value ? 'TRUE' : 'FALSE';
|
|
156
|
+
if (typeof value === 'number')
|
|
157
|
+
return formatNumber(value, format);
|
|
158
|
+
return value;
|
|
159
|
+
}
|
|
160
|
+
class Evaluator {
|
|
161
|
+
workbook;
|
|
162
|
+
sheets = new Map();
|
|
163
|
+
memo = new Map();
|
|
164
|
+
visiting = new Set();
|
|
165
|
+
parser;
|
|
166
|
+
depParser;
|
|
167
|
+
flags = emptyFlags();
|
|
168
|
+
budget = SHEET_LIMITS.maxEvaluationCells;
|
|
169
|
+
constructor(workbook) {
|
|
170
|
+
this.workbook = workbook;
|
|
171
|
+
for (const sheet of workbook.sheets)
|
|
172
|
+
this.sheets.set(sheet.name.toLowerCase(), sheet);
|
|
173
|
+
this.parser = new FormulaParser({
|
|
174
|
+
onCell: (ref) => this.parserCell(ref),
|
|
175
|
+
onRange: (ref) => this.parserRange(ref),
|
|
176
|
+
functions: createSheetFunctions({ value: CATALOG.VALUE, na: CATALOG.NA, num: CATALOG.NUM, div0: CATALOG.DIV0, ref: CATALOG.REF }),
|
|
177
|
+
});
|
|
178
|
+
this.depParser = new FormulaParser.DepParser({ onVariable: () => undefined });
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Resolve every cell a formula references before the formula parser starts.
|
|
182
|
+
* fast-formula-parser parses synchronously and is not reentrant, so a formula
|
|
183
|
+
* cell reached through a resolver would start a nested parse. Preparing the
|
|
184
|
+
* dependencies first keeps every parse call sequential.
|
|
185
|
+
* @param source the stored formula.
|
|
186
|
+
* @param sheetName the sheet the formula lives on.
|
|
187
|
+
* @param row the one-based row.
|
|
188
|
+
* @param column the zero-based column.
|
|
189
|
+
*/
|
|
190
|
+
prepareDependencies(source, sheetName, row, column) {
|
|
191
|
+
const body = source.startsWith('=') ? source.slice(1) : source;
|
|
192
|
+
let dependencies;
|
|
193
|
+
try {
|
|
194
|
+
dependencies = this.depParser.parse(body, { row, col: column + 1, sheet: sheetName });
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
if (!Array.isArray(dependencies))
|
|
200
|
+
return;
|
|
201
|
+
for (const dependency of dependencies) {
|
|
202
|
+
if (dependency === null || typeof dependency !== 'object')
|
|
203
|
+
continue;
|
|
204
|
+
const record = { ...dependency };
|
|
205
|
+
const depSheet = typeof record.sheet === 'string' ? record.sheet : sheetName;
|
|
206
|
+
if (typeof record.row === 'number' && typeof record.col === 'number') {
|
|
207
|
+
this.resultOf(depSheet, record.row, record.col - 1);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
const from = record.from;
|
|
211
|
+
const to = record.to;
|
|
212
|
+
if (from === null || typeof from !== 'object' || to === null || typeof to !== 'object')
|
|
213
|
+
continue;
|
|
214
|
+
const fromRow = from.row;
|
|
215
|
+
const fromCol = from.col;
|
|
216
|
+
const toRow = to.row;
|
|
217
|
+
const toCol = to.col;
|
|
218
|
+
if (typeof fromRow !== 'number' || typeof fromCol !== 'number' || typeof toRow !== 'number' || typeof toCol !== 'number')
|
|
219
|
+
continue;
|
|
220
|
+
const firstRow = Math.max(1, fromRow);
|
|
221
|
+
const lastRow = Math.min(SHEET_LIMITS.maxRows, toRow);
|
|
222
|
+
const firstColumn = Math.max(0, fromCol - 1);
|
|
223
|
+
const lastColumn = Math.min(SHEET_LIMITS.maxColumns - 1, toCol - 1);
|
|
224
|
+
const size = (lastRow - firstRow + 1) * (lastColumn - firstColumn + 1);
|
|
225
|
+
if (!this.spend(size))
|
|
226
|
+
return;
|
|
227
|
+
for (let depRow = firstRow; depRow <= lastRow; depRow += 1) {
|
|
228
|
+
for (let depColumn = firstColumn; depColumn <= lastColumn; depColumn += 1)
|
|
229
|
+
this.resultOf(depSheet, depRow, depColumn);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
spend(cells = 1) {
|
|
234
|
+
this.budget -= cells;
|
|
235
|
+
if (this.budget >= 0)
|
|
236
|
+
return true;
|
|
237
|
+
this.flags.limit = true;
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
parserCell(ref) {
|
|
241
|
+
const sheetName = ref.sheet ?? this.workbook.sheets[0]?.name ?? '';
|
|
242
|
+
const column = Math.max(0, ref.col - 1);
|
|
243
|
+
if (this.unevaluatedFormula(sheetName, ref.row, column)) {
|
|
244
|
+
this.flags.unresolved = true;
|
|
245
|
+
return libraryError('#REF!');
|
|
246
|
+
}
|
|
247
|
+
// The parser uses one-based columns; the workbook model uses zero-based ones.
|
|
248
|
+
return this.toParserValue(this.resultOf(sheetName, ref.row, column), ref.sheet !== undefined);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Whether a reference names a formula cell that has not been prepared yet.
|
|
252
|
+
* That can only happen through a function the dependency parser cannot see,
|
|
253
|
+
* such as INDIRECT.
|
|
254
|
+
* @param sheetName the sheet.
|
|
255
|
+
* @param row the one-based row.
|
|
256
|
+
* @param column the zero-based column.
|
|
257
|
+
* @returns whether starting that formula now would nest a parse.
|
|
258
|
+
*/
|
|
259
|
+
unevaluatedFormula(sheetName, row, column) {
|
|
260
|
+
const sheet = this.sheets.get(sheetName.toLowerCase());
|
|
261
|
+
if (sheet === undefined)
|
|
262
|
+
return false;
|
|
263
|
+
const cell = sheet.cells[formatCellRef(row, column)];
|
|
264
|
+
if (cell === undefined || cell.formula === undefined)
|
|
265
|
+
return false;
|
|
266
|
+
return !this.memo.has(sheet.name.toLowerCase() + '!' + formatCellRef(row, column));
|
|
267
|
+
}
|
|
268
|
+
parserRange(ref) {
|
|
269
|
+
const sheet = ref.sheet ?? this.workbook.sheets[0]?.name ?? '';
|
|
270
|
+
const firstRow = Math.max(1, ref.from.row);
|
|
271
|
+
const lastRow = Math.min(SHEET_LIMITS.maxRows, ref.to.row);
|
|
272
|
+
const firstColumn = Math.max(0, ref.from.col - 1);
|
|
273
|
+
const lastColumn = Math.min(SHEET_LIMITS.maxColumns - 1, ref.to.col - 1);
|
|
274
|
+
if (lastRow < firstRow || lastColumn < firstColumn)
|
|
275
|
+
return [];
|
|
276
|
+
if (!this.spend((lastRow - firstRow + 1) * (lastColumn - firstColumn + 1)))
|
|
277
|
+
return [[libraryError('#REF!')]];
|
|
278
|
+
const rows = [];
|
|
279
|
+
for (let row = firstRow; row <= lastRow; row += 1) {
|
|
280
|
+
const cells = [];
|
|
281
|
+
for (let column = firstColumn; column <= lastColumn; column += 1)
|
|
282
|
+
cells.push(this.toParserValue(this.resultOf(sheet, row, column), false));
|
|
283
|
+
rows.push(cells);
|
|
284
|
+
}
|
|
285
|
+
return rows;
|
|
286
|
+
}
|
|
287
|
+
toParserValue(outcome, fromReference) {
|
|
288
|
+
void fromReference;
|
|
289
|
+
if (!isError(outcome))
|
|
290
|
+
return outcome.value;
|
|
291
|
+
if (this.flags.referenceError === undefined)
|
|
292
|
+
this.flags.referenceError = outcome;
|
|
293
|
+
if (outcome.error === '#CYCLE!') {
|
|
294
|
+
this.flags.cycle = true;
|
|
295
|
+
return libraryError('#REF!');
|
|
296
|
+
}
|
|
297
|
+
if (outcome.error === '#LIMIT!') {
|
|
298
|
+
this.flags.limit = true;
|
|
299
|
+
return libraryError('#REF!');
|
|
300
|
+
}
|
|
301
|
+
if (outcome.error === '#SYNTAX!') {
|
|
302
|
+
if (this.flags.syntax === undefined)
|
|
303
|
+
this.flags.syntax = outcome.message ?? 'A referenced formula has invalid syntax.';
|
|
304
|
+
return libraryError('#REF!');
|
|
305
|
+
}
|
|
306
|
+
return libraryError(outcome.error);
|
|
307
|
+
}
|
|
308
|
+
evaluateFormula(source, sheet, row, column) {
|
|
309
|
+
const body = source.startsWith('=') ? source.slice(1) : source;
|
|
310
|
+
try {
|
|
311
|
+
const raw = this.parser.parse(body, { row, col: column + 1, sheet });
|
|
312
|
+
return this.outcomeOf(raw);
|
|
313
|
+
}
|
|
314
|
+
catch (error) {
|
|
315
|
+
const message = error instanceof Error ? error.message : 'The formula could not be parsed.';
|
|
316
|
+
const code = errorCodeOf(error);
|
|
317
|
+
if (code !== undefined)
|
|
318
|
+
return { error: normaliseCode(code, message), message: message.split('\n')[0] ?? message };
|
|
319
|
+
if (/not implemented/i.test(message))
|
|
320
|
+
return { error: '#NAME?', message };
|
|
321
|
+
return { error: '#SYNTAX!', message: message.split('\n')[0] ?? message };
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
outcomeOf(raw) {
|
|
325
|
+
if (raw === null || raw === undefined)
|
|
326
|
+
return { value: null };
|
|
327
|
+
if (typeof raw === 'number')
|
|
328
|
+
return Number.isFinite(raw) ? { value: raw } : { error: '#NUM!', message: 'The result is not a finite number.' };
|
|
329
|
+
if (typeof raw === 'string' || typeof raw === 'boolean')
|
|
330
|
+
return { value: raw };
|
|
331
|
+
if (Array.isArray(raw))
|
|
332
|
+
return { error: '#VALUE!', message: 'Array formulas are not supported in a sheet artifact.' };
|
|
333
|
+
const code = errorCodeOf(raw);
|
|
334
|
+
if (code !== undefined) {
|
|
335
|
+
const message = errorMessageOf(raw);
|
|
336
|
+
return { error: normaliseCode(code, message), message: message.split('\n')[0] ?? message };
|
|
337
|
+
}
|
|
338
|
+
return { error: '#VALUE!', message: 'The formula returned a value this build cannot show.' };
|
|
339
|
+
}
|
|
340
|
+
resultOf(sheetName, row, column) {
|
|
341
|
+
const sheet = this.sheets.get(sheetName.toLowerCase());
|
|
342
|
+
if (sheet === undefined)
|
|
343
|
+
return { error: '#REF!', message: 'There is no sheet named "' + sheetName + '".' };
|
|
344
|
+
const ref = formatCellRef(row, column);
|
|
345
|
+
const key = sheet.name.toLowerCase() + '!' + ref;
|
|
346
|
+
const memo = this.memo.get(key);
|
|
347
|
+
if (memo !== undefined)
|
|
348
|
+
return memo;
|
|
349
|
+
if (this.visiting.has(key)) {
|
|
350
|
+
this.flags.cycle = true;
|
|
351
|
+
return { error: '#CYCLE!', message: 'The formula refers back to its own cell.' };
|
|
352
|
+
}
|
|
353
|
+
if (!this.spend())
|
|
354
|
+
return { error: '#LIMIT!', message: 'The formula calculation is too large.' };
|
|
355
|
+
const cell = sheet.cells[ref];
|
|
356
|
+
if (cell === undefined || cell.formula === undefined) {
|
|
357
|
+
const literal = { value: cell === undefined ? null : cell.value ?? null };
|
|
358
|
+
this.memo.set(key, literal);
|
|
359
|
+
return literal;
|
|
360
|
+
}
|
|
361
|
+
// Each cell evaluates with its own flags. A sibling cell inside a range
|
|
362
|
+
// must not inherit this cell's cycle or limit state.
|
|
363
|
+
const parentFlags = this.flags;
|
|
364
|
+
this.flags = emptyFlags();
|
|
365
|
+
this.visiting.add(key);
|
|
366
|
+
let outcome;
|
|
367
|
+
try {
|
|
368
|
+
this.prepareDependencies(cell.formula, sheet.name, row, column);
|
|
369
|
+
outcome = this.evaluateFormula(cell.formula, sheet.name, row, column);
|
|
370
|
+
if (this.flags.cycle)
|
|
371
|
+
outcome = { error: '#CYCLE!', message: 'The formula refers back to its own cell.' };
|
|
372
|
+
else if (this.flags.limit)
|
|
373
|
+
outcome = { error: '#LIMIT!', message: 'The formula calculation is too large.' };
|
|
374
|
+
else if (this.flags.unresolved)
|
|
375
|
+
outcome = { error: '#REF!', message: 'A dynamic reference points at a formula this build could not resolve first.' };
|
|
376
|
+
else if (isError(outcome) && this.flags.syntax !== undefined)
|
|
377
|
+
outcome = { error: '#SYNTAX!', message: this.flags.syntax };
|
|
378
|
+
else if (!isError(outcome) && this.flags.referenceError !== undefined && isTopLevelAggregate(cell.formula))
|
|
379
|
+
outcome = this.flags.referenceError;
|
|
380
|
+
}
|
|
381
|
+
finally {
|
|
382
|
+
this.visiting.delete(key);
|
|
383
|
+
this.flags = parentFlags;
|
|
384
|
+
}
|
|
385
|
+
this.memo.set(key, outcome);
|
|
386
|
+
return outcome;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Evaluate every cell in the workbook.
|
|
390
|
+
* @returns evaluated cells and advisory warnings.
|
|
391
|
+
*/
|
|
392
|
+
run() {
|
|
393
|
+
const results = {};
|
|
394
|
+
const warnings = [];
|
|
395
|
+
for (const sheet of this.workbook.sheets) {
|
|
396
|
+
const sheetResults = {};
|
|
397
|
+
results[sheet.name] = sheetResults;
|
|
398
|
+
for (const entry of Object.entries(sheet.cells)) {
|
|
399
|
+
const ref = entry[0];
|
|
400
|
+
const cell = entry[1];
|
|
401
|
+
const index = ref === undefined ? undefined : parseIndex(ref);
|
|
402
|
+
if (index === undefined)
|
|
403
|
+
continue;
|
|
404
|
+
const outcome = this.resultOf(sheet.name, index.row, index.column);
|
|
405
|
+
if (isError(outcome) && cell.formula !== undefined && warnings.length < 8) {
|
|
406
|
+
const detail = outcome.message === undefined ? '' : outcome.message.replace(/\.$/, '');
|
|
407
|
+
warnings.push('formula-error: ' + sheet.name + '!' + ref + ' shows ' + outcome.error + ' for "' + cell.formula + '"' +
|
|
408
|
+
(detail === '' ? '.' : '; ' + detail + '.'));
|
|
409
|
+
}
|
|
410
|
+
sheetResults[ref] = {
|
|
411
|
+
value: isError(outcome) ? null : outcome.value,
|
|
412
|
+
...(isError(outcome) ? { error: outcome.error } : {}),
|
|
413
|
+
...(isError(outcome) && outcome.message !== undefined ? { message: outcome.message } : {}),
|
|
414
|
+
display: isError(outcome) ? outcome.error : formatScalar(outcome.value, cell.style?.numberFormat),
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return { results, warnings };
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const AGGREGATE = /^=\s*(?:SUM|AVERAGE|MIN|MAX|PRODUCT|MEDIAN)\s*\(/i;
|
|
422
|
+
/**
|
|
423
|
+
* Whether a formula is a plain aggregate of references. The parser skips an
|
|
424
|
+
* errored cell inside a range for these functions, where Excel reports the
|
|
425
|
+
* error, so the wrapper restores the Excel result. A formula that names
|
|
426
|
+
* IFERROR, IF, or ISERROR is left to the parser because it handles the error
|
|
427
|
+
* on purpose.
|
|
428
|
+
* @param formula the stored formula.
|
|
429
|
+
* @returns whether to propagate a referenced error.
|
|
430
|
+
*/
|
|
431
|
+
function isTopLevelAggregate(formula) {
|
|
432
|
+
if (!AGGREGATE.test(formula))
|
|
433
|
+
return false;
|
|
434
|
+
return !/\b(?:IFERROR|IF|ISERROR|ISNA)\s*\(/i.test(formula);
|
|
435
|
+
}
|
|
436
|
+
function parseIndex(ref) {
|
|
437
|
+
const match = /^([A-Z]{1,3})([1-9][0-9]{0,6})$/.exec(ref);
|
|
438
|
+
if (match === null)
|
|
439
|
+
return undefined;
|
|
440
|
+
const letters = match[1] ?? '';
|
|
441
|
+
let column = 0;
|
|
442
|
+
for (const letter of letters)
|
|
443
|
+
column = column * 26 + (letter.charCodeAt(0) - 64);
|
|
444
|
+
return { row: Number(match[2]), column: column - 1 };
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Evaluate every cell of a workbook.
|
|
448
|
+
* @param workbook the workbook.
|
|
449
|
+
* @returns the evaluated cells and advisory warnings.
|
|
450
|
+
*/
|
|
451
|
+
export function evaluateWorkbook(workbook) {
|
|
452
|
+
return new Evaluator(workbook).run();
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* The display text of a cell, following its stored number format.
|
|
456
|
+
* @param cell the stored cell, when any.
|
|
457
|
+
* @param result the evaluated result, when any.
|
|
458
|
+
* @returns the text the panel and the exports show.
|
|
459
|
+
*/
|
|
460
|
+
export function displayCell(cell, result) {
|
|
461
|
+
if (result === undefined)
|
|
462
|
+
return '';
|
|
463
|
+
if (result.error !== undefined)
|
|
464
|
+
return result.error;
|
|
465
|
+
return formatScalar(result.value, cell?.style?.numberFormat);
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Whether a library error object is one of the standard codes.
|
|
469
|
+
* @param value the value.
|
|
470
|
+
* @returns the code, when it is an error.
|
|
471
|
+
*/
|
|
472
|
+
export function formulaErrorOf(value) {
|
|
473
|
+
const code = errorCodeOf(value);
|
|
474
|
+
return code === undefined ? undefined : normaliseCode(code, errorMessageOf(value));
|
|
475
|
+
}
|