简体   繁体   English

满足多个列中的多个条件时发送电子邮件-Google表格脚本

[英]Send email when multiple conditions across multiple columns are met - Google Sheet Script

This is my first time posting here and I'm a newbie when it comes to understanding code. 这是我第一次在这里发布文章,在理解代码方面我是一个新手。 I'll try my best to clarify what is the desired result and where I am at. 我会尽力弄清楚什么是理想的结果以及我在哪里。

I have a "column A" which is a dropdown which contains Options "1,2,3,4,5". 我有一个“列A”,它是一个包含选项“ 1,2,3,4,5”的下拉列表。 In the same sheet I have another column - "column B" which is a dropdown which contains options "A, B" 在同一张纸上,我还有另一列-“列B”,这是一个包含选项“ A,B”的下拉列表

Desired Outcome - Whenever the user selects option "2" and option "B", an email should get triggered to a recipient. 期望的结果 -每当用户选择选项“ 2”和选项“ B”时,都会触发一封电子邮件给收件人。

Current State - I've browsed through this thread and Cooper's solution got me to a certain extent. 当前状态 -我已经浏览了此线程 ,Cooper的解决方案在一定程度上使我受益。 However, I'm not able to send the email when there are two conditions to be met. 但是,有两个条件要满足时,我将无法发送电子邮件。 This is how I've set the trigger - The function sendNotification --> From spreadsheet --> On Edit 这就是我设置触发器的方式-sendNotification函数->来自电子表格->编辑时

Any help will be much appreciated. 任何帮助都感激不尽。 Thanks in advance! 提前致谢!

EDIT - This is the code I'm using - 编辑-这是我正在使用的代码-

    function sendNotification(e){
     if(e.range.getColumn()==9 && e.value=='Approved'){
      var recipients = "blah@blah.com";
      var subject = "Custom Subject Line";
      var valColB=e.range.getSheet().getRange(e.range.getRow(),2).getValue();
      var body = "Custom body with variable - " + valColB + "from the sheet titled - " + e.range.getSheet().getName();
     MailApp.sendEmail(recipients, subject, body)
 }
}

EDIT 2 - This was an alternate method I tried, but somehow even this did not work. 编辑2-这是我尝试的另一种方法,但是无论如何它还是行不通的。 It wouldn't work the moment I put in the IF statement inside funTwo. 当我在funTwo中放入IF语句时,它将无法正常工作。

function funOne(e) {
  if(e.range.getColumn() == 5 && e.value == 'Option 2') {
    funTwo();
  }
}

function funTwo(a) {
  if(a.range.getColumn() == 9 && a.value == 'Option B') {
    MailApp.sendEmail('blah@blah.com, 'subject', 'body')
 }
}

Add a another condition with offset : 添加另一个带有offset条件:

Try changing from 尝试从

if(e.range.getColumn()==9 && e.value=='Approved'){

To

if(e.range.getColumn()==9 && e.value=='Approved' && e.range.offset(0,-1).getValue() == '2' ){ //if ninth column is edited to Approved and the corresponding eighth column is 2, then...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM