Openxlsx超链接输出在Excel中显示 [英] Openxlsx hyperlink output display in Excel
问题描述
我正在尝试将一个具有两列的excel工作表放入数据框中,
I am trying to take in a data frame an excel sheet that has two columns,
列A包含商店名称B列包含这些商店的URL.
Column A contains names of stores Column B contains the URL of those stores.
我想采用A列,并使其成为可单击的超链接,因此它是通向商店网站的超链接,而不是纯文本.
I would like to take Column A and make it a clickable hyperlink so instead of plain text, it is a hyperlink to the store website.
我尝试使用openxlsx软件包生成正确的输出.
I have attempted to use openxlsx package to generate the correct output.
我尝试使用以下代码段.
I have attempted to use the following code snip.
x <- c("https://www.google.com", "https://www.google.com.au")
names(x) <- c("google", "google Aus")
class(x) <- "hyperlink"
writeData(wb, sheet = 1, x = x, startCol = 10)
来自类似性质的帖子. https://stackoverflow.com/a/48973469/11958444
which comes from this post of a similar nature. https://stackoverflow.com/a/48973469/11958444
但是我的问题是当我替换代码的适当部分时,例如:
My problem however is when I replace the appropriate parts of the code e.g.:
x <- df$b
names(x) <- df$a
class(x) <- "hyperlink"
writeData(wb, sheet = 1, x = x, startCol = 10)
不是给我一列以商店名称作为输出的超链接,而是给我整个URL作为输出.我的代码中缺少什么吗?
instead of giving me a column of hyperlinks that has the store name as the output, it gives me the entire URL as the output. Is there something I am missing from my code?
我得到的输出具有可单击的链接,但是它没有打印出带有名称的URL,而是仅打印出URL.
I get an output that has a clickable link, but instead of the URL appearing with the name, it instead just prints out the URL.
推荐答案
使用 openxlsx
的方法:
library(openxlsx)
library(dplyr)
# create sample data
df <- data.frame(
site_name = c("Zero Hedge", "Free Software Foundation"),
site_url = c("https://www.zerohedge.com", "https://www.fsf.org")
)
# add new column that manually constructs Excel hyperlink formula
# note backslash is required for quotes to appear in Excel
df <- df %>%
mutate(
excel_link = paste0(
"HYPERLINK(\"",
site_url,
"\", \"",
site_name,
"\")"
)
)
# specify column as formula per openxlsx::writeFormula option #2
class(df$excel_link) <- "formula"
# create and write workbook
wb <- createWorkbook()
addWorksheet(wb, "df_sheet")
writeData(wb, "df_sheet", df)
saveWorkbook(wb, "wb.xlsx", overwrite = TRUE)
这篇关于Openxlsx超链接输出在Excel中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!