2014-10-17 60 views
0

我们正在尝试在Gmail中向线索添加标签。这两个var的相同的输出“2014/10月/发送” 但是只有SentLabel2工作​​,如果我们设置SentLabel threads[i].addLabel(SentLabel);它失败了。为Gmail中的电子邮件添加标签失败

我们需要的是SentLabel怎么一回事,因为我们已经做了自动一个月这样的事(即获得了短路电流月,这样一来我们不需要每个月手动去到脚本和修改变量):

var SentLabel = "2014/" + monthInWords[d.getMonth()] + "/Sent"; 

下面是它看起来像现在:

function wtf2() { 
     var SentLabel = "2014/October/Sent"; 
     var SentLabel2 = GmailApp.getUserLabelByName("2014/October/Sent"); 


     var threads = GmailApp.getInboxThreads(0, 5); 
     for (var i = 0; i < threads.length; i++) { 
     var labels = threads[i].getLabels()[0];   
    threads[i].addLabel(SentLabel); 

    } 
    } 

回答

0

为什么下面的语句失败的原因是因为SentLabel是一个字符串变量,而不是GMAILLABEL变量。

threads [i] .addLabel(SentLabel);

要解决这个问题,您需要有条件地创建一个gmailLabel变量(我会称这个var标签),具体取决于它是否已经存在。这里是你如何能做到这一点:

var label = GmailApp.getUserLabelByName(SentLabel); //declares a variable called label and initializes it to be the value thats returned when searching GmailApp for any label that is named the same as the string you provided in your SentLabel variable 

    if (label) { //if gmail finds a matching label 
     Logger.log('Label exists.'); 
    } else { 
     Logger.log('Label does not exist. Creating it.'); 
     label = GmailApp.createLabel(SentLabel); 
    } 

一旦运行,那么你将有一个标签变量,然后如果你改成这样您的声明将工作:

threads[i].addLabel(label); 

您可以阅读有关GmailLabel这里的类,https://developers.google.com/apps-script/reference/gmail/gmail-label#

+0

哈哈姆我试过,并没有工作,当我现在再试一次,它没有工作>。<大声笑,谢谢彼得! – 2014-10-18 12:45:51