diff --git a/src/AbsoluteCellRange.ts b/src/AbsoluteCellRange.ts index 8bb5e4bc65..003995ef73 100644 --- a/src/AbsoluteCellRange.ts +++ b/src/AbsoluteCellRange.ts @@ -83,6 +83,18 @@ export class AbsoluteCellRange { return false } + public arrayOfAddressesInRange(): SimpleCellAddress[][] { + const result: SimpleCellAddress[][] = [] + for (let y = 0; y < this.height(); ++y) { + result[y] = [] + for (let x = 0; x < this.width(); ++x) { + const value = simpleCellAddress(this.sheet, this.start.col + x, this.start.row + y) + result[y].push(value) + } + } + return result + } + public containsRange(range: AbsoluteCellRange): boolean { return this.addressInRange(range.start) && this.addressInRange(range.end) } diff --git a/src/DependencyGraph/DependencyGraph.ts b/src/DependencyGraph/DependencyGraph.ts index 5a62c61cc3..62344514f2 100644 --- a/src/DependencyGraph/DependencyGraph.ts +++ b/src/DependencyGraph/DependencyGraph.ts @@ -536,20 +536,6 @@ export class DependencyGraph { return this.rangeMapping.getRange(start, end) } - public getValuesInRange(range: AbsoluteCellRange): InternalCellValue[][] { - const result: InternalCellValue[][] = [] - - for (let y = 0; y < range.height(); ++y) { - result[y] = [] - for (let x = 0; x < range.width(); ++x) { - const value = this.getCellValue(simpleCellAddress(range.sheet, range.start.col + x, range.start.row + y)) - result[y].push(value) - } - } - - return result - } - public topSortWithScc(): TopSortResult { return this.graph.topSortWithScc() } diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 6d637f81ec..0cc307c6ae 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -5,7 +5,6 @@ import { CellValueType, getCellType, getCellValueType, - InternalCellValue, simpleCellAddress, SimpleCellAddress, } from './Cell' @@ -232,7 +231,7 @@ export class HyperFormula { * Returns map containing dimensions of all sheets. * */ - public getSheetsDimensions(): Record { + public getAllSheetsDimensions(): Record { return this.genericAllGetter((...args) => this.getSheetDimensions(...args)) } @@ -252,7 +251,7 @@ export class HyperFormula { * Returns map containing values of all sheets. * */ - public getSheetsValues(): Record { + public getAllSheetsValues(): Record { return this.genericAllGetter((...args) => this.getSheetValues(...args)) } @@ -260,7 +259,7 @@ export class HyperFormula { * Returns map containing formulas of all sheets. * */ - public getSheetsFormulas(): Record[][]> { + public getAllSheetsFormulas(): Record[][]> { return this.genericAllGetter((...args) => this.getSheetFormulas(...args)) } @@ -268,7 +267,7 @@ export class HyperFormula { * Returns map containing formulas or values of all sheets. * */ - public getSheetsSerialized(): Record { + public getAllSheetsSerialized(): Record { return this.genericAllGetter((...args) => this.getSheetSerialized(...args)) } @@ -569,9 +568,9 @@ export class HyperFormula { * @param width - width of the cell block being copied * @param height - height of the cell block being copied * */ - public copy(sourceLeftCorner: SimpleCellAddress, width: number, height: number): InternalCellValue[][] { + public copy(sourceLeftCorner: SimpleCellAddress, width: number, height: number): CellValue[][] { this.crudOperations.copy(sourceLeftCorner, width, height) - return this.getValuesInRange(AbsoluteCellRange.spanFrom(sourceLeftCorner, width, height)) + return this.getRangeValues(AbsoluteCellRange.spanFrom(sourceLeftCorner, width, height)) } /** @@ -584,9 +583,9 @@ export class HyperFormula { * @param width - width of the cell block being copied * @param height - height of the cell block being copied * */ - public cut(sourceLeftCorner: SimpleCellAddress, width: number, height: number): InternalCellValue[][] { + public cut(sourceLeftCorner: SimpleCellAddress, width: number, height: number): CellValue[][] { this.crudOperations.cut(sourceLeftCorner, width, height) - return this.getValuesInRange(AbsoluteCellRange.spanFrom(sourceLeftCorner, width, height)) + return this.getRangeValues(AbsoluteCellRange.spanFrom(sourceLeftCorner, width, height)) } /** @@ -613,11 +612,37 @@ export class HyperFormula { * * @param range */ - public getValuesInRange(range: AbsoluteCellRange): InternalCellValue[][] { - return this.dependencyGraph.getValuesInRange(range).map( - (subarray: InternalCellValue[]) => subarray.map( - (arg) => this.exporter.exportValue(arg), - ), + public getRangeValues(range: AbsoluteCellRange): CellValue[][] { + return range.arrayOfAddressesInRange().map( + (subarray) => subarray.map( + (address) => this.getCellValue(address) + ) + ) + } + + /** + * Returns cell formulas in given range + * + * @param range + */ + public getRangeFormulas(range: AbsoluteCellRange): Maybe[][] { + return range.arrayOfAddressesInRange().map( + (subarray) => subarray.map( + (address) => this.getCellFormula(address) + ) + ) + } + + /** + * Returns serialized cell in given range + * + * @param range + */ + public getRangeSerialized(range: AbsoluteCellRange): CellValue[][] { + return range.arrayOfAddressesInRange().map( + (subarray) => subarray.map( + (address) => this.getCellSerialized(address) + ) ) } diff --git a/test/build-engine-from-arrays.spec.ts b/test/build-engine-from-arrays.spec.ts index 500fa84923..ce774a65ba 100644 --- a/test/build-engine-from-arrays.spec.ts +++ b/test/build-engine-from-arrays.spec.ts @@ -32,6 +32,6 @@ describe('Building engine from arrays', () => { const config = new Config() const engine = HyperFormula.buildFromArray([], config) - expect(engine.getSheetsDimensions()).toEqual({'Sheet1': {'height': 0, 'width': 0}}) + expect(engine.getAllSheetsDimensions()).toEqual({'Sheet1': {'height': 0, 'width': 0}}) }) }) diff --git a/test/engine.spec.ts b/test/engine.spec.ts index 016a19a1bb..b445be1a79 100644 --- a/test/engine.spec.ts +++ b/test/engine.spec.ts @@ -41,7 +41,7 @@ describe('Integration', () => { Foo: [[1]], }) - expect(engine.getSheetsValues()).toEqual({'Foo': [[1]], 'Sheet1': []}) + expect(engine.getAllSheetsValues()).toEqual({'Foo': [[1]], 'Sheet1': []}) }) it('getAllFormulas', () => { @@ -50,7 +50,7 @@ describe('Integration', () => { Foo: [[1]], }) - expect(engine.getSheetsFormulas()).toEqual({'Foo': [[undefined]], 'Sheet1': [['=A()']]}) + expect(engine.getAllSheetsFormulas()).toEqual({'Foo': [[undefined]], 'Sheet1': [['=A()']]}) }) it('getAllSerialized', () => { @@ -59,7 +59,7 @@ describe('Integration', () => { Foo: [[1]], }) - expect(engine.getSheetsSerialized()).toEqual({'Foo': [[1]], 'Sheet1': [['=A()']]}) + expect(engine.getAllSheetsSerialized()).toEqual({'Foo': [[1]], 'Sheet1': [['=A()']]}) }) it('handle different input types', () => {