Html table output formatting when sending email from Microsoft Outlook using R

与世无争的帅哥 提交于 2019-12-23 16:43:36

问题


I am trying to convert a dataframe into an html table using the htmlTable package and then send an email using Microsoft Outlook as the email client using RDCOMClient package by appending the html table as the body of the email. I am new to HTML coding so I am not very familiar with how to format the table with HTML tags. Below is an example of what I am trying to do.

# Library to send email from Microsoft Outlook
library(RDCOMClient)
# Library to create HTML table
library(htmlTable)

# Reading data from inbuilt 'mtcars' dataset
x <- head(mtcars)

# Create HTML table
y <- htmlTable(x,
               rnames = FALSE,
               caption="This is from htmlTable package",
               align = paste(rep("c", ncol(x)), collapse = "|"),
               align.header = paste(rep("c", ncol(x)), collapse = "|"))

# add the table as body of the email
body <- paste0("<html>", y, "</html>")

## init com api
OutApp <- COMCreate("Outlook.Application")
## create an email 
outMail = OutApp$CreateItem(0)
## configure  email parameter 
outMail[["To"]] = "test@test"
outMail[["subject"]] = "TEST"
outMail[["HTMLbody"]] = body
## send it                     
outMail$Send()

This above code works and I even get the email output as shown below.

Output from email sent using above code in Microsoft Outlook

My question is, how to I format this table? I want the output to be in a nice table format with row and columns separated by lines. I can add column separator lines as seen in the output, but I am unable to add row separator lines. I also want to adjust the line spacing between the rows and columns, and change the formatting of the fonts to calibri 11. Below is the output that I am looking for.

Desired Output with rows and columns formatted

Any help on how I can achieve this using htmlTable package or any other workaround would be really appreciated. Thanks a lot in advance.

UPDATE: Below is the solution, thanks to valuable inputs provided by @Syfer.

library(RDCOMClient)
library(htmlTable)

x <- head(mtcars)
html_y <- htmlTable(x, rnames = FALSE)

body <- paste0("<html><head>
               <style>
               body{font-family:Arial, \"sans-serif\";}
               table{border-left:1px solid #000000;border-top:1px solid #000000;}
               table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:12px; font-weight:normal;}
               table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:14px;}
               </style>
               </head><body>",
               html_y, 
               "</body></html>")

OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "test@test.com"
outMail[["subject"]] = "TEST EMAIL"
outMail[["HTMLbody"]] = body
outMail$Send()

Final output in Microsoft Outlook likes this.


回答1:


I havent done 'r' but the syntax looks logical so I will take a stab at the solution:

body <- paste0("<html><head><style>body{font-family:Arial, "sans-serif";}table{border-left:1px solid #000000;border-top:1px solid #000000;}table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:normal;}table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:bold;}</style></head><body>", y, "</body></html>")

Basically i have added a few things to your code:

  • head of the html document which contains the style for the HTML document that you will sending out.
  • start and end tag for body.

<style>
    body{font-family:Arial, "sans-serif"}
    table{border-left:1px solid #000000;border-top:1px solid #000000;}
    table td{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:normal;}
    table th{border-right:1px solid #000000;border-bottom:1px solid #000000;font-size:16px; font-weight:bold;}
</style>

From the above CSS you should be able to the border colors and fonts to suit your liking. Once the table is rendered in emails it should show something similar to:

Let me know if that worked for you.

Cheers



来源:https://stackoverflow.com/questions/47248728/html-table-output-formatting-when-sending-email-from-microsoft-outlook-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!