如何使用google应用脚本将google表单中的答案(洗牌选项顺序)随机化?
有一些方法可用来设置点数,在表单中设置必需选项,但是否有任何方法可用于洗牌答案。
我现在的代码示例:-
var quest = SpreadsheetApp.getActiveSheet().getRange(1,4).getValue();
var ans1 = SpreadsheetApp.getActiveSheet().getRange(1,4).getValue();
var ans2 = SpreadsheetApp.getActiveSheet().getRange(1,5).getValue();
var ans3 = SpreadsheetApp.getActiveSheet().getRange(1,6).getValue();
var ans4 = SpreadsheetApp.getActiveSheet().getRange(1,9).getValue();
var item = form.addMultipleChoiceItem();
item.setChoices([
item.createChoice(ans1, false),
item.createChoice(ans2, false),
item.createChoice(ans3, false),
item.createChoice(ans4, true),
]);
item.setTitle(quest)
item.setRequired(true);
item.setPoints(1);编辑:已实现的解决方案
对于上述问题,我已按以下逻辑加以实施,并取得了成效。我希望它也能帮助到其他人。下面是代码片段
var quest = SpreadsheetApp.getActiveSheet().getRange(2,3).getValue();
var ans1 = SpreadsheetApp.getActiveSheet().getRange(2,4).getValue();
var ans2 = SpreadsheetApp.getActiveSheet().getRange(2,5).getValue();
var ans3 = SpreadsheetApp.getActiveSheet().getRange(2,6).getValue();
var ans4 = SpreadsheetApp.getActiveSheet().getRange(2,7).getValue();
var correctans = SpreadsheetApp.getActiveSheet().getRange(2,8).getValue();
var item = form.addMultipleChoiceItem();
item.setTitle(quest);
if (ans1 == correctans){
item.setChoices([item.createChoice(ans1, true),item.createChoice(ans2,false),item.createChoice(ans3,false),item.createChoice(ans4,false)]);};
if (ans2 == correctans){
item.setChoices([item.createChoice(ans1, false),item.createChoice(ans2,true),item.createChoice(ans3,false),item.createChoice(ans4,false)]);};
if (ans3 == correctans){
item.setChoices([item.createChoice(ans1, false),item.createChoice(ans2,false),item.createChoice(ans3,true),item.createChoice(ans4,false)]);};
if (ans4 == correctans){
item.setChoices([item.createChoice(ans1, false),item.createChoice(ans2,false),item.createChoice(ans3,false),item.createChoice(ans4,true)]);};
item.setPoints(1);
item.setRequired(true);发布于 2020-09-16 12:37:29
将所有选项存储在数组中,并对数组进行洗牌。
options)并对该数组的所有可能选项进行推。代码示例:
function addRandomizedOptions() {
var sheet = SpreadsheetApp.getActiveSheet();
var quest = sheet.getRange(1,4).getValue();
var options = []
options.push(sheet.getRange(1,4).getValue());
options.push(sheet.getRange(1,5).getValue());
options.push(sheet.getRange(1,6).getValue());
options.push(sheet.getRange(1,9).getValue());
shuffleArray(options);
var item = form.addMultipleChoiceItem();
item.setChoices([
item.createChoice(options[0], false),
item.createChoice(options[1], false),
item.createChoice(options[2], false),
item.createChoice(options[3], true),
]);
item.setTitle(quest)
item.setRequired(true);
item.setPoints(1);
}
/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}注意:
getValue()而不是使用getValues()一次,多次使用SpreadsheetApp.getActiveSheet()而不是将活动表存储在变量中(在代码示例中固定不变),以及一个接一个地创建选项。为了提高效率,我建议你试着重新设计一下。相关信息:
https://stackoverflow.com/questions/63919184
复制相似问题