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:

Welcome!
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
 DocsList.getFileById(docId).setTrashed(true);
 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

Software Consultant at Knoldus Software LLP having around 2 year experience working in Scala and java. She has good understanding of various technologies like LiftWeb, Akka, Amazon EC2, jQuery, javascript, CSS etc. She is RAD (Rational Application Developer) for WebSphere Software and RFT (Rational Funtional Tester) for Java certified. She is a skilled professional focused on the basic business functions and creation of software. She has a firm understanding of design methodology. She likes to work with responsibility, dedication and perseverance, she has a facility to establish good interpersonal relationships and foremost she is loyal to her principles, ethics and always giving to the best of her caliber.
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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s