Google表单确认电子邮件包含来自链接表的信息

时间:2016-12-14 18:02:26

标签: google-sheets google-api google-form confirmation-email

我设置了一个Google表单确认电子邮件触发器,我找到了here。同时,连接的答案表在每个提交的单独列(我的案例中为B列)中计算一个唯一ID - 通过我找到here的公式。

我想要达到的目的是在确认电子邮件中插入此唯一ID。问题是我不知道如何在Forms脚本中引用相应的Sheets字段。

我已经对int main() { #define BUF_SIZE 1024 char buffer[BUF_SIZE]; size_t contentSize = 1; /* Preallocate space. We could just allocate one char here, but that wouldn't be efficient. */ char *content = malloc(sizeof(char) * BUF_SIZE); if(content == NULL) { perror("Failed to allocate content"); exit(1); } content[0] = '\0'; // make null-terminated while(fgets(buffer, BUF_SIZE, stdin)) { char *old = content; contentSize += strlen(buffer); content = realloc(content, contentSize); if(content == NULL) { perror("Failed to reallocate content"); free(old); exit(2); } strcat(content, buffer); if (content[0]== '+') { printf("OK\n"); } else { printf("NOT OK\n"); } } if(ferror(stdin)) { free(content); perror("Error reading from stdin."); exit(3); } } 进行过实验,但我似乎无法使其发挥作用。

这是没有任何对Sheets的引用的脚本(这个函数完美无缺):

e.values[1]

这是我实现目标的尝试,但它不起作用:

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}); } 部分,一个在代码中,另一个在邮件中发送给收件人。

关于如何使这项工作的任何想法?

1 个答案:

答案 0 :(得分:0)

假设唯一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

修改

根据评论:

这是您原始代码的一部分,但我会转到资源&gt;当前项目在代码编辑器中触发并从那里开始。

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});
}