0

Goal: use Google App Script to get {link:url} and {driveFile:alternativeLink} from student submissions (attachments) to a Google Classroom Assignment.

Issue: While I can get all of the attachments, I cannot filter down to the specific type of attachment or it's respected property. Specific types of attachments return 'undefined'. Any help would be greatly appreciated.

I can get the the desired results using the Classroom API website by adding to the "field" input: studentSubmissions.assignmentSubmission.attachments.driveFile

https://developers.google.com/classroom/reference/rest/v1/courses.courseWork.studentSubmissions/liststrong text

function testStudSubs(){
 console.log(getStudSubs());
}


function getStudSubs(){

  const COURSE_ID = "60005382479";
  const COURSE_WORK_ID = "141252225149";
  const USR_ID = {userId:"105308051639096321984"};
  const ID = "Cg0IhMWczB0Q_dCnmo4E";



  const submissions = Classroom.Courses.CourseWork.StudentSubmissions.list(COURSE_ID, COURSE_WORK_ID, USR_ID).studentSubmissions

  return submissions.map(submission => {
                    return `${submission.assignmentSubmission.attachments}` 
  });         
}

1 Answers1

0

Answer: (Special thanks to Yagisanatode.com for pointing me in the correct direction.)

1st: ensure proper scopes have been added...see response from Sourabh Choraia stackOverflow response. The scopes will ensure we have access to the objects. Once we request a specific object (ex: link or driveFile), attachments that are not of that object type will display as undefined.

2nd: we need to remove the undefined objects. To do this, we can following w3resource (javascript version), adding the format to our "test" function (w3resource example).

We also need to tweak the array by flattening it. Flattening the array will show the correct length by including the undefined objects.

Finally, for the result, we will map it and pull the desired property (Google Api - Student Submissions List).

Here is working example:

function testStudSubs(){
  console.log(getStudSubs());
  console.log(getStudSubs().length);
  console.log(getStudSubs().flat(2));  // creates separate object for each...ex: 4

const myFlat = getStudSubs().flat(2);


  let index = -1;
    const arr_length = myFlat ? myFlat.length : 0;
  let resIndex = -1;
    const result = [];

  while (++index < arr_length) {
    const value = myFlat[index];

    if (value) {
        result[++resIndex] = value;
    }
}


  console.log(result.map(result => { return result.alternateLink + `:` + result.title}));
  return result.map(result => { return result.alternateLink + `:` + result.title});
}

/*/////////////////////////////
/
/ Pulls student submitted work from Classroom
/
*//////////////////////////////

function getStudSubs(){

  const COURSE_ID = "60005382479";   // update
  const COURSE_WORK_ID = "141252225149";  //update
  const USR_ID = {userId:"105308051639096321984"};  //update



  const submissions = Classroom.Courses.CourseWork.StudentSubmissions.list(COURSE_ID, COURSE_WORK_ID, USR_ID).studentSubmissions

  return submissions.map(submission => {
                return submission.assignmentSubmission.attachments.map(attachments => 
{
                   return attachments.driveFile
                });
  }); 

  return submissions

}