-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (36 loc) · 1.05 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
'use strict;'
let fetch
try {
fetch = window.fetch
} catch (e) {
fetch = require('node-fetch')
}
const buildGDocsURL = (key) => (
`https://spreadsheets.google.com/feeds/list/${key}/od6/public/values?alt=json`
)
const getGDocsData = (spreadsheetId, page = 0, limit = 50) => (
fetch(buildGDocsURL(spreadsheetId))
.then(res => res.json())
.then(data => {
const entries = Object.values(data.feed.entry)
const [firstRow, ...rows] = entries
const keyDescriptorsKeys = Object.keys(firstRow)
.filter(key => key.match(/gsx\$/))
const keys = keyDescriptorsKeys.reduce((acc, key) => {
return Object.assign(acc, {
[key]: firstRow[key]['$t'] || 'id'
})
}, {})
return rows.slice(page * limit, page * limit + limit).map(entry => (
keyDescriptorsKeys.reduce((acc, key) => {
if (!entry[key]) {
return acc
}
return Object.assign(acc, {
[keys[key]]: entry[key]['$t']
})
}, {})
))
})
)
module.exports = getGDocsData