Skip to content
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed error caused by the named expression names that start with a cell reference. [#1058](https://github.com/handsontable/hyperformula/issues/1058)
- Fixed the validation of the named expression names. Strings `R4C5`, `RC1000`, `R1C`, `RC`, etc. are now considered invalid. (cell references in R1C1 style). [#1058](https://github.com/handsontable/hyperformula/issues/1058)
- Fixed error caused by removing a sheet with a cell reference to a value in the same sheet. [#1121](https://github.com/handsontable/hyperformula/issues/1121)

## [2.3.0] - 2022-12-22

Expand Down
18 changes: 2 additions & 16 deletions src/DependencyGraph/DependencyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,28 +291,16 @@ export class DependencyGraph {
}

public removeSheet(removedSheetId: number) {
const arrays: Set<ArrayVertex> = new Set()
this.clearSheet(removedSheetId)
Comment thread
adamu-handsontable marked this conversation as resolved.
Resolved

for (const [adr, vertex] of this.addressMapping.sheetEntries(removedSheetId)) {
if (vertex instanceof ArrayVertex) {
if (arrays.has(vertex)) {
continue
} else {
arrays.add(vertex)
}
}
for (const adjacentNode of this.graph.adjacentNodes(vertex)) {
this.graph.markNodeAsSpecialRecentlyChanged(adjacentNode)
}
this.removeVertex(vertex)
this.addressMapping.removeCell(adr)
}

this.stats.measure(StatType.ADJUSTING_ARRAY_MAPPING, () => {
for (const array of arrays.values()) {
this.arrayMapping.removeArray(array.getRange())
}
})

Comment on lines -310 to -315

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is no longer necessary because clearSheet also removes array vertices.

this.stats.measure(StatType.ADJUSTING_RANGES, () => {
const rangesToRemove = this.rangeMapping.removeRangesInSheet(removedSheetId)
for (const range of rangesToRemove) {
Expand All @@ -323,8 +311,6 @@ export class DependencyGraph {
this.addressMapping.removeSheet(removedSheetId)
})
})

this.addStructuralNodesToChangeSet()
}

public clearSheet(sheetId: number) {
Expand Down
32 changes: 32 additions & 0 deletions test/cruds/removing-sheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ describe('remove sheet', () => {
expect(engine.sheetMapping.numberOfSheets()).toBe(0)
expect(Array.from(engine.addressMapping.entries())).toEqual([])
})

it('should remove a sheet with a cell reference to a value in the same sheet', () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be a good idea to check if by removing one sheet, we're not affecting values in another sheet which is referenced. This does not have to be a scope of this PR, but maybe it's worth checking if it makes sense:

  it('should not remove data from cell referenced in another sheet', () => {
    const engine = HyperFormula.buildFromSheets({
      Sheet1: [
        ['=Sheet2!A1)'],
      ],
      Sheet2: [
        ['1'],
      ],
    })

    engine.removeSheet(0)

    expect(Array.from(engine.sheetMapping.displayNames())).toEqual(['Sheet2'])
    expect(engine.getCellValue(adr('A1', 1))).toBe(1)
  })

  it('should not remove data from cell range referenced in another sheet', () => {
    const engine = HyperFormula.buildFromSheets({
      Sheet1: [
        ['=SUM(Sheet2!A1:A2)'],
      ],
      Sheet2: [
        ['1'],
        ['5'],
      ],
    })

    engine.removeSheet(0)

    expect(Array.from(engine.sheetMapping.displayNames())).toEqual(['Sheet2'])
    expect(engine.getCellValue(adr('A1', 1))).toBe(1)
    expect(engine.getCellValue(adr('A2', 1))).toBe(5)
  })

Let me know what you think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, these test cases are absolutely valid. I added an it that covers both of them

const engine = HyperFormula.buildFromArray([
[1, '=A1'],
])

engine.removeSheet(0)

expect(engine.sheetMapping.numberOfSheets()).toBe(0)
expect(Array.from(engine.addressMapping.entries())).toEqual([])
})

it('should not affect data in the sheets that were referenced by the sheet being removed', () => {
const engine = HyperFormula.buildFromSheets({
Sheet1: [
['=Sheet2!A1)'],
['=SUM(Sheet2!A1:A2)'],
],
Sheet2: [
[1],
[2],
[3],
],
})

engine.removeSheet(0)

expect(Array.from(engine.sheetMapping.displayNames())).toEqual(['Sheet2'])
expect(engine.getCellValue(adr('A1', 1))).toBe(1)
expect(engine.getCellValue(adr('A2', 1))).toBe(2)
expect(engine.getCellValue(adr('A3', 1))).toBe(3)
})
})

describe('remove sheet - adjust edges', () => {
Expand Down