【问题标题】:Syntax issue involving a ".replace" function涉及“.replace”函数的语法问题
【发布时间】:2019-05-01 15:21:30
【问题描述】:

所以我有一个收集客户注册数据的 Google 表格。收集的数据中包括学生姓名、学生选择参加的课程和信用卡号码。提交后,我会收到通知。收到通知后,我会转到我的 Google 表格并从信用卡中收取适当的金额。

信用卡扣款后,我想向客户发送一封确认电子邮件,其中包含学生姓名、学生注册参加的课程以及从信用卡中扣除的金额。我的代码似乎工作正常,除了用我定义为变量的实际值替换“模板文本”中的大括号占位符(即 {Name}、{sessions} 和 {ChargeAmount})。



Note:  When I just replace {StudentName} with text like "Joe" it works.  However, {Sessions} does not get replaced and neither does {ChargeAmount}.  I think this is a syntax error.

这是我真正想要但无法开始工作的事情:

var emailText = templateText.replace("{Name}",studentName);templateText.replace("{Sessions}",sessionName);templateText.replace("{ChargeAmount}",ChgAmt);


function SendEmail() {
 // Fetch the email address

var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B2");
var studentRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("C2");
var studentName = studentRange.getValues();
var sessionRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("D2");
var sessionName = sessionRange.getValues();
var emailAddress = emailRange.getValues();
var ChgAmt = Browser.inputBox("Charge Amount");
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1,1).getValue();

  // NOTE!  What I really want to do is replace {Name} with the var "studentName"... not the text "Joe", {Sessions} with the var "sessionName" and {ChargeAmount} with the var "ChgAmt". I can't this to work either!  STRUGGLING!!!

var emailText = templateText.replace("{StudentName}","Joe");templateText.replace("{Sessions}","May - Session#1");templateText.replace("{ChargeAmount}","$500");

// Send Alert Email.
var subject = 'Junior Golf Clinic Registration Receipt';
var message = emailText;

MailApp.sendEmail(emailAddress, subject, message); 


}

这是我现有代码的结果,它没有正确提取我需要的值。

useremail@gmail.com
9:18 AM (37 minutes ago)
to me

This is the email body.

This is the amount charged to credit card {ChargeAmount}.  [Not right.  This should be the actual value represented by the var "ChgAmt"]

This is the student's name: Joe  [this seems to work when I replace the placeholder {Name} with text like "Joe" but not when I try to replace the placeholder {Name} with the var "studentName"]

These are the sessions the student is scheduled to attend: {Sessions}  
[Not right.  This should be the actual sessions represented by the var "sessionName"]

Here's more information about the clinics themselves.

【问题讨论】:

    标签: javascript


    【解决方案1】:

    此行不正确,您实际上从未存储其他替换语句。

    var emailText = templateText.replace("{StudentName}","Joe");templateText.replace("{Sessions}","May - Session#1");templateText.replace("{ChargeAmount}","$500");

    应该是:

    var emailText = templateText.replace("{StudentName}","Joe").replace("{Sessions}","May - Session#1").replace("{ChargeAmount}","$500");

    【讨论】:

      【解决方案2】:

      正如 jmcgriz 所说,问题在于对.replace 的单独调用。链接它们可以解决您的问题。

      这里有一些虚拟代码显示了它的样子:

      function SendEmail() {
        // fetch these values as you need
        var studentName = 'Joe'
        var sessionName = "May - Session#1";
        var emailAddress = 'joe@joe.com'
        var chargeAmt = "$500";
      
        // This is what needed to change
        var emailText = templateText
           .replace("{StudentName}", studentName)
           .replace("{Sessions}", sessionName)
           .replace("{ChargeAmount}", chargeAmt);
      
        var subject = 'Junior Golf Clinic Registration Receipt';
        var message = emailText;
      
        MailApp.sendEmail(emailAddress, subject, message); 
      }
      
      SendEmail()
      <script>
      // Mocking MailApp for testing
      
      const MailApp = {
        sendEmail: (addr, subj, mess) => 
          console.log(`Sending "${addr}" email "${subj}"\n\n${mess}`)
      }
      
      const templateText = `
      This is the email body.
      
      This is the amount charged to credit card {ChargeAmount}.
      
      This is the student's name: {StudentName}
      
      These are the sessions the student is scheduled to attend: {Sessions}  
      
      Here's more information about the clinics themselves.`
      
      
      </script>

      【讨论】:

      • 好的。我正要放弃这个。我已经为此工作了几个小时,并且无法在我的一生中让这个程序起作用。这是我尝试运行代码时得到的详细报告:“类型错误:在对象中找不到函数替换这是电子邮件正文。这是向信用卡收取的金额:{Cost}。有什么办法吗?可以将此项目公开给某人,以便他们查看我的实际代码并告诉我到底做错了什么?
      • 该错误意味着很可能意味着您尝试从文档中获取的模板不是字符串,而是对象。以下是一些代码共享网站的列表:sitepoint.com/7-code-playgrounds。但我不知道他们中是否有任何方法可以与 Google Docs 集成。
      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2014-05-29
      • 2014-05-15
      • 1970-01-01
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多