Here is a short, step-by-step guide for using dynamic templates with Google Apps Script in Google Classroom. This process is great for advanced users looking to automate repetitive tasks and can be adapted and simplified for a wider audience.
Step 1: Prepare Your MaterialsCreate a "Master Template" Folder: In your Google Drive, create a new folder and name it something like "Master Templates."
Step 2: Set Up the Google Apps Script
function createCustomAssignments() {
// 1. Specify the ID of your 'Master Templates' folder
var templateFolderId = 'YOUR_MASTER_TEMPLATE_FOLDER_ID_HERE'; // Find this in the URL of your folder
// 2. Specify the ID of your 'Class Roster' Spreadsheet
var spreadsheetId = 'YOUR_CLASS_ROSTER_SPREADSHEET_ID_HERE'; // Find this in the URL of your Spreadsheet
var sheetName = 'Sheet1'; // Make sure this matches your sheet name
// 3. Define the placeholder text to replace
var studentNamePlaceholder = '[Student Name]';
var datePlaceholder = '[Date]'; // Define this one as needed
// --- SCRIPT LOGIC (Do not edit below this line unless you know what you are doing) ---
var folder = DriveApp.getFolderById(templateFolderId);
var rosterSpreadsheet = SpreadsheetApp.openById(spreadsheetId);
var sheet = rosterSpreadsheet.getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
// Loop through each student in the roster
for (var i = 1; i < data.length; i++) {
var studentName = data[i][0];
var studentEmail = data[i][1];
// Replace placeholders in all files within the template folder
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var newFile = file.makeCopy(studentName + ' - ' + file.getName());
if (newFile.getMimeType() === MimeType.GOOGLE_DOCS) {
var docBody = DocumentApp.openById(newFile.getId()).getBody();
docBody.replaceText(studentNamePlaceholder, studentName);
// docBody.replaceText(datePlaceholder, new Date().toLocaleDateString()); // Example for date
}
// Add logic for other file types if needed (Slides, Sheets, etc.)
// Grant student edit access (optional but common)
newFile.addEditor(studentEmail);
}
}
}
Find and Insert Your IDs:
Folder ID: Open your "Master Templates" folder in Drive. The ID is the long string of characters in the URL after folders/.
Spreadsheet ID: Open your "Class Roster" Spreadsheet. The ID is the long string of characters in the URL after spreadsheets/d/ and before /edit.
Paste these IDs into the code where indicated.
Step 3: Run the Script
Click the "Save" icon (floppy disk) in the Apps Script editor.
Run the script: Click the "Run" button (triangle play icon).
Review Permissions: The first time you run a script that accesses your Drive, you'll need to grant permissions. Follow the on-screen prompts.
Step 4: Assign in Google Classroom
Once the script has finished running, personalized copies of your templates will be in your Google Drive. You can now use Google Classroom to assign them.
Create a New Assignment in your Google Classroom class.
Attach the Files: Instead of attaching files directly from your computer, you can select "Google Drive" and browse to the location where your script-generated files are stored.
Choose "Make a Copy for Each Student": When attaching the files, ensure this option is selected for the assignment type. This tells Google Classroom to use your personalized copies.
Simplifying this process: You can create a simplified script that only replaces a single placeholder in one document to make this guide even easier for teachers to follow initially. The key value is showing how a little bit of Apps Script can unlock massive time savings!

No comments
Post a Comment