diff --git a/CHANGELOG.md b/CHANGELOG.md index d178b31bb6..7415c91d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- **Breaking change**: Removed support for matrix formulas (`{=FORMULA}`) notation. Engine now supports formulas returning array of values (instead of only scalars). (#652) + ### Added - Added support for array arithmetic. (#628) - Added ARRAYFORMULA function. (#630) - Added performance improvements for array handling. (#629) - Accepting time in JS Date() objects on the input. (#648) +### Fixed +- Fixed an issue with arrays and cruds. (#651) + ## [0.6.0] - 2021-04-27 ### Added diff --git a/src/AbsoluteCellRange.ts b/src/AbsoluteCellRange.ts index 6ea635c34d..0092d18874 100644 --- a/src/AbsoluteCellRange.ts +++ b/src/AbsoluteCellRange.ts @@ -5,6 +5,7 @@ import {CellRange, simpleCellAddress, SimpleCellAddress, SimpleColumnAddress, SimpleRowAddress} from './Cell' import {DependencyGraph} from './DependencyGraph' +import {Maybe} from './Maybe' import {AstNodeType, CellAddress, CellRangeAst} from './parser' import {ColumnRangeAst, RowRangeAst} from './parser/Ast' import {RowsSpan, Span} from './Span' @@ -37,6 +38,17 @@ export class AbsoluteCellRange { ) } + public static fromCellRangeOrUndef(x: CellRange, baseAddress: SimpleCellAddress): Maybe { + try { + return new AbsoluteCellRange( + new CellAddress(x.start.sheet, x.start.col, x.start.row, x.start.type).toSimpleCellAddress(baseAddress), + new CellAddress(x.end.sheet, x.end.col, x.end.row, x.end.type).toSimpleCellAddress(baseAddress), + ) + } catch (e) { + return undefined + } + } + public static spanFrom(topLeftCorner: SimpleCellAddress, width: number, height: number): AbsoluteCellRange { if (!Number.isFinite(width) && Number.isFinite(height)) { return new AbsoluteRowRange(topLeftCorner.sheet, topLeftCorner.row, topLeftCorner.row + height - 1) diff --git a/src/CellContentParser.ts b/src/CellContentParser.ts index b422e9689c..2c6e1cbe0f 100644 --- a/src/CellContentParser.ts +++ b/src/CellContentParser.ts @@ -58,15 +58,6 @@ export namespace CellContent { } } - export class MatrixFormula { - constructor(public readonly formula: string) { - } - - public formulaWithBraces(): string { - return '{' + this.formula + '}' - } - } - export class Error { public readonly value: CellError @@ -75,7 +66,7 @@ export namespace CellContent { } } - export type Type = Number | String | Boolean | Empty | Formula | MatrixFormula | Error + export type Type = Number | String | Boolean | Empty | Formula | Error } /** @@ -92,13 +83,6 @@ export function isBoolean(text: string): boolean { return tl === 'true' || tl === 'false' } -export function isMatrix(text: RawCellContent): boolean { - if (typeof text !== 'string') { - return false - } - return (text.length > 1) && (text.startsWith('{')) && (text.endsWith('}')) -} - export function isError(text: string, errorMapping: Record): boolean { const upperCased = text.toUpperCase() const errorRegex = /#[A-Za-z0-9\/]+[?!]?/ @@ -157,8 +141,6 @@ export class CellContentParser { } else if (typeof content === 'string') { if (isBoolean(content)) { return new CellContent.Boolean(content.toLowerCase() === 'true') - } else if (isMatrix(content)) { - return new CellContent.MatrixFormula(content.substr(1, content.length - 2)) } else if (isFormula(content)) { return new CellContent.Formula(content) } else if (isError(content, this.config.errorMapping)) { diff --git a/src/CrudOperations.ts b/src/CrudOperations.ts index ba42431c79..84bdbb1507 100644 --- a/src/CrudOperations.ts +++ b/src/CrudOperations.ts @@ -5,7 +5,7 @@ import {AbsoluteCellRange} from './AbsoluteCellRange' import {invalidSimpleCellAddress, simpleCellAddress, SimpleCellAddress} from './Cell' -import {CellContent, CellContentParser, isMatrix, RawCellContent} from './CellContentParser' +import {CellContent, CellContentParser, RawCellContent} from './CellContentParser' import {ClipboardCell, ClipboardOperations} from './ClipboardOperations' import {AddColumnsCommand, AddRowsCommand, Operations, RemoveColumnsCommand, RemoveRowsCommand} from './Operations' import {ColumnSearchStrategy} from './Lookup/SearchStrategy' @@ -21,7 +21,6 @@ import { import { InvalidAddressError, InvalidArgumentsError, - MatrixFormulasNotSupportedError, NamedExpressionDoesNotExistError, NamedExpressionNameIsAlreadyTakenError, NamedExpressionNameIsInvalidError, @@ -252,11 +251,6 @@ export class CrudOperations { if (!(cellContents[i] instanceof Array)) { throw new InvalidArgumentsError('an array of arrays or a raw cell value.') } - for (let j = 0; j < cellContents[i].length; j++) { - if (isMatrix(cellContents[i][j])) { - throw new Error('Cant change matrices in batch operation') - } - } } } @@ -681,9 +675,7 @@ export class CrudOperations { private ensureNamedExpressionIsValid(expression: RawCellContent): void { const parsedExpression = this.cellContentParser.parse(expression) - if (parsedExpression instanceof CellContent.MatrixFormula) { - throw new MatrixFormulasNotSupportedError() - } else if (parsedExpression instanceof CellContent.Formula) { + if (parsedExpression instanceof CellContent.Formula) { const parsingResult = this.parser.parse(parsedExpression.formula, simpleCellAddress(-1, 0, 0)) if (doesContainRelativeReferences(parsingResult.ast)) { throw new NoRelativeAddressesAllowedError() diff --git a/src/DependencyGraph/AddressMapping/AddressMapping.ts b/src/DependencyGraph/AddressMapping/AddressMapping.ts index af79f988cd..d037865c14 100644 --- a/src/DependencyGraph/AddressMapping/AddressMapping.ts +++ b/src/DependencyGraph/AddressMapping/AddressMapping.ts @@ -6,6 +6,7 @@ import {SimpleCellAddress} from '../../Cell' import {RawCellContent} from '../../CellContentParser' import {EmptyValue, InterpreterValue} from '../../interpreter/InterpreterValue' +import {Maybe} from '../../Maybe' import {ColumnsSpan, RowsSpan} from '../../Span' import {EmptyCellVertex, MatrixVertex, ValueCellVertex} from '../index' import {CellVertex} from '../Vertex' @@ -28,7 +29,7 @@ export class AddressMapping { if (!sheetMapping) { throw new NoSheetWithIdError(address.sheet) } - return sheetMapping.getCell(address) + return sheetMapping.getCell(address) ?? null } public fetchCell(address: SimpleCellAddress): CellVertex { diff --git a/src/DependencyGraph/AddressMapping/DenseStrategy.ts b/src/DependencyGraph/AddressMapping/DenseStrategy.ts index f3c0bf3e6f..3bb2dad9d8 100644 --- a/src/DependencyGraph/AddressMapping/DenseStrategy.ts +++ b/src/DependencyGraph/AddressMapping/DenseStrategy.ts @@ -4,6 +4,7 @@ */ import {SheetCellAddress, simpleCellAddress, SimpleCellAddress} from '../../Cell' +import {Maybe} from '../../Maybe' import {ColumnsSpan, RowsSpan} from '../../Span' import {CellVertex} from '../Vertex' import {IAddressMappingStrategy} from './IAddressMappingStrategy' @@ -33,7 +34,7 @@ export class DenseStrategy implements IAddressMappingStrategy { } /** @inheritDoc */ - public getCell(address: SheetCellAddress): CellVertex | null { + public getCell(address: SheetCellAddress): Maybe { return this.getCellVertex(address.col, address.row) } @@ -190,11 +191,7 @@ export class DenseStrategy implements IAddressMappingStrategy { } } - private getCellVertex(x: number, y: number): CellVertex | null { - const row = this.mapping[y] - if (row) { - return this.mapping[y][x] || null - } - return null + private getCellVertex(x: number, y: number): Maybe { + return this.mapping[y]?.[x] } } diff --git a/src/DependencyGraph/AddressMapping/IAddressMappingStrategy.ts b/src/DependencyGraph/AddressMapping/IAddressMappingStrategy.ts index 9662b3a942..9c4d1fe3d7 100644 --- a/src/DependencyGraph/AddressMapping/IAddressMappingStrategy.ts +++ b/src/DependencyGraph/AddressMapping/IAddressMappingStrategy.ts @@ -4,6 +4,7 @@ */ import {SheetCellAddress, SimpleCellAddress} from '../../Cell' +import {Maybe} from '../../Maybe' import {ColumnsSpan, RowsSpan} from '../../Span' import {CellVertex} from '../Vertex' @@ -17,7 +18,7 @@ export interface IAddressMappingStrategy { * * @param address - cell address */ - getCell(address: SheetCellAddress): CellVertex | null, + getCell(address: SheetCellAddress): Maybe, /** * Set vertex for given address diff --git a/src/DependencyGraph/AddressMapping/SparseStrategy.ts b/src/DependencyGraph/AddressMapping/SparseStrategy.ts index 431ac2f805..19fec4f273 100644 --- a/src/DependencyGraph/AddressMapping/SparseStrategy.ts +++ b/src/DependencyGraph/AddressMapping/SparseStrategy.ts @@ -4,6 +4,7 @@ */ import {SheetCellAddress, simpleCellAddress, SimpleCellAddress} from '../../Cell' +import {Maybe} from '../../Maybe' import {ColumnsSpan, RowsSpan} from '../../Span' import {CellVertex} from '../Vertex' import {IAddressMappingStrategy} from './IAddressMappingStrategy' @@ -26,12 +27,8 @@ export class SparseStrategy implements IAddressMappingStrategy { } /** @inheritDoc */ - public getCell(address: SheetCellAddress): CellVertex | null { - const colMapping = this.mapping.get(address.col) - if (!colMapping) { - return null - } - return colMapping.get(address.row) || null + public getCell(address: SheetCellAddress): Maybe { + return this.mapping.get(address.col)?.get(address.row) } /** @inheritDoc */ diff --git a/src/DependencyGraph/DependencyGraph.ts b/src/DependencyGraph/DependencyGraph.ts index 5d8d5b780a..ed3c6fe4ec 100644 --- a/src/DependencyGraph/DependencyGraph.ts +++ b/src/DependencyGraph/DependencyGraph.ts @@ -6,12 +6,7 @@ import assert from 'assert' import {AbsoluteCellRange} from '../AbsoluteCellRange' import {absolutizeDependencies} from '../absolutizeDependencies' -import { - CellError, - ErrorType, - simpleCellAddress, - SimpleCellAddress -} from '../Cell' +import {CellError, ErrorType, simpleCellAddress, SimpleCellAddress} from '../Cell' import {RawCellContent} from '../CellContentParser' import {CellDependency} from '../CellDependency' import {Config} from '../Config' @@ -39,15 +34,17 @@ import {Graph, TopSortResult} from './Graph' import {MatrixMapping} from './MatrixMapping' import {RangeMapping} from './RangeMapping' import {SheetMapping} from './SheetMapping' -import {RawAndParsedValue, ValueCellVertexValue} from './ValueCellVertex' +import {RawAndParsedValue} from './ValueCellVertex' import {FunctionRegistry} from '../interpreter/FunctionRegistry' import { EmptyValue, + ExtendedNumber, getRawValue, InternalScalarValue, InterpreterValue, - RawScalarValue, ExtendedNumber + RawScalarValue } from '../interpreter/InterpreterValue' +import {MatrixSize} from '../Matrix' export class DependencyGraph { /* @@ -156,6 +153,16 @@ export class DependencyGraph { } } + public setMatrixToCell(address: SimpleCellAddress, ast: Ast, dependencies: CellDependency[], size: MatrixSize) { + const vertex = new MatrixVertex(address, size.width, size.height, ast) + this.exchangeOrAddMatrixVertex(vertex) + + this.processCellDependencies(dependencies, vertex) + + this.graph.markNodeAsSpecialRecentlyChanged(vertex) + this.correctInfiniteRangesDependency(address) + } + public ensureThatVertexIsNonMatrixCellVertex(vertex: CellVertex | null) { assert.ok(!(vertex instanceof MatrixVertex), 'Illegal operation') } @@ -548,7 +555,7 @@ export class DependencyGraph { public addVertex(address: SimpleCellAddress, vertex: CellVertex): void { this.graph.addNode(vertex) - this.setVertexAddress(address, vertex) + this.addressMapping.setCell(address, vertex) } public addMatrixVertex(address: SimpleCellAddress, vertex: CellVertex): void { @@ -556,24 +563,6 @@ export class DependencyGraph { this.setAddressMappingForMatrixVertex(vertex, address) } - public addNewMatrixVertex(matrixVertex: MatrixVertex): void { - const range = AbsoluteCellRange.spanFrom(matrixVertex.getAddress(), matrixVertex.width, matrixVertex.height) - for (const vertex of this.verticesFromRange(range)) { - if (vertex instanceof MatrixVertex) { - throw Error('You cannot modify only part of an array') - } - } - - this.setMatrix(range, matrixVertex) - - for (const [address, vertex] of this.entriesFromRange(range)) { - if (vertex) { - this.exchangeGraphNode(vertex, matrixVertex) - } - this.setVertexAddress(address, matrixVertex) - } - } - public* matrixFormulaNodes(): IterableIterator { for (const vertex of this.graph.nodes) { if (vertex instanceof MatrixVertex && vertex.isFormula()) { @@ -618,10 +607,6 @@ export class DependencyGraph { return value } - public setVertexAddress(address: SimpleCellAddress, vertex: CellVertex) { - this.addressMapping.setCell(address, vertex) - } - public existsEdge(fromNode: Vertex, toNode: Vertex): boolean { return this.graph.existsEdge(fromNode, toNode) } @@ -881,8 +866,22 @@ export class DependencyGraph { } } + private exchangeOrAddMatrixVertex(matrixVertex: MatrixVertex): void { + const range = AbsoluteCellRange.spanFrom(matrixVertex.getAddress(), matrixVertex.width, matrixVertex.height) + for (const vertex of this.verticesFromRange(range)) { + this.ensureThatVertexIsNonMatrixCellVertex(vertex) + } + + this.setMatrix(range, matrixVertex) + + for (const [address, vertex] of this.entriesFromRange(range)) { + this.exchangeOrAddGraphNode(vertex, matrixVertex) + this.addressMapping.setCell(address, matrixVertex) + } + } + private setAddressMappingForMatrixVertex(vertex: CellVertex, formulaAddress: SimpleCellAddress): void { - this.setVertexAddress(formulaAddress, vertex) + this.addressMapping.setCell(formulaAddress, vertex) if (!(vertex instanceof MatrixVertex)) { return @@ -892,7 +891,7 @@ export class DependencyGraph { this.setMatrix(range, vertex) for (const address of range.addresses(this)) { - this.setVertexAddress(address, vertex) + this.addressMapping.setCell(address, vertex) } } diff --git a/src/Evaluator.ts b/src/Evaluator.ts index bf6e4e2d7c..1630379547 100644 --- a/src/Evaluator.ts +++ b/src/Evaluator.ts @@ -7,8 +7,6 @@ import {AbsoluteCellRange} from './AbsoluteCellRange' import {absolutizeDependencies} from './absolutizeDependencies' import {CellError, ErrorType, SimpleCellAddress} from './Cell' import {InterpreterState} from './interpreter/InterpreterState' -import {SimpleRangeValue} from './interpreter/SimpleRangeValue' -import {ColumnSearchStrategy} from './Lookup/SearchStrategy' import {Config} from './Config' import {ContentChanges} from './ContentChanges' import {DateTimeHelper} from './DateTimeHelper' @@ -17,6 +15,8 @@ import {ErrorMessage} from './error-message' import {FunctionRegistry} from './interpreter/FunctionRegistry' import {Interpreter} from './interpreter/Interpreter' import {EmptyValue, getRawValue, InterpreterValue} from './interpreter/InterpreterValue' +import {OnlyRangeData, SimpleRangeValue} from './interpreter/SimpleRangeValue' +import {ColumnSearchStrategy} from './Lookup/SearchStrategy' import {Matrix} from './Matrix' import {NamedExpressions} from './NamedExpressions' import {NumberLiteralHelper} from './NumberLiteralHelper' @@ -74,15 +74,19 @@ export class Evaluator { const formula = vertex.getFormula() as Ast const currentValue = vertex.isComputed() ? vertex.getCellValue() : null const newCellValue = this.evaluateAstToRangeValue(formula, new InterpreterState(address, this.config.arrays)) - if (newCellValue instanceof SimpleRangeValue) { + if(newCellValue instanceof SimpleRangeValue && newCellValue.hasOnlyNumbers() && !(newCellValue.onlyRangeData())) { const newCellMatrix = new Matrix(newCellValue.rawNumbers()) vertex.setCellValue(newCellMatrix) changes.addMatrixChange(newCellMatrix, address) this.columnSearch.change(currentValue, newCellMatrix, address) } else { - vertex.setErrorValue(newCellValue) - changes.addChange(newCellValue, address) - this.columnSearch.change(currentValue, newCellValue, address) + const errorVal = newCellValue instanceof CellError ? newCellValue + : newCellValue.onlyRangeData() ? + new CellError(ErrorType.VALUE, ErrorMessage.ScalarExpected) + : new CellError(ErrorType.VALUE, ErrorMessage.CellRangeExpected) + vertex.setErrorValue(errorVal) + changes.addChange(errorVal, address) + this.columnSearch.change(currentValue, errorVal, address) } return true } else if (vertex instanceof RangeVertex) { @@ -153,13 +157,17 @@ export class Evaluator { const address = vertex.getAddress() const formula = vertex.getFormula() as Ast const newCellValue = this.evaluateAstToRangeValue(formula, new InterpreterState(address, this.config.arrays)) - if (newCellValue instanceof SimpleRangeValue) { + if(newCellValue instanceof SimpleRangeValue && newCellValue.hasOnlyNumbers() && !newCellValue.onlyRangeData()) { const newCellMatrix = new Matrix(newCellValue.rawNumbers()) vertex.setCellValue(newCellMatrix) this.columnSearch.add(newCellMatrix, address) } else { - vertex.setErrorValue(newCellValue) - this.columnSearch.add(newCellValue, address) + const errorVal = newCellValue instanceof CellError ? newCellValue + : newCellValue.onlyRangeData() ? + new CellError(ErrorType.VALUE, ErrorMessage.ScalarExpected) + : new CellError(ErrorType.VALUE, ErrorMessage.CellRangeExpected) + vertex.setErrorValue(errorVal) + this.columnSearch.add(errorVal, address) } } else if (vertex instanceof RangeVertex) { vertex.clearCache() @@ -182,10 +190,10 @@ export class Evaluator { const interpreterValue = this.interpreter.evaluateAst(ast, state) if (interpreterValue instanceof CellError) { return interpreterValue - } else if (interpreterValue instanceof SimpleRangeValue && interpreterValue.hasOnlyNumbers()) { + } else if (interpreterValue instanceof SimpleRangeValue) { return interpreterValue } else { - return new CellError(ErrorType.VALUE, ErrorMessage.CellRangeExpected) + return SimpleRangeValue.fromScalar(interpreterValue) } } } diff --git a/src/GraphBuilder.ts b/src/GraphBuilder.ts index d12f9583aa..b96587cd44 100644 --- a/src/GraphBuilder.ts +++ b/src/GraphBuilder.ts @@ -101,33 +101,27 @@ export class SimpleStrategy implements GraphBuilderStrategy { const address = simpleCellAddress(sheetId, j, i) const parsedCellContent = this.cellContentParser.parse(cellContent) - if (parsedCellContent instanceof CellContent.MatrixFormula) { - if (this.dependencyGraph.existsVertex(address)) { - continue - } - const parseResult = this.stats.measure(StatType.PARSER, () => this.parser.parse(parsedCellContent.formula, address)) - if (parseResult.errors.length > 0) { - const vertex = new ParsingErrorVertex(parseResult.errors, parsedCellContent.formulaWithBraces()) - this.dependencyGraph.addVertex(address, vertex) - } else { - const vertex = buildMatrixVertex(parseResult.ast as ProcedureAst, address) - dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) - this.dependencyGraph.addMatrixVertex(address, vertex) - } - } else if (parsedCellContent instanceof CellContent.Formula) { + if (parsedCellContent instanceof CellContent.Formula) { const parseResult = this.stats.measure(StatType.PARSER, () => this.parser.parse(parsedCellContent.formula, address)) if (parseResult.errors.length > 0) { const vertex = new ParsingErrorVertex(parseResult.errors, parsedCellContent.formula) this.dependencyGraph.addVertex(address, vertex) } else { - const vertex = new FormulaCellVertex(parseResult.ast, address, 0) - dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) - this.dependencyGraph.addVertex(address, vertex) - if (parseResult.hasVolatileFunction) { - this.dependencyGraph.markAsVolatile(vertex) - } - if (parseResult.hasStructuralChangeFunction) { - this.dependencyGraph.markAsDependentOnStructureChange(vertex) + const size = checkMatrixSize(parseResult.ast, address) + if(size instanceof CellError || (size.width<=1 && size.height<=1) || size.isRef) { + const vertex = new FormulaCellVertex(parseResult.ast, address, 0) + dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) + this.dependencyGraph.addVertex(address, vertex) + if (parseResult.hasVolatileFunction) { + this.dependencyGraph.markAsVolatile(vertex) + } + if (parseResult.hasStructuralChangeFunction) { + this.dependencyGraph.markAsDependentOnStructureChange(vertex) + } + } else { + const vertex = new MatrixVertex(address, size.width, size.height, parseResult.ast) + dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) + this.dependencyGraph.addMatrixVertex(address, vertex) } } } else if (parsedCellContent instanceof CellContent.Empty) { @@ -176,28 +170,28 @@ export class MatrixDetectionStrategy implements GraphBuilderStrategy { const address = simpleCellAddress(sheetId, j, i) const parsedCellContent = this.cellContentParser.parse(cellContent) - if (parsedCellContent instanceof CellContent.MatrixFormula) { - if (this.dependencyGraph.existsVertex(address)) { - continue - } - const parseResult = this.stats.measure(StatType.PARSER, () => this.parser.parse(parsedCellContent.formula, address)) - if (parseResult.errors.length > 0) { - const vertex = new ParsingErrorVertex(parseResult.errors, parsedCellContent.formulaWithBraces()) - this.dependencyGraph.addVertex(address, vertex) - } else { - const vertex = buildMatrixVertex(parseResult.ast as ProcedureAst, address) - dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) - this.dependencyGraph.addMatrixVertex(address, vertex) - } - } else if (parsedCellContent instanceof CellContent.Formula) { + if (parsedCellContent instanceof CellContent.Formula) { const parseResult = this.stats.measure(StatType.PARSER, () => this.parser.parse(parsedCellContent.formula, address)) if (parseResult.errors.length > 0) { const vertex = new ParsingErrorVertex(parseResult.errors, parsedCellContent.formula) this.dependencyGraph.addVertex(address, vertex) } else { - const vertex = new FormulaCellVertex(parseResult.ast, address, 0) - dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) - this.dependencyGraph.addVertex(address, vertex) + const size = checkMatrixSize(parseResult.ast, address) + if(size instanceof CellError || (size.width<=1 && size.height<=1) || size.isRef) { + const vertex = new FormulaCellVertex(parseResult.ast, address, 0) + dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) + this.dependencyGraph.addVertex(address, vertex) + if (parseResult.hasVolatileFunction) { + this.dependencyGraph.markAsVolatile(vertex) + } + if (parseResult.hasStructuralChangeFunction) { + this.dependencyGraph.markAsDependentOnStructureChange(vertex) + } + } else { + const vertex = new MatrixVertex(address, size.width, size.height, parseResult.ast) + dependencies.set(vertex, absolutizeDependencies(parseResult.dependencies, address)) + this.dependencyGraph.addMatrixVertex(address, vertex) + } } } else if (parsedCellContent instanceof CellContent.Empty) { /* we don't care about empty cells here */ diff --git a/src/HyperFormula.ts b/src/HyperFormula.ts index 347ce5b417..716e820c18 100644 --- a/src/HyperFormula.ts +++ b/src/HyperFormula.ts @@ -3293,7 +3293,6 @@ export class HyperFormula implements TypedEmitter { * @throws [[ExpectedValueOfTypeError]] if any of its basic type argument is of wrong type * @throws [[NamedExpressionNameIsAlreadyTakenError]] when the named expression name is not available. * @throws [[NamedExpressionNameIsInvalidError]] when the named expression name is not valid - * @throws [[MatrixFormulasNotSupportedError]] when the named expression formula is a Matrix formula * @throws [[NoRelativeAddressesAllowedError]] when the named expression formula contains relative references * @throws [[NoSheetWithNameError]] when the given sheet name does not exists * @@ -3507,7 +3506,6 @@ export class HyperFormula implements TypedEmitter { * @throws [[ExpectedValueOfTypeError]] if any of its basic type argument is of wrong type * @throws [[NamedExpressionDoesNotExistError]] when the given expression does not exist. * @throws [[NoSheetWithNameError]] when the given sheet name does not exists - * @throws [[MatrixFormulasNotSupportedError]] when the named expression formula is a Matrix formula * @throws [[NoRelativeAddressesAllowedError]] when the named expression formula contains relative references * * @example diff --git a/src/Matrix.ts b/src/Matrix.ts index 5b12bcbfde..cdb9e8e029 100644 --- a/src/Matrix.ts +++ b/src/Matrix.ts @@ -15,6 +15,7 @@ export class MatrixSize { constructor( public width: number, public height: number, + public isRef?: boolean, ) { if (width <= 0 || height <= 0) { throw Error('Incorrect matrix size') @@ -118,10 +119,16 @@ export function checkMatrixSize(ast: Ast, formulaAddress: SimpleCellAddress): Ma } } } else if (ast.type === AstNodeType.CELL_RANGE) { - const range = AbsoluteCellRange.fromCellRange(ast, formulaAddress) - return {width: range.width(), height: range.height()} - } else if (ast.type === AstNodeType.NUMBER || ast.type === AstNodeType.CELL_REFERENCE) { - return {width: 1, height: 1} + const range = AbsoluteCellRange.fromCellRangeOrUndef(ast, formulaAddress) + if(range === undefined) { + return new CellError(ErrorType.VALUE) + } else { + return new MatrixSize(range.width(), range.height(), true) + } + } else if (ast.type === AstNodeType.NUMBER) { + return new MatrixSize(1, 1) + } else if (ast.type === AstNodeType.CELL_REFERENCE) { + return new MatrixSize(1, 1, true) } else { return new CellError(ErrorType.VALUE) //TODO } diff --git a/src/Operations.ts b/src/Operations.ts index 89adc4f6b1..2e145f020b 100644 --- a/src/Operations.ts +++ b/src/Operations.ts @@ -42,6 +42,7 @@ import { import {buildMatrixVertex} from './GraphBuilder' import {LazilyTransformingAstService} from './LazilyTransformingAstService' import {ColumnSearchStrategy} from './Lookup/SearchStrategy' +import {checkMatrixSize} from './Matrix' import { doesContainRelativeReferences, InternalNamedExpression, @@ -635,31 +636,21 @@ export class Operations { vertex.setMatrixCellValue(address, getRawValue(newValue)) this.columnSearch.change(getRawValue(oldValue), getRawValue(newValue), address) this.changes.addChange(newValue, address) - } else if (!(vertex instanceof MatrixVertex) && parsedCellContent instanceof CellContent.MatrixFormula) { - const {ast, errors, dependencies} = this.parser.parse(parsedCellContent.formula, address) - if (errors.length > 0) { - this.dependencyGraph.setParsingErrorToCell(address, new ParsingErrorVertex(errors, parsedCellContent.formulaWithBraces())) - } else { - const newVertex = buildMatrixVertex(ast as ProcedureAst, address) - if (newVertex instanceof ValueCellVertex) { - throw Error('What if new matrix vertex is not properly constructed?') - } - this.dependencyGraph.addNewMatrixVertex(newVertex) - this.dependencyGraph.processCellDependencies(absolutizeDependencies(dependencies, address), newVertex) - this.dependencyGraph.graph.markNodeAsSpecialRecentlyChanged(newVertex) - } } else if (!(vertex instanceof MatrixVertex)) { if (parsedCellContent instanceof CellContent.Formula) { const {ast, errors, hasVolatileFunction, hasStructuralChangeFunction, dependencies} = this.parser.parse(parsedCellContent.formula, address) if (errors.length > 0) { this.dependencyGraph.setParsingErrorToCell(address, new ParsingErrorVertex(errors, parsedCellContent.formula)) } else { - this.dependencyGraph.setFormulaToCell(address, ast, absolutizeDependencies(dependencies, address), hasVolatileFunction, hasStructuralChangeFunction) + const size = checkMatrixSize(ast, address) + if(size instanceof CellError || (size.width<=1 && size.height<=1) || size.isRef) { + this.dependencyGraph.setFormulaToCell(address, ast, absolutizeDependencies(dependencies, address), hasVolatileFunction, hasStructuralChangeFunction) + } else { + this.dependencyGraph.setMatrixToCell(address, ast, absolutizeDependencies(dependencies, address), size) + } } } else if (parsedCellContent instanceof CellContent.Empty) { this.setCellEmpty(address) - } else if (parsedCellContent instanceof CellContent.MatrixFormula) { - throw new Error('Cant happen') } else { this.setValueToCell({parsedValue: parsedCellContent.value, rawValue: newCellContent}, address) } @@ -765,9 +756,7 @@ export class Operations { private storeNamedExpressionInCell(address: SimpleCellAddress, expression: RawCellContent) { const parsedCellContent = this.cellContentParser.parse(expression) - if (parsedCellContent instanceof CellContent.MatrixFormula) { - throw new Error('Matrix formulas are not supported') - } else if (parsedCellContent instanceof CellContent.Formula) { + if (parsedCellContent instanceof CellContent.Formula) { const parsingResult = this.parser.parse(parsedCellContent.formula, simpleCellAddress(-1, 0, 0)) if (doesContainRelativeReferences(parsingResult.ast)) { throw new NoRelativeAddressesAllowedError() diff --git a/src/Serialization.ts b/src/Serialization.ts index 199c0d4b62..a0d5267c32 100644 --- a/src/Serialization.ts +++ b/src/Serialization.ts @@ -36,9 +36,12 @@ export class Serialization { const formula = formulaVertex.getFormula(this.dependencyGraph.lazilyTransformingAstService) return this.unparser.unparse(formula, address) } else if (formulaVertex instanceof MatrixVertex) { + if(formulaVertex.getAddress().row !== address.row || formulaVertex.getAddress().col !== address.col || formulaVertex.getAddress().sheet !== address.sheet) { + return undefined + } const formula = formulaVertex.getFormula() if (formula) { - return '{' + this.unparser.unparse(formula, formulaVertex.getAddress()) + '}' + return this.unparser.unparse(formula, formulaVertex.getAddress()) } } else if (formulaVertex instanceof ParsingErrorVertex) { return formulaVertex.getFormula() diff --git a/src/errors.ts b/src/errors.ts index 20deb63edb..316888df6d 100644 --- a/src/errors.ts +++ b/src/errors.ts @@ -363,18 +363,6 @@ export class TargetLocationHasMatrixError extends Error { } } -/** - * Error thrown when trying to use matrix expression as named expression. - * - * @see [[addNamedExpression]] - * @see [[changeNamedExpression]] - */ -export class MatrixFormulasNotSupportedError extends Error { - constructor() { - super('Matrix formulas are not supported in named expressions.') - } -} - /** * Error thrown when named expression contains relative addresses. * diff --git a/src/index.ts b/src/index.ts index 69d9c3e19a..268d4f2f1e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,7 +19,6 @@ import { InvalidArgumentsError, LanguageAlreadyRegisteredError, LanguageNotRegisteredError, - MatrixFormulasNotSupportedError, MissingTranslationError, NamedExpressionDoesNotExistError, NamedExpressionNameIsAlreadyTakenError, @@ -69,7 +68,6 @@ class HyperFormulaNS extends HyperFormula { public static InvalidArgumentsError = InvalidArgumentsError public static LanguageNotRegisteredError = LanguageNotRegisteredError public static LanguageAlreadyRegisteredError = LanguageAlreadyRegisteredError - public static MatrixFormulasNotSupportedError = MatrixFormulasNotSupportedError public static MissingTranslationError = MissingTranslationError public static NamedExpressionDoesNotExistError = NamedExpressionDoesNotExistError public static NamedExpressionNameIsAlreadyTakenError = NamedExpressionNameIsAlreadyTakenError @@ -140,7 +138,6 @@ export { InvalidArgumentsError, LanguageAlreadyRegisteredError, LanguageNotRegisteredError, - MatrixFormulasNotSupportedError, MissingTranslationError, NamedExpressionDoesNotExistError, NamedExpressionNameIsAlreadyTakenError, diff --git a/src/interpreter/Interpreter.ts b/src/interpreter/Interpreter.ts index 00e5a39942..fa99a33bfd 100644 --- a/src/interpreter/Interpreter.ts +++ b/src/interpreter/Interpreter.ts @@ -64,6 +64,9 @@ export class Interpreter { val = cloneNumber(val, fixNegativeZero(getRawValue(val))) } } + if(val instanceof SimpleRangeValue && val.height() === 1 && val.width() === 1) { + [[val]] = val.raw() + } return wrapperForAddress(val, state.formulaAddress) } diff --git a/src/interpreter/SimpleRangeValue.ts b/src/interpreter/SimpleRangeValue.ts index f53e88a7ae..8112c80e78 100644 --- a/src/interpreter/SimpleRangeValue.ts +++ b/src/interpreter/SimpleRangeValue.ts @@ -263,4 +263,8 @@ export class SimpleRangeValue { public sameDimensionsAs(other: SimpleRangeValue): boolean { return this.width() === other.width() && this.height() === other.height() } + + public onlyRangeData(): boolean { + return this.data instanceof OnlyRangeData + } } diff --git a/src/interpreter/plugin/MatrixPlugin.ts b/src/interpreter/plugin/MatrixPlugin.ts index 069057aaff..a024de50db 100644 --- a/src/interpreter/plugin/MatrixPlugin.ts +++ b/src/interpreter/plugin/MatrixPlugin.ts @@ -8,9 +8,9 @@ import {ErrorMessage} from '../../error-message' import {MatrixSize, matrixSizeForMultiplication, matrixSizeForPoolFunction, matrixSizeForTranspose} from '../../Matrix' import {ProcedureAst} from '../../parser' import {Interpreter} from '../Interpreter' -import {InterpreterState} from '../InterpreterState' import {InterpreterValue} from '../InterpreterValue' import {SimpleRangeValue} from '../SimpleRangeValue' +import {InterpreterState} from '../InterpreterState' import {ArgumentTypes, FunctionPlugin, FunctionPluginTypecheck} from './FunctionPlugin' export type KernelRunShortcut = (...args: any[]) => number[][] @@ -77,6 +77,9 @@ export class MatrixPlugin extends FunctionPlugin implements FunctionPluginTypech if (!leftMatrix.hasOnlyNumbers() || !rightMatrix.hasOnlyNumbers()) { return new CellError(ErrorType.VALUE, ErrorMessage.NumberRange) } + if( rightMatrix.height() !== leftMatrix.width()) { + return new CellError(ErrorType.VALUE, ErrorMessage.MatrixDimensions) + } const outputSize = matrixSizeForMultiplication(leftMatrix.size, rightMatrix.size) const result = this.createKernel(function(a: number[][], b: number[][], width: number): number { diff --git a/test/CellContentParser.spec.ts b/test/CellContentParser.spec.ts index b5ee71825c..e214cc2018 100644 --- a/test/CellContentParser.spec.ts +++ b/test/CellContentParser.spec.ts @@ -16,15 +16,6 @@ describe('CellContentParser', () => { const config = new Config() const cellContentParser = new CellContentParser(config, new DateTimeHelper(config), new NumberLiteralHelper(config)) - it('a matrix', () => { - expect(cellContentParser.parse('{=FOO()}')).toEqual(new CellContent.MatrixFormula('=FOO()')) - }) - - it('not a matrix', () => { - expect(cellContentParser.parse('{=FOO()')).not.toBeInstanceOf(CellContent.MatrixFormula) - expect(cellContentParser.parse('=FOO()}')).not.toEqual(CellContent.MatrixFormula) - }) - it('a formula', () => { expect(cellContentParser.parse('=FOO()')).toEqual(new CellContent.Formula('=FOO()')) }) diff --git a/test/cruds/adding-columns.spec.ts b/test/cruds/adding-columns.spec.ts index 55b63e16d1..91cb3cc9f4 100644 --- a/test/cruds/adding-columns.spec.ts +++ b/test/cruds/adding-columns.spec.ts @@ -61,8 +61,8 @@ describe('Adding column - checking if its possible', () => { it('no if theres a formula matrix in place where we add', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}', '13'], - ['3', '4', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['1', '2', '=TRANSPOSE(A1:B2)', undefined, '13'], + ['3', '4'], ]) expect(engine.isItPossibleToAddColumns(0, [1, 1])).toEqual(true) @@ -103,8 +103,8 @@ describe('Adding column - matrix check', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], + [], ['13'], ]) @@ -115,8 +115,8 @@ describe('Adding column - matrix check', () => { it('should be possible to add row right before matrix', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(C1:D2)}', '{=TRANSPOSE(C1:D2)}', '1', '2'], - ['{=TRANSPOSE(C1:D2)}', '{=TRANSPOSE(C1:D2)}', '3', '4'], + ['=TRANSPOSE(C1:D2)', undefined, '1', '2'], + [undefined, undefined, '3', '4'], ]) engine.addColumns(0, [0, 1]) @@ -129,8 +129,8 @@ describe('Adding column - matrix check', () => { it('should be possible to add row right after matrix', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(C1:D2)}', '{=TRANSPOSE(C1:D2)}', '1', '2'], - ['{=TRANSPOSE(C1:D2)}', '{=TRANSPOSE(C1:D2)}', '3', '4'], + ['=TRANSPOSE(C1:D2)', undefined, '1', '2'], + [undefined, undefined, '3', '4'], ]) engine.addColumns(0, [2, 1]) @@ -217,8 +217,8 @@ describe('Adding column - FormulaCellVertex#address update', () => { describe('Adding column - MatrixVertex', () => { it('MatrixVertex#formula should be updated', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['3', '4', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['1', '2', '=TRANSPOSE(A1:B2)'], + ['3', '4'], ]) engine.addColumns(0, [1, 1]) @@ -228,8 +228,8 @@ describe('Adding column - MatrixVertex', () => { it('MatrixVertex#address should be updated', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['3', '4', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['1', '2', '=TRANSPOSE(A1:B2)'], + ['3', '4'], ]) engine.addColumns(0, [1, 1]) @@ -245,8 +245,7 @@ describe('Adding column - MatrixVertex', () => { ['3', '4'], ], Sheet2: [ - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], + ['=TRANSPOSE(Sheet1!A1:B2)'], ], }) diff --git a/test/cruds/adding-row.spec.ts b/test/cruds/adding-row.spec.ts index ba3c88dd87..f89109c2b8 100644 --- a/test/cruds/adding-row.spec.ts +++ b/test/cruds/adding-row.spec.ts @@ -62,8 +62,8 @@ describe('Adding row - checking if its possible', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], + [], ['13'], ]) @@ -105,8 +105,8 @@ describe('Adding row - matrix check', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], + [], ['13'], ]) @@ -117,8 +117,8 @@ describe('Adding row - matrix check', () => { it('should be possible to add row right above matrix', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(A3:B4)}', '{=TRANSPOSE(A3:B4)}'], - ['{=TRANSPOSE(A3:B4)}', '{=TRANSPOSE(A3:B4)}'], + ['=TRANSPOSE(A3:B4)'], + [], ['1', '2'], ['3', '4'], ]) @@ -133,8 +133,8 @@ describe('Adding row - matrix check', () => { it('should be possible to add row right after matrix', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(A3:B4)}', '{=TRANSPOSE(A3:B4)}'], - ['{=TRANSPOSE(A3:B4)}', '{=TRANSPOSE(A3:B4)}'], + ['=TRANSPOSE(A3:B4)'], + [], ['1', '2'], ['3', '4'], ]) @@ -213,8 +213,7 @@ describe('Adding row - MatrixVertex', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], ]) engine.addRows(0, [1, 1]) @@ -229,8 +228,7 @@ describe('Adding row - MatrixVertex', () => { ['3', '4'], ], Sheet2: [ - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], + ['=TRANSPOSE(Sheet1!A1:B2)'], ], }) @@ -243,8 +241,7 @@ describe('Adding row - MatrixVertex', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], ]) engine.addRows(0, [1, 1]) diff --git a/test/cruds/change-cell-content.spec.ts b/test/cruds/change-cell-content.spec.ts index b4b9b7b565..ef05e44ea0 100644 --- a/test/cruds/change-cell-content.spec.ts +++ b/test/cruds/change-cell-content.spec.ts @@ -24,8 +24,8 @@ describe('Changing cell content - checking if its possible', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], + [], ['13'], ]) @@ -240,7 +240,7 @@ describe('changing cell content', () => { ] const engine = HyperFormula.buildFromArray(sheet) - engine.setCellContents(adr('A3'), '{=MMULT(A1:B2,A1:B2)}') + engine.setCellContents(adr('A3'), '=MMULT(A1:B2,A1:B2)') expect(engine.addressMapping.fetchCell(adr('A3'))).toBeInstanceOf(MatrixVertex) expect(engine.addressMapping.fetchCell(adr('B4'))).toBeInstanceOf(MatrixVertex) expect(engine.getCellValue(adr('A3'))).toBe(7) @@ -421,12 +421,12 @@ describe('changing cell content', () => { it('should not be possible to edit part of a Matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - [null, '{=TRANSPOSE(A1:B1)}'], + [null, '=TRANSPOSE(A1:B1)'], ]) expect(() => { - engine.setCellContents(adr('A2'), '{=TRANSPOSE(C1:C2)}') - }).toThrowError('You cannot modify only part of an array') + engine.setCellContents(adr('A2'), '=TRANSPOSE(C1:C2)') + }).toThrowError('Illegal operation') }) it('is not possible to set cell content in sheet which does not exist', () => { @@ -498,7 +498,7 @@ describe('changing cell content', () => { const sheet = [ ['1', '2'], ['3', '4'], - ['{=MMULT(A1:B2,A1:B2)}'], + ['=MMULT(A1:B2,A1:B2)'], ] const engine = HyperFormula.buildFromArray(sheet) @@ -574,10 +574,10 @@ describe('changing cell content', () => { it('update empty cell to unparsable matrix formula', () => { const engine = HyperFormula.buildFromArray([]) - engine.setCellContents(adr('A1'), '{=TRANSPOSE(}') + engine.setCellContents(adr('A1'), '=TRANSPOSE(') expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.ERROR, ErrorMessage.ParseError)) - expect(engine.getCellFormula(adr('A1'))).toEqual('{=TRANSPOSE(}') + expect(engine.getCellFormula(adr('A1'))).toEqual('=TRANSPOSE(') }) it('should throw when trying to set cell content outside sheet limits', () => { @@ -597,6 +597,14 @@ describe('changing cell content', () => { expect(() => engine.setCellContents(cellInLastColumn, null)).not.toThrow() expect(() => engine.setCellContents(cellInLastRow, null)).not.toThrow() }) + + it('should set matrix with range out of current sheet scope', () => { + const sheet = [ + ['1', '2'], + ] + const engine = HyperFormula.buildFromArray(sheet) + engine.setCellContents(adr('C1'), '=MMULT(A1:B2,A1:B2)') + }) }) describe('change multiple cells contents', () => { @@ -648,16 +656,16 @@ describe('change multiple cells contents', () => { expect(evaluatorCallSpy).toHaveBeenCalledTimes(1) }) - it('it not possible to change matrices', () => { + it('possible to change matrices', () => { const sheet = [ ['1', '2'], ] const engine = HyperFormula.buildFromArray(sheet) - expect(() => { - engine.setCellContents(adr('A1'), [['42', '{=MMULT(A1:B2,A1:B2)}']]) - }).toThrowError('Cant change matrices in batch operation') - expect(engine.getCellValue(adr('A1'))).toBe(1) + engine.setCellContents(adr('A1'), [['42', '18', '=MMULT(A1:B1,TRANSPOSE(A1:B1))']]) + expect(engine.getCellValue(adr('A1'))).toBe(42) + expect(engine.getCellValue(adr('B1'))).toBe(18) + expect(engine.getCellValue(adr('C1'))).toBe(2088) }) it('returns changes of multiple values', () => { @@ -800,7 +808,7 @@ describe('column ranges', () => { ['2'], ]) - engine.setCellContents(adr('B1'), '{=TRANSPOSE(A2:A3)}') + engine.setCellContents(adr('B1'), '=TRANSPOSE(A2:A3)') const range = engine.rangeMapping.fetchRange(colStart('B'), colEnd('C')) const b1 = engine.addressMapping.fetchCell(adr('B1')) @@ -844,7 +852,7 @@ describe('row ranges', () => { ['=SUM(2:3)', '1', '2'], ]) - engine.setCellContents(adr('A2'), '{=TRANSPOSE(B1:C1)}') + engine.setCellContents(adr('A2'), '=TRANSPOSE(B1:C1)') const range = engine.rangeMapping.fetchRange(rowStart(2), rowEnd(3)) const a2 = engine.addressMapping.fetchCell(adr('A2')) diff --git a/test/cruds/clear-sheet.spec.ts b/test/cruds/clear-sheet.spec.ts index 6ca38ee4ed..86b3849fff 100644 --- a/test/cruds/clear-sheet.spec.ts +++ b/test/cruds/clear-sheet.spec.ts @@ -73,11 +73,11 @@ describe('Clear sheet content', () => { expect(changes.length).toEqual(2) }) - it('should clear sheet with formula matrix', () => { + it('should clear sheet with matrix', () => { const engine = HyperFormula.buildFromSheets({ Sheet1: [ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ], Sheet2: [ ['=Sheet1!A2'], diff --git a/test/cruds/copy-paste.spec.ts b/test/cruds/copy-paste.spec.ts index 31a573690f..0eb4afd11c 100644 --- a/test/cruds/copy-paste.spec.ts +++ b/test/cruds/copy-paste.spec.ts @@ -382,8 +382,7 @@ describe('Copy - paste integration', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'] + ['=TRANSPOSE(A1:B2)'], ]) expect(engine.matrixMapping.matrixMapping.size).toEqual(1) @@ -402,8 +401,7 @@ describe('Copy - paste integration', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'] + ['=TRANSPOSE(A1:B2)'], ]) engine.copy(adr('A1'), 2, 2) diff --git a/test/cruds/cut-paste.spec.ts b/test/cruds/cut-paste.spec.ts index 505e5dd96b..93e8587b67 100644 --- a/test/cruds/cut-paste.spec.ts +++ b/test/cruds/cut-paste.spec.ts @@ -335,7 +335,7 @@ describe('moving ranges', () => { it('should not be possible to move area with matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { @@ -347,7 +347,7 @@ describe('moving ranges', () => { it('should not be possible to move cells to area with matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { @@ -712,8 +712,7 @@ describe('overlapping areas', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], ]) engine.cut(simpleCellAddress(0, 0, 0), 2, 2) @@ -729,8 +728,7 @@ describe('overlapping areas', () => { ['3', '4'], ], Sheet2: [ - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], + ['=TRANSPOSE(Sheet1!A1:B2)'], ], }) @@ -747,14 +745,15 @@ describe('column index', () => { it('should update column index when moving cell', () => { const engine = HyperFormula.buildFromArray([ ['1'], - ['=VLOOKUP(1, A1:A1, 1, TRUE())'], + ['1'], + ['=VLOOKUP(1, A1:A2, 1, TRUE())'], ], {useColumnIndex: true}) engine.cut(adr('A1'), 1, 1) engine.paste(adr('B1')) const index = engine.columnSearch as ColumnIndex - expectArrayWithSameContent([1], index.getValueIndex(0, 0, 1).index) + expectArrayWithSameContent([1, 2], index.getValueIndex(0, 0, 1).index) expectArrayWithSameContent([0], index.getValueIndex(0, 1, 1).index) }) @@ -819,7 +818,7 @@ describe('move cells with matrices', () => { it('should not be possible to move part of formula matrix', function() { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { @@ -831,7 +830,7 @@ describe('move cells with matrices', () => { it('should not be possible to move formula matrix at all', function() { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { diff --git a/test/cruds/move-cells.spec.ts b/test/cruds/move-cells.spec.ts index 8b833918b4..60cf7f9584 100644 --- a/test/cruds/move-cells.spec.ts +++ b/test/cruds/move-cells.spec.ts @@ -78,8 +78,8 @@ describe('Moving rows - checking if its possible', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], + [], ['13'], ]) @@ -90,8 +90,8 @@ describe('Moving rows - checking if its possible', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], + [], ['13'], ]) @@ -452,7 +452,7 @@ describe('moving ranges', () => { it('should not be possible to move area with matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { @@ -463,7 +463,7 @@ describe('moving ranges', () => { it('should not be possible to move cells to area with matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { @@ -812,8 +812,7 @@ describe('overlapping areas', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], ]) engine.moveCells(simpleCellAddress(0, 0, 0), 2, 2, simpleCellAddress(0, 2, 0)) @@ -828,8 +827,7 @@ describe('overlapping areas', () => { ['3', '4'], ], Sheet2: [ - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], - ['{=TRANSPOSE(Sheet1!A1:B2)}', '{=TRANSPOSE(Sheet1!A1:B2)}'], + ['=TRANSPOSE(Sheet1!A1:B2)'], ], }) @@ -875,13 +873,14 @@ describe('column index', () => { it('should update column index when moving cell', () => { const engine = HyperFormula.buildFromArray([ ['1'], - ['=VLOOKUP(1, A1:A1, 1, TRUE())'], + ['1'], + ['=VLOOKUP(1, A1:A2, 1, TRUE())'], ], { useColumnIndex: true }) engine.moveCells(adr('A1'), 1, 1, adr('B1')) const index = engine.columnSearch as ColumnIndex - expectArrayWithSameContent([1], index.getValueIndex(0, 0, 1).index) + expectArrayWithSameContent([1, 2], index.getValueIndex(0, 0, 1).index) expectArrayWithSameContent([0], index.getValueIndex(0, 1, 1).index) }) @@ -944,7 +943,7 @@ describe('move cells with matrices', () => { it('should not be possible to move part of formula matrix', function() { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { @@ -955,7 +954,7 @@ describe('move cells with matrices', () => { it('should not be possible to move formula matrix at all', function() { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(() => { diff --git a/test/cruds/move-columns.spec.ts b/test/cruds/move-columns.spec.ts index 38c5e16e49..6e0310825f 100644 --- a/test/cruds/move-columns.spec.ts +++ b/test/cruds/move-columns.spec.ts @@ -59,7 +59,7 @@ describe('Ensure it is possible to move columns', () => { it('should not be possible to move row with formula matrix', () => { const engine = HyperFormula.buildFromArray([ - ['1', '{=TRANSPOSE(A1:A2)}', '{=TRANSPOSE(A1:A2)}'], + ['1', '=TRANSPOSE(A1:A2)'], ['2'], ]) @@ -69,7 +69,7 @@ describe('Ensure it is possible to move columns', () => { it('should not be possible to move row inside formula matrix', () => { const engine = HyperFormula.buildFromArray([ - ['1', '', '{=TRANSPOSE(A1:A2)}', '{=TRANSPOSE(A1:A2)}'], + ['1', '', '=TRANSPOSE(A1:A2)'], ['2'], ]) diff --git a/test/cruds/move-rows.spec.ts b/test/cruds/move-rows.spec.ts index 9fb946f927..1274eb0f9c 100644 --- a/test/cruds/move-rows.spec.ts +++ b/test/cruds/move-rows.spec.ts @@ -64,8 +64,7 @@ describe('Ensure it is possible to move rows', () => { it('should not be possible to move row with formula matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(engine.isItPossibleToMoveRows(0, 1, 1, 5)).toBe(false) @@ -76,8 +75,7 @@ describe('Ensure it is possible to move rows', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], [''], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(engine.isItPossibleToMoveRows(0, 0, 1, 2)).toBe(true) diff --git a/test/cruds/removing-columns.spec.ts b/test/cruds/removing-columns.spec.ts index 24d833acbd..79c23b80ec 100644 --- a/test/cruds/removing-columns.spec.ts +++ b/test/cruds/removing-columns.spec.ts @@ -61,8 +61,8 @@ describe('Removing columns - checking if its possible', () => { it('no if theres a formula matrix in place where we remove', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}', '13'], - ['3', '4', '{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['1', '2', '=TRANSPOSE(A1:B2)', undefined, '13'], + ['3', '4'], ]) expect(engine.isItPossibleToRemoveColumns(0, [1, 1])).toEqual(true) @@ -505,7 +505,7 @@ describe('Removing columns - reevaluation', () => { describe('Removing columns - matrices', () => { it('should not remove column within formula matrix', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=MMULT(A1:B2, A1:B2)}'], + ['1', '2', '=MMULT(A1:B2, A1:B2)'], ['3', '4'], ]) @@ -588,9 +588,8 @@ describe('Removing columns - matrices', () => { it('MatrixVertex#formula should be updated', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '3', '{=TRANSPOSE(A1:C2)}', '{=TRANSPOSE(A1:C2)}'], - ['4', '5', '6', '{=TRANSPOSE(A1:C2)}', '{=TRANSPOSE(A1:C2)}'], - ['', '', '', '{=TRANSPOSE(A1:C2)}', '{=TRANSPOSE(A1:C2)}'], + ['1', '2', '3', '=TRANSPOSE(A1:C2)'], + ['4', '5', '6'], ]) engine.removeColumns(0, [1, 1]) @@ -600,9 +599,8 @@ describe('Removing columns - matrices', () => { it('MatrixVertex#address should be updated', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '3', '{=TRANSPOSE(A1:C2)}', '{=TRANSPOSE(A1:C2)}'], - ['4', '5', '6', '{=TRANSPOSE(A1:C2)}', '{=TRANSPOSE(A1:C2)}'], - ['', '', '', '{=TRANSPOSE(A1:C2)}', '{=TRANSPOSE(A1:C2)}'], + ['1', '2', '3', '=TRANSPOSE(A1:C2)'], + ['4', '5', '6'], ]) engine.removeColumns(0, [1, 1]) @@ -618,9 +616,7 @@ describe('Removing columns - matrices', () => { ['4', '5', '6'], ], Sheet2: [ - ['{=TRANSPOSE(Sheet1!A1:C2)}', '{=TRANSPOSE(Sheet1!A1:C2)}'], - ['{=TRANSPOSE(Sheet1!A1:C2)}', '{=TRANSPOSE(Sheet1!A1:C2)}'], - ['{=TRANSPOSE(Sheet1!A1:C2)}', '{=TRANSPOSE(Sheet1!A1:C2)}'], + ['=TRANSPOSE(Sheet1!A1:C2)'], ], }) diff --git a/test/cruds/removing-rows.spec.ts b/test/cruds/removing-rows.spec.ts index a21f33a48b..a7bda0a33e 100644 --- a/test/cruds/removing-rows.spec.ts +++ b/test/cruds/removing-rows.spec.ts @@ -63,8 +63,7 @@ describe('Removing rows - checking if its possible', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], - ['{=TRANSPOSE(A1:B2)}', '{=TRANSPOSE(A1:B2)}'], + ['=TRANSPOSE(A1:B2)'], ['13'], ]) @@ -565,7 +564,7 @@ describe('Removing rows - matrices', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=MMULT(A1:B2, A1:B2)}'], + ['=MMULT(A1:B2, A1:B2)'], ]) expect(() => engine.removeRows(0, [2, 1])).toThrowError('Cannot perform this operation, source location has a matrix inside.') @@ -649,8 +648,7 @@ describe('Removing rows - matrices', () => { ['1', '4'], ['2', '5'], ['3', '6'], - ['{=TRANSPOSE(A1:B3)}', '{=TRANSPOSE(A1:B3)}, {=TRANSPOSE(A1:B3)}'], - ['{=TRANSPOSE(A1:B3)}', '{=TRANSPOSE(A1:B3)}, {=TRANSPOSE(A1:B3)}'], + ['=TRANSPOSE(A1:B3)'], ]) engine.removeRows(0, [1, 1]) @@ -663,8 +661,7 @@ describe('Removing rows - matrices', () => { ['1', '4'], ['2', '5'], ['3', '6'], - ['{=TRANSPOSE(A1:B3)}', '{=TRANSPOSE(A1:B3)}, {=TRANSPOSE(A1:B3)}'], - ['{=TRANSPOSE(A1:B3)}', '{=TRANSPOSE(A1:B3)}, {=TRANSPOSE(A1:B3)}'], + ['=TRANSPOSE(A1:B3)'], ]) engine.removeRows(0, [1, 1]) @@ -681,8 +678,7 @@ describe('Removing rows - matrices', () => { ['3', '6'], ], Sheet2: [ - ['{=TRANSPOSE(Sheet1!A1:B3)}', '{=TRANSPOSE(Sheet1!A1:B3)}'], - ['{=TRANSPOSE(Sheet1!A1:B3)}', '{=TRANSPOSE(Sheet1!A1:B3)}'], + ['=TRANSPOSE(Sheet1!A1:B3)'], ], }) diff --git a/test/cruds/removing-sheet.spec.ts b/test/cruds/removing-sheet.spec.ts index 55b64f117c..0c81728cef 100644 --- a/test/cruds/removing-sheet.spec.ts +++ b/test/cruds/removing-sheet.spec.ts @@ -267,24 +267,19 @@ describe('remove sheet - adjust matrix mapping', () => { const engine = HyperFormula.buildFromSheets({ Sheet1: [ ['1', '2'], - ['{=TRANSPOSE(A1:A1)}'], - ['{=TRANSPOSE(A2:A2)}'], + ['=TRANSPOSE(A1:B1)'], ], Sheet2: [ ['1', '2'], - ['{=TRANSPOSE(A1:A1)}'], - ['{=TRANSPOSE(A2:A2)}'], + ['=TRANSPOSE(A1:B1)'], ], }) - expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A2'), 1, 1))).toBeInstanceOf(MatrixVertex) - expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A3'), 1, 1))).toBeInstanceOf(MatrixVertex) + expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A2'), 1, 2))).toBeInstanceOf(MatrixVertex) engine.removeSheet('Sheet1') - expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A2'), 1, 1))).toBeUndefined() - expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A3'), 1, 1))).toBeUndefined() - expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A2', 1), 1, 1))).toBeInstanceOf(MatrixVertex) - expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A3', 1), 1, 1))).toBeInstanceOf(MatrixVertex) + expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A2'), 1, 2))).toBeUndefined() + expect(engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A2', 1), 1, 2))).toBeInstanceOf(MatrixVertex) }) }) diff --git a/test/cruds/set-column-order.spec.ts b/test/cruds/set-column-order.spec.ts index ec8075187c..50ae02ed55 100644 --- a/test/cruds/set-column-order.spec.ts +++ b/test/cruds/set-column-order.spec.ts @@ -44,23 +44,23 @@ describe('swapping columns - checking if it is possible', () => { }) it('should check for matrices', () => { - const engine = HyperFormula.buildFromArray([[0, 0, '{=1}']]) + const engine = HyperFormula.buildFromArray([[0, 0, '=TRANSPOSE(A1:B1)']]) expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(false) expect(() => 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.isItPossibleToSwapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(true) + it('should check for matrices only in moved columns', () => { + const engine = HyperFormula.buildFromArray([[0, 0, '=TRANSPOSE(A1:B1)']]) + expect(engine.isItPossibleToSwapColumnIndexes(0, [[0, 1], [1, 0], [2, 2]])).toEqual(true) expect(() => - engine.swapColumnIndexes(0, [[0, 2], [1, 1], [2, 0]]) + engine.swapColumnIndexes(0, [[0, 1], [1, 0], [2, 2]]) ).not.toThrowError() }) }) -describe('swapping rows should correctly work', () => { +describe('swapping columns 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) @@ -209,7 +209,7 @@ describe('setting column order - checking if it is possible', () => { }) it('should check for matrices', () => { - const engine = HyperFormula.buildFromArray([[0, 0, '{=1}']]) + const engine = HyperFormula.buildFromArray([[0, 0, '=TRANSPOSE(A1:B1)']]) expect(engine.isItPossibleToSetColumnOrder(0, [2, 1, 0])).toEqual(false) expect(() => engine.setColumnOrder(0, [2, 1, 0]) @@ -217,10 +217,10 @@ describe('setting column order - checking if it is possible', () => { }) 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) + const engine = HyperFormula.buildFromArray([[0, 0, '=TRANSPOSE(A1:B1)']]) + expect(engine.isItPossibleToSetColumnOrder(0, [1, 0, 2])).toEqual(true) expect(() => - engine.setColumnOrder(0, [2, 1, 0]) + engine.setColumnOrder(0, [1, 0, 2]) ).not.toThrowError() }) }) diff --git a/test/cruds/set-matrix-empty.spec.ts b/test/cruds/set-matrix-empty.spec.ts index 28dcf86f1f..84006969ce 100644 --- a/test/cruds/set-matrix-empty.spec.ts +++ b/test/cruds/set-matrix-empty.spec.ts @@ -6,8 +6,7 @@ describe('Set matrix empty', () => { it('should set matrix empty', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) const dependencyGraph = engine.dependencyGraph // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -23,8 +22,7 @@ describe('Set matrix empty', () => { it('should adjust edges between matrix cells and formula', () => { const engine = HyperFormula.buildFromArray([ ['1', '2', '=A1+A2'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) const dependencyGraph = engine.dependencyGraph // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -48,9 +46,8 @@ describe('Set matrix empty', () => { it('should adjust edges between matrix cells and formula matrix', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=TRANSPOSE(A2)}'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['1', '2', '=TRANSPOSE(A1:B1)'], + ['=TRANSPOSE(A1:B1)'], ]) const dependencyGraph = engine.dependencyGraph // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -63,16 +60,15 @@ describe('Set matrix empty', () => { expect(dependencyGraph.matrixMapping.matrixMapping.size).toEqual(1) const formulaMatrix = dependencyGraph.fetchCell(adr('C1')) - const a2 = dependencyGraph.fetchCell(adr('A2')) + const a1 = dependencyGraph.fetchCell(adr('A1')) expect(dependencyGraph.existsEdge(matrixVertex, formulaMatrix)).toBe(false) - expect(dependencyGraph.existsEdge(a2, formulaMatrix)).toBe(true) + expect(dependencyGraph.existsEdge(a1, formulaMatrix)).toBe(false) }) it('should adjust edges between matrix cells and range', () => { const engine = HyperFormula.buildFromArray([ ['1', '2', '=SUM(A2:A3)'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) const dependencyGraph = engine.dependencyGraph // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -98,8 +94,7 @@ describe('Set matrix empty', () => { it('should adjust edges between matrix cells and range crossing matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2', '=SUM(A1:A2)'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) const dependencyGraph = engine.dependencyGraph // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/test/cruds/set-row-order.spec.ts b/test/cruds/set-row-order.spec.ts index b8f5c7b070..2894c36e7d 100644 --- a/test/cruds/set-row-order.spec.ts +++ b/test/cruds/set-row-order.spec.ts @@ -44,7 +44,7 @@ describe('swapping rows - checking if it is possible', () => { }) it('should check for matrices', () => { - const engine = HyperFormula.buildFromArray([[0], [0], ['{=1}']]) + const engine = HyperFormula.buildFromArray([[0], [0], ['=TRANSPOSE(A1:A2)']]) expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(false) expect(() => engine.swapRowIndexes(0, [[0, 2], [1, 1], [2, 0]]) @@ -52,10 +52,10 @@ describe('swapping rows - checking if it is possible', () => { }) it('should check for matrices only in moved rows', () => { - const engine = HyperFormula.buildFromArray([[0], ['{=1}'], [0]]) - expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 2], [1, 1], [2, 0]])).toEqual(true) + const engine = HyperFormula.buildFromArray([[0], [0], ['=TRANSPOSE(A1:A2)']]) + expect(engine.isItPossibleToSwapRowIndexes(0, [[0, 1], [1, 0], [2, 2]])).toEqual(true) expect(() => - engine.swapRowIndexes(0, [[0, 2], [1, 1], [2, 0]]) + engine.swapRowIndexes(0, [[0, 1], [1, 0], [2, 2]]) ).not.toThrowError() }) }) @@ -216,7 +216,7 @@ describe('setting row order - checking if it is possible', () => { }) it('should check for matrices', () => { - const engine = HyperFormula.buildFromArray([[0], [0], ['{=1}']]) + const engine = HyperFormula.buildFromArray([[0], [0], ['=TRANSPOSE(A1:A2)']]) expect(engine.isItPossibleToSetRowOrder(0, [2, 1, 0])).toEqual(false) expect(() => engine.setRowOrder(0, [2, 1, 0]) @@ -224,10 +224,10 @@ 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, [2, 1, 0])).toEqual(true) + const engine = HyperFormula.buildFromArray([[0], [0], ['=TRANSPOSE(A1:A2)']]) + expect(engine.isItPossibleToSetRowOrder(0, [1, 0, 2])).toEqual(true) expect(() => - engine.setRowOrder(0, [2, 1, 0]) + engine.setRowOrder(0, [1, 0, 2]) ).not.toThrowError() }) }) diff --git a/test/disable-matrix-optimizations.spec.ts b/test/disable-matrix-optimizations.spec.ts index 2b649d1c59..5e515993cd 100644 --- a/test/disable-matrix-optimizations.spec.ts +++ b/test/disable-matrix-optimizations.spec.ts @@ -53,12 +53,12 @@ describe('Disable matrix optimizatoins', () => { const sheet = [ ['1', '2'], ['3', '4'], - ['{=TRANSPOSE(A1)}'], + ['=TRANSPOSE(A1:B2)'], ] const engine = HyperFormula.buildFromArray(sheet, {matrixDetection: true, matrixDetectionThreshold: 1}) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const matrix = engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A3'), 1, 1))! + const matrix = engine.matrixMapping.getMatrix(AbsoluteCellRange.spanFrom(adr('A3'), 2, 2))! engine.dependencyGraph.disableNumericMatrices() @@ -68,7 +68,7 @@ describe('Disable matrix optimizatoins', () => { expect(a1).toBeInstanceOf(ValueCellVertex) expect(engine.graph.getDependencies(matrix).length).toBe(1) - expect(engine.graph.existsEdge(a1, matrix)).toBe(true) + expect(engine.graph.existsEdge(a1, matrix)).toBe(false) expect(engine.graph.existsEdge(b1, matrix)).toBe(false) }) diff --git a/test/engine.spec.ts b/test/engine.spec.ts index 8f6cfe6de7..1c0a096f5d 100644 --- a/test/engine.spec.ts +++ b/test/engine.spec.ts @@ -154,14 +154,13 @@ describe('#getCellFormula', () => { const engine = HyperFormula.buildFromArray([ ['1', '1'], ['1', '1'], - ['{=MMULT(A1:B2,A1:B2)}', '{=MMULT(A1:B2,A1:B2)}'], - ['{=MMULT(A1:B2,A1:B2)}', '{=MMULT(A1:B2,A1:B2)}'], + ['=MMULT(A1:B2,A1:B2)'], ]) - expect(engine.getCellFormula(adr('A3'))).toEqual('{=MMULT(A1:B2,A1:B2)}') - expect(engine.getCellFormula(adr('A4'))).toEqual('{=MMULT(A1:B2,A1:B2)}') - expect(engine.getCellFormula(adr('B3'))).toEqual('{=MMULT(A1:B2,A1:B2)}') - expect(engine.getCellFormula(adr('B4'))).toEqual('{=MMULT(A1:B2,A1:B2)}') + expect(engine.getCellFormula(adr('A3'))).toEqual('=MMULT(A1:B2,A1:B2)') + expect(engine.getCellFormula(adr('A4'))).toEqual(undefined) + expect(engine.getCellFormula(adr('B3'))).toEqual(undefined) + expect(engine.getCellFormula(adr('B4'))).toEqual(undefined) }) it('returns undefined for numeric matrices', () => { @@ -187,11 +186,11 @@ describe('#getCellFormula', () => { it('returns invalid matrix formula literal', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(}'] + ['=TRANSPOSE('] ]) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.ERROR, ErrorMessage.ParseError)) - expect(engine.getCellFormula(adr('A1'))).toEqual('{=TRANSPOSE(}') + expect(engine.getCellFormula(adr('A1'))).toEqual('=TRANSPOSE(') }) }) @@ -278,8 +277,7 @@ describe('#getCellValue', () => { it('should return value of a cell in a formula matrix', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], - ['{=TRANSPOSE(A1:B1)}'], - ['{=TRANSPOSE(A1:B1)}'], + ['=TRANSPOSE(A1:B1)'], ]) expect(engine.getCellValue(adr('A2'))).toEqual(1) @@ -595,7 +593,7 @@ describe('#getCellType', () => { }) it('formula matrix', () => { - const engine = HyperFormula.buildFromArray([['{=TRANSPOSE(C1:C2)}', '{=TRANSPOSE(C1:C2)}']]) + const engine = HyperFormula.buildFromArray([['=TRANSPOSE(C1:C2)']]) expect(engine.getCellType(adr('A1'))).toBe(CellType.MATRIX) expect(engine.getCellType(adr('B1'))).toBe(CellType.MATRIX) @@ -726,7 +724,7 @@ describe('#doesCellHaveSimpleValue', () => { }) it('false', () => { - const engine = HyperFormula.buildFromArray([['=SUM(1, 2)', null, '{=TRANSPOSE(A1:A1)}']]) + const engine = HyperFormula.buildFromArray([['=SUM(1, 2)', null, '=TRANSPOSE(A1:A1)']]) expect(engine.doesCellHaveSimpleValue(adr('A1'))).toEqual(false) expect(engine.doesCellHaveSimpleValue(adr('B1'))).toEqual(false) expect(engine.doesCellHaveSimpleValue(adr('C1'))).toEqual(false) @@ -768,7 +766,7 @@ describe('#isCellEmpty', () => { describe('#isCellPartOfMatrix', () => { it('true', () => { - const engine = HyperFormula.buildFromArray([['{=TRANSPOSE(B1:B1)}']]) + const engine = HyperFormula.buildFromArray([['=TRANSPOSE(B1:C1)']]) expect(engine.isCellPartOfMatrix(adr('A1'))).toEqual(true) }) diff --git a/test/interpreter/function-formulatext.spec.ts b/test/interpreter/function-formulatext.spec.ts index 3db98467ba..f8afb74387 100644 --- a/test/interpreter/function-formulatext.spec.ts +++ b/test/interpreter/function-formulatext.spec.ts @@ -67,14 +67,6 @@ describe('Function FORMULATEXT', () => { expect(engine.getCellValue(adr('B1'))).toEqualError(detailedError(ErrorType.REF, ErrorMessage.CellRefExpected)) }) - it('should work for matrix formula', () => { - const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(A2)}', '=FORMULATEXT(A1)'] - ]) - - expect(engine.getCellValue(adr('B1'))).toEqual('{=TRANSPOSE(A2)}') - }) - it('should work for unparsed formula', () => { const engine = HyperFormula.buildFromArray([ ['=SUM(1,', '=FORMULATEXT(A1)'] diff --git a/test/interpreter/function-vlookup.spec.ts b/test/interpreter/function-vlookup.spec.ts index 3fc1707cf4..c885e98ea8 100644 --- a/test/interpreter/function-vlookup.spec.ts +++ b/test/interpreter/function-vlookup.spec.ts @@ -262,7 +262,7 @@ const sharedExamples = (builder: (sheet: Sheet, config?: Partial) ['=VLOOKUP(3, A4:B6, 2, TRUE())'], ['1', '2', '3'], ['4', '5', '6'], - ['{=TRANSPOSE(A2:C3)}'], + ['=TRANSPOSE(A2:C3)'], ]) expect(engine.getCellValue(adr('A1'))).toEqual(6) @@ -273,7 +273,7 @@ const sharedExamples = (builder: (sheet: Sheet, config?: Partial) ['=VLOOKUP(4, A4:B6, 2, TRUE())'], ['1', '2', '3'], ['4', '5', '6'], - ['{=TRANSPOSE(A2:C3)}'], + ['=TRANSPOSE(A2:C3)'], ]) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.ValueNotFound)) diff --git a/test/interpreter/matrix-plugin.spec.ts b/test/interpreter/matrix-plugin.spec.ts index 5ff764b0f1..21b05aa635 100644 --- a/test/interpreter/matrix-plugin.spec.ts +++ b/test/interpreter/matrix-plugin.spec.ts @@ -1,10 +1,9 @@ -import {HyperFormula} from '../../src' -import {ErrorType} from '../../src/Cell' +import GPU from 'gpu.js' +import {ErrorType, HyperFormula} from '../../src' +import {ConfigParams} from '../../src/Config' import {ErrorMessage} from '../../src/error-message' import {MatrixPlugin} from '../../src/interpreter/plugin/MatrixPlugin' import {adr, detailedError} from '../testUtils' -import {ConfigParams} from '../../src/Config' -import GPU from 'gpu.js' describe('Matrix plugin', () => { beforeAll(() => { @@ -19,7 +18,7 @@ describe('Matrix plugin', () => { ['5', '6'], ['1', '2'], ['3', '4'], - ['{=MMULT(A1:B3,A4:B5)}'], + ['=MMULT(A1:B3,A4:B5)'], ], config) @@ -39,7 +38,7 @@ describe('Matrix plugin', () => { ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], - ['{=mmult(A1:B3,A4:C6)}'], + ['=mmult(A1:B3,A4:C6)'], ], config) expect(engine.getCellValue(adr('A7'))).toEqualError(detailedError(ErrorType.VALUE, ErrorMessage.MatrixDimensions)) @@ -48,9 +47,9 @@ describe('Matrix plugin', () => { it('matrix multiplication with string in data', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=MMULT(A1:B2,A3:B4)}'], + ['1', '2', '=MMULT(A1:B2,A3:B4)'], ['3', 'foo'], - ['1', '2', '{=MMULT(A3:B4,A1:B2)}'], + ['1', '2', '=MMULT(A3:B4,A1:B2)'], ['3', '4'], ], config) @@ -68,7 +67,7 @@ describe('Matrix plugin', () => { const engine = HyperFormula.buildFromArray([ ['1', '2'], ['3', '4'], - ['{=MMULT(A1:B2, MMULT(A1:B2,A1:B2))}'], + ['=MMULT(A1:B2, MMULT(A1:B2,A1:B2))'], ], config) expect(engine.getCellValue(adr('A3'))).toEqual(37) @@ -79,9 +78,9 @@ describe('Matrix plugin', () => { it('mmult of other mmult', () => { const engine = HyperFormula.buildFromArray([ - ['1', '2', '{=MMULT(A1:B2, A1:B2)}', '{=MMULT(A1:B2, A1:B2)}'], - ['3', '4', '{=MMULT(A1:B2, A1:B2)}', '{=MMULT(A1:B2, A1:B2)}'], - ['{=MMULT(A1:B2, C1:D2)}'], + ['1', '2', '=MMULT(A1:B2, A1:B2)', '=MMULT(A1:B2, A1:B2)'], + ['3', '4', '=MMULT(A1:B2, A1:B2)', '=MMULT(A1:B2, A1:B2)'], + ['=MMULT(A1:B2, C1:D2)'], ], config) expect(engine.getCellValue(adr('A3'))).toEqual(37) @@ -92,7 +91,7 @@ describe('Matrix plugin', () => { it('mmult of a number', () => { const engine = HyperFormula.buildFromArray([ - ['{=MMULT(3, 4)}'], + ['=MMULT(3, 4)'], ], config) expect(engine.getCellValue(adr('A1'))).toEqual(12) @@ -100,7 +99,7 @@ describe('Matrix plugin', () => { it('mmult wrong number of arguments', () => { const engine = HyperFormula.buildFromArray([ - ['{=MMULT(A1:A2)}', '{=MMULT(A1:A2, B1:B2, C1:C2)}'], + ['=MMULT(0)', '=MMULT(0,0,0)'], ], config) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) @@ -132,7 +131,7 @@ describe('Matrix plugin', () => { ['1', '2', '3', '4', '5', '6'], ['11', '12', '13', '14', '15', '16'], ['21', '22', '23', '24', '25', '26'], - ['{=maxpool(A1:F3,3)}'], + ['=maxpool(A1:F3,3)'], ], config) expect(engine.getCellValue(adr('A4'))).toBeCloseTo(23) @@ -145,7 +144,7 @@ describe('Matrix plugin', () => { ['11', '12', '13', '14', '15', '16'], ['21', '22', '23', '24', '25', '26'], ['28', '29', '30', '31', '32', '33'], - ['{=maxpool(A1:F4,3,1)}'], + ['=maxpool(A1:F4,3,1)'], ], config) expect(engine.getCellValue(adr('A5'))).toBeCloseTo(23) @@ -160,7 +159,7 @@ describe('Matrix plugin', () => { it('maxpool wrong number of arguments', () => { const engine = HyperFormula.buildFromArray([ - ['{=MAXPOOL(A1:A2)}', '{=MAXPOOL(A1:A2, 0,0,0)}'], + ['=MAXPOOL(0)', '=MAXPOOL(0, 0,0,0)'], ], config) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) @@ -171,7 +170,7 @@ describe('Matrix plugin', () => { const engine = HyperFormula.buildFromArray([ ['1', '2', '1', '2', '1', '5'], ['3', '4', '3', '7', '6', '7'], - ['{=medianpool(A1:F2,2)}'], + ['=medianpool(A1:F2,2)'], ], config) expect(engine.getCellValue(adr('A3'))).toBeCloseTo(2.5) @@ -181,7 +180,7 @@ describe('Matrix plugin', () => { it('medianpool wrong number of arguments', () => { const engine = HyperFormula.buildFromArray([ - ['{=MEDIANPOOL(A1:A2)}', '{=MEDIANPOOL(A1:A2, 0,0,0)}'], + ['=MEDIANPOOL(0)', '=MEDIANPOOL(0,0,0,0)'], ], config) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) @@ -202,7 +201,7 @@ describe('Matrix plugin', () => { ['4', '6', '7'], ['8', '8', '8'], - ['{=medianpool(A1:C9,3)}'], + ['=medianpool(A1:C9,3)'], ], config) expect(engine.getCellValue(adr('A10'))).toBeCloseTo(2) @@ -222,7 +221,7 @@ describe('Function TRANSPOSE', () => { ['1', '2'], ['3', '4'], ['5', '6'], - ['{=TRANSPOSE(A1:B3)}'], + ['=TRANSPOSE(A1:B3)'], ]) expect(engine.getCellValue(adr('A4'))).toBeCloseTo(1) @@ -235,7 +234,7 @@ describe('Function TRANSPOSE', () => { it('transpose works for scalar', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(1)}'], + ['=TRANSPOSE(1)'], ]) expect(engine.getCellValue(adr('A1'))).toBeCloseTo(1) @@ -243,15 +242,15 @@ describe('Function TRANSPOSE', () => { it('transpose returns error if argument evaluates to error', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE(4/0)}'], + ['=TRANSPOSE(4/0)'], ]) - expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.VALUE)) + expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.DIV_BY_ZERO)) }) it('transpose wrong number of arguments', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE()}', '{=TRANSPOSE(A1:A2, B1:B2)}'], + ['=TRANSPOSE()', '=TRANSPOSE(C1:C2, D1:D2)'], ]) expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.WrongArgNumber)) @@ -259,10 +258,10 @@ describe('Function TRANSPOSE', () => { }) it('transpose returns VALUE when wrong type', () => { const engine = HyperFormula.buildFromArray([ - ['{=TRANSPOSE("fdsa")}'], + ['=TRANSPOSE("fdsa")'], ]) - expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.VALUE)) + expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.VALUE, ErrorMessage.NumberRange)) }) it('transpose without braces', () => { @@ -273,8 +272,8 @@ describe('Function TRANSPOSE', () => { ['=TRANSPOSE(A1:B3)'], ]) - expect(engine.getCellValue(adr('A4'))).toEqualError(detailedError(ErrorType.VALUE, ErrorMessage.ScalarExpected)) - expect(engine.getCellValue(adr('A5'))).toBe(null) - expect(engine.getCellValue(adr('B4'))).toBe(null) + expect(engine.getCellValue(adr('A4'))).toBe(1) + expect(engine.getCellValue(adr('A5'))).toBe(2) + expect(engine.getCellValue(adr('B4'))).toBe(3) }) }) diff --git a/test/named-expressions.spec.ts b/test/named-expressions.spec.ts index 3275b02a6e..e42439ccd2 100644 --- a/test/named-expressions.spec.ts +++ b/test/named-expressions.spec.ts @@ -249,8 +249,8 @@ describe('Named expressions - store manipulation', () => { const engine = HyperFormula.buildEmpty() expect(() => { - engine.addNamedExpression('myName', '{=TRANSPOSE(A1:B2)}') - }).toThrowError(/Matrix formulas are not supported/) + engine.addNamedExpression('myName', '=TRANSPOSE(A1:B2)') + }).toThrowError(/Relative addresses not allowed in named expressions./) }) it('retrieving non-existing named expression', () => { @@ -324,8 +324,8 @@ describe('Named expressions - store manipulation', () => { engine.addNamedExpression('myName', '=42') expect(() => { - engine.changeNamedExpression('myName', '{=TRANSPOSE(A1:B2)}') - }).toThrowError(/not supported/) + engine.changeNamedExpression('myName', '=TRANSPOSE(A1:B2)') + }).toThrowError(/Relative addresses not allowed in named expressions./) }) it('changing not existing named expression', () => {