如果声明或/不做什么?Gmail谷歌应用程序脚本

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

本文关键字:Gmail 谷歌 应用程序 脚本 什么 声明 如果      更新时间:2023-09-26

我试图在电子邮件上添加两个不同的标签。如果电子邮件中包含"生产"或"许可证",则应发送一封电子邮件并标记为"SentRabel2"。

如果邮件中不包含"生产"或"许可证",则应使用另一个标签"NotSentRabel2"进行标记。

问题:它将所有电子邮件标记为"NotSentRabel2"

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);
      }

当前您的最后一条语句始终为true,因为"License"是一个truthy值。你想要这样的东西

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

但是,既然您正在使用regex,为什么不同时使用测试和测试两者的呢

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