简体   繁体   中英

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");
}

...

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.

Here is the code we have been using for a few months now, no glitches:

    function onOpen() {
      var ui = SpreadsheetApp.getUi();
  ui.createMenu('Asistencia')
      .addItem('Asistencia', 'checkAll')
      .addItem('*Crear*', 'createNewSheets')
      .addToUi();
} 

function createNewSheets() {
  var ss        = SpreadsheetApp.getActiveSpreadsheet();

   // Get the range of cells that store employee data.
  var employeeDataRange = ss.getRangeByName("A:A");

  var employeeObjects = employeeDataRange.getValues();

  var template = ss.getSheetByName('Template');

  for (var i=0; i < employeeObjects.length; i++) {

     // Put the sheet you want to create in a variable
     var sheet = ss.getSheetByName(employeeObjects[i]);

      // Check if the sheet you want to create already exists. If so,
      // log this and loop back. If not, create the new sheet.
        if (sheet) {
           Logger.log("Sheet " + employeeObjects[i] + "already exists");
        } else {
           template.copyTo(ss).setName(employeeObjects[i]);
           }
        }
  return;
}



    function importCourses() {
  var optionalArgs = {
    teacherId: 'me',
    pageSize: 5
  };
  var response = Classroom.Courses.list(optionalArgs);
  var courses = response.courses;
  for (var i = 0; i < 2; 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();

//let today = new Date()
//var staringTime = new Date().toISOString();  
  
//  var DateValue = ss.getRange(1, 1).getValues();
  


  for (var i = 0; i < sheet.length-1; i++) {
    var emailAddress = sheet[i+1][1]
    var optionalArgs = {
      startTime: formatStartDate(),
      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 == undefined) {
        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("A");
}

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

function formatStartDate(){
  let date = new Date();

  const offset = date.getTimezoneOffset();

  
  let offsetDate = new Date(date.getTime() - (offset*145*1000));
  return offsetDate.toISOString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM