(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestPivotTable(t *testing.T) { |
| 14 | f := NewFile() |
| 15 | // Create some data in a sheet |
| 16 | month := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} |
| 17 | year := []int{2017, 2018, 2019} |
| 18 | types := []string{"Meat", "Dairy", "Beverages", "Produce"} |
| 19 | region := []string{"East", "West", "North", "South"} |
| 20 | assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "Year", "Type", "Sales", "Region"})) |
| 21 | for row := 2; row < 32; row++ { |
| 22 | assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), month[rand.Intn(12)])) |
| 23 | assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), year[rand.Intn(3)])) |
| 24 | assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), types[rand.Intn(4)])) |
| 25 | assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), rand.Intn(5000))) |
| 26 | assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)])) |
| 27 | } |
| 28 | expected := &PivotTableOptions{ |
| 29 | pivotTableXML: "xl/pivotTables/pivotTable1.xml", |
| 30 | pivotCacheXML: "xl/pivotCache/pivotCacheDefinition1.xml", |
| 31 | DataRange: "Sheet1!A1:E31", |
| 32 | PivotTableRange: "Sheet1!G2:M34", |
| 33 | Name: "PivotTable1", |
| 34 | Rows: []PivotTableField{{Data: "Month", ShowAll: true, DefaultSubtotal: true}, {Data: "Year"}}, |
| 35 | Filter: []PivotTableField{{Data: "Region"}}, |
| 36 | Columns: []PivotTableField{{Data: "Type", ShowAll: true, InsertBlankRow: true, DefaultSubtotal: true}}, |
| 37 | Data: []PivotTableField{{Data: "Sales", Subtotal: "Sum", Name: "Summarize by Sum", NumFmt: 38}}, |
| 38 | RowGrandTotals: true, |
| 39 | ColGrandTotals: true, |
| 40 | ShowDrill: true, |
| 41 | ClassicLayout: true, |
| 42 | ShowError: true, |
| 43 | ShowRowHeaders: true, |
| 44 | ShowColHeaders: true, |
| 45 | ShowLastColumn: true, |
| 46 | FieldPrintTitles: true, |
| 47 | ItemPrintTitles: true, |
| 48 | PivotTableStyleName: "PivotStyleLight16", |
| 49 | } |
| 50 | assert.NoError(t, f.AddPivotTable(expected)) |
| 51 | // Test get pivot table |
| 52 | pivotTables, err := f.GetPivotTables("Sheet1") |
| 53 | assert.NoError(t, err) |
| 54 | assert.Len(t, pivotTables, 1) |
| 55 | assert.Equal(t, *expected, pivotTables[0]) |
| 56 | // Use different order of coordinate tests |
| 57 | assert.NoError(t, f.AddPivotTable(&PivotTableOptions{ |
| 58 | DataRange: "Sheet1!A1:E31", |
| 59 | PivotTableRange: "Sheet1!U34:O2", |
| 60 | Rows: []PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}}, |
| 61 | Columns: []PivotTableField{{Data: "Type", DefaultSubtotal: true}}, |
| 62 | Data: []PivotTableField{{Data: "Sales", Subtotal: "Average", Name: "Summarize by Average"}}, |
| 63 | RowGrandTotals: true, |
| 64 | ColGrandTotals: true, |
| 65 | ShowDrill: true, |
| 66 | ShowRowHeaders: true, |
| 67 | ShowColHeaders: true, |
| 68 | ShowLastColumn: true, |
| 69 | })) |
| 70 | // Test get pivot table with default style name |
nothing calls this directly
no test coverage detected