作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我制作了一个表格来捕获 2 个数据。
"johndoh@email.com" subscribed to "Group 1"
function onFormSubmit() {
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
// Checks if it is multiple choice option
if (itemResponse.getItem().getType() == FormApp.ItemType.MULTIPLE_CHOICE) {
// If user chooses group 1, open spreadsheet and store user's email in 1st column
if (itemResponse.getResponse() == "1") {
var ss = SpreadsheetApp.openById("id goes here");
var sheet = ss.getSheetByName("Group subscription email");
}
}
}
}
}
最佳答案
Is there a way to retrieve data specifically from a particular textbox/options ..etc?
response
访问属性。无需遍历回复或打开表单。
getItemResponses()
返回的数组将按照它们在表单中出现的顺序包含项目响应。
/**
* Trigger function for Google Forms "Form submit" event.
* Simple, assumes all answers are provided, no error checking.
*/
function onFormSubmit(e) {
var formResponse = e.response;
// If all questions are required, getItemResponses returns responses in form-order
var itemResponses = formResponse.getItemResponses();
var email = itemResponses[0].getResponse(); // returns a string
var group = itemResponses[1].getResponse();
var sheet = getGroupSheet( group ); // use helper function to get right sheet
if (sheet) {
sheet.appendRow([email]); // Add subscriber as a single-cell row
}
}
/**
* Gets the sheet for the indicated group.
* Helper function for onFormSubmit().
*
* @param {string} group The ID of the group, from user's response
*
* @returns {Sheet} Sheet object for the given group,
* null if group is unknown.
*/
function getGroupSheet( group ) {
var ssId = "id goes here";
switch (group) {
case "1":
var name = "Group subscription email";
break;
// case "2": // Add cases to handle other groups
// var name = "Other sheet name";
// break;
default: // If the group is unknown, log it
name = "";
Logger.log("Unexpected group ID ("+group+")");
break;
}
if (name) {
var result = SpreadsheetApp.openById(ssId).getSheetByName(name);
}
else {
result = null; // Return null for unknown groups
}
return result;
}
/**
* Trigger function for Google Forms "Form submit" event.
* Flexible - checks for specific response items by getting the item index
* for each response item. Example: if user answered questions 1 and 3, but not 2,
* e.response would contain itemResponses for items 0 and 2, but not 1.
*/
function onFormSubmit2(e) {
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
for (var i=0; i<itemResponses.length; i++) {
switch (itemResponses[i].getItem().getIndex()) {
case 0:
var email = itemResponses[i].getResponse(); // returns a string
break;
case 1:
var group = itemResponses[i].getResponse();
break;
}
}
var sheet = getGroupSheet( group ); // use helper function to get right sheet
if (sheet) {
sheet.appendRow([email]); // Add subscriber as a single-cell row
}
}
/**
* Trigger function for Google Forms "Form submit" event.
* More Flexible - checks for specific response items by getting the item title
* for each response item. No need to know the exact order of questions this way,
* as long as we know what the question was. (Ideal if the form is
* created programmatically.)
*/
function onFormSubmit3(e) {
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
for (var i=0; i<itemResponses.length; i++) {
switch (itemResponses[i].getItem().getTitle()) {
case "Email Address":
var email = itemResponses[i].getResponse(); // returns a string
break;
case "Group":
var group = itemResponses[i].getResponse();
break;
}
}
var sheet = getGroupSheet( group ); // use helper function to get right sheet
if (sheet) {
sheet.appendRow([email]); // Add subscriber as a single-cell row
}
}
关于google-apps-script - 如何从 Google 表单中提取响应项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26253706/
我是一名优秀的程序员,十分优秀!