我正在设计一份调查问卷,要求受访者输入他们的姓名以及特定于其雇主的公司代码。我想有一个脚本,以便不同的公司将根据指定的公司代码获得电子邮件通知。我在表单中创建了一个Google表格,其中列出了公司代码,相应的电子邮件和消息。
我找到了一个执行此操作的脚本,但我必须根据自己的形式进行调整。任何人都可以这样做或有更好的代码吗?
演示表格链接:Here
链接到演示电子表格: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}
}
&#13;
答案 0 :(得分:1)
在行中使用的formcompanycode
if (settings[i][0] === formcompanycode){
未定义。
见How can I test a trigger function in GAS?。这里描述的技术将帮助您调试代码并发现其他错误。