在 Google 张中记录收到的 Gmail 邮件
Record incoming Gmail messages in Google sheets
大家好。
我刚开始学习 GS。
我不明白为什么数据没有写入 Google 表。
一切都被正确记录。
我运行脚本通过一个容器,g运行ted访问。
我需要这个脚本只接收未读消息并将它们标记为已读。
如果你能纠正我的脚本,我会很高兴。
function Gmail() {
//this is just the stuff that recognizes what spreadsheet you're in
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getSheetByName('sheet2'); //gets the right sheet
/* searches your GMail for emails matching things "label:unread" + " label:support"
(support is the name of the folder into which letters are collected) */
var query = "label:unread" + " label:support";
var threads = GmailApp.search(query);
var supportStats = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var from = messages[m].getFrom(); //from field
var to = messages[m].getTo(); //to field
var time = messages[m].getDate(); //date field
var subject = messages[m].getSubject(); //subject field
var body = messages[m].getPlainBody(); //body field
var mId = messages[m].getId(); //id field to create the link later
if (query === "label:unread" + " label:support") {
supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
messages[m].markRead(); // marks as read
}
}
if(!threads.length) return; // if there are no unread ones, do nothing
sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,supportStats.
length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
}
解释:
- 主要问题之一是您的代码获得了 html body
包含大量文本,因为它 returns 消息的 html body。
- 但是,如果您仍想获得 body,我建议您改用普通的 body。为此,您可以使用 getPlainBody(),它为您提供了此消息的 body 内容,但没有 HTML 格式。
- 您还可以在 for 循环中调用
appendRow()
,这是一种计算成本非常高的方法。相反,我建议您使用 setValues().
解决方案:
function myFunction() {
//this is just the stuff that recognizes what spreadsheet you're in
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getSheetByName("data"); //gets the right sheet
//this chunk gets the date info
var today = new Date();
var dd = today.getDate()-1;
var mm = today.getMonth()+1; //January is 0 DO NOT FORGET THIS
var yyyy = today.getFullYear();
var yesterday = yyyy + '/' + mm + '/' + dd;
//****************************************************
/*searches your GMail for emails matching two things, written after yesterday and with the label support*/
var query = "label:unread after:" + yesterday + " label:support";
var threads = GmailApp.search(query);
var supportStats = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
messages[m].markRead();
var from = messages[m].getFrom(); //from field
var to = messages[m].getTo();//to field
var time = messages[m].getDate();//date field
var subject = messages[m].getSubject();//subject field
var body = messages[m].getPlainBody();//body field
var mId = messages[m].getId();//id field to create the link later
var mYear = time.getFullYear();
var mMonth = time.getMonth()+1;
var mDay = time.getDate();
var messageDate = mYear + '/' + mMonth + '/' + mDay;
if (messageDate === yesterday) {
supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
}
SpreadsheetApp.getActiveSheet().getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,1,supportStats.length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
}
大家好。 我刚开始学习 GS。 我不明白为什么数据没有写入 Google 表。 一切都被正确记录。 我运行脚本通过一个容器,g运行ted访问。 我需要这个脚本只接收未读消息并将它们标记为已读。
如果你能纠正我的脚本,我会很高兴。
function Gmail() {
//this is just the stuff that recognizes what spreadsheet you're in
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getSheetByName('sheet2'); //gets the right sheet
/* searches your GMail for emails matching things "label:unread" + " label:support"
(support is the name of the folder into which letters are collected) */
var query = "label:unread" + " label:support";
var threads = GmailApp.search(query);
var supportStats = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var from = messages[m].getFrom(); //from field
var to = messages[m].getTo(); //to field
var time = messages[m].getDate(); //date field
var subject = messages[m].getSubject(); //subject field
var body = messages[m].getPlainBody(); //body field
var mId = messages[m].getId(); //id field to create the link later
if (query === "label:unread" + " label:support") {
supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
messages[m].markRead(); // marks as read
}
}
if(!threads.length) return; // if there are no unread ones, do nothing
sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,supportStats.
length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
}
解释:
- 主要问题之一是您的代码获得了 html body 包含大量文本,因为它 returns 消息的 html body。
- 但是,如果您仍想获得 body,我建议您改用普通的 body。为此,您可以使用 getPlainBody(),它为您提供了此消息的 body 内容,但没有 HTML 格式。
- 您还可以在 for 循环中调用
appendRow()
,这是一种计算成本非常高的方法。相反,我建议您使用 setValues().
解决方案:
function myFunction() {
//this is just the stuff that recognizes what spreadsheet you're in
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getSheetByName("data"); //gets the right sheet
//this chunk gets the date info
var today = new Date();
var dd = today.getDate()-1;
var mm = today.getMonth()+1; //January is 0 DO NOT FORGET THIS
var yyyy = today.getFullYear();
var yesterday = yyyy + '/' + mm + '/' + dd;
//****************************************************
/*searches your GMail for emails matching two things, written after yesterday and with the label support*/
var query = "label:unread after:" + yesterday + " label:support";
var threads = GmailApp.search(query);
var supportStats = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
messages[m].markRead();
var from = messages[m].getFrom(); //from field
var to = messages[m].getTo();//to field
var time = messages[m].getDate();//date field
var subject = messages[m].getSubject();//subject field
var body = messages[m].getPlainBody();//body field
var mId = messages[m].getId();//id field to create the link later
var mYear = time.getFullYear();
var mMonth = time.getMonth()+1;
var mDay = time.getDate();
var messageDate = mYear + '/' + mMonth + '/' + mDay;
if (messageDate === yesterday) {
supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
}
SpreadsheetApp.getActiveSheet().getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,1,supportStats.length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
}