-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (90 loc) · 2.08 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"flag"
"os"
"fmt"
"github.com/tealeg/xlsx"
"strings"
"strconv"
"github.com/olekukonko/tablewriter"
)
func min(a, b int64) int64 {
if a > b {
return b
}
return a
}
func main() {
sheet := flag.String("s", "1", "sheet name or number")
rn := flag.String("r", "1,10", "row numbers")
cn := flag.String("c", "1,10", "cell numbers, you must give number instead of letters")
flag.Parse()
args := os.Args[1:]
fname := args[len(args)-1]
if 0 == len(fname) {
fmt.Println("file name is missing")
os.Exit(-1)
}
rns := strings.Split(*rn, ",")
sr, err := strconv.ParseInt(rns[0], 10, 64)
if err != nil {
fmt.Println("rows start number is not a number")
os.Exit(-1)
}
er, err := strconv.ParseInt(rns[1], 10, 64)
if err != nil {
fmt.Println("rows end number is not a number")
os.Exit(-1)
}
cns := strings.Split(*cn, ",")
sc, err := strconv.ParseInt(cns[0], 10, 64)
if err != nil {
fmt.Println("cell start number is not a number")
os.Exit(-1)
}
ec, err := strconv.ParseInt(cns[1], 10, 64)
if err != nil {
fmt.Println("cell end number is not a number")
os.Exit(-1)
}
fp, err := xlsx.OpenFile(fname)
if err != nil {
fmt.Println("file not found")
os.Exit(-1)
}
var sh *xlsx.Sheet
// they give us sheet number
if pn, err := strconv.ParseInt(*sheet, 10, 64); err == nil {
if 0 < pn {
pn -= 1
}
sh = fp.Sheets[pn]
} else {
// they give us sheet's name (how they know sheets name?, who cares, they know somehow)
var ok bool
sh, ok = fp.Sheet[*sheet]
if !ok {
fmt.Println("sheet not found")
os.Exit(-1)
}
}
if 0 < sr {
sr -= 1
}
if 0 < sc {
sc -= 1
}
table := tablewriter.NewWriter(os.Stdout)
for _, row := range sh.Rows[sr:min(int64(len(sh.Rows)), int64(er - 1))] {
data := make([]string, 0)
for _, cell := range row.Cells[sc:min(ec - 1, int64(len(row.Cells)))] {
if str, err := cell.FormattedValue(); err == nil {
data = append(data, strings.TrimSpace(str[0:min(int64(15), int64(len(str)))]))
}
}
table.Append(data)
}
table.SetRowLine(true)
table.SetRowSeparator("-")
table.Render()
}