2016-08-30 11:51:31 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
2016-08-30 14:00:21 +08:00
|
|
|
"strconv"
|
2016-08-30 11:51:31 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExcelize(t *testing.T) {
|
2016-10-19 18:42:29 +08:00
|
|
|
// Test update a XLSX file.
|
|
|
|
f1, 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.
|
|
|
|
rows := f1.GetRows("Sheet4")
|
|
|
|
// Test get all the rows in a sheet.
|
|
|
|
rows = f1.GetRows("Sheet2")
|
|
|
|
for _, row := range rows {
|
|
|
|
for _, cell := range row {
|
|
|
|
t.Log(cell, "\t")
|
|
|
|
}
|
|
|
|
t.Log("\r\n")
|
|
|
|
}
|
2017-01-12 03:43:31 +08:00
|
|
|
f1.UpdateLinkedValue()
|
|
|
|
f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32))
|
|
|
|
f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
|
2016-10-19 18:42:29 +08:00
|
|
|
f1.SetCellInt("SHEET2", "A1", 100)
|
|
|
|
f1.SetCellStr("SHEET2", "C11", "Knowns")
|
2016-12-23 17:47:25 +08:00
|
|
|
f1.NewSheet(3, "Maximum 31 characters allowed in sheet title.")
|
2016-10-19 18:42:29 +08:00
|
|
|
f1.SetCellInt("Sheet3", "A23", 10)
|
|
|
|
f1.SetCellStr("SHEET3", "b230", "10")
|
|
|
|
f1.SetCellStr("SHEET10", "b230", "10")
|
|
|
|
f1.SetActiveSheet(2)
|
2016-12-24 22:47:36 +08:00
|
|
|
f1.GetCellFormula("Sheet1", "B19") // Test get cell formula with given rows number.
|
|
|
|
f1.GetCellFormula("Sheet2", "B20") // Test get cell formula with illegal sheet index.
|
|
|
|
f1.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.
|
|
|
|
f1.GetCellValue("Sheet2", "a-1")
|
|
|
|
// Test read cell value with given lowercase column number.
|
|
|
|
f1.GetCellValue("Sheet2", "a5")
|
|
|
|
f1.GetCellValue("Sheet2", "C11")
|
|
|
|
f1.GetCellValue("Sheet2", "D11")
|
|
|
|
f1.GetCellValue("Sheet2", "D12")
|
|
|
|
// Test SetCellValue function.
|
|
|
|
f1.SetCellValue("Sheet2", "F1", "Hello")
|
|
|
|
f1.SetCellValue("Sheet2", "G1", []byte("World"))
|
|
|
|
f1.SetCellValue("Sheet2", "F2", 42)
|
2016-11-24 11:42:51 +08:00
|
|
|
f1.SetCellValue("Sheet2", "F2", int8(42))
|
|
|
|
f1.SetCellValue("Sheet2", "F2", int16(42))
|
|
|
|
f1.SetCellValue("Sheet2", "F2", int32(42))
|
|
|
|
f1.SetCellValue("Sheet2", "F2", int64(42))
|
2017-01-12 03:43:31 +08:00
|
|
|
f1.SetCellValue("Sheet2", "F2", float32(42.65418))
|
|
|
|
f1.SetCellValue("Sheet2", "F2", float64(-42.65418))
|
2016-11-24 11:42:51 +08:00
|
|
|
f1.SetCellValue("Sheet2", "F2", float32(42))
|
|
|
|
f1.SetCellValue("Sheet2", "F2", float64(42))
|
2016-10-19 18:42:29 +08:00
|
|
|
f1.SetCellValue("Sheet2", "G2", nil)
|
2016-10-23 16:07:57 +08:00
|
|
|
// Test completion column.
|
|
|
|
f1.SetCellValue("Sheet2", "M2", nil)
|
2016-10-19 18:42:29 +08:00
|
|
|
// Test read cell value with given axis large than exists row.
|
2016-10-23 16:46:46 +08:00
|
|
|
f1.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.
|
|
|
|
f1.GetSheetName(f1.GetActiveSheetIndex())
|
|
|
|
// Test get sheet name of XLSX by given invalid sheet index.
|
|
|
|
f1.GetSheetName(4)
|
|
|
|
// Test get sheet map of XLSX.
|
|
|
|
f1.GetSheetMap()
|
2016-09-06 21:20:24 +08:00
|
|
|
|
2016-09-05 16:37:15 +08:00
|
|
|
for i := 1; i <= 300; i++ {
|
2016-10-19 18:42:29 +08:00
|
|
|
f1.SetCellStr("SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|
2016-10-19 18:42:29 +08:00
|
|
|
err = f1.Save()
|
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
|
|
|
}
|
2016-10-19 18:42:29 +08:00
|
|
|
// Test write file to given path.
|
|
|
|
err = f1.WriteTo("./test/Workbook_2.xlsx")
|
2016-09-02 10:28:29 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
2016-10-19 18:42:29 +08:00
|
|
|
// Test write file to not exist directory.
|
|
|
|
err = f1.WriteTo("")
|
2016-09-02 10:28:29 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
2016-08-30 11:51:31 +08:00
|
|
|
|
2016-10-19 18:42:29 +08:00
|
|
|
// Test write file with broken file struct.
|
|
|
|
f2 := File{}
|
|
|
|
err = f2.Save()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
// Test write file with broken file struct with given path.
|
|
|
|
err = f2.WriteTo("./test/Workbook_3.xlsx")
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test create a XLSX file.
|
|
|
|
f3 := CreateFile()
|
|
|
|
f3.NewSheet(2, "XLSXSheet2")
|
|
|
|
f3.NewSheet(3, "XLSXSheet3")
|
|
|
|
f3.SetCellInt("Sheet2", "A23", 56)
|
|
|
|
f3.SetCellStr("SHEET1", "B20", "42")
|
|
|
|
f3.SetActiveSheet(0)
|
|
|
|
err = f3.WriteTo("./test/Workbook_3.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
|
|
|
}
|
2016-08-30 21:54:28 +08:00
|
|
|
|
2016-10-23 16:46:46 +08:00
|
|
|
// Test set active sheet without BookViews and Sheets maps in xl/workbook.xml.
|
|
|
|
f4, err := OpenFile("./test/badWorkbook.xlsx")
|
|
|
|
f4.SetActiveSheet(2)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
|
2016-10-19 18:42:29 +08:00
|
|
|
// Test open a XLSX file with given illegal path.
|
2016-09-06 21:20:24 +08:00
|
|
|
_, err = OpenFile("./test/Workbook.xlsx")
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
2016-08-30 11:51:31 +08:00
|
|
|
}
|