-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-spreadsheet.js
73 lines (63 loc) · 1.75 KB
/
create-spreadsheet.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
const mysql = require('mysql');
const GoogleSpreadsheet = require('google-spreadsheet');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'devathon'
});
connection.connect();
const doc = new GoogleSpreadsheet('1Mb1gFpii-yxZw0bEnwLQdvaNZl-f_33qkX-_HkSibug');
let sheet;
function auth() {
doc.useServiceAccountAuth(require('./service-account-details.json'), (err) => {
if (err) {
throw err;
}
getInfo();
});
}
function getInfo() {
doc.getInfo((err, info) => {
if (err) {
throw err;
}
console.log(`Editing ${info.title} by ${info.author.name} ${info.author.email}`);
sheet = info.worksheets[0];
updateSheet();
});
}
function updateSheet() {
// retrieve stuff from mysql
connection.query('SELECT `username` FROM `users`', (err, rows) => {
if (err) {
throw err;
}
const usernames = rows.map(({username}) => username);
sheet.getCells({
'min-row': 2,
'max-row': usernames.length + 1,
'return-empty': true
}, (err, cells) => {
if (err) {
throw err;
}
const width = 26;
usernames.forEach((username, index) => {
const rowStart = index * width;
cells[rowStart].value = username;
cells[rowStart + 1].value = `https://github.com/JoinDevathon/${username}-2016`;
});
sheet.bulkUpdateCells(cells, (err) => {
if (err) {
throw err;
}
end();
});
});
});
}
function end() {
connection.end();
}
auth();