gpt4 book ai didi

javascript - 涉及 ".replace"函数的语法问题

转载 作者:行者123 更新时间:2023-12-01 01:02:23 27 4
gpt4 key购买 nike

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

信用卡收费后,我想向客户生成一封确认电子邮件,其中包括学生的姓名、学生注册参加的类(class)以及从信用卡收取的金额。我的代码似乎工作正常,除了用我定义为变量的实际值替换“模板文本”中的大括号占位符(即 {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.

最佳答案

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

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

应该是:

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

关于javascript - 涉及 ".replace"函数的语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938583/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com