-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
202 lines (186 loc) · 3.85 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"flag"
"fmt"
"github.com/tealeg/xlsx"
"math"
"os"
"strconv"
"strings"
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `
xl: Row oriented Excel operation tool
Usage:
xl -in [filepath]
or
xl -out [filepath]
Option:
`)
flag.PrintDefaults()
}
in := flag.String("in", "", "input excel file path")
out := flag.String("out", "", "output excel file path")
sep := flag.String("s", " ", "text column separater character")
sheet := flag.String("S", "", "operation sheet name")
base := flag.String("b", "", "base cell position")
flag.Parse()
if (*in == "" && *out == "") || (*in != "" && *out != "") {
flag.Usage()
}
cellPos, err := optToCellPos(*base)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
if *in != "" {
if err := excelToStdout(*in, *sep, *sheet, cellPos); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
}
if *out != "" {
if err := StdinToExcel(*out, *sep, *sheet, cellPos); err != nil {
fmt.Fprint(os.Stderr, err.Error())
os.Exit(1)
}
}
}
type CellPos struct {
Column int
Row int
}
func optToCellPos(opt string) (*CellPos, error) {
if opt == "" {
return &CellPos{}, nil
}
opts := strings.Split(opt, ",")
if len(opts) != 2 {
return nil, fmt.Errorf("Invalid option: %s", opt)
}
col, err := colIdxToI(opts[0])
if err != nil {
return nil, err
}
row, err := strconv.Atoi(opts[1])
if err != nil {
return nil, err
}
return &CellPos{col, row - 1}, nil
}
// if necessary convert A1 Format column index to R1C1 Format index.
func colIdxToI(idxChar string) (int, error) {
c := strings.ToUpper(idxChar)
d, err := strconv.Atoi(c)
if err == nil {
return d, nil
}
ret := 0
for i, r := range c {
if r < 'A' || 'Z' < r {
return 0, fmt.Errorf("%s is not a alphabet", r)
}
ret += int(math.Pow(float64(26), float64(len(c)-i-1))) * (int(r) - int('A') + 1)
}
return ret - 1, nil
}
func excelToStdout(inFilePath string, sep string, sheet string, cellPos *CellPos) error {
f, err := xlsx.OpenFile(inFilePath)
if err != nil {
return err
}
s := f.Sheets[0]
if sheet != "" {
s = f.Sheet[sheet]
}
if s == nil {
return fmt.Errorf("There is no sheet of '%s'", sheet)
}
for ridx, row := range s.Rows {
if ridx < cellPos.Row {
continue
}
fields := make([]string, 0)
for cidx, cell := range row.Cells {
if cidx < cellPos.Column {
continue
}
s := cell.String()
if err != nil {
return err
}
if s == "" {
break
}
fields = append(fields, s)
}
if len(fields) == 0 {
break
}
outStr := strings.Join(fields, sep)
fmt.Println(outStr)
}
return nil
}
func isExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func StdinToExcel(outFilePath string, sep string, sheet string, cellPos *CellPos) error {
var f *xlsx.File
var sh *xlsx.Sheet
if isExist(outFilePath) == false {
var err error
f = xlsx.NewFile()
sh, err = f.AddSheet("sheet1")
if err != nil {
return err
}
} else {
var err error
f, err = xlsx.OpenFile(outFilePath)
if err != nil {
return err
}
sh = f.Sheets[0]
if sheet != "" {
sh = f.Sheet[sheet]
}
if sh == nil {
return fmt.Errorf("There is no sheet of '%s'", sheet)
}
}
rowIdx := cellPos.Row
for i := 0; i < cellPos.Row; i++ {
sh.AddRow()
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
s := scanner.Text()
strs := strings.Split(s, sep)
for i, v := range strs {
var row *xlsx.Row
if len(sh.Rows) <= rowIdx {
row = sh.AddRow()
} else {
row = sh.Rows[rowIdx]
}
var cell *xlsx.Cell
for len(row.Cells) <= i + cellPos.Column {
cell = row.AddCell()
}
cell = row.Cells[i+cellPos.Column]
cell.Value = v
}
rowIdx++
}
if err := scanner.Err(); err != nil {
return err
}
if err := f.Save(outFilePath); err != nil {
return err
}
return nil
}