Skip to content

Commit 65dba0a

Browse files
Merge branch 'qax-os:master' into numfmt
2 parents dbdf451 + c805be1 commit 65dba0a

File tree

11 files changed

+697
-58
lines changed

11 files changed

+697
-58
lines changed

calc.go

Lines changed: 2 additions & 2 deletions
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
}

errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ func newInvalidStyleID(styleID int) error {
264264
return fmt.Errorf("invalid style ID %d", styleID)
265265
}
266266

267+
// newNoExistSlicerError defined the error message on receiving the non existing
268+
// slicer name.
269+
func newNoExistSlicerError(name string) error {
270+
return fmt.Errorf("slicer %s does not exist", name)
271+
}
272+
267273
// newNoExistTableError defined the error message on receiving the non existing
268274
// table name.
269275
func newNoExistTableError(name string) error {

pivotTable.go

Lines changed: 48 additions & 10 deletions
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 {
@@ -761,12 +785,11 @@ func (f *File) getPivotTableDataRange(opts *PivotTableOptions) error {
761785
opts.pivotDataRange = opts.DataRange
762786
return nil
763787
}
764-
for _, sheetName := range f.GetSheetList() {
765-
tables, err := f.GetTables(sheetName)
766-
e := ErrSheetNotExist{sheetName}
767-
if err != nil && err.Error() != newNotWorksheetError(sheetName).Error() && err.Error() != e.Error() {
768-
return err
769-
}
788+
tbls, err := f.getTables()
789+
if err != nil {
790+
return err
791+
}
792+
for sheetName, tables := range tbls {
770793
for _, table := range tables {
771794
if table.Name == opts.DataRange {
772795
opts.pivotDataRange, opts.namedDataRange = fmt.Sprintf("%s!%s", sheetName, table.Range), true
@@ -891,6 +914,7 @@ func (f *File) extractPivotTableFields(order []string, pt *xlsxPivotTableDefinit
891914
Data: order[field.Fld],
892915
Name: field.Name,
893916
Subtotal: cases.Title(language.English).String(field.Subtotal),
917+
NumFmt: field.NumFmtID,
894918
})
895919
}
896920
}
@@ -991,8 +1015,8 @@ func (f *File) DeletePivotTable(sheet, name string) error {
9911015
return err
9921016
}
9931017
pivotTableCaches := map[string]int{}
994-
for _, sheetName := range f.GetSheetList() {
995-
sheetPivotTables, _ := f.GetPivotTables(sheetName)
1018+
pivotTables, _ := f.getPivotTables()
1019+
for _, sheetPivotTables := range pivotTables {
9961020
for _, sheetPivotTable := range sheetPivotTables {
9971021
pivotTableCaches[sheetPivotTable.pivotCacheXML]++
9981022
}
@@ -1013,3 +1037,17 @@ func (f *File) DeletePivotTable(sheet, name string) error {
10131037
}
10141038
return newNoExistTableError(name)
10151039
}
1040+
1041+
// getPivotTables provides a function to get all pivot tables in a workbook.
1042+
func (f *File) getPivotTables() (map[string][]PivotTableOptions, error) {
1043+
pivotTables := map[string][]PivotTableOptions{}
1044+
for _, sheetName := range f.GetSheetList() {
1045+
pts, err := f.GetPivotTables(sheetName)
1046+
e := ErrSheetNotExist{sheetName}
1047+
if err != nil && err.Error() != newNotWorksheetError(sheetName).Error() && err.Error() != e.Error() {
1048+
return pivotTables, err
1049+
}
1050+
pivotTables[sheetName] = append(pivotTables[sheetName], pts...)
1051+
}
1052+
return pivotTables, nil
1053+
}

pivotTable_test.go

Lines changed: 6 additions & 4 deletions
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,
@@ -343,6 +343,8 @@ func TestPivotTable(t *testing.T) {
343343
f.Pkg.Store("xl/pivotTables/pivotTable1.xml", MacintoshCyrillicCharset)
344344
_, err = f.GetPivotTables("Sheet1")
345345
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
346+
_, err = f.getPivotTables()
347+
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
346348
assert.NoError(t, f.Close())
347349
}
348350

0 commit comments

Comments
 (0)