Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function exportGrades(cell) {
- var r = cell.range;
- Logger.log(r);
- var row = r.getRowIndex();
- var column = r.getColumnIndex();
- var grade = cell.value;
- if (grade == null) {
- grade = 0;
- }
- grade = parseFloat(grade);
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- var sheet_index = sheet.getIndex();
- var col_name = sheet.getRange(1, column).getValue();
- if (!(isNaN(grade)) && (column < sheet.getMaxRows()) && (row <= sheet.getMaxColumns())) {
- // grade = String(grade);
- var nickname = sheet.getRange(row, 1).getValue();
- var col_name = sheet.getRange(1, column).getValue();
- Logger.log(grade);
- Logger.log(nickname);
- Logger.log(col_name);
- var admin_token = "eyJ1c2VyX2F1dGhlbnRpY2F0aW9uX2lkIjo1fQ:1kPQzY:ZFQYJhOt7Ot7y7Y_Qr_VUbwBub8"
- var url = "https://track.miem.hse.ru/api/v1/"
- var headers = {
- "x-disable-pagination": "True",
- "Content-Type": "application/json",
- "Authorization": "Bearer " + admin_token
- };
- var project_names = new Map();
- var options_get = {
- "method" : "get",
- "headers": headers,
- "muteHttpExceptions": true
- };
- var resp_projects = UrlFetchApp.fetch(url + "projects", options_get);
- var resp_out = JSON.parse(resp_projects.getContentText());
- for (let project of resp_out){
- project_names.set(project["id"], project["name"]);
- }
- var user_p_id = 0;
- var teacher_p_id = 0;
- for (let [key, value] of project_names) {
- if (value == (nickname + " Учеба в МИЭМ")) {
- user_p_id = key;
- }
- else if (value == "Компьютерная графика") {
- teacher_p_id = key;
- }
- }
- Logger.log(user_p_id)
- var resp_tasks = UrlFetchApp.fetch(url + "tasks?project=" + String(user_p_id), options_get);
- var tasks = JSON.parse(resp_tasks.getContentText());
- var resp_task_ca = UrlFetchApp.fetch(url + "task-custom-attributes?project=" + String(user_p_id), options_get);
- var task_ca = JSON.parse(resp_task_ca.getContentText());
- for (var task of tasks){
- if (task["subject"] == col_name){
- var task_edited = task;
- var resp_get_task_val = UrlFetchApp.fetch(url + "tasks/custom-attributes-values/" +
- String(task_edited['id']), options_get);
- var task_ca_obj = Object.entries(JSON.parse(resp_get_task_val.getContentText())["attributes_values"]);
- var task_ca_id = task_ca_obj[0][0];
- var curr_task_value = JSON.parse(resp_get_task_val.getContentText())["attributes_values"][task_ca_id];
- var add_grade = String((parseFloat(grade) - parseFloat(curr_task_value)).toFixed(2));
- var data_task = {
- "attributes_values": {[String(task_ca[0]["id"])] : String(grade)},
- "version" : 1
- };
- var options_patch = {
- "method" : "patch",
- "payload" : JSON.stringify(data_task),
- "headers": headers
- };
- var response = UrlFetchApp.fetch(url + "tasks/custom-attributes-values/" + String(task["id"]), options_patch);
- var resp_statuses = UrlFetchApp.fetch(url + "task-statuses?project=" + String(user_p_id), options_get);
- var statuses = JSON.parse(resp_statuses.getContentText());
- for (i = 0; i < statuses.length; i++){
- if (statuses[i]["name"] == "Closed"){
- var closed_stat = statuses[i]["id"];
- }
- }
- var data_close_task = {
- "status": closed_stat,
- "version" : parseInt(task_edited["version"])
- };
- var options_patch_close = {
- "method" : "patch",
- "payload" : JSON.stringify(data_close_task),
- "headers": headers
- };
- var resp_close_task = UrlFetchApp.fetch(url + "tasks/" + String(task_edited["id"]), options_patch_close);
- }
- }
- var resp_epic = UrlFetchApp.fetch(url + "epics?project=" + String(user_p_id), options_get);
- var epic_id = JSON.parse(resp_epic.getContentText())[0]["id"];
- var resp_us = UrlFetchApp.fetch(url + "userstories?epic=" + String(epic_id), options_get);
- var userstories = JSON.parse(resp_us.getContentText());
- var totals = [61, 42, 22, 16, 20, 52, 34, 20, 16, 29, 15];
- for (i = 0; i < userstories.length; i++){
- if(userstories[i]["id"] == task_edited["user_story_extra_info"]["id"]){
- var resp_us_ca_val = UrlFetchApp.fetch(url + "userstories/custom-attributes-values/" + String(userstories[i]["id"]), options_get);
- var us_ca_obj = Object.entries(JSON.parse(resp_us_ca_val.getContentText())["attributes_values"]);
- var us_ca_id = us_ca_obj[0][0];
- var curr_us_value = JSON.parse(resp_us_ca_val.getContentText())["attributes_values"][us_ca_id];
- add_grade = add_grade / totals[sheet_index - 2];
- var total_us_grade = String((parseFloat(curr_us_value) + add_grade).toFixed(2));
- var data_ca_us_edit = {
- "attributes_values": {[us_ca_id] : total_us_grade},
- "version" : 1
- };
- var data_us_patch = {
- "method" : "patch",
- "payload" : JSON.stringify(data_ca_us_edit),
- "headers": headers
- };
- var resp_ca_us_edit = UrlFetchApp.fetch(url + "userstories/custom-attributes-values/" +
- String(userstories[i]["id"]), data_us_patch);
- }
- }
- var resp_get_epic_val = UrlFetchApp.fetch(url + "epics/custom-attributes-values/" + String(epic_id), options_get);
- var epic_ca_obj = Object.entries(JSON.parse(resp_get_epic_val.getContentText())["attributes_values"]);
- var epic_ca_id = epic_ca_obj[0][0];
- var curr_epic_value = JSON.parse(resp_get_epic_val.getContentText())["attributes_values"][epic_ca_id];
- var total_epic_grade = String(parseFloat(parseFloat(curr_epic_value) + add_grade / 11).toFixed(2));
- var data_ca_epic_edit = {
- "attributes_values": {[epic_ca_id] : total_epic_grade},
- "version" : 1
- };
- var data_epic_patch = {
- "method" : "patch",
- "payload" : JSON.stringify(data_ca_epic_edit),
- "headers": headers
- };
- var resp_ca_epic_edit = UrlFetchApp.fetch(url + "epics/custom-attributes-values/" + String(epic_id), data_epic_patch);
- //Передача оценки в грейдбук для преподавателя
- var resp_teach_epics = UrlFetchApp.fetch(url + "epics?project=" + teacher_p_id, options_get);
- var stud_epic = JSON.parse(resp_teach_epics.getContentText())[1];
- var resp_name = UrlFetchApp.fetch(url + "users", options_get);
- var resp_names = JSON.parse(resp_name.getContentText());
- for (i = 0; i < resp_names.length; i++) {
- if(resp_names[i]["username"] == nickname) {
- var stud_name = resp_names[i]["full_name"];
- }
- }
- var resp_teach_us = UrlFetchApp.fetch(url + "userstories?epic=" + String(stud_epic["id"]), options_get);
- var teach_us = JSON.parse(resp_teach_us.getContentText());
- for (i = 0; i < teach_us.length; i++) {
- if(teach_us[i]["subject"] == stud_name) {
- var stud_us_id = teach_us[i]["id"];
- }
- }
- var resp_get_us_stud_val = UrlFetchApp.fetch(url + "userstories/custom-attributes-values/" +
- String(stud_us_id), options_get);
- var us_ca_stud_map = Object.entries(JSON.parse(resp_get_us_stud_val.getContentText())["attributes_values"]);
- var us_ca_stud_id = us_ca_stud_map[0][0];
- var data_ca_us_stud_edit = {
- "attributes_values": {[us_ca_stud_id] : total_epic_grade},
- "version" : 1
- };
- var data_ca_us_stud_edit_patch = {
- "method" : "patch",
- "payload" : JSON.stringify(data_ca_us_stud_edit),
- "headers": headers
- };
- var resp_ca_us_stud_edit = UrlFetchApp.fetch(url + 'userstories/custom-attributes-values/' + String(stud_us_id),
- data_ca_us_stud_edit_patch);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment