You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解析邮件并仅提取HTML文本中的日期?附现有代码

Extract Dates from HTML Emails in Google Apps Script

Got it, let's fix up your script to pull only dates from those reservation confirmation emails instead of the full plain text body. Here's a tailored solution that builds on your existing code:

Key Changes to Implement

  • Fetch HTML content: Swap out getPlainBody() for getBody() to access the full HTML structure of each email—this lets us target dates embedded in formatted text.
  • Match dates with regex: Use regular expressions to zero in on common date formats (tweak these to match exactly what’s in your emails).
  • Extract & write dates: Pull the matched date value into your spreadsheet, with a clear fallback if no date is detected.

Full Modified Code

var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();

function getEmails() {
  var label = GmailApp.getUserLabelByName("Reservation confirmed");
  var threads = label.getThreads();
  var row = 2; // Start writing from row 2 (assuming row 1 is your header)
  
  // Define regex patterns for date formats—adjust these to match your email's date style!
  const datePatterns = [
    /\b\d{4}-\d{2}-\d{2}\b/,          // YYYY-MM-DD (e.g., 2024-10-05)
    /\b\d{2}\/\d{2}\/\d{4}\b/,          // MM/DD/YYYY (e.g., 10/05/2024)
    /\b(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{1,2},\s+\d{4}\b/i // Full month name (e.g., October 5, 2024)
  ];

  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var m = 0; m < messages.length; m++) {
      const emailHtml = messages[m].getBody();
      let extractedDate = "Date not found";

      // Check each pattern until we find a valid match
      for (const pattern of datePatterns) {
        const match = emailHtml.match(pattern);
        if (match) {
          extractedDate = match[0];
          break; // Stop checking once we find the first valid date
        }
      }

      // Write the extracted date to column A
      sheet.getRange(row, 1).setValue(extractedDate);
      row++;
    }
  }
}

function onOpen() {
  // Add a custom menu to run the script easily from your spreadsheet
  const ui = SpreadsheetApp.getUi();
  ui.createMenu('Reservation Tools')
    .addItem('Extract Confirmation Dates', 'getEmails')
    .addToUi();
}

How to Customize This for Your Emails

  1. Update regex patterns: If your emails use a unique format (like DD-MM-YYYY or 10.05.2024), add the corresponding regex to the datePatterns array. For example, use /\b\d{2}-\d{2}-\d{4}\b/ for DD-MM-YYYY.
  2. Adjust starting row: If your spreadsheet headers are in a different row, change the row variable from 2 to your desired starting row.
  3. Test with a single email: To avoid overwriting data, test first with one thread by modifying threads to label.getThreads(0,1) (grabs only the first thread).

Quick Notes

  • If an email has multiple dates, the script picks the first matching one. To collect all dates, remove the break and store matches in an array.
  • The "Date not found" fallback ensures you don’t end up with empty cells if an email lacks a recognizable date.

内容的提问来源于stack exchange,提问作者Michael Watanabe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:07:28