【问题标题】:How to get the active value from a cell in a Google spreadsheet on form submission?如何在提交表单时从 Google 电子表格中的单元格获取活动值?
【发布时间】:2016-02-06 11:31:45
【问题描述】:

我制作了一个将数据保存在电子表格中的谷歌表单,我正在为此类电子表格编写代码,因此它会向我发送一封包含其中一些数据的电子邮件,但是我正在努力获取其中一个值。第 4 (D) 列从发件人那里获取电子邮件地址,因此我想恢复该值以将其作为“replyTo”变量并能够直接回复任何询问的人。

这是我目前的代码:

function Initialize() {

  try {

    var triggers = ScriptApp.getProjectTriggers();

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

    ScriptApp.newTrigger("EmailGoogleFormData")
      .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
      .onFormSubmit().create();

  } catch (error) {
    throw new Error("Please add this code in the Google Spreadsheet");
  }
}

function EmailGoogleFormData(e) {

  try {

    if (MailApp.getRemainingDailyQuota() > 0) {

      // You may replace this with another email address
      var email = "nico.pinera@filmmusiclive.com";

      /*********** Sender email address || I HAVE PROBLEMS HERE ***********/

      var replyTo, senderMail,ss,r_max;
      ss = SpreadsheetApp.getActiveSheet();
      //r_max = ss.getmaxRows();


      // Returns the active cell
      var range = ss.getActiveRange();
      senderMail = range.getValues()[3];


      // Enter your subject for Google Form email notifications
      var subject = "Parte de NNTT recibido"


      var key, entry,
        message = "<b>Hola pepito</b><br/>aa</br><br><br><br><br>",
        ss = SpreadsheetApp.getActiveSheet(),
        cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];

      // Iterate through the Form Fields
      for (var keys in cols) {

        key = cols[keys];
        entry = e.namedValues[key] ? e.namedValues[key].toString() : "";            

        // Only include form fields that are not blank
        if ((entry !== "Sí") && (entry !== "") && (entry !== ",") && (entry.replace(/,/g, "") !== "Sí"))
          message += '<img src="" /><br/><br/>' + ' <b><u> ' + key + '</u></b>' + ' => ' + entry ;           


      }

      MailApp.sendEmail(email, replyTo, subject, message);
    }
  } catch (error) {
    Logger.log(error.toString());
  }
}

有人知道如何从该单元格中获取活动值吗?这将是 D 列中的任何行。 此外,我确实通过找到 maxRows() 的 int 然后找到 maxRows() 的单元格和 D 列的值来做到这一点,但是如果我手动编辑电子表格(比如我在最后一行手动添加一个值),新的表单请求将存储在该请求之上并将其向下移动。 (如果我编辑第 12 行并收到新请求,我的行会下移到第 13 行,新的行会存储在第 12 行,依此类推)。

【问题讨论】:

    标签: javascript google-apps-script google-sheets google-forms


    【解决方案1】:

    简答

    如果电子邮件所需的数据包含在表单响应中,则不要使用电子表格有界脚本,而是使用表单有界脚本。

    说明

    Google 表单也可以有有界脚本。这些脚本可以在表单提交时触发,并且表单提交响应作为表单响应对象包含在事件对象中。请参阅 Google Form EventsForm Response

    好处

    • 将回复数据放在一个地方,一个表单文件,而不是两个,一个表单文件和一个电子表格。
    • 响应位于响应对象中。无需从单元格中调用值,这样可以节省几行代码并调用 Google Apps 脚本服务
    • 响应对象包含默认不添加到电子表格的编辑响应 URL。

    简要代码示例

    Les Bell 在Sending Confirmation Emails from Google Apps Forms 上发布的代码的简化版本,适用于添加作为回复参数响应提交的电子邮件地址

    function sendConfirmationEmail(e) {
      // e is a Form Event object 
    
      // 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>";
    
      // response is a FormResponse 
      var response = e.response;
    
      var textbody, sendTo, bcc, replyTo;
    
      // 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];
        // If this field is the email address, then use it to fill in 
        // the sendTo variable
        // Check that your form item is named "Email Address" or edit to match
        if (itemResponse.getItem().getTitle() == "Email Address") {
          sendTo = itemResponse.getResponse();
          replyTo = sendTo;
        }
      }
    
      GmailApp.sendEmail(sendTo, subject, message, {
                           bcc: bcc,  
                           name: sendername,  
                           htmlBody: message,  
                           replyTo: replyTo
                         });
    }
    

    完整代码

    以下代码取自 Les Bell 的 Sending Confirmation Emails from Google Apps Forms。移除了作为 cmets 包含的 URL,并添加了一些断线以避免出现水平滚动条。

    其目的是将提交的响应和编辑响应 URL 发送到表单提交中捕获的电子邮件地址。

    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 
    
      // 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 
      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 "Email Address" or edit 
        // to match
        if (itemResponse.getItem().getTitle() == "Email Address") {
          sendTo = itemResponse.getResponse();
        }
      }
    
      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,
                           {bcc: bcc, name: sendername, htmlBody: message});
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      相关资源
      最近更新 更多