Google表单发送带有编辑链接的确认电子邮件

时间:2019-05-13 17:53:40

标签: google-apps-script google-form

我已经开发出一种简单的表格来收集数据,并且我想使用AppScript来发送确认邮件和编辑链接给填写该邮件的用户(在同一组织内)。

因此,我找到了以下脚本代码,并且只有在提交表单后它才有效。如果我组织中的其他任何人都试图这样做,那么我会将消息发送到我的电子邮件地址,而不是用户电子邮件。

我使用了第一个功能来设置触发器,第二个是主功能。我尝试发布脚本,但仍然无法正常工作。

function setup() {

/* First, delete all previous triggers */
var triggers = ScriptApp.getProjectTriggers();

for (var i in triggers) {
  ScriptApp.deleteTrigger(triggers[i]);
}

/* Then add a trigger to send an email on form submit */
ScriptApp.newTrigger("sendConfirmationEmail")
.forForm(FormApp.getActiveForm())
.onFormSubmit()
.create();

}

function sendConfirmationEmail(e) {


// Edit this to set the subject line for the sent email
var subject = "Registration Successful";

// This will show up as the sender's name
var sendername = "Your Name Goes Here";

// This is the body of the registration confirmation message
var message = "Thank you for registering.<br>We will be in touch. 
<br><br>";
 message += "Your form responses were:<br><br>";

 // response is a FormResponse - see https://developers.google.com   
 /apps-script/reference/forms/form-response
 var response = e.response;

 var textbody, sendTo, bcc;

// Get the script owner's email address, in order to bcc: them
 bcc = Session.getActiveUser().getEmail();

 // Now loop around, getting the item responses and writing them into
the email message
var itemResponses = response.getItemResponses();
 for (var i = 0; i < itemResponses.length; i++) {
   var itemResponse = itemResponses[i];
     message += itemResponse.getItem().getTitle() +": " + 
     itemResponse.getResponse() + "<br>";
    sendTo = bbc
   }
  }

  message += "<br>If you wish to edit your response, please click on   
 <a href=\"" + response.getEditResponseUrl() + "\">this link</a>.";
  message += "<br><br>";
   textbody = message.replace("<br>", "\n");

  GmailApp.sendEmail(sendTo, subject, textbody,
                   {name: sendername, htmlBody: message});
}

0 个答案:

没有答案