Skip to content

Commit 53a4955

Browse files
committed
This closes qax-os#1358, made a refactor with breaking changes, see details:
This made a refactor with breaking changes: Motivation and Context When I decided to add set horizontal centered support for this library to resolve qax-os#1358, the reason I made this huge breaking change was: - There are too many exported types for set sheet view, properties, and format properties, although a function using the functional options pattern can be optimized by returning an anonymous function, these types or property set or get function has no binding categorization, so I change these functions like `SetAppProps` to accept a pointer of options structure. - Users can not easily find out which properties should be in the `SetSheetPrOptions` or `SetSheetFormatPr` categories - Nested properties cannot proceed modify easily Introduce 5 new export data types: `HeaderFooterOptions`, `PageLayoutMarginsOptions`, `PageLayoutOptions`, `SheetPropsOptions`, and `ViewOptions` Rename 4 exported data types: - Rename `PivotTableOption` to `PivotTableOptions` - Rename `FormatHeaderFooter` to `HeaderFooterOptions` - Rename `FormatSheetProtection` to `SheetProtectionOptions` - Rename `SparklineOption` to `SparklineOptions` Remove 54 exported types: `AutoPageBreaks`, `BaseColWidth`, `BlackAndWhite`, `CodeName`, `CustomHeight`, `Date1904`, `DefaultColWidth`, `DefaultGridColor`, `DefaultRowHeight`, `EnableFormatConditionsCalculation`, `FilterPrivacy`, `FirstPageNumber`, `FitToHeight`, `FitToPage`, `FitToWidth`, `OutlineSummaryBelow`, `PageLayoutOption`, `PageLayoutOptionPtr`, `PageLayoutOrientation`, `PageLayoutPaperSize`, `PageLayoutScale`, `PageMarginBottom`, `PageMarginFooter`, `PageMarginHeader`, `PageMarginLeft`, `PageMarginRight`, `PageMarginsOptions`, `PageMarginsOptionsPtr`, `PageMarginTop`, `Published`, `RightToLeft`, `SheetFormatPrOptions`, `SheetFormatPrOptionsPtr`, `SheetPrOption`, `SheetPrOptionPtr`, `SheetViewOption`, `SheetViewOptionPtr`, `ShowFormulas`, `ShowGridLines`, `ShowRowColHeaders`, `ShowRuler`, `ShowZeros`, `TabColorIndexed`, `TabColorRGB`, `TabColorTheme`, `TabColorTint`, `ThickBottom`, `ThickTop`, `TopLeftCell`, `View`, `WorkbookPrOption`, `WorkbookPrOptionPtr`, `ZeroHeight` and `ZoomScale` Remove 2 exported constants: `OrientationPortrait` and `OrientationLandscape` Change 8 functions: - Change the `func (f *File) SetPageLayout(sheet string, opts ...PageLayoutOption) error` to `func (f *File) SetPageLayout(sheet string, opts *PageLayoutOptions) error` - Change the `func (f *File) GetPageLayout(sheet string, opts ...PageLayoutOptionPtr) error` to `func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error)` - Change the `func (f *File) SetPageMargins(sheet string, opts ...PageMarginsOptions) error` to `func (f *File) SetPageMargins(sheet string, opts *PageLayoutMarginsOptions) error` - Change the `func (f *File) GetPageMargins(sheet string, opts ...PageMarginsOptionsPtr) error` to `func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error)` - Change the `func (f *File) SetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOption) error` to `func (f *File) SetSheetView(sheet string, viewIndex int, opts *ViewOptions) error` - Change the `func (f *File) GetSheetViewOptions(sheet string, viewIndex int, opts ...SheetViewOptionPtr) error` to `func (f *File) GetSheetView(sheet string, viewIndex int) (ViewOptions, error)` - Change the `func (f *File) SetWorkbookPrOptions(opts ...WorkbookPrOption) error` to `func (f *File) SetWorkbookProps(opts *WorkbookPropsOptions) error` - Change the `func (f *File) GetWorkbookPrOptions(opts ...WorkbookPrOptionPtr) error` to `func (f *File) GetWorkbookProps() (WorkbookPropsOptions, error)` Introduce new function to instead of existing functions: - New function `func (f *File) SetSheetProps(sheet string, opts *SheetPropsOptions) error` instead of `func (f *File) SetSheetPrOptions(sheet string, opts ...SheetPrOption) error` and `func (f *File) SetSheetFormatPr(sheet string, opts ...SheetFormatPrOption
1 parent efcf599 commit 53a4955

33 files changed

+1343
-2622
lines changed

chart.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -469,30 +469,30 @@ var (
469469
}
470470
)
471471

472-
// parseFormatChartSet provides a function to parse the format settings of the
472+
// parseChartOptions provides a function to parse the format settings of the
473473
// chart with default value.
474-
func parseFormatChartSet(formatSet string) (*formatChart, error) {
475-
format := formatChart{
476-
Dimension: formatChartDimension{
474+
func parseChartOptions(opts string) (*chartOptions, error) {
475+
options := chartOptions{
476+
Dimension: chartDimensionOptions{
477477
Width: 480,
478478
Height: 290,
479479
},
480-
Format: formatPicture{
480+
Format: pictureOptions{
481481
FPrintsWithSheet: true,
482482
XScale: 1,
483483
YScale: 1,
484484
},
485-
Legend: formatChartLegend{
485+
Legend: chartLegendOptions{
486486
Position: "bottom",
487487
},
488-
Title: formatChartTitle{
488+
Title: chartTitleOptions{
489489
Name: " ",
490490
},
491491
VaryColors: true,
492492
ShowBlanksAs: "gap",
493493
}
494-
err := json.Unmarshal([]byte(formatSet), &format)
495-
return &format, err
494+
err := json.Unmarshal([]byte(opts), &options)
495+
return &options, err
496496
}
497497

498498
// AddChart provides the method to add chart in a sheet by given chart format
@@ -881,13 +881,13 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
881881
// fmt.Println(err)
882882
// }
883883
// }
884-
func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
884+
func (f *File) AddChart(sheet, cell, opts string, combo ...string) error {
885885
// Read sheet data.
886886
ws, err := f.workSheetReader(sheet)
887887
if err != nil {
888888
return err
889889
}
890-
formatSet, comboCharts, err := f.getFormatChart(format, combo)
890+
options, comboCharts, err := f.getChartOptions(opts, combo)
891891
if err != nil {
892892
return err
893893
}
@@ -898,11 +898,11 @@ func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
898898
drawingID, drawingXML = f.prepareDrawing(ws, drawingID, sheet, drawingXML)
899899
drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
900900
drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
901-
err = f.addDrawingChart(sheet, drawingXML, cell, formatSet.Dimension.Width, formatSet.Dimension.Height, drawingRID, &formatSet.Format)
901+
err = f.addDrawingChart(sheet, drawingXML, cell, options.Dimension.Width, options.Dimension.Height, drawingRID, &options.Format)
902902
if err != nil {
903903
return err
904904
}
905-
f.addChart(formatSet, comboCharts)
905+
f.addChart(options, comboCharts)
906906
f.addContentTypePart(chartID, "chart")
907907
f.addContentTypePart(drawingID, "drawings")
908908
f.addSheetNameSpace(sheet, SourceRelationship)
@@ -913,12 +913,12 @@ func (f *File) AddChart(sheet, cell, format string, combo ...string) error {
913913
// format set (such as offset, scale, aspect ratio setting and print settings)
914914
// and properties set. In Excel a chartsheet is a worksheet that only contains
915915
// a chart.
916-
func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
916+
func (f *File) AddChartSheet(sheet, opts string, combo ...string) error {
917917
// Check if the worksheet already exists
918918
if f.GetSheetIndex(sheet) != -1 {
919919
return ErrExistsWorksheet
920920
}
921-
formatSet, comboCharts, err := f.getFormatChart(format, combo)
921+
options, comboCharts, err := f.getChartOptions(opts, combo)
922922
if err != nil {
923923
return err
924924
}
@@ -945,8 +945,8 @@ func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
945945
f.prepareChartSheetDrawing(&cs, drawingID, sheet)
946946
drawingRels := "xl/drawings/_rels/drawing" + strconv.Itoa(drawingID) + ".xml.rels"
947947
drawingRID := f.addRels(drawingRels, SourceRelationshipChart, "../charts/chart"+strconv.Itoa(chartID)+".xml", "")
948-
f.addSheetDrawingChart(drawingXML, drawingRID, &formatSet.Format)
949-
f.addChart(formatSet, comboCharts)
948+
f.addSheetDrawingChart(drawingXML, drawingRID, &options.Format)
949+
f.addChart(options, comboCharts)
950950
f.addContentTypePart(chartID, "chart")
951951
f.addContentTypePart(sheetID, "chartsheet")
952952
f.addContentTypePart(drawingID, "drawings")
@@ -960,45 +960,45 @@ func (f *File) AddChartSheet(sheet, format string, combo ...string) error {
960960
return err
961961
}
962962

963-
// getFormatChart provides a function to check format set of the chart and
963+
// getChartOptions provides a function to check format set of the chart and
964964
// create chart format.
965-
func (f *File) getFormatChart(format string, combo []string) (*formatChart, []*formatChart, error) {
966-
var comboCharts []*formatChart
967-
formatSet, err := parseFormatChartSet(format)
965+
func (f *File) getChartOptions(opts string, combo []string) (*chartOptions, []*chartOptions, error) {
966+
var comboCharts []*chartOptions
967+
options, err := parseChartOptions(opts)
968968
if err != nil {
969-
return formatSet, comboCharts, err
969+
return options, comboCharts, err
970970
}
971971
for _, comboFormat := range combo {
972-
comboChart, err := parseFormatChartSet(comboFormat)
972+
comboChart, err := parseChartOptions(comboFormat)
973973
if err != nil {
974-
return formatSet, comboCharts, err
974+
return options, comboCharts, err
975975
}
976976
if _, ok := chartValAxNumFmtFormatCode[comboChart.Type]; !ok {
977-
return formatSet, comboCharts, newUnsupportedChartType(comboChart.Type)
977+
return options, comboCharts, newUnsupportedChartType(comboChart.Type)
978978
}
979979
comboCharts = append(comboCharts, comboChart)
980980
}
981-
if _, ok := chartValAxNumFmtFormatCode[formatSet.Type]; !ok {
982-
return formatSet, comboCharts, newUnsupportedChartType(formatSet.Type)
981+
if _, ok := chartValAxNumFmtFormatCode[options.Type]; !ok {
982+
return options, comboCharts, newUnsupportedChartType(options.Type)
983983
}
984-
return formatSet, comboCharts, err
984+
return options, comboCharts, err
985985
}
986986

987987
// DeleteChart provides a function to delete chart in spreadsheet by given
988988
// worksheet name and cell reference.
989-
func (f *File) DeleteChart(sheet, cell string) (err error) {
989+
func (f *File) DeleteChart(sheet, cell string) error {
990990
col, row, err := CellNameToCoordinates(cell)
991991
if err != nil {
992-
return
992+
return err
993993
}
994994
col--
995995
row--
996996
ws, err := f.workSheetReader(sheet)
997997
if err != nil {
998-
return
998+
return err
999999
}
10001000
if ws.Drawing == nil {
1001-
return
1001+
return err
10021002
}
10031003
drawingXML := strings.ReplaceAll(f.getSheetRelationshipsTargetByID(sheet, ws.Drawing.RID), "..", "xl")
10041004
return f.deleteDrawing(col, row, drawingXML, "Chart")

comment.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import (
2323
"strings"
2424
)
2525

26-
// parseFormatCommentsSet provides a function to parse the format settings of
26+
// parseCommentOptions provides a function to parse the format settings of
2727
// the comment with default value.
28-
func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
29-
format := formatComment{
28+
func parseCommentOptions(opts string) (*commentOptions, error) {
29+
options := commentOptions{
3030
Author: "Author:",
3131
Text: " ",
3232
}
33-
err := json.Unmarshal([]byte(formatSet), &format)
34-
return &format, err
33+
err := json.Unmarshal([]byte(opts), &options)
34+
return &options, err
3535
}
3636

3737
// GetComments retrieves all comments and returns a map of worksheet name to
@@ -93,8 +93,8 @@ func (f *File) getSheetComments(sheetFile string) string {
9393
// comment in Sheet1!$A$30:
9494
//
9595
// err := f.AddComment("Sheet1", "A30", `{"author":"Excelize: ","text":"This is a comment."}`)
96-
func (f *File) AddComment(sheet, cell, format string) error {
97-
formatSet, err := parseFormatCommentsSet(format)
96+
func (f *File) AddComment(sheet, cell, opts string) error {
97+
options, err := parseCommentOptions(opts)
9898
if err != nil {
9999
return err
100100
}
@@ -123,19 +123,19 @@ func (f *File) AddComment(sheet, cell, format string) error {
123123
}
124124
commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
125125
var colCount int
126-
for i, l := range strings.Split(formatSet.Text, "\n") {
126+
for i, l := range strings.Split(options.Text, "\n") {
127127
if ll := len(l); ll > colCount {
128128
if i == 0 {
129-
ll += len(formatSet.Author)
129+
ll += len(options.Author)
130130
}
131131
colCount = ll
132132
}
133133
}
134-
err = f.addDrawingVML(commentID, drawingVML, cell, strings.Count(formatSet.Text, "\n")+1, colCount)
134+
err = f.addDrawingVML(commentID, drawingVML, cell, strings.Count(options.Text, "\n")+1, colCount)
135135
if err != nil {
136136
return err
137137
}
138-
f.addComment(commentsXML, cell, formatSet)
138+
f.addComment(commentsXML, cell, options)
139139
f.addContentTypePart(commentID, "comments")
140140
return err
141141
}
@@ -144,11 +144,12 @@ func (f *File) AddComment(sheet, cell, format string) error {
144144
// worksheet name. For example, delete the comment in Sheet1!$A$30:
145145
//
146146
// err := f.DeleteComment("Sheet1", "A30")
147-
func (f *File) DeleteComment(sheet, cell string) (err error) {
147+
func (f *File) DeleteComment(sheet, cell string) error {
148+
var err error
148149
sheetXMLPath, ok := f.getSheetXMLPath(sheet)
149150
if !ok {
150151
err = newNoExistSheetError(sheet)
151-
return
152+
return err
152153
}
153154
commentsXML := f.getSheetComments(filepath.Base(sheetXMLPath))
154155
if !strings.HasPrefix(commentsXML, "/") {
@@ -173,7 +174,7 @@ func (f *File) DeleteComment(sheet, cell string) (err error) {
173174
}
174175
f.Comments[commentsXML] = comments
175176
}
176-
return
177+
return err
177178
}
178179

179180
// addDrawingVML provides a function to create comment as
@@ -279,9 +280,9 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount,
279280

280281
// addComment provides a function to create chart as xl/comments%d.xml by
281282
// given cell and format sets.
282-
func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
283-
a := formatSet.Author
284-
t := formatSet.Text
283+
func (f *File) addComment(commentsXML, cell string, opts *commentOptions) {
284+
a := opts.Author
285+
t := opts.Text
285286
if len(a) > MaxFieldLength {
286287
a = a[:MaxFieldLength]
287288
}
@@ -291,10 +292,10 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
291292
comments := f.commentsReader(commentsXML)
292293
authorID := 0
293294
if comments == nil {
294-
comments = &xlsxComments{Authors: xlsxAuthor{Author: []string{formatSet.Author}}}
295+
comments = &xlsxComments{Authors: xlsxAuthor{Author: []string{opts.Author}}}
295296
}
296-
if inStrSlice(comments.Authors.Author, formatSet.Author, true) == -1 {
297-
comments.Authors.Author = append(comments.Authors.Author, formatSet.Author)
297+
if inStrSlice(comments.Authors.Author, opts.Author, true) == -1 {
298+
comments.Authors.Author = append(comments.Authors.Author, opts.Author)
298299
authorID = len(comments.Authors.Author) - 1
299300
}
300301
defaultFont := f.GetDefaultFont()

docProps.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,20 @@ import (
6464
// HyperlinksChanged: true,
6565
// AppVersion: "16.0000",
6666
// })
67-
func (f *File) SetAppProps(appProperties *AppProperties) (err error) {
67+
func (f *File) SetAppProps(appProperties *AppProperties) error {
6868
var (
6969
app *xlsxProperties
70+
err error
71+
field string
7072
fields []string
71-
output []byte
7273
immutable, mutable reflect.Value
73-
field string
74+
output []byte
7475
)
7576
app = new(xlsxProperties)
7677
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathDocPropsApp)))).
7778
Decode(app); err != nil && err != io.EOF {
7879
err = newDecodeXMLError(err)
79-
return
80+
return err
8081
}
8182
fields = []string{"Application", "ScaleCrop", "DocSecurity", "Company", "LinksUpToDate", "HyperlinksChanged", "AppVersion"}
8283
immutable, mutable = reflect.ValueOf(*appProperties), reflect.ValueOf(app).Elem()
@@ -94,7 +95,7 @@ func (f *File) SetAppProps(appProperties *AppProperties) (err error) {
9495
app.Vt = NameSpaceDocumentPropertiesVariantTypes.Value
9596
output, err = xml.Marshal(app)
9697
f.saveFileList(defaultXMLPathDocPropsApp, output)
97-
return
98+
return err
9899
}
99100

100101
// GetAppProps provides a function to get document application properties.
@@ -167,23 +168,24 @@ func (f *File) GetAppProps() (ret *AppProperties, err error) {
167168
// Language: "en-US",
168169
// Version: "1.0.0",
169170
// })
170-
func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
171+
func (f *File) SetDocProps(docProperties *DocProperties) error {
171172
var (
172173
core *decodeCoreProperties
173-
newProps *xlsxCoreProperties
174+
err error
175+
field, val string
174176
fields []string
175-
output []byte
176177
immutable, mutable reflect.Value
177-
field, val string
178+
newProps *xlsxCoreProperties
179+
output []byte
178180
)
179181

180182
core = new(decodeCoreProperties)
181183
if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLPathDocPropsCore)))).
182184
Decode(core); err != nil && err != io.EOF {
183185
err = newDecodeXMLError(err)
184-
return
186+
return err
185187
}
186-
newProps, err = &xlsxCoreProperties{
188+
newProps = &xlsxCoreProperties{
187189
Dc: NameSpaceDublinCore,
188190
Dcterms: NameSpaceDublinCoreTerms,
189191
Dcmitype: NameSpaceDublinCoreMetadataInitiative,
@@ -200,7 +202,7 @@ func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
200202
ContentStatus: core.ContentStatus,
201203
Category: core.Category,
202204
Version: core.Version,
203-
}, nil
205+
}
204206
if core.Created != nil {
205207
newProps.Created = &xlsxDcTerms{Type: core.Created.Type, Text: core.Created.Text}
206208
}
@@ -226,7 +228,7 @@ func (f *File) SetDocProps(docProperties *DocProperties) (err error) {
226228
output, err = xml.Marshal(newProps)
227229
f.saveFileList(defaultXMLPathDocPropsCore, output)
228230

229-
return
231+
return err
230232
}
231233

232234
// GetDocProps provides a function to get document core properties.

0 commit comments

Comments
 (0)