-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSerialization.ts
162 lines (141 loc) · 5.91 KB
/
Serialization.ts
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
/**
* @license
* Copyright (c) 2024 Handsoncode. All rights reserved.
*/
import {simpleCellAddress, SimpleCellAddress} from './Cell'
import {RawCellContent} from './CellContentParser'
import {CellValue} from './CellValue'
import {Config} from './Config'
import {ArrayVertex, DependencyGraph, FormulaCellVertex, ParsingErrorVertex} from './DependencyGraph'
import {Exporter} from './Exporter'
import {Maybe} from './Maybe'
import {NamedExpressionOptions, NamedExpressions} from './NamedExpressions'
import {buildLexerConfig, ProcedureAst, Unparser} from './parser'
export interface SerializedNamedExpression {
name: string,
expression: RawCellContent,
scope?: number,
options?: NamedExpressionOptions,
}
export class Serialization {
constructor(
private readonly dependencyGraph: DependencyGraph,
private readonly unparser: Unparser,
private readonly exporter: Exporter
) {
}
public getCellHyperlink(address: SimpleCellAddress): Maybe<string> {
const formulaVertex = this.dependencyGraph.getCell(address)
if (formulaVertex instanceof FormulaCellVertex) {
const formula = formulaVertex.getFormula(this.dependencyGraph.lazilyTransformingAstService) as ProcedureAst
if ('HYPERLINK' === formula.procedureName) {
return formula.hyperlink
}
}
return undefined
}
public getCellFormula(address: SimpleCellAddress, targetAddress?: SimpleCellAddress): Maybe<string> {
const formulaVertex = this.dependencyGraph.getCell(address)
if (formulaVertex instanceof FormulaCellVertex) {
const formula = formulaVertex.getFormula(this.dependencyGraph.lazilyTransformingAstService)
targetAddress = targetAddress ?? address
return this.unparser.unparse(formula, targetAddress)
} else if (formulaVertex instanceof ArrayVertex) {
const arrayVertexAddress = formulaVertex.getAddress(this.dependencyGraph.lazilyTransformingAstService)
if (arrayVertexAddress.row !== address.row || arrayVertexAddress.col !== address.col || arrayVertexAddress.sheet !== address.sheet) {
return undefined
}
targetAddress = targetAddress ?? address
const formula = formulaVertex.getFormula(this.dependencyGraph.lazilyTransformingAstService)
if (formula !== undefined) {
return this.unparser.unparse(formula, targetAddress)
}
} else if (formulaVertex instanceof ParsingErrorVertex) {
return formulaVertex.getFormula()
}
return undefined
}
public getCellSerialized(address: SimpleCellAddress, targetAddress?: SimpleCellAddress): RawCellContent {
return this.getCellFormula(address, targetAddress) ?? this.getRawValue(address)
}
public getCellValue(address: SimpleCellAddress): CellValue {
return this.exporter.exportValue(this.dependencyGraph.getScalarValue(address))
}
public getRawValue(address: SimpleCellAddress): RawCellContent {
return this.dependencyGraph.getRawValue(address)
}
public getSheetValues(sheet: number): CellValue[][] {
return this.genericSheetGetter(sheet, (arg) => this.getCellValue(arg))
}
public getSheetFormulas(sheet: number): Maybe<string>[][] {
return this.genericSheetGetter(sheet, (arg) => this.getCellFormula(arg))
}
public genericSheetGetter<T>(sheet: number, getter: (address: SimpleCellAddress) => T): T[][] {
const sheetHeight = this.dependencyGraph.getSheetHeight(sheet)
const sheetWidth = this.dependencyGraph.getSheetWidth(sheet)
const arr: T[][] = new Array(sheetHeight)
for (let i = 0; i < sheetHeight; i++) {
arr[i] = new Array(sheetWidth)
for (let j = 0; j < sheetWidth; j++) {
const address = simpleCellAddress(sheet, j, i)
arr[i][j] = getter(address)
}
for (let j = sheetWidth - 1; j >= 0; j--) {
if (arr[i][j] === null || arr[i][j] === undefined) {
arr[i].pop()
} else {
break
}
}
}
for (let i = sheetHeight - 1; i >= 0; i--) {
if (arr[i].length === 0) {
arr.pop()
} else {
break
}
}
return arr
}
public genericAllSheetsGetter<T>(sheetGetter: (sheet: number) => T): Record<string, T> {
const result: Record<string, T> = {}
for (const sheetName of this.dependencyGraph.sheetMapping.displayNames()) {
const sheetId = this.dependencyGraph.sheetMapping.fetch(sheetName)
result[sheetName] = sheetGetter(sheetId)
}
return result
}
public getSheetSerialized(sheet: number): RawCellContent[][] {
return this.genericSheetGetter(sheet, (arg) => this.getCellSerialized(arg))
}
public getAllSheetsValues(): Record<string, CellValue[][]> {
return this.genericAllSheetsGetter((arg) => this.getSheetValues(arg))
}
public getAllSheetsFormulas(): Record<string, Maybe<string>[][]> {
return this.genericAllSheetsGetter((arg) => this.getSheetFormulas(arg))
}
public getAllSheetsSerialized(): Record<string, RawCellContent[][]> {
return this.genericAllSheetsGetter((arg) => this.getSheetSerialized(arg))
}
public getAllNamedExpressionsSerialized(): SerializedNamedExpression[] {
const idMap: number[] = []
let id = 0
for (const sheetName of this.dependencyGraph.sheetMapping.displayNames()) {
const sheetId = this.dependencyGraph.sheetMapping.fetch(sheetName)
idMap[sheetId] = id
id++
}
return this.dependencyGraph.namedExpressions.getAllNamedExpressions().map((entry) => {
return {
name: entry.expression.displayName,
expression: this.getCellSerialized(entry.expression.address),
scope: entry.scope !== undefined ? idMap[entry.scope] : undefined,
options: entry.expression.options
}
})
}
public withNewConfig(newConfig: Config, namedExpressions: NamedExpressions): Serialization {
const newUnparser = new Unparser(newConfig, buildLexerConfig(newConfig), this.dependencyGraph.sheetMapping.fetchDisplayName, namedExpressions)
return new Serialization(this.dependencyGraph, newUnparser, this.exporter)
}
}