Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[write_xlsx] restore first_active_{col,row} and first_{col,row} #903

Merged
merged 2 commits into from Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion NEWS.md
Expand Up @@ -8,9 +8,11 @@

* Character strings with XML content were not written correctly: `a <br/> b` was converted to something neither we nor spreadsheet software was able to decipher. [895](https://github.com/JanMarvin/openxlsx2/pull/895)

* Restore `first_active_row`/`first_active_col` and `first_col`/`first_row` functionality in `write_xlsx()`. [903](https://github.com/JanMarvin/openxlsx2/pull/903)

## Breaking changes

* Updating to themes. This includes updates to the default style `'Office Theme'`:
* Updating to themes. This includes updates to the default style `'Office Theme'` [899](https://github.com/JanMarvin/openxlsx2/pull/899)
* This includes switching to the new default font `'Aptos Narrow'`
* A new style `'Office 2013 - 2022 Theme'` was added

Expand Down
24 changes: 12 additions & 12 deletions R/write_xlsx.R
Expand Up @@ -389,35 +389,35 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) {

freeze_pane <- FALSE
firstActiveRow <- rep_len(1L, length.out = nSheets)
if ("firstActiveRow" %in% names(params)) {
firstActiveRow <- params$firstActiveRow
if ("first_active_row" %in% names(params)) {
firstActiveRow <- params$first_active_row
freeze_pane <- TRUE
if (length(firstActiveRow) != nSheets) {
firstActiveRow <- rep_len(firstActiveRow, length.out = nSheets)
}
}

firstActiveCol <- rep_len(1L, length.out = nSheets)
if ("firstActiveCol" %in% names(params)) {
firstActiveCol <- params$firstActiveCol
if ("first_active_col" %in% names(params)) {
firstActiveCol <- params$first_active_col
freeze_pane <- TRUE
if (length(firstActiveCol) != nSheets) {
firstActiveCol <- rep_len(firstActiveCol, length.out = nSheets)
}
}

firstRow <- rep_len(FALSE, length.out = nSheets)
if ("firstRow" %in% names(params)) {
firstRow <- params$firstRow
if ("first_row" %in% names(params)) {
firstRow <- params$first_row
freeze_pane <- TRUE
if (inherits(x, "list") && (length(firstRow) != nSheets)) {
firstRow <- rep_len(firstRow, length.out = nSheets)
}
}

firstCol <- rep_len(FALSE, length.out = nSheets)
if ("firstCol" %in% names(params)) {
firstCol <- params$firstCol
if ("first_col" %in% names(params)) {
firstCol <- params$first_col
freeze_pane <- TRUE
if (inherits(x, "list") && (length(firstCol) != nSheets)) {
firstCol <- rep_len(firstCol, length.out = nSheets)
Expand All @@ -430,10 +430,10 @@ write_xlsx <- function(x, file, as_table = FALSE, ...) {
wb <- wb_freeze_pane(
wb = wb,
sheet = i,
firstActiveRow = firstActiveRow[i],
firstActiveCol = firstActiveCol[i],
firstRow = firstRow[i],
firstCol = firstCol[i]
first_active_row = firstActiveRow[i],
first_active_col = firstActiveCol[i],
first_row = firstRow[i],
first_col = firstCol[i]
)
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/testthat/test-save.R
Expand Up @@ -358,3 +358,46 @@ test_that("write_xlsx() works", {
expect_equal(exp, got)

})

test_that("write_xlsx() freezing rows works", {

tmp <- temp_xlsx()

wb <- write_xlsx(list(mtcars, mtcars), tmp, firstRow = TRUE, firstCol = TRUE, tab_color = wb_color("green"))

# tabColor
exp <- c(
"<sheetPr><tabColor rgb=\"FF00FF00\"/></sheetPr>",
"<sheetPr><tabColor rgb=\"FF00FF00\"/></sheetPr>"
)
got <- c(
wb$worksheets[[1]]$sheetPr,
wb$worksheets[[2]]$sheetPr
)
expect_equal(exp, got)

# firstCol/firstRow
exp <- c(
"<pane ySplit=\"1\" xSplit=\"1\" topLeftCell=\"B2\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>",
"<pane ySplit=\"1\" xSplit=\"1\" topLeftCell=\"B2\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>"
)
got <- c(
wb$worksheets[[1]]$freezePane,
wb$worksheets[[2]]$freezePane
)
expect_equal(exp, got)

wb <- write_xlsx(list(mtcars, mtcars), tmp, firstActiveRow = 4, firstActiveCol = 3)

# firstActiveCol/firstActiveRow
exp <- c(
"<pane ySplit=\"3\" xSplit=\"2\" topLeftCell=\"C4\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>",
"<pane ySplit=\"3\" xSplit=\"2\" topLeftCell=\"C4\" activePane=\"bottomRight\" state=\"frozen\"/><selection pane=\"bottomRight\"/>"
)
got <- c(
wb$worksheets[[1]]$freezePane,
wb$worksheets[[2]]$freezePane
)
expect_equal(exp, got)

})