How to Create a Google Classroom using Google Apps Script

October 04, 2021
How to Create a Google Classroom using Google Apps Script
Table of Contents
Table of Contents

Due to the pandemic, all schools had to shut down and shift to online mode. During the online mode of schooling, Google Classroom picked up and everyone started taking classes, giving out assignments, uploading notes on Google Classroom.

Creating a Google Classroom and maintaining it can be a bit hectic for so many students in a school. Say you are the moderator of your school and your task is to create 10 Google Classrooms for different teachers. Manually doing this can be a very hectic process, that’s where Google Apps Script comes in.

Using Google Apps Script, you can automatically create multiple Google Classrooms from a Google Sheet, and store the classroom codes in the Google Sheet.

Step 1: Sample Google Sheet

In this blog, I already have a sheet that contains the name of the class, followed by the name of the teacher, section of the class, room and the description of the class. If you want to work with the Google Sheet to create a classroom in Google, click here.

Step 2: Write the Automation Script

While you are in the Google Sheet, let’s open up the Script Editor to write some Google Apps Script. To open the script editor follow these steps:

  1. Click on Tools and open the Script Editor.

2. This brings up the Script Editor as shown below.

We have reached the Script Editor.

Step 3: Add the Google Classroom API

Before writing the code, we have to add the Classroom API to our Apps Script Project, to do that follow these steps:

  1. Click on the plus next to Services.

2. Scroll down and click on the Google Classroom API.

4. After selecting the API, click on Add.

OWNER_EMAIL = "aryanirani123@gmail.com"; //Don’t forget to update this value with your email address :)
function onOpen(){
    const menu = SpreadsheetApp.getUi().createMenu('Custom');
    menu.addItem('Create Classroom','createClasses');
    menu.addToUi();
}
function classroomData(properties){
    
    const crs = Classroom.newCourse();
    Object.keys(properties).forEach(key => {        
        Crs[key] = properties[key];    
    })	
    
    const createdCourse = Classroom.Courses.create(crs);
    return createdCourse.enrollmentCode;
}
function createClasses(){
    
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getActiveSheet();
    const START_ROW= 2;
    const START_COLUMN = 1;
    const LAST_COLUMN= 4;
    const data = sheet.getRange(
        START_ROW,
        START_COLUMN,
        sheet.getLastRow()-1,LAST_COLUMN).getValues();
    const enrollmentCodes = [];
    const nameIndex = 0;
    const sectionIndex = 1;
    const roomIndex = 2
    const descriptionIndex = 3;
    const START_COLUMN = 5;
    const LAST_COLUMN = 1;
    data.forEach(row => {
        const eCode = classroomData({
            name: row[nameIndex],
            section: row[sectionIndex],
            room: row[roomIndex],
            description: row[descriptionIndex],
            ownerId: OWNER_EMAIL
        });
       enrollmentCodes.push([eCode]);
        sheet.getRange(
            START_ROW,
            COLUMN_START,
            enrollmentCodes.length,
            COLUMN_LAST)
            .setValues(enrollmentCodes);
    });
}

Here I have created a function to add a menu to run the code from the Google Sheet. This makes it easier for the moderator to create classes with the click of a button. To know more about menus, check out the video given below.

To create a menu in the Google Sheet, I started out by getting the UI of the sheet using the getUi() function, followed by using the createMenu() function to create the menu in the sheet.

Every menu that we create has a name and the function that needs to be run on clicking it. So in the additem() function, we have passed the name of the menu, followed by the function that we want to run. After creating the menu, we use the addtoUI() function to add them to the Google Sheet successfully.

function classroomData(properties){
    const crs = Classroom.newCourse();
    Object.keys(properties).forEach(key => {
        Crs[key] = properties[key];    })
    const createdCourse = Classroom.Courses.create(crs);
    return createdCourse.enrollmentCode;
}

Here we have created a function called ClassroomData, where we pass an object properties, containing all the details about the classrooms.

Next, we create a new course using the Classroom.newCourse() function and store it in a variable crs. The properties object contains the following details:

  1. Name of the Class
  2. Description of the Class
  3. Room
  4. Email address of the class

Next, we are going to assign each of the details to the required variable. For example, to set the name of the classroom, we are going to assign ClassData.name to the crs.name variable. We will be doing the same for all the variables by iterating through each of them.

After we have all the data stored about the classroom’s that need to be created in the variable crs. We are going to create the classroom and pass the crs variable as an argument using the Classroom.Courses.create() function.

As discussed previously, we want to paste the enrollment code into the Google Sheet. To get the enrollment code, we are going to use the Google Script setvalues and createdCourse.enrollmentCode functions.

function createClasses(){
    
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getActiveSheet();
    const START_ROW= 2;
    const START_COLUMN = 1;
    const LAST_COLUMN= 4;
    const data = sheet.getRange(
        START_ROW,
        START_COLUMN,
        sheet.getLastRow()-1,
        LAST_COLUMN).getValues();
    
    const enrollmentCodes = [];
    const nameIndex = 0;
    const sectionIndex = 1;
    const roomIndex = 2
    const descriptionIndex = 3;
    const START_COLUMN = 5;
    const LAST_COLUMN = 1;
    data.forEach(row => {
        const eCode = classroomData({
            name: row[nameIndex],
            section: row[sectionIndex],
            room: row[roomIndex],
            description: row[descriptionIndex],
            ownerId: OWNER_EMAIL
        });
        enrollmentCodes.push([eCode]);
        sheet.getRange(
            START_ROW,
            COLUMN_START,
            enrollmentCodes.length,
            COLUMN_LAST).setValues(enrollmentCodes);
    });

}

In this function, we are going to get the data that we need to send back to the above function followed by pasting the enrollment code for each of the Google Classrooms created.

We are going to start out by getting the Active Spreadsheet using the getActiveSpreadsheet function, followed by getting the active sheet.

Next, we are going to get the data using the getRange, by passing the following parameters:

  1. Start Row Index
  2. Start Column Index
  3. used the getLastRow function to get the last row
  4. Last Column Index

After using the getRange function we use the getValues() function to get the data according to the range specified.

const enrollmentCodes = [];

Here we have created an object that will contain all the Enrollment codes for each Class.

data.forEach(row => {
    const eCode = classroomData({
        name: row[nameIndex],
        section: row[sectionIndex],
        room: row[roomIndex],
        description: row[descriptionIndex],
        ownerId: OWNER_EMAIL
    });

Previously, we stored all the data about the classes in the data variable, now using the forEach function we are going to go through each of them. Since, the name of the classroom is first, to get the name we wrote r[nameIndex]. This is the same for all the other variables present.

sheet.getRange(START_ROW,
               COLUMN_START,
               enrollmentCodes.length,COLUMN_LAST)
               .setValues(enrollmentCodes);

Now that we have got all the enrollment codes, it’s time to paste them back into the Google Sheet. To do that we are first going to use the getRange function to get the exact column for the exact codes, followed by using the setValues function to paste the values to the sheet. In the setValues function, we have passed the object enrollmentCodes that we recently created.

We are done with our code, let’s move towards the output.

Step 4: Check the Output

At the beginning of the blog, we created a function that would add a menu to our Google Sheet, which will be later used to run the code. Select the correct function (onOpen) as shown below and run the program.

On successful execution, the script will add a menu to the Google Sheet with the name Create Classroom as shown below.

To run the main code just click on Create Classroom. On successful execution of the main function, you will see the following output.

Here you can see the class code has been posted for each of the Classrooms. To check whether the mentioned details have been added to the Google Classroom, go to and open Google Classroom. Here I have opened a Classroom and all the details have come in successfully.

What is Google Classroom?

Google Classroom is a free learning platform developed by Google for schools that aim to simplify creating, distributing and grading assignments.

Interested in learning Google Classroom? check out this course to know more.

Summary: How to Set Up a Google Classroom with Google Apps Script

We saw how to make a Google Classroom with custom details in bulk using the Google Classroom API and Google Apps Script. To sum up:

  1. Created a menu function to run our code
  2. Added the Google Classroom API to the project
  3. Created classes by passing the details for each Class
  4. Got the Enrollment code for each Class
  5. Pasted the Enrollment code next to each Class in the Google Sheet.

I hope you have understood how to Create Bulk Classes in Google Classroom using the Google Classroom API and Google Apps Script. You can send your feedback to aryanirani123@gmail.com.

Great! Next, complete checkout for full access to Form Publisher Blog
Welcome back! You've successfully signed in
You've successfully subscribed to Form Publisher Blog
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated