如何在Google Apps脚本中使用带有.createChoice的for循环来创建工作表中的测验?

问题描述 投票:0回答:1

我正在使用Google Apps脚本从工作表生成Google表单。问题在行中,问题选择在列中。

enter image description here如果需要,这是一个link to the Google sheet

使用.setChoiceValues(values)时,这是一个简单的任务

if (questionType == 'CHOICE') {
  var choicesForQuestion = [];
  for (var j = 4; j < numberColumns; j++)
    if (data[i][j] != "")
      choicesForQuestion.push(data[i][j]);

  form.addMultipleChoiceItem()
    .setChoiceValues(choicesForQuestion);
}

但是,当我尝试使用.createChoice(value, isCorrect)时,参数调用value为字符串,isCorrect为布尔值。

没有循环的示例如下所示:

  var item = FormApp.getActiveForm().addCheckboxItem();
  item.setTitle(data[3][1]);
  // Set options and 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)
  ]);

我无法弄清楚如何添加循环。阅读其他帖子后,我尝试了以下内容:

if (questionType == 'CHOICE') {
  var questionInfo = [];
  for (var j = optionsCol; j < maxOptions + 1; j++)
    if (data[i][j] != "")
      questionInfo.push( form.createChoice(data[i][j], data[i][j + maxOptions]) );

    form.addMultipleChoiceItem()
      .setChoices(questionInfo);
  }

optionsCol是第一列问题选项maxOptions是表格允许的选项数量(目前为5)。 isCorrect信息是右侧的5列。

但是,这不起作用,因为数组questionsInfo是空的。

做这个的最好方式是什么?

google-apps-script google-sheets google-form google-form-quiz
1个回答
0
投票

可能你的问题与你引用的方法有关 - Form#createChoice - 不存在。你需要先调用MultipleChoiceItem#createChoice,首先创建项目:

/**
 * @param {Form} formObj the Google Form Quiz being created
 * @param {any[]} data a 1-D array of data for configuring a multiple-choice quiz question
 * @param {number} index The index into `data` that specifies the first choice
 * @param {number} numChoices The maximum possible number of choices for the new item
 */ 
function addMCItemToForm_(formObj, data, index, numChoices) {
  if (!formObj || !data || !Array.isArray(data)
      || Array.isArray(data[0]) || data.length < (index + 2 * numChoices))
  {
    console.error({message: "Bad args given", hasForm: !!formObj, info: data,
        optionIndex: index, numChoices: numChoices});
    throw new Error("Bad arguments given to `addMCItemToForm_` (view on StackDriver)");
  }
  const title = data[1];

  // Shallow-copy the desired half-open interval [index, index + numChoices).
  const choices = data.slice(index, index + numChoices);
  // Shallow-copy the associated true/false data.
  const correctness = data.slice(index + numChoices, index + 2 * numChoices);
  const hasAtLeastOneChoice = choices.some(function (c, i) {
    return (c && typeof correctness[i] === 'boolean');
  });
  if (hasAtLeastOneChoice) {
    const mc = formObj.addMultipleChoiceItem().setTitle(title);

    // Remove empty/unspecified choices.
    while (choices[choices.length - 1] === "") {
      choices.pop();
    }
    // Convert to choices for this specific MultipleChoiceItem.
    mc.setChoices(choices.map(function (choice, i) {
      return mc.createChoice(choice, correctness[i]);
    });
  } else {
    console.warn({message: "Skipped bad mc-item inputs", config: data, 
        choices: choices, correctness: correctness});
  }
}

您可以使用JSDoc所描述的上述函数 - 将其传递给Google Form对象实例以创建测验项,问题的详细信息数组以及详细信息数组中选择信息的位置描述。例如:

function foo() {
  const form = FormApp.openById("some id");
  const data = SpreadsheetApp.getActive().getSheetByName("Form Initializer")
    .getSheetValues(/*row*/, /*col*/, /*numRows*/, /*numCols*/);

  data.forEach(function (row) {
    var qType = row[0];
    ...
    if (qType === "CHOICE") {
      addMCItemToForm_(form, row, optionColumn, numOptions);
    } else if (qType === ...
    ...
}

参考

© www.soinside.com 2019 - 2024. All rights reserved.