You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
1.8 KiB
88 lines
1.8 KiB
package excel
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
NotStruct = "this obj not a struct"
|
|
RowIsEmpty = "rows is empty"
|
|
)
|
|
|
|
func reflectRow(t reflect.Type, v reflect.Value, name string, move, hover, index int, o *Sheet) (int, int) {
|
|
for k := 0; k < t.NumField(); k++ {
|
|
if len(getTagExcel(v, k)) == 0 && v.Field(k).Type().Kind() == reflect.Struct {
|
|
aMove, bHover := reflectRow(v.Field(k).Type(), v.Field(k), name, move, hover, index, o)
|
|
move += aMove
|
|
hover += bHover
|
|
continue
|
|
}
|
|
|
|
if value, ok := o.authMap[o.title[move].Auth]; ok {
|
|
if value(o, o.title[move].Name) {
|
|
move += 1
|
|
continue
|
|
}
|
|
}
|
|
move += 1
|
|
hover += 1
|
|
|
|
axis := fmt.Sprintf("%s%d", genFlag(hover), index+2)
|
|
_ = o.File.SetCellValue(name, axis, v.Field(k).Interface())
|
|
}
|
|
return move, hover
|
|
}
|
|
|
|
func getTagExcel(v reflect.Value, index int) string {
|
|
fieldInfo := v.Type().Field(index)
|
|
tag := fieldInfo.Tag
|
|
return tag.Get("excel")
|
|
}
|
|
|
|
func tag2map(tag string) map[string]string {
|
|
data := strings.Split(tag, ";")
|
|
|
|
var l [][]string
|
|
for _, attr := range data {
|
|
style := strings.Split(attr, ":")
|
|
l = append(l, style)
|
|
}
|
|
return list2map(l)
|
|
}
|
|
|
|
func reflectStruct(v reflect.Value, title []TagAttr) []TagAttr {
|
|
result := title
|
|
for i := 0; i < v.NumField(); i++ {
|
|
tag := getTagExcel(v, i)
|
|
if len(tag) == 0 && v.Field(i).Type().Kind() == reflect.Struct {
|
|
var temp []TagAttr
|
|
r := reflectStruct(v.Field(i), temp)
|
|
result = append(result, r...)
|
|
continue
|
|
}
|
|
if len(tag) == 0 {
|
|
continue
|
|
}
|
|
s := TagAttr{}
|
|
s.initTag(tag2map(tag))
|
|
result = append(result, s)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (o *Sheet) reflectTitle() (err error, title []TagAttr) {
|
|
st := o.Rows[0]
|
|
t := reflect.TypeOf(st)
|
|
v := reflect.ValueOf(st)
|
|
|
|
if t.Kind() == reflect.Ptr {
|
|
t = reflect.TypeOf(st).Elem()
|
|
v = reflect.ValueOf(st).Elem()
|
|
}
|
|
|
|
title = reflectStruct(v, title)
|
|
return
|
|
}
|