目标:使用Google App脚本从学生提交的附件(附件)中获取{link:url}和{driveFile:alternativeLink}到Google课堂作业。
问题:虽然我可以获取所有附件,但无法过滤到特定类型的附件或其受尊敬的属性。特定类型的附件返回“未定义”。任何帮助将不胜感激。
我可以使用Classroom API网站通过添加到“字段”输入中来获得所需的结果: studentSubmissions.assignmentSubmission.attachments.driveFile
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}`
});
}
答案 0 :(得分:0)
答案:(特别感谢Yagisanatode.com为我指明了正确的方向。)
第一:确保已添加适当的范围...请参阅Sourabh Choraia stackOverflow response的回复。范围将确保我们可以访问对象。一旦我们请求了一个特定的对象(例如:link或driveFile),不是该对象类型的附件将显示为未定义。
2nd:我们需要删除未定义的对象。为此,我们可以遵循w3resource(javascript版本),将格式添加到我们的“测试”功能(w3resource example)中。
我们还需要通过展平数组来对其进行调整。通过包含未定义的对象,展平数组将显示正确的长度。
最后,对于结果,我们将其映射并提取所需的属性(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
}