-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsheetHelper.js
63 lines (50 loc) · 1.36 KB
/
sheetHelper.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
const Tabletop = require('tabletop');
const random = require('random');
const config = require('./config');
const bot = require('./bot');
let rows = null;
let tabletop = null;
function tweetRandom(callback = () => {}) {
const randomInt = random.int(0, rows.length - 1);
const row = getRowAtIdx(randomInt);
if (row == null || row[config.headerName] == null) {
console.log('unreadable row');
return;
}
const value = row[config.headerName];
console.log(`Random at idx:${randomInt} is "${value}"`);
bot.tweet(value, callback);
}
function getRowAtIdx(idx) {
if (rows == null || rows.length < 1) {
console.log('spreadsheet empty');
return;
}
console.log(`Found ${rows.length} rows`);
if (idx < 0 || idx >= rows.length) {
console.log('bad index, out of range');
return null;
}
const row = rows[idx];
if (row == null) {
console.log('unreadable row');
return null;
}
return row;
}
function init(callback = () => {}) {
Tabletop.init({
key: config.googleSheetUrl,
callback: (data, _tabletop) => {
rows = data;
tabletop = _tabletop;
callback(rows, tabletop);
},
simpleSheet: true
});
}
module.exports = {
init: init,
tweetRandom: tweetRandom,
getRowAtIdx: getRowAtIdx,
};