如何从教室URL中获取课程ID?

问题描述 投票:0回答:1

目标:我想通过Google Apps Script使用Google Classroom URL快速连接到Google Classroom。

问题:需要帮助通过URL过滤课程地图。

背景:Classroom API对GAS的文档很少。 此外,COURSE_ID几乎用于所有的连接。 我可以映射活动课程,但我不能过滤地图。下面的代码来源于 矢车菊 的修改,试图通过URL映射活动课程。 将记录器改为(courseData),可以看到双数组的创建。

function findCourseByUrl() {
const courseList = Classroom.Courses.list({"courseStates":["ACTIVE"]}).courses;


const courseData = courseList.map(course => {

    let ownerName = Classroom
                .Courses
                .Teachers
                .get(course.id, course.ownerId)
                .profile
                .name
                .fullName;


    return `[${course.name}, ${course.id}, ${ownerName}, ${course.alternateLink}]`;

});

const link = 'https://classroom.google.com/c/YOUCLASSROOMURL'; //change this
const data = courseData.filter(function(item){return item[4] === link;});
Logger.log(data);
};

任何帮助将是感激的。 我被卡住了。

google-apps-script google-api map-function google-classroom
1个回答
0
投票

答案:链接没有定义,因为它是在courseData.filter(function(item){})之外。解决方法是调用一个全局变量或者在function(item)中用声明的变量创建一个条件。

toString是在寻找一个完全匹配的URL文本,这自然是唯一的。视频参考。https:/youtu.bePT_TDhMhWsE

准则:

function findCourseByUrl() {
    const courseList = Classroom.Courses.list({"courseStates":["ACTIVE"]}).courses;
    
    
    const courseData = courseList.map(course => {
    
        let ownerName = Classroom
                    .Courses
                    .Teachers
                    .get(course.id, course.ownerId)
                    .profile
                    .name
                    .fullName;
              
 
        return `[${course.name}, ${course.id}, ${ownerName}, ${course.alternateLink}]`;
    
    });
    
    const filterCourse = function(item){
    let link = 'https://classroom.google.com/c/YOURCOURSEURL'  ///Change this or replace with a global variable
    if(item.toString().indexOf(link) === -1){
    return false;
    } else {
    return true
    }
    };

    
   let theCourse = courseData.filter(filterCourse);  //this could be a return if called by function in Test.gs
   
   Logger.log(theCourse); //remove if using a function with console.log in Test.gs

};
© www.soinside.com 2019 - 2024. All rights reserved.