Search code examples
google-apps-scriptgoogle-docsgoogle-docs-api

Google Script Send Email with Blob as "Body"


I'm trying to send an email with the data from a certain google doc as the email body, I do NOT want to send it as attachment it specifically has to be in the body as text. I've tried already the following:

 var file = DriveApp.getFileById(fileId);
 var fileBlob = file.getBlob();
 var blobToString = fileblob.toString();
 MailApp.sendEmail(email,subject,blobToString);

It sends an email containing function () { [native code] } in the body.

I cannot seem to figure out how to get the blob as text, I would greatly appreciate any help.


Solution

  • Solution:

    The simplest way to do this is to use DocumentApp methods to get the body of the document and get the text.

    For example, in this very simple Google Docs:

    enter image description here

    You can use this script to get the text. Replace doc-id with the document ID.

    function getText() {
      var document = DocumentApp.openById("<doc-id>");
      var text = document.getBody().getText();
    }
    

    Sample Result: (dump into log)

    enter image description here

    References:

    Class Document

    Class Body

    P.S. Please note that this is a very bare bones solution. Please take your time to explore the references to get text information from other structural elements like tables or headers/footers.