How to create Google Form Quiz using Google Apps Script or GAS?

╄→гoц情女王★ 提交于 2020-04-17 20:41:50

问题


Is it possible to create a quiz form using GAS from a list of questions and answers on the spreadsheet? I'm making a word test for English learners, on which test-takers are asked to type an answer for each question. I've created a quiz with multiple questions or with radio buttons..., but I have been not able to import TEXT answers from spreadsheets to the TEXT-type quiz.

The script I have is below...

function myFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
 var range = ss.getDataRange(); 
 var data = range.getValues();
 var numberRows = range.getNumRows();
 var numberColumns = range.getNumColumns();
 var firstRow = 1;
 var form = FormApp.openById('');

 for(var i=0;i<numberRows;i++){
  var questionType = data[i][0]; 
  if (questionType==''){
     continue;
  }
  else if(questionType=='TEXT'){
   form.addTextItem()
     .setTitle(data[i][1]) 
     .setHelpText(data[i][2])
     .setRequired(true);
  } 

回答1:


I was ecstatic when Google finally let this happen with GAS. Check out this Google blog about creating a quiz using GAS. The scripting for the individual questions is shown below.

  // Make a 10 point question and set feedback on it
  var item = FormApp.getActiveForm().addCheckboxItem();
  item.setTitle("What flavors are in neapolitan ice cream?");
  item.setPoints(10);
  // chocolate, vanilla, and strawberry are the correct answers
  item.setChoices([
    item.createChoice("chocolate", true),
    item.createChoice("vanilla", true),
    item.createChoice("rum raisin", false),
    item.createChoice("strawberry", true),
    item.createChoice("mint", false)
  ]);



回答2:


  1. Get a look on documentation sample
  2. Once you understand how does it work, start by adding addTextItem()
  3. Adapt it to your sheet/needs

Example:

  • I have this list of questions on column A:

  • And now I want to convert these questions into this form:

  • Source code which you should adapt to your needs:
function convertToForm(){

  const ss = SpreadsheetApp.getActive().getActiveSheet();
  const questionList = ss.getRange("A1:A7").getValues();

  const form = FormApp.create('New Form');

  questionList.forEach( (question) => { 
    const item = form.addTextItem();
    item.setTitle(question[0])
  })

  // press Ctrl+Enter to see form urls
  Logger.log('Published URL: ' + form.getPublishedUrl());
  Logger.log('Editor URL: ' + form.getEditUrl());

}

Reference:

  • Forms
  • addTextItem()


来源:https://stackoverflow.com/questions/60646633/how-to-create-google-form-quiz-using-google-apps-script-or-gas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!