From 7ef857f9a592a9a5ff4decfadc7d23176b6faef0 Mon Sep 17 00:00:00 2001 From: izulin Date: Sun, 13 Dec 2020 15:23:16 +0100 Subject: [PATCH 01/36] setroworder --- src/CrudOperations.ts | 29 ++++++++++++++++++++++++++++- src/HyperFormula.ts | 5 +++++ src/Operations.ts | 17 +++++++++++++++++ src/UndoRedo.ts | 22 ++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 04410f9d90..54db847079 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -7,6 +7,7 @@ import {AbsoluteCellRange} from './AbsoluteCellRange' import {invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress} from './Cell' import {CellContent, CellContentParser, isMatrix, RawCellContent} from './CellContentParser' import {ClipboardCell, ClipboardOperations} from './ClipboardOperations' +import {ExportedChange} from './Exporter' import {AddColumnsCommand, AddRowsCommand, Operations, RemoveColumnsCommand, RemoveRowsCommand} from './Operations' import {ColumnSearchStrategy} from './Lookup/SearchStrategy' import {Config} from './Config' @@ -56,7 +57,7 @@ import { RemoveRowsUndoEntry, RemoveSheetUndoEntry, RenameSheetUndoEntry, - SetCellContentsUndoEntry, + SetCellContentsUndoEntry, SetRowOrderUndoEntry, SetSheetContentUndoEntry, UndoRedo } from './UndoRedo' @@ -293,6 +294,32 @@ export class CrudOperations { this.undoRedo.saveOperation(new SetSheetContentUndoEntry(sheetId, oldSheetContent, values)) } + public setRowOrder(sheetId: number, rowMapping: Map): void { + if (!this.sheetMapping.hasSheetWithId(sheetId)) { + throw new NoSheetWithIdError(sheetId) + } + this.validateRowMapping(sheetId, rowMapping) + this.operations.setRowOrder(sheetId, rowMapping) + this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) + } + + private validateRowMapping(sheetId: number, rowMapping: Map): void { + const sheetHeight = this.dependencyGraph.getSheetHeight(sheetId) + const values = Array.from(rowMapping.values()).sort((a,b) => a-b) + const keys = Array.from(rowMapping.keys()).sort((a,b) => a-b) + + for(let pos of keys) { + if(!isNonnegativeInteger(pos) || pos >= sheetHeight) { + throw new InvalidArgumentsError('row number to be nonnegative integer and less than sheet height.') + } + } + for(let i=0;i): ExportedChange[] { + this._crudOperations.setRowOrder(sheetId, rowMapping) + return this.recomputeIfDependencyGraphNeedsIt() + } + /** * Returns information whether it is possible to add rows into a specified position in a given sheet. * Checks against particular rules to ascertain that addRows can be called. diff --git a/src/Operations.ts b/src/Operations.ts index 7e70087050..45cc2cc548 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -309,6 +309,23 @@ export class Operations { } } + public setRowOrder(sheetId: number, rowMapping: Map): void { + const buffer: [SimpleCellAddress, ClipboardCell][][] = [] + for(const [source, target] of rowMapping.entries() ) { + if(source!==target) { + const row = this.getRangeClipboardCells(AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source},Infinity,1)) + buffer.push( + row.map( + ([{sheet, col, row}, cell]) => [{sheet, col, row: target},cell] + ) + ) + } + } + buffer.forEach( + row => this.restoreClipboardCells(sheetId, row.values()) + ) + } + public addNamedExpression(expressionName: string, expression: RawCellContent, sheetId?: number, options?: NamedExpressionOptions) { this.storeNamedExpressionInCell(this.namedExpressions.lookupNextAddress(expressionName, sheetId), expression) const namedExpression = this.namedExpressions.addNamedExpression(expressionName, sheetId, options) diff --git a/src/UndoRedo.ts b/src/UndoRedo.ts index 8c49d3beda..21f556c6f9 100644 --- a/src/UndoRedo.ts +++ b/src/UndoRedo.ts @@ -46,6 +46,14 @@ export class AddRowsUndoEntry { } } +export class SetRowOrderUndoEntry { + constructor( + public readonly sheetId: number, + public readonly rowMapping: Map + ) { + } +} + export class SetSheetContentUndoEntry { constructor( public readonly sheetId: number, @@ -220,6 +228,7 @@ type UndoStackEntry | AddNamedExpressionUndoEntry | RemoveNamedExpressionUndoEntry | ChangeNamedExpressionUndoEntry + | SetRowOrderUndoEntry export class UndoRedo { private undoStack: UndoStackEntry[] = [] @@ -333,6 +342,8 @@ export class UndoRedo { this.undoRemoveNamedExpression(operation) } else if (operation instanceof ChangeNamedExpressionUndoEntry) { this.undoChangeNamedExpression(operation) + } else if (operation instanceof SetRowOrderUndoEntry) { + this.undoSetRowOrder(operation) } else { throw 'Unknown element' } @@ -495,6 +506,11 @@ export class UndoRedo { this.operations.restoreNamedExpression(operation.namedExpression, operation.oldContent, operation.scope) } + private undoSetRowOrder(operation: SetRowOrderUndoEntry) { + const reverseMap = new Map(Array.from(operation.rowMapping, a => a.reverse()) as [number,number][]) + this.operations.setRowOrder(operation.sheetId, reverseMap) + } + public redo() { const operation = this.redoStack.pop() @@ -544,6 +560,8 @@ export class UndoRedo { this.redoRemoveNamedExpression(operation) } else if (operation instanceof ChangeNamedExpressionUndoEntry) { this.redoChangeNamedExpression(operation) + } else if (operation instanceof SetRowOrderUndoEntry) { + this.redoSetRowOrder(operation) } else { throw 'Unknown element' } @@ -634,6 +652,10 @@ export class UndoRedo { this.operations.changeNamedExpressionExpression(operation.namedExpression.displayName, operation.newContent, operation.scope, operation.options) } + private redoSetRowOrder(operation: SetRowOrderUndoEntry) { + this.operations.setRowOrder(operation.sheetId, operation.rowMapping) + } + private restoreOldDataFromVersion(version: number) { const oldDataToRestore = this.oldData.get(version) || [] for (const entryToRestore of oldDataToRestore) { From 9ed086f08c1e99e5919cdd6df1184ef4a01ddf38 Mon Sep 17 00:00:00 2001 From: izulin Date: Sun, 13 Dec 2020 22:19:28 +0100 Subject: [PATCH 02/36] undo-redo + tests --- src/CrudOperations.ts | 21 ++++--- src/HyperFormula.ts | 2 +- src/Operations.ts | 4 +- src/UndoRedo.ts | 4 +- test/cruds/change-cell-content.spec.ts | 4 +- test/cruds/set-row-order.spec.ts | 84 ++++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 test/cruds/set-row-order.spec.ts diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 54db847079..92e33156ce 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -294,7 +294,7 @@ export class CrudOperations { this.undoRedo.saveOperation(new SetSheetContentUndoEntry(sheetId, oldSheetContent, values)) } - public setRowOrder(sheetId: number, rowMapping: Map): void { + public setRowOrder(sheetId: number, rowMapping: [number, number][]): void { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } @@ -303,18 +303,23 @@ export class CrudOperations { this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) } - private validateRowMapping(sheetId: number, rowMapping: Map): void { + private validateRowMapping(sheetId: number, rowMapping: [number, number][]): void { const sheetHeight = this.dependencyGraph.getSheetHeight(sheetId) - const values = Array.from(rowMapping.values()).sort((a,b) => a-b) - const keys = Array.from(rowMapping.keys()).sort((a,b) => a-b) + const sources = rowMapping.map(([a,b])=>a).sort((a,b) => a-b) + const targets = rowMapping.map(([a,b])=>b).sort((a,b) => a-b) - for(let pos of keys) { + for(let pos of sources) { if(!isNonnegativeInteger(pos) || pos >= sheetHeight) { - throw new InvalidArgumentsError('row number to be nonnegative integer and less than sheet height.') + throw new InvalidArgumentsError('row numbers to be nonnegative integers and less than sheet height.') } } - for(let i=0;i): ExportedChange[] { + public setRowOrder(sheetId: number, rowMapping: [number, number][]): ExportedChange[] { this._crudOperations.setRowOrder(sheetId, rowMapping) return this.recomputeIfDependencyGraphNeedsIt() } diff --git a/src/Operations.ts b/src/Operations.ts index 45cc2cc548..e3b8a1e468 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -309,9 +309,9 @@ export class Operations { } } - public setRowOrder(sheetId: number, rowMapping: Map): void { + public setRowOrder(sheetId: number, rowMapping: [number, number][]): void { const buffer: [SimpleCellAddress, ClipboardCell][][] = [] - for(const [source, target] of rowMapping.entries() ) { + for(const [source, target] of rowMapping ) { if(source!==target) { const row = this.getRangeClipboardCells(AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source},Infinity,1)) buffer.push( diff --git a/src/UndoRedo.ts b/src/UndoRedo.ts index 21f556c6f9..5398e4a7ad 100644 --- a/src/UndoRedo.ts +++ b/src/UndoRedo.ts @@ -49,7 +49,7 @@ export class AddRowsUndoEntry { export class SetRowOrderUndoEntry { constructor( public readonly sheetId: number, - public readonly rowMapping: Map + public readonly rowMapping: [number, number][] ) { } } @@ -507,7 +507,7 @@ export class UndoRedo { } private undoSetRowOrder(operation: SetRowOrderUndoEntry) { - const reverseMap = new Map(Array.from(operation.rowMapping, a => a.reverse()) as [number,number][]) + const reverseMap = operation.rowMapping.map(([source,target]) => [target,source] as [number,number]) this.operations.setRowOrder(operation.sheetId, reverseMap) } diff --git a/test/cruds/change-cell-content.spec.ts b/test/cruds/change-cell-content.spec.ts index 7626c1ad82..fc3e273906 100644 --- a/test/cruds/change-cell-content.spec.ts +++ b/test/cruds/change-cell-content.spec.ts @@ -660,7 +660,7 @@ describe('change multiple cells contents', () => { expect(engine.getCellValue(adr('A1'))).toBe(1) }) - it('returns changes of mutliple values', () => { + it('returns changes of multiple values', () => { const sheet = [ ['1', '2'], ['3', '4'], @@ -674,7 +674,7 @@ describe('change multiple cells contents', () => { expectArrayWithSameContent(changes.map((change) => change.newValue), [7, 8, 9, 10]) }) - it('returns changes of mutliple values dependent formulas', () => { + it('returns changes of multiple values dependent formulas', () => { const sheet = [ ['1', '2'], ['3', '4'], diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts new file mode 100644 index 0000000000..5bfc5354b2 --- /dev/null +++ b/test/cruds/set-row-order.spec.ts @@ -0,0 +1,84 @@ +import {HyperFormula} from '../../src' +import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddressMappingPolicy' + +describe('setting row order - checking if it is possible', () => { + it('should validate numbers for negative rows', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(() => + engine.setRowOrder(0, [[-1,0]]) + ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') + }) + + it('should validate sources for noninteger values', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(() => + engine.setRowOrder(0, [[1,1],[0.5,0]]) + ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') + }) + + it('should validate sources for values exceeding sheet height', () => { + const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(() => + engine.setRowOrder(0, [[1,1],[3,0]]) + ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') + }) + + it('should validate sources to be unique', () => { + const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(() => + engine.setRowOrder(0, [[0,0], [1,1], [1,2]]) + ).toThrowError('Invalid arguments, expected source row numbers to be unique.') + }) + + it('should validate sources to be permutation of targets', () => { + const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(() => + engine.setRowOrder(0, [[0,0], [1,1], [2,1]]) + ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') + }) +}) + +describe('should correctly work', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1,'abcd',3], [3,5,true]]) + engine.setRowOrder(0, [[0,1],[1,0]]) + expect(engine.getSheetSerialized(0)).toEqual([[3,5,true],[1,'abcd',3]]) + }) + + it('should return number of changed cells', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6]]) + const ret = engine.setRowOrder(0, [[0,1],[1,0]]) + expect(ret.length).toEqual(6) + }) + + it('should work on static engine with uneven rows', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6,7,8]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + engine.setRowOrder(0, [[0,1],[1,0]]) + expect(engine.getSheetSerialized(0)).toEqual([[4,5,6,7,8],[1,2,3]]) + }) + + it('should work with more complicated permutations', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9]]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6]]) + }) + + it('should not move values unnecessarily', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6]]) + const ret = engine.setRowOrder(0, [[0,0],[1,1]]) + expect(ret.length).toEqual(0) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetValues(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], [7, 5]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8',8,9], ['=A3','=B3:B4',3], ['=A11','=B11:B16',6]]) + }) +}) From 2948c79ef39b96e0dad9f14bce72188051b04663 Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 14 Dec 2020 19:12:12 +0100 Subject: [PATCH 03/36] undo tests --- test/cruds/set-row-order.spec.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 5bfc5354b2..55bee72882 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -82,3 +82,26 @@ describe('should correctly work', () => { expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8',8,9], ['=A3','=B3:B4',3], ['=A11','=B11:B16',6]]) }) }) + +describe('reorder working with undo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9]]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1,2,3], [4,5,6], [7,8,9]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) + }) +}) From 1b7f8c40ac294e5a584bf0a4bad30f2a360746c4 Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 14 Dec 2020 19:23:46 +0100 Subject: [PATCH 04/36] extra logic --- src/CrudOperations.ts | 2 ++ test/cruds/set-row-order.spec.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 92e33156ce..ae3d52da41 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -299,6 +299,8 @@ export class CrudOperations { throw new NoSheetWithIdError(sheetId) } this.validateRowMapping(sheetId, rowMapping) + this.undoRedo.clearRedoStack() + this.clipboardOperations.abortCut() this.operations.setRowOrder(sheetId, rowMapping) this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) } diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 55bee72882..c3ea531503 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -105,3 +105,30 @@ describe('reorder working with undo', () => { expect(engine.getSheetSerialized(0)).toEqual([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) }) }) + +describe('reorder working with redo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9]]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + engine.undo() + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + engine.undo() + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetValues(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], [7, 5]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) + engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + engine.undo() + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8',8,9], ['=A3','=B3:B4',3], ['=A11','=B11:B16',6]]) + }) +}) From f4c64f6c7d5b19dabded11a1bfd1aef765547aef Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 14 Dec 2020 21:09:14 +0100 Subject: [PATCH 05/36] docs --- src/HyperFormula.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 638228b274..457fb1bef4 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1126,6 +1126,49 @@ export class HyperFormula implements TypedEmitter { return this.recomputeIfDependencyGraphNeedsIt() } + /** + * Reorders rows of a sheet according to a permutation. + * + * Note that this method may trigger dependency graph recalculation. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {[number, number][]} rowMapping - array mapping original positions to final positions of rows + * + * @fires [[valuesUpdated]] if recalculation was triggered by this change + * + * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist + * @throws an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1], + * [2], + * [4, 5], + * ]); + * + * // should set swap rows 0 and 2 in place, returns: + * // [{ + * // address: { sheet: 0, col: 0, row: 2 }, + * // newValue: 1, + * // }, + * // { + * // address: { sheet: 0, col: 1, row: 2 }, + * // newValue: null, + * // }, + * // { + * // address: { sheet: 0, col: 0, row: 0 }, + * // newValue: 4, + * // }, + * // { + * // address: { sheet: 0, col: 1, row: 0 }, + * // newValue: 5, + * // }] + * const changes = hfInstance.setRowOrder(0, [[0,2],[2,0]]); + * ``` + * + * @category Cells + */ public setRowOrder(sheetId: number, rowMapping: [number, number][]): ExportedChange[] { this._crudOperations.setRowOrder(sheetId, rowMapping) return this.recomputeIfDependencyGraphNeedsIt() From 77bdb967be43b962d73a5df57c8069121e6e9898 Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 14 Dec 2020 21:12:30 +0100 Subject: [PATCH 06/36] . --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2a673d21a..583cf55ac2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support for scientific notation. (#579) - Added 26 math functions COMPLEX, IMABS, IMAGINARY, IMARGUMENT, IMCONJUGATE, IMCOS, IMCOSH, IMCOT, IMCSC, IMCSCH, IMDIV, IMEXP, IMLN, IMLOG10, IMLOG2, IMPOWER, IMPRODUCT, IMREAL, IMSEC, IMSECH, IMSIN, IMSINH, IMSQRT, IMSUB, IMSUM, IMTAN. - Added support for complex numbers. (#281) +- Added support for row and column reordering. (#343) ### Changed - Changed CEILING function implementation to be consistent with existing implementations. (#582) From cbd46e6cf34925d4810567a5384742b6b37d3667 Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 15 Dec 2020 09:37:05 +0100 Subject: [PATCH 07/36] numeric matrices --- src/Operations.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Operations.ts b/src/Operations.ts index e3b8a1e468..b800891ead 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -313,7 +313,9 @@ export class Operations { const buffer: [SimpleCellAddress, ClipboardCell][][] = [] for(const [source, target] of rowMapping ) { if(source!==target) { - const row = this.getRangeClipboardCells(AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source},Infinity,1)) + const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source},Infinity,1) + this.dependencyGraph.breakNumericMatricesInRange(rowRange) + const row = this.getRangeClipboardCells(rowRange) buffer.push( row.map( ([{sheet, col, row}, cell]) => [{sheet, col, row: target},cell] From 68faa65c5670f3e7e604c23f2655a60711064583 Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 15 Dec 2020 09:39:09 +0100 Subject: [PATCH 08/36] linter --- src/CrudOperations.ts | 6 +-- src/Operations.ts | 4 +- src/UndoRedo.ts | 2 +- test/cruds/set-row-order.spec.ts | 88 ++++++++++++++++---------------- 4 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index ae3d52da41..b2163c1a6c 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -307,10 +307,10 @@ export class CrudOperations { private validateRowMapping(sheetId: number, rowMapping: [number, number][]): void { const sheetHeight = this.dependencyGraph.getSheetHeight(sheetId) - const sources = rowMapping.map(([a,b])=>a).sort((a,b) => a-b) - const targets = rowMapping.map(([a,b])=>b).sort((a,b) => a-b) + const sources = rowMapping.map(([a, b])=>a).sort((a, b) => a-b) + const targets = rowMapping.map(([a, b])=>b).sort((a, b) => a-b) - for(let pos of sources) { + for(const pos of sources) { if(!isNonnegativeInteger(pos) || pos >= sheetHeight) { throw new InvalidArgumentsError('row numbers to be nonnegative integers and less than sheet height.') } diff --git a/src/Operations.ts b/src/Operations.ts index b800891ead..ddcc79b2ff 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -313,12 +313,12 @@ export class Operations { const buffer: [SimpleCellAddress, ClipboardCell][][] = [] for(const [source, target] of rowMapping ) { if(source!==target) { - const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source},Infinity,1) + const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source}, Infinity, 1) this.dependencyGraph.breakNumericMatricesInRange(rowRange) const row = this.getRangeClipboardCells(rowRange) buffer.push( row.map( - ([{sheet, col, row}, cell]) => [{sheet, col, row: target},cell] + ([{sheet, col, row}, cell]) => [{sheet, col, row: target}, cell] ) ) } diff --git a/src/UndoRedo.ts b/src/UndoRedo.ts index 5398e4a7ad..cb8b18b259 100644 --- a/src/UndoRedo.ts +++ b/src/UndoRedo.ts @@ -507,7 +507,7 @@ export class UndoRedo { } private undoSetRowOrder(operation: SetRowOrderUndoEntry) { - const reverseMap = operation.rowMapping.map(([source,target]) => [target,source] as [number,number]) + const reverseMap = operation.rowMapping.map(([source, target]) => [target, source] as [number, number]) this.operations.setRowOrder(operation.sheetId, reverseMap) } diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index c3ea531503..a5e093d3f7 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -5,130 +5,130 @@ describe('setting row order - checking if it is possible', () => { it('should validate numbers for negative rows', () => { const engine = HyperFormula.buildFromArray([[]]) expect(() => - engine.setRowOrder(0, [[-1,0]]) + engine.setRowOrder(0, [[-1, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources for noninteger values', () => { const engine = HyperFormula.buildFromArray([[]]) expect(() => - engine.setRowOrder(0, [[1,1],[0.5,0]]) + engine.setRowOrder(0, [[1, 1], [0.5, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources for values exceeding sheet height', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) expect(() => - engine.setRowOrder(0, [[1,1],[3,0]]) + engine.setRowOrder(0, [[1, 1], [3, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources to be unique', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) expect(() => - engine.setRowOrder(0, [[0,0], [1,1], [1,2]]) + engine.setRowOrder(0, [[0, 0], [1, 1], [1, 2]]) ).toThrowError('Invalid arguments, expected source row numbers to be unique.') }) it('should validate sources to be permutation of targets', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) expect(() => - engine.setRowOrder(0, [[0,0], [1,1], [2,1]]) + engine.setRowOrder(0, [[0, 0], [1, 1], [2, 1]]) ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') }) }) describe('should correctly work', () => { it('should work on static engine', () => { - const engine = HyperFormula.buildFromArray([[1,'abcd',3], [3,5,true]]) - engine.setRowOrder(0, [[0,1],[1,0]]) - expect(engine.getSheetSerialized(0)).toEqual([[3,5,true],[1,'abcd',3]]) + const engine = HyperFormula.buildFromArray([[1, 'abcd', 3], [3, 5, true]]) + engine.setRowOrder(0, [[0, 1], [1, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[3, 5, true], [1, 'abcd', 3]]) }) it('should return number of changed cells', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6]]) - const ret = engine.setRowOrder(0, [[0,1],[1,0]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + const ret = engine.setRowOrder(0, [[0, 1], [1, 0]]) expect(ret.length).toEqual(6) }) it('should work on static engine with uneven rows', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6,7,8]], {chooseAddressMappingPolicy: new AlwaysSparse()}) - engine.setRowOrder(0, [[0,1],[1,0]]) - expect(engine.getSheetSerialized(0)).toEqual([[4,5,6,7,8],[1,2,3]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6, 7, 8]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + engine.setRowOrder(0, [[0, 1], [1, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[4, 5, 6, 7, 8], [1, 2, 3]]) }) it('should work with more complicated permutations', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9]]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) - expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) }) it('should not move values unnecessarily', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6]]) - const ret = engine.setRowOrder(0, [[0,0],[1,1]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + const ret = engine.setRowOrder(0, [[0, 0], [1, 1]]) expect(ret.length).toEqual(0) }) it('should work with external references', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) - expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], ['=A1', '=SUM(A2:A3)']]) - expect(engine.getSheetValues(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], [7, 5]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) }) it('should work with internal references', () => { - const engine = HyperFormula.buildFromArray([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) - expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8',8,9], ['=A3','=B3:B4',3], ['=A11','=B11:B16',6]]) + const engine = HyperFormula.buildFromArray([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8', 8, 9], ['=A3', '=B3:B4', 3], ['=A11', '=B11:B16', 6]]) }) }) describe('reorder working with undo', () => { it('should work on static engine', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9]]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.getSheetSerialized(0)).toEqual([[1,2,3], [4,5,6], [7,8,9]]) + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) }) it('should work with external references', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.getSheetSerialized(0)).toEqual([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) }) it('should work with internal references', () => { - const engine = HyperFormula.buildFromArray([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + const engine = HyperFormula.buildFromArray([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.getSheetSerialized(0)).toEqual([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) + expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) }) }) describe('reorder working with redo', () => { it('should work on static engine', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9]]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6]]) + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) }) it('should work with external references', () => { - const engine = HyperFormula.buildFromArray([[1,2,3], [4,5,6], [7,8,9], ['=A1', '=SUM(A2:A3)']]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], ['=A1', '=SUM(A2:A3)']]) - expect(engine.getSheetValues(0)).toEqual([[7,8,9], [1,2,3], [4,5,6], [7, 5]]) + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) }) it('should work with internal references', () => { - const engine = HyperFormula.buildFromArray([['=A2','=B2:B3',3], ['=A10','=B10:B15',6], ['=C1:C10',8,9]]) - engine.setRowOrder(0, [[0,1],[1,2],[2,0]]) + const engine = HyperFormula.buildFromArray([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8',8,9], ['=A3','=B3:B4',3], ['=A11','=B11:B16',6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8', 8, 9], ['=A3', '=B3:B4', 3], ['=A11', '=B11:B16', 6]]) }) }) From a74b28137720da9eb1616f9ed88fb5a07f748a0d Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 15 Dec 2020 11:02:43 +0100 Subject: [PATCH 09/36] columns --- src/CrudOperations.ts | 25 ++++++++++++++++++------- src/HyperFormula.ts | 5 +++++ src/Operations.ts | 25 +++++++++++++++++++++++++ src/UndoRedo.ts | 22 ++++++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index b2163c1a6c..ae0b297471 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -298,31 +298,42 @@ export class CrudOperations { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } - this.validateRowMapping(sheetId, rowMapping) + this.validateRowOrColumnMapping(sheetId, rowMapping, 'row') this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setRowOrder(sheetId, rowMapping) this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) } - private validateRowMapping(sheetId: number, rowMapping: [number, number][]): void { - const sheetHeight = this.dependencyGraph.getSheetHeight(sheetId) + public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { + if (!this.sheetMapping.hasSheetWithId(sheetId)) { + throw new NoSheetWithIdError(sheetId) + } + this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') + this.undoRedo.clearRedoStack() + this.clipboardOperations.abortCut() + this.operations.setColumnOrder(sheetId, columnMapping) + this.undoRedo.saveOperation(new SetColumnOrderUndoEntry(sheetId, columnMapping)) + } + + private validateRowOrColumnMapping(sheetId: number, rowMapping: [number, number][], rowOrColumn: 'row' | 'column'): void { + const limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId) const sources = rowMapping.map(([a, b])=>a).sort((a, b) => a-b) const targets = rowMapping.map(([a, b])=>b).sort((a, b) => a-b) for(const pos of sources) { - if(!isNonnegativeInteger(pos) || pos >= sheetHeight) { - throw new InvalidArgumentsError('row numbers to be nonnegative integers and less than sheet height.') + if(!isNonnegativeInteger(pos) || pos >= limit) { + throw new InvalidArgumentsError(`${rowOrColumn} numbers to be nonnegative integers and less than sheet height.`) } } for(let i=0;i [{sheet, col: target, row}, cell] + ) + ) + } + } + buffer.forEach( + column => this.restoreClipboardCells(sheetId, column.values()) + ) + } + public addNamedExpression(expressionName: string, expression: RawCellContent, sheetId?: number, options?: NamedExpressionOptions) { this.storeNamedExpressionInCell(this.namedExpressions.lookupNextAddress(expressionName, sheetId), expression) const namedExpression = this.namedExpressions.addNamedExpression(expressionName, sheetId, options) diff --git a/src/UndoRedo.ts b/src/UndoRedo.ts index cb8b18b259..8fca476e88 100644 --- a/src/UndoRedo.ts +++ b/src/UndoRedo.ts @@ -54,6 +54,14 @@ export class SetRowOrderUndoEntry { } } +export class SetColumnOrderUndoEntry { + constructor( + public readonly sheetId: number, + public readonly columnMapping: [number, number][] + ) { + } +} + export class SetSheetContentUndoEntry { constructor( public readonly sheetId: number, @@ -229,6 +237,7 @@ type UndoStackEntry | RemoveNamedExpressionUndoEntry | ChangeNamedExpressionUndoEntry | SetRowOrderUndoEntry + | SetColumnOrderUndoEntry export class UndoRedo { private undoStack: UndoStackEntry[] = [] @@ -344,6 +353,8 @@ export class UndoRedo { this.undoChangeNamedExpression(operation) } else if (operation instanceof SetRowOrderUndoEntry) { this.undoSetRowOrder(operation) + } else if (operation instanceof SetColumnOrderUndoEntry) { + this.undoSetColumnOrder(operation) } else { throw 'Unknown element' } @@ -511,6 +522,11 @@ export class UndoRedo { this.operations.setRowOrder(operation.sheetId, reverseMap) } + private undoSetColumnOrder(operation: SetColumnOrderUndoEntry) { + const reverseMap = operation.columnMapping.map(([source, target]) => [target, source] as [number, number]) + this.operations.setColumnOrder(operation.sheetId, reverseMap) + } + public redo() { const operation = this.redoStack.pop() @@ -562,6 +578,8 @@ export class UndoRedo { this.redoChangeNamedExpression(operation) } else if (operation instanceof SetRowOrderUndoEntry) { this.redoSetRowOrder(operation) + } else if (operation instanceof SetColumnOrderUndoEntry) { + this.redoSetColumnOrder(operation) } else { throw 'Unknown element' } @@ -656,6 +674,10 @@ export class UndoRedo { this.operations.setRowOrder(operation.sheetId, operation.rowMapping) } + private redoSetColumnOrder(operation: SetColumnOrderUndoEntry) { + this.operations.setColumnOrder(operation.sheetId, operation.columnMapping) + } + private restoreOldDataFromVersion(version: number) { const oldDataToRestore = this.oldData.get(version) || [] for (const entryToRestore of oldDataToRestore) { From 902f932b7ae3a1a6d48e81091491c26d2762594a Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 15 Dec 2020 11:06:40 +0100 Subject: [PATCH 10/36] test --- src/CrudOperations.ts | 2 +- test/cruds/set-row-order.spec.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index ae0b297471..08f7619918 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -57,7 +57,7 @@ import { RemoveRowsUndoEntry, RemoveSheetUndoEntry, RenameSheetUndoEntry, - SetCellContentsUndoEntry, SetRowOrderUndoEntry, + SetCellContentsUndoEntry, SetColumnOrderUndoEntry, SetRowOrderUndoEntry, SetSheetContentUndoEntry, UndoRedo } from './UndoRedo' diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index a5e093d3f7..7866f92760 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -36,6 +36,20 @@ describe('setting row order - checking if it is possible', () => { engine.setRowOrder(0, [[0, 0], [1, 1], [2, 1]]) ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') }) + + it('should check for matrices', () => { + const engine = HyperFormula.buildFromArray([[0], [0], ['{=1}']]) + expect(() => + engine.setRowOrder(0, [[0, 2], [1, 1], [2, 0]]) + ).toThrowError('Cannot perform this operation, source location has a matrix inside.') + }) + + it('should check for matrices only in moved rows', () => { + const engine = HyperFormula.buildFromArray([[0], ['{=1}'], [0]]) + expect(() => + engine.setRowOrder(0, [[0, 2], [1, 1], [2, 0]]) + ).not.toThrowError() + }) }) describe('should correctly work', () => { From 927885492eab5fbf1e2c3b776e7cc75e0a6c30f1 Mon Sep 17 00:00:00 2001 From: izulin Date: Tue, 15 Dec 2020 13:38:04 +0100 Subject: [PATCH 11/36] tests --- test/cruds/set-column-order.spec.ts | 148 ++++++++++++++++++++++++++++ test/cruds/set-row-order.spec.ts | 12 +-- 2 files changed, 154 insertions(+), 6 deletions(-) create mode 100644 test/cruds/set-column-order.spec.ts diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts new file mode 100644 index 0000000000..6bafd3f879 --- /dev/null +++ b/test/cruds/set-column-order.spec.ts @@ -0,0 +1,148 @@ +import {HyperFormula} from '../../src' +import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddressMappingPolicy' + +describe('setting column order - checking if it is possible', () => { + it('should validate numbers for negative columns', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(() => + engine.setColumnOrder(0, [[-1, 0]]) + ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') + }) + + it('should validate sources for noninteger values', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(() => + engine.setColumnOrder(0, [[1, 1], [0.5, 0]]) + ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') + }) + + it('should validate sources for values exceeding sheet width', () => { + const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(() => + engine.setColumnOrder(0, [[1, 1], [3, 0]]) + ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') + }) + + it('should validate sources to be unique', () => { + const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(() => + engine.setColumnOrder(0, [[0, 0], [1, 1], [1, 2]]) + ).toThrowError('Invalid arguments, expected source column numbers to be unique.') + }) + + it('should validate sources to be permutation of targets', () => { + const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(() => + engine.setColumnOrder(0, [[0, 0], [1, 1], [2, 1]]) + ).toThrowError('Invalid arguments, expected target column numbers to be permutation of source column numbers.') + }) + + it('should check for matrices', () => { + const engine = HyperFormula.buildFromArray([[0, 0, '{=1}']]) + expect(() => + engine.setColumnOrder(0, [[0, 2], [1, 1], [2, 0]]) + ).toThrowError('Cannot perform this operation, source location has a matrix inside.') + }) + + it('should check for matrices only in moved rows', () => { + const engine = HyperFormula.buildFromArray([[0, '{=1}', 0]]) + expect(() => + engine.setColumnOrder(0, [[0, 2], [1, 1], [2, 0]]) + ).not.toThrowError() + }) +}) + +describe('should correctly work', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 'abcd'], [3, 3], [5, true]]) + engine.setColumnOrder(0, [[0, 1], [1, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([['abcd', 1], [3, 3], [true, 5]]) + }) + + it('should return number of changed cells', () => { + const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5, 6]]) + const ret = engine.setColumnOrder(0, [[0, 1], [1, 0]]) + expect(ret.length).toEqual(6) + }) + + it('should work on static engine with uneven column', () => { + const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + engine.setColumnOrder(0, [[0, 1], [1, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[2,1], [4, 3], [null, 5]]) + }) + + it('should work with more complicated permutations', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) + }) + + it('should not move values unnecessarily', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + const ret = engine.setColumnOrder(0, [[0, 0], [1, 1]]) + expect(ret.length).toEqual(0) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) + expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=10', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) + }) +}) + +describe('reorder working with undo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + }) +}) + +describe('reorder working with redo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.undo() + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.undo() + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) + expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.undo() + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=10', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) + }) +}) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 7866f92760..65e51af92c 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -91,9 +91,9 @@ describe('should correctly work', () => { }) it('should work with internal references', () => { - const engine = HyperFormula.buildFromArray([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) - expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8', 8, 9], ['=A3', '=B3:B4', 3], ['=A11', '=B11:B16', 6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(C-1:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) @@ -113,10 +113,10 @@ describe('reorder working with undo', () => { }) it('should work with internal references', () => { - const engine = HyperFormula.buildFromArray([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) }) }) @@ -139,10 +139,10 @@ describe('reorder working with redo', () => { }) it('should work with internal references', () => { - const engine = HyperFormula.buildFromArray([['=A2', '=B2:B3', 3], ['=A10', '=B10:B15', 6], ['=C1:C10', 8, 9]]) + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([['=C-1:C8', 8, 9], ['=A3', '=B3:B4', 3], ['=A11', '=B11:B16', 6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(C-1:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) From ad839db5e3441aa646b72f95f0999f18b98a36e0 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 16 Dec 2020 14:12:32 +0100 Subject: [PATCH 12/36] fixes bug with serialization of negative refs --- src/Cell.ts | 4 +++ src/Operations.ts | 4 ++- src/parser/CellAddress.ts | 8 ++++-- src/parser/ColumnAddress.ts | 14 +++++++++-- src/parser/RowAddress.ts | 14 +++++++++-- src/parser/Unparser.ts | 18 +++++++------ test/cruds/copy-paste.spec.ts | 39 ++++++++++++++++++++++++++++- test/cruds/set-column-order.spec.ts | 4 +-- test/cruds/set-row-order.spec.ts | 4 +-- 9 files changed, 90 insertions(+), 19 deletions(-) diff --git a/src/Cell.ts b/src/Cell.ts index 51d2ed936a..1d428629ca 100644 --- a/src/Cell.ts +++ b/src/Cell.ts @@ -136,6 +136,8 @@ export interface SimpleRowAddress { export const simpleRowAddress = (sheet: number, row: number): SimpleRowAddress => ({sheet, row}) +export const invalidSimpleRowAddress = (address: SimpleRowAddress): boolean => (address.row < 0) + export interface SimpleColumnAddress { col: number, sheet: number, @@ -143,6 +145,8 @@ export interface SimpleColumnAddress { export const simpleColumnAddress = (sheet: number, col: number): SimpleColumnAddress => ({sheet, col}) +export const invalidSimpleColumnAddress = (address: SimpleColumnAddress): boolean => (address.col < 0) + export interface SimpleCellAddress { col: number, row: number, diff --git a/src/Operations.ts b/src/Operations.ts index 48bd81c092..4a58365a47 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -27,6 +27,7 @@ import { } from './DependencyGraph' import {ValueCellVertexValue} from './DependencyGraph/ValueCellVertex' import { + InvalidAddressError, InvalidArgumentsError, NamedExpressionDoesNotExistError, NoRelativeAddressesAllowedError, @@ -690,7 +691,8 @@ export class Operations { public setFormulaToCellFromCache(formulaHash: string, address: SimpleCellAddress) { const {ast, hasVolatileFunction, hasStructuralChangeFunction, dependencies} = this.parser.fetchCachedResult(formulaHash) - this.dependencyGraph.setFormulaToCell(address, ast, absolutizeDependencies(dependencies, address), hasVolatileFunction, hasStructuralChangeFunction) + const absoluteDependencies = absolutizeDependencies(dependencies, address) + this.dependencyGraph.setFormulaToCell(address, ast, absoluteDependencies, hasVolatileFunction, hasStructuralChangeFunction) } public setParsingErrorToCell(rawInput: string, errors: ParsingError[], address: SimpleCellAddress) { diff --git a/src/parser/CellAddress.ts b/src/parser/CellAddress.ts index c6d2d44968..0cd33662da 100644 --- a/src/parser/CellAddress.ts +++ b/src/parser/CellAddress.ts @@ -4,7 +4,7 @@ */ import { - absoluteSheetReference, + absoluteSheetReference, ErrorType, invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress, simpleColumnAddress, @@ -12,6 +12,7 @@ import { simpleRowAddress, SimpleRowAddress, } from '../Cell' +import {Maybe} from '../Maybe' import {columnIndexToLabel} from './addressRepresentationConverters' import {AddressWithColumn, AddressWithRow} from './Address' @@ -158,8 +159,11 @@ export class CellAddress implements AddressWithColumn, AddressWithRow { } } - public unparse(baseAddress: SimpleCellAddress): string { + public unparse(baseAddress: SimpleCellAddress): Maybe { const simpleAddress = this.toSimpleCellAddress(baseAddress) + if(invalidSimpleCellAddress(simpleAddress)) { + return undefined + } const column = columnIndexToLabel(simpleAddress.col) const rowDollar = this.type === CellReferenceType.CELL_REFERENCE_ABSOLUTE || this.type === CellReferenceType.CELL_REFERENCE_ABSOLUTE_ROW ? '$' : '' const colDollar = this.type === CellReferenceType.CELL_REFERENCE_ABSOLUTE || this.type === CellReferenceType.CELL_REFERENCE_ABSOLUTE_COL ? '$' : '' diff --git a/src/parser/ColumnAddress.ts b/src/parser/ColumnAddress.ts index 26b77dc185..a36d675d05 100644 --- a/src/parser/ColumnAddress.ts +++ b/src/parser/ColumnAddress.ts @@ -3,7 +3,14 @@ * Copyright (c) 2020 Handsoncode. All rights reserved. */ -import {absoluteSheetReference, SimpleCellAddress, simpleColumnAddress, SimpleColumnAddress} from '../Cell' +import { + absoluteSheetReference, + invalidSimpleCellAddress, invalidSimpleColumnAddress, + SimpleCellAddress, + simpleColumnAddress, + SimpleColumnAddress +} from '../Cell' +import {Maybe} from '../Maybe' import {columnIndexToLabel} from './addressRepresentationConverters' import {AddressWithColumn} from './Address' @@ -83,8 +90,11 @@ export class ColumnAddress implements AddressWithColumn { } } - public unparse(baseAddress: SimpleCellAddress): string { + public unparse(baseAddress: SimpleCellAddress): Maybe { const simpleAddress = this.toSimpleColumnAddress(baseAddress) + if(invalidSimpleColumnAddress(simpleAddress)) { + return undefined + } const column = columnIndexToLabel(simpleAddress.col) const dollar = this.type === ReferenceType.ABSOLUTE ? '$' : '' return `${dollar}${column}` diff --git a/src/parser/RowAddress.ts b/src/parser/RowAddress.ts index bccf724de9..f95c67e68a 100644 --- a/src/parser/RowAddress.ts +++ b/src/parser/RowAddress.ts @@ -3,7 +3,14 @@ * Copyright (c) 2020 Handsoncode. All rights reserved. */ -import {absoluteSheetReference, SimpleCellAddress, simpleRowAddress, SimpleRowAddress} from '../Cell' +import { + absoluteSheetReference, + invalidSimpleColumnAddress, invalidSimpleRowAddress, + SimpleCellAddress, + simpleRowAddress, + SimpleRowAddress +} from '../Cell' +import {Maybe} from '../Maybe' import {ReferenceType} from './ColumnAddress' import {AddressWithRow} from './Address' @@ -78,8 +85,11 @@ export class RowAddress implements AddressWithRow { } } - public unparse(baseAddress: SimpleCellAddress): string { + public unparse(baseAddress: SimpleCellAddress): Maybe { const simpleAddress = this.toSimpleRowAddress(baseAddress) + if(invalidSimpleRowAddress(simpleAddress)) { + return undefined + } const dollar = this.type === ReferenceType.ABSOLUTE ? '$' : '' return `${dollar}${simpleAddress.row + 1}` } diff --git a/src/parser/Unparser.ts b/src/parser/Unparser.ts index 3869529304..6592adf487 100644 --- a/src/parser/Unparser.ts +++ b/src/parser/Unparser.ts @@ -3,7 +3,10 @@ * Copyright (c) 2020 Handsoncode. All rights reserved. */ -import {ErrorType, SimpleCellAddress} from '../Cell' +import {ErrorType, invalidSimpleCellAddress, SimpleCellAddress} from '../Cell' +import {NoSheetWithIdError} from '../index' +import {NamedExpressions} from '../NamedExpressions' +import {SheetIndexMappingFn, sheetIndexToString} from './addressRepresentationConverters' import { Ast, AstNodeType, @@ -16,9 +19,6 @@ import { import {binaryOpTokenMap} from './binaryOpTokenMap' import {ILexerConfig} from './LexerConfig' import {ParserConfig} from './ParserConfig' -import {NamedExpressions} from '../NamedExpressions' -import {SheetIndexMappingFn, sheetIndexToString} from './addressRepresentationConverters' -import {NoSheetWithIdError} from '../index' export class Unparser { constructor( @@ -60,10 +60,11 @@ export class Unparser { case AstNodeType.CELL_REFERENCE: { let image if (ast.reference.sheet !== null) { - image = this.unparseSheetName(ast.reference.sheet) + '!' + ast.reference.unparse(address) + image = this.unparseSheetName(ast.reference.sheet) + '!' } else { - image = ast.reference.unparse(address) + image = '' } + image += ast.reference.unparse(address) ?? this.config.translationPackage.getErrorTranslation(ErrorType.REF) return imageWithWhitespace(image, ast.leadingWhitespace) } case AstNodeType.COLUMN_RANGE: @@ -124,7 +125,10 @@ export class Unparser { endSheet = this.unparseSheetName(ast.end.sheet) + '!' } - return `${startSheeet}${ast.start.unparse(baseAddress)}:${endSheet}${ast.end.unparse(baseAddress)}` + const unparsedStart = ast.start.unparse(baseAddress) ?? this.config.translationPackage.getErrorTranslation(ErrorType.REF) + const unparsedEnd = ast.end.unparse(baseAddress) ?? this.config.translationPackage.getErrorTranslation(ErrorType.REF) + + return `${startSheeet}${unparsedStart}:${endSheet}${unparsedEnd}` } } diff --git a/test/cruds/copy-paste.spec.ts b/test/cruds/copy-paste.spec.ts index bd2c583cc3..d4bb5ae229 100644 --- a/test/cruds/copy-paste.spec.ts +++ b/test/cruds/copy-paste.spec.ts @@ -183,13 +183,50 @@ describe('Copy - paste integration', () => { it('should return ref when pasted reference is out of scope', () => { const engine = HyperFormula.buildFromArray([ [null, null], - [null, '=B1'], + [null, '=A1'], ]) engine.copy(adr('B2'), 1, 1) engine.paste(adr('A1')) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.REF, ErrorMessage.BadRef)) + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!') + }) + + it('should return ref when pasted range is out of scope', () => { + const engine = HyperFormula.buildFromArray([ + [null, null], + [null, '=A1:B2'], + ]) + + engine.copy(adr('B2'), 1, 1) + engine.paste(adr('A1')) + + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!:A1') + }) + + it('should return ref when pasted column range is out of scope', () => { + const engine = HyperFormula.buildFromArray([ + [null, null], + [null, '=A:B'], + ]) + + engine.copy(adr('B2'), 1, 1) + engine.paste(adr('A1')) + + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!:A') + }) + + it('should return ref when pasted row range is out of scope', () => { + const engine = HyperFormula.buildFromArray([ + [null, null], + [null, '=1:2'], + ]) + + engine.copy(adr('B2'), 1, 1) + engine.paste(adr('A1')) + + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!:1') }) it('should create new range vertex - cell range', () => { diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index 6bafd3f879..e89647e306 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -93,7 +93,7 @@ describe('should correctly work', () => { it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) - expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=10', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) + expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) }) @@ -143,6 +143,6 @@ describe('reorder working with redo', () => { engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=10', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) + expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) }) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 65e51af92c..7fa3ffe55d 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -93,7 +93,7 @@ describe('should correctly work', () => { it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) - expect(engine.getSheetSerialized(0)).toEqual([['=SUM(C-1:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) @@ -143,6 +143,6 @@ describe('reorder working with redo', () => { engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([['=SUM(C-1:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) From 5e064e195adfd25c198a83a0393320a9993c3b9f Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 16 Dec 2020 14:16:11 +0100 Subject: [PATCH 13/36] doc --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 583cf55ac2..aa51871857 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed a problem with HLOOKUP/VLOOKUP getting stuck in binary search. (#559, #562) - Fixed a problem with logic of dependency resolving. (#561, #563) - Fixed a minor bug with ATAN2 function. (#581) +- Fixed a bug with serialization of some addresses after CRUDs. (#587) ## [0.3.0] - 2020-10-22 From 3f5fbf02597db1dd41621b02555baf88015aabb7 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 16 Dec 2020 14:24:16 +0100 Subject: [PATCH 14/36] doc --- src/HyperFormula.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 130d52d8b5..2762fcb08e 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1174,6 +1174,48 @@ export class HyperFormula implements TypedEmitter { return this.recomputeIfDependencyGraphNeedsIt() } + /** + * Reorders columns of a sheet according to a permutation. + * + * Note that this method may trigger dependency graph recalculation. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {[number, number][]} columnMapping - array mapping original positions to final positions of columns + * + * @fires [[valuesUpdated]] if recalculation was triggered by this change + * + * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist + * @throws an error when columnMapping does not define correct column permutation for some subset of columns of the given sheet + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1, 2, 4], + * [5] + * ]); + * + * // should set swap columns 0 and 2 in place, returns: + * // [{ + * // address: { sheet: 0, col: 2, row: 0 }, + * // newValue: 1, + * // }, + * // { + * // address: { sheet: 0, col: 2, row: 1 }, + * // newValue: 5, + * // }, + * // { + * // address: { sheet: 0, col: 0, row: 0 }, + * // newValue: 4, + * // }, + * // { + * // address: { sheet: 0, col: 0, row: 1 }, + * // newValue: null, + * // }] + * const changes = hfInstance.setRowOrder(0, [[0,2],[2,0]]); + * ``` + * + * @category Cells + */ public setColumnOrder(sheetId: number, columnMapping: [number, number][]): ExportedChange[] { this._crudOperations.setColumnOrder(sheetId, columnMapping) return this.recomputeIfDependencyGraphNeedsIt() From b087b305988b76a51222029aee63d67935e3000d Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 16 Dec 2020 14:25:38 +0100 Subject: [PATCH 15/36] linter --- test/cruds/set-column-order.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index e89647e306..c9b7008b10 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -68,7 +68,7 @@ describe('should correctly work', () => { it('should work on static engine with uneven column', () => { const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5]], {chooseAddressMappingPolicy: new AlwaysSparse()}) engine.setColumnOrder(0, [[0, 1], [1, 0]]) - expect(engine.getSheetSerialized(0)).toEqual([[2,1], [4, 3], [null, 5]]) + expect(engine.getSheetSerialized(0)).toEqual([[2, 1], [4, 3], [null, 5]]) }) it('should work with more complicated permutations', () => { From 89cf8aad55f0ec447fc963ad97c442d9a450061e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Uzna=C5=84ski?= <40573492+izulin@users.noreply.github.com> Date: Tue, 29 Dec 2020 10:50:47 +0100 Subject: [PATCH 16/36] Update src/HyperFormula.ts Co-authored-by: Wojciech Czerniak --- src/HyperFormula.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 2762fcb08e..a6f1691917 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1214,7 +1214,7 @@ export class HyperFormula implements TypedEmitter { * const changes = hfInstance.setRowOrder(0, [[0,2],[2,0]]); * ``` * - * @category Cells + * @category Columns */ public setColumnOrder(sheetId: number, columnMapping: [number, number][]): ExportedChange[] { this._crudOperations.setColumnOrder(sheetId, columnMapping) From 59890a3b72137ce2710b725c8964e3b31af26c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Uzna=C5=84ski?= <40573492+izulin@users.noreply.github.com> Date: Tue, 29 Dec 2020 10:50:54 +0100 Subject: [PATCH 17/36] Update src/HyperFormula.ts Co-authored-by: Wojciech Czerniak --- src/HyperFormula.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index a6f1691917..96907c1b65 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1167,7 +1167,7 @@ export class HyperFormula implements TypedEmitter { * const changes = hfInstance.setRowOrder(0, [[0,2],[2,0]]); * ``` * - * @category Cells + * @category Rows */ public setRowOrder(sheetId: number, rowMapping: [number, number][]): ExportedChange[] { this._crudOperations.setRowOrder(sheetId, rowMapping) From 5dacc68a539c9af30a9c8d65ecf1b28396fd72a1 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 30 Dec 2020 20:50:40 +0100 Subject: [PATCH 18/36] extra methods --- src/CrudOperations.ts | 20 ++++++++++++++------ src/HyperFormula.ts | 20 ++++++++++++++++++++ src/Operations.ts | 24 ++++++++++++++++++++++++ test/cruds/set-column-order.spec.ts | 17 +++++++++++++++++ test/cruds/set-row-order.spec.ts | 16 ++++++++++++++++ 5 files changed, 91 insertions(+), 6 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 08f7619918..4541b6817c 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -295,27 +295,35 @@ export class CrudOperations { } public setRowOrder(sheetId: number, rowMapping: [number, number][]): void { - if (!this.sheetMapping.hasSheetWithId(sheetId)) { - throw new NoSheetWithIdError(sheetId) - } - this.validateRowOrColumnMapping(sheetId, rowMapping, 'row') + this.validateSetRowOrder(sheetId, rowMapping) this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setRowOrder(sheetId, rowMapping) this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) } - public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { + public validateSetRowOrder(sheetId: number, rowMapping: [number,number][]): void { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } - this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') + this.validateRowOrColumnMapping(sheetId, rowMapping, 'row') + } + + public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { + this.validateSetColumnOrder(sheetId, columnMapping) this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setColumnOrder(sheetId, columnMapping) this.undoRedo.saveOperation(new SetColumnOrderUndoEntry(sheetId, columnMapping)) } + public validateSetColumnOrder(sheetId: number, columnMapping: [number,number][]): void { + if (!this.sheetMapping.hasSheetWithId(sheetId)) { + throw new NoSheetWithIdError(sheetId) + } + this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') + } + private validateRowOrColumnMapping(sheetId: number, rowMapping: [number, number][], rowOrColumn: 'row' | 'column'): void { const limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId) const sources = rowMapping.map(([a, b])=>a).sort((a, b) => a-b) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 96907c1b65..91215897b8 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1138,6 +1138,7 @@ export class HyperFormula implements TypedEmitter { * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist * @throws an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet + * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example * ```js @@ -1174,6 +1175,15 @@ export class HyperFormula implements TypedEmitter { return this.recomputeIfDependencyGraphNeedsIt() } + public isItPossibleToSetRowOrder(sheetId: number, rowMapping: [number,number][]): boolean { + try { + this._crudOperations.validateSetRowOrder(sheetId, rowMapping) + } catch (e) { + return false + } + return this._crudOperations.operations.testRowOrderForMatrices(sheetId, rowMapping) + } + /** * Reorders columns of a sheet according to a permutation. * @@ -1186,6 +1196,7 @@ export class HyperFormula implements TypedEmitter { * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist * @throws an error when columnMapping does not define correct column permutation for some subset of columns of the given sheet + * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example * ```js @@ -1221,6 +1232,15 @@ export class HyperFormula implements TypedEmitter { return this.recomputeIfDependencyGraphNeedsIt() } + public isItPossibleToSetColumnOrder(sheetId: number, columnMapping: [number,number][]): boolean { + try { + this._crudOperations.validateSetColumnOrder(sheetId, columnMapping) + } catch (e) { + return false + } + return this._crudOperations.operations.testColumnOrderForMatrices(sheetId, columnMapping) + } + /** * Returns information whether it is possible to add rows into a specified position in a given sheet. * Checks against particular rules to ascertain that addRows can be called. diff --git a/src/Operations.ts b/src/Operations.ts index 4a58365a47..8c97894300 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -332,6 +332,18 @@ export class Operations { ) } + public testRowOrderForMatrices(sheetId: number, rowMapping: [number, number][]): boolean { + for(const [source, target] of rowMapping ) { + if(source!==target) { + const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source}, Infinity, 1) + if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { + return false + } + } + } + return true + } + public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { const buffer: [SimpleCellAddress, ClipboardCell][][] = [] for(const [source, target] of columnMapping ) { @@ -354,6 +366,18 @@ export class Operations { ) } + public testColumnOrderForMatrices(sheetId: number, columnMapping: [number, number][]): boolean { + for(const [source, target] of columnMapping ) { + if(source!==target) { + const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: source, row: 0}, 1, Infinity) + if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { + return false + } + } + } + return true + } + public addNamedExpression(expressionName: string, expression: RawCellContent, sheetId?: number, options?: NamedExpressionOptions) { this.storeNamedExpressionInCell(this.namedExpressions.lookupNextAddress(expressionName, sheetId), expression) const namedExpression = this.namedExpressions.addNamedExpression(expressionName, sheetId, options) diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index c9b7008b10..9a86261cb0 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -4,6 +4,7 @@ import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddre describe('setting column order - checking if it is possible', () => { it('should validate numbers for negative columns', () => { const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[-1, 0]])).toEqual(false) expect(() => engine.setColumnOrder(0, [[-1, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') @@ -11,6 +12,7 @@ describe('setting column order - checking if it is possible', () => { it('should validate sources for noninteger values', () => { const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[1, 1], [0.5, 0]])).toEqual(false) expect(() => engine.setColumnOrder(0, [[1, 1], [0.5, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') @@ -18,6 +20,7 @@ describe('setting column order - checking if it is possible', () => { it('should validate sources for values exceeding sheet width', () => { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[1,1],[3,0]])).toEqual(false) expect(() => engine.setColumnOrder(0, [[1, 1], [3, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') @@ -25,6 +28,7 @@ describe('setting column order - checking if it is possible', () => { it('should validate sources to be unique', () => { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,0],[1,1],[1,2]])).toEqual(false) expect(() => engine.setColumnOrder(0, [[0, 0], [1, 1], [1, 2]]) ).toThrowError('Invalid arguments, expected source column numbers to be unique.') @@ -32,6 +36,7 @@ describe('setting column order - checking if it is possible', () => { it('should validate sources to be permutation of targets', () => { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,0],[1,1],[2,1]])).toEqual(false) expect(() => engine.setColumnOrder(0, [[0, 0], [1, 1], [2, 1]]) ).toThrowError('Invalid arguments, expected target column numbers to be permutation of source column numbers.') @@ -39,6 +44,7 @@ describe('setting column order - checking if it is possible', () => { it('should check for matrices', () => { const engine = HyperFormula.buildFromArray([[0, 0, '{=1}']]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,2],[1,1],[2,0]])).toEqual(false) expect(() => engine.setColumnOrder(0, [[0, 2], [1, 1], [2, 0]]) ).toThrowError('Cannot perform this operation, source location has a matrix inside.') @@ -46,6 +52,7 @@ describe('setting column order - checking if it is possible', () => { it('should check for matrices only in moved rows', () => { const engine = HyperFormula.buildFromArray([[0, '{=1}', 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,2],[1,1],[2,0]])).toEqual(true) expect(() => engine.setColumnOrder(0, [[0, 2], [1, 1], [2, 0]]) ).not.toThrowError() @@ -55,36 +62,42 @@ describe('setting column order - checking if it is possible', () => { describe('should correctly work', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 'abcd'], [3, 3], [5, true]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,0]])).toEqual(true) engine.setColumnOrder(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([['abcd', 1], [3, 3], [true, 5]]) }) it('should return number of changed cells', () => { const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5, 6]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,0]])).toEqual(true) const ret = engine.setColumnOrder(0, [[0, 1], [1, 0]]) expect(ret.length).toEqual(6) }) it('should work on static engine with uneven column', () => { const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,0]])).toEqual(true) engine.setColumnOrder(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[2, 1], [4, 3], [null, 5]]) }) it('should work with more complicated permutations', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) }) it('should not move values unnecessarily', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,0],[1,1]])).toEqual(true) const ret = engine.setColumnOrder(0, [[0, 0], [1, 1]]) expect(ret.length).toEqual(0) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) @@ -92,6 +105,7 @@ describe('should correctly work', () => { it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) @@ -125,6 +139,7 @@ describe('reorder working with redo', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) }) @@ -133,6 +148,7 @@ describe('reorder working with redo', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) @@ -142,6 +158,7 @@ describe('reorder working with redo', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() + expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 7fa3ffe55d..ecb2279304 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -4,6 +4,7 @@ import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddre describe('setting row order - checking if it is possible', () => { it('should validate numbers for negative rows', () => { const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetRowOrder(0, [[-1, 0]])).toEqual(false) expect(() => engine.setRowOrder(0, [[-1, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') @@ -11,6 +12,7 @@ describe('setting row order - checking if it is possible', () => { it('should validate sources for noninteger values', () => { const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetRowOrder(0, [[1, 1], [0.5, 0]])).toEqual(false) expect(() => engine.setRowOrder(0, [[1, 1], [0.5, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') @@ -18,6 +20,7 @@ describe('setting row order - checking if it is possible', () => { it('should validate sources for values exceeding sheet height', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [[1, 1], [3,0]])).toEqual(false) expect(() => engine.setRowOrder(0, [[1, 1], [3, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') @@ -25,6 +28,7 @@ describe('setting row order - checking if it is possible', () => { it('should validate sources to be unique', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 0],[1,1],[1,2]])).toEqual(false) expect(() => engine.setRowOrder(0, [[0, 0], [1, 1], [1, 2]]) ).toThrowError('Invalid arguments, expected source row numbers to be unique.') @@ -32,6 +36,7 @@ describe('setting row order - checking if it is possible', () => { it('should validate sources to be permutation of targets', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 0],[1,1],[2,1]])).toEqual(false) expect(() => engine.setRowOrder(0, [[0, 0], [1, 1], [2, 1]]) ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') @@ -39,6 +44,7 @@ describe('setting row order - checking if it is possible', () => { it('should check for matrices', () => { const engine = HyperFormula.buildFromArray([[0], [0], ['{=1}']]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 2],[1,1],[2,0]])).toEqual(false) expect(() => engine.setRowOrder(0, [[0, 2], [1, 1], [2, 0]]) ).toThrowError('Cannot perform this operation, source location has a matrix inside.') @@ -46,6 +52,7 @@ describe('setting row order - checking if it is possible', () => { it('should check for matrices only in moved rows', () => { const engine = HyperFormula.buildFromArray([[0], ['{=1}'], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 2],[1,1],[2,0]])).toEqual(true) expect(() => engine.setRowOrder(0, [[0, 2], [1, 1], [2, 0]]) ).not.toThrowError() @@ -55,30 +62,35 @@ describe('setting row order - checking if it is possible', () => { describe('should correctly work', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 'abcd', 3], [3, 5, true]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,0]])).toEqual(true) engine.setRowOrder(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, 5, true], [1, 'abcd', 3]]) }) it('should return number of changed cells', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,0]])).toEqual(true) const ret = engine.setRowOrder(0, [[0, 1], [1, 0]]) expect(ret.length).toEqual(6) }) it('should work on static engine with uneven rows', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6, 7, 8]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,0]])).toEqual(true) engine.setRowOrder(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[4, 5, 6, 7, 8], [1, 2, 3]]) }) it('should work with more complicated permutations', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) }) it('should not move values unnecessarily', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 0],[1,1]])).toEqual(true) const ret = engine.setRowOrder(0, [[0, 0], [1, 1]]) expect(ret.length).toEqual(0) }) @@ -92,6 +104,7 @@ describe('should correctly work', () => { it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) @@ -125,6 +138,7 @@ describe('reorder working with redo', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) }) @@ -133,6 +147,7 @@ describe('reorder working with redo', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) @@ -142,6 +157,7 @@ describe('reorder working with redo', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() + expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) From a2aa30f41aaa3393bf26833eaa11ed5542454851 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 30 Dec 2020 21:06:25 +0100 Subject: [PATCH 19/36] rename --- src/CrudOperations.ts | 4 +- src/HyperFormula.ts | 12 ++--- test/cruds/set-column-order.spec.ts | 74 ++++++++++++++--------------- test/cruds/set-row-order.spec.ts | 72 ++++++++++++++-------------- 4 files changed, 81 insertions(+), 81 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 4541b6817c..84bc89d246 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -302,7 +302,7 @@ export class CrudOperations { this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) } - public validateSetRowOrder(sheetId: number, rowMapping: [number,number][]): void { + public validateSetRowOrder(sheetId: number, rowMapping: [number, number][]): void { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } @@ -317,7 +317,7 @@ export class CrudOperations { this.undoRedo.saveOperation(new SetColumnOrderUndoEntry(sheetId, columnMapping)) } - public validateSetColumnOrder(sheetId: number, columnMapping: [number,number][]): void { + public validateSetColumnOrder(sheetId: number, columnMapping: [number, number][]): void { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 91215897b8..3cd7798554 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1165,17 +1165,17 @@ export class HyperFormula implements TypedEmitter { * // address: { sheet: 0, col: 1, row: 0 }, * // newValue: 5, * // }] - * const changes = hfInstance.setRowOrder(0, [[0,2],[2,0]]); + * const changes = hfInstance.swapRowIndexes(0, [[0,2],[2,0]]); * ``` * * @category Rows */ - public setRowOrder(sheetId: number, rowMapping: [number, number][]): ExportedChange[] { + public swapRowIndexes(sheetId: number, rowMapping: [number, number][]): ExportedChange[] { this._crudOperations.setRowOrder(sheetId, rowMapping) return this.recomputeIfDependencyGraphNeedsIt() } - public isItPossibleToSetRowOrder(sheetId: number, rowMapping: [number,number][]): boolean { + public isItPossibleToSwapRowIndexes(sheetId: number, rowMapping: [number, number][]): boolean { try { this._crudOperations.validateSetRowOrder(sheetId, rowMapping) } catch (e) { @@ -1222,17 +1222,17 @@ export class HyperFormula implements TypedEmitter { * // address: { sheet: 0, col: 0, row: 1 }, * // newValue: null, * // }] - * const changes = hfInstance.setRowOrder(0, [[0,2],[2,0]]); + * const changes = hfInstance.swapColumnIndexes(0, [[0,2],[2,0]]); * ``` * * @category Columns */ - public setColumnOrder(sheetId: number, columnMapping: [number, number][]): ExportedChange[] { + public swapColumnIndexes(sheetId: number, columnMapping: [number, number][]): ExportedChange[] { this._crudOperations.setColumnOrder(sheetId, columnMapping) return this.recomputeIfDependencyGraphNeedsIt() } - public isItPossibleToSetColumnOrder(sheetId: number, columnMapping: [number,number][]): boolean { + public isItPossibleToSwapColumnIndexes(sheetId: number, columnMapping: [number, number][]): boolean { try { this._crudOperations.validateSetColumnOrder(sheetId, columnMapping) } catch (e) { diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index 9a86261cb0..c21f690729 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -4,57 +4,57 @@ import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddre describe('setting column order - checking if it is possible', () => { it('should validate numbers for negative columns', () => { const engine = HyperFormula.buildFromArray([[]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[-1, 0]])).toEqual(false) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[-1, 0]])).toEqual(false) expect(() => - engine.setColumnOrder(0, [[-1, 0]]) + engine.swapColumnIndexes(0, [[-1, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources for noninteger values', () => { const engine = HyperFormula.buildFromArray([[]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[1, 1], [0.5, 0]])).toEqual(false) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[1, 1], [0.5, 0]])).toEqual(false) expect(() => - engine.setColumnOrder(0, [[1, 1], [0.5, 0]]) + engine.swapColumnIndexes(0, [[1, 1], [0.5, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources for values exceeding sheet width', () => { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[1,1],[3,0]])).toEqual(false) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[1, 1], [3, 0]])).toEqual(false) expect(() => - engine.setColumnOrder(0, [[1, 1], [3, 0]]) + engine.swapColumnIndexes(0, [[1, 1], [3, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources to be unique', () => { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,0],[1,1],[1,2]])).toEqual(false) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 0], [1, 1], [1, 2]])).toEqual(false) expect(() => - engine.setColumnOrder(0, [[0, 0], [1, 1], [1, 2]]) + engine.swapColumnIndexes(0, [[0, 0], [1, 1], [1, 2]]) ).toThrowError('Invalid arguments, expected source column numbers to be unique.') }) it('should validate sources to be permutation of targets', () => { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,0],[1,1],[2,1]])).toEqual(false) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 0], [1, 1], [2, 1]])).toEqual(false) expect(() => - engine.setColumnOrder(0, [[0, 0], [1, 1], [2, 1]]) + engine.swapColumnIndexes(0, [[0, 0], [1, 1], [2, 1]]) ).toThrowError('Invalid arguments, expected target column numbers to be permutation of source column numbers.') }) it('should check for matrices', () => { const engine = HyperFormula.buildFromArray([[0, 0, '{=1}']]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,2],[1,1],[2,0]])).toEqual(false) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(false) expect(() => - engine.setColumnOrder(0, [[0, 2], [1, 1], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]]) ).toThrowError('Cannot perform this operation, source location has a matrix inside.') }) it('should check for matrices only in moved rows', () => { const engine = HyperFormula.buildFromArray([[0, '{=1}', 0]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,2],[1,1],[2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(true) expect(() => - engine.setColumnOrder(0, [[0, 2], [1, 1], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]]) ).not.toThrowError() }) }) @@ -62,51 +62,51 @@ describe('setting column order - checking if it is possible', () => { describe('should correctly work', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 'abcd'], [3, 3], [5, true]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,0]])).toEqual(true) - engine.setColumnOrder(0, [[0, 1], [1, 0]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 0]])).toEqual(true) + engine.swapColumnIndexes(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([['abcd', 1], [3, 3], [true, 5]]) }) it('should return number of changed cells', () => { const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5, 6]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,0]])).toEqual(true) - const ret = engine.setColumnOrder(0, [[0, 1], [1, 0]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 0]])).toEqual(true) + const ret = engine.swapColumnIndexes(0, [[0, 1], [1, 0]]) expect(ret.length).toEqual(6) }) it('should work on static engine with uneven column', () => { const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5]], {chooseAddressMappingPolicy: new AlwaysSparse()}) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,0]])).toEqual(true) - engine.setColumnOrder(0, [[0, 1], [1, 0]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 0]])).toEqual(true) + engine.swapColumnIndexes(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[2, 1], [4, 3], [null, 5]]) }) it('should work with more complicated permutations', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) }) it('should not move values unnecessarily', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,0],[1,1]])).toEqual(true) - const ret = engine.setColumnOrder(0, [[0, 0], [1, 1]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 0], [1, 1]])).toEqual(true) + const ret = engine.swapColumnIndexes(0, [[0, 0], [1, 1]]) expect(ret.length).toEqual(0) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) }) it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) }) @@ -114,21 +114,21 @@ describe('should correctly work', () => { describe('reorder working with undo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) }) it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) }) @@ -137,18 +137,18 @@ describe('reorder working with undo', () => { describe('reorder working with redo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) @@ -156,9 +156,9 @@ describe('reorder working with redo', () => { it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) - engine.setColumnOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.isItPossibleToSetColumnOrder(0, [[0,1],[1,2], [2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index ecb2279304..58b7f27a52 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -4,57 +4,57 @@ import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddre describe('setting row order - checking if it is possible', () => { it('should validate numbers for negative rows', () => { const engine = HyperFormula.buildFromArray([[]]) - expect(engine.isItPossibleToSetRowOrder(0, [[-1, 0]])).toEqual(false) + expect(engine.isItPossibleToSwapRowIndexes(0, [[-1, 0]])).toEqual(false) expect(() => - engine.setRowOrder(0, [[-1, 0]]) + engine.swapRowIndexes(0, [[-1, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources for noninteger values', () => { const engine = HyperFormula.buildFromArray([[]]) - expect(engine.isItPossibleToSetRowOrder(0, [[1, 1], [0.5, 0]])).toEqual(false) + expect(engine.isItPossibleToSwapRowIndexes(0, [[1, 1], [0.5, 0]])).toEqual(false) expect(() => - engine.setRowOrder(0, [[1, 1], [0.5, 0]]) + engine.swapRowIndexes(0, [[1, 1], [0.5, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources for values exceeding sheet height', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) - expect(engine.isItPossibleToSetRowOrder(0, [[1, 1], [3,0]])).toEqual(false) + expect(engine.isItPossibleToSwapRowIndexes(0, [[1, 1], [3, 0]])).toEqual(false) expect(() => - engine.setRowOrder(0, [[1, 1], [3, 0]]) + engine.swapRowIndexes(0, [[1, 1], [3, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) it('should validate sources to be unique', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 0],[1,1],[1,2]])).toEqual(false) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 0], [1, 1], [1, 2]])).toEqual(false) expect(() => - engine.setRowOrder(0, [[0, 0], [1, 1], [1, 2]]) + engine.swapRowIndexes(0, [[0, 0], [1, 1], [1, 2]]) ).toThrowError('Invalid arguments, expected source row numbers to be unique.') }) it('should validate sources to be permutation of targets', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 0],[1,1],[2,1]])).toEqual(false) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 0], [1, 1], [2, 1]])).toEqual(false) expect(() => - engine.setRowOrder(0, [[0, 0], [1, 1], [2, 1]]) + engine.swapRowIndexes(0, [[0, 0], [1, 1], [2, 1]]) ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') }) it('should check for matrices', () => { const engine = HyperFormula.buildFromArray([[0], [0], ['{=1}']]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 2],[1,1],[2,0]])).toEqual(false) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(false) expect(() => - engine.setRowOrder(0, [[0, 2], [1, 1], [2, 0]]) + engine.swapRowIndexes(0, [[0, 2], [1, 1], [2, 0]]) ).toThrowError('Cannot perform this operation, source location has a matrix inside.') }) it('should check for matrices only in moved rows', () => { const engine = HyperFormula.buildFromArray([[0], ['{=1}'], [0]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 2],[1,1],[2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(true) expect(() => - engine.setRowOrder(0, [[0, 2], [1, 1], [2, 0]]) + engine.swapRowIndexes(0, [[0, 2], [1, 1], [2, 0]]) ).not.toThrowError() }) }) @@ -62,50 +62,50 @@ describe('setting row order - checking if it is possible', () => { describe('should correctly work', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 'abcd', 3], [3, 5, true]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,0]])).toEqual(true) - engine.setRowOrder(0, [[0, 1], [1, 0]]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 0]])).toEqual(true) + engine.swapRowIndexes(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[3, 5, true], [1, 'abcd', 3]]) }) it('should return number of changed cells', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,0]])).toEqual(true) - const ret = engine.setRowOrder(0, [[0, 1], [1, 0]]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 0]])).toEqual(true) + const ret = engine.swapRowIndexes(0, [[0, 1], [1, 0]]) expect(ret.length).toEqual(6) }) it('should work on static engine with uneven rows', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6, 7, 8]], {chooseAddressMappingPolicy: new AlwaysSparse()}) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,0]])).toEqual(true) - engine.setRowOrder(0, [[0, 1], [1, 0]]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 0]])).toEqual(true) + engine.swapRowIndexes(0, [[0, 1], [1, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[4, 5, 6, 7, 8], [1, 2, 3]]) }) it('should work with more complicated permutations', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) }) it('should not move values unnecessarily', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 0],[1,1]])).toEqual(true) - const ret = engine.setRowOrder(0, [[0, 0], [1, 1]]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 0], [1, 1]])).toEqual(true) + const ret = engine.swapRowIndexes(0, [[0, 0], [1, 1]]) expect(ret.length).toEqual(0) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) }) it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) @@ -113,21 +113,21 @@ describe('should correctly work', () => { describe('reorder working with undo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) }) it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) }) @@ -136,18 +136,18 @@ describe('reorder working with undo', () => { describe('reorder working with redo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) }) it('should work with external references', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) @@ -155,9 +155,9 @@ describe('reorder working with redo', () => { it('should work with internal references', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) - engine.setRowOrder(0, [[0, 1], [1, 2], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) engine.undo() - expect(engine.isItPossibleToSetRowOrder(0, [[0, 1],[1,2],[2,0]])).toEqual(true) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) From 32e08b001ea3265f6aec424918b9a1077ed9cb8c Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 30 Dec 2020 22:57:18 +0100 Subject: [PATCH 20/36] range serailization --- src/parser/Unparser.ts | 8 +++++--- test/cruds/copy-paste.spec.ts | 6 +++--- test/cruds/set-row-order.spec.ts | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/parser/Unparser.ts b/src/parser/Unparser.ts index 6592adf487..1b39f9bfa4 100644 --- a/src/parser/Unparser.ts +++ b/src/parser/Unparser.ts @@ -125,9 +125,11 @@ export class Unparser { endSheet = this.unparseSheetName(ast.end.sheet) + '!' } - const unparsedStart = ast.start.unparse(baseAddress) ?? this.config.translationPackage.getErrorTranslation(ErrorType.REF) - const unparsedEnd = ast.end.unparse(baseAddress) ?? this.config.translationPackage.getErrorTranslation(ErrorType.REF) - + const unparsedStart = ast.start.unparse(baseAddress) + const unparsedEnd = ast.end.unparse(baseAddress) + if(unparsedStart === undefined || unparsedEnd === undefined) { + return this.config.translationPackage.getErrorTranslation(ErrorType.REF) + } return `${startSheeet}${unparsedStart}:${endSheet}${unparsedEnd}` } } diff --git a/test/cruds/copy-paste.spec.ts b/test/cruds/copy-paste.spec.ts index d4bb5ae229..dfe1788c5c 100644 --- a/test/cruds/copy-paste.spec.ts +++ b/test/cruds/copy-paste.spec.ts @@ -202,7 +202,7 @@ describe('Copy - paste integration', () => { engine.copy(adr('B2'), 1, 1) engine.paste(adr('A1')) - expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!:A1') + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!') }) it('should return ref when pasted column range is out of scope', () => { @@ -214,7 +214,7 @@ describe('Copy - paste integration', () => { engine.copy(adr('B2'), 1, 1) engine.paste(adr('A1')) - expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!:A') + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!') }) it('should return ref when pasted row range is out of scope', () => { @@ -226,7 +226,7 @@ describe('Copy - paste integration', () => { engine.copy(adr('B2'), 1, 1) engine.paste(adr('A1')) - expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!:1') + expect(engine.getCellSerialized(adr('A1'))).toEqual('=#REF!') }) it('should create new range vertex - cell range', () => { diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 58b7f27a52..80ed5dad2c 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -106,7 +106,7 @@ describe('should correctly work', () => { const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) - expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) @@ -159,6 +159,6 @@ describe('reorder working with redo', () => { engine.undo() expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 2], [2, 0]])).toEqual(true) engine.redo() - expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!:C8)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) From c5d556559844157caf806b304981bd584b7bc9f8 Mon Sep 17 00:00:00 2001 From: izulin Date: Thu, 31 Dec 2020 11:12:34 +0100 Subject: [PATCH 21/36] . --- src/CrudOperations.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 84bc89d246..fc6aa52544 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -7,7 +7,6 @@ import {AbsoluteCellRange} from './AbsoluteCellRange' import {invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress} from './Cell' import {CellContent, CellContentParser, isMatrix, RawCellContent} from './CellContentParser' import {ClipboardCell, ClipboardOperations} from './ClipboardOperations' -import {ExportedChange} from './Exporter' import {AddColumnsCommand, AddRowsCommand, Operations, RemoveColumnsCommand, RemoveRowsCommand} from './Operations' import {ColumnSearchStrategy} from './Lookup/SearchStrategy' import {Config} from './Config' From 8a73d6b2bdaea8826ae9bd8fe399170509c2a70c Mon Sep 17 00:00:00 2001 From: izulin Date: Fri, 1 Jan 2021 19:23:07 +0100 Subject: [PATCH 22/36] more methods --- src/CrudOperations.ts | 27 ++++- src/HyperFormula.ts | 35 +++++- test/cruds/set-column-order.spec.ts | 169 +++++++++++++++++++++++++-- test/cruds/set-row-order.spec.ts | 170 +++++++++++++++++++++++++++- 4 files changed, 382 insertions(+), 19 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index fc6aa52544..c9d62e61ce 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -294,14 +294,14 @@ export class CrudOperations { } public setRowOrder(sheetId: number, rowMapping: [number, number][]): void { - this.validateSetRowOrder(sheetId, rowMapping) + this.validateSwapRowIndexes(sheetId, rowMapping) this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setRowOrder(sheetId, rowMapping) this.undoRedo.saveOperation(new SetRowOrderUndoEntry(sheetId, rowMapping)) } - public validateSetRowOrder(sheetId: number, rowMapping: [number, number][]): void { + public validateSwapRowIndexes(sheetId: number, rowMapping: [number, number][]): void { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } @@ -309,20 +309,37 @@ export class CrudOperations { } public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { - this.validateSetColumnOrder(sheetId, columnMapping) + this.validateSwapColumnIndexes(sheetId, columnMapping) this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setColumnOrder(sheetId, columnMapping) this.undoRedo.saveOperation(new SetColumnOrderUndoEntry(sheetId, columnMapping)) } - public validateSetColumnOrder(sheetId: number, columnMapping: [number, number][]): void { + public validateSwapColumnIndexes(sheetId: number, columnMapping: [number, number][]): void { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') } + public mappingFromOrder(sheetId: number, newOrder: number[], rowOrColumn: 'row' | 'column'): [number,number][] { + if (!this.sheetMapping.hasSheetWithId(sheetId)) { + throw new NoSheetWithIdError(sheetId) + } + const limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId) + if(newOrder.length !== limit) { + throw new InvalidArgumentsError(`number of ${rowOrColumn}s provided to be sheet ${rowOrColumn==='row'?'height':'width'}.`) + } + const ret: [number,number][] = [] + for(let i=0;ia).sort((a, b) => a-b) @@ -330,7 +347,7 @@ export class CrudOperations { for(const pos of sources) { if(!isNonnegativeInteger(pos) || pos >= limit) { - throw new InvalidArgumentsError(`${rowOrColumn} numbers to be nonnegative integers and less than sheet height.`) + throw new InvalidArgumentsError(`${rowOrColumn} numbers to be nonnegative integers and less than sheet ${rowOrColumn==='row'?'height':'width'}.`) } } for(let i=0;i { +describe('swapping columns - checking if it is possible', () => { it('should validate numbers for negative columns', () => { const engine = HyperFormula.buildFromArray([[]]) expect(engine.isItPossibleToSwapColumnIndexes(0, [[-1, 0]])).toEqual(false) expect(() => engine.swapColumnIndexes(0, [[-1, 0]]) - ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') + ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet width.') }) it('should validate sources for noninteger values', () => { @@ -15,7 +15,7 @@ describe('setting column order - checking if it is possible', () => { expect(engine.isItPossibleToSwapColumnIndexes(0, [[1, 1], [0.5, 0]])).toEqual(false) expect(() => engine.swapColumnIndexes(0, [[1, 1], [0.5, 0]]) - ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') + ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet width.') }) it('should validate sources for values exceeding sheet width', () => { @@ -23,7 +23,7 @@ describe('setting column order - checking if it is possible', () => { expect(engine.isItPossibleToSwapColumnIndexes(0, [[1, 1], [3, 0]])).toEqual(false) expect(() => engine.swapColumnIndexes(0, [[1, 1], [3, 0]]) - ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet height.') + ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet width.') }) it('should validate sources to be unique', () => { @@ -59,7 +59,7 @@ describe('setting column order - checking if it is possible', () => { }) }) -describe('should correctly work', () => { +describe('swapping rows should correctly work', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 'abcd'], [3, 3], [5, true]]) expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 0]])).toEqual(true) @@ -111,7 +111,7 @@ describe('should correctly work', () => { }) }) -describe('reorder working with undo', () => { +describe('swapping rows working with undo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) @@ -134,7 +134,7 @@ describe('reorder working with undo', () => { }) }) -describe('reorder working with redo', () => { +describe('swapping rows working with redo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) engine.swapColumnIndexes(0, [[0, 1], [1, 2], [2, 0]]) @@ -163,3 +163,158 @@ describe('reorder working with redo', () => { expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) }) + +describe('setting column order - checking if it is possible', () => { + it('should check for length', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetColumnOrder(0, [0])).toEqual(false) + expect(() => + engine.setColumnOrder(0, [0]) + ).toThrowError('Invalid arguments, expected number of columns provided to be sheet width.') + }) + + it('should validate sources for noninteger values', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetColumnOrder(0, [0, 0.5])).toEqual(false) + expect(() => + engine.setColumnOrder(0, [0, 0.5]) + ).toThrowError('Invalid arguments, expected number of columns provided to be sheet width.') + }) + + it('should validate for repeated values', () => { + const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [0, 1, 1])).toEqual(false) + expect(() => + engine.setColumnOrder(0, [0, 1, 1]) + ).toThrowError('Invalid arguments, expected target column numbers to be permutation of source column numbers.') + }) + + it('should validate sources to be permutation of targets', () => { + const engine = HyperFormula.buildFromArray([[0, 0, 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 3])).toEqual(false) + expect(() => + engine.setColumnOrder(0, [1, 2, 3]) + ).toThrowError('Invalid arguments, expected target column numbers to be permutation of source column numbers.') + }) + + it('should check for matrices', () => { + const engine = HyperFormula.buildFromArray([[0, 0, '{=1}']]) + expect(engine.isItPossibleToSetColumnOrder(0, [2, 1, 0])).toEqual(false) + expect(() => + engine.setColumnOrder(0, [2, 1, 0]) + ).toThrowError('Cannot perform this operation, source location has a matrix inside.') + }) + + it('should check for matrices only in moved columns', () => { + const engine = HyperFormula.buildFromArray([[0, '{=1}', 0]]) + expect(engine.isItPossibleToSetColumnOrder(0, [2, 1, 0])).toEqual(true) + expect(() => + engine.setColumnOrder(0, [2, 1, 0]) + ).not.toThrowError() + }) +}) + +describe('reorder base case', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 'abcd'], [3, 3], [5, true]]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 0])).toEqual(true) + engine.setColumnOrder(0, [1, 0]) + expect(engine.getSheetSerialized(0)).toEqual([['abcd', 1], [3, 3], [true, 5]]) + }) + + it('should return number of changed cells', () => { + const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5, 6]]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 0])).toEqual(true) + const ret = engine.setColumnOrder(0, [1, 0]) + expect(ret.length).toEqual(6) + }) + + it('should work on static engine with uneven column', () => { + const engine = HyperFormula.buildFromArray([[1, 2], [3, 4], [5]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 0])).toEqual(true) + engine.setColumnOrder(0, [1, 0]) + expect(engine.getSheetSerialized(0)).toEqual([[2, 1], [4, 3], [null, 5]]) + }) + + it('should work with more complicated permutations', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 0])).toEqual(true) + engine.setColumnOrder(0, [1, 2, 0]) + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) + }) + + it('should not move values unnecessarily', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + expect(engine.isItPossibleToSetColumnOrder(0, [0, 1, 2])).toEqual(true) + const ret = engine.setColumnOrder(0, [0, 1, 2]) + expect(ret.length).toEqual(0) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 0, 3, 4])).toEqual(true) + engine.setColumnOrder(0, [1, 2, 0, 3, 4]) + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) + expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 0, 3])).toEqual(true) + engine.setColumnOrder(0, [1, 2, 0, 3]) + expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) + }) +}) + +describe('reorder working with undo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [1, 2, 0]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [1, 2, 0, 3, 4]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + engine.setColumnOrder(0, [1, 2, 0, 3]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + }) +}) + +describe('reorder working with redo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [1, 2, 0]) + engine.undo() + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 0])).toEqual(true) + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2], [6, 4, 5], [9, 7, 8]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3, '=A1', '=SUM(A2:A3)'], [4, 5, 6], [7, 8, 9]]) + engine.setColumnOrder(0, [1, 2, 0, 3, 4]) + engine.undo() + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 0, 3, 4])).toEqual(true) + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[3, 1, 2, '=A1', '=SUM(A2:A3)'], [6, 4, 5], [9, 7, 8]]) + expect(engine.getSheetValues(0)).toEqual([[3, 1, 2, 3, 15], [6, 4, 5], [9, 7, 8]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], [1, '=SUM(B10:B15)', '=A10'], ['=SUM(D1:D10)', 8, 9]]) + engine.setColumnOrder(0, [1, 2, 0, 3]) + engine.undo() + expect(engine.isItPossibleToSetColumnOrder(0, [1, 2, 0, 3, 4])).toEqual(true) + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) + }) +}) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 80ed5dad2c..dae651bfa8 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -1,7 +1,7 @@ import {HyperFormula} from '../../src' import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddressMappingPolicy' -describe('setting row order - checking if it is possible', () => { +describe('swapping rows - checking if it is possible', () => { it('should validate numbers for negative rows', () => { const engine = HyperFormula.buildFromArray([[]]) expect(engine.isItPossibleToSwapRowIndexes(0, [[-1, 0]])).toEqual(false) @@ -59,7 +59,7 @@ describe('setting row order - checking if it is possible', () => { }) }) -describe('should correctly work', () => { +describe('swapping rows should correctly work', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 'abcd', 3], [3, 5, true]]) expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 0]])).toEqual(true) @@ -110,7 +110,7 @@ describe('should correctly work', () => { }) }) -describe('reorder working with undo', () => { +describe('swapping rows working with undo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) @@ -133,7 +133,7 @@ describe('reorder working with undo', () => { }) }) -describe('reorder working with redo', () => { +describe('swapping rows working with redo', () => { it('should work on static engine', () => { const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) engine.swapRowIndexes(0, [[0, 1], [1, 2], [2, 0]]) @@ -162,3 +162,165 @@ describe('reorder working with redo', () => { expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) }) + +function fillValues(order: number[], fill: number): number[] { + while(order.length < fill) { + const x = order.length + order[x] = x + } + return order +} + +describe('setting row order - checking if it is possible', () => { + it('should check for length', () => { + const engine = HyperFormula.buildFromArray([[]]) + expect(engine.isItPossibleToSetRowOrder(0, [0])).toEqual(false) + expect(() => + engine.setRowOrder(0, [0]) + ).toThrowError('Invalid arguments, expected number of rows provided to be sheet height.') + }) + + it('should validate sources for noninteger values', () => { + const engine = HyperFormula.buildFromArray([[0], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [0, 0.5])).toEqual(false) + expect(() => + engine.setRowOrder(0, [0, 0.5]) + ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') + }) + + it('should validate for repeated values', () => { + const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [0, 1, 1])).toEqual(false) + expect(() => + engine.setRowOrder(0, [0, 1, 1]) + ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') + }) + + it('should validate sources to be permutation of targets', () => { + const engine = HyperFormula.buildFromArray([[0], [0], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [1, 2, 3])).toEqual(false) + expect(() => + engine.setRowOrder(0, [1, 2, 3]) + ).toThrowError('Invalid arguments, expected target row numbers to be permutation of source row numbers.') + }) + + it('should check for matrices', () => { + const engine = HyperFormula.buildFromArray([[0], [0], ['{=1}']]) + expect(engine.isItPossibleToSetRowOrder(0, [2, 1, 0])).toEqual(false) + expect(() => + engine.setRowOrder(0, [2, 1, 0]) + ).toThrowError('Cannot perform this operation, source location has a matrix inside.') + }) + + it('should check for matrices only in moved rows', () => { + const engine = HyperFormula.buildFromArray([[0], ['{=1}'], [0]]) + expect(engine.isItPossibleToSetRowOrder(0, [2, 1, 0])).toEqual(true) + expect(() => + engine.setRowOrder(0, [2, 1, 0]) + ).not.toThrowError() + }) +}) + +describe('reorder base case', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 'abcd', 3], [3, 5, true]]) + expect(engine.isItPossibleToSetRowOrder(0, [1, 0])).toEqual(true) + engine.setRowOrder(0, [1, 0]) + expect(engine.getSheetSerialized(0)).toEqual([[3, 5, true], [1, 'abcd', 3]]) + }) + + it('should return number of changed cells', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + expect(engine.isItPossibleToSetRowOrder(0, [1, 0])).toEqual(true) + const ret = engine.setRowOrder(0, [1, 0]) + expect(ret.length).toEqual(6) + }) + + it('should work on static engine with uneven rows', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6, 7, 8]], {chooseAddressMappingPolicy: new AlwaysSparse()}) + expect(engine.isItPossibleToSetRowOrder(0, [1, 0])).toEqual(true) + engine.setRowOrder(0, [1, 0]) + expect(engine.getSheetSerialized(0)).toEqual([[4, 5, 6, 7, 8], [1, 2, 3]]) + }) + + it('should work with more complicated permutations', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + expect(engine.isItPossibleToSetRowOrder(0, [1, 2, 0])).toEqual(true) + engine.setRowOrder(0, [1, 2, 0]) + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) + }) + + it('should not move values unnecessarily', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6]]) + expect(engine.isItPossibleToSetRowOrder(0, [0, 1])).toEqual(true) + const ret = engine.setRowOrder(0, [0, 1]) + expect(ret.length).toEqual(0) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [1, 2, 0, 3]) + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) + expect(engine.isItPossibleToSetRowOrder(0, fillValues([1, 2, 0], 15))).toEqual(true) + engine.setRowOrder(0, fillValues([1, 2, 0], 15)) + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) + }) +}) + +describe('reorder working with undo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setRowOrder(0, [1, 2, 0]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [1, 2, 0, 3]) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) + engine.setRowOrder(0, fillValues([1, 2, 0], 15)) + engine.undo() + expect(engine.getSheetSerialized(0)).toEqual([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) + }) +}) + +describe('reorder working with redo', () => { + it('should work on static engine', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) + engine.setRowOrder(0, [1, 2, 0]) + engine.undo() + expect(engine.isItPossibleToSetRowOrder(0, [1, 2, 0])).toEqual(true) + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6]]) + }) + + it('should work with external references', () => { + const engine = HyperFormula.buildFromArray([[1, 2, 3], [4, 5, 6], [7, 8, 9], ['=A1', '=SUM(A2:A3)']]) + engine.setRowOrder(0, [1, 2, 0, 3]) + engine.undo() + expect(engine.isItPossibleToSetRowOrder(0, [1, 2, 0, 3])).toEqual(true) + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], ['=A1', '=SUM(A2:A3)']]) + expect(engine.getSheetValues(0)).toEqual([[7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 5]]) + }) + + it('should work with internal references', () => { + const engine = HyperFormula.buildFromArray([['=A2', '=SUM(B2:B3)', 3], ['=A10', '=SUM(B10:B15)', 6], ['=SUM(C1:C10)', 8, 9]]) + engine.setRowOrder(0, fillValues([1, 2, 0], 15)) + engine.undo() + expect(engine.isItPossibleToSetRowOrder(0, fillValues( [1, 2, 0], 16))).toEqual(true) + engine.redo() + expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) + }) +}) From 2339770810a631e168137e00d27809825763337a Mon Sep 17 00:00:00 2001 From: izulin Date: Fri, 1 Jan 2021 19:38:14 +0100 Subject: [PATCH 23/36] abort cut --- src/CrudOperations.ts | 6 +++--- test/cruds/cut-paste.spec.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index c9d62e61ce..db3212ba60 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -323,7 +323,7 @@ export class CrudOperations { this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') } - public mappingFromOrder(sheetId: number, newOrder: number[], rowOrColumn: 'row' | 'column'): [number,number][] { + public mappingFromOrder(sheetId: number, newOrder: number[], rowOrColumn: 'row' | 'column'): [number, number][] { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) } @@ -331,10 +331,10 @@ export class CrudOperations { if(newOrder.length !== limit) { throw new InvalidArgumentsError(`number of ${rowOrColumn}s provided to be sheet ${rowOrColumn==='row'?'height':'width'}.`) } - const ret: [number,number][] = [] + const ret: [number, number][] = [] for(let i=0;i { expect(engine.isClipboardEmpty()).toBe(true) }) + + it('should be aborted when swapping rows', () => { + const engine = HyperFormula.buildFromArray([['1']]) + engine.cut(adr('A1'), 1, 1) + engine.swapRowIndexes(0, [[0,0]]) + expect(engine.isClipboardEmpty()).toBe(true) + }) + + it('should be aborted when swapping columns', () => { + const engine = HyperFormula.buildFromArray([['1']]) + engine.cut(adr('A1'), 1, 1) + engine.swapColumnIndexes(0, [[0,0]]) + expect(engine.isClipboardEmpty()).toBe(true) + }) + + it('should be aborted when setting row order', () => { + const engine = HyperFormula.buildFromArray([['1']]) + engine.cut(adr('A1'), 1, 1) + engine.setRowOrder(0, [0]) + expect(engine.isClipboardEmpty()).toBe(true) + }) + + it('should be aborted when setting column order', () => { + const engine = HyperFormula.buildFromArray([['1']]) + engine.cut(adr('A1'), 1, 1) + engine.setColumnOrder(0, [0]) + expect(engine.isClipboardEmpty()).toBe(true) + }) }) From 05e1962d38a88add6095f17d82eae3216708d08b Mon Sep 17 00:00:00 2001 From: izulin Date: Fri, 1 Jan 2021 19:43:53 +0100 Subject: [PATCH 24/36] clear redo stack --- test/cruds/set-column-order.spec.ts | 21 +++++++++++++++++++++ test/cruds/set-row-order.spec.ts | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index 2cde9fec80..8a8518c175 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -1,5 +1,6 @@ import {HyperFormula} from '../../src' import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddressMappingPolicy' +import {adr} from '../testUtils' describe('swapping columns - checking if it is possible', () => { it('should validate numbers for negative columns', () => { @@ -162,6 +163,16 @@ describe('swapping rows working with redo', () => { engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) + + it('clears redo stack', () => { + const engine = HyperFormula.buildFromArray([[1]]) + engine.setCellContents(adr('A1'), 42) + engine.undo() + + engine.swapColumnIndexes(0, [[0,0]]) + + expect(engine.isThereSomethingToRedo()).toBe(false) + }) }) describe('setting column order - checking if it is possible', () => { @@ -317,4 +328,14 @@ describe('reorder working with redo', () => { engine.redo() expect(engine.getSheetSerialized(0)).toEqual([[3, '=B2', '=SUM(C2:C3)'], ['=#REF!', 1, '=SUM(C10:C15)'], [9, '=SUM(E1:E10)', 8]]) }) + + it('clears redo stack', () => { + const engine = HyperFormula.buildFromArray([[1]]) + engine.setCellContents(adr('A1'), 42) + engine.undo() + + engine.setColumnOrder(0, [0]) + + expect(engine.isThereSomethingToRedo()).toBe(false) + }) }) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index dae651bfa8..70379a5d34 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -1,5 +1,6 @@ import {HyperFormula} from '../../src' import {AlwaysSparse} from '../../src/DependencyGraph/AddressMapping/ChooseAddressMappingPolicy' +import {adr} from '../testUtils' describe('swapping rows - checking if it is possible', () => { it('should validate numbers for negative rows', () => { @@ -161,6 +162,16 @@ describe('swapping rows working with redo', () => { engine.redo() expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) + + it('clears redo stack', () => { + const engine = HyperFormula.buildFromArray([[1]]) + engine.setCellContents(adr('A1'), 42) + engine.undo() + + engine.swapRowIndexes(0, [[0,0]]) + + expect(engine.isThereSomethingToRedo()).toBe(false) + }) }) function fillValues(order: number[], fill: number): number[] { @@ -323,4 +334,14 @@ describe('reorder working with redo', () => { engine.redo() expect(engine.getSheetSerialized(0)).toEqual([['=SUM(#REF!)', 8, 9], ['=A3', '=SUM(B3:B4)', 3], ['=A11', '=SUM(B11:B16)', 6]]) }) + + it('clears redo stack', () => { + const engine = HyperFormula.buildFromArray([[1]]) + engine.setCellContents(adr('A1'), 42) + engine.undo() + + engine.setRowOrder(0, [0]) + + expect(engine.isThereSomethingToRedo()).toBe(false) + }) }) From fb80fc6443403fe6ff89550d1c3b514bb4bbb4c0 Mon Sep 17 00:00:00 2001 From: izulin Date: Fri, 1 Jan 2021 19:44:40 +0100 Subject: [PATCH 25/36] linter --- test/cruds/cut-paste.spec.ts | 4 ++-- test/cruds/set-column-order.spec.ts | 2 +- test/cruds/set-row-order.spec.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/cruds/cut-paste.spec.ts b/test/cruds/cut-paste.spec.ts index 8d61373eac..ce1450179d 100644 --- a/test/cruds/cut-paste.spec.ts +++ b/test/cruds/cut-paste.spec.ts @@ -1038,14 +1038,14 @@ describe('aborting cut paste', () => { it('should be aborted when swapping rows', () => { const engine = HyperFormula.buildFromArray([['1']]) engine.cut(adr('A1'), 1, 1) - engine.swapRowIndexes(0, [[0,0]]) + engine.swapRowIndexes(0, [[0, 0]]) expect(engine.isClipboardEmpty()).toBe(true) }) it('should be aborted when swapping columns', () => { const engine = HyperFormula.buildFromArray([['1']]) engine.cut(adr('A1'), 1, 1) - engine.swapColumnIndexes(0, [[0,0]]) + engine.swapColumnIndexes(0, [[0, 0]]) expect(engine.isClipboardEmpty()).toBe(true) }) diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index 8a8518c175..f45567e5dc 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -169,7 +169,7 @@ describe('swapping rows working with redo', () => { engine.setCellContents(adr('A1'), 42) engine.undo() - engine.swapColumnIndexes(0, [[0,0]]) + engine.swapColumnIndexes(0, [[0, 0]]) expect(engine.isThereSomethingToRedo()).toBe(false) }) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index 70379a5d34..fdac384e6a 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -168,7 +168,7 @@ describe('swapping rows working with redo', () => { engine.setCellContents(adr('A1'), 42) engine.undo() - engine.swapRowIndexes(0, [[0,0]]) + engine.swapRowIndexes(0, [[0, 0]]) expect(engine.isThereSomethingToRedo()).toBe(false) }) From 7cb9734203e95319c772f5cc49cad8b3528320ae Mon Sep 17 00:00:00 2001 From: izulin Date: Sun, 3 Jan 2021 14:20:11 +0100 Subject: [PATCH 26/36] docs --- src/HyperFormula.ts | 181 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 2 deletions(-) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 8518a1bf3a..886ba68d7f 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1127,7 +1127,7 @@ export class HyperFormula implements TypedEmitter { } /** - * Reorders rows of a sheet according to a permutation. + * Reorders rows of a sheet according to a source-target mapping. * * Note that this method may trigger dependency graph recalculation. * @@ -1175,6 +1175,29 @@ export class HyperFormula implements TypedEmitter { return this.recomputeIfDependencyGraphNeedsIt() } + /** + * Checks if it is possible to reorder rows of a sheet according to a source-target mapping. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {[number, number][]} rowMapping - array mapping original positions to final positions of rows + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1], + * [2], + * [4, 5], + * ]); + * + * // returns true + * hfInstance.swapRowIndexes(0, [[0,2],[2,0]]); + * + * // returns false + * hfInstance.swapRowIndexes(0, [[0,1]]); + * ``` + * + * @category Rows + */ public isItPossibleToSwapRowIndexes(sheetId: number, rowMapping: [number, number][]): boolean { try { this._crudOperations.validateSwapRowIndexes(sheetId, rowMapping) @@ -1184,11 +1207,78 @@ export class HyperFormula implements TypedEmitter { } } + /** + * Reorders rows of a sheet according to a permutation. + * + * Note that this method may trigger dependency graph recalculation. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {number[]} newRowOrder - permutation of rows + * + * @fires [[valuesUpdated]] if recalculation was triggered by this change + * + * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist + * @throws an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet + * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1], + * [2], + * [4, 5], + * ]); + * + * // should set swap rows 0 and 2 in place, returns: + * // [{ + * // address: { sheet: 0, col: 0, row: 2 }, + * // newValue: 1, + * // }, + * // { + * // address: { sheet: 0, col: 1, row: 2 }, + * // newValue: null, + * // }, + * // { + * // address: { sheet: 0, col: 0, row: 0 }, + * // newValue: 4, + * // }, + * // { + * // address: { sheet: 0, col: 1, row: 0 }, + * // newValue: 5, + * // }] + * const changes = hfInstance.setRowOrder(0, [2, 1, 0]); + * ``` + * + * @category Rows + */ public setRowOrder(sheetId: number, newRowOrder: number[]): ExportedChange[] { const mapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'row') return this.swapRowIndexes(sheetId, mapping) } + /** + * Checks if it is possible to reorder rows of a sheet according to a permutation. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {number[]} newRowOrder - permutation of rows + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1], + * [2], + * [4, 5], + * ]); + * + * // returns true + * hfInstance.setRowOrder(0, [2, 1, 0]); + * + * // returns false + * hfInstance.setRowOrder(0, [2]); + * ``` + * + * @category Rows + */ public isItPossibleToSetRowOrder(sheetId: number, newRowOrder: number[]): boolean { try { const rowMapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'row') @@ -1200,7 +1290,7 @@ export class HyperFormula implements TypedEmitter { } /** - * Reorders columns of a sheet according to a permutation. + * Reorders columns of a sheet according to a source-target mapping. * * Note that this method may trigger dependency graph recalculation. * @@ -1247,6 +1337,27 @@ export class HyperFormula implements TypedEmitter { return this.recomputeIfDependencyGraphNeedsIt() } + /** + * Checks if it is possible to reorder columns of a sheet according to a source-target mapping. + * + * @fires [[valuesUpdated]] if recalculation was triggered by this change + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1, 2, 4], + * [5] + * ]); + * + * // returns true + * hfInstance.swapColumnIndexes(0, [[0,2],[2,0]]); + * + * // returns false + * hfInstance.swapColumnIndexes(0, [[0,1]]); + * ``` + * + * @category Columns + */ public isItPossibleToSwapColumnIndexes(sheetId: number, columnMapping: [number, number][]): boolean { try { this._crudOperations.validateSwapColumnIndexes(sheetId, columnMapping) @@ -1256,11 +1367,76 @@ export class HyperFormula implements TypedEmitter { return this._crudOperations.operations.testColumnOrderForMatrices(sheetId, columnMapping) } + /** + * Reorders columns of a sheet according to a permutation. + * + * Note that this method may trigger dependency graph recalculation. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {number[]} newColumnOrder - permutation of columns + * + * @fires [[valuesUpdated]] if recalculation was triggered by this change + * + * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist + * @throws an error when columnMapping does not define correct column permutation for some subset of columns of the given sheet + * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1, 2, 4], + * [5] + * ]); + * + * // should set swap columns 0 and 2 in place, returns: + * // [{ + * // address: { sheet: 0, col: 2, row: 0 }, + * // newValue: 1, + * // }, + * // { + * // address: { sheet: 0, col: 2, row: 1 }, + * // newValue: 5, + * // }, + * // { + * // address: { sheet: 0, col: 0, row: 0 }, + * // newValue: 4, + * // }, + * // { + * // address: { sheet: 0, col: 0, row: 1 }, + * // newValue: null, + * // }] + * const changes = hfInstance.setColumnOrder(0, [2, 1, 0]]); + * ``` + * + * @category Columns + */ public setColumnOrder(sheetId: number, newColumnOrder: number[]): ExportedChange[] { const mapping = this._crudOperations.mappingFromOrder(sheetId, newColumnOrder, 'column') return this.swapColumnIndexes(sheetId, mapping) } + /** + * Checks if it possible to reorder columns of a sheet according to a permutation. + * + * @param {number} sheetId - ID of a sheet to operate on + * @param {number[]} newColumnOrder - permutation of columns + * + * @example + * ```js + * const hfInstance = HyperFormula.buildFromArray([ + * [1, 2, 4], + * [5] + * ]); + * + * // returns true + * hfInstance.setColumnOrder(0, [2, 1, 0]]); + * + * // returns false + * hfInstance.setColumnOrder(0, [1]]); + * ``` + * + * @category Columns + */ public isItPossibleToSetColumnOrder(sheetId: number, newRowOrder: number[]): boolean { try { const columnMapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'column') @@ -1270,6 +1446,7 @@ export class HyperFormula implements TypedEmitter { return false } } + /** * Returns information whether it is possible to add rows into a specified position in a given sheet. * Checks against particular rules to ascertain that addRows can be called. From 4c7e3ee724ee39df4ab062e64e3e895eeea6337f Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 4 Jan 2021 11:51:54 +0100 Subject: [PATCH 27/36] simpler --- src/CrudOperations.ts | 8 ++------ test/cruds/set-column-order.spec.ts | 2 +- test/cruds/set-row-order.spec.ts | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index db3212ba60..fc804e1bc5 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -345,17 +345,13 @@ export class CrudOperations { const sources = rowMapping.map(([a, b])=>a).sort((a, b) => a-b) const targets = rowMapping.map(([a, b])=>b).sort((a, b) => a-b) - for(const pos of sources) { - if(!isNonnegativeInteger(pos) || pos >= limit) { + for(let i=0;i= limit) { throw new InvalidArgumentsError(`${rowOrColumn} numbers to be nonnegative integers and less than sheet ${rowOrColumn==='row'?'height':'width'}.`) } - } - for(let i=0;i { const engine = HyperFormula.buildFromArray([[0, 0, 0]]) expect(engine.isItPossibleToSwapColumnIndexes(0, [[1, 1], [3, 0]])).toEqual(false) expect(() => - engine.swapColumnIndexes(0, [[1, 1], [3, 0]]) + engine.swapColumnIndexes(0, [[3, 0]]) ).toThrowError('Invalid arguments, expected column numbers to be nonnegative integers and less than sheet width.') }) diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index fdac384e6a..b8f5c7b070 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -23,7 +23,7 @@ describe('swapping rows - checking if it is possible', () => { const engine = HyperFormula.buildFromArray([[0], [0], [0]]) expect(engine.isItPossibleToSwapRowIndexes(0, [[1, 1], [3, 0]])).toEqual(false) expect(() => - engine.swapRowIndexes(0, [[1, 1], [3, 0]]) + engine.swapRowIndexes(0, [[3, 0]]) ).toThrowError('Invalid arguments, expected row numbers to be nonnegative integers and less than sheet height.') }) From 281a28f193c39ba2293a0a7da181941ffe877ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Uzna=C5=84ski?= <40573492+izulin@users.noreply.github.com> Date: Mon, 4 Jan 2021 12:24:52 +0100 Subject: [PATCH 28/36] Update src/CrudOperations.ts Co-authored-by: Wojciech Czerniak --- src/CrudOperations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index fc804e1bc5..5f11d636fd 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -343,7 +343,7 @@ export class CrudOperations { private validateRowOrColumnMapping(sheetId: number, rowMapping: [number, number][], rowOrColumn: 'row' | 'column'): void { const limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId) const sources = rowMapping.map(([a, b])=>a).sort((a, b) => a-b) - const targets = rowMapping.map(([a, b])=>b).sort((a, b) => a-b) + const targets = rowMapping.map(([_, b])=>b).sort((a, b) => a-b) for(let i=0;i= limit) { From cf0780ad48d9201fb1f5f1d175c82d4a08f1f8ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Uzna=C5=84ski?= <40573492+izulin@users.noreply.github.com> Date: Mon, 4 Jan 2021 12:25:06 +0100 Subject: [PATCH 29/36] Update src/CrudOperations.ts Co-authored-by: Wojciech Czerniak --- src/CrudOperations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 5f11d636fd..aa0743165e 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -342,7 +342,7 @@ export class CrudOperations { private validateRowOrColumnMapping(sheetId: number, rowMapping: [number, number][], rowOrColumn: 'row' | 'column'): void { const limit = rowOrColumn === 'row' ? this.dependencyGraph.getSheetHeight(sheetId) : this.dependencyGraph.getSheetWidth(sheetId) - const sources = rowMapping.map(([a, b])=>a).sort((a, b) => a-b) + const sources = rowMapping.map(([a, _])=>a).sort((a, b) => a-b) const targets = rowMapping.map(([_, b])=>b).sort((a, b) => a-b) for(let i=0;i Date: Mon, 4 Jan 2021 12:26:23 +0100 Subject: [PATCH 30/36] Update src/HyperFormula.ts Co-authored-by: Wojciech Czerniak --- src/HyperFormula.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 886ba68d7f..76570a1eaf 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1190,10 +1190,10 @@ export class HyperFormula implements TypedEmitter { * ]); * * // returns true - * hfInstance.swapRowIndexes(0, [[0,2],[2,0]]); + * const isSwappable = hfInstance.isItPossibleToSwapRowIndexes(0, [[0,2],[2,0]]); * * // returns false - * hfInstance.swapRowIndexes(0, [[0,1]]); + * const isSwappable = hfInstance.isItPossibleToSwapRowIndexes(0, [[0,1]]); * ``` * * @category Rows From 76371921cdf71f0042f32a536540101a77b3d9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Uzna=C5=84ski?= <40573492+izulin@users.noreply.github.com> Date: Mon, 4 Jan 2021 12:26:32 +0100 Subject: [PATCH 31/36] Update src/HyperFormula.ts Co-authored-by: Wojciech Czerniak --- src/HyperFormula.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 76570a1eaf..9900444f8d 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1137,7 +1137,7 @@ export class HyperFormula implements TypedEmitter { * @fires [[valuesUpdated]] if recalculation was triggered by this change * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist - * @throws an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet + * @throws [[InvalidArgumentsError]] an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example From 3f595ac9b176942882938ebbc64cfb97ad7807b7 Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 4 Jan 2021 12:29:23 +0100 Subject: [PATCH 32/36] docs --- src/HyperFormula.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 9900444f8d..c1a832ce12 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1137,7 +1137,7 @@ export class HyperFormula implements TypedEmitter { * @fires [[valuesUpdated]] if recalculation was triggered by this change * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist - * @throws [[InvalidArgumentsError]] an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet + * @throws [[InvalidArgumentsError]] when rowMapping does not define correct row permutation for some subset of rows of the given sheet * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example @@ -1218,7 +1218,7 @@ export class HyperFormula implements TypedEmitter { * @fires [[valuesUpdated]] if recalculation was triggered by this change * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist - * @throws an error when rowMapping does not define correct row permutation for some subset of rows of the given sheet + * @throws [[InvalidArgumentsError]] when rowMapping does not define correct row permutation for some subset of rows of the given sheet * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example @@ -1271,10 +1271,10 @@ export class HyperFormula implements TypedEmitter { * ]); * * // returns true - * hfInstance.setRowOrder(0, [2, 1, 0]); + * hfInstance.isItPossibleToSetRowOrder(0, [2, 1, 0]); * * // returns false - * hfInstance.setRowOrder(0, [2]); + * hfInstance.isItPossibleToSetRowOrder(0, [2]); * ``` * * @category Rows @@ -1300,7 +1300,7 @@ export class HyperFormula implements TypedEmitter { * @fires [[valuesUpdated]] if recalculation was triggered by this change * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist - * @throws an error when columnMapping does not define correct column permutation for some subset of columns of the given sheet + * @throws [[InvalidArgumentsError]] when columnMapping does not define correct column permutation for some subset of columns of the given sheet * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example @@ -1350,10 +1350,10 @@ export class HyperFormula implements TypedEmitter { * ]); * * // returns true - * hfInstance.swapColumnIndexes(0, [[0,2],[2,0]]); + * hfInstance.isItPossibleToSwapColumnIndexes(0, [[0,2],[2,0]]); * * // returns false - * hfInstance.swapColumnIndexes(0, [[0,1]]); + * hfInstance.isItPossibleToSwapColumnIndexes(0, [[0,1]]); * ``` * * @category Columns @@ -1378,7 +1378,7 @@ export class HyperFormula implements TypedEmitter { * @fires [[valuesUpdated]] if recalculation was triggered by this change * * @throws [[NoSheetWithIdError]] when the given sheet ID does not exist - * @throws an error when columnMapping does not define correct column permutation for some subset of columns of the given sheet + * @throws [[InvalidArgumentsError]] when columnMapping does not define correct column permutation for some subset of columns of the given sheet * @throws [[SourceLocationHasMatrixError]] when the selected position has matrix inside * * @example @@ -1429,10 +1429,10 @@ export class HyperFormula implements TypedEmitter { * ]); * * // returns true - * hfInstance.setColumnOrder(0, [2, 1, 0]]); + * hfInstance.isItPossibleToSetColumnOrder(0, [2, 1, 0]]); * * // returns false - * hfInstance.setColumnOrder(0, [1]]); + * hfInstance.isItPossibleToSetColumnOrder(0, [1]]); * ``` * * @category Columns From 1c75d16c0370f4aba22e3c530862a3f40f28aabf Mon Sep 17 00:00:00 2001 From: izulin Date: Mon, 4 Jan 2021 14:11:26 +0100 Subject: [PATCH 33/36] warnings --- src/Operations.ts | 39 ++++++++++++++++++------------------- src/parser/CellAddress.ts | 5 +++-- src/parser/ColumnAddress.ts | 4 ++-- src/parser/RowAddress.ts | 4 ++-- src/parser/Unparser.ts | 2 +- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/Operations.ts b/src/Operations.ts index 8c97894300..67ef852ba3 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -3,16 +3,14 @@ * Copyright (c) 2020 Handsoncode. All rights reserved. */ -import {Statistics, StatType} from './statistics' -import {ClipboardCell, ClipboardCellType} from './ClipboardOperations' +import {AbsoluteCellRange} from './AbsoluteCellRange' +import {absolutizeDependencies} from './absolutizeDependencies' import {EmptyValue, invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress} from './Cell' import {CellContent, CellContentParser, RawCellContent} from './CellContentParser' -import {ColumnsSpan, RowsSpan} from './Span' +import {ClipboardCell, ClipboardCellType} from './ClipboardOperations' +import {Config} from './Config' import {ContentChanges} from './ContentChanges' -import {ColumnSearchStrategy} from './Lookup/SearchStrategy' -import {absolutizeDependencies} from './absolutizeDependencies' -import {LazilyTransformingAstService} from './LazilyTransformingAstService' -import {buildMatrixVertex} from './GraphBuilder' +import {ColumnRowIndex} from './CrudOperations' import { AddressMapping, CellVertex, @@ -26,8 +24,13 @@ import { ValueCellVertex } from './DependencyGraph' import {ValueCellVertexValue} from './DependencyGraph/ValueCellVertex' +import {AddColumnsTransformer} from './dependencyTransformers/AddColumnsTransformer' +import {AddRowsTransformer} from './dependencyTransformers/AddRowsTransformer' +import {MoveCellsTransformer} from './dependencyTransformers/MoveCellsTransformer' +import {RemoveColumnsTransformer} from './dependencyTransformers/RemoveColumnsTransformer' +import {RemoveRowsTransformer} from './dependencyTransformers/RemoveRowsTransformer' +import {RemoveSheetTransformer} from './dependencyTransformers/RemoveSheetTransformer' import { - InvalidAddressError, InvalidArgumentsError, NamedExpressionDoesNotExistError, NoRelativeAddressesAllowedError, @@ -35,24 +38,20 @@ import { SourceLocationHasMatrixError, TargetLocationHasMatrixError } from './errors' -import {NamedExpressionDependency, ParserWithCaching, ProcedureAst, RelativeDependency} from './parser' -import {ParsingError} from './parser/Ast' -import {AddRowsTransformer} from './dependencyTransformers/AddRowsTransformer' -import {RemoveRowsTransformer} from './dependencyTransformers/RemoveRowsTransformer' -import {AddColumnsTransformer} from './dependencyTransformers/AddColumnsTransformer' -import {MoveCellsTransformer} from './dependencyTransformers/MoveCellsTransformer' -import {RemoveSheetTransformer} from './dependencyTransformers/RemoveSheetTransformer' -import {RemoveColumnsTransformer} from './dependencyTransformers/RemoveColumnsTransformer' -import {AbsoluteCellRange} from './AbsoluteCellRange' -import {findBoundaries, Sheet} from './Sheet' -import {Config} from './Config' +import {buildMatrixVertex} from './GraphBuilder' +import {LazilyTransformingAstService} from './LazilyTransformingAstService' +import {ColumnSearchStrategy} from './Lookup/SearchStrategy' import { doesContainRelativeReferences, InternalNamedExpression, NamedExpressionOptions, NamedExpressions } from './NamedExpressions' -import {ColumnRowIndex} from './CrudOperations' +import {NamedExpressionDependency, ParserWithCaching, ProcedureAst, RelativeDependency} from './parser' +import {ParsingError} from './parser/Ast' +import {findBoundaries, Sheet} from './Sheet' +import {ColumnsSpan, RowsSpan} from './Span' +import {Statistics, StatType} from './statistics' export class RemoveRowsCommand { constructor( diff --git a/src/parser/CellAddress.ts b/src/parser/CellAddress.ts index 0cd33662da..9a7953b6cb 100644 --- a/src/parser/CellAddress.ts +++ b/src/parser/CellAddress.ts @@ -4,7 +4,8 @@ */ import { - absoluteSheetReference, ErrorType, invalidSimpleCellAddress, + absoluteSheetReference, + invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress, simpleColumnAddress, @@ -13,8 +14,8 @@ import { SimpleRowAddress, } from '../Cell' import {Maybe} from '../Maybe' -import {columnIndexToLabel} from './addressRepresentationConverters' import {AddressWithColumn, AddressWithRow} from './Address' +import {columnIndexToLabel} from './addressRepresentationConverters' /** Possible kinds of cell references */ export enum CellReferenceType { diff --git a/src/parser/ColumnAddress.ts b/src/parser/ColumnAddress.ts index a36d675d05..4389c4a6e6 100644 --- a/src/parser/ColumnAddress.ts +++ b/src/parser/ColumnAddress.ts @@ -5,14 +5,14 @@ import { absoluteSheetReference, - invalidSimpleCellAddress, invalidSimpleColumnAddress, + invalidSimpleColumnAddress, SimpleCellAddress, simpleColumnAddress, SimpleColumnAddress } from '../Cell' import {Maybe} from '../Maybe' -import {columnIndexToLabel} from './addressRepresentationConverters' import {AddressWithColumn} from './Address' +import {columnIndexToLabel} from './addressRepresentationConverters' export enum ReferenceType { RELATIVE = 'RELATIVE', diff --git a/src/parser/RowAddress.ts b/src/parser/RowAddress.ts index f95c67e68a..2736b17441 100644 --- a/src/parser/RowAddress.ts +++ b/src/parser/RowAddress.ts @@ -5,14 +5,14 @@ import { absoluteSheetReference, - invalidSimpleColumnAddress, invalidSimpleRowAddress, + invalidSimpleRowAddress, SimpleCellAddress, simpleRowAddress, SimpleRowAddress } from '../Cell' import {Maybe} from '../Maybe' -import {ReferenceType} from './ColumnAddress' import {AddressWithRow} from './Address' +import {ReferenceType} from './ColumnAddress' export class RowAddress implements AddressWithRow { private constructor( diff --git a/src/parser/Unparser.ts b/src/parser/Unparser.ts index 1b39f9bfa4..e7bb7e04db 100644 --- a/src/parser/Unparser.ts +++ b/src/parser/Unparser.ts @@ -3,7 +3,7 @@ * Copyright (c) 2020 Handsoncode. All rights reserved. */ -import {ErrorType, invalidSimpleCellAddress, SimpleCellAddress} from '../Cell' +import {ErrorType, SimpleCellAddress} from '../Cell' import {NoSheetWithIdError} from '../index' import {NamedExpressions} from '../NamedExpressions' import {SheetIndexMappingFn, sheetIndexToString} from './addressRepresentationConverters' From 4cb55c76f5c58e8f79e93b96813601c7ba4f444d Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 6 Jan 2021 12:53:23 +0100 Subject: [PATCH 34/36] refactor --- src/CrudOperations.ts | 26 ++++++++++++++++++++++++++ src/HyperFormula.ts | 8 ++++---- src/Operations.ts | 24 ------------------------ 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index aa0743165e..4200484228 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -308,6 +308,19 @@ export class CrudOperations { this.validateRowOrColumnMapping(sheetId, rowMapping, 'row') } + public testColumnOrderForMatrices(sheetId: number, columnMapping: [number, number][]): boolean { + for(const [source, target] of columnMapping ) { + if(source!==target) { + const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: source, row: 0}, 1, Infinity) + if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { + return false + } + } + } + return true + } + + public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { this.validateSwapColumnIndexes(sheetId, columnMapping) this.undoRedo.clearRedoStack() @@ -323,6 +336,19 @@ export class CrudOperations { this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') } + public testRowOrderForMatrices(sheetId: number, rowMapping: [number, number][]): boolean { + for(const [source, target] of rowMapping ) { + if(source!==target) { + const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source}, Infinity, 1) + if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { + return false + } + } + } + return true + } + + public mappingFromOrder(sheetId: number, newOrder: number[], rowOrColumn: 'row' | 'column'): [number, number][] { if (!this.sheetMapping.hasSheetWithId(sheetId)) { throw new NoSheetWithIdError(sheetId) diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index c1a832ce12..0214d56da5 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1201,7 +1201,7 @@ export class HyperFormula implements TypedEmitter { public isItPossibleToSwapRowIndexes(sheetId: number, rowMapping: [number, number][]): boolean { try { this._crudOperations.validateSwapRowIndexes(sheetId, rowMapping) - return this._crudOperations.operations.testRowOrderForMatrices(sheetId, rowMapping) + return this._crudOperations.testRowOrderForMatrices(sheetId, rowMapping) } catch (e) { return false } @@ -1283,7 +1283,7 @@ export class HyperFormula implements TypedEmitter { try { const rowMapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'row') this._crudOperations.validateSwapRowIndexes(sheetId, rowMapping) - return this._crudOperations.operations.testRowOrderForMatrices(sheetId, rowMapping) + return this._crudOperations.testRowOrderForMatrices(sheetId, rowMapping) } catch (e) { return false } @@ -1364,7 +1364,7 @@ export class HyperFormula implements TypedEmitter { } catch (e) { return false } - return this._crudOperations.operations.testColumnOrderForMatrices(sheetId, columnMapping) + return this._crudOperations.testColumnOrderForMatrices(sheetId, columnMapping) } /** @@ -1441,7 +1441,7 @@ export class HyperFormula implements TypedEmitter { try { const columnMapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'column') this._crudOperations.validateSwapColumnIndexes(sheetId, columnMapping) - return this._crudOperations.operations.testColumnOrderForMatrices(sheetId, columnMapping) + return this._crudOperations.testColumnOrderForMatrices(sheetId, columnMapping) } catch (e) { return false } diff --git a/src/Operations.ts b/src/Operations.ts index 67ef852ba3..62b5937994 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -331,18 +331,6 @@ export class Operations { ) } - public testRowOrderForMatrices(sheetId: number, rowMapping: [number, number][]): boolean { - for(const [source, target] of rowMapping ) { - if(source!==target) { - const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source}, Infinity, 1) - if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { - return false - } - } - } - return true - } - public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { const buffer: [SimpleCellAddress, ClipboardCell][][] = [] for(const [source, target] of columnMapping ) { @@ -365,18 +353,6 @@ export class Operations { ) } - public testColumnOrderForMatrices(sheetId: number, columnMapping: [number, number][]): boolean { - for(const [source, target] of columnMapping ) { - if(source!==target) { - const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: source, row: 0}, 1, Infinity) - if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { - return false - } - } - } - return true - } - public addNamedExpression(expressionName: string, expression: RawCellContent, sheetId?: number, options?: NamedExpressionOptions) { this.storeNamedExpressionInCell(this.namedExpressions.lookupNextAddress(expressionName, sheetId), expression) const namedExpression = this.namedExpressions.addNamedExpression(expressionName, sheetId, options) From bda2cbfb3203ffc0fd0d499fac94d89eb7ae0231 Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 6 Jan 2021 13:08:41 +0100 Subject: [PATCH 35/36] refactor --- src/CrudOperations.ts | 12 ++++++------ src/HyperFormula.ts | 12 ++++++++---- src/Operations.ts | 6 ------ 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index 4200484228..31f08e2c5f 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -295,6 +295,7 @@ export class CrudOperations { public setRowOrder(sheetId: number, rowMapping: [number, number][]): void { this.validateSwapRowIndexes(sheetId, rowMapping) + this.testRowOrderForMatrices(sheetId, rowMapping) this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setRowOrder(sheetId, rowMapping) @@ -308,21 +309,21 @@ export class CrudOperations { this.validateRowOrColumnMapping(sheetId, rowMapping, 'row') } - public testColumnOrderForMatrices(sheetId: number, columnMapping: [number, number][]): boolean { + public testColumnOrderForMatrices(sheetId: number, columnMapping: [number, number][]): void { for(const [source, target] of columnMapping ) { if(source!==target) { const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: source, row: 0}, 1, Infinity) if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { - return false + throw new SourceLocationHasMatrixError() } } } - return true } public setColumnOrder(sheetId: number, columnMapping: [number, number][]): void { this.validateSwapColumnIndexes(sheetId, columnMapping) + this.testColumnOrderForMatrices(sheetId, columnMapping) this.undoRedo.clearRedoStack() this.clipboardOperations.abortCut() this.operations.setColumnOrder(sheetId, columnMapping) @@ -336,16 +337,15 @@ export class CrudOperations { this.validateRowOrColumnMapping(sheetId, columnMapping, 'column') } - public testRowOrderForMatrices(sheetId: number, rowMapping: [number, number][]): boolean { + public testRowOrderForMatrices(sheetId: number, rowMapping: [number, number][]): void { for(const [source, target] of rowMapping ) { if(source!==target) { const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source}, Infinity, 1) if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { - return false + throw new SourceLocationHasMatrixError() } } } - return true } diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 0214d56da5..d1d794199d 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -1201,7 +1201,8 @@ export class HyperFormula implements TypedEmitter { public isItPossibleToSwapRowIndexes(sheetId: number, rowMapping: [number, number][]): boolean { try { this._crudOperations.validateSwapRowIndexes(sheetId, rowMapping) - return this._crudOperations.testRowOrderForMatrices(sheetId, rowMapping) + this._crudOperations.testRowOrderForMatrices(sheetId, rowMapping) + return true } catch (e) { return false } @@ -1283,7 +1284,8 @@ export class HyperFormula implements TypedEmitter { try { const rowMapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'row') this._crudOperations.validateSwapRowIndexes(sheetId, rowMapping) - return this._crudOperations.testRowOrderForMatrices(sheetId, rowMapping) + this._crudOperations.testRowOrderForMatrices(sheetId, rowMapping) + return true } catch (e) { return false } @@ -1361,10 +1363,11 @@ export class HyperFormula implements TypedEmitter { public isItPossibleToSwapColumnIndexes(sheetId: number, columnMapping: [number, number][]): boolean { try { this._crudOperations.validateSwapColumnIndexes(sheetId, columnMapping) + this._crudOperations.testColumnOrderForMatrices(sheetId, columnMapping) + return true } catch (e) { return false } - return this._crudOperations.testColumnOrderForMatrices(sheetId, columnMapping) } /** @@ -1441,7 +1444,8 @@ export class HyperFormula implements TypedEmitter { try { const columnMapping = this._crudOperations.mappingFromOrder(sheetId, newRowOrder, 'column') this._crudOperations.validateSwapColumnIndexes(sheetId, columnMapping) - return this._crudOperations.testColumnOrderForMatrices(sheetId, columnMapping) + this._crudOperations.testColumnOrderForMatrices(sheetId, columnMapping) + return true } catch (e) { return false } diff --git a/src/Operations.ts b/src/Operations.ts index 62b5937994..522e6908a0 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -314,9 +314,6 @@ export class Operations { for(const [source, target] of rowMapping ) { if(source!==target) { const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: 0, row: source}, Infinity, 1) - if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { - throw new SourceLocationHasMatrixError() - } this.dependencyGraph.breakNumericMatricesInRange(rowRange) const row = this.getRangeClipboardCells(rowRange) buffer.push( @@ -336,9 +333,6 @@ export class Operations { for(const [source, target] of columnMapping ) { if(source!==target) { const rowRange = AbsoluteCellRange.spanFrom({sheet: sheetId, col: source, row: 0}, 1, Infinity) - if (this.dependencyGraph.matrixMapping.isFormulaMatrixInRange(rowRange)) { - throw new SourceLocationHasMatrixError() - } this.dependencyGraph.breakNumericMatricesInRange(rowRange) const column = this.getRangeClipboardCells(rowRange) buffer.push( From 034b865f715ffc715c733901e343223eef801d9f Mon Sep 17 00:00:00 2001 From: izulin Date: Wed, 6 Jan 2021 13:14:02 +0100 Subject: [PATCH 36/36] doc --- CHANGELOG.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f29b21ff66..1c3efbb71a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Added support for row and column reordering. (#343) + +### Fixed +- Fixed a bug with serialization of some addresses after CRUDs. (#587) + ## [0.4.0] - 2020-12-17 ### Changed @@ -17,7 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added function aliases mechanism. (PR #569) - Added support for scientific notation. (#579) - Added support for complex numbers. (#281) -- Added support for row and column reordering. (#343) ### Fixed - Fixed a problem with dependencies not collected for specific functions. (#550, #549) @@ -25,7 +30,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed a problem with HLOOKUP/VLOOKUP getting stuck in binary search. (#559, #562) - Fixed a problem with the logic of dependency resolving. (#561, #563) - Fixed a minor bug with ATAN2 function. (#581) -- Fixed a bug with serialization of some addresses after CRUDs. (#587) ## [0.3.0] - 2020-10-22