简体   繁体   English

如何使用谷歌应用程序脚本发送具有某些功能的邮件

[英]How to send mails with some functionality using google apps script

I am new to Google Apps Script.我是 Google Apps 脚本的新手。 I have a sheet that collects some "Order Number" from form submit.我有一张从表单提交中收集一些“订单号”的表。 I want to send mails through an event (On form submit) from my spreadsheet.我想通过电子表格中的事件(表单提交时)发送邮件。 The form will serve an order number.该表格将提供订单号。 When the form is submitted, it will match the older submitted order numbers throughout the whole column.提交表单后,它将匹配整个列中较早提交的订单号。 If it got matched once, the mail won't be sent.如果匹配一次,则不会发送邮件。 If it doesn't match then it will send a mail to the email address next to the order number.如果不匹配,它将向订单号旁边的电子邮件地址发送一封邮件。

The email address will come from another sheet on the same spreadsheet using VLOOKUP.电子邮件地址将来自使用 VLOOKUP 的同一电子表格上的另一个工作表。 I managed to do this.我设法做到了这一点。 Sorry if I make any mistake with my English.对不起,如果我的英语有任何错误。

Edit: I tried map() , filter() , indexOf() these methods.编辑:我尝试了map()filter()indexOf()这些方法。 But I too new with this.但我对这个太陌生了。

function search(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Copy of orderStatus");
  var lr = ss.getLastRow() - 1;  
  var keyword = ss.getRange("H5").getValue();
  var dataSource = ss.getRange(2, 2, lr, 1).getValues();
  
  var mapped = dataSource.map(function(r){ 
                                         return r[0]});  
  
  var showPos = mapped.indexOf(keyword) + 2;
  
  var getMail = ss.getRange(showPos, 4).getValue();
  
  var filted = mapped.filter(filterlogic);  

}

var filterlogic = function(r){
  if(r !== "zil20200010"){
    return true;
  } else {
    return false;
  }
    
}
  • On form submit, select the column (range) where you store all the order numbers and create a TextFinder and store it in a variable using the createTextFinder(findText) method for the specified range.在表单提交时,选择存储所有订单号的列(范围)并创建一个TextFinder ,然后使用createTextFinder(findText)方法将其存储在指定范围的变量中。
  • Get the TextFinder from the previous step and search the order number using the findNext() method.获取上一步中的TextFinder并使用findNext()方法搜索订单号。
  • If findNext() returns null then move to the next step.如果findNext()返回null则转到下一步。 else , do nothing. else ,什么都不做。
  • Get the email address to which you plan to send the order number.获取您计划将订单号发送到的电子邮件地址。
  • After having the email address, use the sendEmail(recipient, subject, body, options) method to send the email.获得电子邮件地址后,使用sendEmail(recipient, subject, body, options)方法发送电子邮件。 If you'd like, you can use HTML in the body to make it more professional.如果您愿意,可以在正文中使用 HTML 使其更专业。

For additional information, read:如需更多信息,请阅读:

Sample code:示例代码:

 // imagine you store all the order numbers in column C, starting from row 2 to the last row in the column: var emailRecipient = test@test.com; var ordernumber = 123; var RangeToSearch = sheet.getRange(2,3,sheet.getLastRow()); var TextFinder = RangeToSearch.createTextFinder(ordernumber); var found = TextFinder.findNext(); if (found == null) { MailApp.sendEmail({ to: emailRecipient, subject: "New Order! Order Number: " + ordernumber, htmlBody: html }); }

First of all, thanks to all of you who helped me to reach this point.首先,感谢所有帮助我走到这一步的人。 I found the solution to my problem after some "trial and error".经过一些“反复试验”,我找到了解决问题的方法。 I wanted to limit sending emails.我想限制发送电子邮件。

This code takes the Range.此代码采用范围。 Get its values in an array.在数组中获取它的值。 I mapped that array to act as a string.我将该数组映射为字符串。 Then I added .pop() to that string, it removes our last/newly submitted data in that range.然后我将.pop()添加到该字符串中,它删除了该范围内我们最后/新提交的数据。 Then I used .includes() method to search my value in the mapped array, and assigned it to a variable called final (just came to my mind).然后我使用.includes()方法在映射数组中搜索我的值,并将其分配给一个名为 final 的变量(我突然想到了)。 This variable returns true/false depending on search results.此变量根据搜索结果返回真/假。 If the order number does not exist then it returns false.如果订单号不存在,则返回 false。 After that, we set an if statement to execute our mailing function.之后,我们设置一个 if 语句来执行我们的邮件功能。 If our order number does not match and return final as false our mailing function happens.如果我们的订单号不匹配并且最终返回 false,我们的邮件功能就会发生。 Else it does nothing (means no email sents).否则它什么都不做(意味着不发送电子邮件)。 And that's it!就是这样!

Here is the code that solved my problem这是解决我的问题的代码

function orderStatus(e) {
  try {
    
    var theirMail, subject, message;
    var ourName, theirName;
    var sSheet, orderNum, cosmetics, orderSts, phNum, lr,dataSource, mapped, final;
    
    
    ourName = "My Company Name";
    
    orderNum = e.namedValues["Order Number"].toString();
    
    sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("orderStatus");
    lr = sSheet.getLastRow() - 1;
    
    dataSource = sSheet.getRange(2, 2, lr).getValues();
    
    mapped = dataSource.map(function(r){ 
      return r[0].toString()});
    mapped.pop();
    
    final = mapped.includes(orderNum);
    
    orderSts = sSheet.getRange(sSheet.getLastRow(),1).getValue();
    theirMail = sSheet.getRange(sSheet.getLastRow(),4).getValue();
    theirName = sSheet.getRange(sSheet.getLastRow(),5).getValue();
    phNum = sSheet.getRange(sSheet.getLastRow(),6).getValue();    
    
    subject = "Order status notification from " + ourName + " to " + theirName;
    
    if (final == false){
        
        message = 
          "<div style='text-align: left; padding-left: 30px;'><h2>Dear <b>" + theirName +
              ",</b></h2><p>Your order no is <b><span style='font-size: 14px;'>" + orderNum +
                "</span>.</b> <b><span style='font-size: 14px;'>Your order has been processed.</span>" + 
                  "</b></p><p>We packaged your order and dropped it to the logistics. You will recieve phone call on <b><span style='font-size: 14px;'>" + phNum + 
                    "</span></b> from logistics.<br>Thanks for purchasing from <b><span style='font-size: 14px;'>" + ourName +
                      "</span></b>.</p><p>Best regards,<br><b><span style='font-size: 14px;'>"+ourName+"</span></b></p></div>"+
                        "<p style='text-align: center;'><br><b>For further information please visit our facebook page <a href='https://www.facebook.com/' target='_blank' rel='noopener'>"+ourName+"</a>.</b></p><hr />";
      
      
      textbody = message.replace("<br>", "\n\n");
      
      
      cosmetics = {name: ourName, htmlBody: message};
      
      
      MailApp.sendEmail(theirMail, subject, message, cosmetics);
    }
    
  }
  catch (e) {
    Logger.log(e.toString());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps脚本不允许我发送外发电子邮件 - Google Apps Script will not allow me to send outgoing e-mails Google Apps脚本:使用mailapp.sendmail服务发送仅带有偶尔附件的邮件 - Google apps script: use the mailapp.sendmail service to send mails with only occasional attachments 如何使用HTML和Google Apps脚本发送表单 - How To Send Form With HTML and Google Apps Script 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data Google Apps脚本[电子邮件发送] - Google Apps Script [Email Send] 无法使用Google Apps脚本发送带有附件的电子邮件 - Fail to Send an email with attachment using Google Apps Script google Apps 脚本使用 mailApp 发送 htmlbody(正在修改的内容) - google Apps script using mailApp to send htmlbody (content being modified) 如何使用 PHP 和 cURL 将 e.parameter 发送到 Google Apps Script - How to send an e.parameter to Google Apps Script using PHP and cURL 如何在HTML页面中包含Google Apps脚本,该脚本在Google云端硬盘中具有上传功能 - How To Include a google apps script in a html page,that has uploading functionality on google Drive 如何使用 Google Apps Script 取消透视表? - How to unpivot a table using Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM