简体   繁体   中英

Issue with links to sheets using the openxlsx package

I use the openxlsx package quite a lot and I'm very happy with it. I recently purchased a new computer with Windows 10 and kept using some of the programs I wrote myself. However, on the new machine I noticed a strange behavior when creating links to sheets in the Excel file itself. Consider the following minimal example:

library(openxlsx)

wbook <- createWorkbook()

addWorksheet(wb = wbook, sheetName = "Index")
addWorksheet(wb = wbook, sheetName = "Data")

writeFormula(wb = wbook, sheet = "Index", startCol = 1, startRow = 1, x = 
makeHyperlinkString(sheet = "Data", text = "Click"))

writeData(wb = wbook, sheet = "Data", x = data.frame(X = 1:10, Y = 11:20))

saveWorkbook(wb = wbook, file = "C:/temp/test.xlsx", overwrite = TRUE)

When I open the file, I see the following in the Index sheet:

在此处输入图像描述

Clicking on the link in the cell brings an error box in Excel, "Cannot open the specified file". I tried this under Arch Linux on the same machine as well and the result was the same. If I execute the following part of the above code

makeHyperlinkString(sheet = "Data", text = "Click")

the result in the console is as expected:

[1] "=HYPERLINK(\"#'Data'!A1\", \"Click\")"

For some reason the double and single quotes are converted to HTML &quote; and &apos; tags when the workbook is written on the disk.

The actual formula in the cell should be =HYPERLINK("#'Data',A1", "Click") and should be displayed In Excel as follows:

在此处输入图像描述

The latter one is produced on my previous computer under Windows 10. I also tried it under Arch Linux on my previous machine and it works just fine. I tried it on a third machine under Windows 10 without a problem. All of the computers I tried it on have the same language settings. Here is the output from the Sys.getlocale() command in R on all three computers under Windows and Linux:

[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"

What could be the reason for this behavior?

There has been an issue in version 4.2.4 which has already been reported and a bug fix should be part of the next release and is already part of the development version 4.2.4.9000.

Try:

remotes::install_github("ycphs/openxlsx")

I just tested it and the result looks as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM