我正在使用改编自Amit Agarwal的脚本文件将我的Gmail附件发送到谷歌驱动器。
我有一个约100人的团队,每人都以MS excel的格式发送每周报告文件作为我的附件。这些电子邮件首先标记为“每周”,脚本文件将通过将其附件保存到Google驱动器来处理每个电子邮件。完成后,电子邮件将标记为“已保存”。
它适用于大多数情况,但每周,当保存过时的每周报告而不是最新报告时,我会有4到10次发生。
这太奇怪了。
function sendToGoogleDrive() {
var gmailLabels = "Weekly";
var driveFolder = "Weekly Report";
var archiveLabel = "Saved";
var moveToLabel = GmailApp.getUserLabelByName(archiveLabel);
if ( ! moveToLabel ) {
moveToLabel = GmailApp.createLabel(archiveLabel);
}
var filter = "has:attachment -label:" + archiveLabel + " label:" + gmailLabels;
var threads = GmailApp.search(filter, 0, 5);
var folder = DriveApp.getFoldersByName(driveFolder);
if (folder.hasNext()) {
folder = folder.next();
} else {
folder = DriveApp.createFolder(driveFolder);
}
for (var x=0; x<threads.length; x++) {
var message = threads[x].getMessages()[0];
var desc = message.getSubject() + " #" + message.getId();
var att = message.getAttachments();
for (var z=0; z<att.length; z++) {
try {
var file = folder.createFile(att[z]);
file.setDescription(desc);
var oldFilename = file.getName();
var newFilename = (Utilities.formatDate(message.getDate(), "GMT+8", "yyyy-MM-dd'T'HH:mm:ss'Z'") + " " + oldFilename);
file.makeCopy(newFilename);
}
catch (e) {
Logger.log(e.toString());
}
}
threads[x].addLabel(moveToLabel);
}
}
我刚刚注意到它可能与消息标题中的“References:”字段有关。我遇到问题的消息在其标题中包含以下行:
.......
Subject: =?GB2312?B?d2Vla2x5IHJlcG9ydC24tb31z7w=?=
References: <201409191817238757191@bekencorp.com>,
<2014092618170700047620@bekencorp.com>,
<201412051803001092350@bekencorp.com>,
<201412121802092504091@bekencorp.com>,
<2014123117571921868416@bekencorp.com>,
<2015010912141701588718@bekencorp.com>,
<201501161011295938041@bekencorp.com>
X-Priority: 3
X-GUID: F196F58C-CD93-4EC3-BAEB-0987910BB9DB
X-Has-Attach: yes
X-Mailer: Foxmail 7, 2, 6, 37[cn]
Mime-Version: 1.0
Message-ID: <2015012318020417198016@bekencorp.com>
.......
而不是保存到gDrive的消息附件&lt; 2015012318020417198016@bekencorp.com> ,它一直保存消息的附件&lt; 201409191817238757191@bekencorp.com>,“参考文献:”中列出的第一条消息