我正在使用Google Apps脚本从工作表生成Google表单。问题在行中,问题选择在列中。
如果需要,这里是link to the Google sheet。
时,这是一项简单的任务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
为空,因此无法正常工作。
最好的方法是什么?
答案 0 :(得分: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 === ...
...
}
参考