THRILLHOUSE
THRILLHOUSE

Reputation: 163

Add a log that records each time email is sent from script

I have a form that sends responses to a spreadsheet. The spreadsheet then sends an email when the response has not been marked as reviewed. I would like keep a log of each time an email is sent from the script.

I have created the spreadsheet and apps script here: https://docs.google.com/spreadsheets/d/1fplP0xdzunRk4e4CRU3-csbyf6iuD8fjpyYUZM2SxfU/edit?usp=sharing

function sendDay3ReminderEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1000;   // Number of rows to process
  var numOfColumns = sheet.getLastColumn();
  var dataRange = sheet.getRange(startRow, 1, numRows, 
numOfColumns);
  var data = dataRange.getValues(); 
  var dt=new Date();
  var dv=new 
Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
  for (var i=0;i<data.length;i++) {
    var row = data[i];
    var reportNumber = row[1];  // 2nd column B
    var emailAddress = row[22];  // 23rd column W
    var ssLink= row[23];  // 24th column X
    var message = ("Report #"+reportNumber+" on the Bully Report 
 Response Spreadsheet has been not been reviewed. Please review the 
issue and indicate its status on the response spreadsheet found 
here: "+ssLink);      // sixth column
    var d=new Date(row[21]);// 22nd column V
   if (!row[0] && new 
Date(d.getFullYear(),d.getMonth(),d.getDate()).valueOf()<=dv) {
      var subject = ("Your attention is needed on Bullying Report 
Number "+reportNumber);
       MailApp.sendEmail(emailAddress, subject, message);
    }
  }
}

My goal is to record something each time the script is run and an email is sent out.

Upvotes: 1

Views: 587

Answers (1)

Jacques-Guzel Heron
Jacques-Guzel Heron

Reputation: 2598

As a workaround we suggest to send a second email after the first one, one which only purpose if to track the operation (so it will contains date, time, report number, recipient of the first email…). You can send this second email to your own mail account or a log-mail. Please, take into consideration inbox size quotas to prevent lost logs. We hope that this workaround is useful for you. Don't hesitate to add information so we can help you better.

UPDATE

After studying new approaches, I now recommend to use the logging environment. As you can see on the documentation, there are many ways to store the logs inside Apps Script. A basic one is to use the getLog method to write down the logs (i.e. on a document, spreadsheet, mail…). You can use stackdriver logs to save a line each time that a mail is sent; this option is specially useful if your project is on Google Cloud. There is also the Properties Service, that provides a handful of operations for logging in various scenarios; as detailed on its documentation. Be aware of logging quotas to prevent lost information. Please, feel free to write if you need help in this new approach or my information is not clear enough.

Upvotes: 1

Related Questions