gpt4 book ai didi

google-apps-script - 谷歌应用程序脚本 Gmail 在没有先前对话的情况下获取消息

转载 作者:行者123 更新时间:2023-12-05 04:18:19 26 4
gpt4 key购买 nike

我正在尝试使用谷歌应用程序脚本将我在特定标签下的电子邮件转储到 Google 文档电子表格。我想将线程中的每个电子邮件正文列为单独的行,这样如果一个线程有 9 条消息的链,则每条消息都单独列出(没有链)在一行中。

我已经设法将它放到每个消息正文及其整个前一个线程的存储位置,在一个单元格中,我可以在一个单元格中获取整个线程。但这不是我想要的。

这段代码会将线程的整个主体排成一行。

function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();

// var threads = GmailApp.getInboxThreads(0, 50);
var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);

var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
for (i = 0; i < 5; ++i)
{
try {
j = messages[i].length; //to process most recent conversation in thread (contains messages from previous conversations as well, reduces redundancy
messageBody = messages[i][j-1].getBody(); //gets body of message in HTML
messageSubject = messages[i][j-1].getSubject();
messageDate = messages[i][j-1].getDate();
messageFrom = messages[i][j-1].getFrom();

Logger.log("Message Subject:" + messageSubject);
Logger.log("Message Date:" + messageDate);
Logger.log("Message From:" + messageFrom);

sheet.getRange(row, 1).setValue(messageFrom);
sheet.getRange(row, 2).setValue(messageSubject);
sheet.getRange(row, 3).setValue(messageDate);
sheet.getRange(row, 4).setValue(getTextFromHtml(messageBody));
row++;
} catch (error) {
spreadsheet.toast("Error Occured. Report it @ http://techawakening.org/", "Status", -1);
}

if (i == threads.length - 1) {
spreadsheet.toast("Successfully loaded emails.", "Status", -1);
spreadsheet.toast("Now mark emails to be forwarded by changing the background color of the cells to green. Then select Forward->Forward selected emails", "Status", -1);
}

}
}

这会将每个消息正文(包括它的前一个线程/消息链)排成一行。

function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();

// var threads = GmailApp.getInboxThreads(0, 50);
var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);

var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array
// messages.length
// jknipp - working except it keeps the thread chain
for (var i = 0; i < threads.length; i++) {
try {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
sheet.getRange(row, 1).setValue(messages[m].getFrom());
sheet.getRange(row, 2).setValue(messages[m].getSubject());
sheet.getRange(row, 3).setValue(messages[m].getDate());
sheet.getRange(row, 4).setValue(getTextFromHtml(messages[m].getBody()));

row++;
}
} catch (error) {
spreadsheet.toast("Error Occured. Report it @ http://techawakening.org/", "Status", -1);
}

if (i == threads.length - 1) {
spreadsheet.toast("Successfully loaded emails.", "Status", -1);
spreadsheet.toast("Now mark emails to be forwarded by changing the background color of the cells to green. Then select Forward->Forward selected emails", "Status", -1);
}
}
}

引用文献 https://stackoverflow.com/a/11034461/39803

最佳答案

通过识别“上一个对话”的开始位置,我只能提取出电子邮件的正文。

var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var LabelWithEmails = sheet.getRange(3, 2).getValue();

function getEmails() {
clearCanvas();
var label = GmailApp.getUserLabelByName(LabelWithEmails);
var threads = label.getThreads();

var row = getFirstRow() + 1;
var firstmessageId = getfirstmsgid();
UserProperties.setProperty("firstmsgid", firstmessageId);
spreadsheet.toast("Loading emails..Please wait. It could take few seconds", "Status", -1);

var messages = GmailApp.getMessagesForThreads(threads); //gets messages in 2D array

for (var i = 0; i < 2;/*threads.length;*/ i++) {
try {
var messages = threads[i].getMessages();

for (var m = 0; m < messages.length; m++) {
var msg = messages[m];
var isForward = msg.getBody().search(/---------- Forwarded message/i) != -1;

if(!isValidMessage) continue;

sheet.getRange(row, 1).setValue(msg.getFrom());
sheet.getRange(row, 2).setValue(msg.getTo() + ";" + msg.getCc() + ";" + msg.getBcc());
sheet.getRange(row, 3).setValue(msg.getSubject());
sheet.getRange(row, 4).setValue(msg.getDate());

if(!isForward) {
// Get only this messages body, ignore the previous chain
var body = msg.getBody();
var firstIndexOfThread = body.search(/gmail_quote/i);
body = (firstIndexOfThread == -1) ? body : body.substring(0, firstIndexOfThread);

sheet.getRange(row, 5).setValue(getTextFromHtml(body));

} else {
// Use the whole body if its a forward
sheet.getRange(row, 5).setValue(getTextFromHtml(msg.getBody()));
sheet.getRange(row, 6).setValue("***");
}

row++;
}
} catch (error) {
Logger.log(error);
spreadsheet.toast("Error Occured - please see the logs.", "Status", -1);
}

if (i == threads.length - 1) {
spreadsheet.toast("Successfully loaded emails.", "Status", -1);
}
}
}

关于google-apps-script - 谷歌应用程序脚本 Gmail 在没有先前对话的情况下获取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16304684/

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