Home > Software engineering >  Google Sheets Script - onEdit Function - Send Email After Two Conditions Are Met
Google Sheets Script - onEdit Function - Send Email After Two Conditions Are Met

Time:03-05

First-time asker, long-time StackOverflow stalker. Here is the situation, I have the script below that sends me an email whenever I enter the correct values in columns 18 and 20, these values can go into any row in the target spreadsheet, and as long as they are in the correct column, an email will be sent. The problem is, I need an email to only be sent after I enter both of the correct values into both of the required cells. Right now, if either condition is met, it sends an email, I need to figure out how to get the script to require both conditions are met before sending the email.

Any ideas?

 function sendMailEdit(e){
        if ((e.range.columnStart != 18 || e.value != "Created") && (e.range.columnStart != 20 || 
    e.value != "Inputted")) return;
    const rData = 
    e.source.getSheetByName('Classified').getRange(e.range.rowStart,1,1,20).getValues();
       let fn = rData[0][5];
       let ln = rData[0][6];
       let ein = rData[0][7];
       let email = rData[0][17];
       let escape = rData[0][19];

    let msg = "Employee "   fn   " "   ln   " has been processed by HR, and can now be further 
    processed by IT";
    Logger.log(msg);
    GmailApp.sendEmail("[email protected]", "HR Has Processed an Employee", msg)
    }

CodePudding user response:

In your situation, how about the following modification?

From:

if ((e.range.columnStart != 18 || e.value != "Created") && (e.range.columnStart != 20 || e.value != "Inputted")) return;

To:

const range = e.range;
if (![18, 20].includes(range.columnStart)) return;
const [r, , t] = range.getSheet().getRange(range.rowStart, 18, 1, 3).getValues()[0];
if (r != "Created" || t != "Inputted") return;
  • By this modification, when the values of columns "R" and "T" are Created and Inputted, respectively, the script below the if statement is run.
  • Related