-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveschoolUpdate.js
103 lines (86 loc) · 3.5 KB
/
LiveschoolUpdate.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
103
updatePoints();
function updatePoints() {
const { google } = require('googleapis');
const keys = require('/Users/vithulravivarma/MathnasiumApp/Mathnasium/Keys.json')
// google sheets api - javascript nodejs tutorial
// https://nodejs.org/en/
// https://code.visualstudio.com/docs/?dv=win
// https://developers.google.com/sheets/api/quickstart/nodejs
// console.developers.google.com
// https://www.googleapis.com/auth/spreadsheets
// Google sheet name 'sheet-test' under [email protected]
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/spreadsheets']
);
client.authorize(function (err) {
if (err) {
console.log(err);
return;
} else {
console.log('connected');
readAndEditSheet(client);
}
});
async function readAndEditSheet(authClient) {
const gsapi = google.sheets({ version: 'v4', auth: authClient });
//IDs from Data From Today
const idsPoints = {
spreadsheetId: '1dZ0nXkkSDKRbFak1osm4Ekq3F2VdX-xPMmlI6Do7J1w',
range: 'Data From Today!A2:A100'
};
//IDs from Data Mastersheet
const idsTotalPoints = {
spreadsheetId: '1dZ0nXkkSDKRbFak1osm4Ekq3F2VdX-xPMmlI6Do7J1w',
range: 'Data Mastersheet!A2:A100',
};
//points from Data From Today
const points = {
spreadsheetId: '1dZ0nXkkSDKRbFak1osm4Ekq3F2VdX-xPMmlI6Do7J1w',
range: 'Data From Today!C2:C100'
};
//points from Data Mastersheet
const totalPoints = {
spreadsheetId: '1dZ0nXkkSDKRbFak1osm4Ekq3F2VdX-xPMmlI6Do7J1w',
range: 'Data Mastersheet!C2:C100'
};
let totalPointsValue = await gsapi.spreadsheets.values.get(totalPoints);
let totalPointsArray = totalPointsValue.data.values;
for (i = 0; i < totalPointsArray.length; i++) {
totalPointsArray[i] = parseInt(totalPointsArray[i], 10);
}
//console.log(totalPointsArray);
let pointsValue = await gsapi.spreadsheets.values.get(points);
let pointsArray = pointsValue.data.values;
for (i = 0; i < pointsArray.length; i++) {
pointsArray[i] = parseInt(pointsArray[i], 10);
}
//Get student data sheet
let range;
let pointsId = await gsapi.spreadsheets.values.get(idsPoints);
let pointsIdArray = pointsId.data.values;
//console.log(pointsIdArray);
let totalPointsId = await gsapi.spreadsheets.values.get(idsTotalPoints);
let totalPointsIdArray = totalPointsId.data.values;
for (i = 0; i < pointsIdArray.length; i++) {
for (j = 0; j < totalPointsIdArray.length; j++) {
if ((parseInt(pointsIdArray[i], 10) == parseInt(totalPointsIdArray[j], 10))) {
range = "Data Mastersheet!C" + (j + 2);
totalPointsArray[j] += pointsArray[i];
updateLiveschoolPoints(range, totalPointsArray[j], gsapi);
}
}
}
}
function updateLiveschoolPoints(range, dataToUpdate, gsapi) {
const updater = {
spreadsheetId: '1dZ0nXkkSDKRbFak1osm4Ekq3F2VdX-xPMmlI6Do7J1w',
range: range,
valueInputOption: 'USER_ENTERED',
resource: { values: [[dataToUpdate]] }
};
let res = gsapi.spreadsheets.values.update(updater);
}
}