首页 > 解决方案 > 从 Google 课堂获取学生提交的内容

问题描述

目标:使用 Google 应用脚本从学生提交(附件)到 Google 课堂作业中获取 {link:url} 和 {driveFile:alternativeLink}。

问题:虽然我可以获得所有附件,但我无法过滤到特定类型的附件或其受人尊敬的属性。特定类型的附件返回“未定义”。任何帮助将不胜感激。

我可以使用 Classroom API 网站通过添加到“字段”输入来获得所需的结果:studentSubmissions.assignmentSubmission.attachments.driveFile

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

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}` 
  });         
}

标签: google-classroom

解决方案


答:(特别感谢 Yagisanatode.com 为我指明了正确的方向。)

第一:确保添加了适当的范围...请参阅 Sourabh Choraia stackOverflow response的响应。范围将确保我们可以访问对象。一旦我们请求特定对象(例如:链接或驱动文件),不属于该对象类型的附件将显示为未定义。

第二:我们需要删除未定义的对象。为此,我们可以关注 w3resource(javascript 版本),将格式添加到我们的“测试”函数(w3resource 示例)。

我们还需要通过展平来调整阵列。展平数组将通过包含未定义的对象来显示正确的长度。

最后,对于结果,我们将映射它并拉取所需的属性(Google Api - Student Submissions List)。

这是工作示例:

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

}

推荐阅读