enhance(excel):调整Excel分析,以避免因为单元格没有填写导致的panic。
This commit is contained in:
parent
17bde54c7c
commit
e1d55e4fc7
|
@ -4,6 +4,7 @@ import (
|
|||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/tools"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
|
@ -25,6 +26,7 @@ type ColumnRecognizer struct {
|
|||
Pattern []string
|
||||
Tag string
|
||||
MatchIndex int
|
||||
MustFill bool
|
||||
}
|
||||
|
||||
type ExcelAnalyzer[T any] struct {
|
||||
|
@ -126,8 +128,18 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) {
|
|||
if alias, ok := field.Tag.Lookup("excel"); ok {
|
||||
for _, recognizer := range a.Regconizers {
|
||||
if alias == recognizer.Tag && recognizer.MatchIndex != -1 {
|
||||
var matchValue string
|
||||
actualField := instance.Elem().FieldByName(field.Name)
|
||||
matchValue := cols[recognizer.MatchIndex]
|
||||
if recognizer.MatchIndex > len(cols)-1 {
|
||||
if recognizer.MustFill {
|
||||
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: errors.New("单元格内不能没有内容。")}})
|
||||
continue
|
||||
} else {
|
||||
matchValue = ""
|
||||
}
|
||||
} else {
|
||||
matchValue = cols[recognizer.MatchIndex]
|
||||
}
|
||||
switch field.Type.String() {
|
||||
case "string":
|
||||
actualField.Set(reflect.ValueOf(matchValue))
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
|
||||
var (
|
||||
endUserNonPVRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1},
|
||||
{Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1},
|
||||
{Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1},
|
||||
{Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true},
|
||||
}
|
||||
endUserPVRecognizers = append(
|
||||
endUserNonPVRecognizers,
|
||||
|
|
|
@ -6,15 +6,15 @@ import (
|
|||
)
|
||||
|
||||
var meter04kVExcelRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1},
|
||||
{Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"户名"}, Tag: "name", MatchIndex: -1},
|
||||
{Pattern: []string{"户址"}, Tag: "address", MatchIndex: -1},
|
||||
{Pattern: []string{"联系人"}, Tag: "contact", MatchIndex: -1},
|
||||
{Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1},
|
||||
{Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1},
|
||||
{Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1},
|
||||
{Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1},
|
||||
{Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1},
|
||||
{Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"摊薄"}, Tag: "dilute", MatchIndex: -1, MustFill: true},
|
||||
}
|
||||
|
||||
func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.Meter04KV], error) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user