diff --git a/NEWS.md b/NEWS.md index 3d64d32cd..affa3966d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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) @@ -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)). *************************************************************************** diff --git a/R/class-workbook.R b/R/class-workbook.R index d1c964ced..3f0d4ef4e 100644 --- a/R/class-workbook.R +++ b/R/class-workbook.R @@ -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, @@ -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, diff --git a/R/helper-functions.R b/R/helper-functions.R index 253b160e2..2f6d24c69 100644 --- a/R/helper-functions.R +++ b/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") @@ -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 diff --git a/R/write.R b/R/write.R index 795b9fc38..43353aa06 100644 --- a/R/write.R +++ b/R/write.R @@ -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") diff --git a/README.Rmd b/README.Rmd index 7d288157b..f5c05d95d 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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: diff --git a/README.md b/README.md index 13d6ccda7..bf3e5fef8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/man/create_hyperlink.Rd b/man/create_hyperlink.Rd index 53a2edcd9..bf4165cde 100644 --- a/man/create_hyperlink.Rd +++ b/man/create_hyperlink.Rd @@ -13,13 +13,16 @@ create_hyperlink(sheet, row = 1, col = 1, text = NULL, file = NULL) \item{col}{column number of letter for hyperlink to link to} -\item{text}{display text} +\item{text}{Display text} -\item{file}{Excel file name to point to. If NULL hyperlink is internal.} +\item{file}{Hyperlink or Excel file name to point to. If \code{NULL}, hyperlink is internal.} } \description{ -Wrapper to create internal hyperlink string to pass to \code{\link[=write_formula]{write_formula()}}. +Wrapper to create internal hyperlink string to pass to \code{\link[=wb_add_formula]{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 \code{file} and \code{text} should be supplied. +You can supply \code{dims} to \code{wb_add_formula()} to control the location of the link. } \examples{ wb <- wb_workbook()$ @@ -54,6 +57,11 @@ wb$add_formula(sheet = "Sheet1", x = x, dims = "A6") 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() } diff --git a/tests/testthat/test-write.R b/tests/testthat/test-write.R index a0e05628f..2e167f6f8 100644 --- a/tests/testthat/test-write.R +++ b/tests/testthat/test-write.R @@ -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", {