11package excelize
22
33import (
4+ "fmt"
5+ "path/filepath"
46 "testing"
57
68 "github.com/stretchr/testify/assert"
@@ -281,7 +283,7 @@ func TestAdjustAutoFilter(t *testing.T) {
281283 Ref : "A1:A3" ,
282284 },
283285 }, rows , 1 , - 1 ))
284- // testing adjustAutoFilter with illegal cell coordinates.
286+ // Test adjustAutoFilter with illegal cell coordinates.
285287 assert .EqualError (t , f .adjustAutoFilter (& xlsxWorksheet {
286288 AutoFilter : & xlsxAutoFilter {
287289 Ref : "A:B1" ,
@@ -294,6 +296,36 @@ func TestAdjustAutoFilter(t *testing.T) {
294296 }, rows , 0 , 0 ), newCellNameToCoordinatesError ("B" , newInvalidCellNameError ("B" )).Error ())
295297}
296298
299+ func TestAdjustTable (t * testing.T ) {
300+ f , sheetName := NewFile (), "Sheet1"
301+ for idx , tableRange := range [][]string {{"B2" , "C3" }, {"E3" , "F5" }, {"H5" , "H8" }, {"J5" , "K9" }} {
302+ assert .NoError (t , f .AddTable (sheetName , tableRange [0 ], tableRange [1 ], fmt .Sprintf (`{
303+ "table_name": "table%d",
304+ "table_style": "TableStyleMedium2",
305+ "show_first_column": true,
306+ "show_last_column": true,
307+ "show_row_stripes": false,
308+ "show_column_stripes": true
309+ }` , idx )))
310+ }
311+ assert .NoError (t , f .RemoveRow (sheetName , 2 ))
312+ assert .NoError (t , f .RemoveRow (sheetName , 3 ))
313+ assert .NoError (t , f .RemoveCol (sheetName , "H" ))
314+ assert .NoError (t , f .SaveAs (filepath .Join ("test" , "TestAdjustTable.xlsx" )))
315+
316+ f = NewFile ()
317+ assert .NoError (t , f .AddTable (sheetName , "A1" , "D5" , "" ))
318+ // Test adjust table with non-table part
319+ f .Pkg .Delete ("xl/tables/table1.xml" )
320+ assert .NoError (t , f .RemoveRow (sheetName , 1 ))
321+ // Test adjust table with unsupported charset
322+ f .Pkg .Store ("xl/tables/table1.xml" , MacintoshCyrillicCharset )
323+ assert .NoError (t , f .RemoveRow (sheetName , 1 ))
324+ // Test adjust table with invalid table range reference
325+ f .Pkg .Store ("xl/tables/table1.xml" , []byte (`<table ref="-" />` ))
326+ assert .NoError (t , f .RemoveRow (sheetName , 1 ))
327+ }
328+
297329func TestAdjustHelper (t * testing.T ) {
298330 f := NewFile ()
299331 f .NewSheet ("Sheet2" )
@@ -303,10 +335,10 @@ func TestAdjustHelper(t *testing.T) {
303335 f .Sheet .Store ("xl/worksheets/sheet2.xml" , & xlsxWorksheet {
304336 AutoFilter : & xlsxAutoFilter {Ref : "A1:B" },
305337 })
306- // testing adjustHelper with illegal cell coordinates.
338+ // Test adjustHelper with illegal cell coordinates.
307339 assert .EqualError (t , f .adjustHelper ("Sheet1" , rows , 0 , 0 ), newCellNameToCoordinatesError ("A" , newInvalidCellNameError ("A" )).Error ())
308340 assert .EqualError (t , f .adjustHelper ("Sheet2" , rows , 0 , 0 ), newCellNameToCoordinatesError ("B" , newInvalidCellNameError ("B" )).Error ())
309- // testing adjustHelper on not exists worksheet.
341+ // Test adjustHelper on not exists worksheet.
310342 assert .EqualError (t , f .adjustHelper ("SheetN" , rows , 0 , 0 ), "sheet SheetN is not exist" )
311343}
312344
0 commit comments