-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
65 lines (50 loc) · 1.16 KB
/
dump.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
package main
import (
"encoding/csv"
"flag"
"os"
"io.bytenix.com/gsheetcsv/sheets"
)
type (
// DumpCommand represents a spreadsheet dump command
DumpCommand struct {
GSheetCommand
Flags struct {
Sheet string
Range string
TabSep bool
Formula bool
}
}
)
// Init initializes the DumpCommand structure and flags
func (c *DumpCommand) Init(f *flag.FlagSet) error {
if err := c.GSheetCommand.Init(); err != nil {
return err
}
f.StringVar(&c.Flags.Sheet, "sheet", "", "Google Sheet ID")
f.StringVar(&c.Flags.Range, "range", "", "Google Sheet Range")
f.BoolVar(&c.Flags.TabSep, "tab", false, "Use tab as fields delimiter")
f.BoolVar(&c.Flags.Formula, "formula", false, "Print formulas")
return nil
}
// Execute executes the DumpCommand business logic
func (c *DumpCommand) Execute() error {
rng, err := sheets.RangeFromString(c.Flags.Range)
if err != nil {
return err
}
values, err := GetSpreadsheetValues(c.OAuth2Client, c.Flags.Sheet, rng, c.Flags.Formula)
if err != nil {
return err
}
w := csv.NewWriter(os.Stdout)
if c.Flags.TabSep {
w.Comma = '\t'
}
for _, v := range values {
w.Write(v)
}
w.Flush()
return nil
}