TRY AND CATCH IN GOOGLE APP SCRIPT HOW TI WORKS?

5,198 views
Skip to first unread message

ANDY LOL

unread,
Nov 6, 2022, 10:15:47 AM11/6/22
to Google Apps Script Community

I have a question

 Assuming I have to send 1000 emails, I take the data from a spreadsheet (1000 rows) The problem is that I may have formally wrong mail blocking the script, generating an error, whatever.

Now, I didn't understand a thing, I could do some tests to verify, but still I ask, assuming that the code sending the mail is inserted in the try and catch structure to handle errors,

try { // code... }
catch (err)
{ // error}

How is the natural behavior inside google script?

Let's assume that in line 5 I have an error, I try to send an email, there is an error, system proceeds with the instructions in catch and I manage it simply send an email to myself with the error, but what does the system after this? proceeds from line 6 of the loop, or everything break and stop the same?

Thank you.


Laurie J. Nason

unread,
Nov 6, 2022, 11:53:06 PM11/6/22
to google-apps-sc...@googlegroups.com
Before I get to the send email part, I do a check of the email(s) (in comma separated list) to make sure it’s a valid one - then I’ll log it to a log tab in the spreadsheet - but it doesn’t break if it’s not right - you do have to check that the list isn’t empty once it comes back to your calling function.

// Check to see if the emails provided are actually valid for this student - if they are, returns them in list, if not - logs and moves on to next
function checkValidEmails(emailList){
var emailArr=emailList.split(',');
var newEmailList='';
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
for (var e=0;e<emailArr.length;e++){
if(emailArr[e].match(mailformat)){
// Valid email address
if(newEmailList.length==0){
newEmailList=emailArr[e];
}else{
newEmailList+=','+emailArr[e];
}
}else{
//invalid email address
//log the error but continue on to send emails
}
}
return newEmailList;
}


------ Original Message ------
From "ANDY LOL" <andyth...@gmail.com>
To "Google Apps Script Community" <google-apps-sc...@googlegroups.com>
Date 06/11/2022 18:15:47
Subject [Apps-Script] TRY AND CATCH IN GOOGLE APP SCRIPT HOW TI WORKS?

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/eca59a9d-fde6-4d4c-b494-63c7a49ead86n%40googlegroups.com.

ANDY LOL

unread,
Nov 7, 2022, 12:52:56 PM11/7/22
to Google Apps Script Community

Thanks for the reply. I post in case people can think it's usefull, the test I did before seeing your suggestion. I sent all the emails to myself, in the lines of the spreadsheet. I intentionally mistake line 2 out of 4 lines in all. The first script function only sends 1 mail, the second with try and catch js sends mail num 1, 3 and 4 and sends mail to myself with the error log

So I need simply this kinf of structure and add also something like you suggest to check mail can be wrong inside the stuff

Unfortunately, I have discovered that in mass export imports senseless mails can enter if the developer has not foreseen any control, even if they should do this,
but obviously there are not always these verification checks



function spedisci_mail_senza_try_catch () {
 
// senza try and catch

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("datimail");
  // indico di lavorare su foglio "datimail"
 
  var startRow = 2;  
  // riga di partenza
  var numRows = sheet.getLastRow() -1; // METTERE -1 per togliere l'intestazione dal n° righe da contare
  // cerco ultima riga compilata in sheet
 
  // indico i riferimenti di partenza riga iniziale, colonna iniziale, riga finale, colonna finale
  var dataRange = sheet.getRange(startRow, 1, numRows, 3)
  var data = dataRange.getValues();

 

  // crea ciclo per ogni riga
 
  for (var x=0; x < data.length;  x++) {
   
    var emailAddress = data[x][2];  // 3 colonna
    var message = data[x][1];       // 2 colonna
    var subject = "Messaggio automatico di posta - senza try and catch";

  // crea funzione per scrivere mail
  MailApp.sendEmail(emailAddress, subject, message);
  }


}


function spedisci_mail_uso_try_catch () {

  // uso try and catch
 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("datimail");
  // indico di lavorare su foglio "datimail"
 
  var startRow = 2;  
  // riga di partenza
  var numRows = sheet.getLastRow() -1; // METTERE -1 per togliere l'intestazione dal n° righe da contare
  // cerco ultima riga compilata in sheet
 
  // indico i riferimenti di partenza riga iniziale, colonna iniziale, riga finale, colonna finale
  var dataRange = sheet.getRange(startRow, 1, numRows, 3)
  var data = dataRange.getValues();
 
  // crea ciclo per ogni riga
 
   for (var x=0; x < data.length;  x++) {
   
    var emailAddress = data[x][2];  // 3 colonna
    var message = data[x][1];       // 2 colonna
    var subject = "Messaggio automatico di posta - senza try and catch";
   
    // crea funzione per scrivere mail - usa try and catch
    try {
        MailApp.sendEmail(emailAddress, subject, message);
      } catch (err) {
        Logger.log ('elemento: ' + message + ' - errore: ' + err);
        var body_mail_err = ('elemento: ' + message + ' - errore: ' + err);
        MailApp.sendEmail('MY_EMAIL_DELETE_FOR_PRIVACY', 'segnalazione errore',  body_mail_err );
      }

  }

}
Reply all
Reply to author
Forward
0 new messages