[英]Google Forms Confirmation Email with Information From Linked Sheet
我已經設置了一個在這里找到的Google表單確認電子郵件觸發器。 同時,連接的答題紙通過我在此處找到的公式為每個提交在單獨的列(在我的情況下為B列)中計算一個唯一ID。
我要實現的是將此唯一ID插入確認電子郵件中。 問題是我不知道如何在Forms腳本中引用適當的Sheets字段。
我已經使用e.values[1]
進行了實驗,但是我似乎無法使其工作。
這是沒有任何參考Sheets的腳本(此功能完美無缺):
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) {
// e is a Form Event object - see https://developers.google.com/apps-script/guides/triggers/events#google_forms_events
// Edit this to set the subject line for the sent email
var subject = "Data Entry Successful";
// This will show up as the sender's name
var sendername = "John Smith";
// This is the body of the registration confirmation message
var message = "Thank you for submitting the details of your project!<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>";
// If this field is the email address, then use it to fill in the sendTo variable
// Check that your form item is named "Please enter your email address" or edit to match
if (itemResponse.getItem().getTitle() == "Please enter your email address") {
sendTo = itemResponse.getResponse();
}
}
message += "<br><a href=\"" + response.getEditResponseUrl() + "\">Please click here</a> if you wish to edit your data or include additional details at a later date.<br>It is essential that you submit any editing through this provided link, since your response data is exclusive only to you. Please do not share your unique edit link with others.<br>If the link doesn't work properly, please copy the following link address manually and then paste it directly into your browser's URL bar:<br>" + response.getEditResponseUrl() + "<br><br><br>Sincerely,<br>John Smith";
message += "<br><br>";
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sendTo, subject, textbody,
{bcc: bcc, name: sendername, htmlBody: message});
}
這是我實現目標的嘗試,但是沒有用:
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) {
// e is a Form Event object - see https://developers.google.com/apps-script/guides/triggers/events#google_forms_events
// Edit this to set the subject line for the sent email
var subject = "Data Entry Successful";
// This will show up as the sender's name
var sendername = "John Smith";
// This is the body of the registration confirmation message
var message = "Thank you for submitting the details of your project!<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();
// Get the sheet-generated ID of the submission
var activitID = e.values[1]; //ID number from column B
// 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>";
// If this field is the email address, then use it to fill in the sendTo variable
// Check that your form item is named "Please enter your email address" or edit to match
if (itemResponse.getItem().getTitle() == "Please enter your email address") {
sendTo = itemResponse.getResponse();
}
}
message += "The ID of the submitted activity is: " + activitID + "<br><br><a href=\"" + response.getEditResponseUrl() + "\">Please click here</a> if you wish to edit your data or include additional details at a later date.<br>It is essential that you submit any editing through this provided link, since your response data is exclusive only to you. Please do not share your unique edit link with others.<br>If the link doesn't work properly, please copy the following link address manually and then paste it directly into your browser's URL bar:<br>" + response.getEditResponseUrl() + "<br><br><br>Sincerely,<br>John Smith";
message += "<br><br>";
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sendTo, subject, textbody,
{bcc: bcc, name: sendername, htmlBody: message});
}
我添加了兩個activitID
部分,一個在代碼中,另一個在要發送給收件人的消息中。
關於如何進行這項工作的任何想法?
假設唯一ID與響應位於同一行,則可以嘗試替換:
// Get the sheet-generated ID of the submission
var activitID = e.values[1]; //ID number from column B
有:
// Get the sheet-generated ID of the submission
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1'); //rename to your sheet name
var row = e.range.getRow();
var activitID = sheet.getRange("B" + row).getValue(); //ID number from column B
編輯
根據評論:
這是您原始代碼的一部分,但是我只是去代碼編輯器中的資源>當前項目觸發器,然后從那里開始。
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 */
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("sendConfirmationEmail")
.forSpreadsheet(sheet)
.onFormSubmit()
.create();
}
只需使用這一部分:
function sendConfirmationEmail(e) {
var form = FormApp.openById("1lBkYf3eRnDzeXJnvawkxvWb5WGYgK14HzApwDmDyWSY");
var formResponses = form.getResponses();
//var response = form.getResponses();
// e is a Form Event object - see https://developers.google.com/apps-script/guides/triggers/events#google_forms_events
// Edit this to set the subject line for the sent email
var subject = "Data Entry Successful";
// This will show up as the sender's name
var sendername = "John Smith";
// This is the body of the registration confirmation message
var message = "Thank you for submitting the details of your project!<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 textbody, sendTo, bcc;
// Get the script owner's email address, in order to bcc: them
bcc = Session.getActiveUser().getEmail();
// Get the sheet-generated ID of the submission
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1'); //rename to your sheet name
var row = e.range.getRow();
var activitID = sheet.getRange("B" + row).getValue(); //ID number from column B
// Now loop around, getting the item responses and writing them into the email message
var r = formResponses.length-1;
var editURL = formResponses[r].getEditResponseUrl();
var formResponse = formResponses[r];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
message += itemResponse.getItem().getTitle() +": " + itemResponse.getResponse() + "<br>";
// If this field is the email address, then use it to fill in the sendTo variable
// Check that your form item is named "Please enter your email address" or edit to match
if (itemResponse.getItem().getTitle() == "Enter in Your email to receive a confirmation.") {
sendTo = itemResponse.getResponse();
}
}
message += "The ID of the submitted activity is: " + activitID + "<br><br><a href=\"" + editURL + "\">Please click here </a> if you wish to edit your data or include additional details at a later date.<br>It is essential that you submit any editing through this provided link, since your response data is exclusive only to you. Please do not share your unique edit link with others.<br>If the link doesn't work properly, please copy the following link address manually and then paste it directly into your browser's URL bar:<br>" + editURL + "<br><br><br>Sincerely,<br>John Smith";
message += "<br><br>";
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sendTo, subject, textbody,
{bcc: bcc, name: sendername, htmlBody: message});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.