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.

  1. use gmailmessage.getdate() filter out messages you've handled. store time script runs in scriptproperties , reload threshold next time.
  2. every message has own unique id, recording along body give key up. think slower first option, faster if stored key values in scriptproperties.
  3. use search() match label , time, again keeping track of when script has run in scriptproperties.

    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.)

  4. 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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -