Google Script仅读取前几行

时间:2013-06-28 13:47:36

标签: javascript debugging google-apps-script

我希望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();
        }
      }
    }
  }
}     

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

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

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

您正在将布尔值与String进行比较。 truefalse都是关键字,因此不应将其括在引号中:

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