Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added support for array arithmetic. (#628)
- Added performance improvements for array handling. (#629)
- Added ARRAYFORMULA function. (#630)
- Added FILTER function. (#668)

### Fixed
- Fixed an issue with arrays and cruds. (#651)
Expand Down
1 change: 1 addition & 0 deletions docs/guide/built-in-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Total number of functions: **{{ $page.functionsCount }}**
| Function ID | Description | Syntax |
| :--- | :--- | :--- |
| ARRAYFORMULA | Enables array arithmetic inside. | ARRAYFORMULA(Formula) |
| FILTER | Filter input array based on boolean arrays. | FILTER(Array; BoolArray1[; BoolArray2[; ...]])

### Date and time

Expand Down
5 changes: 3 additions & 2 deletions src/DependencyGraph/MatrixVertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ export class MatrixVertex {
}

public setCellValue(matrix: Matrix) {
matrix.resize(this.matrix.size)
this.matrix = matrix
}

public setErrorValue(error: CellError) {
this.matrix = new ErroredMatrix(error, this.matrix.size)
}

public getCellValue(): Matrix | CellError {
public getCellValue(): Matrix {
if (this.matrix instanceof NotComputedMatrix) {
throw Error('Matrix not computed yet.')
}
return this.matrix as (Matrix | CellError)
return this.matrix as Matrix
}

public getMatrixCellValue(address: SimpleCellAddress): InternalScalarValue {
Expand Down
6 changes: 6 additions & 0 deletions src/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export class Evaluator {
const error = new CellError(ErrorType.CYCLE, undefined, vertex.address)
vertex.setCellValue(error)
changes.addChange(error, vertex.address)
} else if (vertex instanceof MatrixVertex && vertex.isFormula()) {
const error = new CellError(ErrorType.CYCLE, undefined, vertex.cellAddress)
vertex.setErrorValue(error)
changes.addChange(error, vertex.cellAddress)
}
},
)
Expand Down Expand Up @@ -144,6 +148,8 @@ export class Evaluator {
cycled.forEach((vertex: Vertex) => {
if (vertex instanceof FormulaCellVertex) {
vertex.setCellValue(new CellError(ErrorType.CYCLE, undefined, vertex.address))
} else if (vertex instanceof MatrixVertex && vertex.isFormula()) {
vertex.setErrorValue(new CellError(ErrorType.CYCLE, undefined, vertex.cellAddress))
}
})
sorted.forEach((vertex: Vertex) => {
Expand Down
25 changes: 20 additions & 5 deletions src/Matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import {CellError, SimpleCellAddress, simpleCellAddress} from './Cell'
import {InternalScalarValue} from './interpreter/InterpreterValue'
import {EmptyValue, InternalScalarValue} from './interpreter/InterpreterValue'
import {MatrixSize} from './MatrixSize'

export interface IMatrix {
Expand Down Expand Up @@ -45,13 +45,13 @@ export class Matrix implements IMatrix {
}

public addRows(aboveRow: number, numberOfRows: number) {
this.matrix.splice(aboveRow, 0, ...this.zeroArrays(numberOfRows, this.width()))
this.matrix.splice(aboveRow, 0, ...this.nullArrays(numberOfRows, this.width()))
this.size.height += numberOfRows
}

public addColumns(aboveColumn: number, numberOfColumns: number) {
for (let i = 0; i < this.height(); i++) {
this.matrix[i].splice(aboveColumn, 0, ...new Array(numberOfColumns).fill(0))
this.matrix[i].splice(aboveColumn, 0, ...new Array(numberOfColumns).fill(EmptyValue))
}
this.size.width += numberOfColumns
}
Expand All @@ -76,10 +76,10 @@ export class Matrix implements IMatrix {
this.size.width -= numberOfColumns
}

public zeroArrays(count: number, size: number) {
public nullArrays(count: number, size: number) {
const result = []
for (let i = 0; i < count; ++i) {
result.push(new Array(size).fill(0))
result.push(new Array(size).fill(EmptyValue))
}
return result
}
Expand Down Expand Up @@ -118,6 +118,21 @@ export class Matrix implements IMatrix {
}
}

public resize(newSize: MatrixSize) {
if(this.height() < newSize.height) {
this.addRows(this.height(), newSize.height-this.height())
}
if(this.height() > newSize.height) {
throw "Resizing to smaller matrix"
}
if(this.width() < newSize.width) {
this.addColumns(this.width(), newSize.width-this.width())
}
if(this.width() > newSize.width) {
throw "Resizing to smaller matrix"
}
}

private outOfBound(col: number, row: number): boolean {
return col < 0 || row < 0 || row > this.size.height - 1 || col > this.size.width - 1
}
Expand Down
17 changes: 15 additions & 2 deletions src/MatrixSize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,25 @@ export class MatrixSizePredictor {
}
return subChecks[0]
}
case 'FILTER': {
if (ast.args.length <= 1) {
return undefined
}
for (const subcheck of subChecks) {
if (subcheck === undefined) {
return undefined
}
}
const width = Math.max(...(subChecks as MatrixSize[]).map(val => val.width))
const height = Math.max(...(subChecks as MatrixSize[]).map(val => val.height))
return new MatrixSize(width, height)
}
case 'SWITCH': {
if(ast.args.length === 0) {
if (ast.args.length === 0) {
return undefined
}
const size = subChecks[0]
if(size === undefined) {
if (size === undefined) {
return undefined
}
return new MatrixSize(size.width, size.height)
Expand Down
2 changes: 2 additions & 0 deletions src/error-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class ErrorMessage {
public static BadCriterion = 'Incorrect criterion.'
public static RangeManySheets = 'Range spans more than one sheet.'
public static CellRangeExpected = 'Cell range expected.'
public static WrongDimension = 'Wrong range dimension.'
public static ScalarExpected = 'Cell range not allowed.'
public static NumberCoercion = 'Value cannot be coerced to number.'
public static NumberExpected = 'Number argument expected.'
Expand All @@ -32,6 +33,7 @@ export class ErrorMessage {
public static NotHex = 'String does not represent a hexadecimal number.'
public static EndStartPeriod = 'End period needs to be at least start period.'
public static CellRefExpected = 'Cell reference expected.'
public static EmptyRange = 'Empty range not allowed.'
public static BadRef = 'Address is not correct.'
public static NumberRange = 'Number-only range expected.'
public static ValueNotFound = 'Value not found.'
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/csCZ.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#HODNOTA!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/daDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VÆRDI!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/deDE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#WERT!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/enGB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VALUE!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/esES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const dictionary: RawTranslationPackage = {
VALUE: '#¡VALOR!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/fiFI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#ARVO!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ITSEISARVO',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/frFR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VALEUR!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/huHU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#ÉRTÉK!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/itIT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VALORE!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ASS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/nbNO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VERDI!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/nlNL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#WAARDE!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'BOOGCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/plPL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#ARG!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'WART.BEZWGL',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/ptPT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VALOR!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/ruRU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#ЗНАЧ!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ACOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/svSE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#VÄRDEFEL!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'ABS',
ACOS: 'ARCCOS',
Expand Down
1 change: 1 addition & 0 deletions src/i18n/languages/trTR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const dictionary: RawTranslationPackage = {
VALUE: '#DEĞER!',
},
functions: {
FILTER: 'FILTER',
ARRAYFORMULA: 'ARRAYFORMULA',
ABS: 'MUTLAK',
ACOS: 'ACOS',
Expand Down
23 changes: 3 additions & 20 deletions src/interpreter/FunctionRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ function validateAndReturnMetadataFromName(functionId: string, plugin: FunctionP
return entry
}

function maybeMetadataFromName(functionId: string, plugin: FunctionPluginDefinition): Maybe<FunctionMetadata> {
const entry = plugin.implementedFunctions[functionId]
const key = plugin.aliases?.[functionId]
if(key === undefined) {
return entry
}
if (entry !== undefined) {
return undefined
}
return plugin.implementedFunctions[key]
}

export class FunctionRegistry {
public static plugins: Map<string, FunctionPluginDefinition> = new Map()

Expand Down Expand Up @@ -176,6 +164,7 @@ export class FunctionRegistry {
private readonly arrayFunctions: Set<string> = new Set()
private readonly structuralChangeFunctions: Set<string> = new Set()
private readonly functionsWhichDoesNotNeedArgumentsToBeComputed: Set<string> = new Set()
private readonly functionsMetadata: Map<string,FunctionMetadata> = new Map()

constructor(private config: Config) {
if (config.functionPlugins.length > 0) {
Expand Down Expand Up @@ -228,14 +217,7 @@ export class FunctionRegistry {
}

public getMetadata(functionId: string): Maybe<FunctionMetadata> {
const pluginEntry = this.functions.get(functionId)
if (pluginEntry !== undefined && this.config.translationPackage.isFunctionTranslated(functionId)) {
const [_pluginFunction, pluginInstance] = pluginEntry
const pluginDefinition = Object.getPrototypeOf(pluginInstance).constructor as FunctionPluginDefinition
return maybeMetadataFromName(functionId, pluginDefinition)
} else {
return undefined
}
return this.functionsMetadata.get(functionId)
}

public getPlugins(): FunctionPluginDefinition[] {
Expand Down Expand Up @@ -273,5 +255,6 @@ export class FunctionRegistry {
if (functionMetadata.isDependentOnSheetStructureChange) {
this.structuralChangeFunctions.add(functionId)
}
this.functionsMetadata.set(functionId, functionMetadata)
}
}
Loading