【问题标题】:Can I use Google Apps Script to make a Google Form display randomized text from a Google Sheet?我可以使用 Google Apps 脚本让 Google 表单显示来自 Google 表格的随机文本吗?
【发布时间】:2020-03-29 06:45:07
【问题描述】:

假设我在 Google 表格中有一个包含 1000 种动物的列表(例如,狗、猫、牛、...、长颈鹿)。我希望 Google 表单在每次受访者打开表单时随机选择其中一种动物。

例如,你见过__________?

在这里,每个受访者的空白都会有所不同(除非他们有幸随机获得匹配的动物)。

我目前有从 Google 表格中随机选择动物的代码,但我不知道如何为每个受访者随机选择动物,因为 onOpen() 函数不能为每个受访者触发,但只有在所有者打开表单。

function onOpen(e){
  var animals = worksheet.getRange(2, 1, worksheet.getLastRow()-1, 1)
              .getValues()
              .map(function(o){ return o[0]})
              .filter(function(o){return o !== ""});
  //Logger.log(animals)

  // get random animal
  var animal = animals[Math.floor(Math.random()*animals.length)];

  Logger.log(animal);
  var id = getBlockIdFromTitle()
  Logger.log(id) 

  if (id !== -1){
    updateLink(id, animal)
  }
}

任何关于如何更改我的代码或采用完全不同的方法来实现相同结果的建议都将不胜感激。谢谢!

【问题讨论】:

  • 请修复代码块

标签: google-apps-script google-sheets google-forms


【解决方案1】:

使用installable onFormSubmit 触发器而不是onOpen 触发器

这将允许您在受访者提交表单后更新您的表单问题。

示例:

function onFormSubmit(e){
  var animals = worksheet.getRange(2, 1, worksheet.getLastRow()-1, 1)
              .getValues()
              .map(function(o){ return o[0]})
              .filter(function(o){return o !== ""});
  //Logger.log(animals)

  // get random animal
  var animal = animals[Math.floor(Math.random()*animals.length)];
  FormApp.openById("XXX").getItems()[0].asTextItem().setTitle("Have you ever seen a " + animal + "?");
  }
}

头脑:

  • 由于问题只会在表单提交时更新,因此在前一个受访者完成提交之前打开表单的受访者将不会看到不同版本的表单。

  • 但是,目前没有其他选项可以为每个受访者动态更改问题内容。

  • 如果对您有帮助 - 可以选择shuffle 问题顺序和针对不同受访者的回答选项。

【讨论】:

    猜你喜欢
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 2015-12-30
    相关资源
    最近更新 更多