Colin Grant
Colin Grant

Reputation: 7

Send email when cell contains a specific value

I am working on a Google Form that allows our employees to submit an in-field inspection of their equipment. I have a script that takes the form responses and creates a new sheet based on the date and the specific unit number of the equipment. The user goes through a checklist and selects either "Good" or "Needs Repair" for each item on the list. They can also add comments and upload pictures of any issues.

I am trying to have the script automatically send an email if "Needs Repair" is selected for any of the checks, as well as if the user adds a comment or a picture. This way we do not have to open every submitted sheet to know if any repairs are required. What I have is just not sending emails and I cannot figure out why. Any help is greatly appreciated!

Here is my current script:

 function onFormSubmit() {
  // onFormSubmit
  // get submitted data and set variables
  var ss = SpreadsheetApp.openById("*Spreadsheet Link*");
  var sheet = ss.getSheetByName("Submissions");
  var row = sheet.getLastRow();
  var Col = sheet.getLastColumn();
  var headings = sheet.getRange(1,1,1,Col).getValues();
  var lastRow = sheet.getRange(row, 1, 1, Col);
  var UnitNumber = sheet.getRange(row,3).getValue(); 
  var newSheet = sheet.getRange(row,4,Col).getValue();
  var fileExist = false;
  var drillSheet = null;
  var folder = DriveApp.getFoldersByName("Fraser Drill Inspections").next();
  var files = folder.getFilesByName(UnitNumber);
  var file = null;
  var employee = sheet.getRange(row,2);
  var checks = sheet.getRange(row, Col, 1, 20);
  // check if Drill has sheet
  while (files.hasNext())
  {
    fileExist = true;
    file = files.next();
    break;
  }
  if (fileExist) //If spreadsheet exists, insert new sheet
  {
    drillSheet = SpreadsheetApp.openById(file.getId());
    drillSheet.insertSheet("" + newSheet);
  }
  else //create new spreadsheet if one doesn't exist
  {
    drillSheet = SpreadsheetApp.create(UnitNumber);
    var ssID = drillSheet.getId();
    file = DriveApp.getFileById(ssID);
    file = file.makeCopy(UnitNumber, folder);
    DriveApp.getFileById(ssID).setTrashed(true);
    drillSheet = SpreadsheetApp.openById(file.getId());
    drillSheet.renameActiveSheet(newSheet);
  }
  // copy submitted data to Drill sheet

drillSheet.getSheetByName(newSheet).getRange(1,1,1,Col).setValues(headings);
  drillSheet.appendRow(lastRow.getValues()[0]);
  drillSheet.appendRow(['=CONCATENATE(B6," ",B5)']);
  drillSheet.appendRow(['=TRANSPOSE(B1:2)']);
  //Hide top rows with raw data
  var hiderange = drillSheet.getRange("A1:A3");
  drillSheet.hideRow(hiderange);
  //Widen columns
  drillSheet.setColumnWidth(1,390);
  drillSheet.setColumnWidth(2,700);

  //Send email if there are any comments or if anything needs repair
  if(lastRow.getValues() == "Needs Repair") {
    function SendEmail() {
      var ui = SpreadsheetApp.getUi();
      MailApp.sendEmail("[email protected]", "Drill Needs Repair", "This drill requires attention according to the most recent inspection report.")
    }
  }
 }

Upvotes: 0

Views: 146

Answers (1)

David Bieber
David Bieber

Reputation: 98

The function to send an email is:

GmailApp.sendEmail(email, subject, body);

Try changing

if(lastRow.getValues() == "Needs Repair") {
    function SendEmail() {
        var ui = SpreadsheetApp.getUi();
        MailApp.sendEmail("[email protected]", "Drill Needs Repair", "This drill requires attention according to the most recent inspection report.")
    }
}

to just the following:

if(lastRow.getValues() == "Needs Repair") {
    GmailApp.sendEmail("[email protected]", "Drill Needs Repair", "This drill requires attention according to the most recent inspection report.");
}

It looks like you've still got some additional work to do too, e.g. to make it send to the email address from the form submission instead of a hardcoded one.

Upvotes: 1

Related Questions