Feature of App Script over G-suite

Feature of App Script over G-suite

02 March 2021

Google Apps Script is a quick application development platform that helps to create business applications which are embedded with Google workspace. Write a code in modern JavaScript and get access to built in libraries for favourite Google workspace applications like Gmail, Sites, Drive, Calendar, etc. There is no need for any installation App Script provides a code editor in your browser and script runs on Google servers.

App Script is based on scripting language. Connecting functionality of Google app link documents, spreadsheets, email, drive (and more g suite products).

This is how it looks like

Feature_of_App_Script_over_G-suite_01

We can create and customize functions in App Script as it has its own IDE (Integrated development environment). It can be used as standalone or bound within Google applications.

We can add menus to google docs, sheets and forms. Also, we can add custom functions to google sheets. As, we can create and publish web applications which save time on repetitive tasks. We can use the G sheet as a database for the complex forms. We can interact with a wide variety of google services- docs, sheets, drive, gmail, forms, maps, calendars, sites. To access all these benefits you need to have a google account (@gmail.com). So what we are looking for is to hit the link for a giant platform. In the app script editor you have to run individual functions at a time for the respective operation. Also, while executing function we need to grant permission for the Google app in order to run the operations.

There are two of Scripts

  • Bounded: Its forever/only tied to one google document (doc, sheer, site or forms)
  • Standalone: An independent script not tied to any G suite document.

Now, Let us see how we can set up a mini project for the Gmail services in App Script. This will let you send emails, compose drafts, manager labels, mark messages and threads, and conduct a variety of other Gmail management tasks.

A one line of code can send email to your ID which is

function SendEmail() {

 GmailApp.sendEmail('deepesh.deshmukh@devwebsite.tudip.uk', 'Test email for the Gmail services', 'Please find the email for the Gmail services');

}

Similarly we can create draft email using GmailApp.createDraft(recipient, subject, body) method, also we can add attachment in the draft email or can send directly to recipients using

function makeDraft(){

  var file = DriveApp.getFileById('1sDo0A7SglnjvmhYwwzWogcVjGne7Hn0yRwv6yNPLatc');

  GmailApp.createDraft('deepesh.deshmukh@devwebsite.tudip.uk', 'Test email for the draft attachment', 'Please check out the attachment', {attachments: [file.getAs(MimeType.PDF)], name: 'My document as PDF'}); }

Google has a super cool feature to convert documents into other file format and the default file format for attachment is PDF. Interestingly we can send HTML template email using App Script.

Using the Email services function we can send HTML templates built in email to the recipient. First create a new HTML file in the new app script editor and build the code which you want to share with the recipient. Add that file in the below function let us name it as email

function sendTemp(){

  var emailMessage = HtmlService.createHtmlOutput('email').getContent();

  emailMessage = emailMessage.replace('#TITLE', 'New string value');

  emailMessasge = emailMessage.replace('#MESSAGE', 'Replace message here');

    MailApp.sendEmail('deepesh.deshmukh@devwebsite.tudip.uk', 'New Email Template','', {

                      htmlBody:emailMessage}

                     )

            }

Is it possible to send bulk emails to users instead of one? The answer is Yes. So here we are going to use a spreadsheet which contains a list of users with their email address, first and last name then a message which we want to send on there email. In the below code use the sheet DI in the function so that it will fetch data for the same. Then we have made some adjustments in the code using array so that it will provide new row information.

function sendTemp(arr,x){

  var emailMessage = HtmlService.createHtmlOutputFromFile('email').getContent();

  emailMessage = emailMessage.replace('#TITLE', 'Bulk Mailer Tester');

  emailMessage = emailMessage.replace('#MESSAGE', arr[3]);

  emailMessage = emailMessage.replace('#FIRST', arr[0]);

  emailMessage = emailMessage.replace('#LAST', arr[1]);

  MailApp.sendEmail(arr[2], 'Email Value'+x, '',{

    htmlBody:emailMessage

  })

  Logger.log(emailMessage);

  return true;

}





function bulkEmails(){

  var sheet = SpreadsheetApp.openById('1vmLo11bGfkt4vVUWlf63b6O1Mm7904kM-r_yabGouCc').getSheetByName('Sheet1');

  var range = sheet.getRange(2,1,sheet.getLastRow()-1,sheet.getLastColumn());

  var value = range.getValues();

  //range.sort({column:1,ascending:true})

  for(var x=0;x<value.length;x++){

   Logger.log(value[x]); 

    var row = 2+x;

    var rep = sendTemp(value[x],x);

    sheet.getRange(row, 5, 1, 2).setValues([[rep,Date()]])

  }

}

So this code will send email to all users which are added in the spreadsheet with provided details.

What result it gave to us one email is

Feature_of_App_Script_over_G-suite_02

Surprisingly we can get a count of active chat threads in our queue on your email inbox. Using GmailApp.getChatThreads() function. It will send a count of chat on email.

function chatThreads(){

  var threads = GmailApp.getChatThreads(0,50);

  Logger.log(threads);

  var emailAddress = Session.getActiveUser().getEmail();

  if(threads.length >0 ){

   GmailApp.sendEmail(emailAddress, 'thread tester', threads[0].getMessageCount());

  }

}

Whatever operation is running in the background we can see it in the Log like console.log() app script has Logger .log() which will show all the log of the variable.

Feature_of_App_Script_over_G-suite_03

search
Blog Categories
Request a quote