gpt4 book ai didi

google-apps-script - 如何预填Google表单复选框?

转载 作者:行者123 更新时间:2023-12-04 10:07:17 46 4
gpt4 key购买 nike

我已经看了问题“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);

但是,复选框可能很棘手,因为它可能有一个项目,多个项目,甚至还有“其他”响应。当响应记录到您的电子表格时,它将显示为逗号分隔的字符串;当在表单提交事件中(例如在触发函数中)收到时,我们得到一个数组(...,其中所有响应都在该数组的第一项中,以逗号分隔的字符串)。 checkboxItem的 createResponse()方法需要一个有效的选择数组...因此,我们可以为它提供一些JavaScript魔术:

            item = items[i].asCheckboxItem();
// Response is a CSV string, need array
var respArray = resp.split(/ *, */);
var respItem = item.createResponse(respArray);

编辑:如果启用了“其他”选项,则Google会出现CheckboxItems和MultipleChoiceItems的错误。这些“其他”选项是允许的,但是在预填充的URL中显示不正确,因此它们不会出现在显示的表单中。请参阅并为 Issue 4454加上星号。

这是 Is it possible to 'prefill' a google form using data from a google spreadsheet?函数的更新版本,已更新为处理列表,多项选择和复选框响应。这个版本比较笼统,可以适应电子表格中的标题。 奖励:如果添加标签为“Prefilled URL”的列,脚本将在其中写入其生成的URL。

/**
* 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/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com