我有一个需要得到同等的人群在各三次调查中。三次调查是除了一个变化相同 - 它包含了不同的图片。
我想一个网址发给我的受访者。
我想算以前的回复我的号,添加一个。我想会话重定向到基于计算三者之一(谷歌表单)的URL
(Responses.Count + 1)MOD 3。
我想我需要一个谷歌Apps的脚本来做到这一点?
下面是一些伪代码:
var form0 = FormApp.openByUrl(
'htttps://docs.google.com/forms/d/e/2342f23f1mg/viewform'
);
var form1 = FormApp.openByUrl(
'htttps://docs.google.com/forms/d/e/23422333g/viewform'
);
var form2 = FormApp.openByUrl(
'htttps://docs.google.com/forms/d/e/2342wfeijqeovig/viewform'
);
var form0Responses = form0.getResponses();
var form1Responses = form1.getResponses();
var form2Responses = form2.getResponses();
var whichURL = (
form0Responses.length +
form1Responses.length +
form2Responses.length + 1
) % 3; // modulo three
// var goToForm = switch ( whichURL ) blah blah;
// redirect to goToForm;
// How do I redirect now?
谢谢!
也许有一个简单的解决方案可能的,但我不认为我知道它:)
你给了常见的链接可能是一个“代理”页面不包含任何东西,但只是将用户重定向到正确的页面的链接。或者,它可能是一个链接到实际的页面嵌入的必要形式。让我们来看看该选项。
0)发布您的代码的Web应用程序
在这两种情况下,你需要让你的代码发布为Web应用程序。气,这更简单比它的声音,你会发现这里的所有信息:https://developers.google.com/apps-script/guides/web
请确保您设置Anyone, even anonymous
可以访问应用程序,它始终为您运行(如果选择User accessing the app
,他们就必须要经过身份验证的过程,是不是你在这里需要什么)。
1)通过气相web应用程序重定向。
你在这种情况下,代码看起来就像这样:
// I changed your function a bit so that it could be easier to work with
// in case the number of your forms changes later on
function getForm() {
var forms = [
{
// I'm using openById instead of openByUrl 'cause I've run into issues with
// the latter
form: FormApp.openById('1')
},
{
form: FormApp.openById('2')
},
{
form: FormApp.openById('3')
}
];
var whichURL = 0;
for (var i in forms) {
forms[i].responses = forms[i].form.getResponses().length;
whichURL += forms[i].responses;
}
whichURL++;
// we're returning the actual URL to which we should redirect the visitors
return forms[whichURL % forms.length].form.getPublishedUrl();
}
// doGet is Google's reserved name for functions that
// take care of http get requests to your web app
function doGet() {
// we're creating an html template from which getForm function is called
// as the template is evaluated, the returned result
// of the function is inserted into it
// window.open function is a client-side function
// that will open the URL passed to it as attribute
return HtmlService.createTemplate('<script>window.open("<?= getForm() ?>", "_top");</script>').evaluate();
}
所以,你已经发布应用程式后,你会得到的链接打开该doGet
功能会运行 - 而且你会被重定向到表单中。
这里的事情是,你得到这样的URL不是挺美,某事像https://script.google.com/macros/s/1234567890abcdefghijklmnopqrstuvwxyz/exec,它也将那些1-2时显示在页面的“该应用程序不是由谷歌开发的”顶部的消息重定向发生之前秒。
2)嵌入您的形式进入另一个网页
这里的想法是不同的:不是给你的用户的“代理”的链接,你会为他们提供一个网页,会问谷歌的脚本,针对一正确的形式链接,将显示形式在页面的iframe中。
因此,有几个步骤:
2.1)更改doGet
功能(getForm
将保持不变):
function doGet() {
return ContentService.createTextOutput(getForm());
}
在这种情况下doGet
不会返回一个HTML浏览器通过渲染,但只是一个链接到您的表单。
旁注:更改代码后,您需要发布您的代码的新版本,以使更改生效。
2.2)在sites.google.com创建一个谷歌网站
2.3)插入一个“嵌入”块到您的网页,用下面的代码:
<script>
function reqListener(response) {
// change height and width as needed
document.body.insertAdjacentHTML('beforeend', '<iframe src="' + response.target.response + '" width="400" height="400"></iframe>');
}
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "INSERT-YOUR-SCRIPT-URL");
oReq.send();
</script>
它的作用:javascript代码发送HTTP请求到脚本,获取表单URL并将其传递到回调函数,reqListener
这反过来将其插入到文档主体的iframe
元素中。
好消息是,你的URL,这将是更加用户友好(你可以用你自己的网站这种做法,太)。