-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.go
145 lines (130 loc) · 3.53 KB
/
target.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
package xls2sheets
import (
"errors"
"fmt"
"io"
"log"
"mime"
"net/http"
"os"
"path/filepath"
"google.golang.org/api/drive/v3"
"google.golang.org/api/sheets/v4"
)
const userEntered = "USER_ENTERED"
const (
bakSuffix = ".bak" // backup file suffix, will be added to file
bakFileMode = 0666
)
var (
errEmptyRange = errors.New("empty source and/or target ranges")
errLengthMismatch = errors.New("source and target ranges have different lengths")
)
func debugPrintout(valueRange *sheets.ValueRange) {
for rowIdx := range valueRange.Values {
for colIdx := range valueRange.Values[rowIdx] {
fmt.Printf("%s,", valueRange.Values[rowIdx][colIdx])
}
fmt.Println()
}
}
// Update updates the target spreadsheet from source spreadsheet.
func (trg *Target) Update(client *http.Client, srcSheetID string, srcAddressRange []string) error {
log.Printf("updating data in target spreadsheet %s", trg.SpreadsheetID)
// TODO: copy everything from spreadsheet if sheetAddressRange and ts.SheetAddress is nil.
if len(srcAddressRange) == 0 || len(trg.SheetAddress) == 0 {
return errEmptyRange
}
if len(srcAddressRange) != len(trg.SheetAddress) {
return errLengthMismatch
}
sheetsService, err := sheets.New(client)
if err != nil {
return err
}
sourcer := sheetSvc{svc: sheetsService, spreadsheetID: srcSheetID}
updater := sheetSvc{svc: sheetsService, spreadsheetID: trg.SpreadsheetID}
// validation of SheetAddresses
if _, err := updater.validate(trg.SheetAddress, trg.Create); err != nil {
return err
}
for sheetIdx := range srcAddressRange {
log.Printf(" * copy range %q to %q", srcAddressRange[sheetIdx], trg.SheetAddress[sheetIdx])
// getting source values
values, err := sourcer.get(srcAddressRange[sheetIdx])
if err != nil {
return err
}
values.Range = trg.SheetAddress[sheetIdx]
if trg.Clear {
// clearing the spreadsheet
log.Print(" * clearing target sheet")
if _, err := updater.clear(trg.SheetAddress[sheetIdx]); err != nil {
return err
}
}
resp, err := updater.update(values)
if err != nil {
return err
}
log.Printf(" * OK: %d cells updated", resp.TotalUpdatedCells)
}
trg.Location = os.ExpandEnv(trg.Location)
if trg.Location != "" {
//save the file if location is set
log.Printf(" * exporting to %s", trg.Location)
if err := trg.download(client); err != nil {
log.Print(" * export FAILED")
return err
}
log.Print(" * export OK")
}
return nil
}
// download downloads the spreadsheet.
func (trg *Target) download(client *http.Client) error {
if trg.Location == "" {
return errors.New("target location is empty")
}
drv, err := drive.New(client)
if err != nil {
return err
}
resp, err := drv.Files.
Export(
trg.SpreadsheetID,
mime.TypeByExtension(filepath.Ext(trg.Location))).
Download()
if err != nil {
return err
}
if err := backup(trg.Location); err != nil {
return fmt.Errorf("error creating a backup: %s", err)
}
f, err := os.Create(trg.Location)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return err
}
return nil
}
func backup(filename string) error {
bakFilename := filename + bakSuffix
bak, err := os.Create(bakFilename)
if err != nil {
return fmt.Errorf("unable to overwrite backup file: %s", err)
}
defer bak.Close()
src, err := os.Open(filename)
if err != nil {
return fmt.Errorf("unable to open the local file for backup: %s", err)
}
defer src.Close()
if _, err := io.Copy(bak, src); err != nil {
return fmt.Errorf("failed to make a backup: %s", err)
}
return nil
}