0

我希望 monitorEmails 功能在收件箱中查找新邮件。无论出于何种原因,GS 不会读取前两行代码,我知道这一点是因为 Log 只显示第一行

Logger.log(..)

声明已经核实。GS 只记录前几行,除了第一条语句之外不提供任何反馈。

如何让 GS 显示 Logger.log(..) 语句?

这是代码:

    //set a time-driven trigger to run this function on the desired frequency
function monitorEmails() {  
  var thread = GmailApp.getInboxThreads(0,1)[0]; // get first thread in inbox 
  // log the number of messages in the thread
  var firstThread = GmailApp.getInboxThreads(0,1)[0];
  var numMsgs = firstThread.getMessageCount();  // # of messages in a given thread
  Logger.log(firstThread.getMessageCount());
  var messages = firstThread.getMessages();
  if (firstThread.isUnread()=='true'){  // check if thread is Read
    //Logger.log(firstThread.isUnread()=='true');
    for (var i = 0; i < messages.length; i++) {  //Go through each message within a   Thread
      // Logger.log(messages[i].getSubject());
      if (messages[i].isUnread()){   // if the message is unread
        Logger.log(messages[i].isUnread());
        var unreadBody = messages[i].getBody();  // Body contents variable that contains the body 
        Logger.log(messages[i].getBody());
        var unreadFrom = messages[i].getFrom();  // Email Sender contents variable
        //Logger.log(messages[i].getFrom());
        messages[i].markRead();  // Mark message as read
        //Logger.log(messages[i].markRead());  
        var fullLot = "Full";  // look for "Full" in text message.
        //Logger.log(unreadBody.search(fullLot) =='true');
        if(unreadBody.search(fullLot) == 'true' && getEmail()==AdminTest()){
          // if the sender has the proper admin email / phone #, then call function sendEmails
          sendEmails();
        }
      }
    }
  }
}     

谢谢你的帮助!

4

1 回答 1

0

您在这样的比较中遇到了问题:

if (firstThread.isUnread()=='true'){  // check if thread is Read

您正在将布尔值与字符串进行比较。true和都是false关键字,因此不应将它们括在引号中:

if (firstThread.isUnread() == true){  // check if thread is Read

在几个地方,您正在执行比较,然后在if块内记录比较结果 - 在比较之前记录比较结果更有用。

我认为您可以通过使用一些上下文信息使您的日志更有意义来帮助自己——修改后的脚本显示了这方面的示例。

日志

[13-07-02 09:40:53:583 EDT] numMsgs=1
[13-07-02 09:40:53:583 EDT] Subject=YOU could be our Player of the Week!
[13-07-02 09:40:53:658 EDT] firstThread.isUnread()==true
[13-07-02 09:40:53:658 EDT] messages[0].subject=YOU could be our Player of the Week!
[13-07-02 09:40:53:658 EDT] messages[0].isUnread=true
...

修改后的脚本

function monitorEmails() {  
  var thread = GmailApp.getInboxThreads(0,1)[0]; // get first thread in inbox 
  // log the number of messages in the thread
  var firstThread = GmailApp.getInboxThreads(0,1)[0];
  var numMsgs = firstThread.getMessageCount();  // # of messages in a given thread
  Logger.log("numMsgs="+numMsgs);
  Logger.log("Subject="+firstThread.getFirstMessageSubject());
  var messages = firstThread.getMessages();
  Logger.log("firstThread.isUnread()=="+firstThread.isUnread());
  if (firstThread.isUnread()== true ){  // check if thread is Read
    for (var i = 0; i < messages.length; i++) {  //Go through each message within a   Thread
      Logger.log("messages["+i+"].subject="+messages[i].getSubject());
      Logger.log("messages["+i+"].isUnread="+messages[i].isUnread());
      if (messages[i].isUnread()){   // if the message is unread
        var unreadBody = messages[i].getBody();  // Body contents variable that contains the body 
        Logger.log("messages["+i+"].body="+unreadBody);
        var unreadFrom = messages[i].getFrom();  // Email Sender contents variable
        Logger.log("messages["+i+"].from="+messages[i].getFrom());
        messages[i].markRead();  // Mark message as read
        Logger.log("messages["+i+"].isUnread="+messages[i].isUnread()); 
        var fullLot = "Full";  // look for "Full" in text message.
        var searchResult = unreadBody.search(fullLot);
        Logger.log("messages["+i+"].searchResult="+searchResult);
        if(searchResult == true && getEmail()==AdminTest()){
          // if the sender has the proper admin email / phone #, then call function sendEmails
          sendEmails();
        }
      }
    }
  }
}     
于 2013-07-02T13:46:35.817 回答