-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogleSheetPlugin.js
More file actions
64 lines (57 loc) · 2.23 KB
/
googleSheetPlugin.js
File metadata and controls
64 lines (57 loc) · 2.23 KB
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
62
63
64
var googlesheetDatasource = function (settings, updateCallback) {
var self = this;
var currentSettings = settings;
function convertToJSON(value) {
var stArray = value.split(', ');
var obj = {};
for (var i in stArray) {
var sig = stArray[i].split(": ");
obj[sig[0]] = sig[1];
}
return obj;
}
this.updateNow = function () {
var currUrl = "https://thingproxy.freeboard.io/fetch/https://spreadsheets.google.com/feeds/list/"+currentSettings.sheet_key+"/"+currentSettings.worksheet_id+"/public/basic?alt=json";
$.ajax({
url: currUrl,
dataType: "JSON",
success: function (data) {
data = data.feed.entry.map(entry => convertToJSON(entry["content"]["$t"]));
var obj = {};
Object.keys(data[0]).forEach(key => obj[key] = []);
data.forEach(entry => Object.keys(entry).forEach(key => obj[key].push(entry[key])));
updateCallback(obj);
},
error: function (xhr, status, error) {
}
});
}
this.onDispose = function () {
}
this.onSettingsChanged = function (newSettings) {
currentSettings = newSettings;
self.updateNow();
}
};
freeboard.loadDatasourcePlugin({
"type_name": "googlesheet",
"display_name": "Google Sheet",
"settings": [
{
"name": "sheet_key",
"display_name": "Sheet Key",
"type": "text",
"default_value": "1tQUMopQ4jFXAEaJ_lMEJ45YnGrBB0iYwnawQJsYPh4o"
},
{
"name":"worksheet_id",
"display_name":"Worksheet ID",
"type":"text",
"default_value": "1",
description: "The number of the worksheet, 1 corresponds to the first worksheet"
}
],
newInstance: function (settings, newInstanceCallback, updateCallback) {
newInstanceCallback(new googlesheetDatasource(settings, updateCallback));
}
});