Quickstart: Check student attendance in Google Meet courses

Complete the steps on this page to create a Google Sheets add-on that tracks attendance for Google Classroom courses delivered using Google Meet conferencing. The following image shows a course with recorded attendance:

Set it up

Follow these steps to set up this quickstart:

  1. Create a new Google Sheet.
  2. From within your new sheet, select the menu item Tools > Script editor. If you are presented with a welcome screen, click Blank Project.
  3. Delete the code in the script editor.
  4. Click on the "Untitled project" project name, and rename to "Attendance."
  5. Click the down arrow to the right of the Code.js file name.
  6. Select Rename.
  7. Rename the file to "Attendance."
  8. Copy and paste the following code into the script editor:
/* Menu Options */
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Options')
      .addItem("Import Last 5 Courses", 'importCourses')
      .addItem('Check Attendance on Current Sheet', 'checkAll')
      .addToUi();
}

/*
  Description: Option for teachers to import their
  most 5 recently created courses
*/
function importCourses() {
  var optionalArgs = {
    teacherId: 'me',
    pageSize: 5
  };
  var response = Classroom.Courses.list(optionalArgs);
  var courses = response.courses
  for (var i = 0; i < courses.length; i++) {
    var courseName = courses[i].name
    var courseId = courses[i].id
    insertCourse(courseName, courseId)
  }
}

/*
  Description: Create the Sheet for Course
  @param {String} courseName - Name of Course
  @param {String} courseId - Corresponding Classroom ID
*/
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(['Student Name', 'Email Address', 'Replace with Meet Code'])
    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)
  }

/*
  Description: Adds the course's students to the course sheet
  @param {String} courseId - Corresponding Classroom ID
*/
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 <= students; i++) {
    try {
      studentNames.push(students[i].profile.name.fullName)
      studentEmails.push(students[i].profile.emailAddress)
    } catch (err) {
       return { "studentNames":studentNames, "studentEmails":studentEmails }
   }
 }
}

/*
  Description: Retrieves the Meet code from the Course Sheet
  and uses helper function to check attendance
*/
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);
    }
  }
}

/*
  Description: Checks the Meet for attendance of the given student
  @param {String} meetCode - Raw Meet Code from Course Sheet
  @param {Integer} index - Index corresponding to the Student's row
  in the Course Sheet
*/
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 == null) {
        markAbsent(ss,i+2,index)
      }
      else {
        markPresent(ss,i+2,index)
      }
    } catch (err) {
        continue
     }
  }
}

/*
  Description: Strips any "-' Characters to match needed format
  for Reports API
  @param {String} meetCode - Raw Meet Code from Course Sheet
*/
function getCleanCode(meetCode) {
  try{
    return code.replace("/-/g","")
  } catch (err) { return code; }
}

/*
  Description: Marks the student as absent for their corresponding cell
  @param {Object} sheet - Course Sheet object
  @param {Integer} i - Index of Sheet cell column to be filled
  @param {Integer} j - Index of Sheet cell row to be filled
*/
function markAbsent(sheet, i, j) {
    var cell = sheet.getRange(i, j);
    cell.setValue("Absent");
}

/*
  Description: Marks the student as absent for their corresponding cell
  @param {Object} sheet - Course Sheet object
  @param {Integer} i - Index of Sheet cell column to be filled
  @param {Integer} j - Index of Sheet cell row to be filled
*/
function markPresent(sheet, i, j) {
    var cell = sheet.getRange(i, j);
    cell.setValue("Present");
}
  1. Select File > Save all.

Try it out

Follow these steps to try this quickstart:

  1. Switch back to your spreadsheet and reload the page. An Options menu appears on the menu bar.
  2. Click Options > Import Last 5 Courses. A dialog box appears indicating that the script requires authorization.
  3. Click Continue. A second dialog box requests authorization for specific Google services. Click Allow. The spreadsheet now contains a sheet, represented by a tab at the bottom, for each of your last 5 courses. Each course has a list of course attendees in the first column and Google Meet IDs across the top row.
  4. Click on one of the course sheets.
  5. Click Options > Check Attendance on Current Sheet. Attendance is entered for the current course's Google Meet IDs.
  6. Repeat steps 4 and 5 for the four other classes.

Publish

Since this is an example add-on, our tutorial ends here. If you were developing a real add-on, the last step would be to publish it for other people to find and install.

Create an attendance app with another language

You can also create an attendance app, or add attendance functionality to an existing distance learning app, using our REST APIs or corresponding libraries.

The REST APIs used in this quickstart are:

The method names in this quickstart are the same as in these REST APIs. For example, AdminReports.Activities.list corresponds to Activities.list in the Google Reports API.

Learn more

To continue learning about how to extend Google Sheets with Apps Script, take a look at the following resources: