0
votes

Scripting question.

We have a form that inserts the data into a response sheet. Inside the response sheet, we have a script that emails to a set group of people.

Script-

     */
     
    function Initialize() {
     
      try {
     
        var triggers = ScriptApp.getProjectTriggers();
     
        for (var i in triggers)
          ScriptApp.deleteTrigger(triggers[i]);
     
        ScriptApp.newTrigger("EmailGoogleFormData")
          .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
          .onFormSubmit().create();
     
      } catch (error) {
        throw new Error("Please add this code in the Google Spreadsheet");
      }
    }
     
    function EmailGoogleFormData(e) {
     
      if (!e) {
        throw new Error("Please go the Run menu and choose Initialize");
      }
     
      try {
     
        if (MailApp.getRemainingDailyQuota() > 0) {
     
          // You may replace this with another email address
          var email = "THIS IS WHERE THE EMAIL ADDRESS GOES...REMOVED FOR PRIVACY";
     
          // Enter your subject for Google Form email notifications
          var subject = "Copier Service Request";
     
          var key, entry,
            message = "",
            ss = SpreadsheetApp.getActiveSheet(),
            cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
     
          // Iterate through the Form Fields
          for (var keys in cols) {
     
            key = cols[keys];
            entry = e.namedValues[key] ? e.namedValues[key].toString() : "";
     
            // Only include form fields that are not blank
            if ((entry !== "") && (entry.replace(/,/g, "") !== ""))
              message += key + ' :: ' + entry + "\n\n";
          }
     
          MailApp.sendEmail(email, subject, message);
        }
      } catch (error) {
        Logger.log(error.toString());
      }
    }
     

However, What I also need is that when the form is submitted, in some way shape or form, it will send an email to a specified user. This could possibly be specified in the form itself, or via a pop-up. It does not matter. However, my skills in getting it to email a user, specified while filling out the form are lacking. That is just above my knowledge.

I am thinking the easiest way is to keep the code, but inside the email address variable, add another variable that is filled in through a certain selection in the form itself. Like, if I typed in [email protected] under an "Email" section, it would find that address, and insert itself into the variable in the email address section.

1

1 Answers

0
votes

This is fairly easy to achieve since you already have a lot of the infrastructure from the form submit trigger once you added it to the form.

email = email + "," + e.namedValues["Email"];