summaryrefslogtreecommitdiff
path: root/shape.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 /shape.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 'shape.go')
-rw-r--r--shape.go58
1 files changed, 29 insertions, 29 deletions
diff --git a/shape.go b/shape.go
index 4fca348..eca354f 100644
--- a/shape.go
+++ b/shape.go
@@ -17,21 +17,21 @@ import (
"strings"
)
-// parseFormatShapeSet provides a function to parse the format settings of the
+// parseShapeOptions provides a function to parse the format settings of the
// shape with default value.
-func parseFormatShapeSet(formatSet string) (*formatShape, error) {
- format := formatShape{
+func parseShapeOptions(opts string) (*shapeOptions, error) {
+ options := shapeOptions{
Width: 160,
Height: 160,
- Format: formatPicture{
+ Format: pictureOptions{
FPrintsWithSheet: true,
XScale: 1,
YScale: 1,
},
- Line: formatLine{Width: 1},
+ Line: lineOptions{Width: 1},
}
- err := json.Unmarshal([]byte(formatSet), &format)
- return &format, err
+ err := json.Unmarshal([]byte(opts), &options)
+ return &options, err
}
// AddShape provides the method to add shape in a sheet by given worksheet
@@ -277,8 +277,8 @@ func parseFormatShapeSet(formatSet string) (*formatShape, error) {
// wavy
// wavyHeavy
// wavyDbl
-func (f *File) AddShape(sheet, cell, format string) error {
- formatSet, err := parseFormatShapeSet(format)
+func (f *File) AddShape(sheet, cell, opts string) error {
+ options, err := parseShapeOptions(opts)
if err != nil {
return err
}
@@ -305,7 +305,7 @@ func (f *File) AddShape(sheet, cell, format string) error {
f.addSheetDrawing(sheet, rID)
f.addSheetNameSpace(sheet, SourceRelationship)
}
- err = f.addDrawingShape(sheet, drawingXML, cell, formatSet)
+ err = f.addDrawingShape(sheet, drawingXML, cell, options)
if err != nil {
return err
}
@@ -315,7 +315,7 @@ func (f *File) AddShape(sheet, cell, format string) error {
// addDrawingShape provides a function to add preset geometry by given sheet,
// drawingXMLand format sets.
-func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *formatShape) error {
+func (f *File) addDrawingShape(sheet, drawingXML, cell string, opts *shapeOptions) error {
fromCol, fromRow, err := CellNameToCoordinates(cell)
if err != nil {
return err
@@ -344,19 +344,19 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
"wavyDbl": true,
}
- width := int(float64(formatSet.Width) * formatSet.Format.XScale)
- height := int(float64(formatSet.Height) * formatSet.Format.YScale)
+ width := int(float64(opts.Width) * opts.Format.XScale)
+ height := int(float64(opts.Height) * opts.Format.YScale)
- colStart, rowStart, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, colIdx, rowIdx, formatSet.Format.OffsetX, formatSet.Format.OffsetY,
+ colStart, rowStart, colEnd, rowEnd, x2, y2 := f.positionObjectPixels(sheet, colIdx, rowIdx, opts.Format.OffsetX, opts.Format.OffsetY,
width, height)
content, cNvPrID := f.drawingParser(drawingXML)
twoCellAnchor := xdrCellAnchor{}
- twoCellAnchor.EditAs = formatSet.Format.Positioning
+ twoCellAnchor.EditAs = opts.Format.Positioning
from := xlsxFrom{}
from.Col = colStart
- from.ColOff = formatSet.Format.OffsetX * EMU
+ from.ColOff = opts.Format.OffsetX * EMU
from.Row = rowStart
- from.RowOff = formatSet.Format.OffsetY * EMU
+ from.RowOff = opts.Format.OffsetY * EMU
to := xlsxTo{}
to.Col = colEnd
to.ColOff = x2 * EMU
@@ -365,7 +365,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
twoCellAnchor.From = &from
twoCellAnchor.To = &to
shape := xdrSp{
- Macro: formatSet.Macro,
+ Macro: opts.Macro,
NvSpPr: &xdrNvSpPr{
CNvPr: &xlsxCNvPr{
ID: cNvPrID,
@@ -377,13 +377,13 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
},
SpPr: &xlsxSpPr{
PrstGeom: xlsxPrstGeom{
- Prst: formatSet.Type,
+ Prst: opts.Type,
},
},
Style: &xdrStyle{
- LnRef: setShapeRef(formatSet.Color.Line, 2),
- FillRef: setShapeRef(formatSet.Color.Fill, 1),
- EffectRef: setShapeRef(formatSet.Color.Effect, 0),
+ LnRef: setShapeRef(opts.Color.Line, 2),
+ FillRef: setShapeRef(opts.Color.Fill, 1),
+ EffectRef: setShapeRef(opts.Color.Effect, 0),
FontRef: &aFontRef{
Idx: "minor",
SchemeClr: &attrValString{
@@ -401,13 +401,13 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
},
},
}
- if formatSet.Line.Width != 1 {
+ if opts.Line.Width != 1 {
shape.SpPr.Ln = xlsxLineProperties{
- W: f.ptToEMUs(formatSet.Line.Width),
+ W: f.ptToEMUs(opts.Line.Width),
}
}
- if len(formatSet.Paragraph) < 1 {
- formatSet.Paragraph = []formatShapeParagraph{
+ if len(opts.Paragraph) < 1 {
+ opts.Paragraph = []shapeParagraphOptions{
{
Font: Font{
Bold: false,
@@ -421,7 +421,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
},
}
}
- for _, p := range formatSet.Paragraph {
+ for _, p := range opts.Paragraph {
u := p.Font.Underline
_, ok := textUnderlineType[u]
if !ok {
@@ -460,8 +460,8 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
}
twoCellAnchor.Sp = &shape
twoCellAnchor.ClientData = &xdrClientData{
- FLocksWithSheet: formatSet.Format.FLocksWithSheet,
- FPrintsWithSheet: formatSet.Format.FPrintsWithSheet,
+ FLocksWithSheet: opts.Format.FLocksWithSheet,
+ FPrintsWithSheet: opts.Format.FPrintsWithSheet,
}
content.TwoCellAnchor = append(content.TwoCellAnchor, &twoCellAnchor)
f.Drawings.Store(drawingXML, content)