I have made a form to capture 2 data.
Each group has their own spreadsheet. [Accordi
Is there a way to retrieve data specifically from a particular textbox/options ..etc?
Yes, and it will simplify your task. The relevant documentation is Event Objects.
Your trigger function will be provided with an event object when it is triggered. In your case, a Forms form submission event, the event includes a FormResponse object, accessible by the response
attribute. No need to loop through responses, or open the form.
Since we can get the email and the group from a single response, relating the two becomes trivial. You'll see a helper function being used to get a handle on the appropriate sheet to add a subscription to.
As long as your questions are required, the array returned by getItemResponses()
will contain the item responses in the order they appear in the form.
/**
* 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;
}
Knowing that all response items were present did make things simple, but we can't always rely on that. If there is a chance that response items could be left blank, we will need to get to specific answers.
There are a few ways to do this. We'll look at two. First, using the item indexes:
/**
* 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
Using indexes freed us from the requirement that all answers be provided, but is still brittle; it would require maintenance effort if the form were modified, to ensure the indexes remain aligned.
An improvement we can use is to use the text of the questions to pick our responses. We'll still have to careful if questions are reworded - but this approach is resilient to changes in the order or questions or the addition of non-question items such as images, page-breaks, videos, or headers.
/**
* 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