Google Apps Script: Read Google Document as Template

DocsList services provides access to the document list

This services provides access to the document list and the files and folders in the document list. we can create files, folders, and subfolders that appear in the document list.

Here, we are going to read a Google Document as a template by Google Apps Script.

Step 1: Create a Document:

Create a document and write some text that we want to send as a template. For example:

This is a demo template.

Step 2: Create Google Script:

Now, create a Google Script and write the following code:

//Get Template(Document) id defined in the Project Proerties
var documentTemplateID="EmailTemplateDocId"
var emailSubject="Document Template"
var user_emailId='<email_id>'

function sendDocumentTemplate(){
 var mailBody= getDocumentTemplate();
// Sending the document Template
 GmailApp.sendEmail(user_emailId, emailSubject, mailBody);

* It returns the document template

function getDocumentTemplate() {
//Get Document ID from Project Properties
 var templateDocId= ScriptProperties.getProperty(documentTemplateID);
// Make Document's copy that would be send
 var docId= DocsList.getFileById(templateDocId).makeCopy().getId();
 var doc=DocumentApp.openById(docId);
 var body=doc.getActiveSection();
 var html=body.getText();
//Delete the document's copy after send the mail
 return html;

Step 3: Set Project Properties

To read document, first required to mentioned the project name and document id in the project properties.

Go File -> Project Properties -> Project Properties Tab

Click on Add row. Give project name as ‘EmailTemplateDocId’ and its doc id in value field. Doc id is the document id. It displays in document url as  ‘…/document/d/<doc_Id>/edit.

About Ruchi Agarwal

Experienced working in Scala/ Java as a Sr. Software Consultant at Knoldus Software LLP for around 4 years. Having good understanding of various technologies includes Play, AngularJS, MongoDB, LiftWeb, Akka, jQuery, Javascript, CSS etc. Certified in RAD (Rational Application Developer) for WebSphere Software and RFT (Rational Funtional Tester) for Java. Skilled professional focused on the basic business functions and creation of software. Having firm understanding of design methodology. Likes to work with responsibility, dedication and perseverance.
This entry was posted in Web and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s