gpt4 book ai didi

css - 使用 R 从 Microsoft Outlook 发送电子邮件时的 Html 表输出格式

转载 作者:行者123 更新时间:2023-11-28 10:11:08 26 4
gpt4 key购买 nike

我正在尝试使用 htmlTable 包将数据框转换为 html 表,然后使用 RDCOMClient 包通过附加html 表格作为电子邮件的正文。我是 HTML 编码的新手,所以我不太熟悉如何使用 HTML 标记格式化表格。下面是我正在尝试做的一个例子。

# 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()

上面的代码有效,我什至得到了如下所示的电子邮件输出。

在 Microsoft Outlook 中使用上述代码发送的电子邮件的输出

image description

我的问题是,我该如何格式化这个表格?我希望输出采用漂亮的表格格式,行和列由行分隔。我可以添加输出中看到的列分隔线,但我无法添加行分隔线。我还想调整行和列之间的行间距,并将字体格式更改为 calibri 11。下面是我正在寻找的输出。

具有格式化的行和列的期望输出 image description

对于如何使用 htmlTable 包或任何其他变通方法实现此目的的任何帮助,我们将不胜感激。非常感谢。

更新:感谢@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()

Microsoft Outlook 中的最终输出是这样的。

Final Output In Microsoft Outlook

最佳答案

我还没有完成“r”,但语法看起来合乎逻辑,所以我会尝试一下解决方案:

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>")

基本上我已经在你的代码中添加了一些东西:

  • 包含您要发送的 HTML 文档样式的 html 文档的头部。
  • 正文的开始和结束标记。

<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>

从上面的 CSS 中,您应该能够根据自己的喜好设置边框颜色和字体。在电子邮件中呈现表格后,它应该显示类似于以下内容:

enter image description here

如果这对您有用,请告诉我。

干杯

关于css - 使用 R 从 Microsoft Outlook 发送电子邮件时的 Html 表输出格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47248728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com