-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
57 lines (43 loc) · 2.04 KB
/
Code.gs
File metadata and controls
57 lines (43 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function onFormSubmit(e) {
var studentEmail = e.response.getRespondentEmail();
var sheetName = studentEmail + ' Problems';
// Get the destination folder by name or create it if it doesn't exist
var destinationFolder = getOrCreateFolder('Networking/subnetproblems');
// Create a new Google Sheet for the student
var spreadsheet = SpreadsheetApp.create(sheetName);
var sheetId = spreadsheet.getId();
// Generate a unique code for each student
var unique_code = Math.floor(Math.random() * 100000);
// Get the newly created sheet
var sheet = SpreadsheetApp.openById(sheetId).getActiveSheet();
// Clear any existing data from the sheet
sheet.clear();
for (var i = 0; i < 5; i++) {
var problem = generateProblem(unique_code + i);
// Write the problems to the sheet
sheet.appendRow(["Problem " + (i+1) + ": IP Address: " + problem[0] + ", CIDR: " + problem[1]]);
}
// Write the unique code to the sheet
sheet.appendRow(["Unique Code: " + unique_code]);
// Move the spreadsheet to the destination folder
var file = DriveApp.getFileById(sheetId);
var movedFile = destinationFolder.createFile(file);
DriveApp.getFileById(sheetId).setTrashed(true); // Remove the original in the root folder
// Share the sheet with the student
movedFile.addViewer(studentEmail);
// Send an email to the student with a link to their sheet
MailApp.sendEmail(studentEmail, 'Your Unique Problems', 'You can view your unique problems at this link: ' + movedFile.getUrl());
}
function getOrCreateFolder(folderPath) {
var folders = folderPath.split('/');
var currentFolder = DriveApp.getRootFolder();
for (var i = 0; i < folders.length; i++) {
var folder = currentFolder.getFoldersByName(folders[i]);
if (folder.hasNext()) {
currentFolder = folder.next();
} else {
currentFolder = currentFolder.createFolder(folders[i]);
}
}
return currentFolder;
}