2016-08-30 11:51:31 +08:00
package excelize
import (
2017-01-22 16:16:03 +08:00
_ "image/gif"
_ "image/jpeg"
_ "image/png"
2017-03-28 21:18:06 +08:00
"io/ioutil"
2016-08-30 14:00:21 +08:00
"strconv"
2016-08-30 11:51:31 +08:00
"testing"
)
2017-01-18 14:47:23 +08:00
func TestOpenFile ( t * testing . T ) {
2016-10-19 18:42:29 +08:00
// Test update a XLSX file.
2017-01-19 14:05:32 +08:00
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
2016-09-06 21:20:24 +08:00
if err != nil {
t . Log ( err )
}
2016-11-02 12:58:51 +08:00
// Test get all the rows in a not exists sheet.
2017-01-19 14:05:32 +08:00
rows := xlsx . GetRows ( "Sheet4" )
2016-11-02 12:58:51 +08:00
// Test get all the rows in a sheet.
2017-01-19 14:05:32 +08:00
rows = xlsx . GetRows ( "Sheet2" )
2016-11-02 12:58:51 +08:00
for _ , row := range rows {
for _ , cell := range row {
t . Log ( cell , "\t" )
}
t . Log ( "\r\n" )
}
2017-01-19 14:05:32 +08:00
xlsx . UpdateLinkedValue ( )
xlsx . SetCellDefault ( "SHEET2" , "A1" , strconv . FormatFloat ( float64 ( 100.1588 ) , 'f' , - 1 , 32 ) )
xlsx . SetCellDefault ( "SHEET2" , "A1" , strconv . FormatFloat ( float64 ( - 100.1588 ) , 'f' , - 1 , 64 ) )
xlsx . SetCellInt ( "SHEET2" , "A1" , 100 )
xlsx . SetCellStr ( "SHEET2" , "C11" , "Knowns" )
// Test max characters in a cell.
var s = "c"
for i := 0 ; i < 32768 ; i ++ {
s += "c"
}
xlsx . SetCellStr ( "SHEET2" , "D11" , s )
xlsx . NewSheet ( 3 , ":\\/?*[]Maximum 31 characters allowed in sheet title." )
2017-01-18 14:47:23 +08:00
// Test set sheet name with illegal name.
2017-01-19 14:05:32 +08:00
xlsx . SetSheetName ( "Maximum 31 characters allowed i" , "[Rename]:\\/?* Maximum 31 characters allowed in sheet title." )
xlsx . SetCellInt ( "Sheet3" , "A23" , 10 )
xlsx . SetCellStr ( "SHEET3" , "b230" , "10" )
xlsx . SetCellStr ( "SHEET10" , "b230" , "10" )
xlsx . SetActiveSheet ( 2 )
xlsx . GetCellFormula ( "Sheet1" , "B19" ) // Test get cell formula with given rows number.
xlsx . GetCellFormula ( "Sheet2" , "B20" ) // Test get cell formula with illegal sheet index.
xlsx . GetCellFormula ( "Sheet1" , "B20" ) // Test get cell formula with illegal rows number.
2016-10-19 18:42:29 +08:00
// Test read cell value with given illegal rows number.
2017-01-19 14:05:32 +08:00
xlsx . GetCellValue ( "Sheet2" , "a-1" )
2016-10-19 18:42:29 +08:00
// Test read cell value with given lowercase column number.
2017-01-19 14:05:32 +08:00
xlsx . GetCellValue ( "Sheet2" , "a5" )
xlsx . GetCellValue ( "Sheet2" , "C11" )
xlsx . GetCellValue ( "Sheet2" , "D11" )
xlsx . GetCellValue ( "Sheet2" , "D12" )
2016-10-19 18:42:29 +08:00
// Test SetCellValue function.
2017-03-28 11:48:09 +08:00
xlsx . SetCellValue ( "Sheet2" , "F1" , " Hello" )
2017-01-19 14:05:32 +08:00
xlsx . SetCellValue ( "Sheet2" , "G1" , [ ] byte ( "World" ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , 42 )
xlsx . SetCellValue ( "Sheet2" , "F2" , int8 ( 42 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , int16 ( 42 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , int32 ( 42 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , int64 ( 42 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , float32 ( 42.65418 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , float64 ( - 42.65418 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , float32 ( 42 ) )
xlsx . SetCellValue ( "Sheet2" , "F2" , float64 ( 42 ) )
xlsx . SetCellValue ( "Sheet2" , "G2" , nil )
2016-10-23 16:07:57 +08:00
// Test completion column.
2017-01-19 14:05:32 +08:00
xlsx . SetCellValue ( "Sheet2" , "M2" , nil )
2016-10-19 18:42:29 +08:00
// Test read cell value with given axis large than exists row.
2017-01-19 14:05:32 +08:00
xlsx . GetCellValue ( "Sheet2" , "E231" )
2016-12-31 23:47:30 +08:00
// Test get active sheet of XLSX and get sheet name of XLSX by given sheet index.
2017-01-19 14:05:32 +08:00
xlsx . GetSheetName ( xlsx . GetActiveSheetIndex ( ) )
2017-04-01 21:02:25 +08:00
// Test get sheet index of XLSX by given worksheet name.
xlsx . GetSheetIndex ( "Sheet1" )
2016-12-31 23:47:30 +08:00
// Test get sheet name of XLSX by given invalid sheet index.
2017-01-19 14:05:32 +08:00
xlsx . GetSheetName ( 4 )
2016-12-31 23:47:30 +08:00
// Test get sheet map of XLSX.
2017-01-19 14:05:32 +08:00
xlsx . GetSheetMap ( )
2016-09-05 16:37:15 +08:00
for i := 1 ; i <= 300 ; i ++ {
2017-01-19 14:05:32 +08:00
xlsx . SetCellStr ( "SHEET3" , "c" + strconv . Itoa ( i ) , strconv . Itoa ( i ) )
}
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
// Test write file to not exist directory.
err = xlsx . WriteTo ( "" )
if err != nil {
t . Log ( err )
2016-08-30 11:51:31 +08:00
}
2017-01-19 14:05:32 +08:00
}
func TestAddPicture ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
2016-08-30 11:51:31 +08:00
if err != nil {
2016-09-02 10:28:29 +08:00
t . Log ( err )
2016-08-30 11:51:31 +08:00
}
2017-01-17 19:06:42 +08:00
// Test add picture to sheet.
2017-02-28 12:59:04 +08:00
err = xlsx . AddPicture ( "Sheet2" , "I9" , "./test/images/excel.jpg" , ` { "x_offset": 140, "y_offset": 120} ` )
2017-01-17 19:06:42 +08:00
if err != nil {
t . Log ( err )
}
2017-01-22 16:16:03 +08:00
// Test add picture to sheet with offset.
2017-02-28 12:59:04 +08:00
err = xlsx . AddPicture ( "Sheet1" , "F21" , "./test/images/excel.png" , ` { "x_offset": 10, "y_offset": 10} ` )
2017-01-17 19:06:42 +08:00
if err != nil {
t . Log ( err )
}
2017-01-22 16:16:03 +08:00
// Test add picture to sheet with invalid file path.
2017-02-28 12:59:04 +08:00
err = xlsx . AddPicture ( "Sheet1" , "G21" , "./test/images/excel.icon" , "" )
2017-01-17 19:06:42 +08:00
if err != nil {
t . Log ( err )
}
// Test add picture to sheet with unsupport file type.
2017-02-28 12:59:04 +08:00
err = xlsx . AddPicture ( "Sheet1" , "G21" , "./test/Workbook1.xlsx" , "" )
2017-01-17 19:06:42 +08:00
if err != nil {
t . Log ( err )
}
2016-10-19 18:42:29 +08:00
// Test write file to given path.
2017-01-19 14:05:32 +08:00
err = xlsx . WriteTo ( "./test/Workbook_2.xlsx" )
2016-09-02 10:28:29 +08:00
if err != nil {
t . Log ( err )
}
2017-01-18 16:05:01 +08:00
}
2016-08-30 11:51:31 +08:00
2017-01-18 16:05:01 +08:00
func TestBrokenFile ( t * testing . T ) {
2016-10-19 18:42:29 +08:00
// Test write file with broken file struct.
2017-01-19 14:05:32 +08:00
xlsx := File { }
err := xlsx . Save ( )
2016-10-19 18:42:29 +08:00
if err != nil {
t . Log ( err )
}
// Test write file with broken file struct with given path.
2017-01-19 14:05:32 +08:00
err = xlsx . WriteTo ( "./test/Workbook_3.xlsx" )
2016-10-19 18:42:29 +08:00
if err != nil {
t . Log ( err )
}
2017-01-18 16:05:01 +08:00
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
f3 , err := OpenFile ( "./test/badWorkbook.xlsx" )
f3 . SetActiveSheet ( 2 )
2017-01-17 19:06:42 +08:00
if err != nil {
t . Log ( err )
}
2017-01-18 16:05:01 +08:00
// Test open a XLSX file with given illegal path.
_ , err = OpenFile ( "./test/Workbook.xlsx" )
2017-01-17 19:06:42 +08:00
if err != nil {
t . Log ( err )
}
2017-01-18 16:05:01 +08:00
}
func TestCreateFile ( t * testing . T ) {
// Test create a XLSX file.
2017-01-19 14:05:32 +08:00
xlsx := CreateFile ( )
xlsx . NewSheet ( 2 , "XLSXSheet2" )
xlsx . NewSheet ( 3 , "XLSXSheet3" )
xlsx . SetCellInt ( "Sheet2" , "A23" , 56 )
xlsx . SetCellStr ( "SHEET1" , "B20" , "42" )
xlsx . SetActiveSheet ( 0 )
2017-01-22 16:16:03 +08:00
// Test add picture to sheet with scaling.
2017-02-28 12:59:04 +08:00
err := xlsx . AddPicture ( "Sheet1" , "H2" , "./test/images/excel.gif" , ` { "x_scale": 0.5, "y_scale": 0.5} ` )
2016-08-30 11:51:31 +08:00
if err != nil {
2016-09-02 10:28:29 +08:00
t . Log ( err )
2016-08-30 11:51:31 +08:00
}
2017-02-28 12:59:04 +08:00
err = xlsx . AddPicture ( "Sheet1" , "C2" , "./test/images/excel.png" , "" )
2016-10-23 16:46:46 +08:00
if err != nil {
t . Log ( err )
}
2017-01-19 14:05:32 +08:00
err = xlsx . WriteTo ( "./test/Workbook_3.xlsx" )
2016-09-06 21:20:24 +08:00
if err != nil {
t . Log ( err )
}
2016-08-30 11:51:31 +08:00
}
2017-01-18 14:47:23 +08:00
func TestSetColWidth ( t * testing . T ) {
2017-01-22 16:16:03 +08:00
xlsx := CreateFile ( )
2017-01-19 14:05:32 +08:00
xlsx . SetColWidth ( "sheet1" , "B" , "A" , 12 )
xlsx . SetColWidth ( "sheet1" , "A" , "B" , 12 )
2017-01-22 16:16:03 +08:00
err := xlsx . WriteTo ( "./test/Workbook_4.xlsx" )
2017-01-19 14:05:32 +08:00
if err != nil {
t . Log ( err )
}
}
func TestSetCellHyperLink ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
if err != nil {
t . Log ( err )
}
// Test set cell hyperlink in a work sheet already have hyperlinks.
xlsx . SetCellHyperLink ( "sheet1" , "B19" , "https://github.com/Luxurioust/excelize" )
// Test add first hyperlink in a work sheet.
xlsx . SetCellHyperLink ( "sheet2" , "C1" , "https://github.com/Luxurioust/excelize" )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
func TestSetCellFormula ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
2017-01-18 14:47:23 +08:00
if err != nil {
t . Log ( err )
}
2017-01-19 14:05:32 +08:00
xlsx . SetCellFormula ( "sheet1" , "B19" , "SUM(Sheet2!D2,Sheet2!D11)" )
xlsx . SetCellFormula ( "sheet1" , "C19" , "SUM(Sheet2!D2,Sheet2!D9)" )
err = xlsx . Save ( )
2017-01-18 14:47:23 +08:00
if err != nil {
t . Log ( err )
}
}
2017-01-24 18:29:02 +08:00
func TestSetSheetBackground ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
if err != nil {
t . Log ( err )
}
err = xlsx . SetSheetBackground ( "sheet2" , "./test/images/background.png" )
if err != nil {
t . Log ( err )
}
err = xlsx . SetSheetBackground ( "sheet2" , "./test/Workbook1.xlsx" )
if err != nil {
t . Log ( err )
}
err = xlsx . SetSheetBackground ( "sheet2" , "./test/images/background.jpg" )
if err != nil {
t . Log ( err )
}
err = xlsx . SetSheetBackground ( "sheet2" , "./test/images/background.jpg" )
if err != nil {
t . Log ( err )
}
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-01-25 15:44:18 +08:00
2017-02-12 19:03:24 +08:00
func TestMergeCell ( t * testing . T ) {
2017-01-25 15:44:18 +08:00
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
if err != nil {
t . Log ( err )
}
2017-01-25 18:13:11 +08:00
xlsx . MergeCell ( "Sheet1" , "D9" , "D9" )
xlsx . MergeCell ( "Sheet1" , "D9" , "E9" )
xlsx . MergeCell ( "Sheet1" , "H14" , "G13" )
xlsx . MergeCell ( "Sheet1" , "C9" , "D8" )
xlsx . MergeCell ( "Sheet1" , "F11" , "G13" )
xlsx . MergeCell ( "Sheet1" , "H7" , "B15" )
xlsx . MergeCell ( "Sheet1" , "D11" , "F13" )
xlsx . MergeCell ( "Sheet1" , "G10" , "K12" )
xlsx . SetCellValue ( "Sheet1" , "G11" , "set value in merged cell" )
xlsx . SetCellInt ( "Sheet1" , "H11" , 100 )
xlsx . SetCellValue ( "Sheet1" , "I11" , float64 ( 0.5 ) )
xlsx . SetCellHyperLink ( "Sheet1" , "J11" , "https://github.com/Luxurioust/excelize" )
xlsx . SetCellFormula ( "Sheet1" , "G12" , "SUM(Sheet1!B19,Sheet1!C19)" )
xlsx . GetCellValue ( "Sheet1" , "H11" )
xlsx . GetCellFormula ( "Sheet1" , "G12" )
2017-01-25 15:44:18 +08:00
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-02-02 10:59:31 +08:00
func TestSetRowHeight ( t * testing . T ) {
xlsx := CreateFile ( )
xlsx . SetRowHeight ( "Sheet1" , 0 , 50 )
xlsx . SetRowHeight ( "Sheet1" , 3 , 90 )
err := xlsx . WriteTo ( "./test/Workbook_5.xlsx" )
if err != nil {
t . Log ( err )
}
}
2017-03-06 12:05:41 +08:00
2017-03-26 15:27:04 +08:00
func TestSetCellStyleAlignment ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
err = xlsx . SetCellStyle ( "Sheet1" , "A22" , "A22" , ` { "alignment": { "horizontal":"center","ident":1,"justify_last_line":true,"reading_order":0,"relative_indent":1,"shrink_to_fit":true,"text_rotation":45,"vertical":"top","wrap_text":true}} ` )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
func TestSetCellStyleBorder ( t * testing . T ) {
2017-03-06 12:05:41 +08:00
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
// Test set border with invalid style parameter.
2017-03-19 17:14:40 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "J21" , "L25" , "" )
2017-03-06 12:05:41 +08:00
if err != nil {
t . Log ( err )
}
// Test set border with invalid style index number.
2017-03-19 17:14:40 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "J21" , "L25" , ` { "border":[ { "type":"left","color":"0000FF","style":-1}, { "type":"top","color":"00FF00","style":14}, { "type":"bottom","color":"FFFF00","style":5}, { "type":"right","color":"FF0000","style":6}, { "type":"diagonalDown","color":"A020F0","style":9}, { "type":"diagonalUp","color":"A020F0","style":8}]} ` )
2017-03-10 23:10:15 +08:00
if err != nil {
t . Log ( err )
}
2017-03-19 17:14:40 +08:00
// Test set border on overlapping area with vertical variants shading styles gradient fill.
err = xlsx . SetCellStyle ( "Sheet1" , "J21" , "L25" , ` { "border":[ { "type":"left","color":"0000FF","style":2}, { "type":"top","color":"00FF00","style":12}, { "type":"bottom","color":"FFFF00","style":5}, { "type":"right","color":"FF0000","style":6}, { "type":"diagonalDown","color":"A020F0","style":9}, { "type":"diagonalUp","color":"A020F0","style":8}]} ` )
2017-03-06 12:05:41 +08:00
if err != nil {
t . Log ( err )
}
2017-03-26 15:27:04 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "M28" , "K24" , ` { "border":[ { "type":"left","color":"0000FF","style":2}, { "type":"top","color":"00FF00","style":3}, { "type":"bottom","color":"FFFF00","style":4}, { "type":"right","color":"FF0000","style":5}, { "type":"diagonalDown","color":"A020F0","style":6}, { "type":"diagonalUp","color":"A020F0","style":7}],"fill": { "type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":1}} ` )
2017-03-06 12:05:41 +08:00
if err != nil {
t . Log ( err )
}
2017-03-19 17:14:40 +08:00
// Test set border and solid style pattern fill for a single cell.
2017-03-26 15:27:04 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "O22" , "O22" , ` { "border":[ { "type":"left","color":"0000FF","style":8}, { "type":"top","color":"00FF00","style":9}, { "type":"bottom","color":"FFFF00","style":10}, { "type":"right","color":"FF0000","style":11}, { "type":"diagonalDown","color":"A020F0","style":12}, { "type":"diagonalUp","color":"A020F0","style":13}],"fill": { "type":"pattern","color":["#E0EBF5"],"pattern":1}} ` )
2017-03-19 17:14:40 +08:00
if err != nil {
t . Log ( err )
}
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
func TestSetCellStyleFill ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
2017-04-07 17:32:14 +08:00
xlsx . SetCellValue ( "Sheet1" , "N23" , 42920.5 )
// Test only set fill and number format for a cell.
err = xlsx . SetCellStyle ( "Sheet1" , "N23" , "N23" , ` { "fill": { "type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":4},"number_format":22} ` )
2017-03-19 17:14:40 +08:00
if err != nil {
t . Log ( err )
}
2017-04-07 17:32:14 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "N24" , "N24" , ` { "fill": { "type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":5},"number_format":23} ` )
2017-03-19 17:14:40 +08:00
if err != nil {
t . Log ( err )
}
// Test set fill for cell with invalid parameter.
2017-03-26 15:27:04 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "O23" , "O23" , ` { "fill": { "type":"gradient","color":["#FFFFFF","#E0EBF5"],"shading":6}} ` )
2017-03-19 17:14:40 +08:00
if err != nil {
t . Log ( err )
}
2017-03-26 15:27:04 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "O23" , "O23" , ` { "fill": { "type":"gradient","color":["#FFFFFF"],"shading":1}} ` )
2017-03-19 17:14:40 +08:00
if err != nil {
t . Log ( err )
}
2017-03-26 15:27:04 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "O23" , "O23" , ` { "fill": { "type":"pattern","color":[],"pattern":1}} ` )
2017-03-19 17:14:40 +08:00
if err != nil {
t . Log ( err )
}
2017-04-07 17:32:14 +08:00
2017-03-26 15:27:04 +08:00
err = xlsx . SetCellStyle ( "Sheet1" , "O23" , "O23" , ` { "fill": { "type":"pattern","color":["#E0EBF5"],"pattern":19}} ` )
2017-03-06 12:05:41 +08:00
if err != nil {
t . Log ( err )
}
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
2017-04-25 18:43:10 +08:00
}
}
func TestSetCellStyleFont ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
err = xlsx . SetCellStyle ( "Sheet2" , "A1" , "A1" , ` { "font": { "bold":true,"italic":true,"family":"Berlin Sans FB Demi","size":36,"color":"#777777","underline":"single"}} ` )
if err != nil {
t . Log ( err )
}
err = xlsx . SetCellStyle ( "Sheet2" , "A2" , "A2" , ` { "font": { "italic":true,"underline":"double"}} ` )
if err != nil {
t . Log ( err )
}
err = xlsx . SetCellStyle ( "Sheet2" , "A3" , "A3" , ` { "font": { "bold":true}} ` )
if err != nil {
t . Log ( err )
}
err = xlsx . SetCellStyle ( "Sheet2" , "A4" , "A4" , ` { "font": { "bold":true,"family":"","size":0,"color":"","underline":""}} ` )
if err != nil {
t . Log ( err )
}
err = xlsx . SetCellStyle ( "Sheet2" , "A5" , "A5" , ` { "font": { "color":"#777777"}} ` )
if err != nil {
t . Log ( err )
}
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
2017-03-06 12:05:41 +08:00
}
}
2017-03-22 18:51:18 +08:00
func TestSetDeleteSheet ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_3.xlsx" )
if err != nil {
t . Log ( err )
}
xlsx . DeleteSheet ( "XLSXSheet3" )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
xlsx , err = OpenFile ( "./test/Workbook_4.xlsx" )
if err != nil {
t . Log ( err )
}
xlsx . DeleteSheet ( "Sheet1" )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-03-28 21:18:06 +08:00
func TestGetPicture ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
file , raw := xlsx . GetPicture ( "Sheet1" , "F21" )
if file == "" {
err = ioutil . WriteFile ( file , raw , 0644 )
if err != nil {
t . Log ( err )
}
}
// Try to get picture from a worksheet that doesn't contain any images.
file , raw = xlsx . GetPicture ( "Sheet3" , "I9" )
if file != "" {
err = ioutil . WriteFile ( file , raw , 0644 )
if err != nil {
t . Log ( err )
}
}
// Try to get picture from a cell that doesn't contain an image.
file , raw = xlsx . GetPicture ( "Sheet2" , "A2" )
t . Log ( file , len ( raw ) )
}
2017-04-04 19:12:35 +08:00
2017-04-26 11:43:39 +08:00
func TestSheetVisibility ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
xlsx . HideSheet ( "Sheet2" )
xlsx . HideSheet ( "Sheet1" )
xlsx . UnhideSheet ( "Sheet1" )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-04-04 19:12:35 +08:00
func TestCopySheet ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
err = xlsx . CopySheet ( 0 , - 1 )
if err != nil {
t . Log ( err )
}
xlsx . NewSheet ( 4 , "CopySheet" )
err = xlsx . CopySheet ( 1 , 4 )
if err != nil {
t . Log ( err )
}
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-04-23 00:10:23 +08:00
2017-04-28 15:49:41 +08:00
func TestAddTable ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
xlsx . AddTable ( "Sheet1" , "B26" , "A21" , ` ` )
xlsx . AddTable ( "Sheet2" , "A2" , "B5" , ` { "table_style":"TableStyleMedium2", "show_first_column":true,"show_last_column":true,"show_row_stripes":false,"show_column_stripes":true} ` )
xlsx . AddTable ( "Sheet2" , "F1" , "F1" , ` { "table_style":"TableStyleMedium8"} ` )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-04-30 20:03:43 +08:00
func TestAddShape ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook_2.xlsx" )
if err != nil {
t . Log ( err )
}
xlsx . AddShape ( "Sheet1" , "A30" , ` { "type":"rect","text":"Rectangle Shape"} ` )
xlsx . AddShape ( "Sheet3" , "H1" , ` { "type":"ellipseRibbon", "color": { "line":"#4286f4","fill":"#8eb9ff"}, "height": 90} ` )
err = xlsx . Save ( )
if err != nil {
t . Log ( err )
}
}
2017-04-23 00:10:23 +08:00
func TestAddChart ( t * testing . T ) {
xlsx , err := OpenFile ( "./test/Workbook1.xlsx" )
if err != nil {
t . Log ( err )
}
2017-04-23 00:39:14 +08:00
categories := map [ string ] string { "A30" : "Small" , "A31" : "Normal" , "A32" : "Large" , "B29" : "Apple" , "C29" : "Orange" , "D29" : "Pear" }
values := map [ string ] int { "B30" : 2 , "C30" : 3 , "D30" : 3 , "B31" : 5 , "C31" : 2 , "D31" : 4 , "B32" : 6 , "C32" : 7 , "D32" : 8 }
for k , v := range categories {
xlsx . SetCellValue ( "Sheet1" , k , v )
}
for k , v := range values {
xlsx . SetCellValue ( "Sheet1" , k , v )
}
2017-04-23 00:10:23 +08:00
xlsx . AddChart ( "SHEET1" , "P1" , ` { "type":"bar3D","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}, { "name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"}, { "name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"bottom","show_legend_key":false},"title": { "name":"Fruit 3D Bar Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"} ` )
xlsx . AddChart ( "SHEET1" , "X1" , ` { "type":"bar","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}, { "name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"}, { "name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"left","show_legend_key":false},"title": { "name":"Fruit Bar Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"} ` )
2017-04-23 00:39:14 +08:00
xlsx . AddChart ( "SHEET1" , "P16" , ` { "type":"doughnut","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"right","show_legend_key":false},"title": { "name":"Fruit Doughnut Chart"},"plotarea": { "show_bubble_size":false,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"zero"} ` )
2017-04-23 00:10:23 +08:00
xlsx . AddChart ( "SHEET1" , "X16" , ` { "type":"line","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}, { "name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"}, { "name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"top","show_legend_key":false},"title": { "name":"Fruit Line Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"} ` )
2017-04-23 00:39:14 +08:00
xlsx . AddChart ( "SHEET1" , "P30" , ` { "type":"pie3D","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"bottom","show_legend_key":false},"title": { "name":"Fruit 3D Pie Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"zero"} ` )
xlsx . AddChart ( "SHEET1" , "X30" , ` { "type":"pie","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"bottom","show_legend_key":false},"title": { "name":"Fruit Pie Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":false,"show_val":false},"show_blanks_as":"gap"} ` )
2017-04-23 00:10:23 +08:00
xlsx . AddChart ( "SHEET2" , "P1" , ` { "type":"radar","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}, { "name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"}, { "name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"top_right","show_legend_key":false},"title": { "name":"Fruit Radar Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"span"} ` )
xlsx . AddChart ( "SHEET2" , "X1" , ` { "type":"scatter","series":[ { "name":"=Sheet1!$A$30","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$30:$D$30"}, { "name":"=Sheet1!$A$31","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$31:$D$31"}, { "name":"=Sheet1!$A$32","categories":"=Sheet1!$B$29:$D$29","values":"=Sheet1!$B$32:$D$32"}],"format": { "x_scale":1.0,"y_scale":1.0,"x_offset":15,"y_offset":10,"print_obj":true,"lock_aspect_ratio":false,"locked":false},"legend": { "position":"bottom","show_legend_key":false},"title": { "name":"Fruit Scatter Chart"},"plotarea": { "show_bubble_size":true,"show_cat_name":false,"show_leader_lines":false,"show_percent":true,"show_series_name":true,"show_val":true},"show_blanks_as":"zero"} ` )
// Save xlsx file by the given path.
err = xlsx . WriteTo ( "./test/Workbook_6.xlsx" )
if err != nil {
t . Log ( err )
}
}