简体   繁体   English

如何使用Google Apps脚本加粗特定文字?

[英]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.Sarunes教我两个剧本。

暂无
暂无

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

相关问题 使用Google脚本将文本更改为粗体或添加图像 - Change Text to Bold or Add Images using Google Script 使用 Apps Script 在 Google Doc 中检索特定文本字符串上的超链接 - Retrieve the hyperlink on a specific text string within Google Doc using Apps Script 如何获取Google Apps脚本的文本内容? - How to get text content of Google Apps Script? 如何在谷歌应用程序脚本中输出文本文件 - How to output a text file in google apps script 使用.setBold()时,为什么Google Apps脚本会抛出“ ReferenceError:未定义粗体”? - Why does Google Apps Script throw 'ReferenceError: “bold” is not defined' when using .setBold()? 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 如何使用 Google Apps 脚本为每个活动创建(在特定的 if 语句中)添加唯一的 Google Meet 链接 - How to add a unique Google Meet link for every event creation (within a specific if statement) using Google Apps Script 如何使用谷歌应用程序脚本获取特定列谷歌电子表格中的下一个空行 - How to get the next empty row in a specific column google spreadsheet using google apps script 如何使用Google Apps脚本将Google文档文本导入电子邮件草稿 - How to Import Google doc text into Email draft using Google Apps Script 如何使用 Google Apps 脚本在 Google 文档中的表格单元格内复制和编辑文本数据 - How to copy and edit text data inside a table cell in Google Documents using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM