0

I have several courses that all have the same set of topics with coursework (Assignments) that have the same title in each of those topics. I am trying to delete those assignments from just a few of the topics in each course using Apps Script. I expected to be able to use Classroom.Courses.CourseWork.delete(), but, so far, my code does nothing. No error - just no change in the Assignments. I think my permissions and such are in order, because these are assignments that I created with this same account/permissions.

My issue might be that I am not able to get the coursework ids from the name of the assignment. I'm not sure how to do that, but I think it could be missing from my code.

Here's what I tried:

function deleteAssignments() {
  var courseIds = ['100000000000','100000000001','100000000002'];
  var topicNames = ['topic3','topic2','topic1'];
  for (var i = 0; i < courseIds.length; i++) {

    var topics = Classroom.Courses.Topics.list(courseIds[i]).topic;
    var topicObj = topics.reduce((o, e) => Object.assign(o, {[e.name]: e.topicId}), {});
    for (var j = 0; j < topicNames.length; j++) {

      var topicId = topicObj[topicNames[j]];

      var exec = Classroom.Courses.CourseWork.delete({
        title: "Example Assignment",
        topicId: topicId,
        workType: "ASSIGNMENT",
      }, courseIds[i]);
    }
  }
} 

I checked the Google Classroom courses.courseWork docs, but I think I don't understand well enough how to structure my code as a whole to make use of the info there, because I don't have any background knowledge/training.

I used Problem listing assignments of a student in Google Classroom to come up with my code, but mine is not working and I can't figure out where I'm going wrong.

I also looked at Using Google Apps Script to list assignments through Google Classroom API using course.coursework.list, but when I run the code in the answer, my log says that it's loading, but it never appears to finish.

moe
  • 43
  • 6
  • Can I ask you about your goal? Which do you want to delete the course works or the topics? By the way, when you want to delete a course work, you can use the script of `Classroom.Courses.CourseWork.remove(courseId, id)`. In this case, ID is the unique ID of course work. [Ref](https://developers.google.com/classroom/reference/rest/v1/courses.courseWork/delete) – Tanaike Jul 06 '20 at 23:08
  • @Tanaike I'm trying to delete the courseworks (keeping the topics) - I have courseworks with the same title in a bunch of different topics, and I only want to delete it from some of those topics. I think I'm having trouble getting the unique ids of the courseworks, so that I can delete them, maybe. – moe Jul 06 '20 at 23:21
  • Thank you for replying. I could understand that you want to delete the course works using Google Apps Script. And also, I understood that for deleting the course works, you want to delete the course works by searching the topic name. But, the topic has the fields of `courseId`, `topicId`, `name` and `updateTime`. And it seems that the course work has no topic IDs and names. So I think that the course work ID cannot be retrieved from the topic name. How about this? – Tanaike Jul 06 '20 at 23:37
  • @Tanaike I was trying to delete it based on the coursework name. The topic names are in there because I needed to use those to get the topic ids to indicate which topics I wanted the coursework deleted from. I'll update the question and see if I can make it clearer. – moe Jul 06 '20 at 23:43
  • Thank you for replying. When you have the coursework names, I think that the the courseworks can be deleted by the coursework name. For this, I can propose a modified script. – Tanaike Jul 06 '20 at 23:45
  • I would like to confirm your goal. You have the coursework names. And you want to delete courseworks using the coursework names. Is my understanding correct? – Tanaike Jul 06 '20 at 23:50
  • @Tanaike Yes - there are many courseworks that have the same name, and I want to delete the ones listed under certain topic sections only. – moe Jul 06 '20 at 23:51
  • Thank you for replying. I have to apologize for my poor English skill. In that case, it is required to retrieve the courseworks by searching the topic names. Unfortunately, it seems that the course work has no topic IDs and names. So I think that the course work ID cannot be retrieved from the topic name. How about this? – Tanaike Jul 06 '20 at 23:53
  • Just deleting using the coursework name would be really useful as well. Thanks! – moe Jul 07 '20 at 00:03
  • I proposed a sample script as an answer. Could you please confirm it? – Tanaike Jul 07 '20 at 00:13

1 Answers1

2

In this sample script, the courseworks are deleted by searching the coursework names.

Sample script:

Before you use this script, please confirm Classroom API is enabled at Advanced Google services, again.

function deleteCourseworks() {
  var courseIds = ['###courseId1###', '###courseId2###',,,];
  var courseworkNames = ["###coursework name1###", "###coursework name2###",,,];

  for (var i = 0; i < courseIds.length; i++) {
    var courseWorks = Classroom.Courses.CourseWork.list(courseIds[i], {courseWorkStates: "PUBLISHED"}).courseWork;
    var courseWorkObj = courseWorks.reduce((o, e) => Object.assign(o, {[e.title]: e.id}), {});
    for (var j = 0; j < courseworkNames.length; j++) {
      var courseWorkId = courseWorkObj[courseworkNames[j]];
      console.log(courseIds[i])
      console.log(courseWorkId)
      var exec = Classroom.Courses.CourseWork.remove(courseIds[i], courseWorkId);
    }
  }
}
  • In this case, if you want to delete the courseworks of the state of "DRAFT", please modify courseWorkStates: "PUBLISHED" to courseWorkStates: "DRAFT".

Note:

  • When an error like "status" : "PERMISSION_DENIED" and The Developer Console project is not permitted to make this request. occurs at Classroom.Courses.CourseWork.remove, the reason of issue can be seen at this thread. Please be careful this.
    • In that case, for example, it seems that the courseworks created with the GAS project can be deleted. It seems that this is the current specification.

References:

Added:

The following sample script deletes the courseworks by searching the coursework names. But in this case, when the same coursework names are existing, all courseworks of them are deleted. So when you use this, please be careful this.

Sample script:

function deleteCourseworks() {
  var courseIds = ['###courseId1###', '###courseId2###',,,];
  var courseworkNames = ["###coursework name1###", "###coursework name2###",,,];

  for (var i = 0; i < courseIds.length; i++) {
    var courseWorks = Classroom.Courses.CourseWork.list(courseIds[i], {courseWorkStates: "PUBLISHED"}).courseWork;
    var courseWorkObj = courseWorks.reduce((o, e) => Object.assign(o, {[e.title]: o[e.title] ? o[e.title].concat(e.id) : [e.id]}), {});
    for (var j = 0; j < courseworkNames.length; j++) {
      var courseWorkIds = courseWorkObj[courseworkNames[j]];
      if (courseWorkIds) {
        courseWorkIds.forEach(id => {
          var exec = Classroom.Courses.CourseWork.remove(courseIds[i], id);
        });
      }
    }
  }
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This one seems to delete one assignment. I have many copies of the same assignment in each course, and I want to delete all of them. Is this a possible thing? – moe Jul 07 '20 at 00:34
  • @moe Thank you for replying. I deeply apologize for the inconvenience. From your replying, I proposed one more sample script in my answer. Could you please confirm it? But in that case, when the same coursework names are existing, all courseworks of them are deleted. So when you use this, please be careful this. – Tanaike Jul 07 '20 at 00:47