@@ -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'
@@ -480,8 +480,6 @@ export class Operations {
480480
481481 /**
482482 * Restores a single cell.
483- * @param {SimpleCellAddress } address
484- * @param {ClipboardCell } clipboardCell
485483 */
486484 public restoreCell ( address : SimpleCellAddress , clipboardCell : ClipboardCell ) : void {
487485 switch ( clipboardCell . type ) {
@@ -591,7 +589,6 @@ export class Operations {
591589
592590 this . setFormulaToCell ( address , size , parserResult )
593591 } catch ( error ) {
594-
595592 if ( ! ( error as Error ) . message ) {
596593 throw error
597594 }
@@ -619,45 +616,66 @@ export class Operations {
619616 }
620617 }
621618
619+ /**
620+ * Sets cell content to an instance of parsing error.
621+ * Creates a ParsingErrorVertex and updates the dependency graph and column search index.
622+ */
622623 public setParsingErrorToCell ( rawInput : string , errors : ParsingError [ ] , address : SimpleCellAddress ) {
623- const oldValue = this . dependencyGraph . getCellValue ( address )
624+ this . removeCellValueFromColumnSearch ( address )
625+
624626 const vertex = new ParsingErrorVertex ( errors , rawInput )
625627 const arrayChanges = this . dependencyGraph . setParsingErrorToCell ( address , vertex )
626- this . columnSearch . remove ( getRawValue ( oldValue ) , address )
628+
627629 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) )
628630 this . changes . addAll ( arrayChanges )
629631 this . changes . addChange ( vertex . getCellValue ( ) , address )
630632 }
631633
634+ /**
635+ * Sets cell content to a formula.
636+ * Creates a FormulaCellVertex and updates the dependency graph and column search index.
637+ */
632638 public setFormulaToCell ( address : SimpleCellAddress , size : ArraySize , {
633639 ast,
634640 hasVolatileFunction,
635641 hasStructuralChangeFunction,
636642 dependencies
637643 } : ParsingResult ) {
638- const oldValue = this . dependencyGraph . getCellValue ( address )
644+ this . removeCellValueFromColumnSearch ( address )
645+
639646 const arrayChanges = this . dependencyGraph . setFormulaToCell ( address , ast , absolutizeDependencies ( dependencies , address ) , size , hasVolatileFunction , hasStructuralChangeFunction )
640- this . columnSearch . remove ( getRawValue ( oldValue ) , address )
647+
641648 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) )
642649 this . changes . addAll ( arrayChanges )
643650 }
644651
652+ /**
653+ * Sets cell content to a value.
654+ * Creates a ValueCellVertex and updates the dependency graph and column search index.
655+ */
645656 public setValueToCell ( value : RawAndParsedValue , address : SimpleCellAddress ) {
646- const oldValue = this . dependencyGraph . getCellValue ( address )
657+ this . changeCellValueInColumnSearch ( address , value . parsedValue )
658+
647659 const arrayChanges = this . dependencyGraph . setValueToCell ( address , value )
648- this . columnSearch . change ( getRawValue ( oldValue ) , getRawValue ( value . parsedValue ) , address )
660+
649661 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) . filter ( change => ! equalSimpleCellAddress ( change . address , address ) ) )
650662 this . changes . addAll ( arrayChanges )
651663 this . changes . addChange ( value . parsedValue , address )
652664 }
653665
666+ /**
667+ * Sets cell content to an empty value.
668+ * Creates an EmptyCellVertex and updates the dependency graph and column search index.
669+ */
654670 public setCellEmpty ( address : SimpleCellAddress ) {
655671 if ( this . dependencyGraph . isArrayInternalCell ( address ) ) {
656672 return
657673 }
658- const oldValue = this . dependencyGraph . getCellValue ( address )
674+
675+ this . removeCellValueFromColumnSearch ( address )
676+
659677 const arrayChanges = this . dependencyGraph . setCellEmpty ( address )
660- this . columnSearch . remove ( getRawValue ( oldValue ) , address )
678+
661679 this . columnSearch . applyChanges ( arrayChanges . getChanges ( ) )
662680 this . changes . addAll ( arrayChanges )
663681 this . changes . addChange ( EmptyValue , address )
@@ -944,6 +962,45 @@ export class Operations {
944962 }
945963 return this . dependencyGraph . fetchCellOrCreateEmpty ( expression . address ) . vertex
946964 }
965+
966+ /**
967+ * Removes a cell value from the columnSearch index.
968+ * Ignores the non-computed formula vertices.
969+ */
970+ private removeCellValueFromColumnSearch ( address : SimpleCellAddress ) : void {
971+ if ( this . isNotComputed ( address ) ) {
972+ return
973+ }
974+
975+ const oldValue = this . dependencyGraph . getCellValue ( address )
976+ this . columnSearch . remove ( getRawValue ( oldValue ) , address )
977+ }
978+
979+ /**
980+ * Changes a cell value in the columnSearch index.
981+ * Ignores the non-computed formula vertices.
982+ */
983+ private changeCellValueInColumnSearch ( address : SimpleCellAddress , newValue : ValueCellVertexValue ) : void {
984+ if ( this . isNotComputed ( address ) ) {
985+ return
986+ }
987+
988+ const oldValue = this . dependencyGraph . getCellValue ( address )
989+ this . columnSearch . change ( getRawValue ( oldValue ) , getRawValue ( newValue ) , address )
990+ }
991+
992+ /**
993+ * Checks if the FormulaCellVertex or ArrayVertex at the given address is not computed.
994+ */
995+ private isNotComputed ( address : SimpleCellAddress ) : boolean {
996+ const vertex = this . dependencyGraph . getCell ( address )
997+
998+ if ( ! vertex ) {
999+ return false
1000+ }
1001+
1002+ return 'isComputed' in vertex && ! vertex . isComputed ( )
1003+ }
9471004}
9481005
9491006export function normalizeRemovedIndexes ( indexes : ColumnRowIndex [ ] ) : ColumnRowIndex [ ] {
0 commit comments