How you can access Google Docs data from Google App Scripts and send an email?

How you can access Google Docs data from Google App Scripts and send an email?

Here we read the document via the Google app script and then send the same data to email.

Lets suppose we have a Resume Template, First we store the template link in the Google Sheet.

To access the Doc, we will write a function in Google App Script. I have included the code below.

function read_google_docs(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('DocLink');
  var doc_url = sheet.getRange('B1').getValues();
  var get_doc = DocumentApp.openByUrl(doc_url);
  var body = get_doc.getBody().getText();
  Logger.log(body);
}

In the code, we first get the sheet and the Doc URL, and then open the document via DocumentApp.

DocumentApp.openByUrl() : Opens and returns the document with the specified URL.

getBody() : Retrieves the active document’s Body.

We get the text of the body.

Now that we have the body text, we will send it to an email. Below you will find the code needed to send the body text.

 GmailApp.sendEmail('[email protected]','Test Subject',body);

Final Code is.

function read_google_docs(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('DocLink');
  var doc_url = sheet.getRange('B1').getValues();
  var get_doc = DocumentApp.openByUrl(doc_url);
  var body = get_doc.getBody().getText();
  GmailApp.sendEmail('[email protected]','Test Subject',body);
}

Now we will create a new Document via Google App Script.

We will begin by getting the text of the body, then we will create a new document, and lastly, we will set the text of the body on that document. The following is the code to create a new document using the Google app script that can be found below.

function create_google_docs(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('DocLink');
  var doc_url = sheet.getRange('B1').getValues();
  var get_doc = DocumentApp.openByUrl(doc_url);
  var body = get_doc.getBody().getText();
 var name = 'Ashish';
 var new_d = DocumentApp.create(name);
 new_d.getBody().setText(body);
 sheet.getRange('B3').setValue(new_d.getUrl());
}
techfixxo Avatar

One response to “How you can access Google Docs data from Google App Scripts and send an email?”

  1. temp mail Avatar

    I do not even know how I ended up here but I thought this post was great I dont know who you are but definitely youre going to a famous blogger if you arent already Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *