Google-apps-script – Google Forms – App script won’t iterate through all form fields

google-apps-scriptgoogle-forms

I have made a Google Form and onSubmit an app script will run iterating through all form questions and answers however some questions, when the corresponding answer is left empty, are not listed/available in the iteration. Nevertheless, many questions whose answer is empty, are "listed" in the iteration.

function onSubmit(e) {
var items = e.response.getItemResponses();
var emailBody = '';
for (i in items){
// the follwoing will not iterate all questions
emailBody+= "<br/>"+ items[i].getItem().getTitle()+": "+items[i].getResponse();
}
......... other code follows .....
}

Best Answer

I think I got it! Items being left out from the list are checkboxes with no answer... from the Google documentation:

getItemResponses() Gets all item responses contained in a form response, in the same order that the items appear in the form. If the form response does not contain a response for a given TextItem, DateItem, TimeItem, or ParagraphTextItem, the ItemResponse returned for that item will have an empty string as the response. If the form response omits a response for any other item type, this method excludes that item from its returned array.