I have a Google form that stores reponses in a Google Sheet. When the form is submitted a email is generated using the stored values in the sheet. The body of the email will include all required fields from the form and only non-required fields that have been filled in.
I am trying to use a for loop to iterate over the array and use an if statement to only output fields that have data in them. I am having a problem where it seems the for loop is not being entered. I have added an else statement to check if the for loop is working but I am not getting any output.
I have added comments to my code to make it clear what the code is doing (or should be doing) in each section.
I can usually figure things out on my own by searching online but I have spent many hours searching online and cannot find a solution.
Thank you in advance for your assistance.
function main() {
// access the sheet with the form responses
var formResponses = getSheetById(1693762938);
// collect the data from the responses sheet
var formData = formResponses.getRange(1, 1, formResponses.getLastRow(), formResponses.getLastColumn()).getDisplayValues();
// collect questions
var formQuestions = formResponses.getRange(1, 2, 1, formResponses.getLastColumn()).getDisplayValue();
// collect the responses
var recentResponse = formResponses.getRange(formResponses.getLastRow(), 2, 1, formResponses.getLastColumn()).getDisplayValues();
// only output answers with values
recentResponse.filter(Boolean).forEach(function(value) {
var filterData = value.filter(String);
Logger.log(recentResponse);
Logger.log([filterData]);
// email receipient (Required Form Fields)
var businessName = Logger.log(recentResponse[0][0])
var receiptEmail = Logger.log(recentResponse[0][1])
var receipientName = Logger.log(recentResponse[0][2])
var receipientAddress = Logger.log(recentResponse[0][3])
var shippingMethod = Logger.log(recentResponse[0][4])
// example of outputting null index values
var orderedItem = Logger.log(recentResponse[0][5] + "," + "Qty =" + " " + (recentResponse[0][6]));
// example of outputting of index with values
var orderedItem = Logger.log(recentResponse[0][7] + "," + "Qty =" + " " + (recentResponse[0][8]));
// another example of outputing null index values
var orderedItem = Logger.log(recentResponse[0][9] + "," + "Qty =" + " " + (recentResponse[0][10]));
// trying to use a for loop to only output fields with data startng with index [0][5]
for (var i = 5; i < recentResponse[0][i]; i++){
if (recentResponse[0][i] !== 0){
for (var i = 5; [i] < recentResponse[0][i]; i++){
Logger.log(recentResponse[0][i] + "," + "Qty =" + " " + (recentResponse[0][i+1]));
}
} else {
Logger.log("no entry);
}
}
Here are the log output
9:50:47 PM Notice Execution started
9:50:56 PM Info [[Happy Days Cafe, [email protected], First Last, 123 Hillbilly Drive, Pick up, , , Apple Pie, 1, , , , , , , , , , , , , , , , , Half Baked, ]]
9:50:56 PM Info [[Happy Days Cafe, [email protected], First Last, 123 Hillbilly Drive, Pick up, Apple Pie, 1, Half Baked]]
9:50:56 PM Info Happy Days Cafe
9:50:56 PM Info [email protected]
9:50:56 PM Info First Last
9:50:56 PM Info 123 Hillbilly Drive
9:50:56 PM Info Pick up
9:50:56 PM Info ,Qty =
9:50:56 PM Info Apple Pie,Qty = 1
9:50:56 PM Info ,Qty =
9:50:49 PM Notice Execution completed
Joe Coyle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.