Create Assignments in Google Classrooms

103 views
Skip to first unread message

J

unread,
Jan 19, 2021, 11:38:07 AM1/19/21
to Google Apps Script Community
Hello,
I have the code below that copies an assignment out to multiple google classrooms.

Issue is that it only works for 5 classroom IDs in a list in col A.

I assume there must be a way to make it work for lots of classroom IDs - ideally however many i put on the list, which could be up to 900 odd as I manage a whole schools classrooms.

I am however having a brick wall moment, so if anyone is able to help me work out why it is restricted to 5 and suggest changes, that would be stupendous.

Many thanks
Jon

function createTasks() {
 var startTime= (new Date()).getTime();
 Logger.log(startTime);
 
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SendAssignments");
 var courseId = ss.getRange(1, 2).getValue();
 var assignmentId = ss.getRange(2, 2).getValue();
 
 
 var resource = Classroom.Courses.CourseWork.get(courseId, assignmentId);
 //Logger.log(resource);
 
 //delete some props
 var props = ["courseId","id","alternateLink","creationTime","updateTime","creatorUserId","topicId","assignment"];
 for (var p=0;p<props.length;p++){
  delete resource[props[p]];
  }
 
 var copiedFiles = [];
 try{
 var materials = resource.materials;
 for (var m=0;m<materials.length;m++){
   try{
   var material = materials[m];
   var driveFile = material.driveFile.driveFile.id;
   var title = material.driveFile.driveFile.title
     delete resource.materials[m].driveFile.driveFile.alternateLink;
 delete resource.materials[m].driveFile.driveFile.thumbnailUrl;
 delete resource.materials[m].driveFile.driveFile.title;
   Logger.log(driveFile);
     var newCopy = DriveApp.getFileById(driveFile).makeCopy(title+" Copy");
     newCopy.setSharing(DriveApp.Access.DOMAIN, DriveApp.Permission.EDIT);
     var newId = newCopy.getId();
     copiedFiles.push(newId);
     resource.materials[m].driveFile.driveFile.id = newId;
     Logger.log(resource.materials[m].driveFile.driveFile.id);
   }catch(error){}
 }
 }catch(error){}
 
 Logger.log(resource);
 var newResource = JSON.stringify(resource);
 
 var data = ss.getDataRange().getValues();
 for (var i=4;i<data.length;i++){
   var status = data[i][2];
   if(status==""){
     var currTime = (new Date()).getTime();
     if(currTime - startTime >= 20000) {
       ScriptApp.newTrigger("createAssignments")
              .timeBased()
              .at(new Date(currTime+30000))
              .create();
     break;
       
   } else {
   var newId = data[i][0];
     var userEmail = data[i][1];
     //var newClassroom = createServiceAsignment(newResource,newId,userEmail);
     var newClassroom = Classroom.Courses.CourseWork.create(resource, newId);
     var returned = JSON.parse(newClassroom);
     var newAssignmentId = returned.id;
   var logStatus = ss.getRange(i+1, 3).setValue('Assignment '+newAssignmentId+' created '+new Date());
     }
   }
 }
 
 //removePermissions
 if(copiedFiles != []){
   for (var c= 0;c<copiedFiles.length;c++){
    var file = copiedFiles[c];
     DriveApp.getFileById(file).setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT);
   }
 }
}



CBM Services

unread,
Jan 19, 2021, 12:46:18 PM1/19/21
to google-apps-sc...@googlegroups.com
The script below seems to stop processing once it hits an item that has a blank status.

Would suggest you check the spreadsheet data to make sure you don't have a blank status field in it before starting.

If that is not the case, share an anonymous version of the spreadsheet so we can see what it is doing.

From: J
Sent: ‎2021-‎01-‎19 8:38 AM
To: Google Apps Script Community
Subject: [Apps-Script] Create Assignments in Google Classrooms

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1c33385c-17da-4e36-92c3-0d17f00f5905o%40googlegroups.com.

J

unread,
Jan 19, 2021, 1:03:28 PM1/19/21
to Google Apps Script Community
yes it should end when theres a blank - instead its ending after 5 rows when the next row is definitely not blank.
A copy of the sheet is here: 

As you can see its dirt simple - bung in a source classroom ID and assignment ID at the top, then a list of all classroom IDs to post to in col A.

CBMServices Web

unread,
Jan 19, 2021, 2:10:56 PM1/19/21
to google-apps-sc...@googlegroups.com
Thanks. But we will need edit access so that we can see the script and how it is configured in the spreadsheet.

 Thanks,
  George

J

unread,
Jan 19, 2021, 2:21:36 PM1/19/21
to Google Apps Script Community
ahh, sorry. you should be able to edit now

CBMServices Web

unread,
Jan 19, 2021, 2:35:27 PM1/19/21
to google-apps-sc...@googlegroups.com
Ok I took a look and it seems to be fine as far as I can tell.

 However, it was creating new variables every for loop which means your stack was getting larger and larger every loop. I dont know how large the variables returned are, but if large enough, it could cause a stack overflow.

 I modified the script to declare the variables outside the For loop and just re-assign the values in each loop.

 Update your script to match what I changed in the copy you provided to us and try it out and let us know how it goes.

 Thanks,
  George

Reply all
Reply to author
Forward
0 new messages