简体   繁体   English

使用Google脚本解析Gmail电子邮件并将其传递给Google表格

[英]Parsing Gmail Email using Google Scripting and passing that to Google Sheets

I have an email that is sent to me with data. 我有一封包含数据的电子邮件。 I want to grab this data and put it in a Google Sheet. 我想获取这些数据并将其放入Google表格中。 I have most of the code working the only problem is the regex area where I am not that proficient in. 我的大多数代码都在工作,唯一的问题是我不那么精通的正则表达式区域。

Here is the HTML snipet of the email. 这是电子邮件的HTML代码段。

<table class="m_4348561758375603924m_886074067026015045MsoNormalTable" border="0" cellspacing="0" cellpadding="0" width="408" style="width:306.0pt;border-collapse:collapse">
<tbody>
    <tr style="height:31.5pt">
        <td width="196" nowrap style="width:147.0pt;border:solid windowtext 1.0pt;background:yellow;padding:0cm 5.4pt 0cm 5.4pt;height:31.5pt">
            <p class="MsoNormal" align="left" style="text-align:left"><b><span lang="EN-US" style="font-size:24.0pt;font-family:宋体;color:black">Date<u></u><u></u></span></b></p>
        </td>
        <td width="212" nowrap style="width:159.0pt;border:solid windowtext 1.0pt;border-left:none;background:yellow;padding:0cm 5.4pt 0cm 5.4pt;height:31.5pt">
            <p class="MsoNormal" align="left" style="text-align:left"><b><span lang="EN-US" style="font-size:24.0pt;font-family:宋体;color:black">Reset Code<u></u><u></u></span></b></p>
        </td>
    </tr>
    <tr style="height:31.5pt">
        <td width="196" nowrap style="width:147.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:31.5pt">
            <p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US" style="font-size:24.0pt;font-family:宋体;color:black">2017.8.10<u></u><u></u></span></strong></p>
        </td>
        <td width="212" nowrap style="width:159.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:31.5pt">
            <p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US" style="font-size:24.0pt;font-family:宋体;color:black">123456<u></u><u></u></span></strong></p>
        </td>
    </tr>
    <tr style="height:31.5pt">
        <td width="196" nowrap style="width:147.0pt;border:solid windowtext 1.0pt;border-top:none;padding:0cm 5.4pt 0cm 5.4pt;height:31.5pt">
            <p class="MsoNormal" align="left" style="text-align:left"><b><span lang="EN-US" style="font-size:24.0pt;font-family:宋体;color:black">2017.8.11<u></u><u></u></span></b></p>
        </td>
        <td width="212" nowrap style="width:159.0pt;border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 5.4pt 0cm 5.4pt;height:31.5pt">
            <p class="MsoNormal" align="left" style="text-align:left"><strong><span lang="EN-US" style="font-size:24.0pt;font-family:宋体;color:black">123456<u></u><u></u></span></strong></p>
        </td>
    </tr>
</tbody>
</table>

The data that I need is the date which is 2017.8.10 and its code which is 123456. This is always 6 characters the dates are in pairs. 我需要的数据是日期为2017.8.10及其代码为123456。这始终是6个字符,日期成对出现。

Here is my code for Google Script. 这是我的Google脚本代码。

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Date Code')
      .addItem('Get Codes','DateCode')
      .addToUi();
}

function DateCode() {    
  var sheet = SpreadsheetApp.getActiveSheet();
  var label = GmailApp.getUserLabelByName("Inbox RC");
  var threads = label.getThreads();

  for (var i = 0; i < threads.length; i++) {      
    var tmp,
        message = threads[i].getMessages()[0],
        subject = message.getSubject(),
        content = message.getPlainBody();

    if (content) {    
      tmp = content.match(2017);
      var comment = (tmp && tmp[1]) ? tmp[1] : 'No Code Found!';

      sheet.appendRow([comment]);    
  }
}

Note* Inbox RC is the folder where this email is sent to by a filter on Gmail. 注意*收件箱RC是Gmail过滤器将该电子邮件发送到的文件夹。

Where I need assistance is in the line tmp = content.match(2017); 我需要帮助的地方是tmp = content.match(2017); where 2017 would have the code that I need in order to get the code and date. 在2017年将有我需要的代码才能获取代码和日期。 I would extract two dates with their codes to two dif. 我会将两个日期及其代码提取为两个dif。 fields. 领域。 Which I can handle that later I just need to get the correct data after this script runs. 我可以稍后处理,我只需要在运行此脚本后获取正确的数据即可。 Its picking up words and numbers but its not picking up what I want as it searches the entire folder Ibox RC. 它会选择单词和数字,但不会搜索我想要的内容,因为它会搜索整个文件夹Ibox RC。

You just need a regex to match the date format that you know? 您只需要一个正则表达式来匹配您知道的日期格式? How about something like this: http://regexr.com/3ghlh 这样的事情怎么样: http : //regexr.com/3ghlh

/>(\d{4}\.\d{1,2}\.\d{1,2})<|>(\d+)</g

https://regex101.com/r/qFHLGK/1 https://regex101.com/r/qFHLGK/1

that's the regex - you'll have to split it into pairs after... How do you split an array into array pairs in JavaScript? 这就是正则表达式-您必须在之后将其拆分为对... 如何在JavaScript中将数组拆分为数组对?

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

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