[英]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;
}
}
TextFinder
and store it in a variable using the createTextFinder(findText)
method for the specified range.TextFinder
,然后使用createTextFinder(findText)
方法将其存储在指定范围的变量中。TextFinder
from the previous step and search the order number using the findNext()
method.TextFinder
并使用findNext()
方法搜索订单号。findNext()
returns null
then move to the next step.findNext()
返回null
则转到下一步。 else
, do nothing. else
,什么都不做。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. 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.