-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheets.js
92 lines (85 loc) · 1.97 KB
/
sheets.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
const { run } = require("./auth");
const debug = require("debug");
const Promise = require("bluebird");
const { google } = require("googleapis");
async function getspreadsheetIdArea(a1Notation, sheetId) {
return new Promise((resolve, recject) => {
run(
getspreadsheetIdAreaInternal.bind(
null,
a1Notation,
sheetId,
resolve,
recject
)
);
});
}
async function getspreadsheetIdAreaInternal(
spreadsheetId,
a1Notation,
resolve,
reject,
auth
) {
try {
const sheets = google.sheets({ version: "v4", auth });
sheets.spreadsheets.values.get(
{
spreadsheetId: spreadsheetId,
range: a1Notation
},
(err, response) => {
if (err) {
reject(err);
} else {
resolve(response);
}
}
);
} catch (e) {
reject(e);
}
}
const getMatchNr = async (spreadsheetId, courtId) => {
try {
const result = await getspreadsheetIdArea(spreadsheetId, "LiveScore!A:I");
// tomme verdier er borte....!
// Colonne E => 4 => bane colonnen
const rows = result.data.values;
const centerCourtMatches = rows.filter(row => row[4] === courtId);
let match;
const matchInPlay = centerCourtMatches.filter(
row => row[row.length - 1] === "Igang"
);
if (matchInPlay.length == 0) {
match = centerCourtMatches.filter(
row => row[row.length - 1] === "Klargjort"
);
} else {
match = matchInPlay;
}
if (match && match[0] && match[0][0]) {
return match[0][0];
} else {
console.log(`Tom, match er tom ${match}`);
}
} catch (err) {
console.log(err);
return -1;
}
};
const getTournamentId = async spreadsheetId => {
try {
const result = await getspreadsheetIdArea(spreadsheetId, "Config!D2");
const rows = result.data.values;
return rows[0][0];
} catch (err) {
console.log(err);
return -1;
}
};
module.exports = {
getMatchNr,
getTournamentId
};