2019-07-25 19:43:46 +03:00
|
|
|
// Copyright 2017 FoxyUtils ehf. All rights reserved.
|
2017-10-01 11:32:03 -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-10-01 11:32:03 -05:00
|
|
|
|
|
|
|
package reference
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-02-11 22:47:08 +03:00
|
|
|
|
|
|
|
"github.com/unidoc/unioffice/spreadsheet/update"
|
2017-10-01 11:32:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// CellReference is a parsed reference to a cell. Input is of the form 'A1',
|
|
|
|
// '$C$2', etc.
|
|
|
|
type CellReference struct {
|
|
|
|
RowIdx uint32
|
|
|
|
ColumnIdx uint32
|
|
|
|
Column string
|
|
|
|
AbsoluteColumn bool
|
|
|
|
AbsoluteRow bool
|
2020-01-29 13:43:43 +03:00
|
|
|
SheetName string
|
2017-10-01 11:32:03 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 22:47:08 +03:00
|
|
|
// String returns a string representation of CellReference.
|
2017-10-01 19:16:11 -05:00
|
|
|
func (c CellReference) String() string {
|
|
|
|
buf := make([]byte, 0, 4)
|
|
|
|
if c.AbsoluteColumn {
|
|
|
|
buf = append(buf, '$')
|
|
|
|
}
|
|
|
|
buf = append(buf, c.Column...)
|
|
|
|
if c.AbsoluteRow {
|
|
|
|
buf = append(buf, '$')
|
|
|
|
}
|
|
|
|
buf = strconv.AppendInt(buf, int64(c.RowIdx), 10)
|
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
2017-10-01 11:32:03 -05:00
|
|
|
// ParseCellReference parses a cell reference of the form 'A10' and splits it
|
|
|
|
// into column/row segments.
|
|
|
|
func ParseCellReference(s string) (CellReference, error) {
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if len(s) < 2 {
|
|
|
|
return CellReference{}, errors.New("cell reference must have at least two characters")
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:43:43 +03:00
|
|
|
r := CellReference{}
|
|
|
|
sl := strings.Split(s, "!")
|
|
|
|
if len(sl) == 2 {
|
|
|
|
r.SheetName = sl[0]
|
|
|
|
s = sl[1]
|
|
|
|
}
|
2017-10-01 11:32:03 -05:00
|
|
|
// check for absolute column
|
|
|
|
if s[0] == '$' {
|
|
|
|
r.AbsoluteColumn = true
|
|
|
|
s = s[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
split := -1
|
|
|
|
lfor:
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
switch {
|
|
|
|
case s[i] >= '0' && s[i] <= '9' || s[i] == '$':
|
|
|
|
split = i
|
|
|
|
break lfor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch split {
|
|
|
|
case 0:
|
|
|
|
return CellReference{}, fmt.Errorf("no letter prefix in %s", s)
|
|
|
|
case -1:
|
|
|
|
return CellReference{}, fmt.Errorf("no digits in %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Column = s[0:split]
|
|
|
|
|
|
|
|
if s[split] == '$' {
|
|
|
|
r.AbsoluteRow = true
|
|
|
|
split++
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ColumnIdx = ColumnToIndex(r.Column)
|
|
|
|
r64, err := strconv.ParseUint(s[split:], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return CellReference{}, fmt.Errorf("error parsing row: %s", err)
|
|
|
|
}
|
|
|
|
r.RowIdx = uint32(r64)
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
2020-02-11 22:47:08 +03:00
|
|
|
|
|
|
|
// Update updates reference to point one of the neighboring cells with respect to the update type after removing a row/column.
|
|
|
|
func (ref *CellReference) Update(updateType update.UpdateAction) *CellReference {
|
|
|
|
switch updateType {
|
|
|
|
case update.UpdateActionRemoveColumn:
|
|
|
|
newRef := ref
|
|
|
|
newRef.ColumnIdx = ref.ColumnIdx - 1
|
|
|
|
newRef.Column = IndexToColumn(newRef.ColumnIdx)
|
|
|
|
return newRef
|
|
|
|
default:
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
}
|