Wednesday, 1 April 2015

Google: Google Spreadsheet Email Alert based on the Input Data

The Google script below allows you to automate the Google Spreadsheet to send an email alert based on the input data:

function checkReminder() {
  // get the spreadsheet object
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  // set the first sheet as active
  SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
  // fetch this sheet
  var sheet = spreadsheet.getActiveSheet();
   
  // figure out what the last row is
  var lastRow = sheet.getLastRow();

  // start with row 10
  var startRow = 10;

  // grab column 3 (the 'days left' column)
  var range = sheet.getRange(10,3,lastRow-startRow+1,1 );
  var numRows = range.getNumRows();
  var days_left_values = range.getValues();
   
  // grab the description column
  range = sheet.getRange(10, 6, lastRow-startRow+1, 1);
  var reminder_info_values = range.getValues();
  
  // grab the vendor column
  range = sheet.getRange(10, 5, lastRow-startRow+1, 1);
  var reminder_vendor_values = range.getValues();
   
  var warning_count = 0;
  var msg = "";
   
  // Loop over the days left values
  for (var i = 0; i <= numRows - 1; i++) {
    var days_left = days_left_values[i][0];
    if(days_left == 90) {
      // if it's exactly 90, do something with the data.
      var vendor = reminder_vendor_values[i][0];
      var description = reminder_info_values[i][0];
       
      msg = msg + "Reminder: "+vendor+" "+description+" is due in "+days_left+" days.\n";
      warning_count++;
    }
  }
   
  if(warning_count) {
    MailApp.sendEmail("italert@marlboroughcollege.my",
        "Contract and Agreement Reminder", msg);
  } 
}

1 comment:

  1. Are you paying more than $5 per pack of cigarettes? I'm buying my cigs from Duty Free Depot and this saves me over 50% from cigarettes.

    ReplyDelete