使用Google App脚本阅读Gmail标签中的所有邮件 [英] Read all message in Gmail Label with Google App Script

查看:114
本文介绍了使用Google App脚本阅读Gmail标签中的所有邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google App Script的业余爱好者.

I am a big amateur in Google App Script.

救出了适用于我但不正确的脚本. 现在,仅读取和发送有关该线程的第一条消息的信息,而不是所有消息.

Rescued this script that works for me but not correctly. Now only read and phsarse info for the first message of the thread not all the messages.

我需要阅读,检查和处理标签的所有消息,而不仅是线程的第一条. 我需要脚本读取并处理所有单独的消息 位于标签上. 然后将所有标记为已读.

I need to read,check, and pharse all messages of a label, not only the first of the thread. I need that the script read and pharse all the individual messages located on a tag. Then marks all as read.

有人可以帮助我并更改代码来做到这一点吗? 我查看了Google APP脚本手册,并尝试了其他方法,但无法正常工作.

Someone could help me and change the code to do this? I review Google APP Script manual and I've tried different things but I can't get it to work.

谢谢!

  //var threads = GmailApp.getInboxThreads();
  // Have to get data separate to avoid google app script limit!
  var start = 0;
  var threads = GmailApp.search("newer_than:1d AND is:unread AND label:eur OR label:desc",0,100); 
  var sheet = SpreadsheetApp.getActiveSheet();
  var result = [];

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

    var content = messages[0].getPlainBody();
    messages[0].markRead();
    // implement your own parsing rule inside
    if (content) {
      var tmp;
      tmp = content.match(/\b([A-B\d][A-B\d]{4})\b/);
      var cod = (tmp && tmp[1]) ? tmp[1].trim() : 'Error';

      tmp = content.match(/\b(\d+[R])/);
      var prom = (tmp && tmp[1]) ? tmp[1].trim() : 'Error';

      tmp = content.match(/\b(\d{2}\.\d{2}\)\b/);
      var exp = (tmp && tmp[1]) ? tmp[1].trim() : 'Error';

      sheet.appendRow([cod, prom, exp]);

      Utilities.sleep(500);
    }


  }
}; ```

推荐答案

为了将消息标记为已读,您必须迭代每个线程的消息.

In order to mark messages as read, you have to iterate messages of each thread.

您只会收到线程的第一条消息,因此您必须替换

You are only getting the first message of the thread, so you have to replace

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

  var content = messages[0].getPlainBody();
  messages[0].markRead();

  .....

类似这样:

function readMails (){

  var threads = GmailApp.search("newer_than:1d AND is:unread AND label:eur OR label:desc",0,100);

  //Iterate threads
  for (var i=0; i < threads.length; i++){

    var messages = threads[i].getMessages();

    //Iterate messages of each thread
    for (var k=0; k < messages.length; k++){
      var content = messages[k].getPlainBody();
      messages[k].markRead();

      .....
    }
  }
}

这篇关于使用Google App脚本阅读Gmail标签中的所有邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆