Reputation: 1
I am designing a questionnaire that requires respondents to input their name and a company code that is specific to their employer. I would like to have a script so that a different company will get an email notification depending on which company code is specified. I created a Google Sheet in the Form that lists company codes, corresponding emails, and messages.
I have found a script that does this but I have to tailor it to my own form. Can anyone do this or has a better code?
Link to Demo Form: Here
Link to demo spreadsheet: Here
//using the obtained settings to set up email info
function onOpen(e) {
FormApp.getUi().createMenu('Authorizer').addItem('Authorize', 'authorize').addToUi();
}
//Easily authorize script to run from Form Editor interface
function authorize(){
var respEmail = Session.getActiveUser().getEmail();
MailApp.sendEmail(respEmail,"Form Authorizer", "Your form has now been authorized to send you emails");
}
function setSettings(e){
var response = e.response;
var settings = getSettings();
var emailSubject = settings[1][2];
var emailMsg = settings[1][3];
var formCompany = getResponse(response,"Company Code");
var formName = getResponse(response,"Name");
var emailAddr;
var companycode;
//loop through and grab the selected school from the Settings spreadsheet.
for (i in settings){
if (settings[i][0] === formcompanycode){
school = settings[i][0];
emailAddr = settings[i][1];
}
}
//build email and send to email send function
emailMsg = emailMsg + '\n' + 'Company Code: ' + formSchool + '\n' + 'Name: ' + formName;
mailSendSimple(emailAddr,emailSubject,emailMsg);
}
//function to find the selected company code.
function getResponse(response,formItem){
var selected;
//loop through the form response to find the company code
for (var i = 0; i < response.getItemResponses().length; i++){
if (formItem == response.getItemResponses()[i].getItem().getTitle()){
selected = response.getItemResponses()[i].getResponse();
}
}
return selected;
}
//grabbing general settings from spreadsheet sheet Settings
function getSettings() {
var form = FormApp.getActiveForm();
var ssID = form.getDestinationId();
var ss = SpreadsheetApp.openById(ssID);
var sheet = ss.getSheetByName("Settings");
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(1,1,lastRow,lastColumn);
var values = range.getValues();
return values;
}
//function to send out mail
function mailSendSimple(emailAddr,emailSub, emailMsg){
MailApp.sendEmail(emailAddr,emailSub, emailMsg); //{cc: ccUser}
}
Upvotes: 0
Views: 2478
Reputation: 38140
In formcompanycode
used in the line
if (settings[i][0] === formcompanycode){
is not defined.
See How can I test a trigger function in GAS?. The technique described there will help you to debug your code and find other errors.
Upvotes: 1