Bläddra i källkod

Add support to flip outline summaries

This adds outlinePr support, with the summaryBelow attribute
which defaults to true.

Closes #304

Signed-off-by: Michael Harris
Harris 7 år sedan
förälder
incheckning
faa7285a4f
3 ändrade filer med 35 tillägg och 0 borttagningar
  1. 20 0
      sheetpr.go
  2. 8 0
      sheetpr_test.go
  3. 7 0
      xmlWorksheet.go

+ 20 - 0
sheetpr.go

@@ -31,8 +31,26 @@ type (
 	FitToPage bool
 	// AutoPageBreaks is a SheetPrOption
 	AutoPageBreaks bool
+	// OutlineSummaryBelow is an outlinePr, within SheetPr option
+	OutlineSummaryBelow bool
 )
 
+func (o OutlineSummaryBelow) setSheetPrOption(pr *xlsxSheetPr) {
+	if pr.OutlinePr == nil {
+		pr.OutlinePr = new(xlsxOutlinePr)
+	}
+	pr.OutlinePr.SummaryBelow = bool(o)
+}
+
+func (o *OutlineSummaryBelow) getSheetPrOption(pr *xlsxSheetPr) {
+	// Excel default: true
+	if pr == nil || pr.OutlinePr == nil {
+		*o = true
+		return
+	}
+	*o = OutlineSummaryBelow(defaultTrue(&pr.OutlinePr.SummaryBelow))
+}
+
 func (o CodeName) setSheetPrOption(pr *xlsxSheetPr) {
 	pr.CodeName = string(o)
 }
@@ -115,6 +133,7 @@ func (o *AutoPageBreaks) getSheetPrOption(pr *xlsxSheetPr) {
 //   Published(bool)
 //   FitToPage(bool)
 //   AutoPageBreaks(bool)
+//   OutlineSummaryBelow(bool)
 func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
 	sheet := f.workSheetReader(name)
 	pr := sheet.SheetPr
@@ -137,6 +156,7 @@ func (f *File) SetSheetPrOptions(name string, opts ...SheetPrOption) error {
 //   Published(bool)
 //   FitToPage(bool)
 //   AutoPageBreaks(bool)
+//   OutlineSummaryBelow(bool)
 func (f *File) GetSheetPrOptions(name string, opts ...SheetPrOptionPtr) error {
 	sheet := f.workSheetReader(name)
 	pr := sheet.SheetPr

+ 8 - 0
sheetpr_test.go

@@ -15,6 +15,7 @@ var _ = []excelize.SheetPrOption{
 	excelize.Published(false),
 	excelize.FitToPage(true),
 	excelize.AutoPageBreaks(true),
+	excelize.OutlineSummaryBelow(true),
 }
 
 var _ = []excelize.SheetPrOptionPtr{
@@ -23,6 +24,7 @@ var _ = []excelize.SheetPrOptionPtr{
 	(*excelize.Published)(nil),
 	(*excelize.FitToPage)(nil),
 	(*excelize.AutoPageBreaks)(nil),
+	(*excelize.OutlineSummaryBelow)(nil),
 }
 
 func ExampleFile_SetSheetPrOptions() {
@@ -35,6 +37,7 @@ func ExampleFile_SetSheetPrOptions() {
 		excelize.Published(false),
 		excelize.FitToPage(true),
 		excelize.AutoPageBreaks(true),
+		excelize.OutlineSummaryBelow(false),
 	); err != nil {
 		panic(err)
 	}
@@ -51,6 +54,7 @@ func ExampleFile_GetSheetPrOptions() {
 		published                         excelize.Published
 		fitToPage                         excelize.FitToPage
 		autoPageBreaks                    excelize.AutoPageBreaks
+		outlineSummaryBelow               excelize.OutlineSummaryBelow
 	)
 
 	if err := xl.GetSheetPrOptions(sheet,
@@ -59,6 +63,7 @@ func ExampleFile_GetSheetPrOptions() {
 		&published,
 		&fitToPage,
 		&autoPageBreaks,
+		&outlineSummaryBelow,
 	); err != nil {
 		panic(err)
 	}
@@ -68,6 +73,7 @@ func ExampleFile_GetSheetPrOptions() {
 	fmt.Println("- published:", published)
 	fmt.Println("- fitToPage:", fitToPage)
 	fmt.Println("- autoPageBreaks:", autoPageBreaks)
+	fmt.Println("- outlineSummaryBelow:", outlineSummaryBelow)
 	// Output:
 	// Defaults:
 	// - codeName: ""
@@ -75,6 +81,7 @@ func ExampleFile_GetSheetPrOptions() {
 	// - published: true
 	// - fitToPage: false
 	// - autoPageBreaks: false
+	// - outlineSummaryBelow: true
 }
 
 func TestSheetPrOptions(t *testing.T) {
@@ -88,6 +95,7 @@ func TestSheetPrOptions(t *testing.T) {
 		{new(excelize.Published), excelize.Published(false)},
 		{new(excelize.FitToPage), excelize.FitToPage(true)},
 		{new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)},
+		{new(excelize.OutlineSummaryBelow), excelize.OutlineSummaryBelow(false)},
 	} {
 		opt := test.nonDefault
 		t.Logf("option %T", opt)

+ 7 - 0
xmlWorksheet.go

@@ -211,6 +211,13 @@ type xlsxSheetPr struct {
 	TransitionEntry                   bool             `xml:"transitionEntry,attr,omitempty"`
 	TabColor                          *xlsxTabColor    `xml:"tabColor,omitempty"`
 	PageSetUpPr                       *xlsxPageSetUpPr `xml:"pageSetUpPr,omitempty"`
+	OutlinePr                         *xlsxOutlinePr   `xml:"outlinePr,omitempty"`
+}
+
+// xlsxOutlinePr maps to the outlinePr element
+// SummaryBelow allows you to adjust the direction of grouper controls
+type xlsxOutlinePr struct {
+	SummaryBelow bool `xml:"summaryBelow,attr"`
 }
 
 // xlsxPageSetUpPr directly maps the pageSetupPr element in the namespace