-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from Zncl2222/Zncl2222/feature/pivot_table
Feat: add pivot table
- Loading branch information
Showing
13 changed files
with
897 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
## PivotTableField | ||
|
||
Represents a field within a PivotTable, which includes various settings like name, | ||
data, compact display, and subtotal configurations. | ||
|
||
| Attribute | Type | Description | | ||
|-------------------|------------------------------------------|-------------------------------------------------------------------| | ||
| `compact` | `Optional[bool]` | Indicates whether the field is displayed in compact form. | | ||
| `data` | `Optional[str]` | The data value associated with the field. | | ||
| `name` | `Optional[str]` | The name of the field. | | ||
| `outline` | `Optional[bool]` | Indicates whether the field is in outline form. | | ||
| `subtotal` | `Optional[str or PivotSubTotal]` | The subtotal type for the field. | | ||
| `default_subtotal`| `Optional[bool]` | Specifies if the field has a default subtotal applied. | | ||
|
||
## PivotTable | ||
|
||
Represents a PivotTable configuration, including data ranges, field settings, and display options. | ||
|
||
| Attribute | Type | Description | | ||
|-----------------------|-----------------------------------------|---------------------------------------------------------------------------------------| | ||
| `data_range` | `str` | Range of data used for the pivot table, e.g., "Sheet1!A1:B2". | | ||
| `pivot_table_range` | `str` | Range where the pivot table will be placed, e.g., "Sheet1!C3:D4". | | ||
| `rows` | `list[PivotTableField]` | List of fields used as rows in the pivot table. | | ||
| `pivot_filter` | `list[PivotTableField]` | List of fields used as filters in the pivot table. | | ||
| `columns` | `list[PivotTableField]` | List of fields used as columns in the pivot table. | | ||
| `data` | `list[PivotTableField]` | List of fields used as data fields in the pivot table. | | ||
| `row_grand_totals` | `Optional[bool]` | Indicates whether to display row grand totals. | | ||
| `column_grand_totals` | `Optional[bool]` | Indicates whether to display column grand totals. | | ||
| `show_drill` | `Optional[bool]` | Indicates whether to show drill indicators. | | ||
| `show_row_headers` | `Optional[bool]` | Indicates whether to display row headers. | | ||
| `show_column_headers` | `Optional[bool]` | Indicates whether to display column headers. | | ||
| `show_row_stripes` | `Optional[bool]` | Indicates whether to display row stripes. | | ||
| `show_col_stripes` | `Optional[bool]` | Indicates whether to display column stripes. | | ||
| `show_last_column` | `Optional[bool]` | Indicates whether to display the last column. | | ||
| `use_auto_formatting` | `Optional[bool]` | Indicates whether to apply automatic formatting. | | ||
| `page_over_then_down` | `Optional[bool]` | Indicates whether pages should be ordered top-to-bottom, then left-to-right. | | ||
| `merge_item` | `Optional[bool]` | Indicates whether to merge items in the pivot table. | | ||
| `compact_data` | `Optional[bool]` | Indicates whether to display data in compact form. | | ||
| `show_error` | `Optional[bool]` | Indicates whether to display errors. | | ||
| `pivot_table_style_name` | `Optional[str]` | Specifies the style for the pivot table, chosen from a predefined set of styles. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/xuri/excelize/v2" | ||
) | ||
|
||
func getPivotTableField(field interface{}) []excelize.PivotTableField { | ||
|
||
var pivotTableFields []excelize.PivotTableField | ||
|
||
fieldMappings := []fieldMapping{ | ||
{Name: "Compact", Type: "bool"}, | ||
{Name: "Data", Type: "string"}, | ||
{Name: "Name", Type: "string"}, | ||
{Name: "Outline", Type: "bool"}, | ||
{Name: "Subtotal", Type: "string"}, | ||
{Name: "DefaultSubtotal", Type: "bool"}, | ||
} | ||
|
||
for _, f := range field.([]interface{}) { | ||
pivotTableField := excelize.PivotTableField{} | ||
setField(&pivotTableField, f.(map[string]interface{}), fieldMappings) | ||
pivotTableFields = append(pivotTableFields, pivotTableField) | ||
} | ||
return pivotTableFields | ||
} | ||
|
||
func createPivotTable(file *excelize.File, pivot_data []interface{}) { | ||
for _, pivot := range pivot_data { | ||
pivotMap := pivot.(map[string]interface{}) | ||
|
||
rowGrandTotals := false | ||
if pivotMap["RowGrandTotals"] != nil { | ||
rowGrandTotals = pivotMap["RowGrandTotals"].(bool) | ||
} | ||
colGrandTotals := false | ||
if pivotMap["ColGrandTotals"] != nil { | ||
colGrandTotals = pivotMap["ColGrandTotals"].(bool) | ||
} | ||
showDrill := false | ||
if pivotMap["ShowDrill"] != nil { | ||
showDrill = pivotMap["ShowDrill"].(bool) | ||
} | ||
showRowHeaders := false | ||
if pivotMap["ShowRowHeaders"] != nil { | ||
showRowHeaders = pivotMap["ShowRowHeaders"].(bool) | ||
} | ||
showColHeaders := false | ||
if pivotMap["ShowColHeaders"] != nil { | ||
showColHeaders = pivotMap["ShowColHeaders"].(bool) | ||
} | ||
showLastColumn := false | ||
if pivotMap["ShowLastColumn"] != nil { | ||
showLastColumn = pivotMap["ShowLastColumn"].(bool) | ||
} | ||
|
||
err := file.AddPivotTable(&excelize.PivotTableOptions{ | ||
DataRange: pivotMap["DataRange"].(string), | ||
PivotTableRange: pivotMap["PivotTableRange"].(string), | ||
Rows: getPivotTableField(pivotMap["Rows"]), | ||
Filter: getPivotTableField(pivotMap["Filter"]), | ||
Columns: getPivotTableField(pivotMap["Columns"]), | ||
Data: getPivotTableField(pivotMap["Data"]), | ||
RowGrandTotals: rowGrandTotals, | ||
ColGrandTotals: colGrandTotals, | ||
ShowDrill: showDrill, | ||
ShowRowHeaders: showRowHeaders, | ||
ShowColHeaders: showColHeaders, | ||
ShowLastColumn: showLastColumn, | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/xuri/excelize/v2" | ||
) | ||
|
||
func TestGetPivotTableField(t *testing.T) { | ||
fieldData := []interface{}{ | ||
map[string]interface{}{ | ||
"Compact": true, | ||
"Data": "SampleData", | ||
"Name": "SampleName", | ||
"Outline": true, | ||
"Subtotal": "SampleSubtotal", | ||
"DefaultSubtotal": false, | ||
}, | ||
} | ||
|
||
expected := []excelize.PivotTableField{ | ||
{ | ||
Compact: true, | ||
Data: "SampleData", | ||
Name: "SampleName", | ||
Outline: true, | ||
Subtotal: "SampleSubtotal", | ||
DefaultSubtotal: false, | ||
}, | ||
} | ||
|
||
result := getPivotTableField(fieldData) | ||
|
||
if len(result) != len(expected) { | ||
t.Errorf("Expected length %d, but got %d", len(expected), len(result)) | ||
} | ||
|
||
for i := range expected { | ||
if result[i] != expected[i] { | ||
t.Errorf("Expected %+v, but got %+v", expected[i], result[i]) | ||
} | ||
} | ||
} | ||
|
||
func TestCreatePivotTable(t *testing.T) { | ||
// Initialize an excel file | ||
file := excelize.NewFile() | ||
|
||
// Mock pivot table data | ||
pivotData := []interface{}{ | ||
map[string]interface{}{ | ||
"DataRange": "Sheet1!A1:D10", | ||
"PivotTableRange": "Sheet1!F1:G10", | ||
"Rows": []interface{}{ | ||
map[string]interface{}{ | ||
"Name": "RowField", | ||
}, | ||
}, | ||
"Filter": []interface{}{ | ||
map[string]interface{}{ | ||
"Name": "FilterField", | ||
}, | ||
}, | ||
"Columns": []interface{}{ | ||
map[string]interface{}{ | ||
"Name": "ColumnField", | ||
}, | ||
}, | ||
"Data": []interface{}{ | ||
map[string]interface{}{ | ||
"Name": "DataField", | ||
}, | ||
}, | ||
"RowGrandTotals": true, | ||
"ColGrandTotals": true, | ||
"ShowDrill": false, | ||
"ShowRowHeaders": true, | ||
"ShowColHeaders": true, | ||
"ShowLastColumn": false, | ||
}, | ||
} | ||
|
||
// Call the function to test | ||
createPivotTable(file, pivotData) | ||
} |
Oops, something went wrong.