Skip to content

Commit 9a38657

Browse files
committed
This related for #720 and closes #1965, add new NumFmt field in the PivotTableField data type
- Support set and get built-in number format of the pivot table data filed cells - Update unit tests - Fixed ineffectual assignment issue
1 parent d21b598 commit 9a38657

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

calc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14459,7 +14459,7 @@ func (fn *formulaFuncs) VALUE(argsList *list.List) formulaArg {
1445914459
value, _ := decimal.Float64()
1446014460
return newNumberFormulaArg(value * percent)
1446114461
}
14462-
dateValue, timeValue, errTime, errDate := 0.0, 0.0, false, false
14462+
dateValue, timeValue, errTime := 0.0, 0.0, false
1446314463
if !isDateOnlyFmt(text) {
1446414464
h, m, s, _, _, err := strToTime(text)
1446514465
errTime = err.Type == ArgError
@@ -14468,7 +14468,7 @@ func (fn *formulaFuncs) VALUE(argsList *list.List) formulaArg {
1446814468
}
1446914469
}
1447014470
y, m, d, _, err := strToDate(text)
14471-
errDate = err.Type == ArgError
14471+
errDate := err.Type == ArgError
1447214472
if !errDate {
1447314473
dateValue = daysBetween(excelMinTime1900.Unix(), makeDate(y, time.Month(m), d)) + 1
1447414474
}

pivotTable.go

+27-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type PivotTableOptions struct {
6262
}
6363

6464
// PivotTableField directly maps the field settings of the pivot table.
65+
//
66+
// Name specifies the name of the data field. Maximum 255 characters
67+
// are allowed in data field name, excess characters will be truncated.
68+
//
6569
// Subtotal specifies the aggregation function that applies to this data
6670
// field. The default value is sum. The possible values for this attribute
6771
// are:
@@ -78,15 +82,17 @@ type PivotTableOptions struct {
7882
// Var
7983
// Varp
8084
//
81-
// Name specifies the name of the data field. Maximum 255 characters
82-
// are allowed in data field name, excess characters will be truncated.
85+
// NumFmt specifies the number format ID of the data field, this filed only
86+
// accepts built-in number format ID and does not support custom number format
87+
// expression currently.
8388
type PivotTableField struct {
8489
Compact bool
8590
Data string
8691
Name string
8792
Outline bool
8893
Subtotal string
8994
DefaultSubtotal bool
95+
NumFmt int
9096
}
9197

9298
// AddPivotTable provides the method to add pivot table by given pivot table
@@ -452,6 +458,7 @@ func (f *File) addPivotDataFields(pt *xlsxPivotTableDefinition, opts *PivotTable
452458
}
453459
dataFieldsSubtotals := f.getPivotTableFieldsSubtotal(opts.Data)
454460
dataFieldsName := f.getPivotTableFieldsName(opts.Data)
461+
dataFieldsNumFmtID := f.getPivotTableFieldsNumFmtID(opts.Data)
455462
for idx, dataField := range dataFieldsIndex {
456463
if pt.DataFields == nil {
457464
pt.DataFields = &xlsxDataFields{}
@@ -460,6 +467,7 @@ func (f *File) addPivotDataFields(pt *xlsxPivotTableDefinition, opts *PivotTable
460467
Name: dataFieldsName[idx],
461468
Fld: dataField,
462469
Subtotal: dataFieldsSubtotals[idx],
470+
NumFmtID: dataFieldsNumFmtID[idx],
463471
})
464472
}
465473

@@ -687,6 +695,22 @@ func (f *File) getPivotTableFieldName(name string, fields []PivotTableField) str
687695
return ""
688696
}
689697

698+
// getPivotTableFieldsNumFmtID prepare fields number format ID by given pivot
699+
// table fields.
700+
func (f *File) getPivotTableFieldsNumFmtID(fields []PivotTableField) []int {
701+
field := make([]int, len(fields))
702+
for idx, fld := range fields {
703+
if _, ok := builtInNumFmt[fld.NumFmt]; ok {
704+
field[idx] = fld.NumFmt
705+
continue
706+
}
707+
if (27 <= fld.NumFmt && fld.NumFmt <= 36) || (50 <= fld.NumFmt && fld.NumFmt <= 81) {
708+
field[idx] = fld.NumFmt
709+
}
710+
}
711+
return field
712+
}
713+
690714
// getPivotTableFieldOptions return options for specific field by given field name.
691715
func (f *File) getPivotTableFieldOptions(name string, fields []PivotTableField) (options PivotTableField, ok bool) {
692716
for _, field := range fields {
@@ -891,6 +915,7 @@ func (f *File) extractPivotTableFields(order []string, pt *xlsxPivotTableDefinit
891915
Data: order[field.Fld],
892916
Name: field.Name,
893917
Subtotal: cases.Title(language.English).String(field.Subtotal),
918+
NumFmt: field.NumFmtID,
894919
})
895920
}
896921
}

pivotTable_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestPivotTable(t *testing.T) {
3434
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
3535
Filter: []PivotTableField{{Data: "Region"}},
3636
Columns: []PivotTableField{{Data: "Type", DefaultSubtotal: true}},
37-
Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Summarize by Sum"}},
37+
Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Summarize by Sum", NumFmt: 38}},
3838
RowGrandTotals: true,
3939
ColGrandTotals: true,
4040
ShowDrill: true,
@@ -131,7 +131,7 @@ func TestPivotTable(t *testing.T) {
131131
PivotTableRange: "Sheet2!A1:AN17",
132132
Rows: []PivotTableField{{Data: "Month"}},
133133
Columns: []PivotTableField{{Data: "Region", DefaultSubtotal: true}, {Data: "Type", DefaultSubtotal: true}, {Data: "Year"}},
134-
Data: []PivotTableField{{Data: "Sales", Subtotal: "Min", Name: "Summarize by Min"}},
134+
Data: []PivotTableField{{Data: "Sales", Subtotal: "Min", Name: "Summarize by Min", NumFmt: 32}},
135135
RowGrandTotals: true,
136136
ColGrandTotals: true,
137137
ShowDrill: true,
@@ -151,7 +151,7 @@ func TestPivotTable(t *testing.T) {
151151
PivotTableRange: "Sheet2!A20:AR60",
152152
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Type"}},
153153
Columns: []PivotTableField{{Data: "Region", DefaultSubtotal: true}, {Data: "Year"}},
154-
Data: []PivotTableField{{Data: "Sales", Subtotal: "Product", Name: "Summarize by Product"}},
154+
Data: []PivotTableField{{Data: "Sales", Subtotal: "Product", Name: "Summarize by Product", NumFmt: 32}},
155155
RowGrandTotals: true,
156156
ColGrandTotals: true,
157157
ShowDrill: true,
@@ -171,7 +171,7 @@ func TestPivotTable(t *testing.T) {
171171
PivotTableRange: "Sheet2!A65:AJ100",
172172
Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},
173173
Columns: []PivotTableField{{Data: "Region", DefaultSubtotal: true}, {Data: "Type"}},
174-
Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Sum of Sales"}, {Data: "Sales", Subtotal: "Average", Name: "Average of Sales"}},
174+
Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Sum of Sales", NumFmt: -1}, {Data: "Sales", Subtotal: "Average", Name: "Average of Sales", NumFmt: 38}},
175175
RowGrandTotals: true,
176176
ColGrandTotals: true,
177177
ShowDrill: true,

styles.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -1902,27 +1902,25 @@ func (f *File) newFont(style *Style) (*xlsxFont, error) {
19021902

19031903
// getNumFmtID provides a function to get number format code ID.
19041904
// If given number format code does not exist, will return -1.
1905-
func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
1906-
numFmtID = -1
1905+
func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) int {
1906+
numFmtID := -1
19071907
if _, ok := builtInNumFmt[style.NumFmt]; ok {
19081908
return style.NumFmt
19091909
}
19101910
if (27 <= style.NumFmt && style.NumFmt <= 36) || (50 <= style.NumFmt && style.NumFmt <= 81) {
1911-
numFmtID = style.NumFmt
1912-
return
1911+
return style.NumFmt
19131912
}
19141913
if fmtCode, ok := currencyNumFmt[style.NumFmt]; ok {
19151914
numFmtID = style.NumFmt
19161915
if styleSheet.NumFmts != nil {
19171916
for _, numFmt := range styleSheet.NumFmts.NumFmt {
19181917
if numFmt.FormatCode == fmtCode {
1919-
numFmtID = numFmt.NumFmtID
1920-
return
1918+
return numFmt.NumFmtID
19211919
}
19221920
}
19231921
}
19241922
}
1925-
return
1923+
return numFmtID
19261924
}
19271925

19281926
// newNumFmt provides a function to check if number format code in the range

xmlPivotTable.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ type xlsxDataField struct {
273273
ShowDataAs string `xml:"showDataAs,attr,omitempty"`
274274
BaseField int `xml:"baseField,attr,omitempty"`
275275
BaseItem int64 `xml:"baseItem,attr,omitempty"`
276-
NumFmtID string `xml:"numFmtId,attr,omitempty"`
276+
NumFmtID int `xml:"numFmtId,attr,omitempty"`
277277
ExtLst *xlsxExtLst `xml:"extLst"`
278278
}
279279

0 commit comments

Comments
 (0)