Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/AbsoluteCellRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -37,6 +38,17 @@ export class AbsoluteCellRange {
)
}

public static fromCellRangeOrUndef(x: CellRange, baseAddress: SimpleCellAddress): Maybe<AbsoluteCellRange> {
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)
Expand Down
20 changes: 1 addition & 19 deletions src/CellContentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

/**
Expand All @@ -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<string, ErrorType>): boolean {
const upperCased = text.toUpperCase()
const errorRegex = /#[A-Za-z0-9\/]+[?!]?/
Expand Down Expand Up @@ -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)) {
Expand Down
12 changes: 2 additions & 10 deletions src/CrudOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -21,7 +21,6 @@ import {
import {
InvalidAddressError,
InvalidArgumentsError,
MatrixFormulasNotSupportedError,
NamedExpressionDoesNotExistError,
NamedExpressionNameIsAlreadyTakenError,
NamedExpressionNameIsInvalidError,
Expand Down Expand Up @@ -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')
}
}
}
}

Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/DependencyGraph/AddressMapping/AddressMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions src/DependencyGraph/AddressMapping/DenseStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -33,7 +34,7 @@ export class DenseStrategy implements IAddressMappingStrategy {
}

/** @inheritDoc */
public getCell(address: SheetCellAddress): CellVertex | null {
public getCell(address: SheetCellAddress): Maybe<CellVertex> {
return this.getCellVertex(address.col, address.row)
}

Expand Down Expand Up @@ -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<CellVertex> {
return this.mapping[y]?.[x]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import {SheetCellAddress, SimpleCellAddress} from '../../Cell'
import {Maybe} from '../../Maybe'
import {ColumnsSpan, RowsSpan} from '../../Span'
import {CellVertex} from '../Vertex'

Expand All @@ -17,7 +18,7 @@ export interface IAddressMappingStrategy {
*
* @param address - cell address
*/
getCell(address: SheetCellAddress): CellVertex | null,
getCell(address: SheetCellAddress): Maybe<CellVertex>,

/**
* Set vertex for given address
Expand Down
9 changes: 3 additions & 6 deletions src/DependencyGraph/AddressMapping/SparseStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<CellVertex> {
return this.mapping.get(address.col)?.get(address.row)
}

/** @inheritDoc */
Expand Down
65 changes: 32 additions & 33 deletions src/DependencyGraph/DependencyGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 {
/*
Expand Down Expand Up @@ -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')
}
Expand Down Expand Up @@ -548,32 +555,14 @@ 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 {
this.graph.addNode(vertex)
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<MatrixVertex> {
for (const vertex of this.graph.nodes) {
if (vertex instanceof MatrixVertex && vertex.isFormula()) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
Loading