MCPcopy
hub / github.com/qax-os/excelize / TestPivotTableDataRange

Function TestPivotTableDataRange

pivotTable_test.go:368–435  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

366}
367
368func TestPivotTableDataRange(t *testing.T) {
369 f := NewFile()
370 // Create table in a worksheet
371 assert.NoError(t, f.AddTable("Sheet1", &Table{
372 Name: "Table1",
373 Range: "A1:D5",
374 }))
375 for row := 2; row < 6; row++ {
376 assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), rand.Intn(10)))
377 assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("B%d", row), rand.Intn(10)))
378 assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("C%d", row), rand.Intn(10)))
379 assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), rand.Intn(10)))
380 }
381 // Test add pivot table with table data range
382 opts := PivotTableOptions{
383 DataRange: "Table1",
384 PivotTableRange: "Sheet1!G2:K7",
385 Rows: []PivotTableField{{Data: "Column1"}},
386 Columns: []PivotTableField{{Data: "Column2"}},
387 RowGrandTotals: true,
388 ColGrandTotals: true,
389 ShowDrill: true,
390 ShowRowHeaders: true,
391 ShowColHeaders: true,
392 ShowLastColumn: true,
393 ShowError: true,
394 PivotTableStyleName: "PivotStyleLight16",
395 }
396 assert.NoError(t, f.AddPivotTable(&opts))
397 assert.NoError(t, f.DeletePivotTable("Sheet1", "PivotTable1"))
398 assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPivotTable2.xlsx")))
399 assert.NoError(t, f.Close())
400
401 assert.NoError(t, f.AddPivotTable(&opts))
402
403 // Test delete pivot table with unsupported table relationships charset
404 f.Pkg.Store("xl/tables/table1.xml", MacintoshCyrillicCharset)
405 assert.EqualError(t, f.DeletePivotTable("Sheet1", "PivotTable1"), "XML syntax error on line 1: invalid UTF-8")
406
407 // Test delete pivot table with unsupported worksheet relationships charset
408 f.Relationships.Delete("xl/worksheets/_rels/sheet1.xml.rels")
409 f.Pkg.Store("xl/worksheets/_rels/sheet1.xml.rels", MacintoshCyrillicCharset)
410 assert.EqualError(t, f.DeletePivotTable("Sheet1", "PivotTable1"), "XML syntax error on line 1: invalid UTF-8")
411
412 // Test delete pivot table without worksheet relationships
413 f.Relationships.Delete("xl/worksheets/_rels/sheet1.xml.rels")
414 f.Pkg.Delete("xl/worksheets/_rels/sheet1.xml.rels")
415 assert.EqualError(t, f.DeletePivotTable("Sheet1", "PivotTable1"), "table PivotTable1 does not exist")
416
417 t.Run("data_range_with_empty_column", func(t *testing.T) {
418 // Test add pivot table with data range doesn't organized as a list with labeled columns
419 f := NewFile()
420 // Create some data in a sheet
421 month := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
422 types := []string{"Meat", "Dairy", "Beverages", "Produce"}
423 assert.NoError(t, f.SetSheetRow("Sheet1", "A1", &[]string{"Month", "", "Type"}))
424 for row := 2; row < 32; row++ {
425 assert.NoError(t, f.SetCellValue("Sheet1", fmt.Sprintf("A%d", row), month[rand.Intn(12)]))

Callers

nothing calls this directly

Calls 9

AddTableMethod · 0.95
SetCellValueMethod · 0.95
AddPivotTableMethod · 0.95
DeletePivotTableMethod · 0.95
SaveAsMethod · 0.95
CloseMethod · 0.95
SetSheetRowMethod · 0.95
NewFileFunction · 0.85

Tested by

no test coverage detected