lundi 16 septembre 2019

Issues with textfinder and IF

I have a piece of code that was scrounged together from another code that is known working. What it does is searches a column using textfinder for a value, in this case, yesterday's date, if nothing is found, it should send an email.

function findAndSendMail() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FormResponses');
  var ss2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('AlertDate');
  var search = ss2.getRange("B2").getValue(); //cell has yesterday's date
  var lastRow = ss.getLastRow();
  var range = ss.getRange(1,4,lastRow); //define range for column D, column D contains dates
  //find all occurrences of search key in column D and push range to array
  var ranges = range.createTextFinder(search).findAll();

if (ranges!==null) {
  var message = '';
  }else {
  var message = 'Test';
  }

  var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EmailGroup").getRange("A1");
  var emailAddress = emailRange.getValues();

  var subject = 'Subject';
  var link = "blahblahblah"

  if (message) {
    MailApp.sendEmail(emailAddress, subject,  '**This is an automated message**\n\n' + message + '\n\n**This is an automated message**\n' + link);
  }
}

As you see, it should search column D, then, if it finds something, the message variable will be blank, else if it finds nothing, it will send an email to the email addresses chosen. I'm not sure how the results from a textfinder work with this and I think the way it is written is incorrect. Any help is appreciated, unfortunately, I cannot share the document in question as my company doesn't allow sharing outside of the domain. Thank you!

Aucun commentaire:

Enregistrer un commentaire