简体   繁体   中英

Get email from Google Docs

I actually wanted to send MoM on daily basis, so I created a folder where I put all MoM docs and title them with the date for which it was created. Then script reads it on the day and send it to fixed email address, but now we inserted the attendees inside the google docs like this:- 在此处输入图像描述

Is there any way I can get those email address/user email address mentioned there.

Script I was using earlier was this:-

function sendMinutes() {
    const folder = DriveApp.getFolderById('Filder id')
    const today = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'MMM dd,yyyy')
    const files = folder.searchFiles(`title contains '${today}'`)
    while(files.hasNext())
    {
       let file= files.next()
       let doc = DocumentApp.openById(file.getId())
       let body = doc.getBody()
       let parag = body.getParagraphs().forEach(r=> {
          .....
       })
    }
}

I got how to get email but, it is just returning first email now, i need to get every email address as ElementType as PERSON:-

body.findElement(DocumentApp.ElementType.PERSON).getElement().asPerson().getEmail()

Thanks

Body.findElement has several forms, one of them use two parameters, the second one is the start point for finding the next element. Use this method form together with a loop. In the following example a while statement is being used:

function logEmailsFromPersonChips() {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  let found;
  const emails = [];
  while ( found = body.findElement(DocumentApp.ElementType.PERSON,found)){
    const email = found.getElement().asPerson().getEmail();
    emails.push(email);
  }
  console.log(JSON.stringify(emails))
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM