[英]I am trying to send an email based on which category something falls into (mgr, str, wtr), but it only sends to the first email in the group?
I have been slaving over this piece of code and the script is saying that it's working, the Logger.log, is popping up after every line, but it's only using on of the emails to send the emails to regardless of category. 我一直在忙于这段代码,脚本说它正在运行,Logger.log在每一行之后都会弹出,但是它仅使用其中一个电子邮件将电子邮件发送到任何类别。 I have tried many different solutions, but I think I need some help (I've searched online forEVER and still can't find anything to help for this problem). 我尝试了许多不同的解决方案,但我认为我需要一些帮助(我一直在网上搜索,但仍然找不到任何可解决此问题的方法)。 Here's my code: 这是我的代码:
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
var sheetProgressReport = ss.getSheetByName("ProgressReport");
var sheetManagers = ss.getSheetByName("Managers");
var range = sheetProgressReport.getRange(4, 7, sh.getLastRow(), 1);
var rangeSentEmail = sheetProgressReport.getRange(4, 8, sh.getLastRow(), 1);
var column = range.getColumnIndex();
if(column === 7) {
var managerEmail = sheetManagers.getRange(22, 2).getValue().toString(); //object to string with respective manager's email
var strategistEmail = sheetManagers.getRange(23, 2).getValue().toString(); //object to string with respective strategist's email
var writerEmail = sheetManagers.getRange(24, 2).getValue().toString(); //object to string with respective writer's email
var client = sheetManagers.getRange(2, 2).getValue().toString(); //object to string with client name
var message = "Please tell Jordan if you get this! Thank you! :)"; //string with message
var category = range.getValues();
var columnSentEmail = rangeSentEmail.getValues();
var mgr = "mgr";
var str = "str";
var wtr = "wtr";
var EMAIL_SENT = "EMAIL_SENT";
if (columnSentEmail != EMAIL_SENT && category === "") {email = "";}
else if (category = mgr) {email = managerEmail;}
else if (category = str) {email = strategistEmail;}
else if (category = wtr) {email = writerEmail;}
MailApp.sendEmail(email, client, message);
rangeSentEmail.setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
I'm guessing it's something to do with the way that I'm processing (or the lack of processing) the data in the "rangeSentEmail/range" vars. 我猜想这与我在“ rangeSentEmail / range”变量中处理(或缺乏处理)数据的方式有关。 Here's my goal if you can't tell from the messy code: I want to read the top 5-10 lines in the "rangeSentEmail/range" vars, then check to make sure that "rangeEmailSent" is not marked with "EMAIL_SENT"; 如果您不能从凌乱的代码中分辨出来,这就是我的目标:我想阅读“ rangeSentEmail / range”变量中的前5-10行,然后检查以确保“ rangeEmailSent”未标记为“ EMAIL_SENT”; if it meets that requirement, then I want the "range" column to check for labels (mgr, wtr, str) and then send emails to the respective "managerEmail, writerEmail, or strategistEmail" based on which label is in the column. 如果满足该要求,那么我希望“范围”列检查标签(mgr,wtr,str),然后根据该列中的标签将电子邮件发送到相应的“ managerEmail,writerEmail或strategistEmail”。 I honestly can't deduce where the problem is, so PLEASE and THHHAAANNKK YOUUUU in advance! 老实说,我无法推断出问题出在哪里,所以请提前和THHHAAANNKK YOUUUU! I'll be checking up regularly for any help! 我会定期检查是否有任何帮助!
Your problem is here var category = range.getValues() 您的问题在这里var category = range.getValues()
change from getValues() to getValue(). 从getValues()更改为getValue()。 The first is returing an array of objects and the second return only the value, be sure that is a single value. 第一种是重新设置对象数组,第二种仅返回值,请确保该值是单个值。
The second problem is here: 第二个问题在这里:
if (columnSentEmail != EMAIL_SENT && category === "") {email = "";}
else if (category = mgr) {email = managerEmail;}
else if (category = str) {email = strategistEmail;}
else if (category = wtr) {email = writerEmail;}
Try using only "==" to compare values. 尝试仅使用“ ==”来比较值。 The operator "===" forces you to check if the value AND type are identical and using only one equals "=" just attribute the value to the category variable. 运算符“ ===”强制您检查值和类型是否相同,并且仅使用一个等于“ =”的值,将值归给category变量。 So, use only "==" to make this comparisons. 因此,仅使用“ ==”进行比较。
Best, 最好,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.