繁体   English   中英

从谷歌电子表格创建谷歌表单/测验

[英]Creating a google form/quiz from a google spreadsheet

我正在尝试创建一个从谷歌电子表格中的数据创建的多项选择题表格。 我设法创建了 60 个问题的表格,每个问题有 4 个选项,并根据我在电子表格中的信息设置正确的选项。

我需要做的最后一件事是根据包含每个问题反馈的电子表格中的 G 列为每个问题插入正确的反馈。

编辑:这是我的电子表格和表单的图片

电子表格的图片

图片显示表单问题的外观

表格问题的图片(没有反馈)

问题是没有被实施,我最多可以为所有问题设置一个固定的反馈/单词,但无法将每个问题的特定反馈导入每个问题的反馈部分,有人可以帮忙吗,下面是我的代码:

function popForm() {
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('Sheet1');
  var numberRows = sheet.getDataRange().getNumRows();
  var myQuestions = sheet.getRange(1,1,numberRows,1).getValues();
  var myAnswers = sheet.getRange(1,2,numberRows,1).getValues();
  var myGuesses = sheet.getRange(1,2,numberRows,4).getValues();
  var myfeedback = sheet.getRange(1,7,numberRows,1).getValues();
  var myShuffled = myGuesses.map(shuffleEachRow);
  Logger.log(myShuffled);
  Logger.log(myAnswers);
  // Create the form as a quiz.  The resulting form's "Quiz options" are different from a manually created quiz.  Be aware (and change manually if needed!
  var form = FormApp.create('Fast Track Question - Domain I');
  form.setIsQuiz(true);
  // Write out each multiple choice question to the form.
  for(var i=0;i<numberRows;i++){
    if (myShuffled[i][0] == myAnswers[i][0]) {
      var addItem = form.addMultipleChoiceItem();
      addItem.setTitle(myQuestions[i][0])
      .setPoints(1)
      .setChoices([
        addItem.createChoice(myShuffled[i][0],true),
        addItem.createChoice(myShuffled[i][1]),
        addItem.createChoice(myShuffled[i][2]),
        addItem.createChoice(myShuffled[i][3])
      ]);
      var incorrectFeedback = FormApp.createFeedback()
      .setText(myfeedback[i][7])
      .build();
      addItem.setFeedbackForIncorrect(incorrectFeedback);
    }
    else if (myShuffled[i][1] == myAnswers[i][0]) {
      var addItem = form.addMultipleChoiceItem();
      addItem.setTitle(myQuestions[i][0])
      .setPoints(1)
      .setChoices([
        addItem.createChoice(myShuffled[i][0]),
        addItem.createChoice(myShuffled[i][1],true),
        addItem.createChoice(myShuffled[i][2]),
        addItem.createChoice(myShuffled[i][3])
      ]);
      var incorrectFeedback = FormApp.createFeedback()
      .setText(myfeedback[i][7])
      .build();
      addItem.setFeedbackForIncorrect(incorrectFeedback);
    }
    else if (myShuffled[i][2] == myAnswers[i][0]) {
      var addItem = form.addMultipleChoiceItem();
      addItem.setTitle(myQuestions[i][0])
      .setPoints(1)
      .setChoices([
        addItem.createChoice(myShuffled[i][0]),
        addItem.createChoice(myShuffled[i][1]),
        addItem.createChoice(myShuffled[i][2],true),
        addItem.createChoice(myShuffled[i][3])
      ]);
      var incorrectFeedback = FormApp.createFeedback()
      .setText(myfeedback[i][7])
      .build();
      addItem.setFeedbackForIncorrect(incorrectFeedback);
    }
    else if (myShuffled[i][3] == myAnswers[i][0]) {
      var addItem = form.addMultipleChoiceItem();
      addItem.setTitle(myQuestions[i][0])
      .setPoints(1)
      .setChoices([
        addItem.createChoice(myShuffled[i][0]),
        addItem.createChoice(myShuffled[i][1]),
        addItem.createChoice(myShuffled[i][2]),
        addItem.createChoice(myShuffled[i][3],true)
      ]);
      var incorrectFeedback = FormApp.createFeedback()
      .setText(myfeedback[i][7])
      .build();
      addItem.setFeedbackForIncorrect(incorrectFeedback);
    }
  }
}


// This function, called by popForm, shuffles the 5 choices.
function shuffleEachRow(array) {
  var i, j, temp;
  for (i = array.length - 1; i > 0; i--) {
    j = Math.floor(Math.random() * (i + 1));
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
  }
  return array;
}

建议更改脚本

您的代码很长,我发现使用一些额外的工具(例如getDataRangepushsplice以及forEach )重写它更容易。

您似乎以正确的方式调用这些方法,但由于您不得不在几个地方重复自己并跟踪许多 arrays 和索引,因此很可能出现了一个小错误。

这是改编自您的工作脚本:

function createQuiz() {
  let file = SpreadsheetApp.getActive();
  let sheet = file.getSheetByName("Sheet1");
  
  // Instead of getting individual ranges, it is more efficient
  // to get all the data in one go, and then operate on the two
  // dimensional array in memory.
  let range = sheet.getDataRange();
  let values = range.getValues();

  // Here I am using a existing form to test, but you can just
  // create a new one if you want.
  var form = FormApp.openById("[TESTING_ID]");
  
  
  form.setIsQuiz(true);

  values.shift(); // Using this to remove the first row of headers

  // Going through each line using a forEach to create a
  // multiple choice question
  values.forEach(q => {
    let choices = [q[1], q[2], q[3], q[4]];
    let title = q[0];
    let feedback = q[5]

    // Calling function to create multiple choice question
    createShuffledChoices(form, title, choices, feedback)
  });
}


function createShuffledChoices(form, title, choices, feedback){

  let item = form.addMultipleChoiceItem();

  item.setTitle(title)
  .setPoints(1)

  // Setting up the array that will be passed into item.setChoices()
  let shuffledChoices = [];
  // Making sure that the correct answer is only marked once
  let correctAnswerChosen = false;

  // I found I had to shuffle the questions within the process of
  // creating choices as it made it easier to maintain the spreadsheet
  for (let i = choices.length; i != 0; i--) {
    let rand = Math.floor(Math.random() * (i - 1));
    // If the first answer is chosen, it is the correct one.
    if (rand == 0 && correctAnswerChosen == false) {
      // Combination of push and splice to remove from ordered array
      // to the shuffled one
      shuffledChoices.push(item.createChoice(choices.splice(rand, 1)[0], true));
      // Marking the correct answer as chosen,
      // so that no others are marked correct.
      correctAnswerChosen = true;
    } else {
      shuffledChoices.push(item.createChoice(choices.splice(rand, 1)[0]));
    }  
  }
  
  // Finally setting the choices.
  item.setChoices(shuffledChoices);

  // Creating the feedback
  let formFeedback = FormApp.createFeedback().setText(feedback).build();
  item.setFeedbackForIncorrect(formFeedback);
}
  • 您创建反馈的方式是正确的,我怀疑您只是混淆了 arrays 和索引。 这就是我试图简化您的代码并消除重复部分的原因。

  • 我将洗牌过程与多项选择题的创建相结合。 这是因为传递给item.setChoices的洗牌数组必须由item.createChoice对象构建。 这不能在另一个 scope 中完成,因为item不可用。

  • 以这种方式组合洗牌的逻辑意味着您不需要在问题A)中有字母前缀。 您也不需要具有正确答案的列,因为该过程知道第一个答案是正确的。 因此,您的工作表可以简化为:

    在此处输入图像描述

  • 要使该脚本正常工作,需要以这种方式组织数据。 (当然你可以随心所欲地调整它)

参考

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM