google apps script - Copy emails with a specific Gmail label to Spreadsheet, without duplicates -
i trying make script takes email within label , puts google sheets. i'm using function @ moment:
function getmessageswithlabel() { var destarray = new array(); var threads = gmailapp.getuserlabelbyname('facebook').getthreads(0,10); for(var n in threads){ var msg = threads[n].getmessages(); var destarrayrow = new array(); destarrayrow.push('thread has '+threads[n].getmessagecount()+' messages'); for(var m in msg){ destarrayrow.push(msg[m].getsubject()); } destarray.push(destarrayrow); } logger.log(destarray); var ss = spreadsheetapp.getactivespreadsheet(); var sh = ss.getactivesheet(); if(ss.getlastrow()==0){sh.getrange(1,1).setvalue('getmessageswithlabel() results')}; sh.getrange(ss.getlastrow()+1,1,destarray.length,destarray[0].length).setvalues(destarray) }
this works @ grabbing emails in 'facebook' label grabs emails every time, creating duplicates every time runs. create new line every time new 1 put 'facebook' label.
four thoughts explore.
- use
gmailmessage.getdate()
filter out messages you've handled. store time script runs inscriptproperties
, reload threshold next time. - every message has own unique id, recording along body give key up. think slower first option, faster if stored key values in
scriptproperties
. use
search()
match label , time, again keeping track of when script has run inscriptproperties
.var threads = gmailapp.search('label:"facebook" after:"2015/6/1"');
that give threads messages added after given date, threads have messages prior date. you'll still have use 1 of earlier options filter out. (unless, you've indicated in comments, thread have no replies , 1 message.)
once thread processed, remove label used categorize it.
var oldlabel = gmailapp.getuserlabelbyname('facebook'); var newlabel = gmailapp.getuserlabelbyname('facebook-processed'); var threads = oldlabel.getthreads(0,10); for(var n in threads){ ... threads[n].removelabel(oldlabel).addlabel(newlabel); }
Comments
Post a Comment