如何预填 Google 表单复选框? [英] How to prefill Google form checkboxes?
问题描述
我看过问题是否可以使用来自谷歌电子表格的数据预填"谷歌表单?"并且答案中提供的代码(感谢 Mogsdad)适用于文本类型的谷歌表单问题.我的问题是:是否可以预先填写复选框类型的 Google 表单问题?
I have looked at the question "Is it possible to 'prefill' a google form using data from a google spreadsheet?" and the code provided in the answer (thanks Mogsdad) works well for text type Google form questions. My question is: Is it possible to prefill a checkbox type Google form question?
例如,如果我有一个现有的电子表格,其中有一个姓名"条目,其中一个条目是Fred, Barney",是否可以通过编码来预填表单,并勾选Fred"的复选框"和Barney"在姓名"复选框下键入 Google 表单问题?
For example, if I have an existing spreadsheet with an entry for "Names" and one of the entries is "Fred, Barney" would it be possible, via coding, to have a form prefill with the checkboxes ticked for "Fred" and "Barney" under a "Names" checkbox type Google form question?
谢谢,格雷格
推荐答案
对于大多数其他类型,每个响应的基本模式都可以重复.例如,这适用于多项选择:
The basic pattern for each response can be repeated for most other types. For example, this works for multiple choice:
item = items[i].asMultipleChoiceItem();
var respItem = item.createResponse(resp);
但是,复选框可能很棘手,因为它可能包含一个项目、多个项目,甚至其他"项目.回应.当回复记录到您的电子表格时,它将显示为逗号分隔的字符串;当在表单提交事件中收到时(例如在触发器函数中),我们得到一个数组(...其中所有响应都在数组的第一项中,以逗号分隔的字符串).checkboxItem 的 createResponse()
方法需要一个有效选项的数组......所以我们可以用一些 JavaScript 魔法来提供它:
However, a checkbox can be tricky, as it may have one item, multiple items, and even "other" responses. When the response is recorded to your spreadsheet, it will appear as a comma-separated string; when received in a form submission event (e.g. in a trigger function), we get an array (... where all responses are in the first item in the array, in a comma-separated string). The createResponse()
method for a checkboxItem expects an array of valid choices... so we can provide that with a little javascript magic:
item = items[i].asCheckboxItem();
// Response is a CSV string, need array
var respArray = resp.split(/ *, */);
var respItem = item.createResponse(respArray);
编辑 Google 在 CheckboxItems 和 MultipleChoiceItems 与其他"一起使用时存在错误选项已启用.那些其他"选项是允许的,但在预填充的 URL 中被错误地呈现,因此它们不会出现在显示的表单中.请参阅问题 4454 并加星标.
这是 是否可以使用来自谷歌电子表格的数据预填"谷歌表单?,已更新以处理列表、多项选择和复选框响应.这个版本更通用,它可以适应电子表格中的标题.奖励:如果您添加一个标记为预填充 URL"的列,脚本将在那里写入其生成的 URL.
Here's an updated version of the function from Is it possible to 'prefill' a google form using data from a google spreadsheet?, updated to handle lists, multiple choice, and checkbox responses. This version is more general, it can adapt to the headings in your spreadsheet. BONUS: if you add a column labeled "Prefilled URL", the script will write its generated URLs there.
/**
* Use Form API to generate pre-filled form URLs
*
* https://stackoverflow.com/a/26395487/1677912
*/
function evenBetterBuildUrls() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Form Responses 1");
var data = ss.getDataRange().getValues(); // Data for pre-fill
var headers = data[0]; // Sheet headers == form titles (questions)
var formUrl = ss.getFormUrl(); // Use form attached to sheet
var form = FormApp.openByUrl(formUrl);
var items = form.getItems();
var urlCol = headers.indexOf("Prefilled URL"); // If there is a column labeled this way, we'll update it
// Skip headers, then build URLs for each row in Sheet1.
for (var row = 1; row < data.length; row++ ) {
Logger.log("Generating pre-filled URL from spreadsheet for row="+row);
// build a response from spreadsheet info.
var response = form.createResponse();
for (var i=0; i<items.length; i++) {
var ques = items[i].getTitle(); // Get text of question for item
var quesCol = headers.indexOf(ques); // Get col index that contains this question
var resp = ques ? data[row][quesCol] : "";
var type = items[i].getType().toString();
Logger.log("Question='"+ques+"', resp='"+resp+"' type:"+type);
// Need to treat every type of answer as its specific type.
switch (items[i].getType()) {
case FormApp.ItemType.TEXT:
var item = items[i].asTextItem();
break;
case FormApp.ItemType.PARAGRAPH_TEXT:
item = items[i].asParagraphTextItem();
break;
case FormApp.ItemType.LIST:
item = items[i].asListItem();
break;
case FormApp.ItemType.MULTIPLE_CHOICE:
item = items[i].asMultipleChoiceItem();
break;
case FormApp.ItemType.CHECKBOX:
item = items[i].asCheckboxItem();
// In a form submission event, resp is an array, containing CSV strings. Join into 1 string.
// In spreadsheet, just CSV string. Convert to array of separate choices, ready for createResponse().
if (typeof resp !== 'string')
resp = resp.join(','); // Convert array to CSV
resp = resp.split(/ *, */); // Convert CSV to array
break;
case FormApp.ItemType.DATE:
item = items[i].asDateItem();
resp = new Date( resp );
resp.setDate(resp.getDate()+1);
break;
case FormApp.ItemType.DATETIME:
item = items[i].asDateTimeItem();
resp = new Date( resp );
break;
default:
item = null; // Not handling DURATION, GRID, IMAGE, PAGE_BREAK, SCALE, SECTION_HEADER, TIME
break;
}
// Add this answer to our pre-filled URL
if (item) {
// Checking if there is any value
if(resp[0].length != 0){
var respItem = item.createResponse(resp);
response.withItemResponse(respItem);
}
}
// else if we have any other type of response, we'll skip it
else Logger.log("Skipping i="+i+", question="+ques+" type:"+type);
}
// Generate the pre-filled URL for this row
var editResponseUrl = response.toPrefilledUrl();
// If there is a "Prefilled URL" column, update it
if (urlCol >= 0) {
var urlRange = sheet.getRange(row+1,urlCol+1).setValue(editResponseUrl);
}
}
};
这篇关于如何预填 Google 表单复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!