更改主题变量时Google表格不会通过电子邮件发送

时间:2015-02-25 22:29:27

标签: forms email google-apps-script spreadsheet

我对Google脚本或脚本编写起来很陌生。我使用以下内容将用户输入的信息发送到一封电子邮件中,发送到特定的电子邮件地址。我想要做的是让第三行作为主题行(在这种情况下它是名称)。这是我在表单链接到的电子表格中的脚本:

function Initialize() {

  var triggers = ScriptApp.getProjectTriggers();

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

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

}

function SendGoogleForm(e) 
{  
  try 
  {      
    // You may replace this with another email address
    var email = "dyingkingdomsrecords@gmail.com";
    var subject = row[3];
    var s = SpreadsheetApp.getActiveSheet();
    var columns = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
    var message = "";    

    // Only include form fields that are not blank
    for ( var keys in columns ) {
      var key = columns[keys];
      if ( e.namedValues[key] && (e.namedValues[key] != "") ) {
        message += key + e.namedValues[key] + "\n\n"; 
      }
    }

    // This is the MailApp service of Google Apps Script
    // that sends the email. You can also use GmailApp for HTML Mail.

    MailApp.sendEmail(email, subject, message); 

  } catch (e) {
    Logger.log(e.toString());
  }

}

当我没有主题变量或输入字符串时,我收到的电子邮件没问题。我确定我错过了一些明显的东西。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

假设工作表中的列名是“Name”,您可以在MailApp.sendEmail方法之前添加此行。

var subject = e.namedValues["Name"].toString();
MailApp.sendEmail(email, subject, message);