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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added 9 text functions LEN, TRIM, PROPER, CLEAN, REPT, RIGHT, LEFT, SEARCH, FIND. (#221)
- Added helper methods for keeping track of cell/range dependencies: getCellPrecedents and getCellDependents. (#441)
- 4 financial functions FV, PMT, PPMT, IPMT.
- Added FORMULATEXT function.

### Fixed
- Fixed hardcoding of languages in i18n tests. (#471)
Expand Down
5 changes: 3 additions & 2 deletions docs/guide/built-in-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ lets you design your own [custom functions](custom-functions).
| XOR | Logical | Returns true if an odd number of arguments evaluates to TRUE. | XOR(Logicalvalue1; Logicalvalue2 ...Logicalvalue30) |
| CHOOSE | Lookup and reference | Uses an index to return a value from a list of up to 30 values.| CHOOSE(Index; Value1; ...; Value30) |
| COLUMNS | Lookup and reference | Returns the number of columns in the given reference. | COLUMNS(Array) |
| OFFSET | Lookup and reference | Returns the value of a cell offset by a certain number of rows and columns from a given reference point. | OFFSET(Reference; Rows; Columns; Height; Width) |
| ROWS | Lookup and reference | Returns the number of rows in the given reference. | ROWS(Array) |
| FORMULATEXT | Lookup and reference | Returns a formula in a given cell as a string. | FORMULATEXT(Reference) |
| INDEX | Lookup and reference | Returns the content of a cell, specified by row and column number, or an optional range name. | INDEX(Reference; Row; Column; Range) |
| MATCH | Lookup and reference | Returns the relative position of an item in an array that matches a specified value. | MATCH(Searchcriterion; Lookuparray; Type) |
| OFFSET | Lookup and reference | Returns the value of a cell offset by a certain number of rows and columns from a given reference point. | OFFSET(Reference; Rows; Columns; Height; Width) |
| ROWS | Lookup and reference | Returns the number of rows in the given reference. | ROWS(Array) |
| VLOOKUP | Lookup and reference | Searches vertically with reference to adjacent cells to the right. | VLOOKUP(Search_Criterion; Array; Index; Sort_Order) |
| ABS | Math and trigonometry | Returns the absolute value of a number. | ABS(Number) |
| ACOS | Math and trigonometry | Returns the inverse trigonometric cosine of a number. | ACOS(Number) |
Expand Down
6 changes: 3 additions & 3 deletions src/BuildEngineFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ export class BuildEngineFactory {
lazilyTransformingAstService.undoRedo = crudOperations.undoRedo
lazilyTransformingAstService.parser = parser

const evaluator = new Evaluator(dependencyGraph, columnSearch, config, stats, dateHelper, numberLiteralHelper, functionRegistry, namedExpressions)
evaluator.run()

const exporter = new Exporter(config, namedExpressions)
const serialization = new Serialization(dependencyGraph, unparser, config, exporter)

const evaluator = new Evaluator(dependencyGraph, columnSearch, config, stats, dateHelper, numberLiteralHelper, functionRegistry, namedExpressions, serialization)
evaluator.run()

stats.end(StatType.BUILD_ENGINE_TOTAL)

return {
Expand Down
6 changes: 4 additions & 2 deletions src/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {Statistics, StatType} from './statistics'
import {NumberLiteralHelper} from './NumberLiteralHelper'
import {NamedExpressions} from './NamedExpressions'
import {FunctionRegistry} from './interpreter/FunctionRegistry'
import {Serialization} from './Serialization'

export class Evaluator {
private interpreter: Interpreter
Expand All @@ -32,9 +33,10 @@ export class Evaluator {
public readonly dateHelper: DateTimeHelper,
private readonly numberLiteralsHelper: NumberLiteralHelper,
private readonly functionRegistry: FunctionRegistry,
private readonly namedExpressions: NamedExpressions
private readonly namedExpressions: NamedExpressions,
private readonly serialization: Serialization
) {
this.interpreter = new Interpreter(this.dependencyGraph, this.columnSearch, this.config, this.stats, this.dateHelper, this.numberLiteralsHelper, this.functionRegistry, this.namedExpressions)
this.interpreter = new Interpreter(this.dependencyGraph, this.columnSearch, this.config, this.stats, this.dateHelper, this.numberLiteralsHelper, this.functionRegistry, this.namedExpressions, this.serialization)
}

public run(): void {
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'NEPRAVDA',
FIND: 'NAJÍT',
FORMULATEXT: 'FORMULATEXT',
FV: 'BUDHODNOTA',
IF: 'KDYŽ',
IFERROR: 'IFERROR',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EKSP',
FALSE: 'FALSE',
FIND: 'FIND',
FORMULATEXT: 'FORMELTEKST',
FV: 'FV',
IF: 'HVIS',
IFERROR: 'HVIS.FEJL',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FALSCH',
FIND: 'FINDEN',
FORMULATEXT: 'FORMELTEKST',
FV: 'ZW',
IF: 'WENN',
IFERROR: 'WENNFEHLER',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FALSE',
FIND: 'FIND',
FORMULATEXT: 'FORMULATEXT',
FV: 'FV',
IF: 'IF',
IFERROR: 'IFERROR',
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 @@ -69,6 +69,7 @@ export const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FALSO',
FIND: 'ENCONTRAR',
FORMULATEXT: 'FORMULATEXTO',
FV: 'VF',
IF: 'SI',
IFERROR: 'SI.ERROR',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EKSPONENTTI',
FALSE: 'EPÄTOSI',
FIND: 'ETSI',
FORMULATEXT: 'KAAVA.TEKSTI',
FV: 'TULEVA.ARVO',
IF: 'JOS',
IFERROR: 'JOSVIRHE',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FAUX',
FIND: 'TROUVE',
FORMULATEXT: 'FORMULETEXTE',
FV: 'VC',
IF: 'SI',
IFERROR: 'SIERREUR',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'KITEVŐ',
FALSE: 'HAMIS',
FIND: 'SZÖVEG.TALÁL',
FORMULATEXT: 'KÉPLETSZÖVEG',
FV: 'JBÉ',
IF: 'HA',
IFERROR: 'HAHIBA',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FALSO',
FIND: 'TROVA',
FORMULATEXT: 'TESTO.FORMULA',
FV: 'VAL.FUT',
IF: 'SE',
IFERROR: 'SE.ERRORE',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EKSP',
FALSE: 'USANN',
FIND: 'FINN',
FORMULATEXT: 'FORMELTEKST',
FV: 'SLUTTVERDI',
IF: 'HVIS',
IFERROR: 'HVISFEIL',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'ONWAAR',
FIND: 'VIND.ALLES',
FORMULATEXT: 'FORMULETEKST',
FV: 'TW',
IF: 'ALS',
IFERROR: 'ALS.FOUT',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FAŁSZ',
FIND: 'ZNAJDŹ',
FORMULATEXT: 'FORMUŁA.TEKST',
FV: 'FV',
IF: 'JEŻELI',
IFERROR: 'JEŻELI.BŁĄD',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FALSO',
FIND: 'PROCURAR',
FORMULATEXT: 'FÓRMULA.TEXTO',
FV: 'VF',
IF: 'SE',
IFERROR: 'SEERRO',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'ЛОЖЬ',
FIND: 'НАЙТИ',
FORMULATEXT: 'Ф.ТЕКСТ',
FV: 'БС',
IF: 'ЕСЛИ',
IFERROR: 'ЕСЛИОШИБКА',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'EXP',
FALSE: 'FALSKT',
FIND: 'HITTA',
FORMULATEXT: 'FORMELTEXT',
FV: 'SLUTVÄRDE',
IF: 'OM',
IFERROR: 'OMFEL',
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 @@ -69,6 +69,7 @@ const dictionary: RawTranslationPackage = {
EXP: 'ÜS',
FALSE: 'YANLIŞ',
FIND: 'BUL',
FORMULATEXT: 'FORMÜLMETNİ',
FV: 'GD',
IF: 'EĞER',
IFERROR: 'EĞERHATA',
Expand Down
2 changes: 2 additions & 0 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {concatenate} from './text'
import {NumberLiteralHelper} from '../NumberLiteralHelper'
import {FunctionRegistry} from './FunctionRegistry'
import {NamedExpressions} from '../NamedExpressions'
import {Serialization} from '../Serialization'

export class Interpreter {
private gpu?: GPU.GPU
Expand All @@ -44,6 +45,7 @@ export class Interpreter {
public readonly numberLiteralsHelper: NumberLiteralHelper,
public readonly functionRegistry: FunctionRegistry,
public readonly namedExpressions: NamedExpressions,
public readonly serialization: Serialization
) {
this.functionRegistry.initializePlugins(this)
this.arithmeticHelper = new ArithmeticHelper(config, dateHelper, numberLiteralsHelper)
Expand Down
51 changes: 51 additions & 0 deletions src/interpreter/plugin/FormulaTextPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* Copyright (c) 2020 Handsoncode. All rights reserved.
*/

import {AstNodeType, ProcedureAst} from '../../parser'
import {CellError, ErrorType, InternalScalarValue, SimpleCellAddress} from '../../Cell'
import {FunctionPlugin} from '../index'
import {AbsoluteCellRange} from '../../AbsoluteCellRange'
import {Maybe} from '../../Maybe'

export class FormulaTextPlugin extends FunctionPlugin {
public static implementedFunctions = {
'FORMULATEXT': {
method: 'formulatext',
doesNotNeedArgumentsToBeComputed: true,
isDependentOnSheetStructureChange: true
},
}

public formulatext(ast: ProcedureAst, formulaAddress: SimpleCellAddress): InternalScalarValue {
if (ast.args.length !== 1) {
return new CellError(ErrorType.NA)
}

const arg = ast.args[0]

let cellReference: Maybe<SimpleCellAddress>

if (arg.type === AstNodeType.CELL_REFERENCE) {
cellReference = arg.reference.toSimpleCellAddress(formulaAddress)
} else if (arg.type === AstNodeType.CELL_RANGE || arg.type === AstNodeType.COLUMN_RANGE || arg.type === AstNodeType.ROW_RANGE) {
try {
cellReference = AbsoluteCellRange.fromAst(arg, formulaAddress).start
} catch (e) {
return new CellError(ErrorType.REF)
}
}

if (cellReference === undefined) {
const value = this.evaluateAst(arg, formulaAddress)
if (value instanceof CellError) {
return value
}
return new CellError(ErrorType.NA)
}

const formula = this.serialization.getCellFormula(cellReference)
return formula || new CellError(ErrorType.NA)
}
}
4 changes: 3 additions & 1 deletion src/interpreter/plugin/FunctionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {Ast, AstNodeType, ProcedureAst} from '../../parser'
import {coerceScalarToString} from '../ArithmeticHelper'
import {Interpreter} from '../Interpreter'
import {InterpreterValue, SimpleRangeValue} from '../InterpreterValue'
import {Maybe} from '../../Maybe'
import {Serialization} from '../../Serialization'

export interface ImplementedFunctions {
[formulaId: string]: FunctionMetadata,
Expand Down Expand Up @@ -52,12 +52,14 @@ export abstract class FunctionPlugin {
protected readonly dependencyGraph: DependencyGraph
protected readonly columnSearch: ColumnSearchStrategy
protected readonly config: Config
protected readonly serialization: Serialization

constructor(interpreter: Interpreter) {
this.interpreter = interpreter
this.dependencyGraph = interpreter.dependencyGraph
this.columnSearch = interpreter.columnSearch
this.config = interpreter.config
this.serialization = interpreter.serialization
}

protected evaluateAst(ast: Ast, formulaAddress: SimpleCellAddress): InterpreterValue {
Expand Down
1 change: 1 addition & 0 deletions src/interpreter/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export {CharPlugin} from './CharPlugin'
export {CodePlugin} from './CodePlugin'
export {ErrorFunctionPlugin} from './ErrorFunctionPlugin'
export {CorrelPlugin} from './CorrelPlugin'
export {FormulaTextPlugin} from './FormulaTextPlugin'
104 changes: 104 additions & 0 deletions test/interpreter/function-formulatext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {ErrorType, HyperFormula} from '../../src'
import {adr, detailedError} from '../testUtils'

describe('Function FORMULATEXT', () => {
it('should return N/A when number of arguments is incorrect', () => {
const engine = HyperFormula.buildFromArray([
['=FORMULATEXT()'],
['=FORMULATEXT(B2, B3)']
])

expect(engine.getCellValue(adr('A1'))).toEqual(detailedError(ErrorType.NA))
expect(engine.getCellValue(adr('A2'))).toEqual(detailedError(ErrorType.NA))
})

it('should return N/A for wrong types of arguments', () => {
const engine = HyperFormula.buildFromArray([
['=FORMULATEXT(1)'],
['=FORMULATEXT("foo")'],
['=FORMULATEXT(SUM(1))'],
])

expect(engine.getCellValue(adr('A1'))).toEqual(detailedError(ErrorType.NA))
expect(engine.getCellValue(adr('A2'))).toEqual(detailedError(ErrorType.NA))
expect(engine.getCellValue(adr('A3'))).toEqual(detailedError(ErrorType.NA))
})

it('should propagate expression error', () => {
const engine = HyperFormula.buildFromArray([
['=FORMULATEXT(1/0)']
])

expect(engine.getCellValue(adr('A1'))).toEqual(detailedError(ErrorType.DIV_BY_ZERO))
})

it('should return text of a formula evaluating to error', () => {
const engine = HyperFormula.buildFromArray([
['=1/0', '=FORMULATEXT(A1)']
])

expect(engine.getCellValue(adr('B1'))).toEqual('=1/0')
})

it('should work', () => {
const engine = HyperFormula.buildFromArray([
['=SUM(1, 2)', '=FORMULATEXT(A1)']
])

expect(engine.getCellValue(adr('B1'))).toEqual('=SUM(1, 2)')
})

it('should return formula of a left corner cell', () => {
const engine = HyperFormula.buildFromArray([
['=SUM(1, 2)', '=FORMULATEXT(A1:A2)']
])

expect(engine.getCellValue(adr('B1'))).toEqual('=SUM(1, 2)')
})

it('should return REF when ', () => {
const engine = HyperFormula.buildFromArray([
['=SUM(1, 2)']
])
engine.addSheet('Sheet2')
engine.setCellContents(adr('B1'), '=FORMULATEXT(Sheet1!A1:Sheet2!A2)')

expect(engine.getCellValue(adr('B1'))).toEqual(detailedError(ErrorType.REF))
})

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)']
])

expect(engine.getCellValue(adr('B1'))).toEqual('=SUM(1,')
})

it('should return itself', () => {
const engine = HyperFormula.buildFromArray([
['=FORMULATEXT(A1)']
])

expect(engine.getCellValue(adr('A1'))).toEqual('=FORMULATEXT(A1)')
})

it('should be dependent on sheet structure changes', () => {
const engine = HyperFormula.buildFromArray([
['=SUM(A2)', '=FORMULATEXT(A1)'],
[1]
])

engine.addRows(0, [1, 1])

expect(engine.getCellFormula(adr('A1'))).toEqual('=SUM(A3)')
expect(engine.getCellValue(adr('B1'))).toEqual('=SUM(A3)')
})
})