Skip to content

Commit a150be0

Browse files
authored
fix tab selection (#506)
* fix tab selection * update NEWS and add test
1 parent 574af7c commit a150be0

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
* Fix reading and saving workbooks with multiple slicers per sheet. [505](https://github.com/JanMarvin/openxlsx2/pull/505)
1818

19+
* Fix tab selection always selecting the first sheet since #303. [506](https://github.com/JanMarvin/openxlsx2/pull/506)
20+
1921
## Breaking changes
2022

2123
* Do not export `write_data2()` anymore. This was used in development in the early stages of the package and should not be used directly anymore.

R/class-workbook.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6474,9 +6474,8 @@ wbWorkbook <- R6::R6Class(
64746474
)
64756475

64766476
# Failsafe: hidden sheet can not be selected.
6477-
self$worksheets[[visible_sheet_index]]$set_sheetview(tabSelected = TRUE)
6478-
if (nSheets > 1) {
6479-
for (i in setdiff(seq_len(nSheets), visible_sheet_index)) {
6477+
if (any(hidden)) {
6478+
for (i in which(hidden)) {
64806479
self$worksheets[[i]]$set_sheetview(tabSelected = FALSE)
64816480
}
64826481
}

tests/testthat/test-loading_workbook.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,17 @@ test_that("vml target is updated on load", {
368368
expect_equal(exp, got)
369369

370370
})
371+
372+
test_that("sheetView is not switched", {
373+
374+
wb <- wb_load(file = system.file("extdata", "loadExample.xlsx", package = "openxlsx2"))
375+
376+
exp <- "<sheetViews><sheetView workbookViewId=\"0\"/></sheetViews>"
377+
got <- wb$worksheets[[1]]$sheetViews
378+
expect_equal(exp, got)
379+
380+
exp <- "<sheetViews><sheetView tabSelected=\"1\" workbookViewId=\"0\"/></sheetViews>"
381+
got <- wb$worksheets[[2]]$sheetViews
382+
expect_equal(exp, got)
383+
384+
})

0 commit comments

Comments
 (0)