-3

I am really new with this of Google scripts and I need your help.

I have a form that among the fields it has, it has one field named owner this is just a name.

I need to create a script to send a notification to the person that is listed in the owner field when the form is submitted.

I know how to burn an email directly on the script

function myFunction() {
  // Fetch the email address
  var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test").getRange("B2");
var emailAddress = emailRange.getValues();
// Send Alert Email.
var message = 'This is your Alert email!'; // Second column
var subject = 'Your Google Spreadsheet Alert';
MailApp.sendEmail(emailAddress, subject, message);
}

The problem I am lost with is that I know how to recognize the email depending on the name of the owner which is stored in another sheet.

Here is the link of a sample form and here is the sample spreadsheet

Can anyone share some light?

1 Answer 1

0

There are four things for you to understand and research.

1 - Create a script that can be created as an Installable "OnFormSubmit" trigger. Documentation is here.. This will execute every time a form is submitted. This is easy to do, and I have added a screenshot of the add trigger screen at the end of this answer.

2 - Learn about the information captured by an "OnFormSubmit" script. In particular it will return the range of the form submission from which you want the value of Column 7 (the Owner).

3 - The emails sheet contains a separate set of data. You can get it by referencing it with getSheetByName - documentation Ref

4 - You need to look for a match between "Owner and the "Name" value on the "Emails" sheet. There are many options for how to find a match with the owner but looping through the "emails" data is probably the easiest. At each new line you check whether the email name is a match for the "Owner". When you find a match, then you get the accompanying email address (in the cell beside the email name). Then you can send the email as you tested.


This code adapts your existing code to work through the steps mentioned.

function so5524531901(e) {

  // this script as an Installable "OnFormSubmit" Trigger

  //setup the spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  //get the range from OnFormSubmit
  var range = e.range;
  //Logger.log("DEBUG: the range is "+range.getA1Notation());//DEBUG

  // get the data for the range
  var response = range.getValues();

  // get the owner name from the form submission
  var owner = response[0][7];
  Logger.log("DEBUG: Owner = "+owner);// DEBUG

  // get the emails list
  var emailSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Emails");
  // get ALL the data from this sheet
  var emaildata = emailSheet.getDataRange().getValues();
  // check how many rows of data
  var emailLastRow = emailSheet.getLastRow();
  // start the loop through the emails data
  for (var i=1; i<emailLastRow; i++){

    // if owner is equal to Email Name
    if (owner == emaildata[i][0]){
      // there is a match
      //get the email address
      var emailAddress = emaildata[i][1];
      Logger.log("DEBUG: owner = "+emaildata[i][0]+", email address: "+emailAddress);// DEBUG

      // Send Alert Email.
      // Uncomment the following rows to declare the message, subject and then send the email.
      // var message = 'This is your Alert email!'; // Second column
      //var subject = 'Your Google Spreadsheet Alert';
      //MailApp.sendEmail(emailAddress, subject, message);

    }
  }
}

Create an Installable Trigger for OnFormSubmit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.