Skip to content

Commit

Permalink
[misc] extend bookview handling (#1193)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMarvin authored Nov 30, 2024
1 parent 0b247cd commit c471256
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 9 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export(wb_get_active_sheet)
export(wb_get_base_colors)
export(wb_get_base_colours)
export(wb_get_base_font)
export(wb_get_bookview)
export(wb_get_cell_style)
export(wb_get_comment)
export(wb_get_creators)
Expand All @@ -116,6 +117,7 @@ export(wb_page_setup)
export(wb_protect)
export(wb_protect_worksheet)
export(wb_read)
export(wb_remove_bookview)
export(wb_remove_col_widths)
export(wb_remove_comment)
export(wb_remove_conditional_formatting)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Make non consecutive equal sized dims behave similar to non equal sized non consecutive dims. This makes `dims = "A1:A5,C1:D5"` behave similar to `dims = "A1,C1:D1,A2:A5,C2:D5"`.
* Improvements to the internal C++ code in `wb_add_data()` to avoid string copies. [1184](https://github.com/JanMarvin/openxlsx2/pull/1184)
This is a continuation of work started in [1177](https://github.com/JanMarvin/openxlsx2/pull/1177) to speedup `wb_add_data()`/`wb_add_data_table()`.
* Extend the `bookview` handling. It is now possible to add more than one `bookview` using `wb_set_bookview(use_view = 2L)` and to remove additional `bookview`s with `wb_remove_bookview()`. Available `bookview`s can be inspected with `wb_get_bookview()`.

## Fixes

Expand Down
39 changes: 37 additions & 2 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1666,8 +1666,25 @@ wb_set_base_colours <- wb_set_base_colors
wb_get_base_colours <- wb_get_base_colors


#' Set the workbook position, size and filter
#'
#' Get and Set the workbook position, size and filter
#' @name wb_set_bookview
#' @return A data frame with the bookview properties
#' @export
wb_get_bookview <- function(wb) {
assert_workbook(wb)
wb$get_bookview()
}

#' @name wb_set_bookview
#' @param remove_view You can remove views using index positions. This will only remove the view wont apply modifications.
#' @return The Workbook object
#' @export
wb_remove_bookview <- function(wb, remove_view = NULL) {
assert_workbook(wb)
wb$clone()$remove_bookview(remove_view = remove_view)
}

#' @rdname wb_set_bookview
#' @param wb A [wbWorkbook] object
#' @param active_tab activeTab
#' @param auto_filter_date_grouping autoFilterDateGrouping
Expand All @@ -1682,8 +1699,24 @@ wb_get_base_colours <- wb_get_base_colors
#' @param window_width windowWidth
#' @param x_window xWindow
#' @param y_window yWindow
#' @param use_view Which view to modify. Default is `1` (the first view).
#' @param ... additional arguments
#' @return The Workbook object
#' @examples
#' wb <- wb_workbook() %>% wb_add_worksheet()
#'
#' # set the first and second bookview (horizontal split)
#' wb <- wb %>%
#' wb_set_bookview(window_height = 17600, window_width = 15120, x_window = 15120, y_window = 760) %>%
#' wb_set_bookview(window_height = 17600, window_width = 15040, x_window = 0, y_window = 760, use_view = 2)
#'
#' wb %>% wb_get_bookview()
#'
#' # remove the first view
#' wb %>% wb_remove_bookview(remove_view = 1) %>% wb_get_bookview()
#'
#' # keep only the first view
#' wb %>% wb_remove_bookview(remove_view = -1) %>% wb_get_bookview()
#' @export
wb_set_bookview <- function(
wb,
Expand All @@ -1700,6 +1733,7 @@ wb_set_bookview <- function(
window_width = NULL,
x_window = NULL,
y_window = NULL,
use_view = 1L,
...
) {
assert_workbook(wb)
Expand All @@ -1717,6 +1751,7 @@ wb_set_bookview <- function(
window_width = window_width,
x_window = x_window,
y_window = y_window,
use_view = use_view,
... = ...
)
}
Expand Down
59 changes: 56 additions & 3 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -4239,7 +4239,46 @@ wbWorkbook <- R6::R6Class(

### book views ----

#' @description Set the book views
#' @description Get the book views
#' @return A dataframe with the bookview properties
get_bookview = function() {
wbv <- self$workbook$bookViews
if (is.null(wbv)) {
wbv <- xml_node_create("workbookView")
} else {
wbv <- xml_node(wbv, "bookViews", "workbookView")
}
rbindlist(xml_attr(wbv, "workbookView"))
},

#' @description Get the book views
#' @param remove_view remove_view
#' @return The `wbWorkbook` object
remove_bookview = function(remove_view = NULL) {

wbv <- self$workbook$bookViews

if (is.null(wbv)) {
return(invisible(self))
} else {
wbv <- xml_node(wbv, "bookViews", "workbookView")
}

if (!is.null(remove_view)) {
if (!is.integer(remove_view)) remove_view <- as.integer(remove_view)
# if there are three views, and 2 is removed, the indices are
# now 1, 2 and not 1, 3. removing -1 keeps only the first view
wbv <- wbv[-remove_view]
}

self$workbook$bookViews <- xml_node_create(
"bookViews",
xml_children = wbv
)

invisible(self)
},

#' @param active_tab activeTab
#' @param auto_filter_date_grouping autoFilterDateGrouping
#' @param first_sheet firstSheet
Expand All @@ -4253,6 +4292,7 @@ wbWorkbook <- R6::R6Class(
#' @param window_width windowWidth
#' @param x_window xWindow
#' @param y_window yWindow
#' @param use_view use_view
#' @return The `wbWorkbook` object
set_bookview = function(
active_tab = NULL,
Expand All @@ -4268,6 +4308,7 @@ wbWorkbook <- R6::R6Class(
window_width = NULL,
x_window = NULL,
y_window = NULL,
use_view = 1L,
...
) {

Expand All @@ -4281,8 +4322,20 @@ wbWorkbook <- R6::R6Class(
wbv <- xml_node(wbv, "bookViews", "workbookView")
}

wbv <- xml_attr_mod(
wbv,
if (use_view > length(wbv)) {
if (use_view == length(wbv) + 1L) {
wbv <- c(wbv, xml_node_create("workbookView"))
} else {
msg <- paste0(
"There is more than one workbook view missing.",
" Available: ", length(wbv), ". Requested: ", use_view
)
stop(msg, call. = FALSE)
}
}

wbv[use_view] <- xml_attr_mod(
wbv[use_view],
xml_attributes = c(
activeTab = as_xml_attr(active_tab),
autoFilterDateGrouping = as_xml_attr(auto_filter_date_grouping),
Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ autocompletion
bandedCols
bandedRows
blankRow
bookview
bool
calcChain
calculatedColumn
Expand Down
39 changes: 38 additions & 1 deletion man/wbWorkbook.Rd

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

35 changes: 33 additions & 2 deletions man/wb_set_bookview.Rd

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

12 changes: 11 additions & 1 deletion tests/testthat/test-class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,23 @@ test_that("wb_get_base_color() is a wrapper", {
expect_wrapper("get_base_colours", wb = wb)
})

# wb_set_bookview() -----------------------------------------------------------
# wb_get_bookview(),wb_remove_bookview(), wb_set_bookview() -------------------

test_that("wb_get_bookview() is a wrapper", {
expect_wrapper("get_bookview")
})

test_that("wb_set_bookview() is a wrapper", {
params <- list(activeTab = "1")
expect_wrapper("set_bookview", params = params)
})

test_that("wb_remove_bookview() is a wrapper", {
wb <- wb_workbook()$add_worksheet()$set_bookview()
params <- list(remove_view = 1L)
expect_wrapper("remove_bookview", params = params)
})

# wb_set_header_footer() ------------------------------------------------------

test_that("wb_set_header_footer() is a wrapper", {
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,46 @@ test_that("handling mips in docMetadata works", {
wb <- wb_load(tmp)
expect_equal(xml, wb$docMetadata)
})

test_that("using and removing secondary bookviews works", {
wb <- wb_workbook() %>% wb_add_worksheet()

# set the first and second bookview (horizontal split)
wb <- wb %>%
wb_set_bookview(window_height = 17600, window_width = 15120, x_window = 15120, y_window = 760) %>%
wb_set_bookview(window_height = 17600, window_width = 15040, x_window = 0, y_window = 760, use_view = 2)

exp <- structure(
list(windowHeight = c("17600", "17600"), windowWidth = c("15120", "15040"),
xWindow = c("15120", "0"), yWindow = c("760", "760")),
row.names = c(NA, 2L), class = "data.frame")
got <- wb %>% wb_get_bookview()
expect_equal(exp, got)

# remove the first view
exp <- structure(
list(windowHeight = c("17600"), windowWidth = c("15040"),
xWindow = c("0"), yWindow = c("760")),
row.names = c(NA, 1L), class = "data.frame")
got <- wb %>% wb_remove_bookview(remove_view = 1) %>% wb_get_bookview()
expect_equal(exp, got)

# keep only the first view
exp <- structure(
list(windowHeight = c("17600"), windowWidth = c("15120"),
xWindow = c("15120"), yWindow = c("760")),
row.names = c(NA, 1L), class = "data.frame")
got <- wb %>% wb_remove_bookview(remove_view = -1) %>% wb_get_bookview()
expect_equal(exp, got)

wb <- wb_workbook() %>% wb_add_worksheet()
exp <- structure(list(), row.names = c(NA, 1L), names = character(0), class = "data.frame")
got <- wb %>% wb_remove_bookview(remove_view = 1) %>% wb_get_bookview()
expect_equal(exp, got)

expect_error(
wb <- wb_workbook() %>% wb_add_worksheet() %>% wb_set_bookview(use_view = 3),
"There is more than one workbook view missing. Available: 1. Requested: 3"
)

})

0 comments on commit c471256

Please sign in to comment.