使用Google App Script更改回复空白

时间:2014-06-12 13:57:30

标签: google-apps-script string

我有一个脚本要求替代地址但不要求字段。电子邮件回复说" undefined"在所有留空的字段上。

是否有办法自定义应用程序脚本为空白字段提供的响应,实际上是在响应电子邮件中将它们留空或者单词" no answer"而不是" undefined"?

我正在从Google表单提交中创建一堆变量。

function onFormSubmit(e) {
 var toAddress = e.values[1];
 var myFirstName = e.values[2];
 var myLastName = e.values[3];
 var myCertifications = e.values[4];
 var myEmployer = e.values[5];
 var myPhoneNumber = e.values[6];
 var myAltPhoneNumber = e.values[7];
 var myFax = e.values[8];
 var myBirthday = e.values[9];
 var myWorkAddy = e.values[10];
 var myWorkAddy2 = e.values[11];
 var myWorkCity = e.values[12];
 var myWorkState = e.values[13];
 var myWorkZip = e.values[14];
 var myAltAddy = e.values[15];
 var myAltAddy2 = e.values[16];
 var myAltCity = e.values[17];
 var myAltState = e.values[18];
 var myAltZip = e.values[19];
 var mySnailPref = e.values[20];
 var myQuestions = e.values[21];

然后我通过电子邮件发送变量数据。如果所有字段都由用户填写,一切正常,但某些字段不是必需的。当他们将字段留空时,脚本不会将数据放在电子邮件中的适当位置。

我可以在这些变量下面添加什么,以便如果某人没有回答其中一个问题,那么," Not Answered"?

最新编辑从这里开始:

我尝试了Serge的方法,有很多不同的方法,但我似乎无法正确理解。

它有点起作用,但似乎将字段所需的变量设置为" undefined"。

这是指向工作表副本的链接。随意查看和编辑脚本。如果有人能够做到正确,所有人都会回到这里并留下答案供其他人学习。

https://docs.google.com/spreadsheets/d/1RkAdssA9dwef_ZeknAlE1rZKEYlO0xs-cJZQir0-9jg/edit?usp=sharing

以下是其中的脚本的完整定义。

 function onFormSubmit(e) {
  var toAddress = e.values[1];
  var myFirstName = e.values[2];
  var myLastName = e.values[3];
  var myCertifications = e.values[4]!=null? e.values[4] : 'no answer';
  var myEmployer = e.values[5]!=null? e.values[5] : 'no answer';
  var myPhoneNumber = e.values[6];
  var myAltPhoneNumber = e.values[7]!=null? e.values[7] : 'no answer';
  var myFax = e.values[8]!=null? e.values[8] : 'no answer';
  var myBirthday = e.values[9]!=null? e.values[9] : 'no answer';
  var myWorkAddy = e.values[10];
  var myWorkAddy2 = e.values[11]!=null? e.values[11] : 'no answer';
  var myWorkCity = e.values[12];
  var myWorkState = e.values[13];
  var myWorkZip = e.values[14];
  var myAltAddy = e.values[15]!=null? e.values[15] : 'no answer';
  var myAltAddy2 = e.values[16]!=null? e.values[16] : 'no answer';
  var myAltCity = e.values[17]!=null? e.values[17] : 'no answer';
  var myAltState = e.values[18]!=null? e.values[18] : 'no answer';
  var myAltZip = e.values[19]!=null? e.values[19] : 'no answer';
  var mySnailPref = e.values[20];
  var myQuestions = e.values[21]!=null? e.values[21] : 'no answer';

  var emailreplyTo = "dave@davelalande.com" ;
  var subject = "Membership Application";
  var emailBody = myFirstName + ", " +
   "\n\nThank you for your Association Registration." +
   "\nThe information we received is as follows." +
   "\n\nFirst Name: " + myFirstName +
   "\nLast Name: " + myLastName +
   "\nCurrent Certifications: " + myCertifications +
   "\nEmployer: " + myEmployer +
   "\nPhone Number: " + myPhoneNumber +
   "\nAlternative Phone Number: " + myAltPhoneNumber +
   "\nFax: " + myFax +
   "\nBirthday and Birth Month: " + myBirthday +
   "\nWork Address: " + myWorkAddy +
   "\nWork Address 2: " + myWorkAddy2 +
   "\nWork City: " + myWorkCity +
   "\nWork State: " + myWorkState +
   "\nWork ZIP: " + myWorkZip +
   "\nAlternaitive Address: " + myAltAddy +
   "\nAlternative Address 2: " + myAltAddy2 +
   "\nAlternative Address City: " + myAltCity +
   "\nAlternative Address State: " + myAltState +
   "\nAlternative Address ZIP: " + myAltZip +
   "\nSnail Mail Address Preference: " + mySnailPref +
   "\nYour Question or Comment: " + myQuestions +
   "\n\nPlease print this page and mail it with a check in the amount of $25.00, payable to the Association." +
   "\n\nMail it to:" +
   "\nDave Lalande" +
   "\n123 Main" +
   "\nSuite 240" +
   "\nAny Town, MO  65201" +
   "\n\nYour membership will become active when the dues have been received and processed. " +
   "\nYou will receive an email notifying you when your membership has been activated. Welcome to the Association! " +
   "\n\nIf you have questions please email me at dave@davelalande.com. ";


var htmlBody = myFirstName +"," +
   "<br/><br/>Thank you for your Missouri State Tumor Registrars' Association (MoSTRA) Registration. " +
   "<br/>The information we received is as follows." +
   "<br/><br/>First Name: " + myFirstName +
   "<br/>Last Name: " + myLastName +
   "<br/>Current Certifications: " + myCertifications +
   "<br/>Employer: " + myEmployer +
   "<br/>Phone Number: " + myPhoneNumber +
   "<br/>Alternative Phone Number: " + myAltPhoneNumber +
   "<br/>Fax: " + myFax +
   "<br/>Birthday and Birth Month: " + myBirthday +
   "<br/>Work Address: " + myWorkAddy +
   "<br/>Work Address 2: " + myWorkAddy2 +
   "<br/>Work City: " + myWorkCity +
   "<br/>Work City: " + myWorkState +
   "<br/>Work City: " + myWorkZip +
   "<br/>Alternative Address: " + myAltAddy +
   "<br/>Alternative Address 2: " + myAltAddy2 +
   "<br/>Alternative Address City: " + myAltCity +
   "<br/>Alternative Address State: " + myAltState +
   "<br/>Alternative Address ZIP: " + myAltZip +
   "<br/>Snail Mail Address Preference: " + mySnailPref +
   "<br/>Your Question or Comment: " + myQuestions +
   "<br/><br/>Please print this page and mail it with a check in the amount of $25.00, payable to the Association. " +
   "<br/><br/>Mail it to: " +
   "<br/><br/>Dave Lalande " +
   "<br/>123 Main " +
   "<br/>Suite 240 " +
   "<br/>Any Town, MO  65201 " +
   "<br/><br/>Your membership will become active when the dues have been received and processed. " +
   "<br/>You will receive an email notifying you when your membership has been activated. Welcome to the Association! " +
   "<br/><br/>If you have questions please email me at dave@davelalande.com. ";


var optAdvancedArgs = {replyTo:emailreplyTo, name: "Membership Chair", htmlBody: htmlBody};

MailApp.sendEmail(toAddress, subject, emailBody, optAdvancedArgs);
}

2 个答案:

答案 0 :(得分:1)

您可以在每个项目上使用简短形式条件,如下例所示:

function myFunction() {
  var e = {};
  e.values='';// these 2 lines only to create test conditions...
//  var test = e.values[0]; // uncomment this line to test "normal" behavior without condition
  var test = e.values[0]!=null? e.values[0] : 'no answer';// example with condition
  Logger.log(test);// see result in logger
}

请在此处查看有关此条件操作的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator


edit -suggestion to try-:想法是用预定义的字符串替换e object中的空字符串(在将对象转换为字符串后执行该操作)并将其解析回对象。 (我没有测试过) 代码应该是这样的:

function onFormSubmit(e) {
  var e_string = JSON.stringify(e);
  e=JSON.parse(e_string.replace(/""/g,'not answered'));
  var toAddress = e.values[1];
  var myFirstName = e.values[2];
  var myLastName = e.values[3];
  //.... continue your function unchanged
}

(编辑说明:这在此上下文中不起作用...它可以在使用doGet / doPost结构的UiApp表单中使用,但不能用于Google表单,...抱歉浪费时间)


编辑2

让它工作,但我不得不修改你的初始方法...... 而不是通过索引(values[n])获取值我使用项目名称...

代码如下:

function onFormSubmit(e) {
  var items = FormApp.openById('1r67Ny2hWl5Su2wtzKKoT5a4ITE1FkKfJigZx0hMXvkU').getItems();
  for(var n = 0 ; n < items.length; n++){
    Logger.log(n+'  - '+items[n].getTitle()+' == ? '+e.namedValues[items[n].getTitle()]);
    if(e.namedValues[items[n].getTitle()]==null){e.namedValues[items[n].getTitle()] = 'no answer'}
  }
  var toAddress = e.namedValues[items[0].getTitle()];
  var myFirstName = e.namedValues[items[1].getTitle()];
  var myLastName = e.namedValues[items[2].getTitle()];
  var myCertifications = e.namedValues[items[3].getTitle()];
  var myEmployer = e.namedValues[items[4].getTitle()];
  var myPhoneNumber = e.namedValues[items[5].getTitle()];
  var myAltPhoneNumber = e.namedValues[items[6].getTitle()];
  var myFax = e.namedValues[items[7].getTitle()];
  var myBirthday = e.namedValues[items[8].getTitle()];
  var myWorkAddy = e.namedValues[items[11].getTitle()];
  var myWorkAddy2 = e.namedValues[items[12].getTitle()];
  var myWorkCity = e.namedValues[items[13].getTitle()];
  var myWorkState = e.namedValues[items[14].getTitle()];
  var myWorkZip = e.namedValues[items[15].getTitle()];
  var myAltAddy = e.namedValues[items[17].getTitle()];
  var myAltAddy2 = e.namedValues[items[18].getTitle()];
  var myAltCity = e.namedValues[items[19].getTitle()];
  var myAltState = e.namedValues[items[20].getTitle()];
  var myAltZip = e.namedValues[items[21].getTitle()];
  var mySnailPref = e.namedValues[items[23].getTitle()];
  var myQuestions = e.namedValues[items[24].getTitle()];

  var emailreplyTo = "dave@davelalande.com" ;
  var subject = "Membership Application";
  var htmlBody = myFirstName +"," +
       "<br><br>Thank you for your Missouri State Tumor Registrars' Association (MoSTRA) Registration. " +
       "<br>The information we received is as follows." +
       "<br><br>First Name: " + myFirstName +
       "<br>Last Name: " + myLastName +
       "<br>Current Certifications: " + myCertifications +
       "<br>Employer: " + myEmployer +
       "<br>Phone Number: " + myPhoneNumber +
       "<br>Alternative Phone Number: " + myAltPhoneNumber +
       "<br>Fax: " + myFax +
       "<br>Birthday and Birth Month: " + myBirthday +
       "<br>Work Address: " + myWorkAddy +
       "<br>Work Address 2: " + myWorkAddy2 +
       "<br>Work City: " + myWorkCity +
       "<br>Work City: " + myWorkState +
       "<br>Work City: " + myWorkZip +
       "<br>Alternative Address: " + myAltAddy +
       "<br>Alternative Address 2: " + myAltAddy2 +
       "<br>Alternative Address City: " + myAltCity +
       "<br>Alternative Address State: " + myAltState +
       "<br>Alternative Address ZIP: " + myAltZip +
       "<br>Snail Mail Address Preference: " + mySnailPref +
       "<br>Your Question or Comment: " + myQuestions +
       "<br><br>Please print this page and mail it with a check in the amount of $25.00, payable to the Association. " +
       "<br><br>Mail it to: " +
       "<br><br>Dave Lalande " +
       "<br>123 Main " +
       "<br>Suite 240 " +
       "<br>Any Town, MO  65201 " +
       "<br><br>Your membership will become active when the dues have been received and processed. " +
       "<br>You will receive an email notifying you when your membership has been activated. Welcome to the Association! " +
       "<br><br>If you have questions please email me at dave@davelalande.com. ";
  var emailBody = htmlBody.replace(/<br>/g,'\n');
  var optAdvancedArgs = {replyTo:emailreplyTo, name: "Membership Chair", htmlBody: htmlBody};
  Logger.log(emailBody);
  MailApp.sendEmail(toAddress, subject, emailBody, optAdvancedArgs);
  var data = SpreadsheetApp.openById('1hnnpK5MAUET8QHIezFP9LskHf9Q-SMNwDpNkIpcWGbM').getSheets()[0].getDataRange().getValues();
  for (var r in data){
    for(var c in data[0]){
      if(data[r][c]==''){data[r][c]='no answer'}
    }
  }
  SpreadsheetApp.openById('1hnnpK5MAUET8QHIezFP9LskHf9Q-SMNwDpNkIpcWGbM').getSheets()[0].getDataRange().setValues(data);
}

注意:您必须编辑表单以避免项目描述中的重复名称...现在有2个字段称为“地址”,“zip”等...所以它无法正确检测它们但是不会很难解决!

另请注意,如果您这样做,则必须取消表单与电子表格的链接,然后重新链接,以便表格中的列对应新项目标题,表单回复也与脚本中的定义相匹配(您当然可以重复使用相同的电子表格,但在其中使用新的表格。)

我在评论中添加了一个可以完美运行的更新表单的链接。

实际上我也更改了邮件构造部分,直接从html版本获取文本版本(因为你只使用了br标签,我只是用\n替换这些标签以获得纯文本版本)使代码更短..

我还添加了电子表格的自动更新(我在另一篇文章中也看到了你想要标记工作表中的空单元格),用“无应答”填充空单元格。

好吧,我想我现在已完成了修改!享受;)

答案 1 :(得分:0)

遇到相同的问题,找到了一个较短的解决方案:

// get the last response
var allResponses = FormApp.getActiveForm().getResponses();
var lastResponses = allResponses[allResponses.length-1].getItemResponses();

// convert values into dictionary
var response = {};
for (var i = 0; i < lastResponses.length; i++) {
  response[lastResponses[i].getItem().getTitle()] = lastResponses[i].getResponse();
}

// access values like this (directly in code)
var full name = response['first name'] + response['last name']