Skip to content

Commit 75b2b07

Browse files
committed
Update sheet strategy in AddressMapping if placeholder exists
1 parent db9ebe0 commit 75b2b07

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/DependencyGraph/AddressMapping/AddressMapping.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface AddressMappingAddSheetOptions {
2525
/**
2626
* Manages cell vertices and provides access to vertex by SimpleCellAddress.
2727
* For each sheet it stores vertices according to AddressMappingStrategy: DenseStrategy or SparseStrategy.
28+
* It also stores placeholder entries (DenseStrategy) for sheets that are used in formulas but not yet added.
2829
*/
2930
export class AddressMapping {
3031
private mapping: Map<number, AddressMappingStrategy> = new Map()
@@ -92,6 +93,35 @@ export class AddressMapping {
9293
return strategy
9394
}
9495

96+
/**
97+
* Adds a sheet or changes the strategy for an existing sheet.
98+
*/
99+
public addSheetOrChangeStrategy(sheetId: number, sheetBoundaries: SheetBoundaries): AddressMappingStrategy {
100+
const newStrategy = this.createStrategyBasedOnBoundaries(sheetBoundaries)
101+
const strategyPlaceholder = this.mapping.get(sheetId)
102+
103+
if (!strategyPlaceholder) {
104+
this.mapping.set(sheetId, newStrategy)
105+
return newStrategy
106+
}
107+
108+
if (newStrategy instanceof DenseStrategy) { // new startegy is the same as the placeholder
109+
return strategyPlaceholder
110+
}
111+
112+
this.moveStrategyContent(strategyPlaceholder, newStrategy, sheetId)
113+
this.mapping.set(sheetId, newStrategy)
114+
115+
return newStrategy
116+
}
117+
118+
private moveStrategyContent(sourceStrategy: AddressMappingStrategy, targetStrategy: AddressMappingStrategy, sheetContext: number) {
119+
const sourceVertices = sourceStrategy.getEntries(sheetContext)
120+
for (const [address, vertex] of sourceVertices) {
121+
targetStrategy.setCell(address, vertex)
122+
}
123+
}
124+
95125
/**
96126
* Adds a sheet and sets the strategy based on the sheet boundaries.
97127
* @param {number} sheetId - The sheet identifier
@@ -100,13 +130,17 @@ export class AddressMapping {
100130
* @throws {Error} if sheet doesn't exist and throwIfSheetNotExists is true
101131
*/
102132
public addSheetAndSetStrategyBasedOnBounderies(sheetId: number, sheetBoundaries: SheetBoundaries, options: AddressMappingAddSheetOptions = { throwIfSheetNotExists: true }) {
133+
this.addSheetWithStrategy(sheetId, this.createStrategyBasedOnBoundaries(sheetBoundaries), options)
134+
}
135+
136+
private createStrategyBasedOnBoundaries(sheetBoundaries: SheetBoundaries): AddressMappingStrategy {
103137
const {height, width, fill} = sheetBoundaries
104138
const strategyConstructor = this.policy.call(fill)
105-
this.addSheetWithStrategy(sheetId, new strategyConstructor(width, height), options)
139+
return new strategyConstructor(width, height)
106140
}
107141

108142
/**
109-
* Adds a placeholder strategy for a sheet. If the sheet already exists, does nothing.
143+
* Adds a placeholder strategy (DenseStrategy) for a sheet. If the sheet already exists, does nothing.
110144
* @param {number} sheetId - The sheet identifier
111145
*/
112146
public addSheetStrategyPlaceholderIfNotExists(sheetId: number): void {

src/DependencyGraph/DependencyGraph.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export class DependencyGraph {
290290
}
291291

292292
public addSheet(sheetId: number) {
293-
this.addressMapping.addSheetAndSetStrategyBasedOnBounderies(sheetId, findBoundaries([]), { throwIfSheetNotExists: false })
293+
this.addressMapping.addSheetOrChangeStrategy(sheetId, findBoundaries([]))
294294

295295
for (const [_, vertex] of this.addressMapping.sheetEntries(sheetId)) {
296296
this.graph.markNodeAsDirty(vertex)

0 commit comments

Comments
 (0)