vjanomolee

Google Apps Script - Form2Dox

Aug 27th, 2020 (edited)
2,210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var form = FormApp.getActiveForm(); // Get the open form and make it a usable var for below
  2. var formTitle = form.getTitle();
  3. var document = DocumentApp.create(formTitle); // create a new Document called FormQuiz_conversion
  4. var question_position = 0; // variables for putting the questions and answers in the right position
  5. var answers_position = 0;
  6. var questions =  form.getItems();
  7. var body = document.getBody();
  8.  
  9. function convertActiveForm2Txt() // main function to run
  10. {
  11.   for(var j = 0; j < questions.length; j++) // this loop is to label the titles with list numbers
  12.   {
  13.     write2doc(questions[j],j);
  14.   }
  15.  
  16.   var bodyContents = body.getText();
  17.   DriveApp.createFile(formTitle+"--import2Respondus.txt", bodyContents ,MimeType.PLAIN_TEXT);
  18. }
  19.  
  20. function write2doc(quarshtun,j) // Iterate this function over all questions in the form
  21. {
  22.   if (quarshtun.getType() == FormApp.ItemType.MULTIPLE_CHOICE) // check if the question is multiple choice
  23.   {
  24.     var question = quarshtun.asMultipleChoiceItem(); // change the type from Item to MultipleChoiceItem
  25.     var choices = question.getChoices();
  26.     var title = question.getTitle();
  27.     var i = 0;
  28.    
  29.     body.appendParagraph(j+1+". "+title); // set the title of the question in the document
  30.    
  31.     for (i; i < choices.length; i++)
  32.     {
  33.       if(choices[i].isCorrectAnswer())
  34.       {
  35.         body.appendParagraph("\t"+"*"+String.fromCharCode(i+97)+". "+choices[i].getValue()); // if the choice is correct append it and * <--
  36.         answers_position++;
  37.       }
  38.       else
  39.       {
  40.         body.appendParagraph("\t"+String.fromCharCode(i+97)+". "+choices[i].getValue());  // if else  append  it<--
  41.         answers_position++;
  42.       }        
  43.     }
  44.     question_position += i;
  45.     answers_position++;
  46.   }
  47.  
  48.   question_position++;
  49.   body.appendParagraph("\n");  //  return break to the next question.
  50.  }
Add Comment
Please, Sign In to add comment