Google应用脚本Gmail无需事先通话即可收到消息

时间:2013-04-30 16:26:12

标签: google-apps-script gmail

我正在尝试使用Google应用脚本将我在特定标签下的电子邮件转储到Google文档电子表格中。我想将一个线程中的每个电子邮件消息正文列为一个单独的行,这样如果一个线程有一个包含9条消息的链,则每个消息都会单独列出(没有链)。

我已经管理到了每个消息体+其整个前一个线程存储的位置,在一个单元格中,我可以在一个单元格中获取整个线程。但这不是我想要的。

此代码将整个线程主体放在一行。

function getEmails() {
  clearCanvas();
  var label = GmailApp.getUserLabelByName(LabelWithEmails);
  var threads = label.getThreads();

  // var threads = GmailApp.getInboxThreads(0, 50);
  var row = getFirstRow() + 1;
  var firstmessageId = getfirstmsgid();
  UserProperties.setProperty("firstmsgid", firstmessageId);
  spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);

  var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
  for (i = 0; i < 5; ++i)
  {
   try {
       j = messages[i].length; //to process most recent conversation in thread (contains messages from previous conversations as well, reduces redundancy
       messageBody = messages[i][j-1].getBody(); //gets body of message in HTML
       messageSubject = messages[i][j-1].getSubject();
       messageDate = messages[i][j-1].getDate();
       messageFrom = messages[i][j-1].getFrom();

       Logger.log("Message Subject:" + messageSubject);
       Logger.log("Message Date:" + messageDate);
       Logger.log("Message From:" + messageFrom);

       sheet.getRange(row, 1).setValue(messageFrom);
       sheet.getRange(row, 2).setValue(messageSubject);
       sheet.getRange(row, 3).setValue(messageDate);
       sheet.getRange(row, 4).setValue(getTextFromHtml(messageBody));
       row++;
    } catch (error) {
        spreadsheet.toast("Error Occured. Report it @ http://techawakening.org/", "Status", -1);
    }

    if (i == threads.length - 1) {
        spreadsheet.toast("Successfully loaded emails.", "Status", -1);
        spreadsheet.toast("Now mark emails to be forwarded by changing the background color of the cells to green. Then select Forward->Forward selected emails", "Status", -1);
    }

  }
}

这将把每个消息体包括在它的前一个线程/消息链中。

function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();

// var threads = GmailApp.getInboxThreads(0, 50);
var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);

var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
// messages.length
// jknipp - working except it keeps the thread chain
for (var i = 0; i < threads.length; i++) {
    try {
        var messages = threads[i].getMessages();
        for (var m = 0; m < messages.length; m++) {
            sheet.getRange(row, 1).setValue(messages[m].getFrom());
            sheet.getRange(row, 2).setValue(messages[m].getSubject());
            sheet.getRange(row, 3).setValue(messages[m].getDate());
            sheet.getRange(row, 4).setValue(getTextFromHtml(messages[m].getBody()));

            row++;
        }
    } catch (error) {
        spreadsheet.toast("Error Occured. Report it @ http://techawakening.org/", "Status", -1);
    }

    if (i == threads.length - 1) {
        spreadsheet.toast("Successfully loaded emails.", "Status", -1);
        spreadsheet.toast("Now mark emails to be forwarded by changing the background color of the cells to green. Then select Forward->Forward selected emails", "Status", -1);
    }
  }
}

参考 https://stackoverflow.com/a/11034461/39803

2 个答案:

答案 0 :(得分:1)

这是“垃圾进入,垃​​圾出”的情况。当您在线程视图中使用gmail应用程序时,Google的服务器正在解析电子邮件的内容并巧妙地隐藏旧邮件的主体。这使得线程中的最新消息看起来只包含该消息的新行,并且您有一个较小消息的“链”。

这是一种幻觉。线程中的最后一条消息通常是首先包含新内容,然后是所有先前消息正文的内容,作为单个消息正文。不同的电子邮件服务和客户端使用不同的模式。

您需要能够识别线程中先前消息的内容在当前消息正文中表示的大部分或全部方式,并使用它来仅提取新内容。

答案 1 :(得分:1)

通过识别“之前的对话”的开始位置,我只能取出电子邮件的正文。

var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var LabelWithEmails = sheet.getRange(3, 2).getValue();

function getEmails() {
    clearCanvas();
    var label = GmailApp.getUserLabelByName(LabelWithEmails);
    var threads = label.getThreads();

    var row = getFirstRow() + 1;
    var firstmessageId = getfirstmsgid();
    UserProperties.setProperty("firstmsgid", firstmessageId);
    spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);

    var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array

    for (var i = 0; i < 2;/*threads.length;*/ i++) {
        try {
            var messages = threads[i].getMessages();

            for (var m = 0; m < messages.length; m++) {
                var msg = messages[m];
                var isForward = msg.getBody().search(/---------- Forwarded message/i) != -1;

                if(!isValidMessage) continue;

                sheet.getRange(row, 1).setValue(msg.getFrom());
                sheet.getRange(row, 2).setValue(msg.getTo() + ";" + msg.getCc() + ";" + msg.getBcc());
                sheet.getRange(row, 3).setValue(msg.getSubject());
                sheet.getRange(row, 4).setValue(msg.getDate());

                if(!isForward) {
                    // Get only this messages body, ignore the previous chain
                    var body = msg.getBody();
                    var firstIndexOfThread = body.search(/gmail_quote/i); 
                    body = (firstIndexOfThread == -1) ? body : body.substring(0, firstIndexOfThread);

                        sheet.getRange(row, 5).setValue(getTextFromHtml(body));

                } else {
                    // Use the whole body if its a forward
                    sheet.getRange(row, 5).setValue(getTextFromHtml(msg.getBody()));
                    sheet.getRange(row, 6).setValue("***");
                }

            row++;
        }
    } catch (error) {
            Logger.log(error);
        spreadsheet.toast("Error Occured - please see the logs.", "Status", -1);
    }

    if (i == threads.length - 1) {
        spreadsheet.toast("Successfully loaded emails.", "Status", -1);
    }
  }
}