Reading Form Input and Send Email
Thu Nov 03 2022 17:01:26 GMT+0000 (Coordinated Universal Time)
Saved by @Waydes #javascript
/** MAIN method * Runs whenever a form is submitted by user */ const onFormSubmit = (e) => { try { /** * Documention for most objects can be found starting at https://developers.google.com/apps-script/reference/forms/form-response * When you are in the Form editor, you create "Items" * When you submit a completed form, an object is passed to the script with a "response" property * The response property is of the type "FormResponse" * A FormResponse includes an array of responses to every answerable ITEM within the form for which the respondent provided an answer * getItemResponses() returns an array of type "ItemResponse" * An ItemResponse has an "Item" (getItem()) and a "Response" (getResponse()) * The Item must be cast as a type before use https://developers.google.com/apps-script/reference/forms/item * The Response is an array of responses to answerable items on the form. * In the case of a grid, a entry is in included in the response array only if user has provided an answer to at least one row * * For a "GridItem" (Item cast as a GridItem) * The item starts with a "Title" * The item has "Rows" (text) and "Columns" (text) * Row responses can't be accessed through the GridItem. You must retireve the row responses as an array through the ItemResponse object (which is the parent of the GridItem object) */ let formResponse = e.response; // formResponse : FormResponse let itemResponses = formResponse.getItemResponses(); // itemResponses : ItemResponse[] let currentUser = formResponse.getRespondentEmail(); let objItems = []; // objItems : array of objItem itemResponses.forEach(itemResponse => { // itemResponse : ItemResponse - A response to one answerable item within the form let item = itemResponse.getItem(); //item : Item let response = itemResponse.getResponse(); // response : String[] in case of GridItem questions (the answer at index n corresponds to the question at row n + 1 in the grid. If no answer, that answer is returned as '') // A Grid Item includes the "title" (first line of text) followed by "rows" (the text part of each row) if (item.getType() == "GRID") { let gridItem = item.asGridItem(); // gridItem : GridItem - Returns item as GridItem. Throws a scripting exception if the ItemType was not already GRID let rows = gridItem.getRows(); // rows : String[] - Gets the text part for every row in the grid. let title = gridItem.getTitle(); // title : String - Gets the text that is at the top of the item let index = gridItem.getIndex(); // index : int let columns = gridItem.getColumns(); // columns : String[] - Gets the possible column values let objItem = new Object(); objItem['title'] = title; objItem['index'] = index; objItem['rows'] = mergeRowsAndResponse(rows, response); objItems.push(objItem); } }); let values = objItems; // Construct html and text bodies. Send EMail //const [htmlBody, textBody] = constructAddContent(values); const [htmlBody, textBody] = constructEMailBody(HTML_TEMPLATE, TEXT_TEMPLATE, values); sendEmailText(textBody, currentUser, "subject", htmlBody); } catch (err) { Logger.log(err); MailApp.sendEmail('wayde_johnson@dpsnc.net', 'Error in onFormatSubmit', err); } }
Comments