Skip to content

Commit 120df3c

Browse files
committed
Refactor
1 parent 87ede30 commit 120df3c

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

src/DependencyGraph/DependencyGraph.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,6 @@ export class DependencyGraph {
308308
})
309309
}
310310

311-
public addSheet(sheetName: string): void {
312-
// TODO
313-
}
314-
315311
public clearSheet(sheetId: number) {
316312
const arrays: Set<ArrayVertex> = new Set()
317313
for (const [address, vertex] of this.addressMapping.sheetEntries(sheetId)) {
@@ -519,6 +515,9 @@ export class DependencyGraph {
519515
this.setAddressMappingForArrayVertex(vertex, address)
520516
}
521517

518+
/**
519+
* TODO
520+
*/
522521
public* arrayFormulaNodes(): IterableIterator<ArrayVertex> {
523522
for (const vertex of this.graph.getNodes()) {
524523
if (vertex instanceof ArrayVertex) {
@@ -527,6 +526,9 @@ export class DependencyGraph {
527526
}
528527
}
529528

529+
/**
530+
* TODO
531+
*/
530532
public* formulaNodes(): IterableIterator<FormulaVertex> {
531533
for (const vertex of this.graph.getNodes()) {
532534
if (vertex instanceof FormulaVertex) {

src/Operations.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,51 @@ export class Operations {
245245
this.columnSearch.removeSheet(sheetId)
246246
}
247247

248+
/**
249+
* TODO
250+
*/
248251
public addSheet(name?: string): { addedSheetName: string, version: number } {
249252
const sheetId = this.sheetMapping.addSheet(name)
250-
const sheet: Sheet = []
251-
this.dependencyGraph.addressMapping.autoAddSheet(sheetId, findBoundaries(sheet))
252253
const addedSheetName = this.sheetMapping.fetchDisplayName(sheetId)
253-
const quotedSheetName = `'${addedSheetName.replace(/'/g, "''")}'`
254+
this.dependencyGraph.addressMapping.autoAddSheet(sheetId, findBoundaries([]))
255+
this.reparseFormulasDependingOnSheet(addedSheetName)
256+
return { addedSheetName, version: 0 } // TODO: calculate version
257+
}
254258

255-
// TODO: move this code to dependencyGraph.addSheet()
256-
this.stats.measure(StatType.TRANSFORM_ASTS, () => {
257-
for (const node of this.dependencyGraph.formulaNodes()) {
258-
const ast = node.getFormula(this.dependencyGraph.lazilyTransformingAstService)
259-
if (ast.type === AstNodeType.ERROR_WITH_RAW_INPUT && this.containsSheetName(ast.rawInput, addedSheetName, quotedSheetName)) {
260-
const address = node.getAddress(this.dependencyGraph.lazilyTransformingAstService)
261-
this.setCellContent(address, `=${ast.rawInput}`) // or maybe take just the inner part of this function body
259+
/**
260+
* TODO
261+
*/
262+
private reparseFormulasDependingOnSheet(sheetName: string): void {
263+
const quotedSheetName = `'${sheetName.replace(/'/g, "''")}'`
264+
for (const node of this.dependencyGraph.formulaNodes()) {
265+
const ast = node.getFormula(this.dependencyGraph.lazilyTransformingAstService)
266+
if (ast.type === AstNodeType.ERROR_WITH_RAW_INPUT && this.containsSheetName(ast.rawInput, sheetName, quotedSheetName)) {
267+
const address = node.getAddress(this.dependencyGraph.lazilyTransformingAstService)
268+
const formulaText = `=${ast.rawInput}`
269+
const parserResult = this.parser.parse(formulaText, address)
270+
const { ast: newAst, errors } = parserResult
271+
if (errors.length > 0) {
272+
this.setParsingErrorToCell(formulaText, errors, address)
273+
} else {
274+
try {
275+
const size = this.arraySizePredictor.checkArraySize(newAst, address)
276+
277+
if (size.width <= 0 || size.height <= 0) {
278+
throw Error('Incorrect array size')
279+
}
280+
281+
this.setFormulaToCell(address, size, parserResult)
282+
} catch (error) {
283+
if (!(error as Error).message) {
284+
throw error
285+
}
286+
287+
const parsingError: ParsingError = { type: ParsingErrorType.InvalidRangeSize, message: 'Invalid range size.' }
288+
this.setParsingErrorToCell(formulaText, [parsingError], address)
289+
}
262290
}
263291
}
264-
})
265-
266-
return { addedSheetName, version: 0 } // TODO: calculate version
292+
}
267293
}
268294

269295
private containsSheetName(rawInput: string, unquotedSheetNameLowercase: string, quotedSheetNameLowercase: string): boolean {

src/dependencyTransformers/RemoveSheetTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class RemoveSheetTransformer extends Transformer {
2424
public performEagerTransformations(graph: DependencyGraph, parser: ParserWithCaching): void {
2525
for (const node of graph.arrayFormulaNodes()) {
2626
const [newAst] = this.transformSingleAst(node.getFormula(graph.lazilyTransformingAstService), node.getAddress(graph.lazilyTransformingAstService))
27-
const cachedAst = parser.rememberNewAst(newAst)
27+
const cachedAst = parser.rememberNewAst(newAst) // TODO: check if this is needed
2828
node.setFormula(cachedAst)
2929
}
3030
}

0 commit comments

Comments
 (0)