How to automatically copy email addresses of google group members into a google spreadsheet

 

Recently I lost access to Hapara Teacher Dashboard which was a sad time because the replacement our school has gone with appears to be missing some of the features that are part of Hapara that streamlined some of my workflows. Hapara made it easy for me to get all of the email addresses for students of a class into a spreadsheet using the class info tab. Sadly there appears to be no equivalent in Securly Classroom. 

No matter, I thought, I can just export the details from the class group in google groups. Whilst yes, this is possible, it is not as convenient as Hapara's copy feature as it exports a load of other data about the group that I don't need as well as downloading all of this data to a CSV file rather than allowing me to copy it to the clipboard and paste it directly into my spreadsheet of choice. 

Therefore, I decided to write a short piece of apps script code to try to replicate the easy of use of the Hapara feature. This spreadsheet allows me to type in any class code and providing there is a google group created for it (which there is for every class automatically in the case of our school) it will retrieve the email addresses for every member of that class.

The code to accomplish this is as follows:

function populateEmailColumn(emails) {
  var range = "B1:B" + emails.length;
  SpreadsheetApp.getActive().getRange(range).setValues(emails);
}

function getClassEmails(classCode) {
  var classEmail = classCode + "@wheatleypark.org";
  var group = GroupsApp.getGroupByEmail(classEmail);
  var users = group.getUsers();
  var emails = []
  for (var i=0; i < users.length; i++) {
    emails.push([users[i].getEmail()]);
  }
  console.log(emails);
  return emails;
}

function getEmailsOfAClass() {
  SpreadsheetApp.getActive().getRange("B1:B").clearContent();
  var classCode = SpreadsheetApp.getActive().getRange("D1").getValue();
  var emails = getClassEmails(classCode);
  populateEmailColumn(emails);
}


When the user clicks the "Get Class Emails" button this invokes the getEmailsOfAClass function. This function clears the current contents of column B, gets code of the class that the user has entered from cell D1 and invokes getClassEmailsThis looks up the group and retrieves a list of User objects representing the users in the group. This list is then iterated over to retrieve the email addresses of the members of the group. Finally, populateEmailColumn is invoked which puts those email addresses in column B.

Further work

Although this will be useful to me in it's current form, I would like to return to it at some stage and add the ability to retrieve other student details. This may take the form of using the People API, which I haven't got my head round yet, or likewise Bromcom's API.