1 Star 0 Fork 0

Erdian718/xlsx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
internal.go 3.97 KB
一键复制 编辑 原始数据 按行查看 历史
Erdian718 提交于 2个月前 . Fix getter with non-struct
package internal
import (
"encoding/xml"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
const (
xmlns = "xmlns"
xmlnsMain = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"
)
// Errors.
var (
ErrUnknownCharset = errors.New("xlsx: unknown charset")
ErrInvalidFile = errors.New("xlsx: invalid file")
ErrInvalidTemplate = errors.New("xlsx: invalid template")
)
var typeTime = reflect.TypeOf(time.Time{})
// RowRef returns the row reference by index.
func RowRef(i int) string {
if i >= 0 {
return strconv.Itoa(i + 1)
}
return ""
}
// ColRef returns the column reference by index.
func ColRef(j int) string {
if j < 0 {
return ""
}
if j < 26 {
return string('A' + rune(j))
}
return ColRef(j/26-1) + ColRef(j%26)
}
// CellRef returns the cell reference by index.
func CellRef(i, j int) string {
return ColRef(j) + RowRef(i)
}
// RangeRef returns the range ref by index.
func RangeRef(ridx1, cidx1 int, ridx2, cidx2 int) string {
ref1 := CellRef(ridx1, cidx1)
ref2 := CellRef(ridx2, cidx2)
if ref2 == "" {
return ref1
}
return ref1 + ":" + ref2
}
// RowIdx returns the row index by reference.
func RowIdx(ref string) int {
if i, err := strconv.Atoi(ref); err == nil {
return i - 1
}
return -1
}
// ColIdx returns the column index by reference.
func ColIdx(ref string) int {
n := len(ref)
if n < 1 {
return -1
}
if n == 1 {
if c := ref[0]; c >= 'A' && c <= 'Z' {
return int(c - 'A')
} else if c >= 'a' && c <= 'z' {
return int(c - 'a')
}
return -1
}
a := ColIdx(ref[n-1:])
if a < 0 {
return a
}
b := ColIdx(ref[:n-1])
if b < 0 {
return b
}
return a + 26*b + 26
}
// CellIdx returns the cell index by reference.
func CellIdx(ref string) (ridx int, cidx int) {
for k := 0; k < len(ref); k++ {
if v := ref[k]; v >= '0' && v <= '9' {
return RowIdx(ref[k:]), ColIdx(ref[:k])
}
}
return -1, -1
}
// RangeIdx returns the range index by ref.
func RangeIdx(ref string) (ridx1, cidx1 int, ridx2, cidx2 int) {
if k := strings.IndexByte(ref, ':'); k >= 0 {
ridx1, cidx1 = CellIdx(ref[:k])
ridx2, cidx2 = CellIdx(ref[k+1:])
} else {
ridx1, cidx1 = CellIdx(ref)
ridx2, cidx2 = ridx1, cidx1
}
return
}
func normXMLSpace(attrs *[]xml.Attr) map[string]string {
ns := make(map[string]string)
as := (*attrs)[:0]
for _, attr := range *attrs {
if attr.Name.Space == "" && attr.Name.Local == xmlns {
ns[attr.Value] = ""
} else if attr.Name.Space == xmlns {
ns[attr.Value] = attr.Name.Local
attr.Name.Space = ""
attr.Name.Local = xmlns + ":" + attr.Name.Local
as = append(as, attr)
}
}
*attrs = as
return ns
}
func normXMLName(ns map[string]string, name *xml.Name) {
if space, ok := ns[name.Space]; ok {
name.Space = ""
if space != "" {
name.Local = space + ":" + name.Local
}
}
}
func isSubKeys(keys []string, ks []string) bool {
if len(ks) < len(keys) {
return false
}
for i, key := range keys {
if ks[i] != key {
return false
}
}
return true
}
func getValueByKey(data reflect.Value, key string) (reflect.Value, error) {
if data.Kind() == reflect.Pointer {
elem := data.Elem()
if elem.Kind() == reflect.Struct {
if v := elem.FieldByName(key); v.IsValid() {
return v, nil
}
}
if f := data.MethodByName(key); f.IsValid() {
return getValueByFunc(f)
}
if f := elem.MethodByName(key); f.IsValid() {
return getValueByFunc(f)
}
} else {
if data.Kind() == reflect.Struct {
if v := data.FieldByName(key); v.IsValid() {
return v, nil
}
}
if f := data.MethodByName(key); f.IsValid() {
return getValueByFunc(f)
}
if data.CanAddr() {
addr := data.Addr()
if f := addr.MethodByName(key); f.IsValid() {
return getValueByFunc(f)
}
}
}
return reflect.Value{}, fmt.Errorf("xlsx: field or method not found: %v", key)
}
func getValueByFunc(f reflect.Value) (reflect.Value, error) {
t := f.Type()
if t.NumIn() != 0 || t.NumOut() != 1 {
return reflect.Value{}, fmt.Errorf("xlsx: invalid getter: %v", t)
}
res := f.Call(nil)
return res[0], nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/erdian718/xlsx.git
git@gitee.com:erdian718/xlsx.git
erdian718
xlsx
xlsx
v0.4.0

搜索帮助