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 928ad0c commit 98c3fa9
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package excelizex

import (
"reflect"
"regexp"
"strconv"

"github.com/xuri/excelize/v2"
Expand Down Expand Up @@ -77,35 +76,34 @@ func getRowData(row any) (list []any) {
return
}

// findHeaderColumnName 寻找表头名称或者是列名称
func (s *Sheet) findHeaderColumnName(headOrColName string) (columnName string, err error) {
// findHeaderColumnNames 寻找表头名称或者是列名称
func (s *Sheet) findHeaderColumnNames(headName string) (columnNames []string, err error) {
var columnName string

for i, h := range s.header {
if h == headOrColName {
columnName, err = excelize.ColumnNumberToName(i + 1)
if h == headName {
if columnName, err = excelize.ColumnNumberToName(i + 1); err != nil {
return
}

return
columnNames = append(columnNames, columnName)
}
}

regular := `[A-Z]+`
reg := regexp.MustCompile(regular)
if !reg.MatchString(headOrColName) {
panic("plz use A-Z ColName or HeaderName for option name ")
}

columnName = headOrColName

return
}

// SetOptions 设置下拉的选项
func (s *Sheet) SetOptions(headOrColName string, options any) *Sheet {
name, err := s.findHeaderColumnName(headOrColName)
names, err := s.findHeaderColumnNames(headOrColName)
if err != nil {
panic(err)
}

pd := newPullDown().addOptions(name, options)
pd := newPullDown()
for _, name := range names {
pd.addOptions(name, options)
}

if s.pd == nil {
s.pd = pd
Expand Down

0 comments on commit 98c3fa9

Please sign in to comment.