Create student folders with Apps Script

The problem

Recently a colleague spoke to me about a requirement she had to collect photos from a large group of students. They could potentially be from different grade levels and could wish to share any number of photos.

We brainstormed options, including creating a Google form with file attachment containers to it and sharing it with the student body. But since the number of files they may choose to share could be a variable, a form felt limiting. Hence, I suggested a Google script that could read from a sheet of email addresses and names, create folders with the student name (while assigning them editing rights) and keep it central to one parent folder which she can browse through without having to scan different locations in her drive.

The solution

STEP 1 : The Parent Folder
I set about creating a small script which would do this. The one thing I have always enjoyed the most is creating my own code solutions to problems before trying to look for solutions/add-ons on the web. The first thing I did was create a Parent Folder which would contain the sheet (that controlled the creation of student folders) and the script to run things.

STEP 2 : The Google Sheet
The sheet itself is pretty basic. Student name (which will also become the folder name), email address (to assign rights) and folder URL for teacher to get to it directly from the sheet. The script will check for location to be blank before creating the folder so that duplication can be avoided.

STEP 3 : The Script

The script below creates a custom menu option for the teacher to run whenever he/she wishes. This is to include new students who may wish to join. It then calls a function that loops through the data range on the sheet and does the following:

  • Check if location is blank.
  • Create folder in parent folder with student name.
  • Assign edit rights to student via email address.
  • Get student folder URL and update the Location column.
  • Send an email to student with link to folder.
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('Create Student Folders', 'createFolders')
      .addToUi();
}


function createFolders()
{
  //data loaded into [][] array from sheet
  var values  = SpreadsheetApp.getActive().getDataRange().getValues();  
  //load the folder ID where all student folders need to be created
  var parentFolder = DriveApp.getFolderById("1GDyPse1YffdX4AsY3LGUii2x3j0c6BFg");
  //message that is sent to the student
  var message ="";
  
  //loop through the values in the sheet
  for(var row =1; row<values.length;row++) //row starts at 1 since first row is header
  {
  
    //if 3rd column is blank, only then create the student folder.
    if(values[row][2]=="")
    {
      //create folder with student name
      var newFolder=parentFolder.createFolder(values[row][0]); 
      
      //set the folder location in the 3rd column for the student folder.
      SpreadsheetApp.getActive().getRange("C"+(row+1)).setValue(newFolder.getUrl());
      
      //add the student email as the editor to the created folder.
      newFolder.addEditor(values[row][1]); 
      
      //design the custom message to be sent
      message="Hi there - Please find the link to your photo folder below:<br><br>"
      message+="<b>Link: </b>" + newFolder.getUrl();
      
      //send the email. Done.
      MailApp.sendEmail(values[row][1],"Photo Folder Created","",{htmlBody:message});
    }
   }

Browser.msgBox("Process complete!"); 
}

STEP 3 : The Result

After running the script the following takes place.

Script in action

Summary

The solution works as expected. The good thing about such home grown solutions is that it helps people get more comfortable with the idea of coding. I am a big advocate of students and teachers alike make coding a regular part of their lives. Showcasing such examples for members in the community can go a long way in empowering them to expand their knowledge and continue to find ways to apply them in their daily activities.