forked from spenwall/gridsome-source-google-sheets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (40 loc) · 1.25 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
const { google } = require('googleapis')
class GoogleSheetSource {
constructor(api, options) {
const spreadsheetData = google.sheets({
version: 'v4',
auth: options.apiKey,
})
options.spreadsheets.forEach(spreadsheet => {
spreadsheet.sheets.forEach(sheet => {
api.loadSource(async actions => {
const collection = actions.addCollection(sheet.collectionName)
await spreadsheetData.spreadsheets.values
.get({
spreadsheetId: spreadsheet.spreadsheetId,
range: `'${sheet.sheetName}'`,
})
.then(response => {
const sheetData = response.data.values
const titles = sheetData.shift()
const nodes = sheetData.map((value, nodeIndex) => {
return titles.reduce(
(title, key, index) => ({
...title,
[key]: value[index],
id: nodeIndex,
}),
{}
)
})
nodes.map(value => {
collection.addNode(value)
})
})
.catch(err => console.log(err))
})
})
})
}
}
module.exports = GoogleSheetSource