/
main.go
156 lines (133 loc) · 3.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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"sync"
"github.com/tealeg/xlsx"
"github.com/urfave/cli"
)
func readWriteSheet(inputFilePath, outputDirPath string) error {
xlFile, err := xlsx.OpenFile(inputFilePath)
if err != nil {
return err
}
for _, sheet := range xlFile.Sheets {
// sheet単位でfile生成
fmt.Printf("Start %s ...\n", sheet.Name)
writeFilePath := strings.Join([]string{outputDirPath, sheet.Name}, "/") + ".md"
f, err := os.Create(writeFilePath)
if err != nil {
log.Fatal(err)
}
hyou := false
// rowはまとめて1行にする
for rowIdx, row := range sheet.Rows {
if rowIdx == 0 {
// #見出し
f.WriteString("# ")
}
text := ""
for _, cell := range row.Cells {
text += cell.Value
}
if len(text) == 0 {
hyou = false
f.WriteString("\n")
f.WriteString("## ")
continue
}
if len(row.Cells) >= 2 && len(row.Cells[0].Value) == 0 {
f.WriteString("- ")
idx := 1
f.WriteString(row.Cells[idx].Value)
} else if len(row.Cells) >= 2 {
// 表
for _, cell := range row.Cells {
f.WriteString("|")
f.WriteString(cell.Value)
}
f.WriteString("|")
if !hyou {
f.WriteString("\n")
f.WriteString(strings.Repeat("| --- ", len(row.Cells)))
f.WriteString("|")
hyou = true
}
} else if strings.HasPrefix(row.Cells[0].Value, "http") {
f.WriteString(fmt.Sprintf("![%s](%s)", row.Cells[0].Value, row.Cells[0].Value))
f.WriteString("\n")
} else {
// その他
f.WriteString(text)
f.WriteString("\n")
}
f.WriteString("\n")
}
fmt.Printf("End %s => %s\n", sheet.Name, writeFilePath)
f.Close()
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "excel-to-markdown"
app.Version = Version
app.Usage = ""
app.Author = "kyokomi"
app.Email = "kyoko1220adword@gmail.com"
app.Action = doMain
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "input-dir,i",
Value: "",
Usage: "convert target directory path",
},
cli.StringFlag{
Name: "output-dir,o",
Value: "",
Usage: "dist directory after convert path",
},
}
app.Run(os.Args)
}
func doMain(c *cli.Context) error {
inputDirPath := c.String("input-dir")
outputDirPath := c.String("output-dir")
if inputDirPath == "" || outputDirPath == "" {
cli.ShowAppHelp(c)
return nil
}
d, err := ioutil.ReadDir(inputDirPath)
if err != nil {
return err
}
var wg sync.WaitGroup
for _, file := range d {
if file.IsDir() {
continue
}
inputFilePath := strings.Join([]string{inputDirPath, file.Name()}, "/")
if !strings.HasSuffix(inputFilePath, ".xlsx") {
fmt.Println("error don't xlsx file.")
continue
}
outputDirPath := strings.Join([]string{outputDirPath, file.Name()}, "/")
outputDirPath = strings.TrimSuffix(outputDirPath, ".xlsx")
if _, err := ioutil.ReadDir(outputDirPath); err != nil {
err := os.Mkdir(outputDirPath, 0755)
if err != nil {
return err
}
}
wg.Add(1)
go func(inputFilePath string) {
readWriteSheet(inputFilePath, outputDirPath)
wg.Done()
}(inputFilePath)
}
wg.Wait()
return nil
}