-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
89 lines (83 loc) · 1.7 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package tab
import (
"fmt"
"net/http"
"os"
"testing"
)
func TestExamples(t *testing.T) {
ExampleReadCSVFile()
ExampleReadXLSFile()
ExampleReadXLSXFile()
ExampleReadHtmlTable()
}
func ExampleReadCSVFile() {
filename := "testdata/test.csv"
file, err := os.Open(filename)
if err != nil {
panic(err)
return
}
os.Remove(filename)
tabulator := NewTabulator(WithReadFrom(CSV))
table, err := tabulator.Open(file)
if err != nil {
panic(err)
}
fmt.Println(table)
tabulator.Write(CSV, filename)
tabulator.Write(XLSX, "testdata/test.xlsx")
}
func ExampleReadXLSFile() {
filename := "testdata/test.xls"
file, err := os.Open(filename)
if err != nil {
panic(err)
return
}
//os.Remove(filename)
tabulator := NewTabulator(WithReadFrom(XLS))
table, err := tabulator.Open(file)
if err != nil {
panic(err)
}
fmt.Println(table)
//tabulator.Write(CSV, filename)
}
func ExampleReadXLSXFile() {
filename := "testdata/test.xlsx"
file, err := os.Open(filename)
if err != nil {
panic(err)
}
os.Remove(filename)
tabulator := NewTabulator(WithReadFrom(XLSX))
table, err := tabulator.Open(file)
if err != nil {
panic(err)
}
fmt.Println(table)
tabulator.Write(XLSX, filename)
}
func ExampleReadHtmlTable() {
url := "https://baike.baidu.com/item/%E4%B8%AD%E5%9B%BD%E4%BC%81%E4%B8%9A500%E5%BC%BA/5542706?fr=aladdin"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
if resp.Body != nil {
defer resp.Body.Close()
}
tabulator := NewTabulator(WithReadFrom(HtmlTable), WithTableIndex(0))
t, err := tabulator.Open(resp.Body)
if err != nil {
panic(err)
}
var file = "testdata/html-table.csv"
err = tabulator.Write(CSV, file)
if err != nil {
panic(err)
}
fmt.Println(t)
os.Remove(file)
}