@@ -22,10 +22,10 @@ import {
2222 ParsingErrorVertex ,
2323 SheetMapping ,
2424 SparseStrategy ,
25- ValueCellVertex
25+ ValueCellVertex ,
2626} from './DependencyGraph'
2727import { FormulaVertex } from './DependencyGraph/FormulaCellVertex'
28- import { RawAndParsedValue } from './DependencyGraph/ValueCellVertex'
28+ import { RawAndParsedValue , ValueCellVertexValue } from './DependencyGraph/ValueCellVertex'
2929import { AddColumnsTransformer } from './dependencyTransformers/AddColumnsTransformer'
3030import { AddRowsTransformer } from './dependencyTransformers/AddRowsTransformer'
3131import { CleanOutOfScopeDependenciesTransformer } from './dependencyTransformers/CleanOutOfScopeDependenciesTransformer'
@@ -459,8 +459,6 @@ export class Operations {
459459
460460 /**
461461 * Restores a single cell.
462- * @param {SimpleCellAddress } address
463- * @param {ClipboardCell } clipboardCell
464462 */
465463 public restoreCell ( address : SimpleCellAddress , clipboardCell : ClipboardCell ) : void {
466464 switch ( clipboardCell . type ) {
@@ -570,7 +568,6 @@ export class Operations {
570568
571569 this . setFormulaToCell ( address , size , parserResult )
572570 } catch ( error ) {
573-
574571 if ( ! ( error as Error ) . message ) {
575572 throw error
576573 }
@@ -598,45 +595,66 @@ export class Operations {
598595 }
599596 }
600597
598+ /**
599+ * Sets cell content to an instance of parsing error.
600+ * Creates a ParsingErrorVertex and updates the dependency graph and column search index.
601+ */
601602 public setParsingErrorToCell ( rawInput : string , errors : ParsingError [ ] , address : SimpleCellAddress ) {
602- const oldValue = this . dependencyGraph . getCellValue ( address )
603+ this . removeCellValueFromColumnSearch ( address )
604+
603605 const vertex = new ParsingErrorVertex ( errors , rawInput )
604606 const arrayChanges = this . dependencyGraph . setParsingErrorToCell ( address , vertex )
605- this . columnSearch . remove ( getRawValue ( oldValue ) , address )
607+
606608 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) )
607609 this . changes . addAll ( arrayChanges )
608610 this . changes . addChange ( vertex . getCellValue ( ) , address )
609611 }
610612
613+ /**
614+ * Sets cell content to a formula.
615+ * Creates a FormulaCellVertex and updates the dependency graph and column search index.
616+ */
611617 public setFormulaToCell ( address : SimpleCellAddress , size : ArraySize , {
612618 ast,
613619 hasVolatileFunction,
614620 hasStructuralChangeFunction,
615621 dependencies
616622 } : ParsingResult ) {
617- const oldValue = this . dependencyGraph . getCellValue ( address )
623+ this . removeCellValueFromColumnSearch ( address )
624+
618625 const arrayChanges = this . dependencyGraph . setFormulaToCell ( address , ast , absolutizeDependencies ( dependencies , address ) , size , hasVolatileFunction , hasStructuralChangeFunction )
619- this . columnSearch . remove ( getRawValue ( oldValue ) , address )
626+
620627 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) )
621628 this . changes . addAll ( arrayChanges )
622629 }
623630
631+ /**
632+ * Sets cell content to a value.
633+ * Creates a ValueCellVertex and updates the dependency graph and column search index.
634+ */
624635 public setValueToCell ( value : RawAndParsedValue , address : SimpleCellAddress ) {
625- const oldValue = this . dependencyGraph . getCellValue ( address )
636+ this . changeCellValueInColumnSearch ( address , value . parsedValue )
637+
626638 const arrayChanges = this . dependencyGraph . setValueToCell ( address , value )
627- this . columnSearch . change ( getRawValue ( oldValue ) , getRawValue ( value . parsedValue ) , address )
639+
628640 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) . filter ( change => ! equalSimpleCellAddress ( change . address , address ) ) )
629641 this . changes . addAll ( arrayChanges )
630642 this . changes . addChange ( value . parsedValue , address )
631643 }
632644
645+ /**
646+ * Sets cell content to an empty value.
647+ * Creates an EmptyCellVertex and updates the dependency graph and column search index.
648+ */
633649 public setCellEmpty ( address : SimpleCellAddress ) {
634650 if ( this . dependencyGraph . isArrayInternalCell ( address ) ) {
635651 return
636652 }
637- const oldValue = this . dependencyGraph . getCellValue ( address )
653+
654+ this . removeCellValueFromColumnSearch ( address )
655+
638656 const arrayChanges = this . dependencyGraph . setCellEmpty ( address )
639- this . columnSearch . remove ( getRawValue ( oldValue ) , address )
657+
640658 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) )
641659 this . changes . addAll ( arrayChanges )
642660 this . changes . addChange ( EmptyValue , address )
@@ -923,6 +941,45 @@ export class Operations {
923941 }
924942 return this . dependencyGraph . fetchCellOrCreateEmpty ( expression . address ) . vertex
925943 }
944+
945+ /**
946+ * Removes a cell value from the columnSearch index.
947+ * Ignores the non-computed formula vertices.
948+ */
949+ private removeCellValueFromColumnSearch ( address : SimpleCellAddress ) : void {
950+ if ( this . isNotComputed ( address ) ) {
951+ return
952+ }
953+
954+ const oldValue = this . dependencyGraph . getCellValue ( address )
955+ this . columnSearch . remove ( getRawValue ( oldValue ) , address )
956+ }
957+
958+ /**
959+ * Changes a cell value in the columnSearch index.
960+ * Ignores the non-computed formula vertices.
961+ */
962+ private changeCellValueInColumnSearch ( address : SimpleCellAddress , newValue : ValueCellVertexValue ) : void {
963+ if ( this . isNotComputed ( address ) ) {
964+ return
965+ }
966+
967+ const oldValue = this . dependencyGraph . getCellValue ( address )
968+ this . columnSearch . change ( getRawValue ( oldValue ) , getRawValue ( newValue ) , address )
969+ }
970+
971+ /**
972+ * Checks if the FormulaCellVertex or ArrayVertex at the given address is not computed.
973+ */
974+ private isNotComputed ( address : SimpleCellAddress ) : boolean {
975+ const vertex = this . dependencyGraph . getCell ( address )
976+
977+ if ( ! vertex ) {
978+ return false
979+ }
980+
981+ return 'isComputed' in vertex && ! vertex . isComputed ( )
982+ }
926983}
927984
928985export function normalizeRemovedIndexes ( indexes : ColumnRowIndex [ ] ) : ColumnRowIndex [ ] {
0 commit comments