Sample Code Google Apps Script

Create a Doc on the fly

function myFunction1() {
const doc = DocumentApp.create('Test 1');
}

Create a Doc add some content

function myFunction2(){
const doc = DocumentApp.create('Test 2');
const body = doc.getBody();
Logger.log(body);
body.appendParagraph('Hello World');
}

Select a Doc add content to existing Google Doc

function myFunction3(){
const id = '1Rznazp0sPf9eSxFmHRHrWFK0H4lmju6FXDbuI3gZwLk';
const doc = DocumentApp.openById(id);
const body = doc.getBody();
const para = body.appendParagraph('Hello World 2');
para.appendText('new text');
Logger.log(para);
}

Add Content to existing Doc, rename existing Doc, Get Doc content as text, Send Doc content to Email address of current user.

function myFunction4(){
const email = Session.getActiveUser().getEmail();
Logger.log(email);
const id = '1Rznazp0sPf9eSxFmHRHrWFK0H4lmju6FXDbuI3gZwLk';
const doc = DocumentApp.openById(id);
const body = doc.getBody();
let emailContent = body.editAsText().getText();
Logger.log(doc.getName());
//doc.setName('my email doc');
const subject = doc.getName();
const url = doc.getUrl();
emailContent += ' ' + url;
GmailApp.sendEmail(email,subject,emailContent);}

Leave a Comment