在Gmail中搜索xls附件

时间:2016-02-14 09:12:36

标签: javascript gmail

使用此代码我用来保存对共享google驱动器文件夹的附件,并在邮件发生更改时收到通知。只要我搜索pdf附件,一切都很好。正如我设置文件:xls返回的消息远远超过预期。不仅调试返回的线程是正确的,而且在Gmail中的搜索字段中插入查询,但getmessages()在这种情况下返回38条消息(和附件!而且不仅是xls而且是pdf)查询参数。 Gmail中的对话视图已停用,与此附件的唯一匹配是from:字段。 我试图改变参考数据(通过添加newer_than),并没有改变;我改变了for循环,没有任何改变。我错过了一些非常愚蠢的东西,或者这是xls的一种错误?

function salvataggioTavoloTecnico(){

var folderName = 'tavoloTecnico';

var p = 0;
var f1s= DriveApp.getFoldersByName(folderName).next().getFiles();
while (f1s.hasNext()) {
   var f1 = f1s.next();
   p++
} Logger.log(p);
var d = new Date();
d.setDate(d.getDate()-1);
var n = d.getFullYear() + "/" + addZero(d.getMonth()+1) + "/" + addZero(d.getDate()); 
var query = 'in:anywhere has:attachment filename:XLS newer_than:5d (from:mariano.casillo@mit.gov.it OR from:donato.castigliego@mit.gov.it OR from:luciano.aloia@mit.gov.it)';
    //var query = 'in:anywhere has:attachment filename:xls '+ 'after:'+n+' (from:mariano.casillo@mit.gov.it OR from:donato.castigliego@mit.gov.it OR from:luciano.aloia@mit.gov.it)';
var elenco = GmailApp.search(query);
var quanteMail = elenco.length; Logger.log(quanteMail);
labelName = GmailApp.createLabel('Movimenti - Tavolo tecnico');   
    for (var i = 0 ; i < quanteMail;  i++) {
    //for ( var i in elenco) {
    elenco[i].addLabel(labelName);
    var thr = elenco[i];
    var nn = thr.getMessageCount();
    Logger.log(nn);
    var mesgs = thr.getMessages();
    var www = mesgs.length;
    for(var j = 0 ; j < www ; j ++){
    var attachments = mesgs[j].getAttachments();
    var rrr = attachments.length;
    for(var k = 0 ; k < rrr ; k ++) {
        var attachment = attachments[k];
        var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
        DriveApp.getFoldersByName(folderName).next().addFile(file);
      }

}
}
var c = 0;
var f2s = DriveApp.getFoldersByName(folderName).next().getFiles();
while (f2s.hasNext()) {
   var f2 = f2s.next();
   c++
 }

if ( c > p) { GmailApp.sendEmail("me@me.com", " avviso "+folderName, "Un nuovo file è stato aggiunto")};

}

1 个答案:

答案 0 :(得分:0)

我仍然不知道为什么按代码搜索会从参数中返回更多消息,但是我通过搜索“错误”消息来解决它只通过解析日期所需的那些消息,就像这样,现在它可以正常工作:

...

var threads = GmailApp.search(query);
        for ( var i in threads) {
       threads[i].addLabel(labelName);
        var mesgs = threads[i].getMessages();
        var www = mesgs.length;
        for(var j = 0 ; j < www ; j ++){
         if ( mesgs[j].getDate() < d ){} else {
        var attachments = mesgs[j].getAttachments();
        var rrr = attachments.length;

       for(var k = 0 ; k < rrr ; k ++) {
        var attachment = attachments[k];
        var attachmentBlob = attachment.copyBlob();
        var file = DriveApp.createFile(attachmentBlob);
        DriveApp.getFoldersByName(folderName).next().addFile(file);
      }
    }
    }
}

.......