Sunday, September 2, 2018

Send emails from Google Sheet

Send emails from Google Sheet

Aim

This Blog post explains you how to send emails from Google Sheet.

Step 01
Open New Google Sheet as shown in the Pic 01.
Pic 01

Step 02

You have to enter sample data to your spread sheet to test our process.Therefore design your spread sheet as shown in the Pic 02.


Pic 02
Step 03

Open script editor by clicking tools menu >> Script editor.
Then you have to type small coding part in script editor.It looks like Pic 03.Please see below image.

Pic 03

Code is as follows;

function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 2; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = row[1]; // Second column
    var emailSent = row[2]; // Third column
    if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
      var subject = 'Sending emails from a Spreadsheet';
      MailApp.sendEmail(emailAddress, subject, message);
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}


Then to test this one run your scriptTo run script,follow the below image (Pic03)





No comments:

Post a Comment

Google Docs-Google Sheet-How to record macros

How to record a macro in Google Sheet Google has recently added to their Google Sheet, macro recording facility.Therefore it allows to u...