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.
Simple approach - all responses required
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.
function onFormSubmit(e) {
var formResponse = e.response;
var itemResponses = formResponse.getItemResponses();
var email = itemResponses[0].getResponse();
var group = itemResponses[1].getResponse();
var sheet = getGroupSheet( group );
if (sheet) {
sheet.appendRow([email]);
}
}
function getGroupSheet( group ) {
var ssId = "id goes here";
switch (group) {
case "1":
var name = "Group subscription email";
break;
default:
name = "";
Logger.log("Unexpected group ID ("+group+")");
break;
}
if (name) {
var result = SpreadsheetApp.openById(ssId).getSheetByName(name);
}
else {
result = null;
}
return result;
}
Adaptable approach #1 - item indexes
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:
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();
break;
case 1:
var group = itemResponses[i].getResponse();
break;
}
}
var sheet = getGroupSheet( group );
if (sheet) {
sheet.appendRow([email]);
}
}
Adaptable approach #2 - item titles (question text)
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.
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();
break;
case "Group":
var group = itemResponses[i].getResponse();
break;
}
}
var sheet = getGroupSheet( group );
if (sheet) {
sheet.appendRow([email]);
}
}