简体   繁体   English

如何在Google电子表格脚本中计算变量,然后返回确认电子邮件

[英]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. 我收到了一封确认电子邮件,该电子邮件基于将Google表单提交到电子表格的提交触发/事件进行工作。 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. 一旦添加了用于计算字段(cost1)的代码,确认电子邮件便停止发送。 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. return总是在JS函数中运行的最后一件事,并且在运行MailApp.sendEmail之前使函数短路。 Also, variables in JS are function scoped . 同样,JS中的变量是函数范围的 So, you don't need to declare var inside of your if blocks. 因此,您无需在if块内部声明var

Below I've used the ternary operator [test ? if true : else] 下面我使用了三元运算符[test ? if true : else] [test ? if true : else] , and set the long string to a variable. [test ? if true : else] ,然后将长字符串设置为变量。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google Apps Script 电子表格嵌入脚本中使用确认按钮? - How to use a confirmation button in Google Apps Script spreadsheet embedded scripts? Google Spreadsheet-如何在执行脚本之前添加确认消息框 - Google Spreadsheet - how to add confirmation msg box before executing script 如何在谷歌应用脚​​本中以 PDF 格式通过电子邮件发送电子表格的特定部分? - How to I email a certain part of a spreadsheet as PDF in google apps script? Google脚本; 如何对两个日期求和并在Google电子表格中返回结果 - Google Script; How to SUM two date and return the result in google spreadsheet 如何使用Google Apps脚本按电子邮件主题创建电子表格 - How to create a spreadsheet by email subject using Google Apps Script 如何根据谷歌应用程序脚本中的电子表格日期发送电子邮件? - How to send email according to spreadsheet dates in google apps script? 带有URL超链接的Google Script电子邮件确认 - Google Script email confirmation with url hyperlink 谷歌脚本在 email 确认后运行 - Google script run after email confirmation Google Apps 脚本通过电子表格发送电子邮件 - Google Apps Script Send Email via Spreadsheet Google 电子表格脚本 - 不记录用户电子邮件 - Google Spreadsheet Script - does not record user email
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM