如何使用 google appscript 随机化 google 表单中的答案(洗牌选项顺序)?

How can I randomize the Answers(shuffle options order ) in the google forms using google appscript?

如何使用 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);

将所有选项存储在一个数组中并打乱数组。

  • 不是将每个选项都存储在不同的变量中,而是创建一个数组变量 (options) 和 push 该数组的所有可能选项。
  • 打乱结果数组(有很多方法可以做到这一点,但我使用了 this answer -credits to Laurens Holst- 中找到的方法)。
  • 数组随机化后,将这些选项中的每一个添加到您的项目中。

代码示例:

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() 而不是存储活动 sheet 在一个变量中(固定在代码示例中)以及一个一个地创建选择。我建议您尝试稍微修改一下以提高效率。

相关:

  • How to randomize (shuffle) a JavaScript array?