我正在使用Google Apps脚本在每个教室的几个先前创建的主题下的几个Google教室中创建作业。我知道主题名称,但不知道ID-有没有一种方法可以使用名称来创建分配以获取ID?
这是我正在尝试的(但是topicID最终为null而不是实际ID):
function addAssignments() {
var courseIds = ['100000000000','100000000001'];
var topicNames = ['Topic1','Topic2'];
for (var i = 0; i < courseIds.length; i++) {
for (var j = 0; j < topicNames.length; j++) {
var topicId = topicNames[j].topicID;
var exec = Classroom.Courses.CourseWork.create({
title: "My Assignment",
topicId: topicId,
workType: "ASSIGNMENT",
}, courseIds[i]);
}
}
}
[看着Adding a Topic ID to a Google Classroom assignment(但这是在分配作业的过程中创建了一个新主题-我的主题已经完成),和Is there an example of the Classroom.Courses.Topics.get so I can call Classroom.Courses.CourseWork.create with an existing topicId?(但是,当我尝试courses.topics.get()时,我不知道一种从响应中仅获取我需要的ID的快速方法-问题也可能是我的迭代之类的,所以我已经在代码示例中包含了它。
我相信您的目标如下。
为此,这个答案如何?
var topicId = topicNames[j].topicID;
,您尝试从topicID
的数组中检索var topicNames = ['Topic1','Topic2'];
。当上述几点反映到您的脚本时,它将变成如下。
function addAssignments() {
var courseIds = ['100000000000','100000000001'];
var topicNames = ['Topic1','Topic2'];
for (var i = 0; i < courseIds.length; i++) {
var topics = Classroom.Courses.Topics.list(courseIds[i]).topic; // Added
var topicObj = topics.reduce((o, e) => Object.assign(o, {[e.name]: e.topicId}), {}); // Added
for (var j = 0; j < topicNames.length; j++) {
var topicId = topicObj[topicNames[j]]; // Added
var exec = Classroom.Courses.CourseWork.create({
title: "My Assignment",
topicId: topicId,
workType: "ASSIGNMENT",
}, courseIds[i]);
}
}
}