简体   繁体   English

如果声明或不做什么? Gmail Google Apps脚本

[英]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". 如果电子邮件中包含“生产”或“许可证”,则应发送电子邮件并将其标记为“ SentLabel2”。

If the mail does not contain "Production" OR "License" it should just label it with another label "NotSentLabel2" . 如果邮件中不包含“生产”或“许可证”,则应仅使用另一个标签“ NotSentLabel2”对其进行标签。

The problem: It labelsall email with "NotSentLabel2" 问题:它将所有电子邮件都标记为“ 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. 目前,您的最后一个陈述始终是正确的,因为"License"是一个真实值。 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") } 

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

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