我可以使用Google Apps脚本使Google表单显示来自Google表格的随机文本吗? [英] Can I use Google Apps Script to make a Google Form display randomized text from a Google Sheet?

查看:42
本文介绍了我可以使用Google Apps脚本使Google表单显示来自Google表格的随机文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在Google表格中列出了1000种动物(例如狗,猫,牛,...,长颈鹿).我希望Google表单在每次受访者打开表单时都会随机选择其中一种动物.

Say I have a list of 1000 animals in a Google Sheet (e.g., dog, cat, cow, ..., giraffe). I'll like the Google Form to randomly pick one of these animals every time a respondent opens the Form.

例如,您见过__________吗?

E.g., Have you ever seen a __________ ?

在这里,每个受访者的空白都会不同(除非他们很幸运能够随机获得匹配的动物).

Here, the blank would be different for every respondent (unless they were lucky enough to randomly get matching animals).

我目前拥有从Google表格中随机选择动物的代码,但是我无法弄清楚如何为每个响应者随机选择动物,因为onOpen()函数无法为每个响应者触发,只有在所有者打开表单.

I currently have the code to randomly select an animal from the Google Sheet, but I can't figure out how to randomly select an animal for each respondent, since the onOpen() function cannot trigger for every respondent, but only when the owner opens the Form.

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)
  }
}

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

Any advice on how to change my code or do a completely different approach to achieve the same results will be appreciated. Thanks!

推荐答案

使用

Instead of onOpen trigger, use the installable onFormSubmit trigger

This will allow you to update your form question after a respondent has submitted the form.

示例:

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 + "?");
  }
}

注意:

  • 由于仅在表单提交时会更新问题,因此将在先前的受访者完成提交之前打开表单的受访者不会看到该表单的其他版本.

  • Since the question will only be updated on form submit, the respondents that will open the form before the preceding respondent finishes submitting will not see a different version of the form.

但是,当前没有其他选项可以动态更改每个受访者的问题内容.

However, currently there is no other option to change the question contents dynamically for each respondent.

如果对您有所帮助-可以使用随机播放问题顺序和答案选项给其他受访者.

If it is helpful for your - there options to shuffle question order and answer options to different respondents.

这篇关于我可以使用Google Apps脚本使Google表单显示来自Google表格的随机文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆