地图函数不传递列表中的最后一项
Map function does not pass the last item in the list
我创建了一个功能,可以在 Gmail 中搜索我的收件箱,然后将每封邮件分为 3 个类别(姓名、电子邮件和 body)。然后它将它粘贴到 Google Sheet 的 3 列中。但是,当我 运行 时,它只会将姓名和电子邮件粘贴到前两列。问题在于 body。即使,当我 运行 Logger.log(d.getPlainBody());
它显示了我正在寻找的 body 文本。
我使用的代码:
// extract emails from label in Gmail
function extractEmails() {
// get the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get all email threads that match label from Sheet
var threads = GmailApp.getInboxThreads();
// get all the messages for the current batch of threads
var messages = GmailApp.getMessagesForThreads(threads);
var emailArray = [];
// get array of email addresses
messages.forEach(
function(message) {
message.forEach(
function(d) {
emailArray.push(d.getFrom(),d.getTo(),d.getPlainBody());
Logger.log(d.getPlainBody());
});
});
// de-duplicate the array
var uniqueEmailArray = emailArray.filter(function(item, pos) {
return emailArray.indexOf(item) == pos;
});
var cleanedEmailArray = uniqueEmailArray.map(
function(el) {
var matches = el.match(/\s*"?([^"]*)"?\s+<(.+)>/);
if (matches) {
name = matches[1];
email = matches[2];
body = matches[3];
}
return [name, email, body];
});
// clear any old data
sheet.getRange(2,1,sheet.getLastRow(), 3).clearContent();
// paste in new names and emails
var printing = sheet.getRange(2 ,1,cleanedEmailArray.length, 3);
printing.setValues(cleanedEmailArray);
}
您的逻辑似乎有问题,尤其是在地图功能上。您实际上是在匹配每个元素,而不是每条消息。而且您的正则表达式仅捕获 2 种模式,因此预计 matches[3]
到 return 空白。
我稍微修改了你的代码。他们是:
- 将变量作为数组(消息)的元素传递,并删除
getTo
因为您没有使用它。但如果你是,请随时再次添加。
// get array of email addresses
messages.forEach(function(message) {
message.forEach(function(d) {
// Push the data as array to easily process them
// Removed getTo as you only are getting name and email from getFrom data based on your regex
emailArray.push([d.getFrom(),d.getPlainBody()]);
});
});
- Map 函数已替换为更简单的
forEach
。这是可能的,因为我们将消息数据作为上面的数组。
var cleanedEmailArray = [];
uniqueEmailArray.forEach(function(message){
// match name and email from pattern "name <email>" in d.getFrom()
// only pass match[1] and match[2], since match[0] = message[0]
[, name, email] = message[0].match(/\s*"?([^"]*)"?\s+<(.+)>/);
// get body from d.getPlainBody()
body = message[1];
cleanedEmailArray.push([name, email, body])
});
- (吹毛求疵)你没有使用
printing
所以我合并了 2 行
// paste in new names and emails
sheet.getRange(2 ,1,cleanedEmailArray.length, 3).setValues(cleanedEmailArray);
这就是您的代码的样子:
代码:
function extractEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var threads = GmailApp.getInboxThreads();
var messages = GmailApp.getMessagesForThreads(threads);
var emailArray = [], cleanedEmailArray = [];
messages.forEach(function(message) {
message.forEach(function(d) {
emailArray.push([d.getFrom(),d.getPlainBody()]);
});
});
var uniqueEmailArray = emailArray.filter(function(item, pos) {
return emailArray.indexOf(item) == pos;
});
uniqueEmailArray.forEach(function(message){
[, name, email] = message[0].match(/\s*"?([^"]*)"?\s+<(.+)>/);
body = message[1];
cleanedEmailArray.push([name, email, body])
});
sheet.getRange(2,1,sheet.getLastRow(), 3).clearContent();
sheet.getRange(2 ,1,cleanedEmailArray.length, 3).setValues(cleanedEmailArray);
}
输出:
如果有人来这里,我已经修改了 NaziA 的一些代码并创建了一个 InboxScraper(The article has step by step with many images). I leave the code below. From the google app script connected to your Google Sheets it will access your Gmail and Scrape email (Column A) and body (Column B). I have posted images in an article。
function extractEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var threads = GmailApp.getInboxThreads();
var messages = GmailApp.getMessagesForThreads(threads);
var emailArray = [], cleanedEmailArray = [];
messages.forEach(function(message) {
message.forEach(function(d) {
emailArray.push([d.getFrom(),d.getPlainBody()]);
});
});
var uniqueEmailArray = emailArray.filter(function(item, pos) {
return emailArray.indexOf(item) == pos;
});
uniqueEmailArray.forEach(function(message){
[, name, email] = message[0].match(/\s*"?([^"]*)"?\s+<(.+)>/);
body = message[1];
cleanedEmailArray.push([name, email, body])
});
sheet.getRange(2,1,sheet.getLastRow(), 3).clearContent();
sheet.getRange(2 ,1,cleanedEmailArray.length, 3).setValues(cleanedEmailArray);
}
我创建了一个功能,可以在 Gmail 中搜索我的收件箱,然后将每封邮件分为 3 个类别(姓名、电子邮件和 body)。然后它将它粘贴到 Google Sheet 的 3 列中。但是,当我 运行 时,它只会将姓名和电子邮件粘贴到前两列。问题在于 body。即使,当我 运行 Logger.log(d.getPlainBody());
它显示了我正在寻找的 body 文本。
我使用的代码:
// extract emails from label in Gmail
function extractEmails() {
// get the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get all email threads that match label from Sheet
var threads = GmailApp.getInboxThreads();
// get all the messages for the current batch of threads
var messages = GmailApp.getMessagesForThreads(threads);
var emailArray = [];
// get array of email addresses
messages.forEach(
function(message) {
message.forEach(
function(d) {
emailArray.push(d.getFrom(),d.getTo(),d.getPlainBody());
Logger.log(d.getPlainBody());
});
});
// de-duplicate the array
var uniqueEmailArray = emailArray.filter(function(item, pos) {
return emailArray.indexOf(item) == pos;
});
var cleanedEmailArray = uniqueEmailArray.map(
function(el) {
var matches = el.match(/\s*"?([^"]*)"?\s+<(.+)>/);
if (matches) {
name = matches[1];
email = matches[2];
body = matches[3];
}
return [name, email, body];
});
// clear any old data
sheet.getRange(2,1,sheet.getLastRow(), 3).clearContent();
// paste in new names and emails
var printing = sheet.getRange(2 ,1,cleanedEmailArray.length, 3);
printing.setValues(cleanedEmailArray);
}
您的逻辑似乎有问题,尤其是在地图功能上。您实际上是在匹配每个元素,而不是每条消息。而且您的正则表达式仅捕获 2 种模式,因此预计 matches[3]
到 return 空白。
我稍微修改了你的代码。他们是:
- 将变量作为数组(消息)的元素传递,并删除
getTo
因为您没有使用它。但如果你是,请随时再次添加。
// get array of email addresses
messages.forEach(function(message) {
message.forEach(function(d) {
// Push the data as array to easily process them
// Removed getTo as you only are getting name and email from getFrom data based on your regex
emailArray.push([d.getFrom(),d.getPlainBody()]);
});
});
- Map 函数已替换为更简单的
forEach
。这是可能的,因为我们将消息数据作为上面的数组。
var cleanedEmailArray = [];
uniqueEmailArray.forEach(function(message){
// match name and email from pattern "name <email>" in d.getFrom()
// only pass match[1] and match[2], since match[0] = message[0]
[, name, email] = message[0].match(/\s*"?([^"]*)"?\s+<(.+)>/);
// get body from d.getPlainBody()
body = message[1];
cleanedEmailArray.push([name, email, body])
});
- (吹毛求疵)你没有使用
printing
所以我合并了 2 行
// paste in new names and emails
sheet.getRange(2 ,1,cleanedEmailArray.length, 3).setValues(cleanedEmailArray);
这就是您的代码的样子:
代码:
function extractEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var threads = GmailApp.getInboxThreads();
var messages = GmailApp.getMessagesForThreads(threads);
var emailArray = [], cleanedEmailArray = [];
messages.forEach(function(message) {
message.forEach(function(d) {
emailArray.push([d.getFrom(),d.getPlainBody()]);
});
});
var uniqueEmailArray = emailArray.filter(function(item, pos) {
return emailArray.indexOf(item) == pos;
});
uniqueEmailArray.forEach(function(message){
[, name, email] = message[0].match(/\s*"?([^"]*)"?\s+<(.+)>/);
body = message[1];
cleanedEmailArray.push([name, email, body])
});
sheet.getRange(2,1,sheet.getLastRow(), 3).clearContent();
sheet.getRange(2 ,1,cleanedEmailArray.length, 3).setValues(cleanedEmailArray);
}
输出:
如果有人来这里,我已经修改了 NaziA 的一些代码并创建了一个 InboxScraper(The article has step by step with many images). I leave the code below. From the google app script connected to your Google Sheets it will access your Gmail and Scrape email (Column A) and body (Column B). I have posted images in an article。
function extractEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var threads = GmailApp.getInboxThreads();
var messages = GmailApp.getMessagesForThreads(threads);
var emailArray = [], cleanedEmailArray = [];
messages.forEach(function(message) {
message.forEach(function(d) {
emailArray.push([d.getFrom(),d.getPlainBody()]);
});
});
var uniqueEmailArray = emailArray.filter(function(item, pos) {
return emailArray.indexOf(item) == pos;
});
uniqueEmailArray.forEach(function(message){
[, name, email] = message[0].match(/\s*"?([^"]*)"?\s+<(.+)>/);
body = message[1];
cleanedEmailArray.push([name, email, body])
});
sheet.getRange(2,1,sheet.getLastRow(), 3).clearContent();
sheet.getRange(2 ,1,cleanedEmailArray.length, 3).setValues(cleanedEmailArray);
}