如何使用 google 表格中的应用程序脚本获取 google 课堂作业数据

How to get google classroom assignment data using app script in google sheets

您好,我正在尝试在我的 sheet 中获取 google 学生名单的课堂作业。我知道我需要如何在 sheet 中添加它的值,但问题是我没有以我希望的格式获取数据。我以错误的格式获取数据。你们中的任何人都可以帮助我吗?这是我的代码。

  const courseWork = Classroom.Courses.CourseWork.list(courseId, { orderBy: 'dueDate desc' }).courseWork
        .filter(x => _.lowerCase(x.title).indexOf('assignment') !== -1 || _.lowerCase(x.title).indexOf('challenge') !== -1)
        .map(x => {
            return _.assign(
                x,
                { sub: _.groupBy(Classroom.Courses.CourseWork.StudentSubmissions.list(courseId, x.id).studentSubmissions, 'userId') }
            );
        });

    const topics = _(courseWork)
        .groupBy('topicId')
        .mapKeys((v, k) => _.find(Classroom.Courses.Topics.list(courseId).topic, ['topicId', k]).name)
        .toPairs()
        .map(x => { return { name: x[0], cw: x[1] }})
        .value();

    sheet.getRange('F1:1').clear();
    sheet.getRange(1, 6, 1, topics.length + courseWork.length).setValues([_.flatMap(topics, t => [t.name, ..._.map(t.cw, 'title')])]);

    sheet.getRange('C2:100').clear().removeCheckboxes();    // assumption: 100 se zyada student nahi honge
    sheet.getRange(2, 3, studentsList.length, 3 + topics.length + courseWork.length)
        .setValues(studentsList.map(s => {
            const topicTreeForStudent = topics.map(t => {
                const sub = t.cw.map(x => x.sub[s.userId][0].state);
                return _.assign(t, { sub: sub, submittedCount: sub.filter(st => st === 'TURNED_IN' || st === 'RETURNED').length });
            });

            return [
                s.profile.name.fullName, s.profile.emailAddress, _.sumBy(topicTreeForStudent, 'submittedCount') / courseWork.length,
                ..._(topicTreeForStudent).map(x => [x.submittedCount / x.sub.length, ...x.sub]).flatten()
            ];
        }));

问题是我得到的数据格式如图所示,但我只想在此处查看相应行的上交、丢失或延迟状态。我不希望状态显示我已创建我只想查看具有相应电子邮件 ID 的数据,如果该作业已提交或尚未丢失或学生提交晚了。

我不知道这里是否有真正的英雄可以解开你华丽的代码(我失败了)......所以,作为最后的手段,我建议(胆小)只是过滤结果table。类似的东西:

function remove_all_but_TURNED_IN() {
  const ss = SpreadsheetApp.getActiveSheet();
  const data = ss.getDataRange().offset(1,0).getValues();
  const new_data = data.filter(x => x[3] == 'TURNED_IN');
  ss.getDataRange().offset(1,0).clearContent();
  ss.getRange(2,1,new_data.length,new_data[0].length).setValues(new_data);
}

输入:

┌───────────────┬─────┬─────────────┬─────────────┬─────────────┬─────────────┐
├───────────────┼─────┼─────────────┼─────────────┼─────────────┼─────────────┤
│ 'aaa@aaa.aaa' │ 123 │  'CREATED'  │  'CREATED'  │  'CREATED'  │  'CREATED'  │
│ 'bbb@bbb.bbb' │ 456 │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │
│ 'ccc@ccc.ccc' │ 789 │  'CREATED'  │  'CREATED'  │  'CREATED'  │  'CREATED'  │
│ 'ddd@ddd.ddd' │ 123 │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │
└───────────────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘

输出:

┌───────────────┬─────┬─────────────┬─────────────┬─────────────┬─────────────┐
├───────────────┼─────┼─────────────┼─────────────┼─────────────┼─────────────┤
│ 'bbb@bbb.bbb' │ 456 │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │
│ 'ddd@ddd.ddd' │ 123 │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │ 'TURNED_IN' │
└───────────────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘