Skip to content

Commit

Permalink
Add initial version of calculateFormula()
Browse files Browse the repository at this point in the history
Issue: #24
  • Loading branch information
swistak35 committed Feb 12, 2020
1 parent 39d28be commit 4771cca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/HyperFormula.ts
@@ -1,5 +1,6 @@
import {AbsoluteCellRange} from './AbsoluteCellRange'
import {BuildEngineFromArraysFactory} from './BuildEngineFromArraysFactory'
import {absolutizeDependencies} from './absolutizeDependencies'
import {
CellType,
CellValueType,
Expand All @@ -9,7 +10,7 @@ import {
simpleCellAddress,
SimpleCellAddress,
} from './Cell'
import {CellContentParser, isMatrix, RawCellContent} from './CellContentParser'
import {CellContent, CellContentParser, isMatrix, RawCellContent} from './CellContentParser'
import {CellValue, CellValueExporter} from './CellValue'
import {IColumnSearchStrategy} from './ColumnSearch/ColumnSearchStrategy'
import {Config} from './Config'
Expand Down Expand Up @@ -119,6 +120,7 @@ export class HyperFormula {

private readonly crudOperations: CrudOperations
private readonly cellValueExporter: CellValueExporter
private nextExternalFormulaId: number = 0

constructor(
/** Engine config */
Expand All @@ -140,6 +142,7 @@ export class HyperFormula {
) {
this.crudOperations = new CrudOperations(config, stats, dependencyGraph, columnSearch, parser, cellContentParser, lazilyTransformingAstService)
this.cellValueExporter = new CellValueExporter(config)
this.addressMapping.autoAddSheet(-1, [])
}

/**
Expand Down Expand Up @@ -820,7 +823,7 @@ export class HyperFormula {
* Run multiple operations and recompute formulas at the end
*
* @param batchOperations
* */
*/
public batch(batchOperations: (e: IBatchExecutor) => void): CellValueChange[] {
try {
batchOperations(this.crudOperations)
Expand All @@ -830,6 +833,23 @@ export class HyperFormula {
return this.recomputeIfDependencyGraphNeedsIt().exportChanges(this.cellValueExporter)
}

/**
* Calculate formula
*
* @param batchOperations
*/
public calculateFormula(formulaString: string): [SimpleCellAddress, CellValueChange[]] {
const parsedCellContent = this.cellContentParser.parse(formulaString)
if (!(parsedCellContent instanceof CellContent.Formula)) {
throw new Error("This is not a formula")
}
const address = { sheet: -1, col: 0, row: this.nextExternalFormulaId }
const {ast, hash, hasVolatileFunction, hasStructuralChangeFunction, dependencies} = this.parser.parse(parsedCellContent.formula, address)
this.dependencyGraph.setFormulaToCell(address, ast, absolutizeDependencies(dependencies, address), hasVolatileFunction, hasStructuralChangeFunction)
this.nextExternalFormulaId++
return [address, this.recomputeIfDependencyGraphNeedsIt().getRoundedChanges(this.config)]
}

/**
* Destroys instance of HyperFormula
* */
Expand Down
16 changes: 16 additions & 0 deletions test/external-formulas.spec.ts
@@ -0,0 +1,16 @@
import {HyperFormula} from '../src'
import {simpleCellAddress} from '../src/Cell'
import './testConfig'
import {adr} from './testUtils'

describe("External formulas", () => {
it('basic usage', () => {
const engine = HyperFormula.buildFromArray([
['42'],
])

const [externalFormulaAddress, _changes] = engine.calculateFormula('=Sheet1!A1+10')

expect(engine.getCellValue(externalFormulaAddress)).toEqual(52)
})
})

0 comments on commit 4771cca

Please sign in to comment.