我正在使用xlsx制作包含多个工作表的工作簿,并且我想在每个工作簿中添加一列超链接,每行都不同。我按照参考手册
中的示例进行了操作wb <- createWorkbook()
sheet1 <- createSheet(wb, "Sheet1")
rows <- createRow(sheet1, 1:10) # 10 rows
cells <- createCell(rows, colIndex=1:8) # 8 columns
cell <- cells[[1,1]]
address <- "http://poi.apache.org/"
setCellValue(cell, "click me!")
addHyperlink(cell, address)
saveWorkbook(wb, "foo.xlsx")
这很简单,但我想将一个超链接矢量传递给addHyperlink()
,然后将一个显示名称向量传递给setCellValue()
。为此,我尝试了:
...
cell <- cells[[1:2,1]] #I am assuming that cells is a matrix like object with
#[[rows, columns]]
给出错误
Error in cells[[1:2, 1]] : attempt to select more than one element
有没有干净的方法来执行此操作,还是xlsx设置为循环制作cell
?
萨姆
答案 0 :(得分:0)
我想说使用for
循环:
library(xlsx)
wb <- createWorkbook()
sheet1 <- createSheet(wb, "Sheet1")
rows <- createRow(sheet1, 1:10) # 10 rows
cells <- createCell(rows, colIndex=1:8) # 8 columns
links <- c("http://stackoverflow.com/questions/20978461/add-many-hyperlinks-using-addhyperlink-in-xlsx",
"http://stackoverflow.com/users/1599501/sam")
names(links) <- c("add many hyperlinks using addHyperlink in xlsx",
"User: Sam")
for (row in 1:length(links)) {
setCellValue(cells[[row,1]], names(links)[row])
addHyperlink(cells[[row,1]], links[row])
}
saveWorkbook(wb, "foo.xlsx")
shell.exec("foo.xlsx")