-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets_integration.js
94 lines (76 loc) · 2.56 KB
/
sheets_integration.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
const { google } = require("googleapis");
const dotenv = require("dotenv");
const math = require("mathjs");
dotenv.config();
async function configureGoogleSheetsAPI() {
const auth = new google.auth.GoogleAuth({
keyFile: process.env.GOOGLE_APPLICATION_CREDENTIALS,
scopes: ["https://www.googleapis.com/auth/spreadsheets"],
});
const client = await auth.getClient();
const googleSheets = google.sheets({ version: "v4", auth: client });
return googleSheets;
}
async function retrieveData(googleSheets, spreadsheetId, range) {
const response = await googleSheets.spreadsheets.values.get({
spreadsheetId,
range,
});
return response.data.values;
}
function processStudentData(data) {
if (!Array.isArray(data)) {
console.error('Data is not an array');
return [];
}
const processedData = data.map((row) => {
const [matricula, aluno, faltas, p1, p2, p3] = row;
const totalAulas = 60;
const maxFaltas = totalAulas * 0.25;
if (faltas > maxFaltas) {
console.log(`${aluno} foi reprovado por falta.`);
return [...row, "Reprovado por Falta", 0];
}
const average = math.mean([parseFloat(p1), parseFloat(p2), parseFloat(p3)]);
let situation = "";
let naf = 0;
if (average < 50) {
situation = "Reprovado por Nota";
console.log(`${aluno} foi reprovado por nota.`);
} else if (average >= 50 && average < 70) {
situation = "Exame Final";
naf = Math.ceil((100 - average) * 2); // Calculate NAF, rounded up
console.log(`${aluno} está de exame final e precisa de uma nota ${naf} para ser aprovado.`);
} else {
situation = "Aprovado";
console.log(`${aluno} foi aprovado.`);
}
return [...row, situation, naf];
});
return processedData;
}
async function updateGoogleSheet(googleSheets, spreadsheetId, range, values) {
await googleSheets.spreadsheets.values.update({
spreadsheetId,
range,
valueInputOption: "USER_ENTERED",
resource: {
values,
},
});
}
async function main() {
const googleSheets = await configureGoogleSheetsAPI();
const spreadsheetId = "1sx0cH1gsTob6eOlfzftLg_x15thAmKgBKv-kn_IfIME";
const range = "engenharia_de_software!A4:F27"
const data = await retrieveData(googleSheets, spreadsheetId, range);
const processedData = processStudentData(data);
const updateRange = `engenharia_de_software!A4:H27${processedData.length + 1}`; // Adjust this to match the range of the original data
await updateGoogleSheet(
googleSheets,
spreadsheetId,
updateRange,
processedData
);
}
main().catch(console.error);