I have a Google Apps Script that pulls and lists all the assignments from Google Classroom within a domain in a Google Sheet, but now I need to pull only certain assignments after a certain date. I must be putting the date or condition wrong, because the array remains empty at the end (whereas if I comment out the if condition with the date filter, the code works perfectly fine and lists all assignments).
The other "non-visible" problem is that the for loop is still going through all of the assignments before applying the filter, which wastes time. The idea would be to have for loop be limited to only the assignments after a certain date from the beginning instead of searching through everything, but I don't know if that is possible.
The code is below.
function listAssignmentsbyDate() {
var s1 = SpreadsheetApp.getActiveSpreadsheet();
var sh = s1.getSheetByName('LA');
var response = Classroom.Courses.list();
var courses = response.courses;
var arr1 = []; var arr2 = []; var arr3 = []; var arr4 = [];
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
for (i = 0; i < courses.length; i++) {
var course = courses[i];
var ids = course.id;
var title = course.name;
var state = course.courseState;
arr1.push([ids,title,state]);
}
for (i = 0; i < arr1.length; i++) {
if (arr1[i][2] !== 'ARCHIVED'){
var list = arr1[i][0];
var page = null, t = [];
do {
var tea = Classroom.Courses.CourseWork.list(list, {pageToken: page, pageSize: 100});
page = tea.nextPageToken; var t = t.concat(tea.courseWork);
} while (page);
try {
for (q = 0; q < t.length; q++) {
var c = t[q];
var name = arr1[i][1];
// The line below is what is not working.
if(oneWeekAgo < c.creationTime){
var ti = c.title;
var des = c.description;
var st = c.state;
var link = c.alternateLink;
var typ = c.workType;
var poi = c.maxPoints;
var create = c.creationTime;
var due = c.dueDate;
var duet = c.dueTime;
var cour = c.courseId;
var ids = c.id;
var user = c.creatorUserId;
arr2.push([name,ti,des,st,link,typ,poi,create,due,duet,cour,ids]);
arr3.push(user);
}
}
} catch (e) {continue;}
}
}
for (r=0; r < arr3.length; r++){
try {
var eu = arr3[r];
var us = AdminDirectory.Users.get(eu).primaryEmail;
arr4.push([us]);
} catch(e){continue;}
}
sh.getRange(2, 2, arr4.length, arr5[0].length).setValues(arr4);
sh.getRange(2, 3, arr2.length, arr2[0].length).setValues(arr2);
s1.getSheetByName('CACHE').getRange('B1').setValue('');
}