-
Notifications
You must be signed in to change notification settings - Fork 1
/
sheets_api.js
102 lines (90 loc) · 2.51 KB
/
sheets_api.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
97
98
99
100
101
102
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const {JWT} = require('google-auth-library');
const Const = require("./constants.js");
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets'];
const TOKEN_PATH = 'token.json';
function authorize(callback) {
const jwtClient = new JWT({
email: process.env.SERVICE_ACCOUNT_EMAIL,
key: process.env.SERVICE_ACCOUNT_KEY.replace(/\\n/g, '\n'),
scopes: SCOPES,
subject: null,
});
jwtClient.authorize(() => callback());
return jwtClient;
}
function callback() {
console.log("Sheets API connected.");
}
console.log("Sheets API connecting...");
const auth = authorize(callback);
module.exports.sheets = google.sheets({version: 'v4', auth});
function getID(sheet) {
if (sheet === "joueurs") {
return Const.JOUEURS_SID;
}
else if (sheet === "niveaux") {
return Const.NIVEAUX_SID;
}
else if (sheet === "blames") {
return Const.BLAMES_SID;
}
return -1;
}
function parseRange(range) {
const regex = /$?([a-z]+)$?(\d+)/i;
const parts = range.split('!');
const cells = parts[1].split(':');
const [from_raw_az, from_nb] = cells[0].match(regex);
const [to_raw_az, to_nb] = cells[1].match(regex);
const from_az = from_raw_az[from_raw_az.length === 1 ? 0 : 1].charCodeAt(0) - 65 + 26 * (from_raw_az[0].charCodeAt(0) - 64);
const to_az = to_raw_az[to_raw_az.length === 1 ? 0 : 1].charCodeAt(0) - 65 + 26 * (to_raw_az[0].charCodeAt(0) - 64);
return {
sheetId: getID(parts[0]),
startColumnIndex: from_az,
endColumnIndex: to_az,
startRowIndex: from_nb,
endRowIndex: to_nb
}
}
module.exports.formatDate = function (range, param=null) {
return {
repeatCell: {
range: parseRange(range),
cell: {
userEnteredFormat: {
numberFormat: {
type: "DATE",
pattern: "dd/mm/yyyy hh:mm:ss"
}
}
},
fields: "userEnteredFormat.numberFormat"
}
}
}
module.exports.updateCells = function (range, param) {
return {
updateCells: {
range: parseRange(range),
fields: 'userEnteredValue.stringValue',
rows: [{
values: [{
userEnteredValue: {
stringValue: param
}
}]
}]
}
}
}
module.exports.batchUpdate = function (batch) {
return sheets.spreadsheets.batchUpdate({
spreadsheetId: process.env.SPREADSHEET_ID,
resource: {
requests: [batch.map((b) => b.request(b.range, b.param))]
}
});
}