Skip to content

Commit

Permalink
col/row now can accept styles, but not only styleID, col/row options …
Browse files Browse the repository at this point in the history
…now can hold styles info too
  • Loading branch information
plandem committed Jul 31, 2019
1 parent e808f65 commit 4246c97
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 33 deletions.
31 changes: 2 additions & 29 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,40 +316,13 @@ func (c *Cell) Styles() styles.DirectStyleID {

//SetStyles sets style format to requested DirectStyleID or styles.Info
func (c *Cell) SetStyles(s interface{}) {
if styleID, ok := s.(styles.DirectStyleID); ok {
c.ml.Style = styleID
return
}

//we can update styleSheet only when sheet is in write mode, to prevent pollution of styleSheet with fake values
if (c.sheet.mode() & sheetModeWrite) == 0 {
panic(errorNotSupportedWrite)
}

var format *styles.Info
if f, ok := s.(styles.Info); ok {
format = &f
} else if f, ok := s.(*styles.Info); ok {
format = f
} else {
panic("only DirectStyleID or styles.Info supported as styles for cell")
}

styleID := c.sheet.workbook.doc.styleSheet.addStyle(format)
c.ml.Style = styleID
c.ml.Style = c.sheet.resolveStyleID(s)
}

//SetValueWithFormat is helper function that internally works as SetValue and SetStyles with NumberFormat
func (c *Cell) SetValueWithFormat(value interface{}, formatCode string) {
//we can update styleSheet only when sheet is in write mode, to prevent pollution of styleSheet with fake values
if (c.sheet.mode() & sheetModeWrite) == 0 {
panic(errorNotSupportedWrite)
}

styleID := c.sheet.workbook.doc.styleSheet.addStyle(styles.New(styles.NumberFormat(formatCode)))

c.ml.Style = c.sheet.resolveStyleID(styles.New(styles.NumberFormat(formatCode)))
c.SetValue(value)
c.ml.Style = styleID
}

//Hyperlink returns resolved hyperlink.Info if there is any hyperlink or nil otherwise
Expand Down
8 changes: 6 additions & 2 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (c *Col) SetOptions(o *options.Info) {
c.ml.CustomWidth = true
}

if o.Format != nil {
c.SetStyles(o.Format)
}

c.ml.OutlineLevel = o.OutlineLevel
c.ml.Hidden = o.Hidden
c.ml.Collapsed = o.Collapsed
Expand All @@ -40,8 +44,8 @@ func (c *Col) Styles() styles.DirectStyleID {
}

//SetStyles sets default style for the column. Affects cells not yet allocated in the column. In other words, this style applies to new cells.
func (c *Col) SetStyles(styleID styles.DirectStyleID) {
c.ml.Style = styleID
func (c *Col) SetStyles(s interface{}) {
c.ml.Style = c.sheet.info().resolveStyleID(s)
}

//CopyTo copies col cells into another col with cIdx index.
Expand Down
8 changes: 6 additions & 2 deletions row.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (r *Row) SetOptions(o *options.Info) {
r.ml.CustomHeight = true
}

if o.Format != nil {
r.SetStyles(o.Format)
}

r.ml.OutlineLevel = o.OutlineLevel
r.ml.Hidden = o.Hidden
r.ml.Collapsed = o.Collapsed
Expand All @@ -40,9 +44,9 @@ func (r *Row) Styles() styles.DirectStyleID {
}

//SetStyles sets default style for the row. Affects cells not yet allocated in the row. In other words, this style applies to new cells.
func (r *Row) SetStyles(styleID styles.DirectStyleID) {
func (r *Row) SetStyles(s interface{}) {
r.ml.CustomFormat = true
r.ml.Style = styleID
r.ml.Style = r.sheet.info().resolveStyleID(s)
}

//CopyTo copies row cells into another row with rIdx index.
Expand Down
27 changes: 27 additions & 0 deletions sheet_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/plandem/ooxml"
sharedML "github.com/plandem/ooxml/ml"
"github.com/plandem/xlsx/format/conditional"
"github.com/plandem/xlsx/format/styles"
"github.com/plandem/xlsx/internal"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/types"
Expand Down Expand Up @@ -288,3 +289,29 @@ func (s *sheetInfo) AfterMarshalXML(content []byte) []byte {

return content
}

func (s *sheetInfo) resolveStyleID(st interface{}) styles.DirectStyleID {
if st == nil {
return 0
}

if styleID, ok := st.(styles.DirectStyleID); ok {
return styleID
}

//we can update styleSheet only when sheet is in write mode, to prevent pollution of styleSheet with fake values
if (s.mode() & sheetModeWrite) == 0 {
panic(errorNotSupportedWrite)
}

var format *styles.Info
if f, ok := st.(styles.Info); ok {
format = &f
} else if f, ok := st.(*styles.Info); ok {
format = f
} else {
panic("only DirectStyleID or styles.Info supported as styles for cell")
}

return s.workbook.doc.styleSheet.addStyle(format)
}
8 changes: 8 additions & 0 deletions types/options/column/col.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Info struct {
Phonetic bool
Hidden bool
Width float32
Format interface{}
}

//Option is helper type to set options for comment
Expand Down Expand Up @@ -67,3 +68,10 @@ func Width(width float32) Option {
i.Width = width
}
}

//Styles sets style format to requested DirectStyleID or styles.Info
func Styles(s interface{}) Option {
return func(i *Info) {
i.Format = s
}
}
3 changes: 3 additions & 0 deletions types/options/column/col_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package options

import (
"github.com/plandem/xlsx/format/styles"
"github.com/stretchr/testify/require"
"testing"
)
Expand All @@ -16,6 +17,7 @@ func TestColumnOptions(t *testing.T) {
Phonetic(true),
Width(45.5),
Collapsed(true),
Styles(styles.DirectStyleID(1)),
)

require.IsType(t, &Info{}, o)
Expand All @@ -25,6 +27,7 @@ func TestColumnOptions(t *testing.T) {
Phonetic: true,
Width: 45.5,
Collapsed: true,
Format: styles.DirectStyleID(1),
}, o)

o = New(
Expand Down
8 changes: 8 additions & 0 deletions types/options/row/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Info struct {
Phonetic bool
Hidden bool
Height float32
Format interface{}
}

//Option is helper type to set options for row
Expand Down Expand Up @@ -67,3 +68,10 @@ func Height(height float32) Option {
i.Height = height
}
}

//Styles sets style format to requested DirectStyleID or styles.Info
func Styles(s interface{}) Option {
return func(i *Info) {
i.Format = s
}
}
3 changes: 3 additions & 0 deletions types/options/row/row_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package options

import (
"github.com/plandem/xlsx/format/styles"
"github.com/stretchr/testify/require"
"testing"
)
Expand All @@ -16,6 +17,7 @@ func TestRowOptions(t *testing.T) {
Phonetic(true),
Height(45.5),
Collapsed(true),
Styles(styles.DirectStyleID(1)),
)

require.IsType(t, &Info{}, o)
Expand All @@ -25,6 +27,7 @@ func TestRowOptions(t *testing.T) {
Phonetic: true,
Height: 45.5,
Collapsed: true,
Format: styles.DirectStyleID(1),
}, o)

o = New(
Expand Down

0 comments on commit 4246c97

Please sign in to comment.