-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgit-xlsx-textconv.go
57 lines (48 loc) · 1.01 KB
/
git-xlsx-textconv.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
package main
import (
"fmt"
"io"
"log"
"os"
"strings"
xlsx "github.com/tealeg/xlsx"
)
func textconv(filename string, w io.Writer) error {
xlFile, err := xlsx.OpenFile(filename)
if err != nil {
return err
}
for _, sheet := range xlFile.Sheets {
for _, row := range sheet.Rows {
if row == nil {
continue
}
cels := make([]string, len(row.Cells))
for i, cell := range row.Cells {
var s string
if cell.Type() == xlsx.CellTypeStringFormula {
s = cell.Formula()
} else {
s = cell.String()
}
s = strings.Replace(s, "\\", "\\\\", -1)
s = strings.Replace(s, "\n", "\\n", -1)
s = strings.Replace(s, "\r", "\\r", -1)
s = strings.Replace(s, "\t", "\\t", -1)
cels[i] = s
}
fmt.Fprintf(w, "[%s] %s\n", sheet.Name, strings.Join(cels, "\t"))
}
}
return nil
}
func main() {
if len(os.Args) != 2 {
log.Fatal("Usage: git-xlsx-textconv file.xlsx")
}
excelFileName := os.Args[1]
err := textconv(excelFileName, os.Stdout)
if err != nil {
log.Fatal(err)
}
}