简体   繁体   English

使用Google Apps脚本从Gmail邮件中提取数字

[英]Extracting numbers from Gmail messages using Google Apps Script

I'm trying to extract numbers such as the ones listed below from my Gmail messages using Google Apps Script. 我正在尝试使用Google Apps脚本从我的Gmail邮件中提取下面列出的数字。

2,495.00
1,594
3,777.23
642.00

This is the code: 这是代码:

function myFunction() {

  var sheet = SpreadsheetApp.getActiveSheet();
  var threads = GmailApp.search('subject:(Transaction) after:2016/7/31 before:2016/8/10');

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

    for (var j=0; j<messages.length; j++)
    {
      var content = messages[j].getBody();
      var subject = messages[j].getSubject();
      var date = messages[j].getDate();
      Logger.log(content);

      if (content)
      {
        tmp = content.match(/\d+(,\d+)*(\.\d+(e\d+)?)?/);
        var number = (tmp && tmp[j]) ? tmp[j] : 'No number';
        sheet.appendRow([number, subject, date]);
      }
      else
      {
        sheet.appendRow([content, subject, date]);
      }

    }
  }
}

I've been getting mixed results. 我得到的结果好坏参半。 For some messages this works as intended but for some it completely skips the numbers from the messages. 对于某些消息,这可以按预期工作,但对于某些消息,则完全跳过消息中的数字。 I'm a newbie to JS/GAS and I thought the problem was in the regex but I'm not sure. 我是JS / GAS的新手,我认为问题出在正则表达式中,但我不确定。 Any help in this would be appreciated. 任何帮助,将不胜感激。

you are facing two trouble here: 您在这里面临两个麻烦:
the regex you are using don't look optimised (but neither what you are looking is clear a number like 1,594 should not be found if you are also looking at number that look like that 642.00 ). 您正在使用的正则表达式看起来没有经过优化(但是您所寻找的内容也不是很清楚,如果您还要查看看起来像642.00数字,则不应找到类似1,594数字)。 Nevertheless you could use aa regex like Shekhar Khairnar proposed in comment or something similar (the g at the end is important as there is more than one number in your mail). 不过,您可以使用诸如Shekhar Khairnar在评论中建议的正则表达式或类似的东西(末尾的g很重要,因为您的邮件中有多个数字)。
The second trouble is in the line var number = (tmp && tmp[j]) ? tmp[j] : 'No number'; 第二个麻烦是在行var number = (tmp && tmp[j]) ? tmp[j] : 'No number'; var number = (tmp && tmp[j]) ? tmp[j] : 'No number'; . Why is there a j var in this line? 为什么在此行中有一个j var? j is reference to the for loop --> number of messages, nothing to do with the occurences in your message. j引用了for循环->消息的数量,与消息中的出现无关。
What I can propose you is something like that: 我可以向您建议的是这样的:

function myFunction() {

  var sheet = SpreadsheetApp.getActiveSheet();
  var threads = GmailApp.search('test');
  var re = /(?:\d{1,3}[,])*\d{1,3}\.{0,1}\d{1,3}/g;

  for (var i=0; i<threads.length && i<5; i++) // added a condition because I didn't wanted to have too many results
  {
    var messages = threads[i].getMessages();
    var tmp;

    for (var j=0; j<messages.length; j++)
    {
      var content = messages[j].getPlainBody(); //.getBody();
      var subject = messages[j].getSubject();
      var date = messages[j].getDate();
      //Logger.log(content);

      if (content)
      {
        tmp = content.match(re); // /\d+(,\d+)*(\.\d+(e\d+)?)?/);
        var number = tmp || ['No number']; // result of tmp is either null or an array --> if it's null then it will take the value 'no number'
        Logger.log(number);
        sheet.appendRow([number.join(" | "), subject, date]);
      }
      else
      {
        sheet.appendRow([content, subject, date]);
      }

    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM