【问题标题】:Extract info from email body with Google Scripts使用 Google 脚本从电子邮件正文中提取信息
【发布时间】:2015-07-10 16:03:25
【问题描述】:

我正在尝试从我在 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]);

    }
  }
};

【问题讨论】:

  • GmailApp.getMessagesForThread 显然期望Thread 之类的东西,而GmailApp.search("label:label name") 返回一个列表。如果返回的列表是消息列表,您可以尝试简单地对其进行迭代(而不是调用 getMessagesForThread)。

标签: regex email google-sheets gmail


【解决方案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);
    }
  }
};

【讨论】:

    【解决方案2】:
    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,上面的意思更清楚了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 2016-04-12
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多