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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added 18 mathematical functions ROMAN, ARABIC, FACT, FACTDOUBLE, COMBIN, COMBINA, GCD, LCM, MROUND, MULTINOMIAL, QUOTIENT, RANDBETWEEN, SERIESSUM, SIGN, SQRTPI, SUMX2MY2, SUMX2PY2, SUMXMY2.

### Fixed
- Fixed a problem with dependencies not collected for specific functions. (#550, #549)

## [0.3.0] - 2020-10-22

### Added
Expand Down
38 changes: 21 additions & 17 deletions src/parser/collectDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,47 @@ import {
import {FunctionRegistry} from '../interpreter/FunctionRegistry'


const collectDependenciesFn = (ast: Ast, functionRegistry: FunctionRegistry, dependenciesSet: RelativeDependency[]) => {
const collectDependenciesFn = (ast: Ast, functionRegistry: FunctionRegistry, dependenciesSet: RelativeDependency[], needArgument: boolean) => {
switch (ast.type) {
case AstNodeType.EMPTY:
case AstNodeType.NUMBER:
case AstNodeType.STRING:
case AstNodeType.ERROR:
return
case AstNodeType.NAMED_EXPRESSION: {
dependenciesSet.push(new NamedExpressionDependency(ast.expressionName))
if (needArgument) {
dependenciesSet.push(new NamedExpressionDependency(ast.expressionName))
}
return
}
case AstNodeType.CELL_REFERENCE: {
dependenciesSet.push(new AddressDependency(ast.reference))
if (needArgument) {
dependenciesSet.push(new AddressDependency(ast.reference))
}
return
}
case AstNodeType.CELL_RANGE: {
if (ast.start.sheet === ast.end.sheet) {
if (needArgument && ast.start.sheet === ast.end.sheet) {
dependenciesSet.push(new CellRangeDependency(ast.start, ast.end))
}
return
}
case AstNodeType.COLUMN_RANGE: {
if (ast.start.sheet === ast.end.sheet) {
if (needArgument && ast.start.sheet === ast.end.sheet) {
dependenciesSet.push(new ColumnRangeDependency(ast.start, ast.end))
}
return
}
case AstNodeType.ROW_RANGE: {
if (ast.start.sheet === ast.end.sheet) {
if (needArgument && ast.start.sheet === ast.end.sheet) {
dependenciesSet.push(new RowRangeDependency(ast.start, ast.end))
}
return
}
case AstNodeType.PERCENT_OP:
case AstNodeType.PLUS_UNARY_OP:
case AstNodeType.MINUS_UNARY_OP: {
collectDependenciesFn(ast.value, functionRegistry, dependenciesSet)
collectDependenciesFn(ast.value, functionRegistry, dependenciesSet, true)
return
}
case AstNodeType.CONCATENATE_OP:
Expand All @@ -67,24 +71,24 @@ const collectDependenciesFn = (ast: Ast, functionRegistry: FunctionRegistry, dep
case AstNodeType.TIMES_OP:
case AstNodeType.DIV_OP:
case AstNodeType.POWER_OP:
collectDependenciesFn(ast.left, functionRegistry, dependenciesSet)
collectDependenciesFn(ast.right, functionRegistry, dependenciesSet)
collectDependenciesFn(ast.left, functionRegistry, dependenciesSet, true)
collectDependenciesFn(ast.right, functionRegistry, dependenciesSet, true)
return
case AstNodeType.PARENTHESIS:
collectDependenciesFn(ast.expression, functionRegistry, dependenciesSet)
collectDependenciesFn(ast.expression, functionRegistry, dependenciesSet, false)
return
case AstNodeType.FUNCTION_CALL:
if (!functionRegistry.doesFunctionNeedArgumentToBeComputed(ast.procedureName)) {
ast.args.forEach((argAst: Ast) =>
collectDependenciesFn(argAst, functionRegistry, dependenciesSet)
)
}
case AstNodeType.FUNCTION_CALL: {
const functionNeedArgument = !functionRegistry.doesFunctionNeedArgumentToBeComputed(ast.procedureName)
ast.args.forEach((argAst: Ast) =>
collectDependenciesFn(argAst, functionRegistry, dependenciesSet, functionNeedArgument)
)
return
}
}
}

export const collectDependencies = (ast: Ast, functionRegistry: FunctionRegistry) => {
const result = new Array<RelativeDependency>()
collectDependenciesFn(ast, functionRegistry, result)
collectDependenciesFn(ast, functionRegistry, result, true)
return result
}
30 changes: 30 additions & 0 deletions test/interpreter/function-column.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,34 @@ describe('Function COLUMN', () => {

expect(engine.getCellValue(adr('B2'))).toEqual(2)
})

it('should collect dependencies of inner function and return argument type error', () => {
const engine = HyperFormula.buildFromArray([
['=SIN(1)'],
['=COLUMN(SUM(A1,A3))'],
['=SIN(1)'],
])

expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.CellRefExpected))
})

it('should propagate error of inner function', () => {
const engine = HyperFormula.buildFromArray([
['=1/0'],
['=COLUMN(SUM(A1, A3))'],
['=1/0']
])

expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.DIV_BY_ZERO))
})

it('should return #CYCLE! when cyclic reference occurs not directly in COLUMN', () => {
const engine = HyperFormula.buildFromArray([
['=COLUMN(SUM(A1))'],
['=COLUMN(A1+A2)'],
])

expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.CYCLE))
expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.CYCLE))
})
})
30 changes: 30 additions & 0 deletions test/interpreter/function-isformula.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,34 @@ describe('Function ISFORMULA', () => {

expect(engine.getCellValue(adr('A1'))).toEqual(true)
})

it('should collect dependencies of inner function and return argument type error', () => {
const engine = HyperFormula.buildFromArray([
['=SIN(1)'],
['=ISFORMULA(SUM(A1,A3))'],
['=SIN(1)'],
])

expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.NA, ErrorMessage.CellRefExpected))
})

it('should propagate error of inner function', () => {
const engine = HyperFormula.buildFromArray([
['=1/0'],
['=ISFORMULA(SUM(A1, A3))'],
['=1/0']
])

expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.DIV_BY_ZERO))
})

it('should return #CYCLE! when cyclic reference occurs not directly in COLUMN', () => {
const engine = HyperFormula.buildFromArray([
['=ISFORMULA(SUM(A1))'],
['=ISFORMULA(A1+A2)'],
])

expect(engine.getCellValue(adr('A1'))).toEqualError(detailedError(ErrorType.CYCLE))
expect(engine.getCellValue(adr('A2'))).toEqualError(detailedError(ErrorType.CYCLE))
})
})