如何解析邮件并仅提取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()forgetBody()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
- Update regex patterns: If your emails use a unique format (like
DD-MM-YYYYor10.05.2024), add the corresponding regex to thedatePatternsarray. For example, use/\b\d{2}-\d{2}-\d{4}\b/for DD-MM-YYYY. - Adjust starting row: If your spreadsheet headers are in a different row, change the
rowvariable from2to your desired starting row. - Test with a single email: To avoid overwriting data, test first with one thread by modifying
threadstolabel.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
breakand 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
相关产品推荐
相关产品推荐

