google-apps-script - 谷歌脚本在标签中提取电子邮件的收件人
问题描述
我有自动电子邮件警报,这些警报会进入我的收件箱中的标签(通过回复),也会发送给用户(通过电子邮件的 to: 部分)。
我希望添加到我的脚本中,以便在电子邮件正文中提取信息。最终,我想拉出电子邮件的收件人(在 to: 部分)并将其放在谷歌表的 B 列中。任何见解将不胜感激。
脚本:
// Modified from http://pipetree.com/qmacro/blog/2011/10/automated-email-to-task-mechanism-with-google-apps-script/
// Globals, constants
var LABEL_PENDING = "Example label/PENDING";
var LABEL_DONE = "Example label/DONE";
// processPending(sheet)
// Process any pending emails and then move them to done
function processPending_(sheet) {
// Date format
var d = new Date();
var date = d.toLocaleDateString();
// Get out labels by name
var label_pending = GmailApp.getUserLabelByName(LABEL_PENDING);
var label_done = GmailApp.getUserLabelByName(LABEL_DONE);
// The threads currently assigned to the 'pending' label
var threads = label_pending.getThreads();
// Process each one in turn, assuming there's only a single
// message in each thread
for (var t in threads) {
var thread = threads[t];
// Gets the message body
var message = thread.getMessages()[0].getBody();
// Processes the messages here
orderinfo = message.split("example split");
rowdata = orderinfo[1].split(" ");
// Add message to sheet
sheet.appendRow([rowdata[1]]);
// Set to 'done' by exchanging labels
thread.removeLabel(label_pending);
thread.addLabel(label_done);
}
}
// main()
// Starter function; to be scheduled regularly
function main_emailDataToSpreadsheet() {
// Get the active spreadsheet and make sure the first
// sheet is the active one
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.setActiveSheet(ss.getSheets()[0]);
// Process the pending emails
processPending_(sh);
}
解决方案
You can access the recipient data with the getTo()
function of the message. You could simply add a line that saves itin the column
//Gets the message body and recipent
var message = thread.getMessages()[0].getBody();
var recipient = thread.getMessages()[0].getTo();
//...
// Add message and recipient to sheet
sheet.appendRow([rowdata[1], recipient]);
More info on the function of the gmailMessage class -> https://developers.google.com/apps-script/reference/gmail/gmail-message
推荐阅读
- python - dev_appserver.py 不运行应用程序引擎,而是询问我要如何打开 Windows 10 的 .py 文件
- java - 如何确保我的 Jar 文件只创建和使用一个 derby 数据库?
- java - 在水法迷宫问题中增加价值
- c# - 区分项目加载到列表与滚动事件 Xamarin 表单列表视图
- ios - 如何在 Swift 中使用 URLSession 添加 UIActivityIndicatorView?
- debian - 如何修改 Debian 源代码包以包含附加的可执行脚本?
- python - 使用 Pandas 对价值和时间指数进行下采样或平均数据
- notepad++ - 如何在记事本++中执行某个功能
- javascript - React/Electron 中的“SyntaxError:无效的正则表达式标志”
- java - 如何显示蓝牙设备列表然后连接?是否可以扫描与特征匹配的设备并连接到它?