1

I'm using Google Apps Script to change the names of a few Topics in several Google Classrooms. I'm using Classroom.Courses.Topics.patch() to change only the 'name' value of the Topics, but my script does not change anything when I look at the Classrooms.

Here is an example: I have two Classrooms (course IDs '100000000000' and '100000000001'). In each Classroom I have three Topics (topic names 'topic1', 'topic2', and 'topic3'). I want to change the name of the first two topics to 'newtopic1' and 'newtopic2' respectively, in both classrooms.

I suspect there could be something off with the way I'm doing the update mask, but I've tried re-ordering things, and I still can't get it to go... Could also be my nested for loops?

function updateTopicNames() {
  var courseIds = ['100000000000','100000000001'];
  var topicNamesOld = ['topic1','topic2'];
  var topicNamesNew = ['newtopic1', 'newtopic2'];
  
  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 < topicObj.length; j++) {
      
      for (var k = 0; k < topicNamesNew.length; k++) {
        var topicId = topicObj[topicNamesOld[j]];
        var newName = {'name':topicNamesNew[k]};
        var extra =  {'updateMask':'name'};
        var exec = Classroom.Courses.Topics.patch(newName, topicId, courseIds[i], extra);
      }
    }
  }
}

I checked out the courses.topics.patch API, but there is no example of the update mask implementation for me to extrapolate from.

I tried to bootstrap from code for other .patch() things: StudentSubmissions.Patch UpdateMask Error and How to change course owner using Classroom.Courses.patch() but something is not working when I try to convert these for Topics.patch().

Rubén
  • 34,714
  • 9
  • 70
  • 166
moe
  • 43
  • 6

1 Answers1

1

I believe your situation as follows.

  • The index of topicNamesOld is corresponding to the index of topicNamesNew.

Modification point:

  • In your script, topicObj is not an array. So you are not required to use the for loop.

When this is reflected to your script, it becomes as follows.

Modified script:

function updateTopicNames() {
  var courseIds = ['100000000000','100000000001'];
  var topicNamesOld = ['topic1','topic2'];
  var topicNamesNew = ['newtopic1', 'newtopic2'];
  
  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 k = 0; k < topicNamesNew.length; k++) {
      var topicId = topicObj[topicNamesOld[k]];
      var newName = {'name': topicNamesNew[k]};
      var extra =  {'updateMask':'name'};
      var exec = Classroom.Courses.Topics.patch(newName, courseIds[i], topicId, extra);
    }
  }
}

Note:

  • In that case, it seems that the topics created with the same GAS project can be updated. For example, it seems that the topics created by other client cannot be updated. It seems that this is the current specification. Please be careful this.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165