Search code examples
google-apps-scriptgmailgoogle-docs

Get Text Between Tags From a Google Docs File


I need a script that allows me to send each document contained in a folder to a specific email address. Inside every doc there is the email address to which that document needs to be sent.

The email address is incapsulated between two tags.

Example: <<example@example.com>>

I would like the script to search for the text contained between << and >> but I don't know how.

Is anyone able to help?

Thanks a lot!


Solution

  • From your following reply,

    Each document is a Google Docs file. I need to extract the email address which is written in the body of the document, convert the document to PDF, and send it as an attachment to the email address extracted from the original Doc.

    I believe your goal is as follows.

    • You have a folder including Google Document files.
    • Each Google Document has a text like <<example@example.com>>.
    • You want to retrieve the email address of example@example.com from <<example@example.com>> in Google Document, and want to convert the Google Document to PDF format, and then, you want to send an email to the retrieved email address including the PDF file as an attachment file.
    • You want to achieve this using Google Apps Script.

    In this case, how about the following sample script?

    Sample script:

    Please copy and paste the following script to the script editor of Google Apps Script, and set the folder ID of your folder, and save the script.

    function myFunction() {
      const folderId = "###"; // Please set your folder ID.
    
      const folder = DriveApp.getFolderById(folderId);
      const docs = folder.getFilesByType(MimeType.GOOGLE_DOCS);
      const token = ScriptApp.getOAuthToken();
      while (docs.hasNext()) {
        const file = docs.next();
        const id = file.getId();
        const doc = DocumentApp.openById(id);
        const obj = doc.getBody().getText().match(/<<(.+?)>>/);
        if (obj) {
          MailApp.sendEmail({ to: obj[1].trim(), subject: "sample subject", body: "sample body", attachments: [doc.getBlob()] });
        } else {
          console.log(`Email was not found in "${file.getName()}" document file.`);
        }
      }
    }
    
    • When this script is run, Google Document files are retrieved from the folder, and the email address is retrieved from the document body. And, the document is converted to PDF format, and an email is sent to the retrieved email address by including the PDF data.

    • In this sample script, the subject and body of the email are the sample text. Please modify them for your actual situation.

    Note:

    • I thought that in this case, const pdf = doc.getBlob(); and const pdf = UrlFetchApp.fetch(`https://docs.google.com/feeds/download/documents/export/Export?exportFormat=pdf&id=${id}`, { headers: { authorization: "Bearer " + token } }).getBlob(); might be able to be used for converting to PDF format.

    References: