google apps script - GmailApp.search doesn't filter the labels -
i try send attachments google drive account have problem search of gmailapp.search.
i have camera in home sends email every 5 minutes same subject , each message has attached file that's name day-time.jpg
first think in search label , of process, put new label mark , don't repeat message, receive message.
i tried test many ways , received message has label:processed.
function () { //all message of camera have label googledrive var threads = gmailapp.search("has:attachment -label:processed label:googledrive", 0, 5); var folder = getfolder(drivefolder); (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(); (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 processed label } }
regards
i have function. use ctrlq.org code base, put principal function.
now copy script
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 <a href="www.labnol.org/internet/send-gmail-to-google-drive/21236/#premium">premium edition of send google drive</a> , unlock new features. can opt one-on-one support via email, skype or google hangouts.') .settitle("send 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 + ")"); (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 + ")"); (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 running. can close sheet", browser.buttons.ok); } function init() { return; } function onopen() { var menu = [ {name: "help , 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 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(); (var = 0; < triggers.length; i++) { scriptapp.deletetrigger(triggers[i]); } if (!e) { browser.msgbox("script stopped", "you can start script anytime later!", browser.buttons.ok); } }
my inbox have these mails:
the problem run script mail has new label (now send trash too), in next run gmailapp.search, find mail have processed label.
i copy configuration sheet
it looks have not defined movetolabel , hence same set of gmail threads getting processed in each loop.
add line before loop.
var movetolabel = gmailapp.getuserlabelbyname("processed"); if ( ! movetolabel ) { movetolabel = gmailapp.createlabel(processed); }
you can full snippet @ ctrlq.org.
Comments
Post a Comment