-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixhorizontalrange.go
65 lines (56 loc) · 2.09 KB
/
prefixhorizontalrange.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package formula
import (
"fmt"
"strconv"
"strings"
"go.devnw.com/ooxml/spreadsheet/update"
)
// PrefixHorizontalRange is a range expression that when evaluated returns a list of Results from references like Sheet1!1:4 (all cells from rows 1 to 4 of sheet 'Sheet1').
type PrefixHorizontalRange struct {
pfx Expression
rowFrom, rowTo int
}
// NewPrefixHorizontalRange constructs a new full rows range with prefix.
func NewPrefixHorizontalRange(pfx Expression, v string) Expression {
sl := strings.Split(v, ":")
if len(sl) != 2 {
return nil
}
from, _ := strconv.Atoi(sl[0])
to, _ := strconv.Atoi(sl[1])
return PrefixHorizontalRange{pfx, from, to}
}
// Eval evaluates a horizontal range with prefix returning a list of results or an error.
func (r PrefixHorizontalRange) Eval(ctx Context, ev Evaluator) Result {
pfx := r.pfx.Reference(ctx, ev)
switch pfx.Type {
case ReferenceTypeSheet:
ref := r.horizontalRangeReference(pfx.Value)
if cached, found := ev.GetFromCache(ref); found {
return cached
}
c := ctx.Sheet(pfx.Value)
from, to := cellRefsFromHorizontalRange(c, r.rowFrom, r.rowTo)
result := resultFromCellRange(c, ev, from, to)
ev.SetCache(ref, result)
return result
default:
return MakeErrorResult(fmt.Sprintf("no support for reference type %s", pfx.Type))
}
}
func (r PrefixHorizontalRange) horizontalRangeReference(sheetName string) string {
return fmt.Sprintf("%s!%d:%d", sheetName, r.rowFrom, r.rowTo)
}
// Reference returns a string reference value to a horizontal range with prefix.
func (r PrefixHorizontalRange) Reference(ctx Context, ev Evaluator) Reference {
pfx := r.pfx.Reference(ctx, ev)
return Reference{Type: ReferenceTypeHorizontalRange, Value: r.horizontalRangeReference(pfx.Value)}
}
// String returns a string representation of a horizontal range with prefix.
func (r PrefixHorizontalRange) String() string {
return fmt.Sprintf("%s!%d:%d", r.pfx.String(), r.rowFrom, r.rowTo)
}
// Update updates references in the PrefixHorizontalRange after removing a row/column.
func (r PrefixHorizontalRange) Update(q *update.UpdateQuery) Expression {
return r
}