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
|
Functions2 (#348)
* MATCH, IFS, MAXA, MINA
* OFFSET fixed
* ISBLANK, ISERR, ISERROR, ISEVEN ,ISFORMULA, ISNONTEXT, ISNUMBER, ISODD, ISTEXT
* ISLEAPYEAR, ISLOGICAL, ISNA, ISREF
* FIND, FINDB
* SEARCH, SEARCHB
* CONCAT, CONCATENATE
* YEAR, YEARFRAC
* CONCAT is fixed, now TRUE and FALSE are concatenated instead of 1 and 0 in case of boolean results
* NOW, TODAY, TIME, TIMEVALUE
* DATE
* DATEDIF
2019-11-21 02:21:00 +03:00
|
|
|
// commercial license can be purchased on https://unidoc.io.
|
2017-09-14 18:59:07 -05:00
|
|
|
|
|
|
|
package formula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-01 11:32:03 -05:00
|
|
|
|
2019-05-04 11:18:06 +03:00
|
|
|
"github.com/unidoc/unioffice/spreadsheet/reference"
|
2020-02-11 22:47:08 +03:00
|
|
|
"github.com/unidoc/unioffice/spreadsheet/update"
|
2017-09-14 18:59:07 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Range is a range expression that when evaluated returns a list of Results.
|
|
|
|
type Range struct {
|
|
|
|
from, to Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRange constructs a new range.
|
|
|
|
func NewRange(from, to Expression) Expression {
|
|
|
|
return Range{from, to}
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:43:43 +03:00
|
|
|
// Eval evaluates a range returning a list of results or an error.
|
2017-09-14 18:59:07 -05:00
|
|
|
func (r Range) Eval(ctx Context, ev Evaluator) Result {
|
|
|
|
from := r.from.Reference(ctx, ev)
|
|
|
|
to := r.to.Reference(ctx, ev)
|
2020-01-29 13:43:43 +03:00
|
|
|
ref := rangeReference(from, to)
|
2017-09-14 18:59:07 -05:00
|
|
|
if from.Type == ReferenceTypeCell && to.Type == ReferenceTypeCell {
|
2020-01-29 13:43:43 +03:00
|
|
|
if cached, found := ev.GetFromCache(ref); found {
|
|
|
|
return cached
|
|
|
|
} else {
|
|
|
|
result := resultFromCellRange(ctx, ev, from.Value, to.Value)
|
|
|
|
ev.SetCache(ref, result)
|
|
|
|
return result
|
|
|
|
}
|
2017-09-14 18:59:07 -05:00
|
|
|
}
|
2020-01-29 13:43:43 +03:00
|
|
|
return MakeErrorResult("invalid range " + ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rangeReference(from, to Reference) string {
|
|
|
|
return fmt.Sprintf("%s:%s", from.Value, to.Value)
|
2017-09-14 18:59:07 -05:00
|
|
|
}
|
|
|
|
|
2020-01-29 13:43:43 +03:00
|
|
|
// Reference returns a string reference value to a range.
|
2017-09-14 18:59:07 -05:00
|
|
|
func (r Range) Reference(ctx Context, ev Evaluator) Reference {
|
2017-09-17 13:41:50 -05:00
|
|
|
from := r.from.Reference(ctx, ev)
|
|
|
|
to := r.to.Reference(ctx, ev)
|
|
|
|
if from.Type == ReferenceTypeCell && to.Type == ReferenceTypeCell {
|
2020-01-29 13:43:43 +03:00
|
|
|
return MakeRangeReference(rangeReference(from, to))
|
2017-09-17 13:41:50 -05:00
|
|
|
}
|
2017-09-14 18:59:07 -05:00
|
|
|
return ReferenceInvalid
|
|
|
|
}
|
|
|
|
|
2017-09-17 12:59:57 -05:00
|
|
|
func resultFromCellRange(ctx Context, ev Evaluator, from, to string) Result {
|
2020-01-29 13:43:43 +03:00
|
|
|
fromRef, fe := reference.ParseCellReference(from)
|
2017-09-17 12:59:57 -05:00
|
|
|
if fe != nil {
|
2020-01-29 13:43:43 +03:00
|
|
|
return MakeErrorResult(fmt.Sprintf("unable to parse range %s: error %s", from, fe.Error()))
|
2017-09-17 12:59:57 -05:00
|
|
|
}
|
2020-01-29 13:43:43 +03:00
|
|
|
fc, fr := fromRef.ColumnIdx, fromRef.RowIdx
|
|
|
|
|
|
|
|
toRef, te := reference.ParseCellReference(to)
|
2017-09-17 12:59:57 -05:00
|
|
|
if te != nil {
|
2020-01-29 13:43:43 +03:00
|
|
|
return MakeErrorResult(fmt.Sprintf("unable to parse range %s: error %s", to, te.Error()))
|
2017-09-17 12:59:57 -05:00
|
|
|
}
|
2020-01-29 13:43:43 +03:00
|
|
|
tc, tr := toRef.ColumnIdx, toRef.RowIdx
|
|
|
|
|
2017-09-17 12:59:57 -05:00
|
|
|
arr := [][]Result{}
|
|
|
|
for r := fr; r <= tr; r++ {
|
|
|
|
args := []Result{}
|
2020-01-29 13:43:43 +03:00
|
|
|
for c := fc; c <= tc; c++ {
|
2017-10-01 11:32:03 -05:00
|
|
|
res := ctx.Cell(fmt.Sprintf("%s%d", reference.IndexToColumn(c), r), ev)
|
2017-09-20 19:04:21 -05:00
|
|
|
args = append(args, res)
|
2017-09-17 12:59:57 -05:00
|
|
|
}
|
|
|
|
arr = append(arr, args)
|
|
|
|
}
|
|
|
|
// for a single row, just return a list
|
|
|
|
if len(arr) == 1 {
|
2017-09-17 13:41:50 -05:00
|
|
|
// single cell result
|
|
|
|
if len(arr[0]) == 1 {
|
|
|
|
return arr[0][0]
|
|
|
|
}
|
2017-09-17 12:59:57 -05:00
|
|
|
return MakeListResult(arr[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
return MakeArrayResult(arr)
|
|
|
|
}
|
2020-02-11 22:47:08 +03:00
|
|
|
|
|
|
|
// String returns a string of a range.
|
|
|
|
func (r Range) String() string {
|
|
|
|
return fmt.Sprintf("%s:%s", r.from.String(), r.to.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates references in the Range after removing a row/column.
|
|
|
|
func (r Range) Update(q *update.UpdateQuery) Expression {
|
|
|
|
new := r
|
|
|
|
if q.UpdateCurrentSheet {
|
|
|
|
new.from = r.from.Update(q)
|
|
|
|
new.to = r.to.Update(q)
|
|
|
|
}
|
|
|
|
return new
|
|
|
|
}
|