Created
November 10, 2016 12:18
-
-
Save rheajt/9fed84b216b56b2bc6f73b1f5e6f8aa9 to your computer and use it in GitHub Desktop.
Use the script editor with Google Forms
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 onOpen() { | |
| FormApp.getUi() | |
| .createAddonMenu() | |
| .addItem('Copy text into form', 'importText') | |
| .addToUi(); | |
| } | |
| function importText() { | |
| var commentModal = HtmlService | |
| .createHtmlOutputFromFile('Textbox') | |
| .setTitle('Import text') | |
| .setWidth(600) | |
| .setHeight(300); | |
| FormApp.getUi().showModalDialog(commentModal, "Import Text"); | |
| } | |
| /** | |
| * This function takes the text from the modal window and creates either multiple choice questions or short answer | |
| */ | |
| function cpCreator(text) { | |
| //declare the FormApp service | |
| var form = FormApp.getActiveForm(); | |
| //create an array for all the questions | |
| //create an array for each question depending on type | |
| var questions = []; | |
| var question = []; | |
| //use the text argument sent from the text box to determine if it is a short answer or multiple choice | |
| text.forEach(function(each) { | |
| //if the element in the array is not empty then add it to the question array | |
| if(each !== '') { | |
| question.push(each); | |
| } | |
| //if the element is empty then add the question array to the questions array and delete it to start over | |
| else { | |
| questions.push(question); | |
| question = []; | |
| } | |
| }); | |
| //this loop will create the form items depending on the length of each array inside the questions array | |
| questions.forEach(function(each) { | |
| if(each.length > 1) { | |
| var title = each.shift(); | |
| var mc = form.addMultipleChoiceItem() | |
| mc.setTitle(title); | |
| mc.setChoices(each.map(function(e) {return mc.createChoice(e)})); | |
| } else if(each.length === 1) { | |
| var t = form.addTextItem() | |
| .setTitle(each[0]); | |
| } | |
| }); | |
| } |
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
| <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css"> | |
| <div class="block form-group"> | |
| <textarea id="quiz-data" rows="15" style="width: 100%;"></textarea> | |
| <button type="button" class="create" id="submit-data">Create form</button> | |
| </div> | |
| <script> | |
| function submitData() { | |
| var formData = document.getElementById('quiz-data').value; | |
| formData = formData.split('\n'); | |
| formData.push(''); | |
| google.script.run | |
| .withSuccessHandler(function(response) { | |
| google.script.host.close(); | |
| }) | |
| .cpCreator(formData); | |
| } | |
| document.getElementById('submit-data').addEventListener('click', submitData); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment