Google Apps脚本| Switch Case仅选择默认选项

时间:2014-06-12 02:59:14

标签: javascript email google-apps-script case

我有一个很好的表单,用户提交数据。为了学习,我只有一个表单字段,这是一个带有2个选项的单选按钮。生成的电子表格只有2个字符串,"时间戳"和#34;名字。"

我的脚本的目标是评估表单提交中的事件驱动数据,然后更改变量中的值,以便将电子邮件发送给不同的用户。案例永远不会评估,结果始终是默认的。这是我的脚本,其中有来自其他来源:Digital Inspiration

  

function Initialize(){       var triggers = ScriptApp.getScriptTriggers();        for(var i in triggers){       ScriptApp.deleteTrigger(触发器[I]); }        ScriptApp.newTrigger(" SendGoogleForm")。forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onFormSubmit()。create();函数SendGoogleForm(e){
  试试{

var subject = "Form Test";  

var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];    
var message = "";    

// Credit to Henrique Abreu for fixing the sort order

for(var i in headers) {
  message += headers[i] + ' :: '+ e.namedValues[headers[i]].toString() + "\n\n"; 
}

message += headers[1] + "\n";
message += e.namedValues[headers[1]]; \\ I left this in to check datavalues in the email
message += e.namedValues[headers[1]]; \\ I left this in to check datavalues in the email
message += e.namedValues[headers[1]] + "\n";

var email = e.namedValues[headers[1]].toString; \\ I would ASSUME that this would store \\the spreadhseet data to the variable email,
     

但它似乎没有这样做。

message += "Sheet URL :: " + SpreadsheetApp.getActiveSpreadsheet().getUrl() + "\n";

     switch (email)
    {
        case "basdf":
            email = "email1@hotmail.com";
            break;
        case "dfdsa":
            email = "email2@hotmail.com";
            break;
        default:
            email = "email3@hotmail.com";
            break;
    }




MailApp.sendEmail({
  to: email,
  subject: subject,
  body: message,
  })
   } catch (e) {
Logger.log(e.toString());   }
 }

1 个答案:

答案 0 :(得分:6)

试试这个代码段。它使用表单提交的值检查标题列名称,然后将相应的值分配给电子邮件变量。

var email = "";

for(var i in headers) {
  if (headers[i] == "email") {
     email = e.namedValues[headers[i]].toString();
  }
  message += headers[i] + ' :: '+ e.namedValues[headers[i]].toString() + "\n\n"; 
}

switch (email)  {
        case "basdf":
            email = "email1@hotmail.com";
            break;
        case "dfdsa":
            email = "email2@hotmail.com";
            break;
        default:
            email = "email3@hotmail.com";
            break;
    }