gpt4 book ai didi

regex - 使用 Google 脚本从电子邮件正文中提取信息

转载 作者:行者123 更新时间:2023-12-01 09:55:01 26 4
gpt4 key购买 nike

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

}
}
};

最佳答案

谢谢大家..这成功了:

// 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);
}
}
};

关于regex - 使用 Google 脚本从电子邮件正文中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31345400/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com