Search code examples
google-apps-scriptgoogle-classroomgoogle-hangouts

Stuck at G Suite Developer Quickstart for "check student attendance in Google Meet courses"


I would be very appreciative of any input. Thank you; I'm using the G Suite Developer Quickstart for a Meet attendance script https://developers.google.com/gsuite/add-ons/editors/sheets/quickstart/attendance Unfortunately, upon using the provided code, and having made adjustments, I either always get "Absent" or "Present" for all students (regardless of whether they were present or not) depending on if I use "null" of "false" in an if statement for activities for applicationName 'meet'. Here is the code I'm using as a superuser:

...

    function onOpen() {
      var ui = SpreadsheetApp.getUi();
  ui.createMenu('Opciones SG')
      .addItem("Importar Curso", 'importCourses')
      .addItem('Verificar Asistencia', 'checkAll')
      .addToUi();
} 

    function importCourses() {
  var optionalArgs = {
    teacherId: 'me',
    pageSize: 5
  };
  var response = Classroom.Courses.list();
  var courses = response.courses;
  for (var i = 0; i < 1; i++) {
    var courseName = courses[i].name;
    var courseId = courses[i].id;
    insertCourse(courseName, courseId)
  }
}

function insertCourse(courseName, courseId) {
    var spreadsheetName = courseName + "(" + courseId + ")"
    var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var yourNewSheet = activeSpreadsheet.getSheetByName(spreadsheetName);

    if (yourNewSheet != null) {
        return
    }
    yourNewSheet = activeSpreadsheet.insertSheet();
    yourNewSheet.setName(spreadsheetName);
    yourNewSheet.appendRow(['Nombre', 'Email', 'Asistencia'])
    yourNewSheet.setFrozenRows(1)
    var studentNames = getRoster(courseId)["studentNames"]
    var studentEmails = getRoster(courseId)["studentEmails"]
    for (var i = 0; i < studentNames.length; i++) {
      yourNewSheet.appendRow([studentNames[i],studentEmails[i]])
    }
    yourNewSheet.autoResizeColumns(1, 2)
    yourNewSheet.setFrozenColumns(2)
  }

function getRoster(courseId) {
  var studentNames = []
  var studentEmails = []
  var optionalArgs = {
      pageSize: 100
  };
  var response = Classroom.Courses.Students.list(courseId, optionalArgs)
  var students = response.students

  for (var i = 0; i < 100; i++) {
    try {
      studentNames.push(students[i].profile.name.fullName)
      studentEmails.push(students[i].profile.emailAddress)
    } catch (err) {
       return { "studentNames":studentNames, "studentEmails":studentEmails }
   }
 }
}

function checkAll() {
  var ss = SpreadsheetApp.getActiveSheet();
  var sheet = ss.getDataRange().getValues();
  for (var i = 2; i < sheet.length * 100; i++){
    var meetCode = getCleanCode(sheet[0][i])
    // No Meet code given
    if (meetCode == null) {
      break;
    }
    else {
      // check whether each student was present in Meet
      checkMeet(meetCode, i+1);
    }
  }
}

function checkMeet(meetCode, index) {
  // universal settings - static
  var userKey = 'all';
  var applicationName = 'meet';
  var ss = SpreadsheetApp.getActiveSheet();
  var sheet = ss.getDataRange().getValues();
  for (var i = 0; i < sheet.length-1; i++) {
    var emailAddress = sheet[i+1][1]
    var optionalArgs = {
      event_name: "call_ended",
      filters: "identifier==" + emailAddress + ",meeting_code==" + meetCode
    };
    try {
      var response = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
      var activities = response.items;
      if (activities == false) {
        markAbsent(ss,i+2,index)
      }
      else {
        markPresent(ss,i+2,index)
      }
    } catch (err) {
        continue
     }
  }
}

function getCleanCode(meetCode) {
  try{
    return meetCode.replace("/-/g","")
  } catch (err) { return meetCode; }
}

function markAbsent(sheet, i, j) {
    var cell = sheet.getRange(i, j);
    cell.setValue("Absent");
}

function markPresent(sheet, i, j) {
    var cell = sheet.getRange(i, j);
    cell.setValue("Present");
}

...


Solution

  • For the correct functionality of the code, check for if (activities == null) or if (activities == undefined) - NOT if (activities == false)

    • If there are no response items (that is no items have been found for a given user and meeting code) - activities will be undefined, but not false.
    • if (activities == false) will never be fulfilled and if you use it - all participants will be marked as present
    • On the other hand, if activities == undefined for other reasons - for example because you introduced the wrong meet code (don't forget to replace 'Asistencia' through a valid meet code), all participants will be marked as absent
    • Note that the Reports API has some delay, so do not expect to retrieve participation data in live time.
    • If all participants are marked as absent - probably the data did not propagate yet, wait some time and try again.
    • Make sure you pass the correct meet code to the script. Your meet code should look something like xxx-xxxx-xxx.
    • The script needs to remove the - for correct functionality. For this please change in function getCleanCode(meetCode) the line return meetCode.replace("/-/g","") to return meetCode.replace(/-/g, "");
    • As for the question in your comment: here you can see all available Hangouts Meet Audit Activity Events that you can use for filtering instead of the meeting_code. Unfortunately the timestamp is not one of possible query parameters.