Skip to content

Commit

Permalink
support extend tag to create dynamic columns excel sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
陈遇文 committed May 5, 2023
1 parent 41ad0a5 commit 928ad0c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 deletions.
6 changes: 3 additions & 3 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func (f *File) getStyle(styleNames []string) (styleID int) {
// 已经存在的style会直接返回styleID
name := strings.Join(styleNames, "+")
if len(styleNames) <= 1 {
_style := f.styleCache[name]
return _style.StyleID
return f.styleCache[name].StyleID
}

_style, ok := f.styleCache[styleNames[0]]
Expand Down Expand Up @@ -349,7 +348,8 @@ func (f *File) setCellsStyle(s *Sheet) (err error) {
}

cell = column + row
if err = f.excel().SetCellStyle(s.name, cell, cell, f.getStyle(styles)); err != nil {
a := f.getStyle(styles)
if err = f.excel().SetCellStyle(s.name, cell, cell, a); err != nil {
return
}
}
Expand Down
2 changes: 0 additions & 2 deletions meta_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type metaRaws struct {
cursor int
parsed bool
raws []*metaRaw
set map[int]struct{}

hasData bool
data [][]any
Expand Down Expand Up @@ -78,7 +77,6 @@ func (mr *metaRaws) sheet(sheetName string) *Sheet {
func newMetas(a any) *metaRaws {
r := &metaRaws{
cursor: 1,
set: map[int]struct{}{},
raws: make([]*metaRaw, 0),
}

Expand Down
7 changes: 4 additions & 3 deletions meta_raw_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
func (mr *metaRaws) append(raw *metaRaw, sliceIndex int, data ...any) {
if !mr.parsed {
mr.raws = append(mr.raws, raw)
mr.set[raw.colIndex] = struct{}{}
mr.cursor += 1
if raw.part != noticePart {
mr.cursor += 1
}
}

if mr.hasData && len(data) > 0 && raw.part != noticePart {
mr.data[sliceIndex] = append(mr.data[sliceIndex], data)
mr.data[sliceIndex] = append(mr.data[sliceIndex], data...)
}

}
Expand Down
71 changes: 48 additions & 23 deletions test/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,16 @@ import (
"github.com/cyclonevox/excelizex"
)

type TestNoStyle struct {
Notice string `excel:"notice"`
Name string `excel:"header|学生姓名"`
Phone int `excel:"header|学生号码"`
}

type TestHasStyle struct {
Notice string `excel:"notice" style:"default-notice"`
Name string `excel:"header|学生姓名" style:"default-header"`
Phone int `excel:"header|学生号码" style:"default-header-red"`
Id int `excel:"header|学生编号" style:"default-header-red"`
}

func TestGenerateFile(t *testing.T) {
t.Run("no_data_no_style", func(t *testing.T) {
err := excelizex.New().AddSheet("helloWorld", new(TestNoStyle)).SaveAs("./xlsx/no_data_no_style.xlsx")
err := excelizex.New().AddSheet("helloWorld", new(noStyle)).SaveAs("./xlsx/no_data_no_style.xlsx")
if err != nil {
t.Errorf("has_data_no_style: %s", err)
}
})

t.Run("no_data_has_style", func(t *testing.T) {
ttt := new(TestHasStyle)
ttt := new(hasStyle)
ttt.Notice = "你好世界你好世界你好世界你好世界你好世界你好世界你好世界"
err := excelizex.New().AddSheet("helloWorld", ttt).SaveAs("./xlsx/no_data_has_style.xlsx")
if err != nil {
Expand All @@ -38,11 +25,11 @@ func TestGenerateFile(t *testing.T) {
})

t.Run("has_data_no_style", func(t *testing.T) {
var hasdata []*TestNoStyle
var hasdata []*noStyle
for i := 0; i < 100; i++ {
hasdata = append(
hasdata,
&TestNoStyle{
&noStyle{
Notice: "你好世界你好世界你好世界你好世界你好世界你好世界你好世界",
Name: "hello" + strconv.FormatInt(int64(i), 10),
Phone: i,
Expand All @@ -57,14 +44,14 @@ func TestGenerateFile(t *testing.T) {
})

t.Run("has_data_has_style", func(t *testing.T) {
var hasdata []*TestHasStyle
var hasdata []*hasStyle
for i := 0; i < 100; i++ {
hasdata = append(
hasdata,
&TestHasStyle{
&hasStyle{
Notice: "你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界",
Name: "hello" + strconv.FormatInt(int64(i), 10),
Phone: i,
Phone: 100 + i,
Id: i,
},
)
Expand All @@ -77,14 +64,14 @@ func TestGenerateFile(t *testing.T) {
})

t.Run("has_data_has_style_has_pull_down", func(t *testing.T) {
var hasdata []*TestHasStyle
var hasdata []*hasStyle
for i := 0; i < 100; i++ {
hasdata = append(
hasdata,
&TestHasStyle{
&hasStyle{
Notice: "你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界",
Name: "hello" + strconv.FormatInt(int64(i), 10),
Phone: i,
Phone: 100 + i,
Id: i,
},
)
Expand All @@ -99,4 +86,42 @@ func TestGenerateFile(t *testing.T) {
t.Errorf("has_data_has_style: %s", err)
}
})

t.Run("has_data_style_pull_down_dynamic", func(t *testing.T) {
var hasdata []*hasStyleHasDynamic
for i := 0; i < 100; i++ {
hasdata = append(
hasdata,
&hasStyleHasDynamic{
Notice: "你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界你好世界",
Name: "hello" + strconv.FormatInt(int64(i), 10),
Phone: 100 + i,
Id: i,
ExtInfo: []excelizex.DefaultExtHeader{
{
HeaderName: "他爹的年纪",
StyleTag: "default-header-red",
ValidateTag: "",
Data: 33 + i,
},
{
HeaderName: "他爷的年纪",
StyleTag: "default-header-red",
ValidateTag: "",
Data: 55 + i,
},
},
},
)
}

err := excelizex.New().AddSheet("考生导入", hasdata,
excelizex.NewOptions("学生姓名", []string{"tom", "jerry"}),
excelizex.NewOptions("学生号码", []string{"13380039232", "13823021932", "17889032312"}),
excelizex.NewOptions("学生编号", []string{"1", "2", "3"}),
).SaveAs("./xlsx/has_data_style_pull_down_dynamic.xlsx", "test")
if err != nil {
t.Errorf("has_data_has_style: %s", err)
}
})
}
22 changes: 22 additions & 0 deletions test/test_utils_struct.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"github.com/cyclonevox/excelizex"
"github.com/cyclonevox/validatorx"
"github.com/go-playground/validator/v10"
)
Expand All @@ -27,3 +28,24 @@ type batchData struct {
Id string `excel:"header|userId" excel-conv:"id-string"`
Phone string `excel:"header|phoneNumber"`
}

type noStyle struct {
Notice string `excel:"notice"`
Name string `excel:"header|学生姓名"`
Phone int `excel:"header|学生号码"`
}

type hasStyle struct {
Notice string `excel:"notice" style:"default-notice"`
Name string `excel:"header|学生姓名" style:"default-header"`
Phone int `excel:"header|学生号码" style:"default-header-red"`
Id int `excel:"header|学生编号" style:"default-header-red"`
}

type hasStyleHasDynamic struct {
Notice string `excel:"notice" style:"default-notice"`
Name string `excel:"header|学生姓名" style:"default-header"`
Phone int `excel:"header|学生号码" style:"default-header-red"`
Id int `excel:"header|学生编号" style:"default-header-red"`
ExtInfo []excelizex.DefaultExtHeader `excel:"extend"`
}

0 comments on commit 928ad0c

Please sign in to comment.