使用Google Scripts从电子邮件正文中提取信息

时间:2015-07-10 16:03:26

标签: regex email google-sheets gmail

我正在尝试从Gmail中的某个标签中的电子邮件中提取特定信息。我基于https://gist.github.com/Ferrari/9678772的脚本一起入侵(我的脚本知识非常有限)以下内容。我收到一个错误:“无法将数组转换为Gmail线程 - 第5行”

非常感谢任何帮助。

/* Based on https://gist.github.com/Ferrari/9678772 */
function parseEmailMessages(start) {

  /* var threads = GmailApp.getInboxThreads(start, 100); */
  var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname"));
  var sheet = SpreadsheetApp.getActiveSheet();

  var tmp, result = [];

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

   // Get the first email message of a threads
    var message = threads[i].getMessages()[0];

   // Get the plain text body of the email message
   // You may also use getRawContent() for parsing HTML
    var content = messages[0].getPlainBody();


   // Implement Parsing rules using regular expressions
    if (content) {

      tmp = content.match(/Name and Surname:\n([A-Za-z0-9\s]+)(\r?\n)/);
      var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';

      tmp = content.match(/Phone Number:\n([\s\S]+)/);
      var phone = (tmp && tmp[1]) ? tmp[1] : 'No phone';

      tmp = content.match(/Email Address:\n([A-Za-z0-9@.]+)/);
      var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email';

      tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
      var comment = (tmp && tmp[1]) ? tmp[1] : 'No office';



      sheet.appendRow([username, phone, email, comment]);

    }
  }
};

2 个答案:

答案 0 :(得分:1)

谢谢大家..这就是诀窍:

// Adapted from https://gist.github.com/Ferrari/9678772
function processInboxToSheet() {

  // Have to get data separate to avoid google app script limit!

  var start = 0;
  var label = GmailApp.getUserLabelByName("yourLabelName");
  var threads = label.getThreads();

  var sheet = SpreadsheetApp.getActiveSheet();
  var result = [];



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

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

    // implement your own parsing rule inside
    if (content) {
      var tmp;
      tmp = content.match(/Name and Surname:\n([A-Za-z0-9\s]+)(\r?\n)/);
      var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';

      tmp = content.match(/Phone Number:\n([\s\S]+)/);
      var phone = (tmp && tmp[1]) ? tmp[1] : 'No phone';

      tmp = content.match(/Email Address:\n([A-Za-z0-9@.]+)/);
      var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email';

      tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
      var comment = (tmp && tmp[1]) ? tmp[1] : 'No office';



      sheet.appendRow([username, phone, email, comment]);

      Utilities.sleep(500);
    }
  }
};

答案 1 :(得分:0)

var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname"));

应该包含一个数组索引,因为GmailApp.search会返回一个数组,即使只找到一个项目。

var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname")[0]);

会起作用,但却很罗嗦。

var thread_list = GmailApp.search("label:labelname");
var threads = GmailApp.getMessagesForThread(thread_list[0]);
IMO,上述内容更清晰。