2014-12-01 75 views
0

我嘗試將所有附件發送到我的Google雲端硬盤帳戶,但我在搜索GmailApp.search時遇到問題。GmailApp.search不會過濾標籤

我在我家發送電子郵件每隔5分鐘用相同的主題,並且每個郵件有附加文件,該文件名稱的攝像機是在一天time.jpg

首先我覺得在搜索按標籤那麼在這個過程中,我會添加一個新標籤來標記並不重複該消息,但我總是收到所有消息。

我試着測試很多方法,並總是收到標籤處理的消息。

function() { 
    //All message of the camera have the label GoogleDrive 
    var threads = GmailApp.search("has:attachment -label:processed label:GoogleDrive", 0, 5); 
    var folder = getFolder(driveFolder); 

    for (var x=0; x<threads.length; x++) { 
    var message = threads[x].getMessages(); 

    for(var y=0; y<message.length; y++) { 
     var desc = message[y].getSubject() + " #" + message[y].getId(); 
     var att = message[y].getAttachments(); 


     for (var z=0; z<att.length; z++) { 
     try {   
      if (check) { 
      var name = att[z].getName(); 
      if (name.indexOf(".") != -1) { 
       var extn = name.substr(name.lastIndexOf(".")+1).toLowerCase(); 
       if (valid.indexOf(extn) != -1) { 
       file = folder.createFile(att[z]); 
       file.setDescription(desc); 
       } else { 
       Logger.log("Skipping " + name); 
       } 
      } 
      } else { 
      file = folder.createFile(att[z]); 
      file.setDescription(desc); 
      } 
     } 
     catch (e) { 
      Logger.log(e.toString()); 
     } 
     } 
    } 
    threads[x].addLabel(moveToLabel); //this variable is processed label 
    } 
} 

問候


我有這個功能。我使用ctrlq.org代碼作爲基礎,只有我把主要功能。

現在我複製所有的腳本

function createFilter(label, archiveLabel) { 

    var filter = "has:attachment -label:" + archiveLabel + " label:" + label; 

    return filter; 

} 


function help() { 
    var html = HtmlService.createHtmlOutputFromFile('help') 
    .setTitle("Google Scripts Support") 
    .setWidth(400) 
    .setHeight(260); 
    var ss = SpreadsheetApp.getActive(); 
    ss.show(html); 
} 


function premium() { 
    var html = HtmlService.createHtmlOutput('Upgrade to the <a href="www.labnol.org/internet/send-gmail-to-google-drive/21236/#premium">premium edition of Send to Google Drive</a> and unlock new features. You can also opt for one-on-one support via email, Skype or Google Hangouts.') 
    .setTitle("Send to Google Drive Premium") 
    .setWidth(240) 
    .setHeight(100); 
    var ss = SpreadsheetApp.getActive(); 
    ss.show(html); 
} 


function sendToGoogleDrive() { 

    var sheet = SpreadsheetApp.getActiveSheet(); 

    var gmailLabels = sheet.getRange("D4:D4").getValue(); 
    var driveFolder = sheet.getRange("D5:D5").getValue(); 
    var archiveLabel = sheet.getRange("D6:D6").getValue(); 
    var filetypes = sheet.getRange("D7:D7").getValue(); 

    var valid = filetypes.replace(/\s/g,"").toLowerCase().split(","); 
    var check = true; 

    if ((valid.indexOf("all") != -1) || (valid.length == 1 && valid[0] == "")) { 
    check = false; 
    } 

    var moveToLabel = getGmailLabel(archiveLabel); 

    var filter = createFilter(gmailLabels, archiveLabel); 
    var threads = GmailApp.search(filter, 0, 5); 
    var folder = getFolder(driveFolder); 

    sheet.getRange("D9:D9").setValue(sheet.getRange("D9:D9").getValue() + ", [th(" + threads.length + ")"); 

    for (var x=0; x<threads.length; x++) { 
    var message = threads[x].getMessages(); 
    sheet.getRange("D9:D9").setValue(sheet.getRange("D9:D9").getValue() + ", ms(" + message.length + ")");  
    for(var y=0; y<message.length; y++) { 
     var desc = message[y].getSubject() + " #" + message[y].getId(); 
     var att = message[y].getAttachments(); 

    //sheet.getRange("D9:D9").setValue(sheet.getRange("D9:D9").getValue() + ", at(" + att.length + ")");  
     for (var z=0; z<att.length; z++) { 
     try {   
      if (check) { 
      var name = att[z].getName(); 
      if (name.indexOf(".") != -1) { 
       var extn = name.substr(name.lastIndexOf(".")+1).toLowerCase(); 
       if (valid.indexOf(extn) != -1) { 
       file = folder.createFile(att[z]); 
       file.setDescription(desc); 
       } else { 
       Logger.log("Skipping " + name); 
       } 
      } 
      } else { 
      file = folder.createFile(att[z]); 
      file.setDescription(desc); 
      } 
     } 
     catch (e) { 
      Logger.log(e.toString()); 
     } 
     } 
    } 
    threads[x].moveToTrash(); 
    threads[x].addLabel(moveToLabel); 
    } 
    sheet.getRange("D9:D9").setValue(sheet.getRange("D9:D9").getValue() + "]"); 

} 


function configure() { 

    reset(true); 

    ScriptApp.newTrigger("sendToGoogleDrive").timeBased().everyMinutes(5).create(); 

    Browser.msgBox("Initialized", "The program is now running. You can close this sheet", Browser.Buttons.OK); 

} 

function init() { 
    return; 
} 

function onOpen() { 
    var menu = [  
    {name: "Help and Support »",functionName: "help"}, 
    null, 
    { name: "Step 1: Authorize", functionName: "init"  }, 
    { name: "Step 2: Run Program", functionName: "configure" }, 
    null, 
    { name: "Uninstall (Stop)", functionName: "reset"  }, 
    null, 
    {name: "Upgrade to Premium »",functionName: "premium"}, 
    null 
    ]; 
    SpreadsheetApp.getActiveSpreadsheet() 
    .addMenu("Gmail Attachments", menu); 
} 

function getFolder(parent) { 

    var parentFolder, searchFolder = DriveApp.getFoldersByName(parent); 

    if (searchFolder.hasNext()) { 
    parentFolder = searchFolder.next(); 
    } else { 
    parentFolder = DriveApp.createFolder(parent); 
    } 

    return parentFolder; 

} 


function getGmailLabel(name) { 

    var label = GmailApp.getUserLabelByName(name); 

    if (! label) { 

    label = GmailApp.createLabel(name); 

    } 

    return label; 

} 

function reset(e) { 

    var triggers = ScriptApp.getProjectTriggers(); 

    for (var i = 0; i < triggers.length; i++) { 
    ScriptApp.deleteTrigger(triggers[i]);  
    } 

    if (!e) { 
    Browser.msgBox("Script Stopped", "You can start the script anytime later!", Browser.Buttons.OK); 
    } 

} 

我的收件箱中有這些郵件:

1

問題是,然後我運行郵件有一個新的標籤(現在我送劇本垃圾郵件「),但在下一次運行GmailApp.Search時,請查找已處理標籤的郵件。

我複製配置表

2

回答

1

看起來你還沒有定義moveToLabel,因此同一組的Gmail線程在每個循環得到處理。

在for循環之前添加此行。

var moveToLabel = GmailApp.getUserLabelByName("processed"); 

    if (! moveToLabel) {  
    moveToLabel = GmailApp.createLabel(processed);  
    } 

你可以在ctrlq.org得到完整的代碼片段。