-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets.js
61 lines (52 loc) · 1.13 KB
/
sheets.js
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
import GoogleSpreadsheet from 'google-spreadsheet'
const checkEnv = () => new Promise((resolve, reject) => {
try {
const {GOOGLE_CLIENT_EMAIL, GOOGLE_PRIVATE_KEY, GOOGLE_SHEET_KEY} = process.env
const GOOGLE = {
client_email: GOOGLE_CLIENT_EMAIL,
private_key: GOOGLE_PRIVATE_KEY,
sheet_key: GOOGLE_SHEET_KEY,
}
resolve(GOOGLE)
} catch(err) {
reject(err)
}
})
const newSheet = (key) => new Promise((resolve, reject) => {
try {
const sheet = new GoogleSpreadsheet(key)
resolve(sheet)
} catch(err) {
reject(err)
}
})
const authSheet = (sheet, creds) => new Promise((resolve, reject) => {
sheet.useServiceAccountAuth(creds, function(err) {
if (err) {
reject(err)
} else {
resolve(sheet)
}
})
})
const auth = () => new Promise(function (resolve, reject) {
checkEnv()
.then(googleCreds => {
newSheet(googleCreds.sheet_key)
.then(function(sheet) {
authSheet(sheet, googleCreds)
.then(resolve)
.catch(reject)
})
.catch(reject)
})
.catch(reject)
})
const addRow = (sheet, data) => {
sheet.addRow(1, data)
}
const sheets = {
auth,
addRow,
}
export default sheets