-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (73 loc) · 2.29 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var Spreadsheet = require('edit-google-spreadsheet');
var config = require('./config');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/spreadsheet', function (req, res) {
var token = req.body.token;
var pageName = req.body.pageName;
// pseudo-login ::::::::::::::::::::::
var page;
if (!token || !pageName) {
return res.send(404);
}
else {
page = config.pages[pageName];
if (!page){
return res.send(404);
}
else {
if (page.token !== token){
return res.send(404);
}
console.log("Authorised request!");
}
}
// pseudo-login ::::::::::::::::::::::
// TODO save form data in excel file!
var sheetId = req.body.sheetId;
var worksheetId = req.body.worksheetId;
// After executing Spreadsheet.load with debug:true, you can see as an advice the values of spreadsheetId and worksheetId
Spreadsheet.load({
debug: true,
spreadsheetId: sheetId,
worksheetId: worksheetId,
// spreadsheetName: 'test_ss',
// worksheetName: 'Sheet1',
oauth2: config.oauth2
}, function (err, spreadsheet) {
if(err) throw err;
spreadsheet.receive(function(err, rows, info) {
if(err) throw err;
res.send({rows: rows});
});
});
});
app.get('/spreadsheet', function (req, res) {
// After executing Spreadsheet.load with debug:true, you can see as an advice the values of spreadsheetId and worksheetId
Spreadsheet.load({
debug: true,
spreadsheetId: '1iY6NDn7jIR-5KO7lPLO1ybP2Jj5_RM8ACOJA4ZuXX-Y',
worksheetId: 'od6',
// spreadsheetName: 'test_ss',
// worksheetName: 'Sheet1',
oauth2: config.oauth2
}, function (err, spreadsheet) {
if(err) throw err;
spreadsheet.receive(function(err, rows, info) {
if(err) throw err;
res.send({rows: rows});
});
});
});
var server = app.listen(3000, function () {
var port = server.address().port;
console.log('Google Spreadsheet Bridge app listening at http://localhost:%s', port);
});