GmailApp.search不会过滤标签

时间:2014-12-01 23:19:37

标签: google-apps-script

我尝试将所有附件发送到我的Google云端硬盘帐户,但我在搜索GmailApp.search时遇到问题。

我家中有一台相机,每隔5分钟发送一封带有相同主题的电子邮件,每封邮件都附有一个附件,其名称为day-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 个答案:

答案 0 :(得分:1)

看起来您还没有定义moveToLabel,因此在每个循环中都会处理同一组Gmail线程。

在for循环之前添加此行。

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

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

您可以在ctrlq.org获取完整代码段。