简体   繁体   中英

if statement or/not what to do? Gmail google apps script

Im trying to add two different labels on emails. If the email contains "Production" OR "License" it should send an email and label it with "SentLabel2".

If the mail does not contain "Production" OR "License" it should just label it with another label "NotSentLabel2" .

The problem: It labelsall email with "NotSentLabel2"

var sub = message.getSubject();
    var res = sub.match(/Production/g);
    var res2 = sub.match(/License/g);

    if (labels == undefined){

      if (res == "Production"){
        GmailApp.sendEmail(from, "test", "ok sounds good :)");
              threads[i].addLabel(SentLabel2);

      }
       if (res2 == "License"){
        GmailApp.sendEmail(from, "test", "ok sounds good :)");
              threads[i].addLabel(SentLabel2);

      }
      if (res || res2 != "Production" || "License"){
                      threads[i].addLabel(NotSentLabel2);

      }

currently your last statement is always true as "License" is a truthy value. you would want something like

if(res !== "Production" && res2 !== "License") {...

but seeing as you are using regex why not use the test and test for both at the same time

 var sub = "Production"; if (/(Production)|(License)/g.test(sub)) { console.log("matched") } else { console.log("didn't match anything") } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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