Created
June 20, 2017 14:33
-
-
Save rheajt/bde0edcbdf99c8d437aee821674f8821 to your computer and use it in GitHub Desktop.
google apps script to create multiple sections programmatically from a spreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rosterMaker() { | |
//spreadsheet id of the rosters | |
var SHEET_ID = FormApp.getActiveForm().getDestinationId(); | |
var ss = SpreadsheetApp.openById(SHEET_ID); | |
var form = FormApp.getActiveForm(); | |
//get only the sheets with 'Roster' in the title | |
var sheets = ss.getSheets() | |
.filter(function(sheet) {return sheet.getName().match(/Roster/gi);}); | |
//add multiple choice item | |
var classSelect = form.addMultipleChoiceItem() | |
.setTitle('Choose a class'); | |
//get the class choices for the multiple choice item | |
var classChoices = getClasses(sheets); | |
//assign the choices to the classSelect variable | |
classSelect.setChoices(classChoices); | |
} | |
function getClasses(sheets) { | |
var classChoices = []; | |
for(var i = 0; i < sheets.length; i++) { | |
var className = sheets[i].getName(); | |
var classSection = form.addPageBreakItem() | |
.setTitle(className) | |
.setGoToPage(FormApp.PageNavigationType.SUBMIT); | |
var students = getStudents(sheets[i]); | |
var studentSelect = form.addCheckboxItem() | |
.setTitle(className + ' absent') | |
.setHelpText('Select the students who are absent from this class'); | |
var studentChoices = []; | |
for(var j = 0; j < students.length; j++) { | |
studentChoices.push(studentSelect.createChoice(students[j])); | |
} | |
studentSelect.setChoices(studentChoices); | |
classChoices.push(classSelect.createChoice(className, classSection)); | |
} | |
return classChoices; | |
} | |
function getStudents(sheet) { | |
var studentValues = sheet.getDataRange().getValues(); | |
var students = []; | |
for(var i = 1; i < studentValues.length; i++) { | |
students.push(studentValues[i].join(' ')); | |
} | |
return students; | |
} |
Great example. Thanks for sharing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Buenísimo.