[英]How to bold specific text using Google Apps Script?
I use Google Spreadsheets' built-in form functionality to build contact forms on my website. 我使用Google Spreadsheets的内置表单功能在我的网站上构建联系表单。
Now, consider this code: 现在,请考虑以下代码:
function sendFormByEmail(e)
{
var email = "team@example.com";
subject = e.namedValues["Subject"].toString();
message = "Time: " + e.namedValues["Timestamp"].toString() + "\n\n"
+ "Name: " + e.namedValues["Name"].toString() + "\n\n"
+ "Email: " + e.namedValues["Email Address"].toString() + "\n\n"
+ "Website: " + e.namedValues["Website"].toString() + "\n\n"
+ "Reason For Contacting?: " + e.namedValues["Reason For Contacting?"].toString() + "\n\n"
+ "Message: " + e.namedValues["Message"].toString() + "\n\n";
MailApp.sendEmail(email, subject, message);
}
It makes sure that I receive an email as soon as someone submits the form, the email's body has info. 它确保我在收到表单后立即收到电子邮件,电子邮件的正文包含信息。 like this (example):
像这样(例子):
Time: 2012/02/25 11:53
时间:2012/02/25 11:53
Name: John Davis
姓名:约翰戴维斯
Email: John@google.com
电子邮件:John@google.com
Website: http://google.com
网站: http : //google.com
Reason For Contacting?: Just wanted to chat with ya
联系原因?:只想和你聊聊
Message: It's been long.
消息:已经很久了。 Catch me tonight.
今晚抓住我。
So, now you should have a clear idea as to what the code does. 所以,现在你应该清楚地了解代码的作用。 The thing is, I want the output to look like this instead (ie, bold some text):
问题是,我希望输出看起来像这样(即粗体一些文本):
Time: 2012/02/25 11:53
时间: 2012/02/25 11:53
Name: John Davis
姓名:约翰戴维斯
Email: John@google.com
电子邮件: John@google.com
Website: http://google.com
网站: http : //google.com
Reason For Contacting?: Just wanted to chat with ya
联系原因?:只想和你聊聊
Message: It's been long.
消息:已经很久了。 Catch me tonight.
今晚抓住我。
How do I modify the code to achieve this? 如何修改代码来实现这一目标? Thanks.
谢谢。
MailApp.sendEmail can take htmlBody as advancedArgs. MailApp.sendEmail可以将htmlBody作为advancedArgs。 Descripted in here http://code.google.com/googleapps/appsscript/class_mailapp.html
这里描述了http://code.google.com/googleapps/appsscript/class_mailapp.html
You can send htmlBody like 你可以像发送htmlBody一样发送
function sendFormByEmail(e) {
var email = "team@example.com";
var subject = e.namedValues["Subject"].toString();
var msgHtml = "<b>Time</b>: " + e.namedValues["Timestamp"].toString() + "<br/>"
+ "<b>Name:</b> " + e.namedValues["Name"].toString() + "<br/>"
+ "<b>Email:</b> " + e.namedValues["Email Address"].toString() + "<br/>"
+ "<b>Website:</b> " + e.namedValues["Website"].toString() + "<br/>"
+ "<b>Reason For Contacting?:</b> " + e.namedValues["Reason For Contacting?"].toString() + "<br/>"
+ "<b>Message:</b> " + e.namedValues["Message"].toString() + "<br/>";
var msgPlain = msgHtml.replace(/\<br\/\>/gi, '\n').replace(/(<([^>]+)>)/ig, ""); // clear html tags and convert br to new lines for plain mail
MailApp.sendEmail(email, subject, msgPlain, { htmlBody: msgHtml });
}
The above one's for linebreaks. 以上是针对换行的。 Use this to separate them by paragraphs:
用它来分隔段落:
function sendFormByEmail(e) {
var email = "team@example.com";
var subject = e.namedValues["Subject"].toString();
var msgHtml = "<p>" + "<b>Time</b>: " + e.namedValues["Timestamp"].toString() + "</p>"
+ "<p>" + "<b>Name:</b> " + e.namedValues["Name"].toString() + "</p>"
+ "<p>" + "<b>Email:</b> " + e.namedValues["Email Address"].toString() + "</p>"
+ "<p>" + "<b>Website:</b> " + e.namedValues["Website"].toString() + "</p>"
+ "<p>" + "<b>Reason For Contacting?:</b> " + e.namedValues["Reason For Contacting?"].toString() + "</p>"
+ "<p>" + "<b>Message:</b> " + e.namedValues["Message"].toString() + "</p>";
var msgPlain = msgHtml.replace(/(<([^>]+)>)/ig, ""); // clear html tags for plain mail
MailApp.sendEmail(email, subject, msgPlain, { htmlBody: msgHtml });
}
I didnt try but it should work. 我没有尝试,但它应该工作。
A simple way that worked for me. 一种对我有用的简单方法。
function inlineImage() {
MailApp.sendEmail({
to: "Your@email.com",
subject: "Subject",
htmlBody: '<a href="http://google.com"> <b>Google</b></a> ' ,
});
}
The answer that Abe.S provided is simpler. Abe.S提供的答案更简单。 I combined it with part of arunes' edited answer .
我将它与arunes的部分编辑答案相结合。 It's now written in the way its_me requested in their comment to arunes :
它现在以his_me在评论arunes中的方式编写 :
function inlineImage() {
MailApp.sendEmail({
to: "team@example.com",
subject: e.namedValues["Subject"].toString(),
htmlBody: "<b>Time</b>: " + e.namedValues["Timestamp"].toString() + "<br/>"
+ "<b>Name:</b> " + e.namedValues["Name"].toString() + "<br/>"
+ "<b>Email:</b> " + e.namedValues["Email Address"].toString() + "<br/>"
+ "<b>Website:</b> " + e.namedValues["Website"].toString() + "<br/>"
+ "<b>Reason For Contacting?:</b> " + e.namedValues["Reason For Contacting?"].toString() + "<br/>"
+ "<b>Message:</b> " + e.namedValues["Message"].toString() + "<br/>",
});
}
I tested it with my own variables in place of all the "e.namedValues[..." for my application and it worked. 我用我自己的变量测试它代替了我的应用程序的所有“e.namedValues [...”并且它有效。 I'm still a novice, so I'm not sure why doing the "msgPlain" to "msgHtml" replacement step would be better.
我还是新手,所以我不确定为什么做“msgPlain”到“msgHtml”的替换步骤会更好。
By the way, I tried to write this as a comment, but I don't have enough points. 顺便说一下,我试着把它写成评论,但我没有足够的分数。 Though I guess what I wrote is technically the answer that its_me was looking for originally.
虽然我猜我写的内容在技术上是它最初寻找的答案。 Many thanks to both Abe.S and arunes for teaching me about both scripts.
非常感谢Abe.S和arunes教我两个剧本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.