[英]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!
谢谢!
onOpen
trigger, use the installable onFormSubmit
triggeronFormSubmit
触发器代替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.