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());
}
Techinfo Avatar

9 responses 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.

  2. AAdil Avatar
    AAdil

    Dude, your post was awesome! Cheers!

  3. ssd Avatar
    ssd

    Hi there! I know this is somewhat off-topic however
    I had to ask. Does operating a well-established blog such as yours require a lot of work?
    I’m completely new to running a blog however I
    do write in my diary daily. I’d like to start a blog so I will be able to share
    my experience and thoughts online. Please let me know
    if you have any recommendations or tips for brand new aspiring blog
    owners. Thankyou!

  4. harrylemos Avatar
    harrylemos

    Normally I don’t read post on blogs, but I would like to say
    that this write-up very compelled me to try and do it! Your writing taste
    has been surprised me. Thanks, very great article.

  5. Brawlhalla bot hack Avatar
    Brawlhalla bot hack

    Having read this I thought it was really informative.
    I appreciate you taking the time and energy to put this content together.
    I once again find myself spending a significant
    amount of time both reading and commenting. But
    so what, it was still worthwhile!

  6. That s warm now trying not be now was Avatar
    That s warm now trying not be now was

    That s warm now trying not be now was

  7. hoor Avatar
    hoor

    Hey! Do you know if they make any plugins to safeguard against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

  8. cock Avatar
    cock

    hey there and thank you for your info – I have definitely picked up something new from right here.

    I did however expertise a few technical points using this site, as
    I experienced to reload the site a lot of times previous to I could
    get it to load properly. I had been wondering if your web host is OK?
    Not that I’m complaining, but slow loading instances
    times will sometimes affect your placement in google and could damage your quality score if advertising and marketing with Adwords.
    Well I am adding this RSS to my email and could look out for a
    lot more of your respective fascinating content. Make sure you update this again soon.

  9. fart Avatar
    fart

    With havin so much content and articles do you ever run into any issues of plagorism or copyright infringement?
    My website has a lot of exclusive content I’ve either created myself or outsourced
    but it appears a lot of it is popping it up all over the internet without my agreement.

    Do you know any solutions to help protect against content from being ripped off?
    I’d genuinely appreciate it.

Leave a Reply

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