简体   繁体   中英

How to calculate a variable in Google spreadsheet script, then return in confirmation email

I'm a little new at this so thanks in advance for any help! I've gotten a confirmation email working based on a submission trigger/event for a Google Form to a spreadsheet. However, I want to be able to have the script grab two fields from the user's submission and then calculate a field to be included in the confirmation email. Once I added in the code to calculate the field (cost1), the confirmation email stopped sending. I checked the execution transcript and it ran much more quickly and said it executed successfully, but no email sent... Is there something wrong with my code or am I asking for the script to do too much?

The script:

function EmailFormConfirmation() {


  var sheetName = "Form Responses"; 
  var columnNumber = 3;
  var subject = "Summer School Registration Confirmation";
  var gn = 2;
  var sfn = 4;
  var sln = 5; 
  var s1 = 13;
  var s2 = 14;


  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var dataSheet = ss.getSheetByName(sheetName);
  var numRows = dataSheet.getLastRow();
  var email = dataSheet.getRange(numRows,columnNumber).getValue();
  var guardianName = dataSheet.getRange(numRows,gn).getValue();
  var studentFirst = dataSheet.getRange(numRows,sfn).getValue();
  var studentLast = dataSheet.getRange(numRows,sln).getValue();
  var session1 = dataSheet.getRange(numRows,s1).getValue();
  var session2 = dataSheet.getRange(numRows,s2).getValue();
  var emailPattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/; //email reg expression
  var validEmailAddress = emailPattern.test(email); 
  var cost1 = 0
    if(session1 === "Driver's Education Classroom & Behind the Wheel")
    {
      var cost1 = cost1+400
    }
    else
    {
      var cost1 = cost1+180
    }

    if(session2 > 0)
    {
      var cost1 = cost1+180
      return(cost1)
    }
    else
    {
      return(cost1)
    };
  var body = "Dear" + " " + guardianName + "," +
  "\n\nThank you for submitting summer school registration for" + " " + studentFirst + " " + studentLast + "." +
  "\n\nYou have selected the following for Session 1:" + " " + session1 +
  "\n\nYou have selected the following for Session 2:" + " " + session2 +
  "\n\nIf any changes need to be made, please contact the office." +
  "\n\nYour total fees due for summer school are:" + " " + "$" + cost1 +  
  "\n\n\nSummer School Registrar";

  if (validEmailAddress) {

      MailApp.sendEmail(email, subject,body); 
    }

  }  

return is always the last thing to run in a JS functions and is short-circuiting your function before the MailApp.sendEmail is run. Also, variables in JS are function scoped . So, you don't need to declare var inside of your if blocks.

Below I've used the ternary operator [test ? if true : else] [test ? if true : else] , and set the long string to a variable.

Your cost calculation should look something like this:

var dcebw = "Driver's Education Classroom & Behind the Wheel";

var cost1 = session1 === dcebw ? 400 : 180;

cost1 += session2 > 0 ? 180 : 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM