2019-07-25 19:43:46 +03:00
|
|
|
// Copyright 2017 FoxyUtils ehf. All rights reserved.
|
2017-09-14 18:59:07 -05:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2017-09-16 10:25:01 -05:00
|
|
|
import (
|
2017-10-01 19:16:11 -05:00
|
|
|
"fmt"
|
|
|
|
|
2019-05-04 11:18:06 +03:00
|
|
|
"github.com/unidoc/unioffice/spreadsheet/formula"
|
|
|
|
"github.com/unidoc/unioffice/spreadsheet/reference"
|
2017-09-16 10:25:01 -05:00
|
|
|
)
|
2017-09-14 18:59:07 -05:00
|
|
|
|
|
|
|
func newEvalContext(s *Sheet) *evalContext {
|
2017-10-01 19:16:11 -05:00
|
|
|
return &evalContext{s: s, evaluating: make(map[string]struct{})}
|
2017-09-14 18:59:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type evalContext struct {
|
2017-10-01 19:16:11 -05:00
|
|
|
s *Sheet
|
|
|
|
colOff, rowOff uint32
|
|
|
|
evaluating map[string]struct{}
|
2017-09-14 18:59:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *evalContext) Cell(ref string, ev formula.Evaluator) formula.Result {
|
2017-10-01 19:16:11 -05:00
|
|
|
cr, err := reference.ParseCellReference(ref)
|
|
|
|
if err != nil {
|
|
|
|
return formula.MakeErrorResult(fmt.Sprintf("error parsing %s: %s", ref, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// offsets are used in shared formulas so that references like 'A1', '$A1',
|
|
|
|
// 'A$1', '$A$1' will behave differently according to the offset
|
|
|
|
if e.colOff != 0 && !cr.AbsoluteColumn {
|
|
|
|
cr.ColumnIdx += e.colOff
|
|
|
|
cr.Column = reference.IndexToColumn(cr.ColumnIdx)
|
|
|
|
}
|
|
|
|
if e.rowOff != 0 && !cr.AbsoluteRow {
|
|
|
|
cr.RowIdx += e.rowOff
|
|
|
|
}
|
|
|
|
|
|
|
|
c := e.s.Cell(cr.String())
|
2017-09-14 18:59:07 -05:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-09-17 19:23:49 -05:00
|
|
|
if c.IsEmpty() {
|
2017-09-20 18:21:22 -05:00
|
|
|
return formula.MakeEmptyResult()
|
2017-09-17 19:23:49 -05:00
|
|
|
} else if c.IsNumber() {
|
2017-09-14 18:59:07 -05:00
|
|
|
v, _ := c.GetValueAsNumber()
|
|
|
|
return formula.MakeNumberResult(v)
|
|
|
|
} else if c.IsBool() {
|
|
|
|
v, _ := c.GetValueAsBool()
|
|
|
|
return formula.MakeBoolResult(v)
|
|
|
|
}
|
2017-09-17 19:23:49 -05:00
|
|
|
|
|
|
|
v, _ := c.GetRawValue()
|
|
|
|
return formula.MakeStringResult(v)
|
2017-09-14 18:59:07 -05:00
|
|
|
}
|
2017-09-16 10:25:01 -05:00
|
|
|
|
|
|
|
func (e *evalContext) Sheet(name string) formula.Context {
|
|
|
|
for _, sheet := range e.s.w.Sheets() {
|
|
|
|
if sheet.Name() == name {
|
|
|
|
return sheet.FormulaContext()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formula.InvalidReferenceContext
|
|
|
|
}
|
2017-09-17 12:59:57 -05:00
|
|
|
|
|
|
|
func (e *evalContext) NamedRange(ref string) formula.Reference {
|
|
|
|
for _, dn := range e.s.w.DefinedNames() {
|
|
|
|
if dn.Name() == ref {
|
|
|
|
return formula.MakeRangeReference(dn.Content())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tbl := range e.s.w.Tables() {
|
|
|
|
if tbl.Name() == ref {
|
|
|
|
return formula.MakeRangeReference(tbl.Reference())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return formula.ReferenceInvalid
|
|
|
|
}
|
2017-10-01 19:16:11 -05:00
|
|
|
|
|
|
|
func (e *evalContext) SetOffset(col, row uint32) {
|
|
|
|
e.colOff = col
|
|
|
|
e.rowOff = row
|
|
|
|
}
|
2019-11-05 19:36:09 +03:00
|
|
|
|
|
|
|
// GetFilename returns the filename of the context's workbook.
|
|
|
|
func (e *evalContext) GetFilename() string {
|
|
|
|
return e.s.w.GetFilename()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFormat returns a cell data format.
|
|
|
|
func (e *evalContext) GetFormat(cellRef string) string {
|
|
|
|
return e.s.Cell(cellRef).getFormat()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabelPrefix returns label prefix which depends on the cell's horizontal alignment.
|
|
|
|
func (e *evalContext) GetLabelPrefix(cellRef string) string {
|
|
|
|
return e.s.Cell(cellRef).getLabelPrefix()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLocked returns if the cell is locked.
|
|
|
|
func (e *evalContext) GetLocked(cellRef string) bool {
|
|
|
|
return e.s.Cell(cellRef).getLocked()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLocked sets cell locked or not.
|
|
|
|
func (e *evalContext) SetLocked(cellRef string, locked bool) {
|
|
|
|
e.s.Cell(cellRef).setLocked(locked)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWidth returns a worksheet's column width.
|
|
|
|
func (e *evalContext) GetWidth(colIdx int) float64 {
|
|
|
|
colIdx++
|
|
|
|
for _, c := range e.s.X().Cols[0].Col {
|
|
|
|
if int(c.MinAttr) <= colIdx && colIdx <= int(c.MaxAttr) {
|
|
|
|
return float64(int(*c.WidthAttr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|