Skip to content

Commit

Permalink
Various wb$sheet_names related fixes and cleanups in wb_functions.R (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin committed Aug 15, 2023
1 parent 9c923c6 commit 3661327
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 146 deletions.
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ export(wb_get_named_regions)
export(wb_get_order)
export(wb_get_person)
export(wb_get_selected)
export(wb_get_sheet_name)
export(wb_get_sheet_names)
export(wb_get_sheet_visibility)
export(wb_get_tables)
export(wb_get_worksheet)
export(wb_grid_lines)
export(wb_group_cols)
export(wb_group_rows)
Expand Down Expand Up @@ -133,7 +131,6 @@ export(wb_ungroup_rows)
export(wb_unmerge_cells)
export(wb_update_table)
export(wb_workbook)
export(wb_ws)
export(write_comment)
export(write_data)
export(write_datatable)
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

* provide solve argument for `wb_merge_cells()`. This allows to solve cell intersecting regions. [733](https://github.com/JanMarvin/openxlsx2/pull/733)

## Breaking changes

* no longer exporting `wb_get_sheet_name()`
* deprecating `delete_data()` and minor improvements to `wb_clean_sheet()`
* removing `wb_get_worksheet()`, `wb_ws()`. These never worked as expected.

## Internal changes

* `wb_get_active_sheet()`, `wb_set_active_sheet()`, `wb_get_selected()` and `wb_set_selected()` are now wrapper functions.


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

Expand Down
46 changes: 46 additions & 0 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -3359,3 +3359,49 @@ wb_set_sheetview <- function(
... = ...
)
}

#' get and set table of sheets and their state as selected and active
#' @description Multiple sheets can be selected, but only a single one can be
#' active (visible). The visible sheet, must not necessarily be a selected
#' sheet.
#' @param wb a workbook
#' @returns a data frame with tabSelected and names
#' @export
#' @examples
#' wb <- wb_load(file = system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2"))
#' # testing is the selected sheet
#' wb_get_selected(wb)
#' # change the selected sheet to Sheet2
#' wb <- wb_set_selected(wb, "Sheet2")
#' # get the active sheet
#' wb_get_active_sheet(wb)
#' # change the selected sheet to Sheet2
#' wb <- wb_set_active_sheet(wb, sheet = "Sheet2")
#' @name select_active_sheet
wb_get_active_sheet <- function(wb) {
assert_workbook(wb)
wb$get_active_sheet()
}

#' @rdname select_active_sheet
#' @param sheet a sheet name of the workbook
#' @export
wb_set_active_sheet <- function(wb, sheet) {
# active tab requires a c index
assert_workbook(wb)
wb$clone()$set_active_sheet(sheet = sheet)
}

#' @name select_active_sheet
#' @export
wb_get_selected <- function(wb) {
assert_workbook(wb)
wb$get_selected()
}

#' @name select_active_sheet
#' @export
wb_set_selected <- function(wb, sheet) {
assert_workbook(wb)
wb$clone()$set_selected(sheet = sheet)
}
52 changes: 46 additions & 6 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -7025,12 +7025,55 @@ wbWorkbook <- R6::R6Class(
invisible(self)
},

#' description get person
#' @description description get person
#' @param name name
get_person = function(name = NULL) {
persons <- rbindlist(xml_attr(self$persons, "personList", "person"))
if (!is.null(name)) persons <- persons[persons$displayName == name, ]
persons
},

#' @description description get active sheet
get_active_sheet = function() {
at <- rbindlist(xml_attr(self$workbook$bookViews, "bookViews", "workbookView"))$activeTab
# return c index as R index
as.numeric(at) + 1
},

#' @description description set active sheet
#' @param sheet a worksheet
set_active_sheet = function(sheet = current_sheet()) {
sheet <- self$validate_sheet(sheet)
self$set_bookview(active_tab = sheet - 1L)
},

#' @description description get selected sheets
get_selected = function() {
len <- length(self$sheet_names)
sv <- vector("list", length = len)

for (i in seq_len(len)) {
sv[[i]] <- xml_node(self$worksheets[[i]]$sheetViews, "sheetViews", "sheetView")
}

# print(sv)
z <- rbindlist(xml_attr(sv, "sheetView"))
z$names <- self$get_sheet_names(escape = TRUE)
z
},

#' description set selected sheet
#' @param sheet a worksheet
set_selected = function(sheet = current_sheet()) {

sheet <- self$validate_sheet(sheet)

for (i in seq_along(self$sheet_names)) {
xml_attr <- ifelse(i == sheet, TRUE, FALSE)
self$worksheets[[i]]$set_sheetview(tabSelected = xml_attr)
}

invisible(self)
}

),
Expand Down Expand Up @@ -7259,10 +7302,6 @@ wbWorkbook <- R6::R6Class(
invisible(rlshp)
},

get_worksheet = function(sheet) {
self$worksheets[[private$get_sheet_index(sheet)]]
},

# this may ahve been removes
updateSharedStrings = function(uNewStr) {
## Function will return named list of references to new strings
Expand Down Expand Up @@ -7931,7 +7970,8 @@ lcr <- function(var) {
#' @param wb a [wbWorkbook] object
#' @param index Sheet name index
#' @return The sheet index
#' @export
#' @keywords internal
#' @noRd
wb_get_sheet_name <- function(wb, index = NULL) {
index <- index %||% seq_along(wb$sheet_names)

Expand Down
14 changes: 10 additions & 4 deletions R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -597,16 +597,22 @@ wbWorksheet <- R6::R6Class(
sel <- cc$r %in% dims
}

# TODO remaining things
# (pivot) tables that are no longer needed?
# (threaded) comments in that area?
# data validation / conditional formatting?
# other worksheet or otherwise related things?

if (numbers)
cc[sel & cc$c_t %in% c("n", ""),
cc[sel & cc$c_t %in% c("b", "e", "n", ""),
c("c_t", "v", "f", "f_t", "f_ref", "f_ca", "f_si", "is")] <- ""

if (characters)
cc[sel & cc$c_t %in% c("inlineStr", "s"),
c("v", "f", "f_t", "f_ref", "f_ca", "f_si", "is")] <- ""
cc[sel & cc$c_t %in% c("inlineStr", "s", "str"),
c("c_t", "c_ph", "v", "f", "f_t", "f_ref", "f_ca", "f_si", "is")] <- ""

if (styles)
cc[sel, c("c_s")] <- ""
cc[sel, c("c_s", "c_cm", "c_vm")] <- ""

self$sheet_data$cc <- cc

Expand Down
1 change: 1 addition & 0 deletions R/openxlsx2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ openxlsx2_celltype <- c(
#' @details
#' * [convertToExcelDate()] -> [convert_to_excel_date()]
#' * [wb_grid_lines()] -> [wb_set_grid_lines()]
#' * [delete_data()] -> [wb_clean_sheet()]
#' @seealso [.Deprecated]
#' @name openxlsx2-deprecated
NULL
91 changes: 4 additions & 87 deletions R/wb_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#' @param fill If `TRUE`, fills the dataframe with variables
#' @examples
#' dims_to_dataframe("A1:B2")
#' @export
#' @keywords internal
#' @export
dims_to_dataframe <- function(dims, fill = FALSE) {

if (grepl(";", dims)) {
Expand Down Expand Up @@ -55,8 +55,8 @@ dims_to_dataframe <- function(dims, fill = FALSE) {
#' @examples
#' df <- dims_to_dataframe("A1:D5;F1:F6;D8", fill = TRUE)
#' dataframe_to_dims(df)
#' @export
#' @keywords internal
#' @export
dataframe_to_dims <- function(df) {

# get continuous sequences of columns and rows in df
Expand Down Expand Up @@ -270,6 +270,8 @@ style_is_hms <- function(cellXfs, numfmt_date) {
#' @export
delete_data <- function(wb, sheet, cols, rows) {

.Deprecated(old = "delete_data", new = "wb_clean_sheet", package = "openxlsx2")

sheet_id <- wb_validate_sheet(wb, sheet)

cc <- wb$worksheets[[sheet_id]]$sheet_data$cc
Expand All @@ -287,88 +289,3 @@ delete_data <- function(wb, sheet, cols, rows) {
wb$worksheets[[sheet_id]]$sheet_data$cc <- cc

}


#' Get a worksheet from a `wbWorkbook` object
#'
#' @param wb a [wbWorkbook] object
#' @param sheet A sheet name or index
#' @returns A `wbWorksheet` object
#' @export
wb_get_worksheet <- function(wb, sheet) {
assert_workbook(wb)
wb$get_worksheet(sheet)
}

#' @rdname wb_get_worksheet
#' @export
wb_ws <- wb_get_worksheet

#' get and set table of sheets and their state as selected and active
#' @description Multiple sheets can be selected, but only a single one can be
#' active (visible). The visible sheet, must not necessarily be a selected
#' sheet.
#' @param wb a workbook
#' @returns a data frame with tabSelected and names
#' @export
#' @examples
#' wb <- wb_load(file = system.file("extdata", "openxlsx2_example.xlsx", package = "openxlsx2"))
#' # testing is the selected sheet
#' wb_get_selected(wb)
#' # change the selected sheet to Sheet2
#' wb <- wb_set_selected(wb, "Sheet2")
#' # get the active sheet
#' wb_get_active_sheet(wb)
#' # change the selected sheet to Sheet2
#' wb <- wb_set_active_sheet(wb, sheet = "Sheet2")
#' @name select_active_sheet
wb_get_active_sheet <- function(wb) {
assert_workbook(wb)
at <- rbindlist(xml_attr(wb$workbook$bookViews, "bookViews", "workbookView"))$activeTab
# return c index as R index
as.numeric(at) + 1
}

#' @rdname select_active_sheet
#' @param sheet a sheet name of the workbook
#' @export
wb_set_active_sheet <- function(wb, sheet) {
# active tab requires a c index
assert_workbook(wb)
sheet <- wb_validate_sheet(wb, sheet)
wb$clone()$set_bookview(active_tab = sheet - 1L)
}

#' @name select_active_sheet
#' @export
wb_get_selected <- function(wb) {

assert_workbook(wb)

len <- length(wb$sheet_names)
sv <- vector("list", length = len)

for (i in seq_len(len)) {
sv[[i]] <- xml_node(wb$worksheets[[i]]$sheetViews, "sheetViews", "sheetView")
}

# print(sv)
z <- rbindlist(xml_attr(sv, "sheetView"))
z$names <- wb$get_sheet_names(escape = TRUE)

z
}

#' @name select_active_sheet
#' @export
wb_set_selected <- function(wb, sheet) {

sheet <- wb_validate_sheet(wb, sheet)

for (i in seq_along(wb$sheet_names)) {
xml_attr <- ifelse(i == sheet, TRUE, FALSE)
wb$worksheets[[i]]$set_sheetview(tabSelected = xml_attr)
}

wb
}
4 changes: 2 additions & 2 deletions R/write.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ write_data2 <- function(
stop("name cannot look like a cell reference.")
}

sheet_name <- wb$sheet_names[[sheetno]]
if (grepl(" ", sheet_name)) sheet_name <- shQuote(sheet_name, "sh")
sheet_name <- wb$get_sheet_names(escape = TRUE)[[sheetno]]
if (grepl("[^A-Za-z0-9]", sheet_name)) sheet_name <- shQuote(sheet_name, "sh")

sheet_dim <- paste0(sheet_name, "!", dims)

Expand Down
1 change: 1 addition & 0 deletions man/openxlsx2-deprecated.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/select_active_sheet.Rd

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

Loading

0 comments on commit 3661327

Please sign in to comment.