How to Filter Emails in GMail and Read Email Body Using GAS?

 How to Filter Emails in GMail and Read Email Body Using GAS?

function getThreadSummariesV3(){
  const threads=GmailApp.getInboxThreads();//Reads first 500 emails only.
  //Logger.log(threads.length)
  let pValue=0;
  let myBody='';
  let mySubject='';
  let booSub='';
  //let recCount=0;
  const sh=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3')
  threads.forEach(function(thread){
    //recCount+=1;
    const lRow=sh.getLastRow()+1;
    //if (recCount<100){
      const threadId=GmailApp.getThreadById(thread.getId());
      const threadMsgs=threadId.getMessages();
      threadMsgs.forEach(function(threadMsg){
        mySubject=threadMsg.getSubject();
        //Logger.log(mySubject);
        booSub=isPresent(mySubject);
        //Logger.log('booSub is '+booSub);
        if(threadMsg.getFrom()=='YouTube <noreply@youtube.com>'){
          //Logger.log(threadMsg.getPlainBody().length);
          Logger.log(booSub);
          if(booSub==true){ //if(booSub='true') here 'true' in bracket was not giving the result.
            if(threadMsg.getPlainBody().length>5000){
              return;
            }
            sh.getRange(lRow,1).setValue(threadMsg.getFrom());
            sh.getRange(lRow,2).setValue(threadMsg.getCc());
            sh.getRange(lRow,3).setValue(threadMsg.getDate());
            //Logger.log(threadMsg.getPlainBody());
            myBody=threadMsg.getPlainBody();
            pValue=myFind(myBody);
            //Logger.log(myBody);
            //Logger.log(pValue);
            //Logger.log(left(myBody,pValue))
            sh.getRange(lRow,4).setValue(left(myBody,pValue));
            sh.getRange(lRow,5).setValue(threadMsg.getId());
          }
        }
      })
    //}
  })
}

function myFind(abc){
  //const abc='Sujeet is my Name.'
  //Logger.log(abc);
  let placeValue=0;
  //Logger.log(abc.length)
  let i=0;
  for (let j=0j<=abc.length-1;j+=1){
    //Logger.log(abc[j]);
    if(abc[j]==" "){
      i+=1;
    }
    if(i==2){
      placeValue=j;
      break;
    }
  }
  return placeValue
  //Logger.log(placeValue);
}

function isPresent(lmv){
  //const lmv='Avinash Jadhav has subscribed to you on YouTube!';
  const boo=lmv.includes('subscribed',1);
  //Logger.log(boo);
  return boo;
}


function right(strchr) {
  return str.slice(str.length-chr,str.length);
}
 
function left(strchr) {
  return str.slice(0chr - str.length);
}

Comments

Popular posts from this blog

Send Email with Image/s in Mail Body Using GAS-2

Copy Paste Data, Remove Filter, Apply Filter, Delete Filtered Data

Send Email with Image/s in Mail Body Using GAS-1