Automate guardian invitations

Why?

Inviting guardians manually student by student via the user interface is laboriously slow…and yet it needs to be done for every student in the school. This is exactly the kind of task we should be handing over to Google Apps Script.

How?

Open a new Google Sheet and rename the sheet to “Students”. Add three columns: Student email, Guardian email(s), Outcome.

Fill the first two columns with data exported from your MIS. Apps Script has a time limit on executions, so it is wise to do this in batches, say one year group at a time.

Copy the following into the Apps Script Editor and then run it from the custom menu in the container sheet:

function onOpen(e) {
  SpreadsheetApp.getUi()
    .createMenu("Classroom")
    .addItem("✉ Send guardian invitations", "sendInvites")
    .addToUi();
}

function sendInvites() {

  // Read the data from the spreadsheet into an array, removing the header row:
  const sheet = SpreadsheetApp.getActive().getSheetByName("Students");
  const data = sheet.getDataRange().getValues();
  data.shift();

  // Keep track of which row of the spreadsheet to write the outcome back to:
  let rowNum = 1;

  // Interate through the students (each row corresponds to one student):
  for (const row of data) {

    rowNum += 1;

    // Read and trim the student's email address:
    const studentEmail = row[0].trim();

    if (studentEmail.length > 0) {

      // Read and trim the guardian email address, splitting into an array if more than one:
      const guardianEmails = row[1].split(",").map(a => a.trim());

      // Interate through the guardian emails for the current student:
      for (const guardianEmail of guardianEmails) {

        if (guardianEmail.length > 0) {

          // Begin the message to record the outcome of the invitation:
          let statusMsg = guardianEmail;

          // Send the invite and add to the outcome message:
          try {
            const result = Classroom.UserProfiles.GuardianInvitations.create({ invitedEmailAddress: guardianEmail }, studentEmail);
            if (result === "ALREADY_EXISTS") {
              statusMsg += " is already invited or a guardian";
            } else if (result === "PERMISSION_DENIED") {
              statusMsg += " is refusing the invitation";
            } else {
              statusMsg += " successfully invited";
            }
          } catch (err) {
            if (String(err).includes("already exists")) {
              statusMsg += " is already invited or a guardian";
            } else {
              statusMsg += " FAILED (" + err + ")";
            }
          }

          // Write back the status message to the end of the current spreadsheet row:
          const range = sheet.getRange(rowNum, 3);
          let value = range.getValue();
          if (value.length > 0) { value += "\r\n"; }
          range.setValue(value + statusMsg);

        }

      }

    }

  }

}

Notes

You can only send invitations to students enrolled in a course you are a teacher of. If you have whole-year group courses that the Head of Year and tutors use for pastoral communications you could just be added to that (as a teacher).

Permissions

To run successfully, you might (try it first and if it runs, don’t worry!) need to add the following scope to the “appsscript.json” manifest file:

"oauthScopes": [
  "https://www.googleapis.com/auth/classroom.guardianlinks.students"
]

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x