summaryrefslogtreecommitdiff
path: root/comment.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2022-09-29 22:00:21 +0800
committerxuri <xuri.me@gmail.com>2022-09-29 22:04:50 +0800
commit53a495563a2b9acf09ae45eae05d5f33aa242a87 (patch)
tree36ae707b1e38e755063aec7c13e878126a128c84 /comment.go
parentefcf599dfe2ec25f10c4d55513a5648addfe989b (diff)
This closes #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 #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
Diffstat (limited to 'comment.go')
-rw-r--r--comment.go41
1 files changed, 21 insertions, 20 deletions
diff --git a/comment.go b/comment.go
index 41f91bb..3d08324 100644
--- a/comment.go
+++ b/comment.go
@@ -23,15 +23,15 @@ import (
"strings"
)
-// parseFormatCommentsSet provides a function to parse the format settings of
+// parseCommentOptions provides a function to parse the format settings of
// the comment with default value.
-func parseFormatCommentsSet(formatSet string) (*formatComment, error) {
- format := formatComment{
+func parseCommentOptions(opts string) (*commentOptions, error) {
+ options := commentOptions{
Author: "Author:",
Text: " ",
}
- err := json.Unmarshal([]byte(formatSet), &format)
- return &format, err
+ err := json.Unmarshal([]byte(opts), &options)
+ return &options, err
}
// GetComments retrieves all comments and returns a map of worksheet name to
@@ -93,8 +93,8 @@ func (f *File) getSheetComments(sheetFile string) string {
// comment in Sheet1!$A$30:
//
// err := f.AddComment("Sheet1", "A30", `{"author":"Excelize: ","text":"This is a comment."}`)
-func (f *File) AddComment(sheet, cell, format string) error {
- formatSet, err := parseFormatCommentsSet(format)
+func (f *File) AddComment(sheet, cell, opts string) error {
+ options, err := parseCommentOptions(opts)
if err != nil {
return err
}
@@ -123,19 +123,19 @@ func (f *File) AddComment(sheet, cell, format string) error {
}
commentsXML := "xl/comments" + strconv.Itoa(commentID) + ".xml"
var colCount int
- for i, l := range strings.Split(formatSet.Text, "\n") {
+ for i, l := range strings.Split(options.Text, "\n") {
if ll := len(l); ll > colCount {
if i == 0 {
- ll += len(formatSet.Author)
+ ll += len(options.Author)
}
colCount = ll
}
}
- err = f.addDrawingVML(commentID, drawingVML, cell, strings.Count(formatSet.Text, "\n")+1, colCount)
+ err = f.addDrawingVML(commentID, drawingVML, cell, strings.Count(options.Text, "\n")+1, colCount)
if err != nil {
return err
}
- f.addComment(commentsXML, cell, formatSet)
+ f.addComment(commentsXML, cell, options)
f.addContentTypePart(commentID, "comments")
return err
}
@@ -144,11 +144,12 @@ func (f *File) AddComment(sheet, cell, format string) error {
// worksheet name. For example, delete the comment in Sheet1!$A$30:
//
// err := f.DeleteComment("Sheet1", "A30")
-func (f *File) DeleteComment(sheet, cell string) (err error) {
+func (f *File) DeleteComment(sheet, cell string) error {
+ var err error
sheetXMLPath, ok := f.getSheetXMLPath(sheet)
if !ok {
err = newNoExistSheetError(sheet)
- return
+ return err
}
commentsXML := f.getSheetComments(filepath.Base(sheetXMLPath))
if !strings.HasPrefix(commentsXML, "/") {
@@ -173,7 +174,7 @@ func (f *File) DeleteComment(sheet, cell string) (err error) {
}
f.Comments[commentsXML] = comments
}
- return
+ return err
}
// addDrawingVML provides a function to create comment as
@@ -279,9 +280,9 @@ func (f *File) addDrawingVML(commentID int, drawingVML, cell string, lineCount,
// addComment provides a function to create chart as xl/comments%d.xml by
// given cell and format sets.
-func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
- a := formatSet.Author
- t := formatSet.Text
+func (f *File) addComment(commentsXML, cell string, opts *commentOptions) {
+ a := opts.Author
+ t := opts.Text
if len(a) > MaxFieldLength {
a = a[:MaxFieldLength]
}
@@ -291,10 +292,10 @@ func (f *File) addComment(commentsXML, cell string, formatSet *formatComment) {
comments := f.commentsReader(commentsXML)
authorID := 0
if comments == nil {
- comments = &xlsxComments{Authors: xlsxAuthor{Author: []string{formatSet.Author}}}
+ comments = &xlsxComments{Authors: xlsxAuthor{Author: []string{opts.Author}}}
}
- if inStrSlice(comments.Authors.Author, formatSet.Author, true) == -1 {
- comments.Authors.Author = append(comments.Authors.Author, formatSet.Author)
+ if inStrSlice(comments.Authors.Author, opts.Author, true) == -1 {
+ comments.Authors.Author = append(comments.Authors.Author, opts.Author)
authorID = len(comments.Authors.Author) - 1
}
defaultFont := f.GetDefaultFont()