-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
61 lines (56 loc) · 1.8 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
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
AUTHOR : avimehenwal
DATED : 10th-April-2020
PURPOSE: Make data reusable from public google sheets
Script takes following 3 inputs and returns items variable with all sheet data
Mandatory Inputs Arguments
---------------------
1. SHEETID - ID of google sheet, kindle refer README on how to fetch it
2. COLUMNS - Number of colums on given google sheet
3. sheetPageNumber - Google sheet Page Number, DEFAULT - 1
NOTE: All input variables in BOLD are mandatory to fetch correct data
*/
import axios from 'axios'
export const vueGsheets = {
created () {
this.fetchData()
},
data: () => ({
items: [],
headers: [],
records: null,
COLUMNS: 3,
sheetPageNumber: 1,
SHEETID: '1Yc2esnockqfrNweacmegXnavuPly8PvjaRzqlRzaXTE'
}),
computed: {
getURL () {
return 'https://spreadsheets.google.com/feeds/cells/' +
this.SHEETID + '/' + this.sheetPageNumber + '/public/full?alt=json'
}
},
methods: {
async fetchData () {
// eslint-disable-next-line no-unused-vars
const data = await axios.get(this.getURL)
.then(response => {
const entry = response.data.feed.entry
this.records = (entry.length / this.COLUMNS) - 1
for (let i = 0; i < this.COLUMNS; i++) {
this.headers.push(entry[i].content.$t)
}
for (let i = this.headers.length; i < entry.length; i += this.COLUMNS) {
const item = {}
for (let j = 0; j < this.headers.length; j++) {
// entry[i].content.$t retrieves the content of each cell
item[this.headers[j]] = entry[i + j].content.$t
}
this.items.push(item)
}
})
.catch(error => {
console.log(error)
})
}
}
}