Created
October 6, 2017 23:25
-
-
Save barrieroberts/729885bc15b6483f34d2bc668f2e2052 to your computer and use it in GitHub Desktop.
Create and update multiple questions in a Google Form from Google Sheet data
This file contains hidden or 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 createFormFromData() { | |
| var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); | |
| //Get data, number of questions and options info | |
| var data = sh.getDataRange().getValues(); | |
| var numOfOptions = data.length-3; | |
| var numOfQs = data[0].length; | |
| //Get questions | |
| var questions = sh.getRange(2, 2, 1, numOfQs).getValues(); | |
| //Get options and store in an array | |
| var allOptions = []; | |
| for (q=2;q<=numOfQs;q++){ | |
| var options = sh.getRange(3, q, numOfOptions, 1).getValues(); | |
| allOptions.push(options); | |
| } | |
| //Create the form | |
| var form = FormApp.create("New form"); | |
| //Add questions and options to form | |
| for (qq=0;qq<numOfQs-1;qq++){ | |
| var formQ = form.addMultipleChoiceItem(); | |
| formQ.setTitle(questions[0][qq]); | |
| formQ.setChoiceValues(allOptions[qq]); | |
| } | |
| } | |
| function updateFormFromData() { | |
| var sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2"); | |
| //Get data, number of questions and options info | |
| var data = sh.getDataRange().getValues(); | |
| var numOfOptions = data.length-3; | |
| var numOfQs = data[0].length; | |
| //Get questions | |
| var questions = sh.getRange(2, 2, 1, numOfQs).getValues(); | |
| //Get options and store in an array | |
| var allOptions = []; | |
| for (q=2;q<=numOfQs;q++){ | |
| var options = sh.getRange(3, q, numOfOptions, 1).getValues(); | |
| allOptions.push(options); | |
| } | |
| //Get existing form | |
| var form = FormApp.openById('1sqVgGMdXIXPMLBtsR6zucsfKNRMD8HLYB3S9kn7On5s'); | |
| var allItems = form.getItems(); | |
| //Add questions and options to form | |
| for (qq=0;qq<numOfQs-1;qq++){ | |
| var formQ = allItems[qq].asMultipleChoiceItem(); | |
| formQ.setTitle(questions[0][qq]); | |
| formQ.setChoiceValues(allOptions[qq]); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment