Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bold specific text using Google Apps Script?

I use Google Spreadsheets' built-in form functionality to build contact forms on my website.

Now, consider this code:

function sendFormByEmail(e)
{

  var email = "[email protected]";

  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

Name: John Davis

Email: [email protected]

Website: 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 (i.e., bold some text):

Time: 2012/02/25 11:53

Name: John Davis

Email: [email protected]

Website: 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.

like image 869
its_me Avatar asked Feb 25 '12 07:02

its_me


1 Answers

MailApp.sendEmail can take htmlBody as advancedArgs. Descripted in here http://code.google.com/googleapps/appsscript/class_mailapp.html

You can send htmlBody like

function sendFormByEmail(e) {
    var email = "[email protected]";
    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 = "[email protected]";
    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.

like image 99
arunes Avatar answered Oct 14 '22 06:10

arunes