谷歌脚本只读取前几行

Google Script Only Reads Frst Few Lines

本文关键字:几行 脚本 读取 谷歌      更新时间:2023-09-26

我希望monitorE-mails函数在收件箱中查找新邮件。无论出于什么原因,GS都不会读取前两行代码,我知道这一点,因为日志只显示第一个

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

谢谢你的帮助!

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

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

您正在将布尔值与字符串进行比较。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();
        }
      }
    }
  }
}