Skip to content

Commit 85e0b6c

Browse files
committed
Support to create of 17 kinds of fill variants styles
- Update the unit tests - Update the `SetHeaderFooter` function parameters name - Update document for the `SetDocProps` and `SetCellHyperLink` functions
1 parent be36b09 commit 85e0b6c

File tree

5 files changed

+67
-57
lines changed

5 files changed

+67
-57
lines changed

cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ type HyperlinkOpts struct {
835835
//
836836
// display, tooltip := "https://github.com/xuri/excelize", "Excelize on GitHub"
837837
// if err := f.SetCellHyperLink("Sheet1", "A3",
838-
// "https://github.com/xuri/excelize", "External", excelize.HyperlinkOpts{
838+
// display, "External", excelize.HyperlinkOpts{
839839
// Display: &display,
840840
// Tooltip: &tooltip,
841841
// }); err != nil {

docProps.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,45 @@ func (f *File) GetAppProps() (ret *AppProperties, err error) {
120120
// properties that can be set are:
121121
//
122122
// Property | Description
123-
// ----------------+-----------------------------------------------------------------------------
123+
// ----------------+-----------------------------------------------------------
124124
// Title | The name given to the resource.
125125
// |
126126
// Subject | The topic of the content of the resource.
127127
// |
128-
// Creator | An entity primarily responsible for making the content of the resource.
128+
// Creator | An entity primarily responsible for making the content of
129+
// | the resource.
129130
// |
130-
// Keywords | A delimited set of keywords to support searching and indexing. This is
131-
// | typically a list of terms that are not available elsewhere in the properties.
131+
// Keywords | A delimited set of keywords to support searching and
132+
// | indexing. This is typically a list of terms that are not
133+
// | available elsewhere in the properties.
132134
// |
133135
// Description | An explanation of the content of the resource.
134136
// |
135-
// LastModifiedBy | The user who performed the last modification. The identification is
136-
// | environment-specific.
137+
// LastModifiedBy | The user who performed the last modification. The
138+
// | identification is environment-specific.
137139
// |
138140
// Language | The language of the intellectual content of the resource.
139141
// |
140-
// Identifier | An unambiguous reference to the resource within a given context.
142+
// Identifier | An unambiguous reference to the resource within a given
143+
// | context.
141144
// |
142145
// Revision | The topic of the content of the resource.
143146
// |
144-
// ContentStatus | The status of the content. For example: Values might include "Draft",
145-
// | "Reviewed" and "Final"
147+
// ContentStatus | The status of the content. For example: Values might
148+
// | include "Draft", "Reviewed" and "Final"
146149
// |
147150
// Category | A categorization of the content of this package.
148151
// |
149-
// Version | The version number. This value is set by the user or by the application.
152+
// Version | The version number. This value is set by the user or by
153+
// | the application.
150154
// |
151-
// Modified | The created time of the content of the resource which
152-
// | represent in ISO 8601 UTC format, for example "2019-06-04T22:00:10Z".
155+
// Created | The created time of the content of the resource which
156+
// | represent in ISO 8601 UTC format, for example
157+
// | "2019-06-04T22:00:10Z".
153158
// |
154159
// Modified | The modified time of the content of the resource which
155-
// | represent in ISO 8601 UTC format, for example "2019-06-04T22:00:10Z".
160+
// | represent in ISO 8601 UTC format, for example
161+
// | "2019-06-04T22:00:10Z".
156162
// |
157163
//
158164
// For example:

sheet.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1183,17 +1183,17 @@ func attrValToBool(name string, attrs []xml.Attr) (val bool, err error) {
11831183
// that same page
11841184
//
11851185
// - No footer on the first page
1186-
func (f *File) SetHeaderFooter(sheet string, settings *HeaderFooterOptions) error {
1186+
func (f *File) SetHeaderFooter(sheet string, opts *HeaderFooterOptions) error {
11871187
ws, err := f.workSheetReader(sheet)
11881188
if err != nil {
11891189
return err
11901190
}
1191-
if settings == nil {
1191+
if opts == nil {
11921192
ws.HeaderFooter = nil
11931193
return err
11941194
}
11951195

1196-
v := reflect.ValueOf(*settings)
1196+
v := reflect.ValueOf(*opts)
11971197
// Check 6 string type fields: OddHeader, OddFooter, EvenHeader, EvenFooter,
11981198
// FirstFooter, FirstHeader
11991199
for i := 4; i < v.NumField()-1; i++ {
@@ -1202,16 +1202,16 @@ func (f *File) SetHeaderFooter(sheet string, settings *HeaderFooterOptions) erro
12021202
}
12031203
}
12041204
ws.HeaderFooter = &xlsxHeaderFooter{
1205-
AlignWithMargins: settings.AlignWithMargins,
1206-
DifferentFirst: settings.DifferentFirst,
1207-
DifferentOddEven: settings.DifferentOddEven,
1208-
ScaleWithDoc: settings.ScaleWithDoc,
1209-
OddHeader: settings.OddHeader,
1210-
OddFooter: settings.OddFooter,
1211-
EvenHeader: settings.EvenHeader,
1212-
EvenFooter: settings.EvenFooter,
1213-
FirstFooter: settings.FirstFooter,
1214-
FirstHeader: settings.FirstHeader,
1205+
AlignWithMargins: opts.AlignWithMargins,
1206+
DifferentFirst: opts.DifferentFirst,
1207+
DifferentOddEven: opts.DifferentOddEven,
1208+
ScaleWithDoc: opts.ScaleWithDoc,
1209+
OddHeader: opts.OddHeader,
1210+
OddFooter: opts.OddFooter,
1211+
EvenHeader: opts.EvenHeader,
1212+
EvenFooter: opts.EvenFooter,
1213+
FirstFooter: opts.FirstFooter,
1214+
FirstHeader: opts.FirstHeader,
12151215
}
12161216
return err
12171217
}

styles.go

+27-30
Original file line numberDiff line numberDiff line change
@@ -1145,9 +1145,9 @@ func parseFormatStyleSet(style *Style) (*Style, error) {
11451145
//
11461146
// Index | Style | Index | Style
11471147
// -------+-----------------+-------+-----------------
1148-
// 0 | Horizontal | 3 | Diagonal down
1149-
// 1 | Vertical | 4 | From corner
1150-
// 2 | Diagonal Up | 5 | From center
1148+
// 0-2 | Horizontal | 9-11 | Diagonal down
1149+
// 3-5 | Vertical | 12-15 | From corner
1150+
// 6-8 | Diagonal Up | 16 | From center
11511151
//
11521152
// The following table shows the pattern styles used in 'Fill.Pattern' supported
11531153
// by excelize index number:
@@ -2459,41 +2459,38 @@ func newFills(style *Style, fg bool) *xlsxFill {
24592459
"gray125",
24602460
"gray0625",
24612461
}
2462-
2463-
variants := []float64{
2464-
90,
2465-
0,
2466-
45,
2467-
135,
2462+
variants := []xlsxGradientFill{
2463+
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2464+
{Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2465+
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
2466+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2467+
{Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2468+
{Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
2469+
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2470+
{Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2471+
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
2472+
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2473+
{Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
2474+
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
2475+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"},
2476+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1},
2477+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1},
2478+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1},
2479+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5},
24682480
}
24692481

24702482
var fill xlsxFill
24712483
switch style.Fill.Type {
24722484
case "gradient":
2473-
if len(style.Fill.Color) != 2 {
2485+
if len(style.Fill.Color) != 2 || style.Fill.Shading < 0 || style.Fill.Shading > 16 {
24742486
break
24752487
}
2476-
var gradient xlsxGradientFill
2477-
switch style.Fill.Shading {
2478-
case 0, 1, 2, 3:
2479-
gradient.Degree = variants[style.Fill.Shading]
2480-
case 4:
2481-
gradient.Type = "path"
2482-
case 5:
2483-
gradient.Type = "path"
2484-
gradient.Bottom = 0.5
2485-
gradient.Left = 0.5
2486-
gradient.Right = 0.5
2487-
gradient.Top = 0.5
2488-
}
2489-
var stops []*xlsxGradientFillStop
2490-
for index, color := range style.Fill.Color {
2491-
var stop xlsxGradientFillStop
2492-
stop.Position = float64(index)
2493-
stop.Color.RGB = getPaletteColor(color)
2494-
stops = append(stops, &stop)
2488+
gradient := variants[style.Fill.Shading]
2489+
gradient.Stop[0].Color.RGB = getPaletteColor(style.Fill.Color[0])
2490+
gradient.Stop[1].Color.RGB = getPaletteColor(style.Fill.Color[1])
2491+
if len(gradient.Stop) == 3 {
2492+
gradient.Stop[2].Color.RGB = getPaletteColor(style.Fill.Color[0])
24952493
}
2496-
gradient.Stop = stops
24972494
fill.GradientFill = &gradient
24982495
case "pattern":
24992496
if style.Fill.Pattern > 18 || style.Fill.Pattern < 0 {

styles_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ func TestUnsetConditionalFormat(t *testing.T) {
223223

224224
func TestNewStyle(t *testing.T) {
225225
f := NewFile()
226+
for i := 0; i < 18; i++ {
227+
_, err := f.NewStyle(&Style{
228+
Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF", "#4E71BE"}, Shading: i},
229+
})
230+
assert.NoError(t, err)
231+
}
232+
f = NewFile()
226233
styleID, err := f.NewStyle(&Style{Font: &Font{Bold: true, Italic: true, Family: "Times New Roman", Size: 36, Color: "#777777"}})
227234
assert.NoError(t, err)
228235
styles, err := f.stylesReader()

0 commit comments

Comments
 (0)