Skip to content

Commit

Permalink
added explicit sheet position
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Oct 2, 2024
1 parent b99abcf commit a60a0a8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
20 changes: 19 additions & 1 deletion marshaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/viant/xunsafe"
"github.com/xuri/excelize/v2"
"reflect"
"sort"
"unsafe"
)

Expand Down Expand Up @@ -77,7 +78,17 @@ func (m *Marshaller) buildSheets(v any, structType reflect.Type, parent *session
xStruct := xunsafe.NewStruct(structType)
ptr := xunsafe.AsPointer(v)
var aSheet *workSheet
for i := range xStruct.Fields {

var fields = xStruct.Fields
sort.Slice(fields, func(i, j int) bool {
posI := sheetPos(&xStruct.Fields[i])
posJ := sheetPos(&xStruct.Fields[j])
if posI == posJ {
return xStruct.Fields[i].Index < xStruct.Fields[j].Index
}
return posI < posJ
})
for i := range fields {
field := &xStruct.Fields[i]
fieldType := field.Type
if fieldType.Kind() == reflect.Ptr {
Expand Down Expand Up @@ -116,6 +127,13 @@ func (m *Marshaller) buildSheets(v any, structType reflect.Type, parent *session
return aSheet, nil
}

func sheetPos(field *xunsafe.Field) int {
if tag, _ := parseTag(field.Tag); tag != nil {
return tag.SheetPos
}
return int(field.Index)
}

func (m *Marshaller) buildSheet(v any, tableType reflect.Type, aSession *session) (*workSheet, error) {
aTable, err := NewTable(tableType, aSession.tag, aSession, nil)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
HeaderStyle *StyleTag
CellStyle *StyleTag
ColumnStyle *StyleTag
SheetPos int
Blank bool
Position *int
Inverted *bool //inverted orientation
Expand Down Expand Up @@ -142,13 +143,21 @@ func (t *Tag) update(key, value string, styles *[]*StyleTag) error {
if err := convertAndSetInt(&t.RowOffset, "rowOffset", value); err != nil {
return err
}
case "sheetpos":
var pos int
if err := convertAndSetInt(&pos, "sheetPos", value); err != nil {
return err
}
t.SheetPos = pos

case "pos":
var pos int
if err := convertAndSetInt(&pos, "pos", value); err != nil {
return err
}
t.Position = &pos
default:

if strings.HasSuffix(attr, "style") {
var style = &StyleTag{Style: value}
if index := strings.Index(attr, "."); index != -1 {
Expand Down

0 comments on commit a60a0a8

Please sign in to comment.