-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathFormulaVertex.ts
More file actions
285 lines (240 loc) · 8.04 KB
/
Copy pathFormulaVertex.ts
File metadata and controls
285 lines (240 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* @license
* Copyright (c) 2025 Handsoncode. All rights reserved.
*/
import {AbsoluteCellRange} from '../AbsoluteCellRange'
import {ArraySize} from '../ArraySize'
import {ArrayValue, ErroredArray, CellArray, NotComputedArray} from '../ArrayValue'
import {CellError, equalSimpleCellAddress, ErrorType, SimpleCellAddress} from '../Cell'
import {RawCellContent} from '../CellContentParser'
import {ErrorMessage} from '../error-message'
import {EmptyValue, getRawValue, InternalScalarValue, InterpreterValue} from '../interpreter/InterpreterValue'
import {LazilyTransformingAstService} from '../LazilyTransformingAstService'
import {Maybe} from '../Maybe'
import {Ast} from '../parser'
import {ColumnsSpan, RowsSpan} from '../Span'
import {CellVertex} from './CellVertex'
/**
* Abstract base class for vertices that contain formulas in the dependency graph.
*
* Stores formula AST, cell address, and version for lazy transformation support.
* Has two concrete implementations: {@link ScalarFormulaVertex} for single-cell formulas
* and {@link ArrayFormulaVertex} for array formulas that span multiple cells.
*/
export abstract class FormulaVertex extends CellVertex {
protected constructor(
protected formula: Ast,
protected cellAddress: SimpleCellAddress,
public version: number
) {
super()
}
public get width(): number {
return 1
}
public get height(): number {
return 1
}
static fromAst(formula: Ast, address: SimpleCellAddress, size: ArraySize, version: number) {
if (size.isScalar()) {
return new ScalarFormulaVertex(formula, address, version)
} else {
return new ArrayFormulaVertex(formula, address, size, version)
}
}
/**
* Returns formula stored in this vertex
*/
public getFormula(updatingService: LazilyTransformingAstService): Ast {
this.ensureRecentData(updatingService)
return this.formula
}
public ensureRecentData(updatingService: LazilyTransformingAstService) {
if (this.version != updatingService.version()) {
const [newAst, newAddress, newVersion] = updatingService.applyTransformations(this.formula, this.cellAddress, this.version)
this.formula = newAst
this.cellAddress = newAddress
this.version = newVersion
}
}
/**
* Returns address of the cell associated with vertex
*/
public getAddress(updatingService: LazilyTransformingAstService): SimpleCellAddress {
this.ensureRecentData(updatingService)
return this.cellAddress
}
/**
* Sets computed cell value stored in this vertex
*/
public abstract setCellValue(cellValue: InterpreterValue): InterpreterValue
/**
* Returns cell value stored in vertex
*/
public abstract getCellValue(): InterpreterValue
public abstract valueOrUndef(): Maybe<InterpreterValue>
public abstract isComputed(): boolean
}
/**
* Represents a formula vertex that produces an array result spanning multiple cells.
*
* Array formulas are transformed eagerly (unlike scalar formulas) and store their
* computed values in a {@link CellArray} structure.
*/
export class ArrayFormulaVertex extends FormulaVertex {
array: CellArray
constructor(formula: Ast, cellAddress: SimpleCellAddress, size: ArraySize, version: number = 0) {
super(formula, cellAddress, version)
if (size.isRef) {
this.array = new ErroredArray(new CellError(ErrorType.REF, ErrorMessage.NoSpaceForArrayResult), ArraySize.error())
} else {
this.array = new NotComputedArray(size)
}
}
get width(): number {
return this.array.width()
}
get height(): number {
return this.array.height()
}
get sheet(): number {
return this.cellAddress.sheet
}
get leftCorner(): SimpleCellAddress {
return this.cellAddress
}
setCellValue(value: InterpreterValue): InterpreterValue {
if (value instanceof CellError) {
this.setErrorValue(value)
return value
}
const array = ArrayValue.fromInterpreterValue(value)
array.resize(this.array.size)
this.array = array
return value
}
getCellValue(): InterpreterValue {
if (this.array instanceof NotComputedArray) {
throw Error('Array not computed yet.')
}
return this.array.simpleRangeValue()
}
public valueOrUndef(): Maybe<InterpreterValue> {
if (this.array instanceof NotComputedArray) {
return undefined
}
return this.array.simpleRangeValue()
}
getArrayCellValue(address: SimpleCellAddress): InternalScalarValue {
const col = address.col - this.cellAddress.col
const row = address.row - this.cellAddress.row
try {
return this.array.get(col, row)
} catch (e) {
return new CellError(ErrorType.REF)
}
}
getArrayCellRawValue(address: SimpleCellAddress): Maybe<RawCellContent> {
const val = this.getArrayCellValue(address)
if (val instanceof CellError || val === EmptyValue) {
return undefined
} else {
return getRawValue(val)
}
}
setArrayCellValue(address: SimpleCellAddress, value: number): void {
const col = address.col - this.cellAddress.col
const row = address.row - this.cellAddress.row
if (this.array instanceof ArrayValue) {
this.array.set(col, row, value)
}
}
setNoSpace(): InterpreterValue {
this.array = new ErroredArray(new CellError(ErrorType.SPILL, ErrorMessage.NoSpaceForArrayResult), ArraySize.error())
return this.getCellValue()
}
getRange(): AbsoluteCellRange {
return AbsoluteCellRange.spanFrom(this.cellAddress, this.width, this.height)
}
getRangeOrUndef(): Maybe<AbsoluteCellRange> {
return AbsoluteCellRange.spanFromOrUndef(this.cellAddress, this.width, this.height)
}
setAddress(address: SimpleCellAddress) {
this.cellAddress = address
}
setFormula(newFormula: Ast) {
this.formula = newFormula
}
spansThroughSheetRows(sheet: number, startRow: number, endRow: number = startRow): boolean {
return (this.cellAddress.sheet === sheet) &&
(this.cellAddress.row <= endRow) &&
(startRow < this.cellAddress.row + this.height)
}
spansThroughSheetColumn(sheet: number, col: number, columnEnd: number = col): boolean {
return (this.cellAddress.sheet === sheet) &&
(this.cellAddress.col <= columnEnd) &&
(col < this.cellAddress.col + this.width)
}
isComputed() {
return (!(this.array instanceof NotComputedArray))
}
columnsFromArray() {
return ColumnsSpan.fromNumberOfColumns(this.cellAddress.sheet, this.cellAddress.col, this.width)
}
rowsFromArray() {
return RowsSpan.fromNumberOfRows(this.cellAddress.sheet, this.cellAddress.row, this.height)
}
/**
* No-op as array vertices are transformed eagerly.
*/
ensureRecentData(_updatingService: LazilyTransformingAstService) {
}
isLeftCorner(address: SimpleCellAddress): boolean {
return equalSimpleCellAddress(this.cellAddress, address)
}
private setErrorValue(error: CellError) {
this.array = new ErroredArray(error, this.array.size)
}
}
/**
* Represents a formula vertex that produces a single scalar value.
*
* Unlike {@link ArrayFormulaVertex}, scalar formulas are transformed lazily
* and cache their computed value for retrieval.
*/
export class ScalarFormulaVertex extends FormulaVertex {
/** Most recently computed value of this formula. */
private cachedCellValue?: InterpreterValue
constructor(
/** Formula in AST format */
formula: Ast,
/** Address which this vertex represents */
address: SimpleCellAddress,
version: number,
) {
super(formula, address, version)
}
public valueOrUndef(): Maybe<InterpreterValue> {
return this.cachedCellValue
}
/**
* Sets computed cell value stored in this vertex
*/
public setCellValue(cellValue: InterpreterValue): InterpreterValue {
this.cachedCellValue = cellValue
return this.cachedCellValue
}
/**
* Returns cell value stored in vertex
*/
public getCellValue(): InterpreterValue {
if (this.cachedCellValue !== undefined) {
return this.cachedCellValue
} else {
throw Error('Value of the formula cell is not computed.')
}
}
public isComputed() {
return (this.cachedCellValue !== undefined)
}
}