Skip to content

Commit 41c7dd3

Browse files
committed
This closes qax-os#1993, support to set and get pivot table classic layout
- Add new field `ClassicLayout` in the `PivotTableOptions` - Add a new exported error variable `ErrPivotTableClassicLayout` - Update unit tests - Add documentation for the SetDefinedName function, ref qax-os#1015
1 parent 02189fb commit 41c7dd3

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

calc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ func TestCalcCellValue(t *testing.T) {
21262126
"=DOLLAR(1234.56,-2)": "$1,200",
21272127
"=DOLLAR(1234.56,-3)": "$1,000",
21282128
"=DOLLAR(-1234.56,3)": "($1,234.560)",
2129-
"=DOLLAR(-1234.56,-3)": "($1,000)",
2129+
"=DOLLAR(-1234.56,-3)": "($1,000)",
21302130
// DOLLARDE
21312131
"=DOLLARDE(1.01,16)": "1.0625",
21322132
// DOLLARFR

errors.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ var (
9797
// ErrPasswordLengthInvalid defined the error message on invalid password
9898
// length.
9999
ErrPasswordLengthInvalid = errors.New("password length invalid")
100+
// ErrPivotTableClassicLayout
101+
ErrPivotTableClassicLayout = errors.New("cannot enable ClassicLayout and CompactData in the same time")
100102
// ErrSave defined the error message for saving file.
101103
ErrSave = errors.New("no path defined for file, consider File.WriteTo or File.Write")
102104
// ErrSheetIdx defined the error message on receive the invalid worksheet

pivotTable.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type PivotTableOptions struct {
5151
UseAutoFormatting bool
5252
PageOverThenDown bool
5353
MergeItem bool
54+
ClassicLayout bool
5455
CompactData bool
5556
ShowError bool
5657
ShowRowHeaders bool
@@ -220,6 +221,9 @@ func (f *File) parseFormatPivotTableSet(opts *PivotTableOptions) (*xlsxWorksheet
220221
if !ok {
221222
return dataSheet, pivotTableSheetPath, ErrSheetNotExist{pivotTableSheetName}
222223
}
224+
if opts.CompactData && opts.ClassicLayout {
225+
return nil, "", ErrPivotTableClassicLayout
226+
}
223227
return dataSheet, pivotTableSheetPath, err
224228
}
225229

@@ -352,6 +356,7 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
352356
MergeItem: &opts.MergeItem,
353357
CreatedVersion: pivotTableVersion,
354358
CompactData: &opts.CompactData,
359+
GridDropZones: opts.ClassicLayout,
355360
ShowError: &opts.ShowError,
356361
FieldPrintTitles: opts.FieldPrintTitles,
357362
ItemPrintTitles: opts.ItemPrintTitles,
@@ -387,6 +392,12 @@ func (f *File) addPivotTable(cacheID, pivotTableID int, opts *PivotTableOptions)
387392
if pt.Name == "" {
388393
pt.Name = fmt.Sprintf("PivotTable%d", pivotTableID)
389394
}
395+
396+
// set classic layout
397+
if opts.ClassicLayout {
398+
pt.Compact, pt.CompactData = boolPtr(false), boolPtr(false)
399+
}
400+
390401
// pivot fields
391402
_ = f.addPivotFields(&pt, opts)
392403

@@ -537,6 +548,14 @@ func (f *File) addPivotColFields(pt *xlsxPivotTableDefinition, opts *PivotTableO
537548
return err
538549
}
539550

551+
// setClassicLayout provides a method to set classic layout for pivot table by
552+
// setting Compact and Outline to false.
553+
func (fld *xlsxPivotField) setClassicLayout(classicLayout bool) {
554+
if classicLayout {
555+
fld.Compact, fld.Outline = boolPtr(false), boolPtr(false)
556+
}
557+
}
558+
540559
// addPivotFields create pivot fields based on the column order of the first
541560
// row in the data region by given pivot table definition and option.
542561
func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOptions) error {
@@ -554,8 +573,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
554573
} else {
555574
items = append(items, &xlsxItem{T: "default"})
556575
}
557-
558-
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
576+
fld := &xlsxPivotField{
559577
Name: f.getPivotTableFieldName(name, opts.Rows),
560578
Axis: "axisRow",
561579
DataField: inPivotTableField(opts.Data, name) != -1,
@@ -568,11 +586,13 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
568586
Count: len(items),
569587
Item: items,
570588
},
571-
})
589+
}
590+
fld.setClassicLayout(opts.ClassicLayout)
591+
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
572592
continue
573593
}
574594
if inPivotTableField(opts.Filter, name) != -1 {
575-
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
595+
fld := &xlsxPivotField{
576596
Axis: "axisPage",
577597
DataField: inPivotTableField(opts.Data, name) != -1,
578598
Name: f.getPivotTableFieldName(name, opts.Columns),
@@ -582,7 +602,9 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
582602
{T: "default"},
583603
},
584604
},
585-
})
605+
}
606+
fld.setClassicLayout(opts.ClassicLayout)
607+
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
586608
continue
587609
}
588610
if inPivotTableField(opts.Columns, name) != -1 {
@@ -593,7 +615,7 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
593615
} else {
594616
items = append(items, &xlsxItem{T: "default"})
595617
}
596-
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
618+
fld := &xlsxPivotField{
597619
Name: f.getPivotTableFieldName(name, opts.Columns),
598620
Axis: "axisCol",
599621
DataField: inPivotTableField(opts.Data, name) != -1,
@@ -606,16 +628,22 @@ func (f *File) addPivotFields(pt *xlsxPivotTableDefinition, opts *PivotTableOpti
606628
Count: len(items),
607629
Item: items,
608630
},
609-
})
631+
}
632+
fld.setClassicLayout(opts.ClassicLayout)
633+
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
610634
continue
611635
}
612636
if inPivotTableField(opts.Data, name) != -1 {
613-
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{
637+
fld := &xlsxPivotField{
614638
DataField: true,
615-
})
639+
}
640+
fld.setClassicLayout(opts.ClassicLayout)
641+
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
616642
continue
617643
}
618-
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, &xlsxPivotField{})
644+
fld := &xlsxPivotField{}
645+
fld.setClassicLayout(opts.ClassicLayout)
646+
pt.PivotFields.PivotField = append(pt.PivotFields.PivotField, fld)
619647
}
620648
return err
621649
}
@@ -847,6 +875,7 @@ func (f *File) getPivotTable(sheet, pivotTableXML, pivotCacheRels string) (Pivot
847875
DataRange: fmt.Sprintf("%s!%s", pc.CacheSource.WorksheetSource.Sheet, pc.CacheSource.WorksheetSource.Ref),
848876
PivotTableRange: fmt.Sprintf("%s!%s", sheet, pt.Location.Ref),
849877
Name: pt.Name,
878+
ClassicLayout: pt.GridDropZones,
850879
FieldPrintTitles: pt.FieldPrintTitles,
851880
ItemPrintTitles: pt.ItemPrintTitles,
852881
}

pivotTable_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ func TestPivotTable(t *testing.T) {
3838
RowGrandTotals: true,
3939
ColGrandTotals: true,
4040
ShowDrill: true,
41+
ClassicLayout: true,
42+
ShowError: true,
4143
ShowRowHeaders: true,
4244
ShowColHeaders: true,
4345
ShowLastColumn: true,
44-
ShowError: true,
45-
ItemPrintTitles: true,
4646
FieldPrintTitles: true,
47+
ItemPrintTitles: true,
4748
PivotTableStyleName: "PivotStyleLight16",
4849
}
4950
assert.NoError(t, f.AddPivotTable(expected))
@@ -265,18 +266,25 @@ func TestPivotTable(t *testing.T) {
265266
assert.NoError(t, err)
266267

267268
// Test add pivot table with invalid sheet name
268-
assert.EqualError(t, f.AddPivotTable(&PivotTableOptions{
269+
assert.Error(t, f.AddPivotTable(&PivotTableOptions{
269270
DataRange: "Sheet:1!A1:E31",
270271
PivotTableRange: "Sheet:1!G2:M34",
271272
Rows: []PivotTableField{{Data: "Year"}},
272-
}), ErrSheetNameInvalid.Error())
273+
}), ErrSheetNameInvalid)
274+
// Test add pivot table with enable ClassicLayout and CompactData in the same time
275+
assert.Error(t, f.AddPivotTable(&PivotTableOptions{
276+
DataRange: "Sheet1!A1:E31",
277+
PivotTableRange: "Sheet1!G2:M34",
278+
CompactData: true,
279+
ClassicLayout: true,
280+
}), ErrPivotTableClassicLayout)
273281
// Test delete pivot table with not exists worksheet
274282
assert.EqualError(t, f.DeletePivotTable("SheetN", "PivotTable1"), "sheet SheetN does not exist")
275283
// Test delete pivot table with not exists pivot table name
276284
assert.EqualError(t, f.DeletePivotTable("Sheet1", "PivotTableN"), "table PivotTableN does not exist")
277285
// Test adjust range with invalid range
278286
_, _, err = f.adjustRange("")
279-
assert.EqualError(t, err, ErrParameterRequired.Error())
287+
assert.Error(t, err, ErrParameterRequired)
280288
// Test adjust range with incorrect range
281289
_, _, err = f.adjustRange("sheet1!")
282290
assert.EqualError(t, err, "parameter is invalid")

sheet.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,24 @@ func (f *File) GetPageLayout(sheet string) (PageLayoutOptions, error) {
16571657
// Comment: "defined name comment",
16581658
// Scope: "Sheet2",
16591659
// })
1660+
//
1661+
// If you fill the RefersTo property with only one columns range without a
1662+
// comma, it will work as "Columns to repeat at left" only. For example:
1663+
//
1664+
// err := f.SetDefinedName(&excelize.DefinedName{
1665+
// Name: "_xlnm.Print_Titles",
1666+
// RefersTo: "Sheet1!$A:$A",
1667+
// Scope: "Sheet1",
1668+
// })
1669+
//
1670+
// If you fill the RefersTo property with only one rows range without a comma,
1671+
// it will work as "Rows to repeat at top" only. For example:
1672+
//
1673+
// err := f.SetDefinedName(&excelize.DefinedName{
1674+
// Name: "_xlnm.Print_Titles",
1675+
// RefersTo: "Sheet1!$1:$1",
1676+
// Scope: "Sheet1",
1677+
// })
16601678
func (f *File) SetDefinedName(definedName *DefinedName) error {
16611679
if definedName.Name == "" || definedName.RefersTo == "" {
16621680
return ErrParameterInvalid

0 commit comments

Comments
 (0)