Apps Script – Creating & sharing Class folders in Drive

Teachers using Drive often need to set up folders for their class and for their individual students. Doing it in Drive is not the most exciting job to do. So, to make your life easier, here’s a script, which will set up your folders for you. I’ve made it so there is a little bit of flexibility depending on what folders you need.

It will create a class folder in My Drive, a folder for each student, and if you want a separate classwork folder and a separate homework folder. It will share the class, classwork, and homework folders with all the students, but only share the individual folders with the specific students.


Class Folder Creator sheet

Here’s the sheet that we will use to fill out the information required and to run from the script from.

Class Folders - 3
Class Folders - 4

At the top, you add the class name, and then Y or N (yes or no), as to whether you want a classwork folder or homework folder or not.

Class Folders - 5

Underneath you add the students’ names and email addresses. These could easily be pasted in if you already have a list.


The code

Here are the main steps of the code:

  • The code will get the details entered on the sheet and store them in the variables.
  • Then create the Class folder in My Drive.
  • Add the students as editors to that folder.
  • Check to see if you want a classwork folder or a homework one, and if so, create them.
  • Finally, it will create the individual student folders and share them with the individual students.
1. function createClassFolders() {
2.  //Get class folder, class name, and classwork/homework (Y/N)
3.  const ss = SpreadsheetApp.getActiveSpreadsheet();
4.  const shClassFolders = ss.getSheetByName("ClassFolders");
5.  const data = shClassFolders.getRange(2,2,3).getValues();
6.  const className = data[0][0];
7.  const classwork = data[1][0];
8.  const homework = data[2][0];

Lines 1 and 3: First we set up the function and get the spreadsheet.

Line 4: We then get the sheet called “ClassFolders”.

Line 5: We get the class name, whether we want a classwork folder, and if we want a homework folder. We can get this in one call, by getting the values from row 2, column 2, and 3 rows (2,2,3). Then storing these 3 values in the variable data.

Lines 6 to 8: We then extract the 3 values and store them in separate variables, just so we can easily refer to them later on. We do that by getting the value at specific positions in the data array. So, class name will be in the first ‘row’ and in the first position, the classwork Y or N in the second ‘row’ and so on.

10.  //Get list of students' names and emails
11.  const lastRow = shClassFolders.getLastRow();
12.  const studentsNames = shClassFolders.getRange(6, 1, lastRow - 5).getValues();
13.  const studentsEmails = shClassFolders.getRange(6, 2, lastRow - 5).getValues();

Line 11: We get the row with the last student name and student email on it, which will the last row on the sheet. Make sure you don’t write anything else below this row, otherwise it will throw an error.

Lines 12 and 13: Next, we need to get the list of student names and their emails. Here we start at row 6 and column 1 (A) and go down until the last row minus 5 (we don’t count the first 5 rows as we started on row 6), then get the values in that range. We store these values in the variable studentsNames. Then do the same for the emails, except this time we get the values in column 2.

15.  //Create Class folder in My Drive  
16.  const newFolder = DriveApp.createFolder(className);
17.  const classFolderID = newFolder.getId();
18.  const classFolder = DriveApp.getFolderById(classFolderID);

Now we need to create the class folder.

Line 16: We’re going to create it in My Drive, so we use the DriveApp class followed by the createFolder() method.  Then we add the class name variable, className. Then we store this in the variable newFolder.

Line 17: We then get the ID of that new folder.

Line 18: Then we need to get that folder by its ID to be able to use it and we store it in the variable classFolder.

20.  //Add editors from list of students' emails  
21.  for (s = 0; s < studentsEmails.length; s++) {
22.    classFolder.addEditor(studentsEmails[s][0]);
23.  }

Now, we add the students as editors of that folder. We do this by looping through the students’ emails and adding them one by one to the folder.

Line 21: We set up a simple for loop and continue it while it’s less than the number of email addresses (using the length of the array studentEmails).

Lines 22-23: Each time we go around the loop, we want to add a student as an editor to the folder. We get the folder (classFolder) and use the addEditor() to add them. We use the variable s in square brackets to show where in the array we are. We need to add [0] to get the value in the array. So, it will start in position 0 and continue to the end of the list. Then close the for loop.

25.  //Create classwork folder in Class folder (if Y)
26.  if (classwork === "Y") {
27.    classFolder.createFolder("Classwork");
28.  }

Now we want to see if the user wants to create a classwork folder and if so, create one in the class folder we recently created.

Line 26: We use an if statement to see if the classwork cell contains a “Y”.

Lines 27: This is similar to line 21, except here we’re going to create the folder not in My Drive but in the class folder. So, we get the classFolder variable and create the folder using that, and call it “Classwork”.

 30. //Create homework folder in Class folder (if Y)  
 31. if (homework === "Y") {
 32.   classFolder.createFolder("Homework");
 33. }

Lines 31-33: We do exactly the same for the homework folder.

35.  //Start loop: Create student folder in Class folder
36.  for (i = 0; i < studentsNames.length; i++) {
37.    let newFolders = classFolder.createFolder(studentsNames[i]);
38.    let individualFolderID = newFolders.getId();
39.    let individualFolder = DriveApp.getFolderById(individualFolderID);

Now we want to create the individual student folders. The folder creation is similar to before, except this time we will be looping down the list of student names previously stored in the variable studentsNames.

Line 36: We set up a for loop to go down the list of student names, using the variable i to track where we are in the list.

Line 37: We then create a folder in the class folder and name it with the current student name we are at in the loop. We do that by using studentsNames[i].

Lines 38-39: Then we get the ID of the student just created and get the folder by that ID.

41.   //remove all editors & add specific student as editor    
42.   for (j = 0; j < studentsEmails.length; j++) {
43.     individualFolder.removeEditor(studentsEmails[j][0]);
44.   }
45.   individualFolder.addEditor(studentsEmails[i][0]);
46. }

Now, we need to change the access rights for the student folders. As the student folders will be created in the class folder, they automatically assume the same access rights as the class folder. So, for each individual folder, we need to remove all the students except for the one who needs access to their personal folder. To do that we remove all the students as editors, then add the specific student.

Line 42: We set up a second loop, this time using the variable j to keep count. This will loop down the list of student emails.

Lines 43-44: Every time the loop goes around, it will remove a student email from being an editor of this folder. To do this we use the removeEditor() method and pass the studentsEmails variable to it. Then we close the j loop.

Lines 45-46: Then we need to add the student we want, who is the same as the student we created the folder for. So, we use addEditor() to add them and we use the i variable to identify which student it is. Then we close the i loop.

48.  //Toast - finished
49.  ss.toast("All folders set up in your My Drive. :)", "Finished", 5);
50. }

Lines 49-50: Finally, I think it’s good to tell the user that the process has finished. So, here we display a toast message, using the toast method. Then we close the function.

To run the code from a menu on the sheet, I’ve also added this onOpen function:

1. function onOpen() {
2.  const ui = SpreadsheetApp.getUi();
3.  ui.createMenu('CREATOR')
4.    .addItem('Make & share class folders', 'createClassFolders')
5.    .addSeparator()
6.    .addItem('Make multiple files', 'makeFiles')
7.    .addSeparator()
8.    .addItem('Make multiple folders', 'makeFolders')
9.    .addToUi();
10.}

Line 1: Open the function and use the keyword onOpen so that it runs automatically when the sheet is opened.

Line 2: Get the UI of the spreadsheet.

Line 3: Create a menu and call it “CREATOR”.

Line 4: Add a menu item. The first part is the name that is shown in the menu, and the second part is the function name you want to run.

Lines 5-8: In the file I’ve also included a make folders and make files scripts, so I’ve added those to the menu too. To add a separator line, use the addSeparator() method.

Lines 9-10: Add this to the UI then close the function.


Running the code

Now let’s run an example with the information entered as below:

Class Folders - 4
Class Folders - 5

Click on the “CREATOR” menu and then “Make & share class folders”.

The first time you’ll be asked to authorise the script. Follow the instruction below:

Click Continue.

Select your account.

Ignore the scary message and click on “Advanced”.

Click “Go to fileAndFolderCreator (unsafe)” – Don’t worry it’s safe!

Click “Allow”. You’ll have to run the program again from the menu.

This will take about 10 seconds and once you see the toast message, you will see the new folder called “Maths 101” in your My Drive.

Double-clicking on that, we can see the sub folders we have created. There are the classwork, homework, and student ones.

If we look at the access rights the class folder has (and also the Classwork and Homework have), we’ll see it’s been shared with everyone on my little list.

Looking at the access rights of the “Baz Roberts” folder we’ll see it’s only been shared with me (Barrie Roberts-the owner) and the student “Baz Roberts”.

The students get an automatic invite to the class folder and an invite to their individual folder.


Hopefully, this will save you some time and effort. You can make a copy of the file and code here. In the same file, I’ve included this script, and the ones from previous posts: file maker and folder maker.

Want to learn more about Google Workspace and Apps Script? The books below are available on Amazon. Just click on a book! (Affiliate links).

JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2
JavaScript Fundamentals for Apps Script users

a

Google Apps Script Projects 1
Google Apps Script Projects 2

8 comments

  1. Hey there, Thanks so much for sharing this script. I was able to adapt it for a project management tool that I’m putting together for my teacher center. It was really helpful.

  2. I am getting the error: “Exception: The parameters (number[]) don’t match the method signature for DriveApp.Folder.addEditor.” when running the ClassFolders script. I have confirmed that if I comment out the addEditor and remove.Editor lines of code (lines 27 49 & 51) the code runs correctly. Can you advise me on how to correct this line of code?

    1. Hi-This is because you’re running the latest version of Apps Script and certain things in older code doesn’t work with it. In this particular case, email addresses that have been grabbed as an array need to be converted to strings. It’s easy to rectify, you’ll need to use the .toString() method.
      In the lines with addEditor or deleteEditor add .toString() after the “studentsEmails[i]” part, for exmaple:

      classFolder.addEditor(studentsEmails[i].toString());

      That’ll resolve the issue.

      1. Oh thank you for this! I was testing, and testing, and testing… lol.

        Do you have additional code that would allow a custom message to be sent along with the automatic invite the added student receives via email?

Comments are closed.