作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经看了问题“Is it possible to 'prefill' a google form using data from a google spreadsheet?”,答案中提供的代码(感谢Mogsdad)非常适用于Google文本类型的文本问题。我的问题是:
是否可以预先填写复选框类型的Google表单问题?
例如,如果我有一个现有的电子表格,其中有一个“名称”条目,而其中一个条目是“Fred,Barney”,则可以通过编码预先填写一个带有“Fred”和““名称”复选框下的“Barney”类型Google表单问题?
谢谢,
格雷格
最佳答案
对于大多数其他类型,可以重复每个响应的基本模式。例如,这适用于多种选择:
item = items[i].asMultipleChoiceItem();
var respItem = item.createResponse(resp);
createResponse()
方法需要一个有效的选择数组...因此,我们可以为它提供一些JavaScript魔术:
item = items[i].asCheckboxItem();
// Response is a CSV string, need array
var respArray = resp.split(/ *, */);
var respItem = item.createResponse(respArray);
/**
* 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) {
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-apps-script - 如何预填Google表单复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26393556/
在使用网站、加载内容等一段时间后,此消息显示“Fill:SelectCommand.Connection 属性尚未初始化”!我认为这是因为 sql 连接,但不确定......我想知道我能做些什么来防止
我是一名优秀的程序员,十分优秀!