unioffice/spreadsheet/context.go
Todd 17a365ee93 spreadsheet: start adding support for formula evaluation
We can take advantage of cached formula results that Excel/LibreOffice
insert to ensure that our formula results match the expected results.
2017-09-14 22:17:42 -05:00

49 lines
1.3 KiB
Go

// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package spreadsheet
import "baliance.com/gooxml/spreadsheet/formula"
func newEvalContext(s *Sheet) *evalContext {
return &evalContext{s, make(map[string]struct{})}
}
type evalContext struct {
s *Sheet
evaluating map[string]struct{}
}
func (e *evalContext) Cell(ref string, ev formula.Evaluator) formula.Result {
c := e.s.Cell(ref)
// if we have a formula, evaluate it
if c.HasFormula() {
if _, ok := e.evaluating[ref]; ok {
// recursively evaluating, so bail out
return formula.MakeErrorResult("recursion detected during evaluation of " + ref)
}
e.evaluating[ref] = struct{}{}
res := ev.Eval(e, c.GetFormula())
delete(e.evaluating, ref)
return res
}
if c.IsNumber() {
v, _ := c.GetValueAsNumber()
return formula.MakeNumberResult(v)
} else if c.IsBool() {
v, _ := c.GetValueAsBool()
return formula.MakeBoolResult(v)
} else {
v, _ := c.GetValue()
return formula.MakeStringResult(v)
}
// TODO: handle this properly
// return formula.MakeErrorResult()
}