我有以下脚本从我的电子邮件中读取信息并在Google工作表中填充信息。大多数情况下,Pending标签中会有一封电子邮件(脚本应该每分钟运行一次),但无论如何,脚本都不会在Pending标签中提取信息。出于某种原因,该表格填写了来自"完成"的信息。标签。此外,它还填充了重复值。知道这里有什么不对吗?
例如,让我们说Pending标签的电子邮件是johndoe@gmail.com,而Done标签的电子邮件是joesmith@gmail.com,该脚本甚至会从joesmith@gmail.com电子邮件中提取信息。虽然它已经处理完毕了。 johndoe@gmail.com仍会从Pending转为Done,但正确的信息不会转移。
// 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();
var recipient = thread.getMessages()[0].getTo();
// Processes the messages here
orderinfo = message.split("example split");
rowdata = orderinfo[1].split(" ");
// Add message to sheet
sheet.appendRow([rowdata[1], recipient]);
// 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);
}