Skip to content

Commit

Permalink
Improve error message in wb_add_data() when no worksheet (#942)
Browse files Browse the repository at this point in the history
* `devtools::build_readme()`

* Improve error message when empty workbook.

* Add hyperlink example with external URL.

* Add docs item to NEWS


* Update condition position + tests
  • Loading branch information
olivroy committed Feb 14, 2024
1 parent 7d6df46 commit f84c5ae
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -22,6 +22,8 @@

* Hyperlinks now use the color of the theme and the base size. [937](https://github.com/JanMarvin/openxlsx2/pull/937)

* `wb_add_data()` and `wb_add_data_table()` yield better error messages if attempting to add data to an empty workbook ([942](https://github.com/JanMarvin/openxlsx2/pull/942), @olivroy).

## Breaking changes

* Updating to themes. This includes updates to the default style `'Office Theme'` [899](https://github.com/JanMarvin/openxlsx2/pull/899)
Expand All @@ -36,6 +38,7 @@

* Updates to test files and findings by various linters ([922](https://github.com/JanMarvin/openxlsx2/pull/922), @olivroy)

* Improve [wbWorkbook documentation](https://janmarvin.github.io/openxlsx2/reference/wbWorkbook.html) to help disambiguate the chaining and piping methods ([936](https://github.com/JanMarvin/openxlsx2/pull/936), @olivroy and [928](https://github.com/JanMarvin/openxlsx2/pull/928)).

***************************************************************************

Expand Down
16 changes: 16 additions & 0 deletions R/class-workbook.R
Expand Up @@ -1290,6 +1290,14 @@ wbWorkbook <- R6::R6Class(
) {

standardize(...)
if (missing(x)) stop("`x` is missing")
if (length(self$sheet_names) == 0) {
stop(
"Can't add data to a workbook with no worksheet.\n",
"Did you forget to add a worksheet with `wb_add_worksheet()`?",
call. = FALSE
)
}

write_data(
wb = self,
Expand Down Expand Up @@ -1357,6 +1365,14 @@ wbWorkbook <- R6::R6Class(
) {

standardize(...)
if (missing(x)) stop("`x` is missing")
if (length(self$sheet_names) == 0) {
stop(
"Can't add data to a workbook with no worksheet.\n",
"Did you forget to add a worksheet with `wb_add_worksheet()`?",
call. = FALSE
)
}

write_datatable(
wb = self,
Expand Down
14 changes: 11 additions & 3 deletions R/helper-functions.R
@@ -1,14 +1,17 @@
# `create_hyperlink()` ---------------------------------------------------------
#' Create Excel hyperlink string
#'
#' Wrapper to create internal hyperlink string to pass to [write_formula()].
#' @description
#' Wrapper to create internal hyperlink string to pass to [wb_add_formula()].
#' Either link to external URLs or local files or straight to cells of local Excel sheets.
#'
#' Note that for an external URL, only `file` and `text` should be supplied.
#' You can supply `dims` to `wb_add_formula()` to control the location of the link.
#' @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
#' @param text display text
#' @param file Excel file name to point to. If NULL hyperlink is internal.
#' @param text Display text
#' @param file Hyperlink or Excel file name to point to. If `NULL`, hyperlink is internal.
#' @examples
#' wb <- wb_workbook()$
#' add_worksheet("Sheet1")$add_worksheet("Sheet2")$add_worksheet("Sheet3")
Expand Down Expand Up @@ -42,6 +45,11 @@
#' x <- create_hyperlink(text = "test.png", file = "D:/somepath/somepicture.png")
#' wb$add_formula(x = x, dims = "A7")
#'
#'
#' ## Link to an URL.
#' x <- create_hyperlink(text = "openxlsx2 website", file = "https://janmarvin.github.io/openxlsx2/")
#'
#' wb$add_formula(x = x, dims = "A8")
#' # if (interactive()) wb$open()
#'
#' @export
Expand Down
1 change: 0 additions & 1 deletion R/write.R
Expand Up @@ -743,7 +743,6 @@ write_data_table <- function(

## Input validating
assert_workbook(wb)
if (missing(x)) stop("`x` is missing")
assert_class(colNames, "logical")
assert_class(rowNames, "logical")
assert_class(withFilter, "logical")
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Expand Up @@ -55,7 +55,7 @@ install.packages('openxlsx2')

`openxlsx2` aims to be the swiss knife for working with the openxml spreadsheet formats xlsx, xlsm and (limited) xlsb (other formats of other spreadsheet software are not supported). We offer two different variants how to work with `openxlsx2`.

* The first one is to simply work with R objects. It is possible to read ([`read_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/wb_to_df.html) and write ([`write_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/write_xlsx.html)) data from and to files. We offer a number of options in the commands to support various features of the openxml format, including reading and writing named ranges and tables. Furthermore, there are several ways to read certain information of an openxml spreadsheet without having opened it in a spreadsheet software before, e.g. to get the contained sheet names or tables.
* The first one is to simply work with R objects. It is possible to read ([`read_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/wb_to_df.html)) and write ([`write_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/write_xlsx.html)) data from and to files. We offer a number of options in the commands to support various features of the openxml format, including reading and writing named ranges and tables. Furthermore, there are several ways to read certain information of an openxml spreadsheet without having opened it in a spreadsheet software before, e.g. to get the contained sheet names or tables.
* As a second variant `openxlsx2` offers the work with so called [`wbWorkbook`](https://janmarvin.github.io/openxlsx2/reference/wbWorkbook.html) objects. Here an openxml file is read into a corresponding `wbWorkbook` object ([`wb_load()`](https://janmarvin.github.io/openxlsx2/reference/wb_load.html)) or a new one is created ([`wb_workbook()`](https://janmarvin.github.io/openxlsx2/reference/wb_workbook.html)). Afterwards the object can be further modified using various functions. For example, worksheets can be added or removed, the layout of cells or entire worksheets can be changed, and cells can be modified (overwritten or rewritten). Afterwards the `wbWorkbook` objects can be written as openxml files and processed by suitable spreadsheet software.

Many examples how to work with `openxlsx2` are in our [manual pages](https://janmarvin.github.io/openxlsx2/reference/index.html) and in our [vignettes](https://janmarvin.github.io/openxlsx2/articles/). You can find them under:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -54,7 +54,7 @@ other spreadsheet software are not supported). We offer two different
variants how to work with `openxlsx2`.

- The first one is to simply work with R objects. It is possible to read
([`read_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/read_xlsx.html))
([`read_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/wb_to_df.html))
and write
([`write_xlsx()`](https://janmarvin.github.io/openxlsx2/reference/write_xlsx.html))
data from and to files. We offer a number of options in the commands
Expand Down
14 changes: 11 additions & 3 deletions man/create_hyperlink.Rd

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

17 changes: 16 additions & 1 deletion tests/testthat/test-write.R
Expand Up @@ -78,11 +78,26 @@ test_that("test options", {

})

test_that("missing x is caught early [246]", {
test_that("missing x is caught early [#246]", {
expect_error(
wb_workbook()$add_data(mtcars),
"`x` is missing"
)
expect_error(
wb_workbook()$add_data_table(mtcars),
"`x` is missing"
)
})

test_that("missing sheet is caught early (#942)", {
expect_error(
wb_workbook()$add_data(x = mtcars),
"no worksheet"
)
expect_error(
wb_workbook()$add_data_table(x = mtcars),
"no worksheet"
)
})

test_that("update_cells", {
Expand Down

0 comments on commit f84c5ae

Please sign in to comment.