From 85e0b6c56eb14c7eabf4332a778bf654f83295ca Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 1 Feb 2023 00:11:08 +0800 Subject: [PATCH] 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 --- cell.go | 2 +- docProps.go | 32 ++++++++++++++++------------ sheet.go | 26 +++++++++++------------ styles.go | 57 ++++++++++++++++++++++++-------------------------- styles_test.go | 7 +++++++ 5 files changed, 67 insertions(+), 57 deletions(-) diff --git a/cell.go b/cell.go index 3005222c..38366e4a 100644 --- a/cell.go +++ b/cell.go @@ -835,7 +835,7 @@ type HyperlinkOpts struct { // // display, tooltip := "https://github.com/xuri/excelize", "Excelize on GitHub" // if err := f.SetCellHyperLink("Sheet1", "A3", -// "https://github.com/xuri/excelize", "External", excelize.HyperlinkOpts{ +// display, "External", excelize.HyperlinkOpts{ // Display: &display, // Tooltip: &tooltip, // }); err != nil { diff --git a/docProps.go b/docProps.go index 3dca7bd2..3d815454 100644 --- a/docProps.go +++ b/docProps.go @@ -120,39 +120,45 @@ func (f *File) GetAppProps() (ret *AppProperties, err error) { // properties that can be set are: // // Property | Description -// ----------------+----------------------------------------------------------------------------- +// ----------------+----------------------------------------------------------- // Title | The name given to the resource. // | // Subject | The topic of the content of the resource. // | -// Creator | An entity primarily responsible for making the content of the resource. +// Creator | An entity primarily responsible for making the content of +// | the resource. // | -// Keywords | A delimited set of keywords to support searching and indexing. This is -// | typically a list of terms that are not available elsewhere in the properties. +// Keywords | A delimited set of keywords to support searching and +// | indexing. This is typically a list of terms that are not +// | available elsewhere in the properties. // | // Description | An explanation of the content of the resource. // | -// LastModifiedBy | The user who performed the last modification. The identification is -// | environment-specific. +// LastModifiedBy | The user who performed the last modification. The +// | identification is environment-specific. // | // Language | The language of the intellectual content of the resource. // | -// Identifier | An unambiguous reference to the resource within a given context. +// Identifier | An unambiguous reference to the resource within a given +// | context. // | // Revision | The topic of the content of the resource. // | -// ContentStatus | The status of the content. For example: Values might include "Draft", -// | "Reviewed" and "Final" +// ContentStatus | The status of the content. For example: Values might +// | include "Draft", "Reviewed" and "Final" // | // Category | A categorization of the content of this package. // | -// Version | The version number. This value is set by the user or by the application. +// Version | The version number. This value is set by the user or by +// | the application. // | -// Modified | The created time of the content of the resource which -// | represent in ISO 8601 UTC format, for example "2019-06-04T22:00:10Z". +// Created | The created time of the content of the resource which +// | represent in ISO 8601 UTC format, for example +// | "2019-06-04T22:00:10Z". // | // Modified | The modified time of the content of the resource which -// | represent in ISO 8601 UTC format, for example "2019-06-04T22:00:10Z". +// | represent in ISO 8601 UTC format, for example +// | "2019-06-04T22:00:10Z". // | // // For example: diff --git a/sheet.go b/sheet.go index 9d1f5f9b..3ea489a5 100644 --- a/sheet.go +++ b/sheet.go @@ -1183,17 +1183,17 @@ func attrValToBool(name string, attrs []xml.Attr) (val bool, err error) { // that same page // // - No footer on the first page -func (f *File) SetHeaderFooter(sheet string, settings *HeaderFooterOptions) error { +func (f *File) SetHeaderFooter(sheet string, opts *HeaderFooterOptions) error { ws, err := f.workSheetReader(sheet) if err != nil { return err } - if settings == nil { + if opts == nil { ws.HeaderFooter = nil return err } - v := reflect.ValueOf(*settings) + v := reflect.ValueOf(*opts) // Check 6 string type fields: OddHeader, OddFooter, EvenHeader, EvenFooter, // FirstFooter, FirstHeader for i := 4; i < v.NumField()-1; i++ { @@ -1202,16 +1202,16 @@ func (f *File) SetHeaderFooter(sheet string, settings *HeaderFooterOptions) erro } } ws.HeaderFooter = &xlsxHeaderFooter{ - AlignWithMargins: settings.AlignWithMargins, - DifferentFirst: settings.DifferentFirst, - DifferentOddEven: settings.DifferentOddEven, - ScaleWithDoc: settings.ScaleWithDoc, - OddHeader: settings.OddHeader, - OddFooter: settings.OddFooter, - EvenHeader: settings.EvenHeader, - EvenFooter: settings.EvenFooter, - FirstFooter: settings.FirstFooter, - FirstHeader: settings.FirstHeader, + AlignWithMargins: opts.AlignWithMargins, + DifferentFirst: opts.DifferentFirst, + DifferentOddEven: opts.DifferentOddEven, + ScaleWithDoc: opts.ScaleWithDoc, + OddHeader: opts.OddHeader, + OddFooter: opts.OddFooter, + EvenHeader: opts.EvenHeader, + EvenFooter: opts.EvenFooter, + FirstFooter: opts.FirstFooter, + FirstHeader: opts.FirstHeader, } return err } diff --git a/styles.go b/styles.go index e5d933cd..af323245 100644 --- a/styles.go +++ b/styles.go @@ -1145,9 +1145,9 @@ func parseFormatStyleSet(style *Style) (*Style, error) { // // Index | Style | Index | Style // -------+-----------------+-------+----------------- -// 0 | Horizontal | 3 | Diagonal down -// 1 | Vertical | 4 | From corner -// 2 | Diagonal Up | 5 | From center +// 0-2 | Horizontal | 9-11 | Diagonal down +// 3-5 | Vertical | 12-15 | From corner +// 6-8 | Diagonal Up | 16 | From center // // The following table shows the pattern styles used in 'Fill.Pattern' supported // by excelize index number: @@ -2459,41 +2459,38 @@ func newFills(style *Style, fg bool) *xlsxFill { "gray125", "gray0625", } - - variants := []float64{ - 90, - 0, - 45, - 135, + variants := []xlsxGradientFill{ + {Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, + {Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, + {Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, + {Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1}, + {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5}, } var fill xlsxFill switch style.Fill.Type { case "gradient": - if len(style.Fill.Color) != 2 { + if len(style.Fill.Color) != 2 || style.Fill.Shading < 0 || style.Fill.Shading > 16 { break } - var gradient xlsxGradientFill - switch style.Fill.Shading { - case 0, 1, 2, 3: - gradient.Degree = variants[style.Fill.Shading] - case 4: - gradient.Type = "path" - case 5: - gradient.Type = "path" - gradient.Bottom = 0.5 - gradient.Left = 0.5 - gradient.Right = 0.5 - gradient.Top = 0.5 + gradient := variants[style.Fill.Shading] + gradient.Stop[0].Color.RGB = getPaletteColor(style.Fill.Color[0]) + gradient.Stop[1].Color.RGB = getPaletteColor(style.Fill.Color[1]) + if len(gradient.Stop) == 3 { + gradient.Stop[2].Color.RGB = getPaletteColor(style.Fill.Color[0]) } - var stops []*xlsxGradientFillStop - for index, color := range style.Fill.Color { - var stop xlsxGradientFillStop - stop.Position = float64(index) - stop.Color.RGB = getPaletteColor(color) - stops = append(stops, &stop) - } - gradient.Stop = stops fill.GradientFill = &gradient case "pattern": if style.Fill.Pattern > 18 || style.Fill.Pattern < 0 { diff --git a/styles_test.go b/styles_test.go index 53cd7cdd..79fb7b3d 100644 --- a/styles_test.go +++ b/styles_test.go @@ -223,6 +223,13 @@ func TestUnsetConditionalFormat(t *testing.T) { func TestNewStyle(t *testing.T) { f := NewFile() + for i := 0; i < 18; i++ { + _, err := f.NewStyle(&Style{ + Fill: Fill{Type: "gradient", Color: []string{"#FFFFFF", "#4E71BE"}, Shading: i}, + }) + assert.NoError(t, err) + } + f = NewFile() styleID, err := f.NewStyle(&Style{Font: &Font{Bold: true, Italic: true, Family: "Times New Roman", Size: 36, Color: "#777777"}}) assert.NoError(t, err) styles, err := f.stylesReader()