@@ -1002,6 +1002,12 @@ def test_none_file_pointer(self):
10021002 with self .assertRaises (RuntimeError ) as context :
10031003 f .get_page_layout ("Sheet1" )
10041004 self .assertEqual (str (context .exception ), expected )
1005+ with self .assertRaises (RuntimeError ) as context :
1006+ f .get_page_margins ("Sheet1" )
1007+ self .assertEqual (str (context .exception ), expected )
1008+ with self .assertRaises (RuntimeError ) as context :
1009+ f .get_pivot_tables ("Sheet1" )
1010+ self .assertEqual (str (context .exception ), expected )
10051011 with self .assertRaises (RuntimeError ) as context :
10061012 f .get_sheet_name (0 )
10071013 self .assertEqual (str (context .exception ), expected )
@@ -1530,16 +1536,16 @@ def test_header_footer(self):
15301536
15311537 def test_page_layout (self ):
15321538 f = excelize .new_file ()
1533- opts = excelize .PageLayoutOptions (
1534- size = 1 ,
1535- orientation = "landscape" ,
1536- first_page_number = 1 ,
1537- adjust_to = 120 ,
1538- fit_to_height = 2 ,
1539- fit_to_width = 2 ,
1540- black_and_white = True ,
1541- page_order = "overThenDown" ,
1542- )
1539+ opts = excelize .PageLayoutOptions (
1540+ size = 1 ,
1541+ orientation = "landscape" ,
1542+ first_page_number = 1 ,
1543+ adjust_to = 120 ,
1544+ fit_to_height = 2 ,
1545+ fit_to_width = 2 ,
1546+ black_and_white = True ,
1547+ page_order = "overThenDown" ,
1548+ )
15431549 self .assertIsNone (f .set_page_layout ("Sheet1" , opts ))
15441550 with self .assertRaises (RuntimeError ) as context :
15451551 f .set_page_layout ("SheetN" , excelize .PageLayoutOptions ())
@@ -1565,21 +1571,17 @@ def test_page_layout(self):
15651571
15661572 def test_page_margins (self ):
15671573 f = excelize .new_file ()
1568- self .assertIsNone (
1569- f .set_page_margins (
1570- "Sheet1" ,
1571- excelize .PageLayoutMarginsOptions (
1572- bottom = 1.0 ,
1573- footer = 1.0 ,
1574- header = 1.0 ,
1575- left = 1.0 ,
1576- right = 1.0 ,
1577- top = 1.0 ,
1578- horizontally = True ,
1579- vertically = True ,
1580- ),
1581- )
1582- )
1574+ opts = excelize .PageLayoutMarginsOptions (
1575+ bottom = 1.0 ,
1576+ footer = 1.0 ,
1577+ header = 1.0 ,
1578+ left = 1.0 ,
1579+ right = 1.0 ,
1580+ top = 1.0 ,
1581+ horizontally = True ,
1582+ vertically = True ,
1583+ )
1584+ self .assertIsNone (f .set_page_margins ("Sheet1" , opts ))
15831585 with self .assertRaises (RuntimeError ) as context :
15841586 f .set_page_margins ("SheetN" , excelize .PageLayoutMarginsOptions ())
15851587 self .assertEqual (str (context .exception ), "sheet SheetN does not exist" )
@@ -1589,6 +1591,16 @@ def test_page_margins(self):
15891591 str (context .exception ),
15901592 "expected type PageLayoutMarginsOptions for argument 'opts', but got int" ,
15911593 )
1594+ self .assertEqual (f .get_page_margins ("Sheet1" ), opts )
1595+ with self .assertRaises (RuntimeError ) as context :
1596+ f .get_page_margins ("SheetN" )
1597+ self .assertEqual (str (context .exception ), "sheet SheetN does not exist" )
1598+ with self .assertRaises (TypeError ) as context :
1599+ f .get_page_margins (1 )
1600+ self .assertEqual (
1601+ str (context .exception ),
1602+ "expected type str for argument 'sheet', but got int" ,
1603+ )
15921604 self .assertIsNone (f .save_as (os .path .join ("test" , "TestPageMargins.xlsx" )))
15931605 self .assertIsNone (f .close ())
15941606
@@ -1666,32 +1678,52 @@ def test_pivot_table(self):
16661678 self .assertIsNone (
16671679 f .set_cell_value ("Sheet1" , f"E{ row } " , region [random .randrange (4 )])
16681680 )
1669- self .assertIsNone (
1670- f .add_pivot_table (
1671- excelize .PivotTableOptions (
1672- data_range = "Sheet1!A1:E31" ,
1673- pivot_table_range = "Sheet1!G2:M34" ,
1674- rows = [
1675- excelize .PivotTableField (data = "Month" , default_subtotal = True ),
1676- excelize .PivotTableField (data = "Year" ),
1677- ],
1678- filter = [excelize .PivotTableField (data = "Region" )],
1679- columns = [
1680- excelize .PivotTableField (data = "Type" , default_subtotal = True ),
1681- ],
1682- data = [
1683- excelize .PivotTableField (
1684- data = "Sales" , name = "Summarize" , subtotal = "Sum"
1685- ),
1686- ],
1687- row_grand_totals = True ,
1688- col_grand_totals = True ,
1689- show_drill = True ,
1690- show_row_headers = True ,
1691- show_col_headers = True ,
1692- show_last_column = True ,
1693- )
1694- )
1681+ opts = excelize .PivotTableOptions (
1682+ data_range = "Sheet1!A1:E31" ,
1683+ pivot_table_range = "Sheet1!G2:M34" ,
1684+ rows = [
1685+ excelize .PivotTableField (data = "Month" , default_subtotal = True ),
1686+ excelize .PivotTableField (data = "Year" ),
1687+ ],
1688+ filter = [excelize .PivotTableField (data = "Region" )],
1689+ columns = [
1690+ excelize .PivotTableField (data = "Type" , default_subtotal = True ),
1691+ ],
1692+ data = [
1693+ excelize .PivotTableField (
1694+ data = "Sales" , name = "Summarize" , subtotal = "Sum"
1695+ ),
1696+ ],
1697+ row_grand_totals = True ,
1698+ col_grand_totals = True ,
1699+ show_drill = True ,
1700+ show_row_headers = True ,
1701+ show_col_headers = True ,
1702+ show_last_column = True ,
1703+ )
1704+ self .assertIsNone (f .add_pivot_table (opts ))
1705+ tables = f .get_pivot_tables ("Sheet1" )
1706+ self .assertEqual (len (tables ), 1 )
1707+ self .assertEqual (tables [0 ].data_range , opts .data_range )
1708+ self .assertEqual (tables [0 ].pivot_table_range , opts .pivot_table_range )
1709+ self .assertEqual (tables [0 ].rows , opts .rows )
1710+ self .assertEqual (tables [0 ].filter , opts .filter )
1711+ self .assertEqual (tables [0 ].columns , opts .columns )
1712+ self .assertEqual (tables [0 ].data , opts .data )
1713+ self .assertEqual (tables [0 ].row_grand_totals , opts .row_grand_totals )
1714+ self .assertEqual (tables [0 ].col_grand_totals , opts .col_grand_totals )
1715+ self .assertEqual (tables [0 ].show_drill , opts .show_drill )
1716+ self .assertEqual (tables [0 ].show_row_headers , opts .show_row_headers )
1717+ self .assertEqual (tables [0 ].show_col_headers , opts .show_col_headers )
1718+ self .assertEqual (tables [0 ].show_last_column , opts .show_last_column )
1719+ with self .assertRaises (RuntimeError ) as context :
1720+ f .get_pivot_tables ("SheetN" )
1721+ self .assertEqual (str (context .exception ), "sheet SheetN does not exist" )
1722+ with self .assertRaises (TypeError ) as context :
1723+ f .get_pivot_tables (1 )
1724+ self .assertEqual (
1725+ str (context .exception ),
1726+ "expected type str for argument 'sheet', but got int" ,
16951727 )
16961728 with self .assertRaises (RuntimeError ) as context :
16971729 f .add_pivot_table (excelize .PivotTableOptions ())
0 commit comments