Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin authored Sep 3, 2021
2 parents 9469b18 + 47df27e commit 1652a67
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 82 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export(createStyle)
export(createWorkbook)
export(dataValidation)
export(deleteData)
export(deleteNamedRegion)
export(freezePane)
export(getBaseFont)
export(getCellRefs)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* `write.xlsx()` now throws an error if it doesn't have write permissions ([#190](https://github.com/ycphs/openxlsx/issues/190))
* `Workbook$show()` no longer fails when called in a 0 sheet workbook([#240](https://github.com/ychps/openxlsx/issues/240))
* `read.xlsx()` again accepts `.xlsm` files ([#205](https://github.com/ychps/openxlsx/issues/205), [#209](https://github.com/ychps/openxlsx/issues/209))
* `makeHyperlinkString()` does no longer require a sheet argument ([#57](https://github.com/ychps/openxlsx/issues/57), [#58](https://github.com/ychps/openxlsx/issues/58))

# openxlsx 4.2.4

Expand Down
34 changes: 22 additions & 12 deletions R/helperFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#' @name makeHyperlinkString
#' @title create Excel hyperlink string
#' @description Wrapper to create internal hyperlink string to pass to writeFormula()
#' @description Wrapper to create internal hyperlink string to pass to writeFormula(). Either link to external urls or local files or straight to cells of local Excel sheets.
#' @param sheet Name of a worksheet
#' @param row integer row number for hyperlink to link to
#' @param col column number of letter for hyperlink to link to
Expand Down Expand Up @@ -74,6 +74,11 @@
#' startRow = 10, startCol = 1,
#' x = '=HYPERLINK(\\"[C:/Users]\\", \\"Link to an external file\\")'
#' )
#'
#' ## Link to internal file
#' x = makeHyperlinkString(text = "test.png", file = "D:/somepath/somepicture.png")
#' writeFormula(wb, "Sheet1", startRow = 11, startCol = 1, x = x)
#'
#' \dontrun{
#' saveWorkbook(wb, "internalHyperlinks.xlsx", overwrite = TRUE)
#' }
Expand All @@ -83,18 +88,23 @@ makeHyperlinkString <- function(sheet, row = 1, col = 1, text = NULL, file = NUL
options("OutDec" = ".")
on.exit(expr = options("OutDec" = od), add = TRUE)


cell <- paste0(int2col(col), row)
if (!is.null(file)) {
dest <- sprintf("[%s]'%s'!%s", file, sheet, cell)
} else {
dest <- sprintf("#'%s'!%s", sheet, cell)
}

if (is.null(text)) {
str <- sprintf("=HYPERLINK(\"%s\")", dest)
if (missing(sheet)) {
if (!missing(row) || !missing(col)) warning("Option for col and/or row found, but no sheet was provided.")

str <- sprintf("=HYPERLINK(\"%s\", \"%s\")", file, text)
} else {
str <- sprintf("=HYPERLINK(\"%s\", \"%s\")", dest, text)
cell <- paste0(int2col(col), row)
if (!is.null(file)) {
dest <- sprintf("[%s]'%s'!%s", file, sheet, cell)
} else {
dest <- sprintf("#'%s'!%s", sheet, cell)
}

if (is.null(text)) {
str <- sprintf("=HYPERLINK(\"%s\")", dest)
} else {
str <- sprintf("=HYPERLINK(\"%s\", \"%s\")", dest, text)
}
}

return(str)
Expand Down
48 changes: 39 additions & 9 deletions R/wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2705,14 +2705,16 @@ names.Workbook <- function(x) {


#' @name createNamedRegion
#' @title Create a named region.
#' @description Create a named region
#' @title Create / delete a named region.
#' @description Create / delete a named region
#' @author Alexander Walker
#' @param wb A workbook object
#' @param sheet A name or index of a worksheet
#' @param rows Numeric vector specifying rows to include in region
#' @param cols Numeric vector specifying columns to include in region
#' @param name Name for region. A character vector of length 1. Note region names musts be case-insensitive unique.
#' @param overwrite Boolean. Overwrite if exists ? Default to FALSE
#'
#' @details Region is given by: min(cols):max(cols) X min(rows):max(rows)
#' @export
#' @seealso \code{\link{getNamedRegions}}
Expand Down Expand Up @@ -2743,14 +2745,20 @@ names.Workbook <- function(x) {
#' getNamedRegions(wb) ## From Workbook object
#' getNamedRegions(out_file) ## From xlsx file
#'
#' ## delete one
#' deleteNamedRegion(wb = wb, name = "iris2")
#' getNamedRegions(wb)
#'
#' ## read named regions
#' df <- read.xlsx(wb, namedRegion = "iris")
#' head(df)
#'
#' df <- read.xlsx(out_file, namedRegion = "iris2")
#' head(df)
#' }
createNamedRegion <- function(wb, sheet, cols, rows, name) {
#'
#' @rdname NamedRegion
createNamedRegion <- function(wb, sheet, cols, rows, name, overwrite = FALSE) {
od <- getOption("OutDec")
options("OutDec" = ".")
on.exit(expr = options("OutDec" = od), add = TRUE)
Expand All @@ -2775,13 +2783,16 @@ createNamedRegion <- function(wb, sheet, cols, rows, name) {
ex_names <- regmatches(wb$workbook$definedNames, regexpr('(?<=name=")[^"]+', wb$workbook$definedNames, perl = TRUE))
ex_names <- tolower(replaceXMLEntities(ex_names))

if (tolower(name) %in% ex_names) {
stop(sprintf("Named region with name '%s' already exists!", name))
} else if (grepl("^[A-Z]{1,3}[0-9]+$", name)) {
if (tolower(name) %in% ex_names & !overwrite) {
stop(sprintf("Named region with name '%s' already exists! Use overwrite = TRUE if you want to replace it", name))
} else if (tolower(name) %in% ex_names & overwrite) {
wb$workbook$definedNames <- wb$workbook$definedNames[!ex_names %in% tolower(name)]
}

if (grepl("^[A-Z]{1,3}[0-9]+$", name)) {
stop("name cannot look like a cell reference.")
}



cols <- round(cols)
rows <- round(rows)

Expand All @@ -2800,7 +2811,25 @@ createNamedRegion <- function(wb, sheet, cols, rows, name) {
}



#' @export
#' @rdname NamedRegion
deleteNamedRegion <- function(wb, name) {

if (!"Workbook" %in% class(wb)) {
stop("First argument must be a Workbook.")
}

ex_names <- regmatches(wb$workbook$definedNames, regexpr('(?<=name=")[^"]+', wb$workbook$definedNames, perl = TRUE))
ex_names <- tolower(replaceXMLEntities(ex_names))

if (tolower(name) %in% ex_names) {
wb$workbook$definedNames <- wb$workbook$definedNames[!ex_names %in% tolower(name)]
} else {
warning(sprintf("Cannot find Named region with name '%s'", name))
}

invisible(0)
}



Expand Down Expand Up @@ -2845,6 +2874,7 @@ createNamedRegion <- function(wb, sheet, cols, rows, name) {
#' df <- read.xlsx(out_file, namedRegion = "iris2")
#' head(df)
#' }
#'
getNamedRegions <- function(x) {
UseMethod("getNamedRegions", x)
}
Expand Down
2 changes: 1 addition & 1 deletion R/writeData.R
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ writeData <- function(
#' @param xy An alternative to specifying \code{startCol} and
#' \code{startRow} individually. A vector of the form
#' \code{c(startCol, startRow)}.
#' @seealso \code{\link{writeData}}
#' @seealso \code{\link{writeData}} \code{\link{makeHyperlinkString}}
#' @export writeFormula
#' @rdname writeFormula
#' @examples
Expand Down
16 changes: 13 additions & 3 deletions man/createNamedRegion.Rd → man/NamedRegion.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/getNamedRegions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/makeHyperlinkString.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/writeFormula.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions revdep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
|collate |C.UTF-8 |
|ctype |C.UTF-8 |
|tz |UTC |
|date |2021-08-27 |
|date |2021-09-02 |

# Dependencies

Expand All @@ -29,12 +29,12 @@
|:--------------|:-------|:-----|:-------|:----|
|appreci8R |? | | | |
|artMS |? | | | |
|cbaf |? | | | |
|CHRONOS |? | | | |
|CNVPanelizer |? | | | |
|DAPAR |? | | | |
|ddPCRclust |? | | | |
|emdi |? | | | |
|fasstr |? | | | |
|fedup |? | | | |
|GA4GHshiny |? | | | |
|hypeR |1.8.0 |1 | | |
Expand All @@ -47,7 +47,7 @@
|PloGO2 |? | | | |
|rbiom |? | | | |
|RIPAT |? | | | |
|sangeranalyseR |? | | | |
|sangeranalyseR |1.2.0 |1 |1 |2 |
|SEtools |? | | | |
|sigFeature |? | | | |
|stplanr |? | | | |
Expand Down
2 changes: 1 addition & 1 deletion revdep/cran.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## revdepcheck results

We checked 122 reverse dependencies (0 from CRAN + 122 from Bioconductor), comparing R CMD check results across CRAN and dev versions of this package.
We checked 123 reverse dependencies (1 from CRAN + 122 from Bioconductor), comparing R CMD check results across CRAN and dev versions of this package.

* We saw 0 new problems
* We failed to check 0 packages
Expand Down
Loading

0 comments on commit 1652a67

Please sign in to comment.