Skip to content

Instantly share code, notes, and snippets.

@rickcnagy
Last active August 29, 2015 13:56
Show Gist options
  • Save rickcnagy/9010093 to your computer and use it in GitHub Desktop.
Save rickcnagy/9010093 to your computer and use it in GitHub Desktop.
Parse subject enrollment information (via Google Apps Script) where each enrollment has a single line (so multiple per student), instead of 1 row per student. To produce the enrollment information, run the report: Subject Enrollment for Subject Set/Band Importer.
// For Google Apps Script
// qs-EnrollmentParser.js
// Rick Nagy, 2/14/14
/*
Creates an output file in the directory of the input file.
Input file should be direct output from Subject Enrollment for Subject Set/Band Importer.
Folder name of input file should be the name of the school.
*/
INPUT_URL = "https://docs.google.com/spreadsheets/d/1swQtAXa6yPxZ0tShHb1SOZzCSqKwlauLLrb-gFtaRoQ/edit";
function main() {
var ss = SpreadsheetApp.openByUrl(INPUT_URL).getSheets()[0];
var enrollmentVals = ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn()).getValues();
students = Student.studentsFromEnrollments(enrollmentVals);
Student.spreadsheetDump(students);
}
function Student(name, classCode) {
this.name = name;
this.classCode = classCode;
this.subjects = [];
}
Student.prototype.subjectsString = function() {
var ret = '';
for (var i = 0; i < this.subjects.length; i++) {
ret += this.subjects[i] + ':';
}
return ret.substring(0, ret.length - 1);
}
Student.prototype.uniqueSubjects = function(other) {
if (this.name !== other.name) return;
for(var i = 0; i < this.subjects.length; i++) {
for(var j = 0; j < other.subjects.length; j++) {
if (this.subjects[i] === other.subjects[j]) {
this.subjects.splice(i, 1);
i--;
break;
}
}
}
}
Student.spreadsheetDump = function(students) {
// output to spreadsheet
var outputSS = outputFile();
for (var i = 0; i < students.length; i++) {
outputSS.getRange(i + 1, 1).setValue(students[i].name);
outputSS.getRange(i + 1, 2).setValue(students[i].classCode);
outputSS.getRange(i + 1, 3).setValue(students[i].subjectsString());
}
setColumnWidths(outputSS);
}
Student.studentsFromEnrollments = function(enrollmentVals) {
var students = [];
for (var i = 1; i < enrollmentVals.length; i++) {
var row = enrollmentVals[i];
Logger.log(row);
var studentName = row[1];
var subjectName = row[2];
var classCode = row[3];
if (studentName !== '') {
students.push(new Student(studentName, classCode));
}
if (students.length >= 1) {
students[students.length - 1].subjects.push(subjectName);
}
}
return students;
}
function outputFile() {
var inputId = SpreadsheetApp.openByUrl(INPUT_URL).getId();
var inputFile = DriveApp.getFileById(inputId);
var folder = inputFile.getParents().next();
var outputFile = createSheet(folder);
var outputSheet = SpreadsheetApp.openById(outputFile.getId()).getSheets()[0];
return outputSheet;
}
function createSheet(folder) {
var outputName = folder.getName() + " (output)";
return folder.createFile(outputName,'',MimeType.GOOGLE_SHEETS);
}
function setColumnWidths(outputSS) {
outputSS.setColumnWidth(1, 200);
outputSS.setColumnWidth(2, 200);
outputSS.setColumnWidth(3, 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment