-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
48 lines (42 loc) · 1.13 KB
/
index.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
const { google } = require('googleapis')
class GoogleSheetSource {
static defaultOptions() {
return {
sheetId: '',
apiKey: '',
type: 'googleSheet',
}
}
constructor(api, options = GoogleSheetSource.defaultOptions()) {
this.options = options
api.loadSource(async store => {
const contentType = store.addCollection({
typeName: this.options.type
})
const sheets = google.sheets({
version: 'v4',
auth: this.options.apiKey,
})
await sheets.spreadsheets.values
.get({
spreadsheetId: this.options.sheetId,
range: 'A1:ZZ10000',
})
.then(response => {
const data = response.data.values
const titles = data.shift()
const nodes = data.map(value => {
return titles.reduce(
(title, key, index) => ({ ...title, [key]: value[index] }),
{}
)
})
nodes.map((value, key, title) => {
contentType.addNode(value)
})
})
.catch(err => console.log(err))
})
}
}
module.exports = GoogleSheetSource