简体   繁体   English

我可以使用 Google Apps Script 使 Google 表单显示 Google 表格中的随机文本吗?

[英]Can I use Google Apps Script to make a Google Form display randomized text from a Google Sheet?

Say I have a list of 1000 animals in a Google Sheet (eg, dog, cat, cow, ..., giraffe).假设我在 Google Sheet 中有一个包含 1000 种动物的列表(例如,狗、猫、牛、...、长颈鹿)。 I'll like the Google Form to randomly pick one of these animals every time a respondent opens the Form.我希望每次受访者打开表单时,Google 表单都会随机选择其中一种动物。

Eg, Have you ever seen a __________ ?例如,你见过 __________ 吗?

Here, the blank would be different for every respondent (unless they were lucky enough to randomly get matching animals).在这里,每个受访者的空白都不同(除非他们足够幸运随机获得匹配的动物)。

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.我目前有从 Google Sheet 中随机选择动物的代码,但我无法弄清楚如何为每个受访者随机选择一种动物,因为 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)
  }
}

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使用可安装的onFormSubmit触发器代替onOpen触发器

This will allow you to update your form question after a respondent has submitted the form.这将允许您在受访者提交表单后更新您的表单问题。

Sample:样本:

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

Mind:头脑:

  • 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.如果它对您有帮助 - 可以选择对不同的受访者进行随机问题顺序和回答选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 用于在数据表中显示来自 Google 工作表的数据的通用 Google Apps 脚本 - General Google Apps script to display data from a Google sheet in a datatable Google Apps 脚本 - 来自 Google 表单/表格的神秘逗号值 - Google Apps Script - Mysterious comma on value from Google Form / Sheet 使用Google表格中的输入来换行(Google Apps脚本) - Wrap text using input from Google Sheet (Google Apps Script) 从Google Apps脚本查询Google表格 - Query a Google Sheet from Google Apps Script 使用 google apps 脚本从网址中找到的 pdf 文件中提取文本并将其插入到 Google 表格中 - Use google apps script to extract text from pdf file found at web address and and insert it into a Google Sheet Google Apps 脚本 - 想要截断表单中特定列中的文本 - Google Apps Script - Want to truncate a text in a particular column in a form sheet 使用 Google 脚本在 HTML 表单中按 Google Sheet Doc 中的字母顺序显示下拉菜单 - Use a Google Script to display a dropdown menu in a HTML form in alphabetical order from a Google Sheet Doc 如何通过谷歌应用程序脚本从 xlsx gmail 附件中提取谷歌表格中的信息 - How can I extract Information in a Google Sheet from a xlsx gmail attachment via google apps script Google Apps脚本->将文本从文档写入表格 - Google Apps Script -> write Text from Document to Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM