gpt4 book ai didi

r - 使用 R-package 'openxlsx' 从 Excel 表中提取链接

转载 作者:行者123 更新时间:2023-12-04 20:31:07 25 4
gpt4 key购买 nike

我正在使用 Excel 工作表,其中某些列包含超链接,这些超链接表示为与超链接指向的实际地址完全不同的文本。我想使用一些 R 代码来修改和子集 Excel 工作表,但保留超链接。我想我可以通过将这些超链接提取为索引字符向量然后使用 makeHyperlinkString() 将它们重新引入新的 Excel 文档来做到这一点。和 writeFormula()功能。但我无法弄清楚如何获取链接本身的向量。

万一这很重要,我的意图是对 Excel 工作表的 data.frame 版本而不是工作簿对象进行所有修改和子集化。

最佳答案

哦,现在我想我有你的问题。我以为只有普通的超链接而不是 Excel 超链接。
我认为这可能会帮助您获得超链接的矢量,尽管它有点困惑。

library(openxlsx)
pathtofile = "path to .xlsx file"

df1 <- read.xlsx(xlsxFile = pathtofile,
sheet = 1, skipEmptyRows = FALSE,
colNames = F, rowNames = F,
startRow = 1)

## Sheet or Tabelle
Sheet = "Sheet" ## Or "Tabelle"

## Get Names of rows from Hyperlink column
rowIndex <- sub(x = df1[,1], pattern = paste0("(#'",Sheet,"\\d'!)"), replacement = "")

## Get the Sheet, where Hyperlinks are saved
SheetName <- regmatches(df1[,1], regexpr(text = df1[,1], pattern = paste0("(",Sheet,"\\d)")))
## Extract only the Sheet number
SheetIndex <- as.numeric(sub(x = SheetName, pattern = Sheet, replacement = ""))

## Get the row Indexes as numeric
RowIndexNum <- as.numeric(regmatches(rowIndex, regexpr(text = rowIndex, pattern = "\\d")))
## Get the column name as character
RowIndexName <- sub(x = rowIndex, pattern = "\\d", "")
## Create uppercase Letters
myLetters <- toupper(letters[1:26])
## Convert Row Name (character) to numeric (based on alphabetical order)
RowIndexNameNum <- match(RowIndexName, myLetters)

## If Hyperlinks only in 1 Sheet or several sheets
if (length(unique(SheetIndex)) == 1) {
dfLinks <- read.xlsx(xlsxFile = pathtofile,
sheet = unique(SheetIndex),
skipEmptyRows = FALSE,
colNames = F, rowNames = F,
rows = RowIndexNum[1]:tail(RowIndexNum,1),
cols = unique(RowIndexNameNum),
startRow = 1
);
} else {
dfLinks <- data.frame()
for (i in unique(SheetIndex)){
dfTmp <- read.xlsx(xlsxFile = pathtofile,
sheet = i,
skipEmptyRows = FALSE,
colNames = F, rowNames = F,
rows = RowIndexNum[1]:tail(RowIndexNum,1),
cols = unique(RowIndexNameNum),
startRow = 1)
dfLinks <- rbind(dfLinks, dfTmp)
}
}

dfLinks
这是我的 Excel 文件的样子:
enter image description here

关于r - 使用 R-package 'openxlsx' 从 Excel 表中提取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533428/

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