forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
50 lines (45 loc) · 1.06 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
// Copyright 2017 Baliance. All rights reserved.
package main
import (
"encoding/csv"
"flag"
"log"
"os"
"baliance.com/gooxml/spreadsheet"
)
func main() {
raw := flag.Bool("raw", false, "print raw values instead of formatted")
flag.Parse()
if flag.NArg() != 1 {
log.Fatalf("pass a single document as a parameter")
}
wb, err := spreadsheet.Open(flag.Arg(0))
if err != nil {
log.Fatalf("error opening: %s", err)
}
for _, sheet := range wb.Sheets() {
f, err := os.Create(sheet.Name() + ".csv")
if err != nil {
log.Fatalf("error creating sheet: %s", err)
}
cw := csv.NewWriter(f)
sc, _, ec, _ := sheet.ExtentsIndex()
scIdx := spreadsheet.ColumnToIndex(sc)
ecIdx := spreadsheet.ColumnToIndex(ec)
for _, r := range sheet.Rows() {
record := []string{}
for c := scIdx; c <= ecIdx; c++ {
cell := r.Cell(spreadsheet.IndexToColumn(c))
if !*raw {
record = append(record, cell.GetFormattedValue())
} else {
v, _ := cell.GetRawValue()
record = append(record, v)
}
}
cw.Write(record)
}
cw.Flush()
f.Close()
}
}