Skip to content

Commit e5c66ed

Browse files
committed
Add new function GetStyle, and unit tests, fix the code review issues
1 parent a368221 commit e5c66ed

File tree

3 files changed

+64
-16
lines changed

3 files changed

+64
-16
lines changed

lib_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ func TestInStrSlice(t *testing.T) {
238238
assert.EqualValues(t, -1, inStrSlice([]string{}, "", true))
239239
}
240240

241+
func TestAttrValue(t *testing.T) {
242+
assert.Empty(t, (&attrValString{}).Value())
243+
assert.False(t, (&attrValBool{}).Value())
244+
assert.Zero(t, (&attrValFloat{}).Value())
245+
}
246+
241247
func TestBoolValMarshal(t *testing.T) {
242248
bold := true
243249
node := &xlsxFont{B: &attrValBool{Val: &bold}}

styles.go

+47-16
Original file line numberDiff line numberDiff line change
@@ -1277,31 +1277,62 @@ func (f *File) extractNumFmt(xf xlsxXf, s *xlsxStyleSheet, style *Style) {
12771277
// extractAlignment provides a function to extract alignment format by
12781278
// given style definition.
12791279
func (f *File) extractAlignment(xf xlsxXf, s *xlsxStyleSheet, style *Style) {
1280-
style.Alignment = &Alignment{
1281-
Horizontal: xf.Alignment.Horizontal,
1282-
Indent: xf.Alignment.Indent,
1283-
JustifyLastLine: xf.Alignment.JustifyLastLine,
1284-
ReadingOrder: xf.Alignment.ReadingOrder,
1285-
RelativeIndent: xf.Alignment.RelativeIndent,
1286-
ShrinkToFit: xf.Alignment.ShrinkToFit,
1287-
TextRotation: xf.Alignment.TextRotation,
1288-
Vertical: xf.Alignment.Vertical,
1289-
WrapText: xf.Alignment.WrapText,
1280+
if xf.ApplyAlignment != nil && *xf.ApplyAlignment && xf.Alignment != nil {
1281+
style.Alignment = &Alignment{
1282+
Horizontal: xf.Alignment.Horizontal,
1283+
Indent: xf.Alignment.Indent,
1284+
JustifyLastLine: xf.Alignment.JustifyLastLine,
1285+
ReadingOrder: xf.Alignment.ReadingOrder,
1286+
RelativeIndent: xf.Alignment.RelativeIndent,
1287+
ShrinkToFit: xf.Alignment.ShrinkToFit,
1288+
TextRotation: xf.Alignment.TextRotation,
1289+
Vertical: xf.Alignment.Vertical,
1290+
WrapText: xf.Alignment.WrapText,
1291+
}
12901292
}
12911293
}
12921294

12931295
// extractProtection provides a function to extract protection settings by
12941296
// given format definition.
12951297
func (f *File) extractProtection(xf xlsxXf, s *xlsxStyleSheet, style *Style) {
1296-
style.Protection = &Protection{}
1297-
if xf.Protection.Hidden != nil {
1298-
style.Protection.Hidden = *xf.Protection.Hidden
1299-
}
1300-
if xf.Protection.Locked != nil {
1301-
style.Protection.Locked = *xf.Protection.Locked
1298+
if xf.ApplyProtection != nil && *xf.ApplyProtection && xf.Protection != nil {
1299+
style.Protection = &Protection{}
1300+
if xf.Protection.Hidden != nil {
1301+
style.Protection.Hidden = *xf.Protection.Hidden
1302+
}
1303+
if xf.Protection.Locked != nil {
1304+
style.Protection.Locked = *xf.Protection.Locked
1305+
}
13021306
}
13031307
}
13041308

1309+
// GetStyle get style details by given style index.
1310+
func (f *File) GetStyle(idx int) (*Style, error) {
1311+
var style *Style
1312+
f.mu.Lock()
1313+
s, err := f.stylesReader()
1314+
if err != nil {
1315+
return style, err
1316+
}
1317+
f.mu.Unlock()
1318+
if idx < 0 || s.CellXfs == nil || len(s.CellXfs.Xf) <= idx {
1319+
return style, newInvalidStyleID(idx)
1320+
}
1321+
style = &Style{}
1322+
xf := s.CellXfs.Xf[idx]
1323+
if xf.ApplyFill != nil && *xf.ApplyFill &&
1324+
xf.FillID != nil && s.Fills != nil &&
1325+
*xf.FillID < len(s.Fills.Fill) {
1326+
f.extractFills(xf, s, style)
1327+
}
1328+
f.extractBorders(xf, s, style)
1329+
f.extractFont(xf, s, style)
1330+
f.extractAlignment(xf, s, style)
1331+
f.extractProtection(xf, s, style)
1332+
f.extractNumFmt(xf, s, style)
1333+
return style, nil
1334+
}
1335+
13051336
// getXfIDFuncs provides a function to get xfID by given style.
13061337
var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
13071338
"numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool {

styles_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,14 @@ func TestGetNumFmtID(t *testing.T) {
488488
assert.NotEqual(t, id1, id2)
489489
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestStyleNumFmt.xlsx")))
490490
}
491+
492+
func TestGetThemeColor(t *testing.T) {
493+
assert.Empty(t, (&File{}).getThemeColor(&xlsxColor{}))
494+
f := NewFile()
495+
assert.Empty(t, f.getThemeColor(nil))
496+
var theme int
497+
assert.Equal(t, "FFFFFF", f.getThemeColor(&xlsxColor{Theme: &theme}))
498+
assert.Equal(t, "FFFFFF", f.getThemeColor(&xlsxColor{RGB: "FFFFFF"}))
499+
assert.Equal(t, "FF8080", f.getThemeColor(&xlsxColor{Indexed: 2, Tint: 0.5}))
500+
assert.Empty(t, f.getThemeColor(&xlsxColor{Indexed: len(IndexedColorMapping), Tint: 0.5}))
501+
}

0 commit comments

Comments
 (0)