Skip to content

Commit db595e7

Browse files
committed
Refactor Operations
1 parent 4370a02 commit db595e7

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/DependencyGraph/DependencyGraph.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -659,17 +659,6 @@ export class DependencyGraph {
659659
}
660660
}
661661

662-
/**
663-
* Iterator over all formula nodes in the graph.
664-
*/
665-
public* formulaNodes(): IterableIterator<FormulaVertex> {
666-
for (const vertex of this.graph.getNodes()) {
667-
if (vertex instanceof FormulaVertex) {
668-
yield vertex
669-
}
670-
}
671-
}
672-
673662
public* entriesFromRowsSpan(rowsSpan: RowsSpan): IterableIterator<[SimpleCellAddress, CellVertex]> {
674663
yield* this.addressMapping.entriesFromRowsSpan(rowsSpan)
675664
}
@@ -686,24 +675,32 @@ export class DependencyGraph {
686675
return this.addressMapping.getCell(address)
687676
}
688677

678+
/**
679+
* Checks if the given sheet ID refers to a placeholder sheet (doesn't exist but is referenced by other sheets)
680+
*/
681+
private isPlaceholder(sheetId: number): boolean {
682+
return sheetId !== NamedExpressions.SHEET_FOR_WORKBOOK_EXPRESSIONS &&
683+
!this.sheetMapping.hasSheetWithId(sheetId, { includePlaceholders: false })
684+
}
685+
689686
public getCellValue(address: SimpleCellAddress): InterpreterValue {
690-
if (address.sheet !== NamedExpressions.SHEET_FOR_WORKBOOK_EXPRESSIONS && !this.sheetMapping.hasSheetWithId(address.sheet, { includePlaceholders: false })) {
687+
if (this.isPlaceholder(address.sheet)) {
691688
return new CellError(ErrorType.REF, ErrorMessage.SheetRef)
692689
}
693690

694691
return this.addressMapping.getCellValue(address)
695692
}
696693

697694
public getRawValue(address: SimpleCellAddress): RawCellContent {
698-
if (address.sheet !== NamedExpressions.SHEET_FOR_WORKBOOK_EXPRESSIONS && !this.sheetMapping.hasSheetWithId(address.sheet, { includePlaceholders: false })) {
695+
if (this.isPlaceholder(address.sheet)) {
699696
return null
700697
}
701698

702699
return this.addressMapping.getRawValue(address)
703700
}
704701

705702
public getScalarValue(address: SimpleCellAddress): InternalScalarValue {
706-
if (address.sheet !== NamedExpressions.SHEET_FOR_WORKBOOK_EXPRESSIONS && !this.sheetMapping.hasSheetWithId(address.sheet, { includePlaceholders: false })) {
703+
if (this.isPlaceholder(address.sheet)) {
707704
return new CellError(ErrorType.REF, ErrorMessage.SheetRef)
708705
}
709706

@@ -1258,7 +1255,7 @@ export class DependencyGraph {
12581255

12591256
const hasDependentInExistingSheet = dependents.some(addr => {
12601257
if (isSimpleCellAddress(addr)) {
1261-
return addr.sheet === NamedExpressions.SHEET_FOR_WORKBOOK_EXPRESSIONS || this.sheetMapping.hasSheetWithId(addr.sheet, { includePlaceholders: false })
1258+
return !this.isPlaceholder(addr.sheet)
12621259
}
12631260

12641261
if (isSimpleCellRange(addr)) {

src/Operations.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,26 @@ export class Operations {
217217
return columnsRemovals
218218
}
219219

220+
/**
221+
* Clears the sheet content.
222+
*/
220223
public clearSheet(sheetId: number) {
221224
this.dependencyGraph.clearSheet(sheetId)
222225
this.columnSearch.removeSheet(sheetId)
223226
}
224227

228+
/**
229+
* Adds a new sheet to the workbook.
230+
*/
225231
public addSheet(name?: string): string {
226232
const sheetId = this.sheetMapping.addSheet(name)
227233
this.dependencyGraph.addSheet(sheetId)
228234
return this.sheetMapping.getSheetNameOrThrowError(sheetId)
229235
}
230236

237+
/**
238+
* Removes a sheet from the workbook.
239+
*/
231240
public removeSheet(sheetId: number): { version: number, scopedNamedExpressions: [InternalNamedExpression, ClipboardCell][] } {
232241
this.dependencyGraph.removeSheet(sheetId)
233242
this.columnSearch.removeSheet(sheetId)
@@ -237,12 +246,18 @@ export class Operations {
237246
return { version: 0, scopedNamedExpressions }
238247
}
239248

249+
/**
250+
* Removes a sheet from the workbook by name.
251+
*/
240252
public removeSheetByName(sheetName: string) {
241253
const sheetId = this.sheetMapping.getSheetIdOrThrowError(sheetName)
242254
return this.removeSheet(sheetId)
243255
}
244256

245-
public renameSheet(sheetId: number, newName: string): Maybe<string> { // TODO: refactor
257+
/**
258+
* Renames a sheet in the workbook.
259+
*/
260+
public renameSheet(sheetId: number, newName: string): Maybe<string> {
246261
const { previousDisplayName, mergedSheetWith } = this.sheetMapping.renameSheet(sheetId, newName)
247262

248263
if (mergedSheetWith !== undefined) {

0 commit comments

Comments
 (0)