Skip to content

Commit

Permalink
fix: AddRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
NX-Official committed Mar 27, 2024
1 parent 8458cae commit 6f2b0e4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 49 deletions.
46 changes: 5 additions & 41 deletions book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,6 @@ import (
"testing"
)

type (
// Overview 总览
Overview struct {
StaffId string `excel:"学号"`
StaffName string `excel:"姓名"`
ClassNo string `excel:"班级"`
Score float64 `excel:"总分"`
Tag []string `excel:"标签"`
Awards []string `excel:"奖项"`
Achievement []OverviewAchievement `excel:"达标情况"`
Pin []OverviewPin `excel:"重要成绩项"`
}
OverviewAchievement struct {
Name string `excel:"奖项"`
Score float64 `excel:"分数"`
AchieveScore float64 `excel:"达标分数"`
Achieved bool `excel:"已达标"`
}
OverviewPin struct {
Name string `excel:"名称"`
Score float64 `excel:"分数"`
}

// Achievement 达标情况
Achievement struct {
StaffId string `excel:"学号"`
StaffName string `excel:"姓名"`
ClassNo string `excel:"班级"`
Achievement map[string]bool `excel:"达标情况"`
}

// Pin 重要成绩项
Pin struct {
StaffId string `excel:"学号"`
StaffName string `excel:"姓名"`
ClassNo string `excel:"班级"`
Pin map[string]float64 `excel:"重要成绩项"`
}
)

var overview = Overview{
StaffId: "22050626",
StaffName: "王文杰",
Expand Down Expand Up @@ -105,7 +65,11 @@ func Test_exporter_buildHeader(t *testing.T) {
o, _ := wb.AddSheet("overview", overview)
o.AddRecord(overview)
a, _ := wb.AddSheet("achievement", achievement)
a.AddRecord(achievement)
//a.AddRecord(achievement)
//a.AddRecord(achievement)
//
//a.AddRecord(achievement)
a.AddRecords([]Achievement{achievement, achievement, achievement})
p, _ := wb.AddSheet("pin", pin)
p.AddRecord(pin)

Expand Down
65 changes: 57 additions & 8 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,68 @@ func (s *sheet) buildHeader() (err error) {
return
}

type (
// Overview 总览
Overview struct {
StaffId string `excel:"学号"`
StaffName string `excel:"姓名"`
ClassNo string `excel:"班级"`
Score float64 `excel:"总分"`
Tag []string `excel:"标签"`
Awards []string `excel:"奖项"`
Achievement []OverviewAchievement `excel:"达标情况"`
Pin []OverviewPin `excel:"重要成绩项"`
}
OverviewAchievement struct {
Name string `excel:"奖项"`
Score float64 `excel:"分数"`
AchieveScore float64 `excel:"达标分数"`
Achieved bool `excel:"已达标"`
}
OverviewPin struct {
Name string `excel:"名称"`
Score float64 `excel:"分数"`
}

// Achievement 达标情况
Achievement struct {
StaffId string `excel:"学号"`
StaffName string `excel:"姓名"`
ClassNo string `excel:"班级"`
Achievement map[string]bool `excel:"达标情况"`
}

// Pin 重要成绩项
Pin struct {
StaffId string `excel:"学号"`
StaffName string `excel:"姓名"`
ClassNo string `excel:"班级"`
Pin map[string]float64 `excel:"重要成绩项"`
}
)

func (s *sheet) AddRecords(records any) error {
if reflect.TypeOf(records).Kind() != reflect.Slice {
return fmt.Errorf("records must be a slice")
}
for i := 0; i < reflect.ValueOf(records).Len(); i++ {
err := s.AddRecord(reflect.ValueOf(records).Index(i).Interface())
err := s.addRecord(reflect.ValueOf(records).Index(i))
if err != nil {
return err
}
}
//v := records.([]Achievement)
//for i := 0; i < len(v); i++ {
// err := s.AddRecord(v[i])
// if err != nil {
// return err
// }
//
//}
return nil
}

func (s *sheet) AddRecord(record any) (err error) {
if reflect.TypeOf(record) != s.template.t {
return fmt.Errorf("record type mismatch")
}

v := reflect.ValueOf(record)

func (s *sheet) addRecord(v reflect.Value) (err error) {
var insert func(v reflect.Value, node *itemNode, currIdx int) (err error, maxIdx int)
insert = func(v reflect.Value, node *itemNode, currIdx int) (err error, maxIdx int) {
maxIdx = max(currIdx, maxIdx)
Expand Down Expand Up @@ -193,6 +235,13 @@ func (s *sheet) AddRecord(record any) (err error) {
return
}

func (s *sheet) AddRecord(record any) (err error) {
if reflect.TypeOf(record) != s.template.t {
return fmt.Errorf("record type mismatch")
}
return s.addRecord(reflect.ValueOf(record))
}

func (s *sheet) writeCell(colName string, row int, value any) error {
cell, _ := excelize.CoordinatesToCellName(s.colIndex[colName], row)
fmt.Println(colName, "->", s.colIndex[colName], row, cell, value)
Expand Down

0 comments on commit 6f2b0e4

Please sign in to comment.