如何通过MailApp永久删除脚本中的邮件?

时间:2017-06-01 11:57:53

标签: google-apps-script

我在这个论坛中找到了以下脚本,并且工作非常顺利(获取文件,将文件保存在正确的文件夹中等)。即我收到一个TXT。每天早上使用我要导入的相对数量文件并保存到我的Gdrive(稍后在其他脚本中使用)。该文件以今天的日期命名,我使用了Utilities.formatDate函数来获取。

不幸的是,每次我运行脚本时,我都会从前几天获得所有文件,这些文件会在几天内将我的文件夹垃圾邮件发送完毕。因此,我的问题是,是否可以删除我的Google搜索查询在第二天找不到之前的电子邮件的脚本后的电子邮件?

对这个难题的任何帮助都表示赞赏!!

P.S:电子邮件来自同一收件人,主题相同的主题为“Stock_Load”,但附件名为“date”_Todays Date。

    // GLOBALS
//Array of file extension which you would like to extract to Drive
var fileTypesToExtract = ['txt'];
//Name of the folder in google drive i which files will be put
var folderName = 'GmailToDrive';
//Name of the label which will be applied after processing the mail message
var labelName = 'GmailToDrive';



function GmailToDrive(){
  //build query to search emails
  var query = 'filename:stock_'+Utilities.formatDate(new Date(), "GMT", "yyyyMMdd").toString()+'.gpg.txt';
  //filename:jpg OR filename:tif OR filename:gif OR fileName:png OR filename:bmp OR filename:svg'; //'after:'+formattedDate+
  for(var i in fileTypesToExtract){
 query += (query == '' ?('filename:'+fileTypesToExtract[i]) : (' OR filename:'+fileTypesToExtract[i]));
  }
  query = 'in:inbox has:nouserlabels is:unread' + query;
  var threads = GmailApp.search(query);
  var label = getGmailLabel_(labelName);
  var Folder = DriveApp.getFolderById("XXX");
  for(var i in threads){
 var mesgs = threads[i].getMessages();
 for(var j in mesgs){
   //get attachments
   var attachments = mesgs[j].getAttachments();
   for(var k in attachments){
     var attachment = attachments[k];
     var isImageType = checkIfImage_(attachment);
     if(!isImageType) continue;
     var attachmentBlob = attachment.copyBlob();
     var file = DriveApp.createFile(attachmentBlob);
     Folder.addFile(file);
   }
 }
 threads[i].addLabel(label);

  }
};


//This function will get the parent folder in Google drive
function getFolder_(folderName){
  var folder;
  try {folder = DriveApp.getFolder(folderName)}
  catch(e){ folder = DriveApp.createFolder(folderName);}
  return folder;
}

//getDate n days back
// n must be integer
function getDateNDaysBack_(n){
  n = parseInt(n);
  var today = new Date();
  var dateNDaysBack = new Date(today.valueOf() - n*24*60*60*1000);
  return dateNDaysBack;
}

function getGmailLabel_(name){
  var label = GmailApp.getUserLabelByName(name);
  if(label == null){
 label = GmailApp.createLabel(name);
  }
  return label;
}

//this function will check for filextension type.
// and return boolean
function checkIfImage_(attachment){
  var fileName = attachment.getName();
  var temp = fileName.split('.');
  var fileExtension = temp[temp.length-1].toLowerCase();
  if(fileTypesToExtract.indexOf(fileExtension) != -1) return true;
  else return false;
}

0 个答案:

没有答案