Search code examples
google-apps-scriptgoogle-classroom

Turn in assignment with Google Apps Script as student


I am trying to create a google apps script that will turn in my Google Classroom assignments. My teachers post assignments frequently which don't require any submissions, and it is annoying to have to turn each one in manually.

So far I have this script:

// this is replaced by an actual course ID
const sampleClassID 123456789012

function turnInWork() {
  // gets course work from sample class 
  let courseWork = Classroom.Courses.CourseWork.list(sampleClassID).courseWork;

  // loops through each assignment
  for (const work in courseWork) {
    console.log(courseWork[work].title, courseWork[work].id);

    // code to turn in work here
  }
}

I have looked through the Google Classroom API and cannot find any way to do this. I feel like there should be a way to do this, but maybe there just isn't. I have tried the courses.courseWork.studentSubmissions.turnIn method, but I don't know what to put for the third parameter id.

Please leave an answer if you have any useful information to give.


Solution

  • You were on the right track with the code you wrote.

    Right now you're trying to turn in a coursework, but that actually represents the assignment generally; what you want to turn in is a submission, which represents a given student's "instance" of the assignment. You can continue your code to loop the submissions and retrieve the submission IDs (that's the third id param you're unsure about). Then you would use the courses.courseWork.studentSubmissions.turnIn method.

    Something like:

    // this is replaced by an actual course ID
    const sampleClassID 123456789012
    
    function turnInWork() {
      // gets course work from sample class 
      let courseWork = Classroom.Courses.CourseWork.list(sampleClassID).courseWork;
    
      // loops through each assignment
      for (const work in courseWork) {
        console.log(courseWork[work].title, courseWork[work].id);
    
        // gets submissions from coursework (assignment)
        const submissions = Classroom.Courses.CourseWork.studentSubmissions.list(sampleClassID, courseWork[work].id)
        
         // loop through each submission
        for (const submission in submissions) {
           
          // now you have the submission ID and can use turnIn
          Classroom.Courses.CourseWork.studentSubmissions.turnIn(sampleClassID, courseWork[work].id, submission.id)
        }
      }
    }
    

    I say "would" though, because I don't believe you are currently able to turn in your work with the API unfortunately. If you look at the documentation for turnIn, you'll see:

    This request must be made by the Developer Console project of the OAuth client ID used to create the corresponding course work item.

    This means unless your script (or project) created the assignment, it can't turn it in. This may be annoying because it is your submission, but the limitation here may be because the API doesn't want to let assignments created by one organization/project/tool to be turned in by other organizations/projects/tools.

    You can go to the Classroom API support page and file a feature request if you think this should be changed.