fix(excel):修复Excel文件解析时识别器不能保存列索引和无法对字符串指针赋值的问题。

This commit is contained in:
徐涛 2022-08-17 14:46:50 +08:00
parent 1e2a7fbb75
commit 81533fcdf3
2 changed files with 8 additions and 8 deletions

View File

@ -21,7 +21,7 @@ type ColumnRecognizer struct {
type ExcelAnalyzer[T any] struct { type ExcelAnalyzer[T any] struct {
File *excelize.File File *excelize.File
ActivedSheet string ActivedSheet string
Regconizers []ColumnRecognizer Regconizers []*ColumnRecognizer
} }
type ExcelAnalysisError struct { type ExcelAnalysisError struct {
@ -47,7 +47,7 @@ func (r *ColumnRecognizer) Recognize(cellValue string) bool {
return false return false
} }
func NewExcelAnalyzer[T any](file io.Reader, recognizers []ColumnRecognizer) (*ExcelAnalyzer[T], error) { func NewExcelAnalyzer[T any](file io.Reader, recognizers []*ColumnRecognizer) (*ExcelAnalyzer[T], error) {
excelFile, err := excelize.OpenReader(file) excelFile, err := excelize.OpenReader(file)
if err != nil { if err != nil {
return nil, err return nil, err
@ -60,7 +60,7 @@ func NewExcelAnalyzer[T any](file io.Reader, recognizers []ColumnRecognizer) (*E
}, nil }, nil
} }
func (a *ExcelAnalyzer[T]) AddRecognizer(recognizer ColumnRecognizer) { func (a *ExcelAnalyzer[T]) AddRecognizer(recognizer *ColumnRecognizer) {
a.Regconizers = append(a.Regconizers, recognizer) a.Regconizers = append(a.Regconizers, recognizer)
} }
@ -85,7 +85,7 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) {
} }
elementType := reflect.TypeOf(bean) elementType := reflect.TypeOf(bean)
collections := reflect.MakeSlice(elementType, 0, 0) collections := reflect.MakeSlice(reflect.SliceOf(elementType), 0, 0)
for rowIndex, cols := range rows { for rowIndex, cols := range rows {
// 标题行,需要完成识别动作 // 标题行,需要完成识别动作
if rowIndex == 0 { if rowIndex == 0 {
@ -100,14 +100,14 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) {
if alias, ok := field.Tag.Lookup("excel"); ok { if alias, ok := field.Tag.Lookup("excel"); ok {
for _, recognizer := range a.Regconizers { for _, recognizer := range a.Regconizers {
if alias == recognizer.Tag && recognizer.MatchIndex != -1 { if alias == recognizer.Tag && recognizer.MatchIndex != -1 {
actualField := instance.FieldByName(field.Name) actualField := instance.Elem().FieldByName(field.Name)
matchValue := cols[recognizer.MatchIndex] matchValue := cols[recognizer.MatchIndex]
switch field.Type.String() { switch field.Type.String() {
case "string": case "string":
actualField.Set(reflect.ValueOf(matchValue)) actualField.Set(reflect.ValueOf(matchValue))
case "*string": case "*string":
if len(matchValue) > 0 { if len(matchValue) > 0 {
actualField.Elem().Set(reflect.ValueOf(&matchValue)) actualField.Set(reflect.ValueOf(&matchValue))
} }
case "decimal.Decimal": case "decimal.Decimal":
decimalValue, err := decimal.NewFromString(matchValue) decimalValue, err := decimal.NewFromString(matchValue)

View File

@ -5,7 +5,7 @@ import (
"io" "io"
) )
var meter04kVExcelRecognizers = []ColumnRecognizer{ var meter04kVExcelRecognizers = []*ColumnRecognizer{
{Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1}, {Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1},
{Pattern: []string{"户名"}, Tag: "name", MatchIndex: -1}, {Pattern: []string{"户名"}, Tag: "name", MatchIndex: -1},
{Pattern: []string{"户址"}, Tag: "address", MatchIndex: -1}, {Pattern: []string{"户址"}, Tag: "address", MatchIndex: -1},
@ -13,7 +13,7 @@ var meter04kVExcelRecognizers = []ColumnRecognizer{
{Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1}, {Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1},
{Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1}, {Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1},
{Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1}, {Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1},
{Pattern: []string{"公共", "公共设备"}, Tag: "public", MatchIndex: -1}, {Pattern: []string{"公用", "公用设备"}, Tag: "public", MatchIndex: -1},
{Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1}, {Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1},
} }