0

I'm running a script automatically every months. There are multiple console.log within my script.

I'd like Apps Script to send to me and to 2 other emails the responses from the execution log once it is completed.

The image below should summarize my request (This is just one part of my entire script)

Thanks in advance for your help Sending emails with the console's response once the script is completed

Damien
  • 143
  • 1
  • 10

1 Answers1

1

Try this:

function timeAndDate(){

  now = new Date();
  var firstDayPrevMonth = new Date(now.getFullYear(), now.getMonth()- 1,1);
  var firstUnix = Math.floor(new Date(firstDayPrevMonth).getTime()/1000);
  var lastDayPrevMonth = new Date(now.getFullYear(), now.getMonth(),1);
  var lastUnix = Math.floor(new Date(lastDayPrevMonth).getTime()/1000);

  Logger.log('Starting Date:',firstDayPrevMonth,'- Unix Timestamp format:',firstUnix);
  Logger.log('Ending Date:',lastDayPrevMonth,'- Unix Timestamp format:',lastUnix);
  var body = Logger.getLog();
  var recipient = Session.getActiveUser().getEmail();
  MailApp.sendEmail(recipient,'App script logs',body);

}

Using getLog() when Logging executions using the Logger class allows you to track the execution and with a simple code you can then email it to yourself using the MailApp.sendEmail method.

Gabriel Carballo
  • 1,278
  • 1
  • 3
  • 9
  • Hi @Gabriel Carballo thanks a lot for this. The email sending works (I added another line with MailApp.sendEmail to send the email to my colleague as well). However, all the Logger responses appear empty https://imgur.com/a/5FkBiyK – Damien Aug 11 '22 at 06:07
  • For some reason the image is not being displayed on imgur, make sure that you are using `Logger.log()` when logging the results so the `Logger.getLog()` can actually gather the results and send them with the email. In your example you where using `console.Log()` – Gabriel Carballo Aug 11 '22 at 13:57
  • Hi @Gabriel Carballo If I'm not mistaken, it seems that there is a known issue with logger.getlog() which I'm using in my script https://stackoverflow.com/questions/11539411/how-to-debug-google-apps-script-aka-where-does-logger-log-log-to that would explain why logger.log does not give me any information in the console and email as well – Damien Aug 16 '22 at 03:53