How to bold specific text using Google Apps Script?

前端 未结 3 1058
小蘑菇
小蘑菇 2020-12-16 07:17

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

Now, consider this code:

function sendFormByEmail(e)
{
         


        
相关标签:
3条回答
  • 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 = "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.

    0 讨论(0)
  • 2020-12-16 07:46

    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> ' ,
    
       });
     }
    
    0 讨论(0)
  • 2020-12-16 07:50

    The answer that Abe.S provided is simpler. I combined it with part of arunes' edited answer. It's now written in the way its_me requested in their comment to 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. I'm still a novice, so I'm not sure why doing the "msgPlain" to "msgHtml" replacement step would be better.

    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.

    0 讨论(0)
提交回复
热议问题