Skip to content

Instantly share code, notes, and snippets.

@thanhph111
Last active September 18, 2024 15:42
Show Gist options
  • Save thanhph111/0212fc37af5d6d9b6665a26a75a017b8 to your computer and use it in GitHub Desktop.
Save thanhph111/0212fc37af5d6d9b6665a26a75a017b8 to your computer and use it in GitHub Desktop.
Automate Google Drive folder creation for class members using Google Apps Script.

Google Drive folder automation for class members

This guide will walk you through automating the creation of folders for each class member and assigning them access using Google Apps Script. The folders will be created inside a specific Google Drive folder and shared with the corresponding class member.

Prerequisites

  1. Google Sheet with the following format:

    • Column A: Names of class members.
    • Column B: Email addresses of class members.
  2. Google Drive folder: A folder in Google Drive where you want to create the individual class member folders.


Step-by-step instructions

Prepare the Google Sheet

Ensure your Google Sheet has two columns:

  • Column A: The class member's name.
  • Column B: The class member's email address.

Here’s an example:

Name Email
John john@example.com
Alice alice@example.com

You can add as many rows (class members) as you need.

Get the specific folder's ID in Google Drive

  • Navigate to the folder in Google Drive where you want the individual folders to be created.
  • Copy the folder's ID from the URL in the address bar. The ID is the string after /folders/ in the URL. For example https://drive.google.com/drive/folders/1SNYmgW1Fxj0qEE5ciiO162bxg6mp-DRw, the folder ID in this case is 1SNYmgW1Fxj0qEE5ciiO162bxg6mp-DRw.

Create a Google Apps Script

  1. Open the Google Sheet that contains your class members' names and email addresses.

  2. Go to Extensions > Apps Script.

  3. In the Apps Script editor, delete any code in the editor and replace it with the following code:

    function createFoldersForClassMembers() {
        const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
        // Get all data from the sheet
        const data = sheet.getDataRange().getValues();
    
        // Replace with the ID of the folder where you want to create member folders
        const parentFolderId = "1SNYmgW1Fxj0qEE5ciiO162bxg6mp-DRw";
    
        try {
            const parentFolder = DriveApp.getFolderById(parentFolderId);
    
            // Loop through each row, starting from index 1 to skip the header row
            data.slice(1).forEach(([name, email]) => {
                const individualFolder = parentFolder.createFolder(name);
                individualFolder.addEditor(email);
    
                // Log the folder URL for tracking purposes
                console.log(
                    `Folder created for ${name}: ${individualFolder.getUrl()}`,
                );
            });
        } catch (error) {
            // Catch and log any issues during folder creation or sharing
            console.error("Failed to create folders:", error);
        }
    }
  4. Replace the parentFolderId value with the folder ID you copied from Google Drive in Step 2.

  5. Save the script by clicking the disk icon or pressing Ctrl + S.

Run the script

  1. In the Apps Script editor, click on the dropdown where it says Select function, and choose createFoldersForClassMembers.

  2. Click the Run button (the play icon).

    The first time you run the script, it will ask for authorization to access your Google Sheets and Google Drive. Follow the prompts and approve the necessary permissions.

Verify the results

  • After the script has run, go to the specified Google Drive folder.
  • You should see individual folders created for each class member, named according to the names in the Google Sheet.
  • Each folder should also be shared with the corresponding email address, giving them edit access.

Notes

  • The script gives edit access to the class member by default. If you want to provide view-only access, you can modify the line:

    individualFolder.addEditor(email);

    To:

    individualFolder.addViewer(email);
  • If the Google Sheet has many rows (members), the script may take some time to complete. If Google imposes any quota limitations, you can split the sheet into smaller parts and run the script in batches.


Troubleshooting

  • Authorization error: If the script asks for permissions multiple times, make sure you’re signed in with the correct Google account that has access to the sheet and the target Drive folder.

  • Folder not created: If a folder is not created, ensure that the parent folder ID is correct and the email addresses are valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment