将Gmail附件保存在Google云端硬盘中 [英] Save Gmail attachments on Google Drive
问题描述
我想将特定电子邮件中的gmail附件(.pdf文件)保存在特定的Google云端硬盘文件夹中.我还需要使用由电子邮件的某些字符串组成的字符串来重命名文件.
I would like to save gmail attachments(.pdf files) from a specific email in a specific Google Drive folder. I also need to rename file with a string composed by some string of the email.
我已经使用带有某些功能的Google Apps脚本开发了一个简单的脚本.
I have developed a simple script using Google Apps Script with some functions.
这是我编写的主要功能:
This is the main function I have wrote:
function GmailToDrive() {
var query = '';
query = 'in:inbox from:noreply@agyo.io has:nouserlabels ';
var threads = GmailApp.search(query);
var label = getGmailLabel_(labelName);
var parentFolder;
if (threads.length > 0) {
parentFolder = getFolder_(folderName);
}
var root = DriveApp.getRootFolder();
for (var i in threads) {
var mesgs = threads[i].getMessages();
for (var j in mesgs) {
//get attachments
var attachments = mesgs[j].getAttachments();
var message_body = mesgs[j].getBody();
for (var k in attachments) {
var attachment = attachments[k];
var isDefinedType = checkIfDefinedType_(attachment);
if (!isDefinedType) continue;
var attachmentBlob = attachment.copyBlob();
var file = DriveApp.createFile(attachmentBlob);
file.setName(renameFile_(attachment, message_body))
parentFolder.addFile(file);
root.removeFile(file);
}
}
threads[i].addLabel(label);
}
}
checkIfDefinedType_(attachment)
函数检查附件是否为.pdf文件,而 renameFile_(attachment,message_body)
重命名附件,从电子邮件中提取一些字符串.
The checkIfDefinedType_(attachment)
function checks if the attachments is a .pdf file and the renameFile_(attachment, message_body)
rename the attachment extracting some string from the email.
该脚本似乎已正确开发,但有时我在Google驱动器文件夹中保存了两个或更多相同的附件.
The script seems to be correctly developed but sometimes I have two or more same attachments saved in my google drive folder.
推荐答案
Stefano,如果与从 https://bit.ly/-extractgmail
Stefano, I had the same issue, if this is the same code as adapted from https://bit.ly/-extractgmail
我删除了"for(在fileTypesToExtract中的var i){"行,该行对我造成了重复.它正在为每种文件类型运行查询.
I removed the line "for (var i in fileTypesToExtract) {" which was causing duplicates for me. It was running the query for each of the file types.
这篇关于将Gmail附件保存在Google云端硬盘中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!