Austin Gilbert
Austin Gilbert

Reputation: 43

Send Daily Email Trigger

I would like to send out an email every weekday. The list of addresses is specified in one of the sheets. When an event specific trigger is used, the email is sent out as it should. However, when I set it to an time driven trigger, it is not executed properly. I receive the following error regarding the numRows variable I created: Please select an active sheet first. (line 21, file "sendEmails")

    // define how HTML object is being sent: 
function convSheetAndEmail(rng, email, subj)
{
  var HTML = SheetConverter.convertRange2html(rng);
  MailApp.sendEmail(email, subj, '', {htmlBody : HTML});
}

// function 2: 
function doGet()
{  
  var sh1 = SpreadsheetApp.getActive().getSheetByName('Dashboard')
  var dataRange = sh1.getDataRange();
  var sh2 = SpreadsheetApp.getActive().getSheetByName('control_panel')
  var numRows = SpreadsheetApp.getActiveSpreadsheet('Sales/Ops Daily Report').getLastRow()-1;

  var data = sh2.getRange(2,1,numRows,1).getValues()
  
  // loop through the array with the mail addresses:
  for (i in data) {
    var row = data[i];
    var emailAddress = row[0];
    var subject = 'Sales/Ops Daily Report';
    convSheetAndEmail(dataRange, emailAddress, subject);
  }
}

// send email
function createTriggers() {
   var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
               ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,                                            
               ScriptApp.WeekDay.FRIDAY];
   for (var i=0; i<days.length; i++) {
      ScriptApp.newTrigger(doGet())
               .timeBased().onWeekDay(days[i])
               .atHour(14).create();
   }
}

  

Upvotes: 0

Views: 93

Answers (1)

Method ScriptApp.newTrigger() should use a string type argument, i.e. function name. You have used doGet() call as the argument instead. As a result the trigger is not installed correctly. If you intend to trigger this function you should write: ScriptApp.newTrigger("doGet") and so on.

Upvotes: 1

Related Questions