@@ -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 {
0 commit comments