为每位受访者生成独特的表单URL [英] Generate unique Form URL for each respondent
问题描述
我拥有Google 表单使用Google电子表格存储回复。在我的电子表格中,我有4列:姓名,电子邮件,收入和第四列Id,用于识别特定收件人。
我在尝试要完成的是为每个响应者生成一个唯一的URL,以便他们可以响应表单和使用相同的URL在以后编辑表单。
我查看了 getEditUrl()
(Google Apps脚本)方法,该方法会在提交后为响应者创建唯一的网址
$ b
function myFunction(){
assignEditUrls();
function assignEditUrls(){
var form = FormApp.openById('1vsqvwomoqSXwF6TlNkmmktAAk2av2r-2LRrBYJdv3VQ');
//在此处输入表单ID
var sheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName('Form Responses');
//根据需要更改工作表名称
var data = sheet.getDataRange()。getValues();
var urlCol = 5; //填充URL的列号; A = 1,B = 2等
var responses = form.getResponses();
var timestamps = [],urls = [],resultUrls = [];
for(var i = 0; i< responses.length; i ++){
var resp = responses [i];
timestamps.push(responses [i] .getTimestamp()。setMilliseconds(0));
urls.push(shortenUrl(responses [i] .getEditResponseUrl())); (var j = 1; j< data.length; j ++){
var dop = data [j] [0]的
withItemResponse(responses [i])
} ]
resultUrls.push([data [j] [0]?urls [timestamps.indexOf(data [j] [0] .setMilliseconds(0))]:'']);
}
sheet.getRange(2,urlCol,resultUrls.length).setValues(resultUrls);
函数shortenUrl(longUrl){
// google url shortener api key
var key =AIzaSyBVG4Q5i1mNI0YAO0XVGZ3suZU8etTvK34;
var serviceUrl =https://www.googleapis.com/urlshortener/v1/url?key=+ key;
var options = {
muteHttpExceptions:true,
方法:post,
contentType:application / json,$ b $有效载荷:JSON。 stringify({'longUrl':longUrl})
};
var response = UrlFetchApp.fetch(serviceUrl,options);
if(response.getResponseCode()== 200){
var content = JSON.parse(response.getContentText()); ((content!= null)&&(content [id]!= null))
返回内容[id];
}
return longUrl;
}
然而,我想要这样做的另一种方式是首先生成独特的然后将URL发送给受访者,以便他们可以提交和编辑他们的回复,而无需向他们发送另一个URL(例如 editurlresponse
)。
这是可能的吗?
原始发布到https://webapps.stackexchange.com/a/86399/88163是的,这是可能的,但有一些不同的方法:
为每个受访者提供一个答案,以获得每个他们,然后发送相应的URL给每个响应者。
下面是一个代码片断,它通过编程提交一个响应,并通过两个代码记录一些响应属性,包括编辑响应url行,第一个有问题,第二个是解决方法。请注意这些行使用 getEditResponseUrl()
而不是 toPrefilledUrl()
。
$
$ b
/ *
这可以作为一个独立的或作为有界脚本使用。代码显示了如何获得以编程方式提交的对
的编辑响应url对Google表单
* /
的响应//以您自己的形式将表单ID替换
var formID ='1234567890abcdefghijklmnopqrstuvwxyz';
函数myFunction(){
var form = FormApp.openById(formID);
var response = form.createResponse();
var items = form.getItems();
var item = items [0];
if(item.getType()=='TEXT'){
var textItem = item.asTextItem();
var itemResponse = textItem.createResponse('my text');
response.withItemResponse(itemResponse);
}
//提交响应
var submittedResponse = response.submit();
//获取提交的响应属性
var values = {
ID:submittedResponse.getId(),
TS:submittedResponse.getTimestamp(),
/ *
Issue 4476:FormApp:getEditResponseUrl()产生无效的URL
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4476
* /
ER1:submittedResponse.getEditResponseUrl(),
/ *来自
的解决方法https://code.google.com/p/google-apps-script-issues/issues/detail?id= 4476#c2
* /
ER2:submittedResponse.getEditResponseUrl()。replace(
/\?edit2=.*/,\"?edit2=+ submittedResponse.getId()
)
};
Logger.log(values);
}
参考文献
I have a Google Form With a Google spreadsheet to store responses. In my spread sheet I have 4 columns: name, email, revenue, and a fourth column, Id, which is used to identify the particular recipient.
What I am trying to accomplish is to generate a unique URL for each respondent so that they can respond to the form and use the same URL to edit the form at a later time.
I've looked at the getEditUrl()
(Google Apps Script) method which creates a unique URL for the respondent after submitting the response-- code below:
function myFunction() {
assignEditUrls();
}
function assignEditUrls() {
var form = FormApp.openById('1vsqvwomoqSXwF6TlNkmmktAAk2av2r-2LRrBYJdv3VQ');
//enter form ID here
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses');
//Change the sheet name as appropriate
var data = sheet.getDataRange().getValues();
var urlCol = 5; // column number where URL's should be populated; A = 1, B = 2 etc
var responses = form.getResponses();
var timestamps = [], urls = [], resultUrls = [];
for (var i = 0; i < responses.length; i++) {
var resp = responses[i];
timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
urls.push(shortenUrl(responses[i].getEditResponseUrl()));
withItemResponse(responses[i])
}
for (var j = 1; j < data.length; j++) {
var dop = data[j][0]
resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]: '']);
}
sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);
}
function shortenUrl(longUrl) {
// google url shortener api key
var key = "AIzaSyBVG4Q5i1mNI0YAO0XVGZ3suZU8etTvK34";
var serviceUrl="https://www.googleapis.com/urlshortener/v1/url?key="+key;
var options={
muteHttpExceptions:true,
method:"post",
contentType: "application/json",
payload : JSON.stringify({'longUrl': longUrl })
};
var response=UrlFetchApp.fetch(serviceUrl, options);
if(response.getResponseCode() == 200) {
var content = JSON.parse(response.getContentText());
if ( (content != null) && (content["id"] != null) )
return content["id"];
}
return longUrl;
}
However I want to do it the other way which is to first generate the unique URL to be then sent to respondents so they can submit and edit their responses without the need of sending them another URL (e.g. the editurlresponse
).
Is this possible to do?
Originally posted to https://webapps.stackexchange.com/a/86399/88163
Yes, it's possible, but with slight different approach:
Submit one answer for each respondent in order to get one edit URL by each of them, then send the corresponding URL to each respondent.
Below is a code snippet that programmatically submits a response and log some response attributes including the edit response url by through two code lines, the first one has an issue, the second one is a workaround. Please note that these lines use getEditResponseUrl()
not the toPrefilledUrl()
.
It will work as a stand alone or as a bounded script.
/*
This code shows how to get the edit response url of a
programmatically submitted response to a Google Form
*/
// Replace the form ID by your own form
var formID = '1234567890abcdefghijklmnopqrstuvwxyz';
function myFunction() {
var form = FormApp.openById(formID);
var response = form.createResponse();
var items = form.getItems();
var item = items[0];
if (item.getType() == 'TEXT') {
var textItem = item.asTextItem();
var itemResponse = textItem.createResponse('my text');
response.withItemResponse(itemResponse);
}
// Submit response
var submittedResponse = response.submit();
// Get submitted response attributes
var values = {
ID : submittedResponse.getId(),
TS : submittedResponse.getTimestamp(),
/*
Issue 4476: FormApp: getEditResponseUrl() produces invalid URL
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4476
*/
ER1 : submittedResponse.getEditResponseUrl(),
/* Workaround from
https://code.google.com/p/google-apps-script-issues/issues/detail?id=4476#c2
*/
ER2 : submittedResponse.getEditResponseUrl().replace(
/\?edit2=.*/,"?edit2=" + submittedResponse.getId()
)
};
Logger.log(values);
}
References
- Class FormResponse - Google Apps Script Reference
- Issue 4476: FormApp: getEditResponseUrl() produces invalid URL
这篇关于为每位受访者生成独特的表单URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!